Photon 1.0.0
Loading...
Searching...
No Matches
canvas_ity::canvas Class Reference

#include <canvas_ity.hpp>

Public Member Functions

 canvas (int width, int height)
 Construct a new canvas.
 
 canvas (int width, int height, rgba color)
 
 canvas ()
 
 ~canvas ()
 Destroy the canvas and release all associated memory.
 
void scale (float x, float y)
 Scale the current transform.
 
void rotate (float angle)
 Rotate the current transform.
 
void translate (float x, float y)
 Translate the current transform.
 
void transform (float a, float b, float c, float d, float e, float f)
 Add an arbitrary transform to the current transform.
 
void set_transform (float a, float b, float c, float d, float e, float f)
 Replace the current transform.
 
void set_global_alpha (float alpha)
 Set the degree of opacity applied to all drawing operations.
 
void set_shadow_color (float red, float green, float blue, float alpha)
 Set the color and opacity of the shadow.
 
void set_shadow_blur (float level)
 Set the level of Gaussian blurring on the shadow.
 
void set_line_width (float width)
 Set the width of the lines when stroking.
 
void set_miter_limit (float limit)
 Set the limit on maximum pointiness allowed for miter joins.
 
void set_line_dash (float const *segments, int count)
 Set or clear the line dash pattern.
 
void set_color (brush_type type, float red, float green, float blue, float alpha)
 Set filling or stroking to use a constant color and opacity.
 
void set_color (brush_type type, rgba c)
 
void set_linear_gradient (brush_type type, float start_x, float start_y, float end_x, float end_y)
 Set filling or stroking to use a linear gradient.
 
void set_radial_gradient (brush_type type, float start_x, float start_y, float start_radius, float end_x, float end_y, float end_radius)
 Set filling or stroking to use a radial gradient.
 
void set_css_radial_gradient (brush_type type, float x, float y, float radius_x, float radius_y)
 
void set_conic_gradient (brush_type type, float x, float y, float angle)
 
void add_color_stop (brush_type type, float offset, float red, float green, float blue, float alpha, std::optional< float > hint={})
 Add a color stop to a linear or radial gradient.
 
void set_pattern (brush_type type, unsigned char const *image, int width, int height, int stride, repetition_style repetition)
 Set filling or stroking to draw with an image pattern.
 
void begin_path ()
 Reset the current path.
 
void move_to (float x, float y)
 Create a new subpath.
 
void close_path ()
 Close the current subpath.
 
void line_to (float x, float y)
 Extend the current subpath with a straight line.
 
void quadratic_curve_to (float control_x, float control_y, float x, float y)
 Extend the current subpath with a quadratic Bezier curve.
 
void bezier_curve_to (float control_1_x, float control_1_y, float control_2_x, float control_2_y, float x, float y)
 Extend the current subpath with a cubic Bezier curve.
 
void arc_to (float vertex_x, float vertex_y, float x, float y, float radius)
 Extend the current subpath with an arc tangent to two lines.
 
void arc (float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise=false)
 Extend the current subpath with an arc between two angles.
 
void rectangle (float x, float y, float width, float height)
 Add a closed subpath in the shape of a rectangle.
 
void polygon (std::vector< xy > points)
 
void fill ()
 Draw the interior of the current path using the fill style.
 
void stroke ()
 Draw the edges of the current path using the stroke style.
 
void clip ()
 Restrict the clip region by the current path.
 
bool is_point_in_path (float x, float y)
 Tests whether a point is in or on the current path.
 
void clear_rectangle (float x, float y, float width, float height)
 Clear a rectangular area back to transparent black.
 
void fill_rectangle (float x, float y, float width, float height)
 Fill a rectangular area.
 
void stroke_rectangle (float x, float y, float width, float height)
 Stroke a rectangular area.
 
bool set_font (unsigned char const *font, int bytes, float size)
 Set the font to use for text drawing.
 
void get_font_metrics (int &ascent, int &descent, int &height, int &x_height)
 
void fill_text (char const *text, float x, float y, float maximum_width=1.0e30f)
 Draw a line of text by filling its outline.
 
void stroke_text (char const *text, float x, float y, float maximum_width=1.0e30f)
 Draw a line of text by stroking its outline.
 
float measure_text (char const *text)
 Measure the width in pixels of a line of text.
 
void draw_image (unsigned char const *image, int width, int height, int stride, float x, float y, float to_width, float to_height)
 Draw an image onto the canvas.
 
void get_image_data (unsigned char *image, int width, int height, int stride, int x, int y)
 Fetch a rectangle of pixels from the canvas to an image.
 
void put_image_data (unsigned char const *image, int width, int height, int stride, int x, int y)
 Replace a rectangle of pixels on the canvas with an image.
 
int width ()
 
int height ()
 
void save ()
 Save the current state as though to a stack.
 
void restore ()
 Restore a previously saved state as though from a stack.
 

Public Attributes

composite_operation global_composite_operation
 Compositing operation for blending new drawing and old pixels.
 
float shadow_offset_x
 Horizontal offset of the shadow in pixels.
 
float shadow_offset_y
 Vertical offset of the shadow in pixels.
 
cap_style line_cap
 Cap style for the ends of open subpaths and dash segments.
 
join_style line_join
 Join style for connecting lines within the paths.
 
float line_dash_offset
 Offset where each subpath starts the dash pattern.
 
