Skip to content

File include/brisk/graphics/Canvas.hpp


Canvas class

Canvas

CanvasA high-level class for rendering graphical elements on a canvas.

The Canvas class provides an interface for drawing various shapes, text, and images onto a canvas. It extends the functionality of BasicCanvas by adding state management and more sophisticated drawing operations.


BlurRadius class

BlurRadius

Texture class

Texture

TextureRepresents a textured fill pattern for drawing operations.

The Texture structure holds an image and a transformation matrix. The image is used as a texture, and the matrix defines how the texture is transformed when applied to a surface.

image variable (Texture::image)

Rc<Image> image

The image used as the texture.

matrix variable (Texture::matrix)

Matrix matrix

The transformation matrix applied to the texture.

mode variable (Texture::mode)

SamplerMode mode = SamplerMode::Wrap

The sampler mode (Clamp or Wrap).

blurRadius variable (Texture::blurRadius)

BlurRadius blurRadius = 0.f

The radius of the blur applied to the image.


Composition class

Composition

Paint typedef

Paint = std::variant<ColorW, Gradient, Texture>

PaintA type representing various fill and stroke styles.

Paint can hold one of several types representing different kinds of paints: a solid color (ColorW), a gradient (Gradient), or a texture (Texture).


PaintAndTransform class (Internal::PaintAndTransform)

PaintAndTransform

CanvasFlags enum

enum class CanvasFlags

Enum class defining flags for canvas rendering options.


Canvas class

Canvas

CanvasA high-level class for rendering graphical elements on a canvas.

The Canvas class provides an interface for drawing various shapes, text, and images onto a canvas. It extends the functionality of BasicCanvas by adding state management and more sophisticated drawing operations.

Canvas function (Canvas::Canvas)

explicit Canvas(RenderContext &context,
                CanvasFlags flags = CanvasFlags::Default)

Constructs a Canvas object using a RenderContext.
Param context The rendering context used for drawing operations.

renderContext function (Canvas::renderContext)

RenderContext &renderContext() const

Retrieves the render context for the canvas.
Returns Reference to the RenderContext object.

flags function (Canvas::flags)

CanvasFlags flags() const noexcept

Retrieves the canvas rendering flags.
Returns The current CanvasFlags value.

strokePath function (Canvas::strokePath)

void strokePath(const Path &path, const Paint &strokePaint,
                const StrokeParams &params,
                const Matrix &matrix,
                const PreparedPath &clipPath,
                Rectangle scissor, float opacity)

Draws a stroked path on the canvas with specified parameters.

This stateless function renders a path outline using the provided stroke paint, parameters, transformation matrix, clipping path, and opacity. It does not modify or access the canvas state.
Param path The path to be stroked.
Param strokePaint The paint used for stroking the path.
Param params The stroke parameters (e.g., width, cap, join).
Param matrix The transformation matrix to apply to the path and clipPath.
Param clipPath The clipping path in canvas coordinates.
Param opacity The opacity value (0.0 to 1.0) for rendering.

fillPath function (Canvas::fillPath)

void fillPath(const Path &path, const Paint &fillPaint,
              const FillParams &fillParams,
              const Matrix &matrix,
              const PreparedPath &clipPath,
              Rectangle scissor, float opacity)

Fills a path on the canvas with specified parameters.

This stateless function fills a path using the provided fill paint, parameters, transformation matrix, clipping path, and opacity. It does not modify or access the canvas state.
Param path The path to be filled.
Param fillPaint The paint used for filling the path.
Param fillParams The fill parameters (e.g., fill rule).
Param matrix The transformation matrix to apply to the path.
Param clipPath The clipping path in canvas coordinates.
Param opacity The opacity value (0.0 to 1.0) for rendering.

getStrokePaint function (Canvas::getStrokePaint)

const Paint &getStrokePaint() const

Retrieves the current stroke paint configuration.
Returns The current stroke Paint struct.

