Debian Codegraph @

pango-examples/src/example_3_glyphs.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 let layout = pango::Layout::new(&pango_context);
24 fill_layout(&layout);
25
26 // We will iterate the layout by hand
27 // line
28 // runs
29 // clusters
30
31 let mut iter = layout.iter();
32 while let Some(_line) = iter.line_readonly() {
33 while let Some(run) = iter.run_readonly() {
34 let item = run.item();
35 let glyph_string = run.glyph_string();
36
37 println!(
38 "item.offfset: {}, item.char_offset: {}",
39 item.offset(),
40 item.char_offset(),
41 );
42
43 println!("glyph_string num_glyphs: {}", glyph_string.num_glyphs(),);
44
45 for glyph_info in glyph_string.glyph_info() {
46 let geometry = glyph_info.geometry();
47
48 println!(
49 " glyph {} width={} x_offset={} y_offset={}",
50 glyph_info.glyph(),
51 geometry.width(),
52 geometry.x_offset(),
53 geometry.y_offset(),
54 );
55 }
56
57 for glyph_info in glyph_string.glyph_info() {
58 let geometry = glyph_info.geometry();
59
60 println!(
61 " glyph {} width={} x_offset={} y_offset={}",
62 glyph_info.glyph(),
63 geometry.width(),
64 geometry.x_offset(),
65 geometry.y_offset(),
66 );
67 }
68
69 if !iter.next_run() {
70 break;
71 }
72 }
73
74 if !iter.next_line() {
75 break;
76 }
77 }
78
79 // Set the paint color to black; that's what pangocairo will use.
80 cr.set_source_rgba(0.0, 0.0, 0.0, 1.0);
81 cr.move_to(100.0, 100.0);
82 pangocairo::functions::show_layout(&cr, &layout);
83
84 // Write a PNG file from the surface
85 let mut output = File::create("example_3_glyphs.png").unwrap();
86 surface.write_to_png(&mut output).unwrap();
87 }
88
89 fn fill_layout(layout: &pango::Layout) {
90 let text = "Hello böld world in itálics!";
91 layout.set_text(&text);
92
93 // Create an attribute list and an array of attributes to put in it
94 let attr_list = pango::AttrList::new();
95
96 // Font description and style; this spans the whole string
97 let mut font_desc = pango::FontDescription::new();
98 font_desc.set_family("Sans");
99 font_desc.set_size(40 * pango::SCALE); // sizes in Pango are scaled by this factor
100 let mut attr = pango::AttrFontDesc::new(&font_desc).upcast();
101 attr.set_start_index(0);
102 attr.set_end_index(text.len() as u32); // in bytes
103 attr_list.insert(attr);
104
105 // Make the word bold; note how we use byte offsets
106 let mut font_desc = pango::FontDescription::new();
107 font_desc.set_weight(pango::Weight::Bold);
108 let mut attr = pango::AttrFontDesc::new(&font_desc).upcast();
109 attr.set_start_index(text.find("böld").unwrap() as u32);
110 attr.set_end_index((text.find("böld").unwrap() + "böld".len()) as u32);
111 attr_list.insert(attr);
112
113 // Make the two words italics; same as the previous one
114 let mut font_desc = pango::FontDescription::new();
115 font_desc.set_style(pango::Style::Italic);
116 let mut attr = pango::AttrFontDesc::new(&font_desc).upcast();
117 attr.set_start_index(text.find("in itálics").unwrap() as u32);
118 attr.set_end_index((text.find("in itálics").unwrap() + "in itálics".len()) as u32);
119 attr_list.insert(attr);
120
121 // Also make "in italics" red
122 let mut attr = pango::AttrColor::new_foreground(0xffff, 0x0000, 0x0000);
123 attr.set_start_index(text.find("in itálics").unwrap() as u32);
124 attr.set_end_index((text.find("in itálics").unwrap() + "in itálics".len()) as u32);
125 attr_list.insert(attr);
126
127 // Finally, set the attribute list on the layout
128 layout.set_attributes(Some(&attr_list));
129 }