align_style text_align
 Horizontal position of the text relative to the anchor point.
 
baseline_style text_baseline
 Vertical position of the text relative to the anchor point.
 

Private Member Functions

 canvas (canvas const &)
 
canvasoperator= (canvas const &)
 
void add_tessellation (xy, xy, xy, xy, float, int)
 
void add_bezier (xy, xy, xy, xy, float)
 
void path_to_lines (bool)
 
void add_glyph (int, float)
 
int character_to_glyph (char const *, int &)
 
void text_to_lines (char const *, xy, float, bool)
 
void dash_lines ()
 
void add_half_stroke (size_t, size_t, bool)
 
void stroke_lines ()
 
void add_runs (xy, xy)
 
void lines_to_runs (xy, int)
 
rgba paint_pixel (xy, paint_brush const &)
 
void render_shadow (paint_brush const &)
 
void render_main (paint_brush const &)
 

Private Attributes

int size_x
 
int size_y
 
affine_matrix forward
 
affine_matrix inverse
 
float global_alpha
 
rgba shadow_color
 
float shadow_blur
 
std::vector< float > shadow
 
float line_width
 
float miter_limit
 
std::vector< float > line_dash
 
paint_brush fill_brush
 
paint_brush stroke_brush
 
paint_brush image_brush
 
bezier_path path
 
line_path lines
 
line_path scratch
 
pixel_runs runs
 
pixel_runs mask
 
font_face face
 
rgbabitmap
 
canvassaves
 

Constructor & Destructor Documentation

◆ canvas() [1/4]

canvas_ity::canvas::canvas ( int  width,
int  height 
)

Construct a new canvas.

It will begin with all pixels set to transparent black. Initially, the visible coordinates will run from (0, 0) in the upper-left to (width, height) in the lower-right and with pixel centers offset (0.5, 0.5) from the integer grid, though all this may be changed by transforms. The sizes must be between 1 and 32768, inclusive.

Parameters
widthhorizontal size of the new canvas in pixels
heightvertical size of the new canvas in pixels

◆ canvas() [2/4]

canvas_ity::canvas::canvas ( int  width,
int  height,
rgba  color 
)

◆ canvas() [3/4]

canvas_ity::canvas::canvas ( )
inline

◆ ~canvas()

canvas_ity::canvas::~canvas ( )

Destroy the canvas and release all associated memory.

◆ canvas() [4/4]

canvas_ity::canvas::canvas ( canvas const &  )
private

Member Function Documentation

◆ add_bezier()

void canvas_ity::canvas::add_bezier ( xy  ,
xy  ,
xy  ,
xy  ,
float   
)
private

◆ add_color_stop()

void canvas_ity::canvas::add_color_stop ( brush_type  type,
float  offset,
float  red,
float  green,
float  blue,
float  alpha,
std::optional< float >  hint = {} 
)

Add a color stop to a linear or radial gradient.

Each color stop has an offset which defines its position from 0.0 at the start of the gradient to 1.0 at the end. Colors and opacity are linearly interpolated along the gradient between adjacent pairs of stops without premultiplying the alpha. If more than one stop is added for a given offset, the first one added is considered closest to 0.0 and the last is closest to 1.0. If no stop is at offset 0.0 or 1.0, the stops with the closest offsets will be extended. If no stops are added, the gradient will be fully transparent black. Set a new linear or radial gradient to clear all the stops and redefine the gradient colors. Color stops may be added to a gradient at any time. The color and opacity values will be clamped to the 0.0 to 1.0 range, inclusive. The offset must be in the 0.0 to 1.0 range, inclusive. If it is not, or if chosen style type is not currently set to a gradient, this does nothing.

Parameters
typewhether to add to the fill_style or stroke_style
offsetposition of the color stop along the gradient
redsRGB red component of the color stop
greensRGB green component of the color stop
bluesRGB blue component of the color stop
alphaopacity of the color stop (not premultiplied)

◆ add_glyph()

void canvas_ity::canvas::add_glyph ( int  ,
float   
)
private

◆ add_half_stroke()

void canvas_ity::canvas::add_half_stroke ( size_t  ,
size_t  ,
bool   
)
private

◆ add_runs()

void canvas_ity::canvas::add_runs ( xy  ,
xy   
)
private

◆ add_tessellation()

void canvas_ity::canvas::add_tessellation ( xy  ,
xy  ,
xy  ,
xy  ,
float  ,
int   
)
private

◆ arc()

void canvas_ity::canvas::arc ( float  x,
float  y,
float  radius,
float  start_angle,
float  end_angle,
bool  counter_clockwise = false 
)

Extend the current subpath with an arc between two angles.

The arc is from the circle centered at the given point and with the given radius. A straight line will be added from the current end point to the starting point of the arc (unless the current path is empty), then the arc along the circle from the starting angle to the ending angle in the given direction will be added. If they are more than two pi radians apart in the given direction, the arc will stop after one full circle. The point at the ending angle will become the new end point of the path. Initially, the angles are clockwise relative to the x-axis. The current transform at the time that this is called will affect the passed in point, angles, and arc. The radius must be non-negative.

Parameters
xhorizontal coordinate of the circle center
yvertical coordinate of the circle center
radiusradius of the circle containing the arc
start_angleradians clockwise from x-axis to begin
end_angleradians clockwise from x-axis to end
counter_clockwisetrue if the arc turns counter-clockwise