setStrokePaint function (Canvas::setStrokePaint)

void setStrokePaint(Paint paint)

Sets the stroke paint configuration.
Param paint The Paint struct to use for strokes.

getFillPaint function (Canvas::getFillPaint)

const Paint &getFillPaint() const

Retrieves the current fill paint configuration.
Returns The current fill Paint struct.

setFillPaint function (Canvas::setFillPaint)

void setFillPaint(Paint paint)

Sets the fill paint configuration.
Param paint The Paint struct to use for fills.

getStrokeWidth function (Canvas::getStrokeWidth)

float getStrokeWidth() const

Retrieves the current stroke width.
Returns The stroke width in pixels.

setStrokeWidth function (Canvas::setStrokeWidth)

void setStrokeWidth(float width)

Sets the stroke width.
Param width The width of the stroke in pixels.

getOpacity function (Canvas::getOpacity)

float getOpacity() const

Retrieves the current opacity level.
Returns The opacity level as a float, where 1.0 is fully opaque and 0.0 is fully transparent.

setOpacity function (Canvas::setOpacity)

void setOpacity(float opacity)

Sets the opacity level.
Param opacity The opacity level, where 1.0 is fully opaque and 0.0 is fully transparent.

getStrokeColor function (Canvas::getStrokeColor)

ColorW getStrokeColor() const

Retrieves the current stroke color.
Returns The stroke color as a ColorW struct.

setStrokeColor function (Canvas::setStrokeColor)

void setStrokeColor(ColorW color)

Sets the stroke color.
Param color The ColorW struct representing the stroke color.

getFillColor function (Canvas::getFillColor)

ColorW getFillColor() const

Retrieves the current fill color.
Returns The fill color as a ColorW struct.

setFillColor function (Canvas::setFillColor)

void setFillColor(ColorW color)

Sets the fill color.
Param color The ColorW struct representing the fill color.

getMiterLimit function (Canvas::getMiterLimit)

float getMiterLimit() const

Retrieves the current miter limit for strokes.
Returns The miter limit as a float.

setMiterLimit function (Canvas::setMiterLimit)

void setMiterLimit(float limit)

Sets the miter limit for strokes.

The miter limit controls the maximum length of the miter join between stroke segments. When the miter limit is exceeded, a bevel join is used instead.
Param limit The miter limit as a float.

getFillRule function (Canvas::getFillRule)

FillRule getFillRule() const

Retrieves the current fill rule used for determining the interior of shapes.
Returns The FillRule enumeration value.

setFillRule function (Canvas::setFillRule)

void setFillRule(FillRule fillRule)

Sets the fill rule used for determining the interior of shapes.
Param fillRule The FillRule enumeration value.

getJoinStyle function (Canvas::getJoinStyle)

JoinStyle getJoinStyle() const

Retrieves the current join style for stroke paths.
Returns The JoinStyle enumeration value.

setJoinStyle function (Canvas::setJoinStyle)

void setJoinStyle(JoinStyle joinStyle)

Sets the join style for stroke paths.
Param joinStyle The JoinStyle enumeration value.

getCapStyle function (Canvas::getCapStyle)

CapStyle getCapStyle() const

Retrieves the current cap style for stroke endpoints.
Returns The CapStyle enumeration value.

setCapStyle function (Canvas::setCapStyle)

void setCapStyle(CapStyle capStyle)

Sets the cap style for stroke endpoints.
Param capStyle The CapStyle enumeration value.

getDashOffset function (Canvas::getDashOffset)

float getDashOffset() const

Retrieves the current dash offset for dashed lines.
Returns The dash offset as a float.

setDashOffset function (Canvas::setDashOffset)

void setDashOffset(float offset)

Sets the dash offset for dashed lines.
Param offset The dash offset as a float.

getDashArray function (Canvas::getDashArray)

const DashArray &getDashArray() const

