meo-canvas 0.2.1

Describe a picture, get image bytes back. CSS flexbox, grid and block layout on a Skia backend.
Documentation

meo-canvas

Describe a picture; get image bytes back. CSS flexbox, grid and block layout on a Skia backend.

The public surface: struct-literal options closed with ..Default::default(), no Skia or taffy types anywhere, and no async runtime imposed on the caller.

cargo add meo-canvas

And one line in .cargo/config.toml, beside your Cargo.toml:

[env]
SKIA_BINARIES_URL = "https://github.com/l7aromeo/meo-skia-canvas/releases/download/skia-binaries-0.156.0-3f566dd7198c/skia-binaries-{key}.tar.gz"

This crate links meo-skia-canvas's patched Skia, the one the npm package draws with, so the two surfaces draw the same pixels. That Skia is downloaded from the archives the line names, and a build without it stops and prints the line. The line names meo-skia-canvas's patch set, and a release that changes it says so under Breaking.

A picture

use meo_canvas::{Renderer, Root, Row, Styled, Text, hex, px};

let renderer = Renderer::new();

let mut canvas = Root::new(520.0)
    .height(180.0)
    .background_color(hex("#101014"))
    .children(
        Row::new()
            .gap(px(20.0))
            .padding(px(24.0))
            .children(Text::new("Ukasyah").font_size(26.0).bold()),
    )
    .render(&renderer)?;

canvas.to_file("out.png")?;

Setters are flat and chained, and they come from [Styled], which every node implements — so importing that trait is what makes background_color and gap available. Root::new takes the width; the height is a setter, because Rust has no optional argument and a page that sizes itself to its content is the more common case.

This example is compiled. It lives at examples/readme.rs and is built by the same cargo clippy --all-targets the rest of the tree is gated with, so a README that stops compiling fails a run rather than a reader.

What it writes

to_file takes the format from the extension; to_file_as takes it as an argument, and to_buffer hands back bytes.

raster      png  jpeg  webp  avif  bmp  ico  tiff
animated    gif  apng
vector      svg  pdf

Features

All three are off by default. A build that names none of them renders on the CPU, reads local paths and inline bytes, and links no HTTP stack.

metal    GPU rasterising on Apple platforms
vulkan   GPU rasterising elsewhere
net      resolve an ImageSource::Url over HTTP

metal and vulkan are alternatives rather than additions: naming both builds, and Metal wins, because the overlap is only reachable on macOS where Vulkan runs through MoltenVK anyway. Canvas::engine reports which one actually drew, since asking for the GPU is not the same as getting it.

net is the same shape as those two and not a smaller surface than the npm package has. There the addon is a prebuilt binary with the HTTP client already inside it, so the capability costs its caller nothing; here you compile it, so it costs dependencies, build time and audit surface. Identical capability, different price, and the flag is what lets the one who pays decide. Without it an ImageSource::Url is an error naming the feature; a caller who would rather fetch for themselves passes ImageSource::Bytes and needs none of this.

System libraries

Unlike the npm package, which ships a binary with fontconfig linked statically, a crate is built on the machine that uses it and links the system copy. FreeType is inside the downloaded Skia. On Linux that means:

Debian/Ubuntu   libfontconfig1   (build: libfontconfig1-dev pkg-config)
RHEL/Alma/Rocky fontconfig       (build: fontconfig-devel pkg-config)

pkg-config is the one to get right. rust-skia probes it for fontconfig and falls back to a bare library name when the probe fails, silently — the error you get is unable to find library -lfontconfig, which points at fontconfig rather than at the missing prober.

cmake is also needed at build time, for libaom, and on x86_64 an assembler: yasm, or nasm older than 3, whose help text libaom 3.11 cannot read.

macOS and Windows need none of this: Skia uses CoreText and DirectWrite there.

Reference

The API documentation is on docs.rs, and the reasoning behind the surface — why the setters are flat, why Root is built rather than constructed, what each divergence from CSS costs — is in docs/design.md in the repository.

The npm package is the same renderer behind a prebuilt binary, for callers who would rather not compile Skia: meo-canvas.

Licence

MIT.