◆ arc_to()

void canvas_ity::canvas::arc_to ( float  vertex_x,
float  vertex_y,
float  x,
float  y,
float  radius 
)

Extend the current subpath with an arc tangent to two lines.

The arc is from the circle with the given radius tangent to both the line from the current end point to the vertex, and to the line from the vertex to the given point. A straight line will be added from the current end point to the first tangent point (unless the current path is empty), then the shortest arc from the first to the second tangent points will be added. The second tangent point will become the new end point. If the radius is large, these tangent points may fall outside the line segments. The current transform at the time that this is called will affect the passed in points and the arc. If the current path was empty, this is equivalent to a move. If the arc would be degenerate, it is equivalent to a line to the vertex point. The radius must be non-negative. If it is not, or if the current transform is not invertible, this does nothing.

Tip: to draw a polygon with rounded corners, call this once for each vertex and pass the midpoint of the adjacent edge as the second point; this works especially well for rounded boxes.

Parameters
vertex_xhorizontal coordinate where the tangent lines meet
vertex_yvertical coordinate where the tangent lines meet
xa horizontal coordinate on the second tangent line
ya vertical coordinate on the second tangent line
radiusradius of the circle containing the arc

◆ begin_path()

void canvas_ity::canvas::begin_path ( )

Reset the current path.

The current path and all subpaths will be cleared after this, and a new path can be built.

◆ bezier_curve_to()

void canvas_ity::canvas::bezier_curve_to ( float  control_1_x,
float  control_1_y,
float  control_2_x,
float  control_2_y,
float  x,
float  y 
)

Extend the current subpath with a cubic Bezier curve.

The curve will go from the current end point (or the first control point if the current path is empty) to the given point, which will become the new end point. The curve will be tangent toward the first control point at the beginning and tangent toward the second control point at the end. The current transform at the time that this is called will affect all points passed in.

Tip: to make multiple curves join smoothly, ensure that each new end point is on a line between the adjacent control points.

Parameters
control_1_xhorizontal coordinate of the first control point
control_1_yvertical coordinate of the first control point
control_2_xhorizontal coordinate of the second control point
control_2_yvertical coordinate of the second control point
xhorizontal coordinate of the new end point
yvertical coordinate of the new end point

◆ character_to_glyph()

int canvas_ity::canvas::character_to_glyph ( char const *  ,
int &   
)
private

◆ clear_rectangle()

void canvas_ity::canvas::clear_rectangle ( float  x,
float  y,
float  width,
float  height 
)

Clear a rectangular area back to transparent black.

The clip region may limit the area cleared. The current path is not affected by this clearing. The current transform at the time that this is called will affect the given point and rectangle. The width and/or the height may be negative or zero. If either is zero, or the current transform is not invertible, this does nothing.

Parameters
xhorizontal coordinate of a rectangle corner
yvertical coordinate of a rectangle corner
widthwidth of the rectangle
heightheight of the rectangle

◆ clip()

void canvas_ity::canvas::clip ( )

Restrict the clip region by the current path.

Intersects the current clip region with the interior of the current path (the region that would be filled), and replaces the current clip region with this intersection. Subsequent calls to clip can only reduce this further. When filling or stroking, only pixels within the current clip region will change. The current path is left unchanged by updating the clip region; begin a new path to clear it. Defaults to the entire canvas.

Tip: to be able to reset the current clip region, save the canvas state first before clipping then restore the state to reset it.

◆ close_path()

void canvas_ity::canvas::close_path ( )

Close the current subpath.

Adds a straight line from the end of the current subpath back to its first point and marks the subpath as closed so that this line will join with the beginning of the path at this point. A new, empty subpath will be started beginning with the same first point. If the current path is empty, this does nothing.

◆ dash_lines()

void canvas_ity::canvas::dash_lines ( )
private

◆ draw_image()

void canvas_ity::canvas::draw_image ( unsigned char const *  image,
int  width,
int  height,
int  stride,
float  x,
float  y,
float  to_width,
float  to_height 
)

Draw an image onto the canvas.

The position of the rectangle that the image is drawn to is affected by the current transform at the time of drawing, and the image will be resampled as needed (with the filtering always clamping to the edges of the image). The drawing is also affected by the shadow, global alpha, global compositing operation settings, and by the clip region. The current path is not affected by drawing an image. The image data, which should be in top to bottom rows of contiguous pixels from left to right, is not retained and it is safe to change or destroy it after this call. The width and height must both be positive and the width and/or the height to scale to may be negative but not zero. Otherwise, or if the image pointer is null, or the current transform is not invertible, this does nothing.

Tip: to use a small piece of a larger image, reduce the width and height, and offset the image pointer while keeping the stride.

Parameters
imagepointer to unpremultiplied sRGB RGBA8 image data
widthwidth of the image in pixels
heightheight of the image in pixels
stridenumber of bytes between the start of each image row
xhorizontal coordinate to draw the corner at
yvertical coordinate to draw the corner at
to_widthwidth to scale the image to
to_heightheight to scale the image to

◆ fill()

void canvas_ity::canvas::fill ( )

Draw the interior of the current path using the fill style.

