Cairo
Thin FFI bindings over the Cairo 2D graphics library.
The goal of this module is to expose Cairo's C API with Carp-friendly naming (kebab-case, grouped under Cairo), without adding a higher-level wrapper on top. Higher-level sketching primitives live in anima.carp.
Installation
Cairo must be installed on the system and discoverable via pkg-config:
brew install cairo # macOS
apt install libcairo2-dev # Debian / Ubuntu
Example
(load "git@github.com:carpentry-org/cairo.carp@0.3.0")
(defn main []
(let-do [surf (Cairo.image-surface-create Cairo.format-argb32 200 200)
cr (Cairo.create surf)]
(Cairo.set-source-rgb cr 1.0 1.0 1.0)
(Cairo.paint cr)
(Cairo.set-source-rgb cr 0.0 0.0 0.0)
(Cairo.set-line-width cr 4.0)
(Cairo.move-to cr 20.0 20.0)
(Cairo.line-to cr 180.0 180.0)
(Cairo.stroke cr)
(ignore (Cairo.surface-write-to-png surf (cstr "out.png")))
(Cairo.destroy cr)
(Cairo.surface-destroy surf)))
Memory management
Cairo uses reference counting internally; this binding does not manage
lifetimes for you. Every Cairo.create must be paired with a Cairo.destroy,
every *-surface-create with a Cairo.surface-destroy, and every
Cairo.region-create* with a Cairo.region-destroy. Cairo.pop-group hands
back a pattern that needs a Cairo.pattern-destroy, where
Cairo.pop-group-to-source disposes of it for you. See the Cairo
manual for details.
=
(Fn [CairoStatus, CairoStatus] Bool)
(= a b)
Equality on status codes, implemented by converting to Int first.
append-path
(Fn [(Ptr CairoContext), (Ref (Array CairoPathElement) a)] ())
(append-path cr elements)
Append elements to the context's current path, interpreting their
coordinates in the context's current user space.
arc
(Fn [(Ptr CairoContext), Double, Double, Double, Double, Double] ())
Add a circular arc centered at (xc yc) with radius, sweeping from angle1 to angle2 radians (positive = clockwise in Cairo's default orientation).
copy-path
(Fn [(Ptr CairoContext)] (Result (Array CairoPathElement) CairoStatus))
(copy-path cr)
Return the current path as an array of CairoPathElements, in user-space
coordinates, or the context's error status in the error branch.
Cairo reports a close-path as a ClosePath followed by a MoveTo back to the
start of the closed subpath, which is where the current point ends up; both are
surfaced as-is.
copy-path-flat
(Fn [(Ptr CairoContext)] (Result (Array CairoPathElement) CairoStatus))
(copy-path-flat cr)
Like copy-path, but with every curve approximated by a series of LineTo
elements within tolerance. The result never contains a CurveTo.
create
(Fn [(Ptr CairoSurface)] (Ptr CairoContext))
Create a drawing context targeting the given surface. Must be paired with destroy.
curve-to
(Fn [(Ptr CairoContext), Double, Double, Double, Double, Double, Double] ())
Append a cubic Bézier spline through (x1 y1), (x2 y2), (x3 y3).
device-to-user
(Fn [(Ptr CairoContext), Double, Double] CairoPoint)
Convert the point (x y) from device space to user space.
device-to-user-distance
(Fn [(Ptr CairoContext), Double, Double] CairoPoint)
Convert the vector (dx dy) from device space to user space, ignoring translation.
fill-extents
(Fn [(Ptr CairoContext)] CairoExtents)
Compute the bounding box of the area that would be affected by fill.
font-extents
(Fn [(Ptr CairoContext)] CairoFontExtents)
Return a CairoFontExtents for the current font (ascent, descent, height, max advances).
font-face-destroy
(Fn [(Ptr CairoFontFace)] ())
Drop a reference to a font face. Pairs with toy-font-face-create and font-face-reference.
font-face-reference
(Fn [(Ptr CairoFontFace)] (Ptr CairoFontFace))
Take an additional reference to a font face and return it. Each reference needs its own font-face-destroy.
format-argb32
CairoFormat
32-bit ARGB, native-endian. Matches SDL_PIXELFORMAT_ARGB8888 on little-endian.
get-current-point-x
(Fn [(Ptr CairoContext)] Double)
Return the x coordinate of the current point. Undefined if has-current-point? is false.
get-current-point-y
(Fn [(Ptr CairoContext)] Double)
Return the y coordinate of the current point. Undefined if has-current-point? is false.
get-dash
(Fn [(Ptr CairoContext)] (Array Double))
Return the current dash pattern as an array of doubles.
get-dash-count
(Fn [(Ptr CairoContext)] Int)
Return the number of dash segments in the current dash pattern (0 means solid).
get-dash-offset
(Fn [(Ptr CairoContext)] Double)
Return the offset into the dash pattern set by set-dash.
get-font-face
(Fn [(Ptr CairoContext)] (Ptr CairoFontFace))
Return the context's current font face. The context keeps ownership; take a font-face-reference to hold on to it past the next set-font-face.
get-group-target
(Fn [(Ptr CairoContext)] (Ptr CairoSurface))
Return the surface drawing currently goes to: the innermost group's surface, or the context's target if no group is active. It is owned by the context and must not be destroyed.
get-matrix
(Fn [(Ptr CairoContext)] CairoMatrix)
Return the context's current transformation matrix.
get-scaled-font
(Fn [(Ptr CairoContext)] (Ptr CairoScaledFont))
Return the scaled font the context would use for text right now, combining its font face, size, and transformation. The context keeps ownership.
glyph-extents
(Fn [(Ptr CairoContext), (Ref (Array CairoGlyph) a)] CairoTextExtents)
Measure glyphs in the context's current font. Bearings and advances are relative to the first glyph's origin.
glyph-path
(Fn [(Ptr CairoContext), (Ref (Array CairoGlyph) a)] ())
Append the outlines of glyphs to the current path, so they can be filled, stroked, or clipped with.
has-current-point?
(Fn [(Ptr CairoContext)] Bool)
Return true if the context has a current point (after move-to or drawing operations).
image-surface-create
(Fn [CairoFormat, Int, Int] (Ptr CairoSurface))
Create an in-memory image surface of the given format, width, and height.
image-surface-create-from-png
(Fn [(Ptr CChar)] (Ptr CairoSurface))
Load a PNG file into a new image surface. Must be paired with surface-destroy.
image-surface-get-data
(Fn [(Ptr CairoSurface)] (Ptr Byte))
Return a raw pointer to the surface's pixel buffer. Must be preceded by surface-flush.
mask
(Fn [(Ptr CairoContext), (Ptr CairoPattern)] ())
Paint the current source through the alpha channel of pattern.
mask-surface
(Fn [(Ptr CairoContext), (Ptr CairoSurface), Double, Double] ())
Paint the current source through the alpha channel of surface, with the
surface's origin placed at (x y) in user space.
matrix-init-scale
(Fn [Double, Double] CairoMatrix)
Return a matrix that scales by sx in x and sy in y.
matrix-init-translate
(Fn [Double, Double] CairoMatrix)
Return a matrix that translates by (tx ty).
matrix-invert
(Fn [(Ref CairoMatrix a)] (Result CairoMatrix CairoStatus))
(matrix-invert m)
Invert m, or return status-invalid-matrix in the error branch if m is not invertible.
matrix-multiply
(Fn [(Ref CairoMatrix a), (Ref CairoMatrix b)] CairoMatrix)
Return the matrix that applies a first, then b.
matrix-rotate
(Fn [(Ref CairoMatrix a), Double] CairoMatrix)
Return m with a rotation by radians applied before it: points are rotated, then transformed by m.
matrix-scale
(Fn [(Ref CairoMatrix a), Double, Double] CairoMatrix)
Return m with a scale by (sx sy) applied before it: points are scaled, then transformed by m.
matrix-transform-distance
(Fn [(Ref CairoMatrix a), Double, Double] CairoPoint)
Transform the vector (dx dy) by m, ignoring translation. The components come back in the point's x and y.
matrix-transform-point
(Fn [(Ref CairoMatrix a), Double, Double] CairoPoint)
Transform the point (x y) by m.
matrix-translate
(Fn [(Ref CairoMatrix a), Double, Double] CairoMatrix)
Return m with a translation by (tx ty) applied before it: points are translated, then transformed by m.
paint
(Fn [(Ptr CairoContext)] ())
Paint the current source everywhere within the current clip region.
path-extents
(Fn [(Ptr CairoContext)] CairoExtents)
Compute the bounding box enclosing the current path, ignoring stroke parameters.
pattern-add-color-stop-rgb
(Fn [(Ptr CairoPattern), Double, Double, Double, Double] ())
Add an opaque color stop at offset (0.0–1.0) to a gradient pattern.
pattern-add-color-stop-rgba
(Fn [(Ptr CairoPattern), Double, Double, Double, Double, Double] ())
Add a translucent color stop at offset (0.0–1.0) to a gradient pattern.
pattern-create-for-surface
(Fn [(Ptr CairoSurface)] (Ptr CairoPattern))
Create a pattern from a surface, useful for tiling or compositing.
pattern-create-linear
(Fn [Double, Double, Double, Double] (Ptr CairoPattern))
Create a linear gradient pattern along the line from (x0 y0) to (x1 y1).
pattern-create-radial
(Fn [Double, Double, Double, Double, Double, Double] (Ptr CairoPattern))
Create a radial gradient between two circles: center (cx0 cy0) radius r0 and center (cx1 cy1) radius r1.
pattern-destroy
(Fn [(Ptr CairoPattern)] ())
Destroy a pattern created by pattern-create-*. Must be called after the pattern is no longer needed.
pattern-get-matrix
(Fn [(Ptr CairoPattern)] CairoMatrix)
Return the pattern's current matrix.
pattern-set-matrix
(Fn [(Ptr CairoPattern), (Ref CairoMatrix a)] ())
Set the pattern's matrix, which maps user space to pattern space — the inverse of how the pattern moves on the surface.
pdf-surface-create
(Fn [(Ptr CChar), Double, Double] (Ptr CairoSurface))
Create a PDF surface writing to filename with dimensions in points (1 point = 1/72 inch).
pop-group
(Fn [(Ptr CairoContext)] (Ptr CairoPattern))
End the innermost group and return it as a pattern, restoring the state
push-group saved. The pattern must be freed with pattern-destroy.
pop-group-to-source
(Fn [(Ptr CairoContext)] ())
End the innermost group and install it as the source, as pop-group
followed by set-source and pattern-destroy would.
push-group
(Fn [(Ptr CairoContext)] ())
Redirect drawing into an intermediate group surface, so that a whole
sequence of operations can be composited onto the target as a unit. Pairs with
pop-group or pop-group-to-source, and saves the drawing state the way save
does.
push-group-with-content
(Fn [(Ptr CairoContext), CairoContent] ())
Like push-group, but with the group surface's content type chosen
explicitly instead of inferred from the target.
region-contains-point?
(Fn [(Ptr CairoRegion), Int, Int] Bool)
Return true if the device-space point (x y) lies inside reg.
region-contains-rectangle
(Fn [(Ptr CairoRegion), (Ref CairoRectangleInt a)] CairoRegionOverlap)
Report whether rect is entirely inside reg (region-overlap-in),
entirely outside it (region-overlap-out), or straddles its edge
(region-overlap-part).
region-copy
(Fn [(Ptr CairoRegion)] (Ptr CairoRegion))
Create an independent copy of reg, which must be destroyed separately.
region-create
(Fn [] (Ptr CairoRegion))
Create an empty region. Must be paired with region-destroy.
region-create-rectangle
(Fn [(Ref CairoRectangleInt a)] (Ptr CairoRegion))
Create a region containing rect.
region-create-rectangles
(Fn [(Ref (Array CairoRectangleInt) a)] (Ptr CairoRegion))
Create a region covering the union of rects.
region-equal?
(Fn [(Ptr CairoRegion), (Ptr CairoRegion)] Bool)
Return true if a and b cover the same area.
region-get-extents
(Fn [(Ptr CairoRegion)] CairoRectangleInt)
Return the bounding box of reg, or a zero-sized rectangle if it is empty.
region-get-rectangle
(Fn [(Ptr CairoRegion), Int] (Maybe CairoRectangleInt))
(region-get-rectangle reg nth)
Return the nth rectangle of reg, or Nothing if nth is out of range.
region-intersect
(Fn [(Ptr CairoRegion), (Ptr CairoRegion)] CairoStatus)
Reduce reg to its intersection with other, returning a CairoStatus.
region-intersect-rectangle
(Fn [(Ptr CairoRegion), (Ref CairoRectangleInt a)] CairoStatus)
Reduce reg to its intersection with rect, returning a CairoStatus.
region-num-rectangles
(Fn [(Ptr CairoRegion)] Int)
Return how many non-overlapping rectangles reg decomposes into.
region-subtract
(Fn [(Ptr CairoRegion), (Ptr CairoRegion)] CairoStatus)
Subtract other from reg, returning a CairoStatus.
region-subtract-rectangle
(Fn [(Ptr CairoRegion), (Ref CairoRectangleInt a)] CairoStatus)
Subtract rect from reg, returning a CairoStatus.
region-translate
(Fn [(Ptr CairoRegion), Int, Int] ())
Move reg by (dx dy) in device space.
region-union
(Fn [(Ptr CairoRegion), (Ptr CairoRegion)] CairoStatus)
Grow reg to also cover other, returning a CairoStatus.
region-union-rectangle
(Fn [(Ptr CairoRegion), (Ref CairoRectangleInt a)] CairoStatus)
Grow reg to also cover rect, returning a CairoStatus.
region-xor
(Fn [(Ptr CairoRegion), (Ptr CairoRegion)] CairoStatus)
Reduce reg to the area covered by exactly one of reg and other,
returning a CairoStatus.
region-xor-rectangle
(Fn [(Ptr CairoRegion), (Ref CairoRectangleInt a)] CairoStatus)
Reduce reg to the area covered by exactly one of reg and rect,
returning a CairoStatus.
restore
(Fn [(Ptr CairoContext)] ())
Pop the top drawing state off the stack and make it current.
rotate
(Fn [(Ptr CairoContext), Double] ())
Rotate the current transformation by angle radians.
save
(Fn [(Ptr CairoContext)] ())
Push a copy of the current drawing state onto an internal stack.
scaled-font-create
(Fn [(Ptr CairoFontFace), (Ref CairoMatrix a), (Ref CairoMatrix b)] (Ptr CairoScaledFont))
Scale face by the font matrix fm — usually (matrix-init-scale size size) — under the transformation ctm, using Cairo's default font options. Must be paired with scaled-font-destroy.
scaled-font-destroy
(Fn [(Ptr CairoScaledFont)] ())
Drop a reference to a scaled font. Pairs with scaled-font-create.
scaled-font-extents
(Fn [(Ptr CairoScaledFont)] CairoFontExtents)
Return the metrics of a scaled font, the way font-extents does for a context's current font.
scaled-font-glyph-extents
(Fn [(Ptr CairoScaledFont), (Ref (Array CairoGlyph) a)] CairoTextExtents)
Measure glyphs in a scaled font, without needing a context.
scaled-font-text-extents
(Fn [(Ptr CairoScaledFont), (Ptr CChar)] CairoTextExtents)
Measure text in a scaled font, the way text-extents does for a context's current font.
scaled-font-text-to-glyphs
(Fn [(Ptr CairoScaledFont), Double, Double, (Ptr CChar)] (Result (Array CairoGlyph) CairoStatus))
(scaled-font-text-to-glyphs sf x y text)
Lay text out in a scaled font starting at (x y), returning the glyphs it maps to, or the failing status in the error branch.
The glyphs are copied out of Cairo's allocation, so the result is an ordinary Carp array and can be handed straight to show-glyphs.
select-font-face
(Fn [(Ptr CairoContext), (Ptr CChar), CairoFontSlant, CairoFontWeight] ())
Select a font face by family name, slant, and weight. Uses Cairo's built-in toy text API.
set-dash
(Fn [(Ptr CairoContext), (Ref (Array Double) a), Double] ())
Set the dash pattern. dashes is an array of on/off lengths; offset shifts the pattern start.
set-font-face
(Fn [(Ptr CairoContext), (Ptr CairoFontFace)] ())
Use face for subsequent text operations on the context.
set-matrix
(Fn [(Ptr CairoContext), (Ref CairoMatrix a)] ())
Replace the context's transformation matrix with m.
set-scaled-font
(Fn [(Ptr CairoContext), (Ptr CairoScaledFont)] ())
Use sf for subsequent text operations, replacing the context's font face, size, and font matrix.
set-source
(Fn [(Ptr CairoContext), (Ptr CairoPattern)] ())
Set the pattern as the source for subsequent drawing operations.
set-source-surface
(Fn [(Ptr CairoContext), (Ptr CairoSurface), Double, Double] ())
Set a surface as the source pattern, translated to (x y).
show-glyphs
(Fn [(Ptr CairoContext), (Ref (Array CairoGlyph) a)] ())
Draw glyphs with the current source, leaving the current point after the last one.
status-invalid-matrix
CairoStatus
Status returned by matrix-invert when the matrix is not invertible.
status-to-string
(Fn [CairoStatus] (Ptr CChar))
Returns a human-readable description of the status code s.
stroke-extents
(Fn [(Ptr CairoContext)] CairoExtents)
Compute the bounding box of the area that would be affected by stroke.
success?
(Fn [CairoStatus] Bool)
(success? s)
Returns true if the given status equals status-success.
surface-flush
(Fn [(Ptr CairoSurface)] ())
Ensure pending drawing for the surface has been applied. Required before reading pixels directly.
surface-write-to-png
(Fn [(Ptr CairoSurface), (Ptr CChar)] CairoStatus)
Write the contents of an image surface to a PNG file. Returns a CairoStatus.
svg-surface-create
(Fn [(Ptr CChar), Double, Double] (Ptr CairoSurface))
Create an SVG surface writing to filename with dimensions in points.
text-extents
(Fn [(Ptr CairoContext), (Ptr CChar)] CairoTextExtents)
Measure text and return a CairoTextExtents with bearing, size, and advance fields.
toy-font-face-create
(Fn [(Ptr CChar), CairoFontSlant, CairoFontWeight] (Ptr CairoFontFace))
Create a font face from the family / slant / weight triple select-font-face takes, without installing it on a context. Must be paired with font-face-destroy.
toy-font-face-get-family
(Fn [(Ptr CairoFontFace)] (Ptr CChar))
Return the family name of a toy font face. The string belongs to the face and dies with it.
transform
(Fn [(Ptr CairoContext), (Ref CairoMatrix a)] ())
Compose m onto the context's transformation, the way translate and rotate do.
user-to-device
(Fn [(Ptr CairoContext), Double, Double] CairoPoint)
Convert the point (x y) from user space to device space.
user-to-device-distance
(Fn [(Ptr CairoContext), Double, Double] CairoPoint)
Convert the vector (dx dy) from user space to device space, ignoring translation.