Foundation
Table of Contents
The Foundation classes provide support for the Artist library, especially for
the canvas class.
point
The point indicates position in the 2D cartesian coordinate space,
represented by the x and y coordinates:
Declaration
struct point
{
point();
point(float x, float y);
point(point const&) = default;
point& operator=(point const &) = default;
bool operator==(point const& other) const;
bool operator!=(point const& other) const;
point move(float dx, float dy) const;
point move_to(float x, float y) const;
point reflect(point p) const;
float x;
float y;
};
Expressions
Notation
x, y
|
Scalar coordinates. |
dx, dy
|
Relative scalar coordinates. |
p, p2
|
Instance of point
|
Constructors and assignment
// Default constructor [1].
point{}
// Constructor [2].
point{ x, y }
// Copy constructor. [3]
point{ p }
// Assignment [4]
p = p2
Semantics
- Default construct a
pointwith initial values{ 0, 0 } - Construct a
pointgiven initial valuesx, andy. - Copy construct a
point,p. - Assign
p2, top.
Equality
// Equality [1]
p == p2
// Non-equality [2]
p != p2
Semantics
- Returns
trueifp2, is equal top. - Returns
trueifp2, is not equal top.
Transformation
// Move [1]
p.move(dx, dy)
// Move To [2]
p.move_to(x, y)
// Reflection [3]
p.reflect(p2)
Semantics
- Move
pdxanddydistance relative top. Non-mutating. Returns the moved instance ofpoint. - Move
pto absolute coordinatesxandy. Non-mutating. Returns the moved instance ofpoint. - Reflect a
point,p, at 180 degree rotation of point,point. Non-mutating. Returns the reflected instance ofpoint.
Member access
p.x
p.y
Semantics
- Direct access to members
xandy.
extent
The extent struct is a specialization of point but deletes the members
move, move_to, and reflect. extent is intended for specifying
2-dimensional sizes.
Declaration
struct extent : point
{
using point::point;
extent(point p);
point move(float dx, float dy) const = delete;
point move_to(float x, float y) const = delete;
point reflect(point p) const = delete;
};
rect
The rect struct represents a 2-dimensional rectangle specified by the
left, top, right, and bottom coordinates.
Declaration
struct rect
{
rect();
rect(float left, float top, float right, float bottom);
rect(point origin, float right, float bottom)
rect(point top_left, point bottom_right);
rect(float left, float top, extent size);
rect(point origin, extent size);
rect(rect const&) = default;
rect& operator=(rect const&) = default;
bool operator==(rect const& other) const;
bool operator!=(rect const& other) const;
bool is_empty() const;
bool includes(point p) const;
bool includes(rect const& other) const;
float width() const;
void width(float w);
float height() const;
void height(float h);
extent size() const;
point top_left() const;
point bottom_right() const;
point top_right() const;
point bottom_left() const;
rect move(float dx, float dy) const;
rect move_to(float x, float y) const;
rect inset(float x = 1.0, float y = 1.0) const;
float left;
float top;
float right;
float bottom;
};
Free Functions
bool is_valid(rect const& r);
bool is_same_size(rect const& a, rect const& b);
bool intersects(rect const& a, rect const& b);
point center_point(rect const& r);
float area(rect const& r);
rect union_(rect const& a, rect const& b);
rect intersection(rect const& a, rect const& b);
rect center(rect const& r, rect const& encl);
rect center_h(rect const& r, rect const& encl);
rect center_v(rect const& r, rect const& encl);
rect align(rect const& r, rect const& encl, float x, float y);
rect align_h(rect const& r, rect const& encl, float x);
rect align_v(rect const& r, rect const& encl, float y);
Expressions
Notation
left, top, right, bottom
|
Scalar coordinates. |
x, y, w, h
|
Scalar coordinates. |
dx, dy
|
Relative scalar coordinates. |
origin, top_left, bottom_right
|
Instance of point. |
size |
Instance of extent. |
r, r2
|
Instance of rect. |
Constructors and assignment
// Default constructor [1].
rect{}
// Constructor [2].
rect{ left, top, right, bottom }
// Constructor [3].
rect{ origin, right, bottom }
// Constructor [4].
rect{ top_left, bottom_right }
// Constructor [5].
rect{ left, top, size }
// Constructor [6].
rect{ origin, size }
// Copy constructor [7].
rect{ r }
// Assignment [8]
r = r2
Semantics
- Default construct a
rectwith initial values{ 0, 0, 0, 0 } - Construct a
rectgiven initial valuesleft,top,right, andbottom. - Construct a
rectgivenorigin(point),rightand bottom`. - Construct a
rectgiventop_left,bottom_rightpoints. - Construct a
rectgivenleft,top, andsize(extent). - Construct a
rectgivenorigin, andsize(extent). - Copy construct a
rect,r. - Assign
r2tor.
Equality
// Equality [1]
r == r2
// Non-equality [2]
r != r2
Semantics
- Returns
trueif ar2is equalr. - Returns
trueif ar2is not equal tor.
Query
// Check for empty rectangle [1]
r.empty()
// Check for inclusion [2]
r.includes(p)
r.includes(r2)
// Get the width and height [3]
r.width()
r.height()
// Get the size [4]
r.size()
// Get the rectangle corners [5]
r.top_left()
r.bottom_right()
r.top_right()
r.bottom_left()
Semantics
- Returns
trueifris empty. Equivalent tor.size() == extent{ 0, 0 }. - Inclusion:
- Returns true if
pis insider. - Returns true if
r2is completely insider.
- Returns true if
- Returns the width and height of the
r. Returns a scalar value. - Get the size of
r. Returns an instance ofextent. - Get the top-left, bottom-right, top-right, and bottom-left corners or
rectangle,
r. Returns an instance ofpoint.
Mutation
// Set the width and height [1]
r.height(w)
r.width(h)
Semantics
- Sets the width and height of
rtowandh, respectively.
Transformation
// Move [1]
r.move(dx, dy)
// Move To [2]
r.move_to(x, y)
// Inset [3]
r.inset(x, y)
r.inset(xy)
Semantics
- Move origin of
rbydxanddydistance. Non-mutating. Returns the moved instance ofrect. - Move origin of
rto absolute coordinatesxandy. Non-mutating. Returns the moved instance ofrect. - Inset
rbyxandy(in each dimension), or byxy(in both x and y dimensions). The rectris shrunk ifx,yorxyare positive, otherwise, expanded if negative. Non-mutating. Returns the inset instance ofrect.
Free Query Functions
// Check for validity [1]
is_valid(r)
// Check for size equality [2]
is_same_size(r, r2)
// Check for intersection [3]
intersects(r, r2)
// Compute the center point [4]
center_point(r)
// Compute the area [5]
area(r)
// Compute the union of two rectangles [6]
union_(r, r2)
// Compute the intersection of two rectangles [7]
intersection(r, r2)
Semantics
- Return
trueifris a valid rectangle (left <= right && top <= bottom). - Return
trueifrandr2have the same size. Equivalent tor.size() == r2.size(). - Return
trueifrintersects withr2. - Returns the center point of
r. returns an instance ofpoint. - Returns the area of
r. Equivalent tor.width() * r.height(). Returns a scalar value. - Compute the union of two rectangles. Returns an instance of
rect. - Compute the intersection of two rectangles. Returns an instance of
rect.
Free Transformation Functions
// Center a rectangle [1]
center(r, r2)
center_h(r, r2)
center_v(r, r2)
// Align a rectangle [2]
align(r, r2, x, y)
align_h(r, r2, x)
align_v(r, r2, y)
Semantics
- Center
rinr2. Returns an instance ofrect.-
center: In both dimensions -
center_h: Horizontally -
center_v: Vertically
-
- Align
rinr2. Given scalarxandyvalues, 0.0 alignsrto the left or top, 1.0 alignsrto the right or bottom, 0.5 alignsrcenter or middle. Returns an instance ofrect.-
align: In both dimensions byxandy, wherexandyare fractional values from 0.0 to 1.0. -
align_h: Horizontally byx, wherexis a fractional value from 0.0 to 1.0. -
align_v: Vertically byy, whereyis a fractional value from 0.0 to 1.0.
-
Member Access
// Member access [1]
r.left
r.top
r.right
r.bottom
Semantic
- Direct access to members
left,top,right, andbottom
circle
The circle is represented by a center point and radius:
struct circle
{
circle();
circle(float cx, float cy, float radius);
circle(point c, float radius);
circle(rect const& r);
circle(circle const&) = default;
circle& operator=(circle const&) = default;
rect bounds() const;
bool operator==(circle const& other) const;
bool operator!=(circle const& other) const;
point center() const;
circle inset(float x) const;
circle move(float dx, float dy) const;
circle move_to(float x, float y) const;
float cx;
float cy;
float radius;
};
Expressions
// Default constructor [1].
circle{}
// Constructor [2].
circle{ x, y, radius }
// Copy constructor. [3]
circle{ c }
// Assignment [4]
c = c2
// Equality [5]
c == c2
// Non-equality [6]
c != c2
// Bounds [7]
c.bounds()
// Get the center [8]
c.center()
// Inset [9]
c.inset(x)
// Move [10]
c.move(dx, dy)
// Move To [11]
c.move_to(x, y)
// Member access [12]
c.cx
c.cy
c.radius
Notation
x, y, radius
|
Scalar coordinates. |
dx, dy, radius
|
Scalar coordinates. |
c, c2
|
Instance of circle. |
Semantics
- Default construct a
circlewith initial values{ 0, 0, 0 } - Construct a
circlegiven initial valuesx,y, andradius. - Copy construct a
circle,c. - Assign
c2toc. - Returns
trueif ac2is equalc. - Returns
trueif ac2is not equal toc. - Get the bounds: smallest
rectthat enclosesc. Returns an instance ofrect. - Get the center of
c. Returns an instance ofpoint. - Inset
cbyx. - Move center of
cbydxanddydistance. Returns the moved instance ofcircle. - Move center of
cto absolute coordinatesxandy. Returns the moved instance ofcircle. - Direct access to members
cx,cy, andradius
color
Color is represented by red, green, blue, and alpha:
struct color
{
color();
color(float red, float green, float blue, float alpha = 1.0f);
color opacity(float alpha_) const;
color level(float amount) const;
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
float alpha = 0.0f;
};
Free Functions
bool operator==(color const& a, color const& b);
bool operator!=(color const& a, color const& b);
color rgb(std::uint32_t rgb);
color rgba(std::uint32_t rgba);
color rgb(std::uint8_t r, std::uint8_t g, std::uint8_t b);
color rgba(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a);
color hsl(float h, float sl, float l);
Expressions
// Default constructor [1].
color{}
// Constructor [2].
color{ red, green, blue, alpha }
// Copy constructor. [3]
color{ c }
// Assignment [4]
c = c2
// Equality [5]
c == c2
// Non-equality [6]
c != c2
// Opacity
opacity(val) [7]
// Linear level
level(amount) [8]
// RGB and RGBA from uint32_t [9]
rgb(urgb);
rgba(urgb);
// RGB and RGBA from uint8_t reg, green, blue, and alpha components [10]
rgb(ured, ugreen, ublue);
rgba(ured, ugreen, ublue, ualpha);
// RGB from HSL [11]
hsl(h, sl, l);
Notation
red, red, red, alpha
|
Scalar values. |
val, amount
|
Scalar values. |
c, c2
|
Instance of color. |
urgb |
Instance of `std::uint32_t |
ured, ugreen, ublue
|
Instance of `std::uint8_t |
Semantics
- Default construct a
colorwith initial values{ 0, 0, 0, 0 } - Construct a
colorgiven initial valuesred,green,blue, andalpha. The floating point values range from 0.0 to 1.0. - Copy construct a
color,c. - Assign
c2toc. - Returns
trueif ac2is equalc. - Returns
trueif ac2is not equal toc. - Sets the opacity (alpha) to
val. Returns an instance ofcolor. - Multiplies all color components by
amount. Returns an instance ofcolor. - Create an RGB or RGBA color from a
uint32_tor the form rrggbb or rrggbbaa respectively, typically with using hex literals (e.g. 0xffffffff). - Create an RGB or RGBA color from
uint8_treg, green, blue, and alpha components, where each component range from0to255. - Create an RBB color from HSL.
Predefined Colors
Namespace cycfi::artist::colors contains some predefined colors, including
256 levels of grays. You normally use the colors, by hoisting the namespace
into your own namespace:
namespace colors = cycfi::artist::colors;
The colors provided are from CSS: Named Colors and Hex Equivalents
The colors
alice_blue,
antique_white,
aquamarine,
azure,
beige,
bisque,
black,
blanched_almond,
blue,
blue_violet,
brown,
burly_wood,
cadet_blue,
chartreuse,
chocolate,
coral,
cornflower_blue,
corn_silk,
cyan,
dark_goldenrod,
dark_green,
dark_khaki,
dark_olive_green,
dark_orange,
dark_orchid,
dark_salmon,
dark_sea_green,
dark_slate_blue,
dark_slate_gray,
dark_turquoise,
dark_violet,
deep_pink,
deep_sky_blue,
dim_gray,
dodger_blue,
firebrick,
floral_white,
forest_green,
gains_boro,
ghost_white,
gold,
goldenrod,
green,
green_yellow,
honeydew,
hot_pink,
indian_red,
ivory,
khaki,
lavender,
lavender_blush,
lawn_green,
lemon_chiffon,
light_blue,
light_coral,
light_cyan,
light_goldenrod,
light_goldenrod_yellow,
light_gray,
light_pink,
light_salmon,
light_sea_green,
light_sky_blue,
light_slate_blue,
light_slate_gray,
light_steel_blue,
light_yellow,
lime_green,
linen,
magenta,
maroon,
medium_aquamarine,
medium_blue,
medium_forest_green,
medium_goldenrod,
medium_orchid,
medium_purple,
medium_sea_green,
medium_slate_blue,
medium_spring_green,
medium_turquoise,
medium_violet_red,
midnight_blue,
mint_cream,
misty_rose,
moccasin,
navajo_white,
navy,
navy_blue,
old_lace,
olive_drab,
orange,
orange_red,
orchid,
pale_goldenrod,
pale_green,
pale_turquoise,
pale_violet_red,
papaya_whip,
peach_puff,
peru,
pink,
plum,
powder_blue,
purple,
red,
rosy_brown,
royal_blue,
saddle_brown,
salmon,
sandy_brown,
sea_green,
sea_shell,
sienna,
sky_blue,
slate_blue,
slate_gray,
snow,
spring_green,
steel_blue,
tan,
thistle,
tomato,
turquoise,
violet,
violet_red,
wheat,
white,
white_smoke,
yellow,
yellow_green
// greys
gray[0] ... gray[255]
auto grey = gray; // Synonym
font_descr
font_desr is a lightweight class that describes a font, including its
weight (e.g. bold), slant (e.g. italic), and stretch (e.g. condensed).
namespace font_constants
{
enum weight_enum
{
thin = 10,
extra_light = 20,
light = 30,
weight_normal = 40,
medium = 50,
semi_bold = 60,
bold = 70,
extra_bold = 80,
black = 90,
extra_black = 95,
};
enum slant_enum
{
slant_normal = 0,
italic = 90,
oblique = 100
};
enum stretch_enum
{
ultra_condensed = 25,
extra_condensed = 31,
condensed = 38,
semi_condensed = 44,
stretch_normal = 50,
semi_expanded = 57,
expanded = 63,
extra_expanded = 75,
ultra_expanded = 100
};
}
struct font_descr
{
font_descr normal() const;
font_descr size(float size_) const;
font_descr weight(font_constants::weight_enum w) const;
font_descr thin() const;
font_descr extra_light() const;
font_descr light() const;
font_descr weight_normal() const;
font_descr medium() const;
font_descr semi_bold() const;
font_descr bold() const;
font_descr extra_bold() const;
font_descr black() const;
font_descr extra_black() const;
font_descr style(font_constants::slant_enum s) const;
font_descr slant_normal() const;
font_descr italic() const;
font_descr oblique() const;
font_descr stretch(font_constants::stretch_enum s) const;
font_descr ultra_condensed() const;
font_descr extra_condensed() const;
font_descr condensed() const;
font_descr semi_condensed() const;
font_descr stretch_normal() const;
font_descr semi_expanded() const;
font_descr expanded() const;
font_descr extra_expanded() const;
font_descr ultra_expanded() const;
using slant_enum = font_constants::slant_enum;
std::string_view _families;
float _size = 12;
uint8_t _weight = font_constants::weight_normal;
slant_enum _slant = font_constants::slant_normal;
uint8_t _stretch = font_constants::stretch_normal;
};
Expressions
// Constructors [1].
font_descr{}
font_descr{ families }
font_descr{ families, sz }
font_descr{ families, sz, wt }
font_descr{ families, sz, wt, sl }
font_descr{ families, sz, wt, sl, str }
// Normal [2]
f.normal()
// Sized [3]
f.size(size)
// Weight [4]
f.weight(wt)
f.thin()
f.extra_light()
f.light()
f.weight_normal()
f.medium()
f.semi_bold()
f.bold()
f.extra_bold()
f.black()
f.extra_black()
// Slant [5]
f.style(sl)
f.slant_normal()
f.italic()
f.oblique()
// Stretch [6]
f.stretch(str)
f.ultra_condensed()
f.extra_condensed()
f.condensed()
f.semi_condensed()
f.stretch_normal()
f.semi_expanded()
f.expanded()
f.extra_expanded()
f.ultra_expanded()
// Member access [7]
f._families
f._size
f._weight
f._slant
f._stretch
Notation
families |
Comma separated list of font families to search for. |
sz |
A scalar value. |
wt |
A weight_enum (see font_constants above) or a scalar value. |
sl |
A slant_enum (see font_constants above) or a scalar value. |
str |
A stretch_enum (see font_constants above) or a scalar value. |
f |
An instance of font_descr. |
![]()
familiesis a comma separated list of font families to search for. Given afont_descr, thefontclass will search the system for a match, iterating over the list of font families, and will load first available font that matches the font descriptor.
Semantics
- Constructors for the
font_descrstruct given initial values. - Returns a copy of the
font_desr,f, with default values. Returns instance offont_descr. - Returns a copy of the
font_desr,f, with the given font size. Returns instance offont_descr. - Returns a copy of the
font_desr,f, with the given font weight. Returns instance offont_descr. - Returns a copy of the
font_desr,f, with the given font slant. Returns instance offont_descr. - Returns a copy of the
font_desr,f, with the given font stretch. Returns instance offont_descr. - Direct access to members.
The syntax allows for concatenation. For example:
font_descr{ "Open Sans", 14 }.bold().italic()
font
The font loads a system or application font given a font descriptor.
class font
{
public:
font();
font(font_descr descr);
font(font const& rhs);
font(font&& rhs) noexcept;
~font();
font& operator=(font const& rhs);
font& operator=(font&& rhs) noexcept;
explicit operator bool() const;
font_impl_ptr impl() const;
struct metrics_info
{
float ascent;
float descent;
float leading;
};
metrics_info metrics() const;
float line_height() const;
float measure_text(std::string_view str) const;
};
Expressions
// Default constructor [1].
font{}
// Constructor [2].
font{ fd }
// Copy constructor. [3]
font{ f }
// Assignment [4]
f = f2
// Validity Check [5]
bool(f)
// Access to font handle [6]
f.impl()
// Font metrics [7]
f.metrics()
f.line_height()
// Measuring text [8]
f.measure_text(str)
Notation
fd |
Instance of font_descr
|
f |
Instance of font
|
str |
Instance of std::string_view
|
Semantics
- Default construct a
font. The default constructed font is invalid. - Construct a
pointgiven a font descriptorfont_descr,fd. - Copy construct a
font,f. - Assign
f2, tof. - Check if a
fis valid. Returnstrueiffis valid. - Get the backend-specific font handle.
- Get the font metrics (
ascent,descent,leadingand theline_height).-
f.metrics()returns an instance offont::metrics_info. -
f.line_height()returns a scalar value.
-
- Measure the length the given string,
str, will occupy when drawn using the fontf. This does not take into account thecanvas’ transformation matrix. Returns a scalar value.
resources
Resources (e.g. images) that are identified by file names can be absolute or relative paths. For relative paths, a vector of paths is searched, in the order they appear in the vector. Platform code provides the initial paths as appropriate. Applications may add additional paths as needed, using the add_search_path function. search_first=true puts the path at the top of the vector and will be searched first.
void add_search_path(fs::path const& path, bool search_first = false);
fs::path find_file(fs::path const& file);
fs::path app_data_path();
Expressions
// Add a search path [1]
add_search_path(path)
add_search_path(path, true)
// Search for a file [2]
find_file(path)
Notation
path |
Instance of std::filesystem::path
|
Semantics
- Add a directory search path. This path, and all paths added via
add_search_path, will be used when searching for a resource file. The second variant, with the second argument, specifies if the directory search path will be pushed into the end (defaultfalse) or pushed to the front (second argument istrue) of the list. Iftrue, the directory search path will be searched first. - Find a resource file by searching the list of directory search paths.
Returns an empty path if file is not found. returns an instance of
std::filesystem::path.