Interior pixels are determined by the non-zero winding rule, with all open subpaths implicitly closed by a straight line beforehand. If shadows have been enabled by setting the shadow color with any opacity and either offsetting or blurring the shadows, then the shadows of the filled areas will be drawn first, followed by the filled areas themselves. Both will be blended into the canvas according to the global alpha, the global compositing operation, and the clip region. If the fill style is a gradient or a pattern, it will be affected by the current transform. The current path is left unchanged by filling; begin a new path to clear it. If the current transform is not invertible, this does nothing.

◆ fill_rectangle()

void canvas_ity::canvas::fill_rectangle ( float  x,
float  y,
float  width,
float  height 
)

Fill a rectangular area.

This behaves as though the current path were reset to a single rectangle and then filled as usual. However, the current path is not actually changed. The current transform at the time that this is called will affect the given point and rectangle. The width and/or the height may be negative but not zero. If either is zero, or the current transform is not invertible, this does nothing.

Parameters
xhorizontal coordinate of a rectangle corner
yvertical coordinate of a rectangle corner
widthwidth of the rectangle
heightheight of the rectangle

◆ fill_text()

void canvas_ity::canvas::fill_text ( char const *  text,
float  x,
float  y,
float  maximum_width = 1.0e30f 
)

Draw a line of text by filling its outline.

This behaves as though the current path were reset to the outline of the given text in the current font and size, positioned relative to the given anchor point according to the current alignment and baseline, and then filled as usual. However, the current path is not actually changed. The current transform at the time that this is called will affect the given anchor point and the text outline. However, the comparison to the maximum width in pixels and the condensing if needed is done before applying the current transform. The maximum width (if given) must be positive. If it is not, or the text pointer is null, or the font has not been set yet, or the last setting of it was unsuccessful, or the current transform is not invertible, this does nothing.

Parameters
textnull-terminated UTF-8 string of text to fill
xhorizontal coordinate of the anchor point
yvertical coordinate of the anchor point
maximum_widthhorizontal width to condense wider text to

◆ get_font_metrics()

void canvas_ity::canvas::get_font_metrics ( int &  ascent,
int &  descent,
int &  height,
int &  x_height 
)

◆ get_image_data()

void canvas_ity::canvas::get_image_data ( unsigned char *  image,
int  width,
int  height,
int  stride,
int  x,
int  y 
)

Fetch a rectangle of pixels from the canvas to an image.

This call is akin to a direct pixel-for-pixel copy from the canvas buffer without resampling. The position and size of the canvas rectangle is not affected by the current transform. The image data is copied into, from top to bottom in rows of contiguous pixels from left to right, and it is safe to change or destroy it after this call. The requested rectangle may safely extend outside the bounds of the canvas; these pixels will be set to transparent black. The width and height must be positive. If not, or if the image pointer is null, this does nothing.

Tip: to copy into a section of a larger image, reduce the width and height, and offset the image pointer while keeping the stride. Tip: use this to get the rendered image from the canvas after drawing.

Parameters
imagepointer to unpremultiplied sRGB RGBA8 image data
widthwidth of the image in pixels
heightheight of the image in pixels
stridenumber of bytes between the start of each image row
xhorizontal coordinate of upper-left pixel to fetch
yvertical coordinate of upper-left pixel to fetch

◆ height()

int canvas_ity::canvas::height ( )
inline

◆ is_point_in_path()

bool canvas_ity::canvas::is_point_in_path ( float  x,
float  y 
)

Tests whether a point is in or on the current path.

Interior areas are determined by the non-zero winding rule, with all open subpaths treated as implicitly closed by a straight line beforehand. Points exactly on the boundary are also considered inside. The point to test is interpreted without being affected by the current transform, nor is the clip region considered. The current path is left unchanged by this test.

Parameters
xhorizontal coordinate of the point to test
yvertical coordinate of the point to test
Returns
true if the point is in or on the current path

◆ line_to()

void canvas_ity::canvas::line_to ( float  x,
float  y 
)

Extend the current subpath with a straight line.

The line will go from the current end point (if the current path is not empty) to the given point, which will become the new end point. Its position is affected by the current transform at the time that this is called. If the current path was empty, this is equivalent to just a move.

Parameters
xhorizontal coordinate of the new end point
yvertical coordinate of the new end point

◆ lines_to_runs()

void canvas_ity::canvas::lines_to_runs ( xy  ,
int   
)
private

◆ measure_text()

float canvas_ity::canvas::measure_text ( char const *  text)

Measure the width in pixels of a line of text.

The measured width is the advance width, which includes the side bearings of the first and last glyphs. However, text as drawn may go outside this (e.g., due to glyphs that spill beyond their nominal widths or stroked text with wide lines). Measurements ignore the current transform. If the text pointer is null, or the font has not been set yet, or the last setting of it was unsuccessful, this returns zero.

Parameters
textnull-terminated UTF-8 string of text to measure
Returns
width of the text in pixels

◆ move_to()

void canvas_ity::canvas::move_to ( float  x,
float  y 
)

Create a new subpath.

The given point will become the first point of the new subpath and is subject to the current transform at the time this is called.

Parameters
xhorizontal coordinate of the new first point
yvertical coordinate of the new first point

◆ operator=()

canvas & canvas_ity::canvas::operator= ( canvas const &  )
private

◆ paint_pixel()

rgba canvas_ity::canvas::paint_pixel ( xy  ,
paint_brush const &   
)
private

