cairo

Cairo 2D vector graphics library bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_cairo.cC FFI wrapper
c/kit_cairo.hC header for FFI wrapper
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata and dependencies
src/cairo.kitKit Cairo - 2D vector graphics library bindings
tests/cairo.test.kitTests for cairo

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-cairo.git

System Requirements

PlatformCommand
macOSbrew install cairo
Ubuntusudo apt install libcairo2-dev
Fedorasudo dnf install cairo-devel

Usage

import Kit.Cairo as Cairo

main = fn =>
  # Create a PNG image surface
  match Cairo.image-surface-create 400 300
    | Ok surface ->
      match Cairo.create surface
        | Ok cr ->
          # White background
          Cairo.set-source-rgb cr 1.0 1.0 1.0
          Cairo.paint cr

          # Draw a blue rectangle
          Cairo.set-source-rgb cr 0.0 0.0 0.8
          Cairo.rectangle cr 50.0 50.0 200.0 150.0
          Cairo.stroke cr

          # Draw a red filled circle
          Cairo.set-source-rgb cr 0.8 0.0 0.0
          Cairo.arc cr 300.0 150.0 60.0 0.0 (2.0 * Cairo.pi)
          Cairo.fill cr

          # Add text
          Cairo.select-font-face cr "Sans" (Cairo.font-slant-normal) (Cairo.font-weight-bold)
          Cairo.set-font-size cr 24.0
          Cairo.set-source-rgb cr 0.0 0.0 0.0
          Cairo.move-to cr 100.0 40.0
          Cairo.show-text cr "Hello, Cairo!"

          # Write to file
          match Cairo.surface-write-to-png surface "/tmp/output.png"
            | Ok _ -> println "PNG written"
            | Err e -> println ("Error: " ++ e)

          Cairo.destroy cr
        | Err e -> println ("Context error: " ++ e)
      Cairo.surface-destroy surface
    | Err e -> println ("Surface error: " ++ e)

main

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/cairo/, making it available for import as Kit.Cairo in other projects.

License

This package is released under the MIT License - see LICENSE for details.

Cairo is released under the LGPL 2.1 / MPL 1.1.

Exported Functions & Types

font-slant-normal

Normal (upright) font slant.

Int

font-slant-italic

Italic font slant.

Int

font-weight-normal

Normal font weight.

Int

font-weight-bold

Bold font weight.

Int

pi

Pi constant for arc calculations.

Float

image-surface-create

Create an ARGB32 image surface with the given dimensions.

Parameters:

Returns:

PositiveInt -> PositiveInt -> Result Ptr String

svg-surface-create

Create an SVG surface that writes to the given filename.

Parameters:

Returns:

NonEmptyString -> PositiveFloat -> PositiveFloat -> Result Ptr String

pdf-surface-create

Create a PDF surface that writes to the given filename.

Parameters:

Returns:

NonEmptyString -> PositiveFloat -> PositiveFloat -> Result Ptr String

surface-destroy

Destroy a surface and free its resources.

Parameters:

    Ptr -> Unit

surface-write-to-png

Write an image surface to a PNG file.

Parameters:

Returns:

Ptr -> NonEmptyString -> Result Unit String

create

Create a drawing context for the given surface.

Parameters:

Returns:

Ptr -> Result Ptr String

destroy

Destroy a drawing context and free its resources.

Parameters:

    Ptr -> Unit

set-source-rgb

Set the source color to an opaque RGB value.

Parameters:

    Ptr -> UnitFloat -> UnitFloat -> UnitFloat -> Unit

set-source-rgba

Set the source color to an RGBA value with transparency.

Parameters:

    Ptr -> UnitFloat -> UnitFloat -> UnitFloat -> UnitFloat -> Unit

set-line-width

Set the line width for stroke operations.

Parameters:

    Ptr -> Float -> Unit

move-to

Move the current point to the given position without drawing.

Parameters:

    Ptr -> Float -> Float -> Unit

line-to

Draw a line from the current point to the given position.

Parameters:

    Ptr -> Float -> Float -> Unit

rectangle

Add a rectangle to the current path.

Parameters:

    Ptr -> Float -> Float -> Float -> Float -> Unit

arc

Add a circular arc to the current path.

Parameters:

    Ptr -> Float -> Float -> PositiveFloat -> Float -> Float -> Unit

close-path

Close the current path by drawing a line back to the start.

Parameters:

    Ptr -> Unit

new-path

Clear the current path.

Parameters:

    Ptr -> Unit

stroke

Stroke the current path (draw its outline) and clear the path.

Parameters:

    Ptr -> Unit

fill

Fill the current path and clear the path.

Parameters:

    Ptr -> Unit

stroke-preserve

Stroke the current path but keep it for further operations.

Parameters:

    Ptr -> Unit

fill-preserve

Fill the current path but keep it for further operations.

Parameters:

    Ptr -> Unit

paint

Paint the entire surface with the current source color.

Parameters:

    Ptr -> Unit

select-font-face

Select a font face by family name, slant, and weight.

Parameters:

    Ptr -> NonEmptyString -> Int -> Int -> Unit

set-font-size

Set the font size.

Parameters:

    Ptr -> PositiveFloat -> Unit

show-text

Display text at the current point.

Parameters:

    Ptr -> String -> Unit

translate

Translate the coordinate system.

Parameters:

    Ptr -> Float -> Float -> Unit

scale

Scale the coordinate system.

Parameters:

    Ptr -> Float -> Float -> Unit

rotate

Rotate the coordinate system.

Parameters:

    Ptr -> Float -> Unit

save

Save the current drawing state (color, line width, transformations).

Parameters:

    Ptr -> Unit

restore

Restore a previously saved drawing state.

Parameters:

    Ptr -> Unit

show-page

Emit the current page and start a new one (for PDF/SVG surfaces).

Parameters:

    Ptr -> Unit

version

Get the Cairo library version string.

Returns:

String