All formats

cairo-rs

LanguageRust
LicenseMIT (cairo itself is LGPL-2.1 or MPL-1.1)
Repositorygithub.com/gtk-rs/gtk-rs-core (folder cairo)
DocumentationPsSurface
Depends onthe cairo C library (system package)
Read / writeno / yes (EPS Level 2 or 3)

cairo::PsSurface writes PostScript; set_eps(true) before drawing makes it write a single-page EPS whose %%BoundingBox is computed from the drawn marks. Fonts are embedded as subsets.

The crate links to the system cairo library, and PostScript output needs the ps feature. cairo can’t read EPS.

Installation#

TOML
[dependencies]
cairo-rs = { version = "0.22", features = ["ps"] }
Shell
sudo apt install libcairo2-dev   # Debian / Ubuntu
brew install cairo               # macOS

Basic usage: write an EPS file#

Rust
use cairo::{Context, FontSlant, FontWeight, PsSurface};

fn main() -> Result<(), cairo::Error> {
    let surface = PsSurface::new(200.0, 100.0, "logo.eps")?;
    surface.set_eps(true); // before drawing
    surface.dsc_comment("%%Title: Logo");

    let cr = Context::new(&surface)?;
    cr.set_source_rgb(0.2, 0.4, 0.8);
    cr.rectangle(10.0, 10.0, 180.0, 80.0); // origin is top-left
    cr.fill()?;

    cr.set_source_rgb(1.0, 1.0, 1.0);
    cr.select_font_face("Sans", FontSlant::Normal, FontWeight::Bold);
    cr.set_font_size(36.0);
    cr.move_to(40.0, 64.0);
    cr.show_text("LOGO")?;
    cr.show_page()?;

    drop(cr);
    surface.finish();
    surface.status()
}

How to check that a surface writes EPS#

Rust
assert!(surface.is_eps());

How to write EPS for PostScript Level 2#

Rust
surface.restrict(cairo::PsLevel::_2);

How to write EPS to stdout or any writer#

Rust
let surface = PsSurface::for_stream(200.0, 100.0, std::io::stdout())?;
surface.set_eps(true);

Tips#