◆ path_to_lines()

void canvas_ity::canvas::path_to_lines ( bool  )
private

◆ polygon()

void canvas_ity::canvas::polygon ( std::vector< xy points)

◆ put_image_data()

void canvas_ity::canvas::put_image_data ( unsigned char const *  image,
int  width,
int  height,
int  stride,
int  x,
int  y 
)

Replace a rectangle of pixels on the canvas with an image.

This call is akin to a direct pixel-for-pixel copy into the canvas buffer without resampling. The position and size of the canvas rectangle is not affected by the current transform. Nor is the result affected by the current settings for the global alpha, global compositing operation, shadows, or the clip region. The image data, which should be in top to bottom rows of contiguous pixels from left to right, is copied from and it is safe to change or destroy it after this call. The width and height must be positive. If not, or if the image pointer is null, this does nothing.

Tip: to copy from a section of a larger image, reduce the width and height, and offset the image pointer while keeping the stride. Tip: use this to prepopulate the canvas before drawing.

Parameters
imagepointer to unpremultiplied sRGB RGBA8 image data
widthwidth of the image in pixels
heightheight of the image in pixels
stridenumber of bytes between the start of each image row
xhorizontal coordinate of upper-left pixel to replace
yvertical coordinate of upper-left pixel to replace

◆ quadratic_curve_to()

void canvas_ity::canvas::quadratic_curve_to ( float  control_x,
float  control_y,
float  x,
float  y 
)

Extend the current subpath with a quadratic Bezier curve.

The curve will go from the current end point (or the control point if the current path is empty) to the given point, which will become the new end point. The curve will be tangent toward the control point at both ends. The current transform at the time that this is called will affect both points passed in.

Tip: to make multiple curves join smoothly, ensure that each new end point is on a line between the adjacent control points.

Parameters
control_xhorizontal coordinate of the control point
control_yvertical coordinate of the control point
xhorizontal coordinate of the new end point
yvertical coordinate of the new end point

◆ rectangle()

void canvas_ity::canvas::rectangle ( float  x,
float  y,
float  width,
float  height 
)

Add a closed subpath in the shape of a rectangle.

The rectangle has one corner at the given point and then goes in the direction along the width before going in the direction of the height towards the opposite corner. The current transform at the time that this is called will affect the given point and rectangle. The width and/or the height may be negative or zero, and this can affect the winding direction.

Parameters
xhorizontal coordinate of a rectangle corner
yvertical coordinate of a rectangle corner
widthwidth of the rectangle
heightheight of the rectangle

◆ render_main()

void canvas_ity::canvas::render_main ( paint_brush const &  )
private

◆ render_shadow()

void canvas_ity::canvas::render_shadow ( paint_brush const &  )
private

◆ restore()

void canvas_ity::canvas::restore ( )

Restore a previously saved state as though from a stack.

This does not affect the pixels in the canvas buffer or the current path. If the stack of canvas states is empty, this does nothing.

◆ rotate()

void canvas_ity::canvas::rotate ( float  angle)

Rotate the current transform.

The rotation is applied clockwise in a direction around the origin.

Tip: to rotate around another point, first translate that point to the origin, then do the rotation, and then translate back.

Parameters
angleclockwise angle in radians

◆ save()

void canvas_ity::canvas::save ( )

Save the current state as though to a stack.

The full state of the canvas is saved, except for the pixels in the canvas buffer, and the current path.

Tip: to be able to reset the current clip region, save the canvas state first before clipping then restore the state to reset it.

◆ scale()

void canvas_ity::canvas::scale ( float  x,
float  y 
)

Scale the current transform.

Scaling may be non-uniform if the x and y scaling factors are different. When a scale factor is less than one, things will be shrunk in that direction. When a scale factor is greater than one, they will grow bigger. Negative scaling factors will flip or mirror it in that direction. The scaling factors must be non-zero. If either is zero, most drawing operations will do nothing.

Parameters
xhorizontal scaling factor
yvertical scaling factor

◆ set_color() [1/2]

void canvas_ity::canvas::set_color ( brush_type  type,
float  red,
float  green,
float  blue,
float  alpha 
)

Set filling or stroking to use a constant color and opacity.

The color and opacity values will be clamped to the 0.0 to 1.0 range, inclusive. Filling and stroking defaults a constant color with 0.0, 0.0, 0.0, 1.0 (opaque black).

Parameters
typewhether to set the fill_style or stroke_style
redsRGB red component of the painting color
greensRGB green component of the painting color
bluesRGB blue component of the painting color
alphaopacity to paint with (not premultiplied)

◆ set_color() [2/2]

void canvas_ity::canvas::set_color ( brush_type  type,
rgba  c 
)
inline

◆ set_conic_gradient()

void canvas_ity::canvas::set_conic_gradient ( brush_type  type,
float  x,
float  y,
float  angle 
)

◆ set_css_radial_gradient()

void canvas_ity::canvas::set_css_radial_gradient ( brush_type  type,
float  x,
float  y,
float  radius_x,
float  radius_y 
)

◆ set_font()

bool canvas_ity::canvas::set_font ( unsigned char const *  font,
int  bytes,
float  size 
)

Set the font to use for text drawing.

