I’ve been working on cairo-mojo, a library that wraps Cairo graphics library into Mojo with both low-level bindings and a more ergonomic, safe API.
It can be a building block for plotting, visualization stack as a rendering backend for vector graphics in svg, png, pdf.
Repo: https://github.com/MoSafi2/cairo-mojo
What it provides
- FFI bindings to
libcairo(auto-generated) - High-level wrappers (
Context,ImageSurfacePatternPaths, etc.) inspired byPycairo. Almost allCairofeatures are safely wrapped - Runtime loader for dynamic
libcairodiscovery on Linux and OSX
Quick example
def main() raises:
var surface = ImageSurface(width=256, height=256)
var ctx = Context(surface)
ctx.set_source_rgb(0.96, 0.97, 0.99); ctx.paint() # background
ctx.set_source_rgb(0.0, 0.0, 0.0) # card
ctx.rectangle(40.0, 40.0, 176.0, 176.0)
ctx.fill()
ctx.set_source_rgb(0.25, 0.55, 0.95) # circle
ctx.arc(128.0, 110.0, 40.0, 0.0, 6.28318)
ctx.fill()
ctx.set_source_rgb(0.92, 0.22, 0.22) # accent bar
ctx.rectangle(72.0, 160.0, 112.0, 14.0)
ctx.fill()
surface.write_to_png("simple_example.png")
Check out 11 additional examples in the examples directory ranging from simple to advanced use cases.
Use it in your project
pixi add -g https://github.com/MoSafi2/cairo-mojo cairo-mojo
Modular community channel addition is coming soon.
Feedback on API design, ergonomics, is very welcome.
