Example that displays various effects that can be achieved via flo_canvas's text layout functions
()
| 8 | /// flo_canvas's text layout functions |
| 9 | /// |
| 10 | pub fn main() { |
| 11 | with_2d_graphics(|| { |
| 12 | let lato = CanvasFontFace::from_slice(include_bytes!("Lato-Regular.ttf")); |
| 13 | let lato_bold = CanvasFontFace::from_slice(include_bytes!("Lato-Bold.ttf")); |
| 14 | |
| 15 | // Create a window |
| 16 | let canvas = create_drawing_window("Text layout example"); |
| 17 | |
| 18 | // Various text layout demonstrations |
| 19 | canvas.draw(|gc| { |
| 20 | // Set up the canvas |
| 21 | gc.canvas_height(1000.0); |
| 22 | gc.center_region(0.0, 0.0, 1000.0, 1000.0); |
| 23 | |
| 24 | // Load the fonts |
| 25 | gc.define_font_data(FontId(1), Arc::clone(&lato)); |
| 26 | gc.define_font_data(FontId(2), Arc::clone(&lato_bold)); |
| 27 | gc.set_font_size(FontId(1), 18.0); |
| 28 | gc.set_font_size(FontId(2), 18.0); |
| 29 | }); |
| 30 | |
| 31 | canvas.draw(|gc| { |
| 32 | // Draw some text with layout in these fonts |
| 33 | gc.fill_color(Color::Rgba(0.0, 0.0, 0.6, 1.0)); |
| 34 | |
| 35 | // Start with a simple text layout |
| 36 | gc.begin_line_layout(18.0, 900.0, TextAlignment::Left); |
| 37 | gc.layout_text(FontId(1), "Simple text layout".to_string()); |
| 38 | gc.draw_text_layout(); |
| 39 | }); |
| 40 | |
| 41 | canvas.draw(|gc| { |
| 42 | // We can change fonts during a layout |
| 43 | gc.begin_line_layout(18.0, 900.0 - 30.0, TextAlignment::Left); |
| 44 | gc.layout_text(FontId(1), "We can change ".to_string()); |
| 45 | gc.layout_text(FontId(2), "fonts".to_string()); |
| 46 | gc.layout_text(FontId(1), " during layout ".to_string()); |
| 47 | gc.draw_text_layout(); |
| 48 | }); |
| 49 | |
| 50 | canvas.draw(|gc| { |
| 51 | // We can change colours during a layout |
| 52 | gc.begin_line_layout(18.0, 900.0 - 60.0, TextAlignment::Left); |
| 53 | gc.layout_text(FontId(1), "Or we could change ".to_string()); |
| 54 | gc.fill_color(Color::Rgba(0.8, 0.6, 0.0, 1.0)); |
| 55 | gc.layout_text(FontId(1), "colours".to_string()); |
| 56 | gc.fill_color(Color::Rgba(0.0, 0.0, 0.6, 1.0)); |
| 57 | gc.layout_text(FontId(1), " during layout ".to_string()); |
| 58 | gc.draw_text_layout(); |
| 59 | }); |
| 60 | |
| 61 | canvas.draw(|gc| { |
| 62 | // We can change font sizes during a layout |
| 63 | gc.begin_line_layout(18.0, 900.0 - 100.0, TextAlignment::Left); |
| 64 | gc.layout_text(FontId(1), "It's also possible to alter ".to_string()); |
| 65 | gc.set_font_size(FontId(1), 36.0); |
| 66 | gc.layout_text(FontId(1), "sizes".to_string()); |
| 67 | gc.set_font_size(FontId(1), 18.0); |
nothing calls this directly
no test coverage detected