Graphics & compute

CDraw

Draw paths, shapes, text, and images into an RGBA bitmap.

C++23 mc/CDraw.h macOS
#include <mc/CDraw.h>

Link Catalyst::MacShared or Catalyst::MacStatic. A supplied output buffer is borrowed and must hold width × height × 4 bytes.

Image coordinates and sizes are in pixels. Colors use four normalized RGBA components. Pair saved graphics states with restores.

Jump to a declaration · 62

CDraw

class CDraw

Types, constants & data

enum class BlendMode { Normal, Additive, Multiply, Screen };
enum class TextAlign { Left, Center, Right };
enum class TextBaseline { Top, Middle, Bottom };

Methods

CDraw

CDraw(size_t width, size_t height, unsigned char* buf = nullptr);
CDraw(const CDraw&) = delete;

Creates a drawing surface of the requested pixel dimensions. Omit buf for owned storage, or provide borrowed storage of at least width × height × 4 bytes; call clear() to establish a background.

buffer

unsigned char* buffer();

Returns borrowed pixel storage, invalidated by destruction or resize.

setAntialiasing

void setAntialiasing(bool enabled);

Enables or disables antialiasing for subsequent drawing, trading sharper single-pixel edges against smoother shape boundaries.

clear

void clear();

Fills the whole bitmap with the current fill color and discards queued drawing commands. It does not reset the selected colors, font, transform, or clip.

render

void render();

Executes queued drawing and updates the CPU-accessible bitmap. Call after submitting shapes, text, or images and before exporting the result.

setFillColor

void setFillColor(const double4& rgba);

Selects normalized (red, green, blue, alpha) components for subsequent filled shapes and text. Each component must lie in [0, 1].

setStrokeColor

void setStrokeColor(const double4& rgba);

Selects normalized RGBA components for subsequent outlines and lines. Each component must lie in [0, 1].

setFillHSVA

void setFillHSVA(const double4& hsva);

Selects the fill color as normalized hue, saturation, value, and alpha. Hue spans one full turn over [0, 1], rather than degrees.

setStrokeHSVA

void setStrokeHSVA(const double4& hsva);

Selects the stroke color as normalized hue, saturation, value, and alpha. Each component must lie in [0, 1].

setLineWidth

void setLineWidth(double width);

Sets the width used by subsequent strokes and line segments. The width must be nonnegative.

fillRect

void fillRect(const double2& topLeft, const double2& size);
void fillRect(const double2& topLeft, const double2& size, double rotation);

Queues a filled rectangle using the current fill color. Positions and sizes are in pixels; the rotation overload takes radians.

strokeRect

void strokeRect(const double2& topLeft, const double2& size);
void strokeRect(const double2& topLeft, const double2& size, double rotation);

Queues a rectangle outline using the current stroke color and line width. The optional rotation is in radians.

fillArc

void fillArc(const double2& center, double radius, double startAngle, double endAngle);
void fillArc(const double2& center, double radius, double startAngle, double endAngle, double rotation);

Queues a filled circular sector about center. Start angle, end angle, and optional rotation are in radians; a full turn produces a filled circle.

strokeArc

void strokeArc(const double2& center, double radius, double startAngle, double endAngle);
void strokeArc(const double2& center, double radius, double startAngle, double endAngle, double rotation);

Queues a circular arc using the current stroke color and line width. Angles and optional rotation are in radians.

fillCircle

void fillCircle(const double2& center, double radius);

Queues a filled circle with the current fill color and pixel radius.

strokeCircle

void strokeCircle(const double2& center, double radius);

Queues a circle outline with the current stroke color, line width, and pixel radius.

moveTo

void moveTo(const double2& pos);

Sets the current pen position and records a path point without drawing a segment.

lineTo

void lineTo(const double2& pos);

Queues a line from the current pen position and advances the pen. If no pen position exists it only establishes one; it also records the endpoint for path operations.

line

void line(const double2& p1, const double2& p2);
void line(const double2& p1, const double2& p2, double rotation);

Queues a standalone segment using the current stroke settings. The rotation overload rotates the second endpoint about the first in radians.

curveTo

void curveTo(const double2& c1, const double2& c2, const double2& pos);

Queues a cubic Bézier curve from the current pen through two control points to pos, then updates the pen. With no current pen, it only establishes the endpoint.

setFont

void setFont(const cstr& fontName, double fontSize);

Selects the font name and size for subsequent text and measurement. Use getFonts() to inspect available family and face names.

text

void text(const cstr& text, const double2& boundsTopLeft, const double2& boundsSize, const double2& pos);
void text(const cstr& text, const double2& boundsTopLeft, const double2& boundsSize, const double2& pos, double rotation);

Queues text layout within the supplied bounds using the current font, fill color, alignment, and baseline. The position anchors the layout; optional rotation is in radians. The current text layout is byte-based and does not provide full Unicode shaping.

textBounds

void textBounds(const cstr& text, const double2& boundsTopLeft, const double2& boundsSize, const double2& pos, double2& topLeft, double2& size);
void textBounds(const cstr& text, const double2& boundsTopLeft, const double2& boundsSize, const double2& pos, double2& topLeft, double2& size, double rotation);

Calculates a bounding rectangle for text using the current font and layout size, without drawing. Empty layout returns zero size at pos; the current measurement ignores boundsTopLeft.

glyphPosition

