Debian Codegraph @

pango-examples/src/example_1_trivial.rs

raw
1 use cairo;
2 use pango;
3 use pango::prelude::*;
4 use pangocairo;
5
6 use std::fs::File;
7
8 fn main() {
9 // Create a font map, in this case the default one which uses the system's fonts.
10 let font_map = pangocairo::FontMap::default();
11
12 // Create a pango context. We set the resolution to 72 dots-per-inch so that the sizes we specify
13 // later will be pixel sizes, instead of point sizes (Pango's API takes points).
14 let pango_context = font_map.create_context();
15 pangocairo::functions::context_set_resolution(&pango_context, 72.0);
16
17 // Create a Cairo image surface for our output
18 let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 400, 400).unwrap();
19 let cr = cairo::Context::new(&surface).unwrap();
20 cr.set_source_rgba(1.0, 1.0, 1.0, 1.0);
21 cr.paint().unwrap();
22
23 // Note how this uses all the defaults in Pango. The text will probably
24 // be set in a sans-serif font, 10 pixels tall or so.
25 let layout = pango::Layout::new(&pango_context);
26 layout.set_text("Hello world!");
27
28 // Set the paint color to black; that's what pangocairo will use.
29 cr.set_source_rgba(0.0, 0.0, 0.0, 1.0);
30 cr.move_to(100.0, 100.0);
31 pangocairo::functions::show_layout(&cr, &layout);
32
33 // Write a PNG file from the surface
34 let mut output = File::create("example_1_trivial.png").unwrap();
35 surface.write_to_png(&mut output).unwrap();
36 }