CDraw
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.
Graphics & compute
Draw paths, shapes, text, and images into an RGBA bitmap.
#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.
class CDrawenum class BlendMode { Normal, Additive, Multiply, Screen };
enum class TextAlign { Left, Center, Right };
enum class TextBaseline { Top, Middle, Bottom };
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.
~CDraw();
size_t width() const;
size_t height() const;
unsigned char* buffer();
Returns borrowed pixel storage, invalidated by destruction or resize.
void setAntialiasing(bool enabled);
Enables or disables antialiasing for subsequent drawing, trading sharper single-pixel edges against smoother shape boundaries.
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.
void render();
Executes queued drawing and updates the CPU-accessible bitmap. Call after submitting shapes, text, or images and before exporting the result.
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].
void setStrokeColor(const double4& rgba);
Selects normalized RGBA components for subsequent outlines and lines. Each component must lie in [0, 1].
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.
void setStrokeHSVA(const double4& hsva);
Selects the stroke color as normalized hue, saturation, value, and alpha. Each component must lie in [0, 1].
void setLineWidth(double width);
Sets the width used by subsequent strokes and line segments. The width must be nonnegative.
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.
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.
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.
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.
void fillCircle(const double2& center, double radius);
Queues a filled circle with the current fill color and pixel radius.
void strokeCircle(const double2& center, double radius);
Queues a circle outline with the current stroke color, line width, and pixel radius.
void moveTo(const double2& pos);
Sets the current pen position and records a path point without drawing a segment.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Returns available font-family names. With withFaces enabled, the result also includes available family/style face names.
void saveState();
Pushes the current drawing settings, including colors, font, pen position, clipping, and transform. It does not snapshot bitmap contents or queued commands.
void restoreState();
Restores and removes the most recently saved drawing state. An empty state stack is a no-op.
void setClipRect(const double2& topLeft, const double2& size);
Sets the rectangular clip used for subsequent commands, replacing the previous clip rectangle.
void clearClip();
Disables rectangular clipping for subsequent drawing. It does not alter pixels already drawn.
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.
void resetTransform();
Restores the identity point transform for subsequent commands.
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.
void closePath();
Marks the recorded path as closed so a later strokePath() connects its final point to its first. It does not draw immediately.
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.
void fillPath();
Currently calls strokePath() and only draws the outline. Filling an arbitrary path interior is not implemented.
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.
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.
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.
void clearDash();
Restores solid lines by clearing the dash pattern and offset.
void setGlobalAlpha(double alpha);
Multiplies the alpha of subsequent drawing colors by this value, clamped to [0, 1].
void setBlendMode(BlendMode mode);
Selects how subsequent drawing combines with existing pixels: normal alpha blending, additive, multiply, or screen.
void setTextAlign(TextAlign align);
Selects whether text layout is anchored left, centered, or right relative to its supplied position and bounds.
void setTextBaseline(TextBaseline baseline);
Selects top, middle, or bottom positioning of text relative to its anchor.
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.
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.
void setImageSmoothing(bool enabled);
Selects smooth interpolation for subsequent scaled images; disabling it uses nearest sampling.
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.
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.
void flush();
Flushes pending drawing to the bitmap.
void resize(size_t width, size_t height, unsigned char* buf = nullptr);
Changes the bitmap dimensions. A replacement external buffer is borrowed.