The font must be a TrueType font (TTF) file which has been loaded or mapped into memory. Following some basic validation, the relevant sections of the font file contents are copied, and it is safe to change or destroy after this call. As an optimization, calling this with either a null pointer or zero for the number of bytes will allow for changing the size of the previous font without recopying from the file. Note that the font parsing is not meant to be secure; only use this with trusted TTF files!

Parameters
fontpointer to the contents of a TrueType font (TTF) file
bytesnumber of bytes in the font file
sizesize in pixels per em to draw at
Returns
true if the font was set successfully

◆ set_global_alpha()

void canvas_ity::canvas::set_global_alpha ( float  alpha)

Set the degree of opacity applied to all drawing operations.

If an operation already uses a transparent color, this can make it yet more transparent. It must be in the range from 0.0 for fully transparent to 1.0 for fully opaque, inclusive. If it is not, this does nothing. Defaults to 1.0 (opaque).

Parameters
alphadegree of opacity applied to all drawing operations

◆ set_line_dash()

void canvas_ity::canvas::set_line_dash ( float const *  segments,
int  count 
)

Set or clear the line dash pattern.

Takes an array with entries alternately giving the lengths of dash and gap segments. All must be non-negative; if any are not, this does nothing. These will be used to draw with dashed lines when stroking, with each subpath starting at the length along the dash pattern indicated by the line dash offset. Initially these lengths are measured in pixels, though the current transform at the time of drawing can affect this. The count must be non-negative. If it is odd, the array will be appended to itself to make an even count. If it is zero, or the pointer is null, the dash pattern will be cleared and strokes will be drawn as solid lines. The array is copied and it is safe to change or destroy it after this call. Defaults to solid lines.

Parameters
segmentspointer to array for dash pattern
countnumber of entries in the array

◆ set_line_width()

void canvas_ity::canvas::set_line_width ( float  width)

Set the width of the lines when stroking.

Initially this is measured in pixels, though the current transform at the time of drawing can affect this. Must be positive. If it is not, this does nothing. Defaults to 1.0.

Parameters
widthwidth of the lines when stroking

◆ set_linear_gradient()

void canvas_ity::canvas::set_linear_gradient ( brush_type  type,
float  start_x,
float  start_y,
float  end_x,
float  end_y 
)

Set filling or stroking to use a linear gradient.

Positions the start and end points of the gradient and clears all color stops to reset the gradient to transparent black. Color stops can then be added again. When drawing, pixels will be painted with the color of the gradient at the nearest point on the line segment between the start and end points. This is affected by the current transform at the time of drawing.

Parameters
typewhether to set the fill_style or stroke_style
start_xhorizontal coordinate of the start of the gradient
start_yvertical coordinate of the start of the gradient
end_xhorizontal coordinate of the end of the gradient
end_yvertical coordinate of the end of the gradient

◆ set_miter_limit()

void canvas_ity::canvas::set_miter_limit ( float  limit)

Set the limit on maximum pointiness allowed for miter joins.

If the distance from the point where the lines intersect to the point where the outside edges of the join intersect exceeds this ratio relative to the line width, then a bevel join will be used instead, and the miter will be lopped off. Larger values allow pointier miters. Only affects stroking and only when the line join style is miter. Must be positive. If it is not, this does nothing. Defaults to 10.0.

Parameters
limitthe limit on maximum pointiness allowed for miter joins

◆ set_pattern()

void canvas_ity::canvas::set_pattern ( brush_type  type,
unsigned char const *  image,
int  width,
int  height,
int  stride,
repetition_style  repetition 
)

Set filling or stroking to draw with an image pattern.

Initially, pixels in the pattern correspond exactly to pixels on the canvas, with the pattern starting in the upper left. The pattern is affected by the current transform at the time of drawing, and the pattern will be resampled as needed (with the filtering always wrapping regardless of the repetition setting). The pattern can be repeated either horizontally, vertically, both, or neither, relative to the source image. If the pattern is not repeated, then beyond it will be considered transparent black. The pattern image, which should be in top to bottom rows of contiguous pixels from left to right, is copied and it is safe to change or destroy it after this call. The width and height must both be positive. If either are not, or the image pointer is null, this does nothing.

Tip: to use a small piece of a larger image, reduce the width and height, and offset the image pointer while keeping the stride.

Parameters
typewhether to set the fill_style or stroke_style
imagepointer to unpremultiplied sRGB RGBA8 image data
widthwidth of the pattern image in pixels
heightheight of the pattern image in pixels
stridenumber of bytes between the start of each image row
repetitionrepeat, repeat_x, repeat_y, or no_repeat

◆ set_radial_gradient()

void canvas_ity::canvas::set_radial_gradient ( brush_type  type,
float  start_x,
float  start_y,
float  start_radius,
float  end_x,
float  end_y,
float  end_radius 
)

Set filling or stroking to use a radial gradient.

Positions the start and end circles of the gradient and clears all color stops to reset the gradient to transparent black. Color stops can then be added again. When drawing, pixels will be painted as though the starting circle moved and changed size linearly to match the ending circle, while sweeping through the colors of the gradient. Pixels not touched by the moving circle will be left transparent black. The radial gradient is affected by the current transform at the time of drawing. The radii must be non-negative.

Parameters
typewhether to set the fill_style or stroke_style
start_xhorizontal starting coordinate of the circle
start_yvertical starting coordinate of the circle
start_radiusstarting radius of the circle
end_xhorizontal ending coordinate of the circle
end_yvertical ending coordinate of the circle
end_radiusending radius of the circle