Retrieves the current dash pattern for dashed lines.
Returns A reference to the DashArray representing the dash pattern.

setDashArray function (Canvas::setDashArray)

void setDashArray(const DashArray &array)

Sets the dash pattern for dashed lines.
Param array A DashArray representing the dash pattern.

strokePath function (Canvas::strokePath)

void strokePath(const Path &path)

Strokes a given path with the current stroke settings.

This function renders the outline of the specified path using the canvas's current stroke settings (e.g., stroke paint, stroke parameters).
Param path The Path struct to stroke.

fillPath function (Canvas::fillPath)

void fillPath(const Path &path)

Fills a given path with the current fill settings.

This function fills the specified path using the canvas's current fill settings (e.g., fill paint, fill parameters).
Param path The Path struct to fill.

drawPath function (Canvas::drawPath)

void drawPath(const Path &path)

Draws a given path with the current stroke and fill settings.

This function renders the specified path by applying both the canvas's current stroke and fill settings (e.g., stroke paint, fill paint, stroke and fill parameters).
Param path The Path struct to draw.

strokeRect function (Canvas::strokeRect)

void strokeRect(RectangleF rect,
                CornersF borderRadius = 0.f,
                bool squircle = false)

Strokes a rectangle with the current stroke settings.

This function renders the outline of the specified rectangle using the canvas's current stroke settings. The rectangle can have rounded corners defined by borderRadius, and if squircle is true, the rounded corners will use a squircle shape.
Param rect The RectangleF struct defining the rectangle to stroke.
Param borderRadius The CornersF struct specifying the border radius for each corner (default: 0.f).
Param squircle If true, rounded corners will have a squircle shape (default: false).

fillRect function (Canvas::fillRect)

void fillRect(RectangleF rect, CornersF borderRadius = 0.f,
              bool squircle = false)

Fills a rectangle with the current fill settings.

This function fills the specified rectangle using the canvas's current fill settings. The rectangle can have rounded corners defined by borderRadius, and if squircle is true, the rounded corners will use a squircle shape.
Param rect The RectangleF struct defining the rectangle to fill.
Param borderRadius The CornersF struct specifying the border radius for each corner (default: 0.f).
Param squircle If true, rounded corners will have a squircle shape (default: false).

drawRect function (Canvas::drawRect)

void drawRect(RectangleF rect, CornersF borderRadius = 0.f,
              bool squircle = false)

Draws a rectangle with the current stroke and fill settings.

This function renders the specified rectangle by applying both the canvas's current stroke and fill settings. The rectangle can have rounded corners defined by borderRadius, and if squircle is true, the rounded corners will use a squircle shape.
Param rect The RectangleF struct defining the rectangle to draw.
Param borderRadius The CornersF struct specifying the border radius for each corner (default: 0.f).
Param squircle If true, rounded corners will have a squircle shape (default: false).

blurRect function (Canvas::blurRect)

void blurRect(RectangleF rect, float blurRadius,
              CornersF borderRadius = 0.f,
              bool squircle = false)

Draws a blurred rectangle, typically used for rendering shadows.

This function renders a blurred rectangle with the specified blur radius. The rectangle can have rounded corners defined by borderRadius, and if squircle is true, the rounded corners will use a squircle shape. This is often used to create shadow effects.
Param rect The RectangleF struct defining the rectangle to blur.
Param blurRadius The radius of the blur effect.
Param borderRadius The CornersF struct specifying the border radius for each corner (default: 0.f).
Param squircle If true, rounded corners will have a squircle shape (default: false).

strokeEllipse function (Canvas::strokeEllipse)

void strokeEllipse(RectangleF rect)

Strokes an ellipse defined by the bounding rectangle.
Param rect The RectangleF struct defining the bounding box of the ellipse.

fillEllipse function (Canvas::fillEllipse)

void fillEllipse(RectangleF rect)

Fills an ellipse defined by the bounding rectangle.
Param rect The RectangleF struct defining the bounding box of the ellipse.

strokePolygon function (Canvas::strokePolygon)

void strokePolygon(std::span<const PointF> points,
                   bool close = true)

Strokes a polygon defined by a series of points.
Param points A span of PointF structs defining the polygon vertices.
Param close Whether to close the polygon by connecting the last point to the first. Defaults to true.

fillPolygon function (Canvas::fillPolygon)

void fillPolygon(std::span<const PointF> points,
                 bool close = true)

Fills a polygon defined by a series of points.
Param points A span of PointF structs defining the polygon vertices.
Param close Whether to close the polygon by connecting the last point to the first. Defaults to true.

getFont function (Canvas::getFont)

Font getFont() const

Retrieves the current font used for text rendering.
Returns The current Font.

setFont function (Canvas::setFont)

void setFont(const Font &font)

Sets the font used for text rendering.
Param font The Font to use.

fillText function (Canvas::fillText)

void fillText(TextWithOptions text, PointF position, PointF alignment = PointF

Fills text at a specified position with alignment.
Param text The text to render.
Param position The PointF struct representing the text position.
Param alignment The alignment of the text relative to the position. Defaults to {0.f, 0.f}.

void fillText(TextWithOptions text, RectangleF position, PointF alignment = PointF

Fills text within a specified rectangular area with alignment.
Param text The text to render.
Param position The RectangleF struct representing the area to fill the text within.
Param alignment The alignment of the text relative to the rectangle. Defaults to {0.f, 0.f}.

void fillText(PointF position, const TextLayout &text)

Fills a text layout at the specified position.

The text layout is immutable and may be retained independently of the FontManager that created it. Glyph rasterization and renderer caching remain private to the graphics/text-layout integration.

void fillText(PointF position, PointF alignment,
              const TextLayout &text)

Fills a text layout aligned relative to the specified position.

fillTextSelection function (Canvas::fillTextSelection)

void fillTextSelection(PointF position,
                       const TextLayout &text,
                       Range<uint32_t> selection)

Fills the selection in a text layout.

Selection offsets are text character/codepoint offsets, with an exclusive maximum, matching the legacy Canvas selection API.

void fillTextSelection(PointF position, PointF alignment,
                       const TextLayout &text,
                       Range<uint32_t> selection)

Fills a text selection aligned relative to the specified position.

strokeLine function (Canvas::strokeLine)

void strokeLine(PointF pt1, PointF pt2)

Strokes a line between two points.
Param pt1 The starting point of the line.
Param pt2 The ending point of the line.

drawImage function (Canvas::drawImage)

void drawImage(RectangleF rect, Rc<Image> image, Matrix matrix =

Draws an image within a specified rectangular area.
Param rect The RectangleF struct defining the area to draw the image.
Param image The Image to draw.
Param matrix The transformation matrix to apply to the image. Defaults to the identity matrix.

getTransform function (Canvas::getTransform)

Matrix getTransform() const

Retrieves the current transformation matrix.
Returns The current transformation Matrix struct.

setTransform function (Canvas::setTransform)

void setTransform(const Matrix &matrix)

Sets the transformation matrix.
Param matrix The Matrix struct representing the transformation.

transform function (Canvas::transform)

void transform(const Matrix &matrix)

Applies an additional transformation to the current matrix.
Param matrix The Matrix struct representing the transformation to apply.

getClipPath function (Canvas::getClipPath)

const Path &getClipPath() const

Retrieves the current clipping path.

setClipRect function (Canvas::setClipRect)

void setClipRect(RectangleF rect)

Sets the clipping rectangle.
Param rect The Rectangle object defining the new clipping area.

resetClipPath function (Canvas::resetClipPath)

void resetClipPath()

Resets the clipping path to cover the entire canvas.

reset function (Canvas::reset)

void reset()

Resets the Canvas state to its default values.

This function clears all current state settings and restores the canvas to its initial default state as defined by defaultState.

save function (Canvas::save)

void save()

Saves the current state of the Canvas.

The saved state, including clip path, transform, paints, and other parameters, is pushed onto an internal stack and can be restored later using the restore() function.

restore function (Canvas::restore)

void restore()

Restores the most recently saved Canvas state.

This function pops the most recently saved state from the internal stack and applies it to the canvas, discarding the previous state.

restoreNoPop function (Canvas::restoreNoPop)

void restoreNoPop()

Restores the most recently saved Canvas state without removing it from the stack.

This function applies the most recently saved state to the canvas but keeps the state on the internal stack for future restores.

State class (Canvas::State)

State

Structure holding the complete state of the Canvas.

This struct contains all configurable properties of the canvas, such as clipping, transformation, paints, and drawing parameters.

scissor variable (Canvas::State::scissor)

Rectangle scissor

The current scissor rectangle in device coordinates.

transform variable (Canvas::State::transform)

Matrix transform

The current transformation matrix.

strokePaint variable (Canvas::State::strokePaint)

Paint strokePaint

The current stroke paint settings.

fillPaint variable (Canvas::State::fillPaint)

Paint fillPaint

The current fill paint settings.

strokeParams variable (Canvas::State::strokeParams)

StrokeParams strokeParams

The current stroke parameters (e.g., width, cap, join).

opacity variable (Canvas::State::opacity)

float opacity

The current opacity value (0.0 to 1.0).

fillParams variable (Canvas::State::fillParams)

FillParams fillParams

The current fill parameters (e.g., fill rule).

font variable (Canvas::State::font)

Font font

The current font settings.

subpixelText variable (Canvas::State::subpixelText)

bool subpixelText

Whether subpixel text rendering is enabled.

clipPath variable (Canvas::State::clipPath)

Path clipPath

The current clipping path.

composition variable (Canvas::State::composition)

Composition composition

The current composition settings.

defaultState variable (Canvas::defaultState)

static const State defaultState

The default state of the Canvas.

This static constant defines the initial values for all state properties when the canvas is reset or initialized.

state function (Canvas::state)

const State &state() const noexcept

Retrieves the current state of the Canvas.
Returns A const reference to the current State object.

setState function (Canvas::setState)

void setState(State state)

Sets the Canvas state to the specified state.

This function applies the provided State object to the canvas, updating all relevant properties such as clip path, transform, paints, and drawing parameters.
Param state The State object to apply to the canvas.

StateSaver class (Canvas::StateSaver)

StateSaver

RAII utility class for saving and restoring the Canvas state.

This class saves the current canvas state upon construction and restores it upon destruction. It provides access to the current state via operator* and operator->, allowing direct modification of state fields for drawing operations. To minimize assignments and copies, assign the result to an auto&& variable, e.g., auto&& state = canvas.saveState();.

canvas variable (Canvas::StateSaver::canvas)

Canvas &canvas

Reference to the associated Canvas.

saved variable (Canvas::StateSaver::saved)

State saved

The saved state to restore on destruction.

StateSaver function (Canvas::StateSaver::StateSaver)

StateSaver(Canvas &canvas)

Constructs a StateSaver, saving the current canvas state.
Param canvas The Canvas whose state is to be saved.

operator* function (Canvas::StateSaver::operator*)

State &operator*()

Provides access to the current canvas state for modification.
Returns Reference to the current State object.

operator-> function (Canvas::StateSaver::operator->)

State *operator->()

Provides pointer-like access to the current canvas state for modification.
Returns Pointer to the current State object.

~StateSaver function (Canvas::StateSaver::~StateSaver)

~StateSaver()

Destructor that restores the saved canvas state.

saveState function (Canvas::saveState)

StateSaver saveState() &

Saves the current canvas state and returns a StateSaver for RAII management.

The returned StateSaver saves the current state and restores it upon destruction. Assign the result to an auto&& variable to avoid unnecessary copies, e.g., auto&& state = canvas.saveState();.
Returns A StateSaver object managing the saved state.

ScissorSaver class (Canvas::ScissorSaver)

ScissorSaver

RAII utility class for saving and restoring the Canvas clip rectangle.

This class saves the current clip rectangle upon construction and restores it upon destruction. It provides access to the current clip rectangle via operator* and operator->, allowing direct modification for drawing operations. To minimize assignments and copies, assign the result to an auto&& variable, e.g., auto&& clip = canvas.saveClipRect();.

canvas variable (Canvas::ScissorSaver::canvas)

Canvas &canvas

Reference to the associated Canvas.

saved variable (Canvas::ScissorSaver::saved)

Rectangle saved

The saved scissor rectangle to restore on destruction.

ScissorSaver function (Canvas::ScissorSaver::ScissorSaver)

ScissorSaver(Canvas &canvas)

Constructs a ClipRectSaver, saving the current clip rectangle.
Param canvas The Canvas whose clip rectangle is to be saved.

operator* function (Canvas::ScissorSaver::operator*)

Rectangle &operator*()

Provides access to the current clip rectangle for modification.
Returns Reference to the current Rectangle object.

operator-> function (Canvas::ScissorSaver::operator->)

Rectangle *operator->()

Provides pointer-like access to the current clip rectangle for modification.
Returns Pointer to the current Rectangle object.

~ScissorSaver function (Canvas::ScissorSaver::~ScissorSaver)

~ScissorSaver()

Destructor that restores the saved clip rectangle.

saveScissor function (Canvas::saveScissor)

ScissorSaver saveScissor() &

Saves the current scissor rectangle and returns a ScissorSaver for RAII management.

The returned ScissorSaver saves the current scissor rectangle and restores it upon destruction. Assign the result to an auto&& variable to avoid unnecessary copies, e.g., auto&& scissor = canvas.saveScissor();.
Returns A ScissorSaver object managing the saved scissor.

beginLayer function (Canvas::beginLayer)

void beginLayer(Size layerSize)

Begins a new drawing layer with the specified size and pushes it onto the internal stack.

This function creates a new layer with the given dimensions, allowing subsequent drawing operations to be performed on this layer. After finishing the layer with finishLayer, the layer content can be drawn to the original canvas or used as an image.
Note The state of the Canvas is saved before creating the new layer and reset to its default state.
Param layerSize The size of the new layer to be created.

layers function (Canvas::layers)

size_t layers() const noexcept

Retrieves the current depth of the layer stack.

This function returns the number of layers that have been opened using beginLayer but not yet closed with finishLayer.
Returns The number of open layers as an integer.

finishLayer function (Canvas::finishLayer)

Rc<Image> finishLayer()

Finalizes the current drawing layer and renders its contents to an Image.

This function completes the rendering process for the current layer, matching a prior call to beginLayer. It captures all the drawn elements on the layer and returns them as an Image object. This is useful for applying effects or further processing on the rendered layer.
Returns Rc<Image> A reference-counted pointer to the rendered Image.

contentsAsImage function (Canvas::contentsAsImage)

Rc<Image> contentsAsImage()

Captures the contents of the canvas as an image.

This function renders everything that has been drawn on the canvas up to this point into an Image object. The resulting image can be used for applying effects or further processing.
Returns Rc<Image> A reference-counted pointer to the rendered Image.

Layer class (Canvas::Layer)

Layer

m_state variable (Canvas::m_state)

State m_state

The current state of the Canvas.

m_stack variable (Canvas::m_stack)

std::vector<State> m_stack

The stack of saved Canvas states.

m_layers variable (Canvas::m_layers)

std::vector<Layer> m_layers

The stack of layers for rendering.


Auto-generated from sources, Revision , https://github.com/brisklib/brisk/blob//include/brisk/