bool glyphPosition(const cstr& text, const double2& boundsTopLeft, const double2& boundsSize, const double2& pos, size_t index, double2& topLeft, double2& size);

Returns the rectangle for a source-byte index in the laid-out text. Returns false without changing outputs when no glyph corresponds to that index; boundsTopLeft is currently ignored.

glyphIndex

int glyphIndex(const cstr& text, const double2& boundsTopLeft, const double2& boundsSize, const double2& pos);

Returns the source-byte index of the nearest laid-out glyph, or -1 for an empty layout. pos is the test point relative to a layout starting at zero; the current implementation ignores boundsTopLeft.

lineInfo

void lineInfo(const cstr& text, const double2& boundsTopLeft, const double2& boundsSize, const double2& pos, CVector<size_t>& lineLengths, CVector<double2>& linePositions);

Replaces the output vectors with wrapped line lengths and positions for the current font and layout. Lengths count the byte-based glyph entries, excluding explicit newline separators; boundsTopLeft is currently ignored.

toRGBA

static double4 toRGBA(const double4& hsva);

Converts normalized HSVA to normalized RGBA, preserving alpha. Hue uses a unit turn; input components outside [0, 1] are rejected.

toHSVA

static double4 toHSVA(const double4& rgba);

Converts normalized RGBA to normalized HSVA, preserving alpha. Hue is zero for achromatic colors and otherwise expressed as a unit turn.

getFonts

CVector<cstr> getFonts(bool withFaces);

Returns available font-family names. With withFaces enabled, the result also includes available family/style face names.

saveState

void saveState();

Pushes the current drawing settings, including colors, font, pen position, clipping, and transform. It does not snapshot bitmap contents or queued commands.

restoreState

void restoreState();

Restores and removes the most recently saved drawing state. An empty state stack is a no-op.

setClipRect

void setClipRect(const double2& topLeft, const double2& size);

Sets the rectangular clip used for subsequent commands, replacing the previous clip rectangle.

clearClip

void clearClip();

Disables rectangular clipping for subsequent drawing. It does not alter pixels already drawn.

setTransform

void setTransform(const double9& matrix);

Replaces the row-major 3×3 point transform. Translation occupies entries 2 and 5; the final row supplies homogeneous division. This is a point transform, not a guarantee that every size, radius, and text metric scales with it.

resetTransform

void resetTransform();

Restores the identity point transform for subsequent commands.

beginPath

void beginPath();

Clears the recorded path points and closure flag. It does not clear queued strokes or reset the pen; follow with moveTo() to establish the intended start.

closePath

void closePath();

Marks the recorded path as closed so a later strokePath() connects its final point to its first. It does not draw immediately.

strokePath

void strokePath();

Queues segments between recorded path points, including the closing segment when requested. The path remains recorded; lineTo() also queues its own stroke when called.

fillPath

void fillPath();

Currently calls strokePath() and only draws the outline. Filling an arbitrary path interior is not implemented.

quadTo

void quadTo(const double2& c, const double2& pos);

Queues a quadratic Bézier curve using one control point and advances the pen to pos. Call moveTo() first to establish its starting point.

arcTo

void arcTo(const double2& p1, const double2& p2, double radius);

Currently draws straight segments through p1 and p2 and ignores radius. A tangent circular arc is not implemented.

setDash

void setDash(const CVector<double>& pattern, double offset = 0.0);

Sets alternating dash and gap lengths for subsequent lines. Nonpositive entries are discarded and a negative offset is clamped to zero.

clearDash

void clearDash();

Restores solid lines by clearing the dash pattern and offset.

setGlobalAlpha

void setGlobalAlpha(double alpha);

Multiplies the alpha of subsequent drawing colors by this value, clamped to [0, 1].

setBlendMode

void setBlendMode(BlendMode mode);

Selects how subsequent drawing combines with existing pixels: normal alpha blending, additive, multiply, or screen.

setTextAlign

void setTextAlign(TextAlign align);

Selects whether text layout is anchored left, centered, or right relative to its supplied position and bounds.

setTextBaseline

void setTextBaseline(TextBaseline baseline);

Selects top, middle, or bottom positioning of text relative to its anchor.

measureGlyphAdvances

void measureGlyphAdvances(const cstr& text, CVector<double>& advances);

Replaces the output with one horizontal advance per input byte for the current font. Missing glyphs receive zero; these values are not a Unicode grapheme layout.

drawImage

void drawImage(unsigned char* data, const double2& srcTopLeft, const double2& srcSize, const double2& dstTopLeft, const double2& dstSize);

Copies pixels during the call. srcSize is the full source image size; srcTopLeft crops to the bottom-right edge. dstSize controls scaling.

setImageSmoothing

void setImageSmoothing(bool enabled);

Selects smooth interpolation for subsequent scaled images; disabling it uses nearest sampling.

hitTestPath

bool hitTestPath(const double2& point) const;

Tests whether a point lies inside the polygon formed by the recorded path points. This does not render or consume the path.

hitTestStroke

bool hitTestStroke(const double2& point) const;

Tests whether a point is close enough to a recorded path segment under the current line width. This does not render or consume the path.

flush

void flush();

Flushes pending drawing to the bitmap.

resize

void resize(size_t width, size_t height, unsigned char* buf = nullptr);

Changes the bitmap dimensions. A replacement external buffer is borrowed.