cairo-mojo provides Mojo bindings and high-level wrappers for the Cairo graphics library (libcairo). The goal is to expose a safe, typed Mojo API for 2D rendering while still allowing low-level access when needed. The API design is inspired by Pycairo with the goal of reaching parity, so existing Pycairo code translates naturally.
- low-level FFI bindings in
cairo_mojo/_binding.mojo - High-level API → ergonomic wrappers (Context, ImageSurface, etc.)
- Runtime loader (dynamic libcairo resolution) →
cairo_mojo/cairo_runtime.mojo
- Pixi
- a system installed
libcairo(should be available on most Linux and OSX systems)
Use Pixi's git flag (-g / --git) to install directly from this repository:
pixi add -g https://github.com/MoSafi2/cairo-mojo cairo-mojoRun an example:
pixi run mojo run examples/red_rectangle_png.mojofrom cairo_mojo import Context, ImageSurface
def main() raises:
var surface = ImageSurface(width=256, height=256)
var ctx = Context(surface)
ctx.set_source_rgb(1.0, 1.0, 1.0)
ctx.paint()
ctx.set_source_rgb(0.92, 0.22, 0.22)
ctx.rectangle(48.0, 48.0, 160.0, 160.0)
ctx.fill()
surface.write_to_png("simple_example.png")pixi run mojo run simple_example.mojoRun any example with:
pixi run mojo run examples/<example_file>.mojoexamples/libcairo_arc_and_arc_negative_png.mojo->libcairo_arc_and_arc_negative.pngexamples/libcairo_curve_to_png.mojo->libcairo_curve_to.pngexamples/libcairo_clip_png.mojo->libcairo_clip.pngexamples/libcairo_text_extents_png.mojo->libcairo_text_extents.pngexamples/libcairo_fill_and_stroke_png.mojo->libcairo_fill_and_stroke.png
examples/pycairo_gradient_png.mojo->pycairo_gradient.pngexamples/pycairo_set_line_cap_png.mojo->pycairo_set_line_cap.pngexamples/pycairo_set_line_join_png.mojo->pycairo_set_line_join.pngexamples/pycairo_text_align_center_png.mojo->pycairo_text_align_center.pngexamples/pycairo_spiral_png.mojo->pycairo_spiral.png
pixi install -e devThis adds:
- mojo-bindgen
- libclang Use this only if you:
- regenerate bindings
- work on FFI/codegen
- debug ABI issues
pixi run testpixi run verifypixi run verify runs only install-time checks (test_install_unit and package smoke).
pixi run buildTo write artifacts to dist/conda:
pixi run package-artifactsCurrent toolchain target:
- Mojo: currently nightly and then starting with
mojo 26.3stable - Cairo >= 1.18