◆ set_shadow_blur()

void canvas_ity::canvas::set_shadow_blur ( float  level)

Set the level of Gaussian blurring on the shadow.

Zero produces no blur, while larger values will blur the shadow more strongly. This is not affected by the current transform. Must be non-negative. If it is not, this does nothing. Defaults to 0.0 (no blur).

Parameters
levelthe level of Gaussian blurring on the shadow

◆ set_shadow_color()

void canvas_ity::canvas::set_shadow_color ( float  red,
float  green,
float  blue,
float  alpha 
)

Set the color and opacity of the shadow.

Most drawing operations can optionally draw a blurred drop shadow before doing the main drawing. The shadow is modulated by the opacity of the drawing and will be blended into the existing pixels subject to the compositing settings and clipping region. Shadows will only be drawn if the shadow color has any opacity and the shadow is either offset or blurred. The color and opacity values will be clamped to the 0.0 to 1.0 range, inclusive. Defaults to 0.0, 0.0, 0.0, 0.0 (transparent black).

Parameters
redsRGB red component of the shadow color
greensRGB green component of the shadow color
bluesRGB blue component of the shadow color
alphaopacity of the shadow (not premultiplied)

◆ set_transform()

void canvas_ity::canvas::set_transform ( float  a,
float  b,
float  c,
float  d,
float  e,
float  f 
)

Replace the current transform.

This takes six values for the upper two rows of a homogenous 3x3 matrix (i.e., {{a, c, e}, {b, d, f}, {0.0, 0.0, 1.0}}) describing an arbitrary affine transform and replaces the current transform with it. The values can represent any affine transform such as scaling, rotation, translation, or skew, or any composition of affine transforms. The matrix must be invertible. If it is not, most drawing operations will do nothing.

Tip: to reset the current transform back to the default, use 1.0, 0.0, 0.0, 1.0, 0.0, 0.0.

Parameters
ahorizontal scaling factor (m11)
bvertical skewing (m12)
chorizontal skewing (m21)
dvertical scaling factor (m22)
ehorizontal translation (m31)
fvertical translation (m32)

◆ stroke()

void canvas_ity::canvas::stroke ( )

Draw the edges of the current path using the stroke style.

Edges of the path will be expanded into strokes according to the current dash pattern, dash offset, line width, line join style (and possibly miter limit), line cap, and transform. If shadows have been enabled by setting the shadow color with any opacity and either offsetting or blurring the shadows, then the shadow will be drawn for the stroked lines first, then the stroked lines themselves. Both will be blended into the canvas according to the global alpha, the global compositing operation, and the clip region. If the stroke style is a gradient or a pattern, it will be affected by the current transform. The current path is left unchanged by stroking; begin a new path to clear it. If the current transform is not invertible, this does nothing.

Tip: to draw with a calligraphy-like angled brush effect, add a non-uniform scale transform just before stroking.

◆ stroke_lines()

void canvas_ity::canvas::stroke_lines ( )
private

◆ stroke_rectangle()

void canvas_ity::canvas::stroke_rectangle ( float  x,
float  y,
float  width,
float  height 
)

Stroke a rectangular area.

This behaves as though the current path were reset to a single rectangle and then stroked as usual. However, the current path is not actually changed. The current transform at the time that this is called will affect the given point and rectangle. The width and/or the height may be negative or zero. If both are zero, or the current transform is not invertible, this does nothing. If only one is zero, this behaves as though it strokes a single horizontal or vertical line.

Parameters
xhorizontal coordinate of a rectangle corner
yvertical coordinate of a rectangle corner
widthwidth of the rectangle
heightheight of the rectangle

◆ stroke_text()

void canvas_ity::canvas::stroke_text ( char const *  text,
float  x,
float  y,
float  maximum_width = 1.0e30f 
)

Draw a line of text by stroking its outline.

This behaves as though the current path were reset to the outline of the given text in the current font and size, positioned relative to the given anchor point according to the current alignment and baseline, and then stroked as usual. However, the current path is not actually changed. The current transform at the time that this is called will affect the given anchor point and the text outline. However, the comparison to the maximum width in pixels and the condensing if needed is done before applying the current transform. The maximum width (if given) must be positive. If it is not, or the text pointer is null, or the font has not been set yet, or the last setting of it was unsuccessful, or the current transform is not invertible, this does nothing.

Parameters
textnull-terminated UTF-8 string of text to stroke
xhorizontal coordinate of the anchor point
yvertical coordinate of the anchor point
maximum_widthhorizontal width to condense wider text to

◆ text_to_lines()

void canvas_ity::canvas::text_to_lines ( char const *  ,
xy  ,
float  ,
bool   
)
private

◆ transform()

void canvas_ity::canvas::transform ( float  a,
float  b,
float  c,
float  d,
float  e,
float  f 
)

Add an arbitrary transform to the current transform.

This takes six values for the upper two rows of a homogenous 3x3 matrix (i.e., {{a, c, e}, {b, d, f}, {0.0, 0.0, 1.0}}) describing an arbitrary affine transform and appends it to the current transform. The values can represent any affine transform such as scaling, rotation, translation, or skew, or any composition of affine transforms. The matrix must be invertible. If it is not, most drawing operations will do nothing.

