Debian Codegraph @

pango-examples/src/example_2_attributes.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, 800, 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
27 let text = "Hello böld world in itálics!";
28 layout.set_text(&text);
29
30 // Create an attribute list and an array of attributes to put in it
31 let attr_list = pango::AttrList::new();
32
33 // Font description and style; this spans the whole string
34 let mut font_desc = pango::FontDescription::new();
35 font_desc.set_family("Sans");
36 font_desc.set_size(40 * pango::SCALE); // sizes in Pango are scaled by this factor
37 let mut attr = pango::AttrFontDesc::new(&font_desc).upcast();
38 attr.set_start_index(0);
39 attr.set_end_index(text.len() as u32); // in bytes
40 attr_list.insert(attr);
41
42 // Make the word bold; note how we use byte offsets
43 let mut font_desc = pango::FontDescription::new();
44 font_desc.set_weight(pango::Weight::Bold);
45 let mut attr = pango::AttrFontDesc::new(&font_desc).upcast();
46 attr.set_start_index(text.find("böld").unwrap() as u32);
47 attr.set_end_index((text.find("böld").unwrap() + "böld".len()) as u32);
48 attr_list.insert(attr);
49
50 // Make the two words italics; same as the previous one
51 let mut font_desc = pango::FontDescription::new();
52 font_desc.set_style(pango::Style::Italic);
53 let mut attr = pango::AttrFontDesc::new(&font_desc).upcast();
54 attr.set_start_index(text.find("in itálics").unwrap() as u32);
55 attr.set_end_index((text.find("in itálics").unwrap() + "in itálics".len()) as u32);
56 attr_list.insert(attr);
57
58 // Also make "in italics" red
59 let mut attr = pango::AttrColor::new_foreground(0xffff, 0x0000, 0x0000);
60 attr.set_start_index(text.find("in itálics").unwrap() as u32);
61 attr.set_end_index((text.find("in itálics").unwrap() + "in itálics".len()) as u32);
62 attr_list.insert(attr);
63
64 // Finally, set the attribute list on the layout
65 layout.set_attributes(Some(&attr_list));
66
67 // Set the paint color to black; that's what pangocairo will use.
68 cr.set_source_rgba(0.0, 0.0, 0.0, 1.0);
69 cr.move_to(100.0, 100.0);
70 pangocairo::functions::show_layout(&cr, &layout);
71
72 // Write a PNG file from the surface
73 let mut output = File::create("example_2_attributes.png").unwrap();
74 surface.write_to_png(&mut output).unwrap();
75 }