Parameters
ahorizontal scaling factor (m11)
bvertical skewing (m12)
chorizontal skewing (m21)
dvertical scaling factor (m22)
ehorizontal translation (m31)
fvertical translation (m32)

◆ translate()

void canvas_ity::canvas::translate ( float  x,
float  y 
)

Translate the current transform.

By default, positive x values shift that many pixels to the right, while negative y values shift left, positive y values shift up, and negative y values shift down.

Parameters
xamount to shift horizontally
yamount to shift vertically

◆ width()

int canvas_ity::canvas::width ( )
inline

Member Data Documentation

◆ bitmap

rgba* canvas_ity::canvas::bitmap
private

◆ face

font_face canvas_ity::canvas::face
private

◆ fill_brush

paint_brush canvas_ity::canvas::fill_brush
private

◆ forward

affine_matrix canvas_ity::canvas::forward
private

◆ global_alpha

float canvas_ity::canvas::global_alpha
private

◆ global_composite_operation

composite_operation canvas_ity::canvas::global_composite_operation

Compositing operation for blending new drawing and old pixels.

The source_copy, source_in, source_out, destination_atop, and destination_in operations may clear parts of the canvas outside the new drawing but within the clip region. Defaults to source_over.

source_atop: Show new over old where old is opaque. source_copy: Replace old with new. source_in: Replace old with new where old was opaque. source_out: Replace old with new where old was transparent. source_over: Show new over old. destination_atop: Show old over new where new is opaque. destination_in: Clear old where new is transparent. destination_out: Clear old where new is opaque. destination_over: Show new under old. exclusive_or: Show new and old but clear where both are opaque. lighter: Sum the new with the old.

◆ image_brush

paint_brush canvas_ity::canvas::image_brush
private

◆ inverse

affine_matrix canvas_ity::canvas::inverse
private

◆ line_cap

cap_style canvas_ity::canvas::line_cap

Cap style for the ends of open subpaths and dash segments.

The actual shape may be affected by the current transform at the time of drawing. Only affects stroking. Defaults to butt.

butt: Use a flat cap flush to the end of the line. square: Use a half-square cap that extends past the end of the line. circle: Use a semicircular cap.

◆ line_dash

std::vector<float> canvas_ity::canvas::line_dash
private

◆ line_dash_offset

float canvas_ity::canvas::line_dash_offset

Offset where each subpath starts the dash pattern.

Changing this shifts the location of the dashes along the path and animating it will produce a marching ants effect. Only affects stroking and only when a dash pattern is set. May be negative or exceed the length of the dash pattern, in which case it will wrap. Defaults to 0.0.

◆ line_join

join_style canvas_ity::canvas::line_join

Join style for connecting lines within the paths.

The actual shape may be affected by the current transform at the time of drawing. Only affects stroking. Defaults to miter.

miter: Continue the ends until they intersect, if within miter limit. bevel: Connect the ends with a flat triangle. round: Join the ends with a circular arc.

◆ line_width

float canvas_ity::canvas::line_width
private

◆ lines

line_path canvas_ity::canvas::lines
private

◆ mask

pixel_runs canvas_ity::canvas::mask
private

◆ miter_limit

float canvas_ity::canvas::miter_limit
private

◆ path

bezier_path canvas_ity::canvas::path
private

◆ runs

pixel_runs canvas_ity::canvas::runs
private

◆ saves

canvas* canvas_ity::canvas::saves
private

◆ scratch

line_path canvas_ity::canvas::scratch
private

◆ shadow

std::vector<float> canvas_ity::canvas::shadow
private

◆ shadow_blur

float canvas_ity::canvas::shadow_blur
private

◆ shadow_color

rgba canvas_ity::canvas::shadow_color
private

◆ shadow_offset_x

float canvas_ity::canvas::shadow_offset_x

Horizontal offset of the shadow in pixels.

Negative shifts left, positive shifts right. This is not affected by the current transform. Defaults to 0.0 (no offset).

◆ shadow_offset_y

float canvas_ity::canvas::shadow_offset_y

Vertical offset of the shadow in pixels.

Negative shifts up, positive shifts down. This is not affected by the current transform. Defaults to 0.0 (no offset).

◆ size_x

int canvas_ity::canvas::size_x
private

◆ size_y

int canvas_ity::canvas::size_y
private

◆ stroke_brush

paint_brush canvas_ity::canvas::stroke_brush
private

◆ text_align

align_style canvas_ity::canvas::text_align

Horizontal position of the text relative to the anchor point.

When drawing text, the positioning of the text relative to the anchor point includes the side bearings of the first and last glyphs. Defaults to leftward.

leftward: Draw the text's left edge at the anchor point. rightward: Draw the text's right edge at the anchor point. center: Draw the text's horizontal center at the anchor point. start: This is a synonym for leftward. ending: This is a synonym for rightward.

◆ text_baseline

baseline_style canvas_ity::canvas::text_baseline

Vertical position of the text relative to the anchor point.

Defaults to alphabetic.

alphabetic: Draw with the alphabetic baseline at the anchor point. top: Draw the top of the em box at the anchor point. middle: Draw the exact middle of the em box at the anchor point. bottom: Draw the bottom of the em box at the anchor point. hanging: Draw 60% of an em over the baseline at the anchor point. ideographic: This is a synonym for bottom.


The documentation for this class was generated from the following file: