|
1 | 1 | use crate::core::alignment; |
2 | 2 | use crate::core::text::{LineHeight, Shaping}; |
3 | | -use crate::core::{Color, Font, Pixels, Point}; |
| 3 | +use crate::core::{Color, Font, Pixels, Point, Size, Vector}; |
| 4 | +use crate::geometry::Path; |
| 5 | +use crate::text; |
4 | 6 |
|
5 | 7 | /// A bunch of text that can be drawn to a canvas |
6 | 8 | #[derive(Debug, Clone)] |
@@ -32,6 +34,137 @@ pub struct Text { |
32 | 34 | pub shaping: Shaping, |
33 | 35 | } |
34 | 36 |
|
| 37 | +impl Text { |
| 38 | + /// Computes the [`Path`]s of the [`Text`] and draws them using |
| 39 | + /// the given closure. |
| 40 | + pub fn draw_with(&self, mut f: impl FnMut(Path, Color)) { |
| 41 | + let mut font_system = |
| 42 | + text::font_system().write().expect("Write font system"); |
| 43 | + |
| 44 | + let mut buffer = cosmic_text::BufferLine::new( |
| 45 | + &self.content, |
| 46 | + cosmic_text::AttrsList::new(text::to_attributes(self.font)), |
| 47 | + text::to_shaping(self.shaping), |
| 48 | + ); |
| 49 | + |
| 50 | + let layout = buffer.layout( |
| 51 | + font_system.raw(), |
| 52 | + self.size.0, |
| 53 | + f32::MAX, |
| 54 | + cosmic_text::Wrap::None, |
| 55 | + ); |
| 56 | + |
| 57 | + let translation_x = match self.horizontal_alignment { |
| 58 | + alignment::Horizontal::Left => self.position.x, |
| 59 | + alignment::Horizontal::Center | alignment::Horizontal::Right => { |
| 60 | + let mut line_width = 0.0f32; |
| 61 | + |
| 62 | + for line in layout.iter() { |
| 63 | + line_width = line_width.max(line.w); |
| 64 | + } |
| 65 | + |
| 66 | + if self.horizontal_alignment == alignment::Horizontal::Center { |
| 67 | + self.position.x - line_width / 2.0 |
| 68 | + } else { |
| 69 | + self.position.x - line_width |
| 70 | + } |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + let translation_y = { |
| 75 | + let line_height = self.line_height.to_absolute(self.size); |
| 76 | + |
| 77 | + match self.vertical_alignment { |
| 78 | + alignment::Vertical::Top => self.position.y, |
| 79 | + alignment::Vertical::Center => { |
| 80 | + self.position.y - line_height.0 / 2.0 |
| 81 | + } |
| 82 | + alignment::Vertical::Bottom => self.position.y - line_height.0, |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + let mut swash_cache = cosmic_text::SwashCache::new(); |
| 87 | + |
| 88 | + for run in layout.iter() { |
| 89 | + for glyph in run.glyphs.iter() { |
| 90 | + let physical_glyph = glyph.physical((0.0, 0.0), 1.0); |
| 91 | + |
| 92 | + let start_x = translation_x + glyph.x + glyph.x_offset; |
| 93 | + let start_y = translation_y + glyph.y_offset + self.size.0; |
| 94 | + let offset = Vector::new(start_x, start_y); |
| 95 | + |
| 96 | + if let Some(commands) = swash_cache.get_outline_commands( |
| 97 | + font_system.raw(), |
| 98 | + physical_glyph.cache_key, |
| 99 | + ) { |
| 100 | + let glyph = Path::new(|path| { |
| 101 | + use cosmic_text::Command; |
| 102 | + |
| 103 | + for command in commands { |
| 104 | + match command { |
| 105 | + Command::MoveTo(p) => { |
| 106 | + path.move_to( |
| 107 | + Point::new(p.x, -p.y) + offset, |
| 108 | + ); |
| 109 | + } |
| 110 | + Command::LineTo(p) => { |
| 111 | + path.line_to( |
| 112 | + Point::new(p.x, -p.y) + offset, |
| 113 | + ); |
| 114 | + } |
| 115 | + Command::CurveTo(control_a, control_b, to) => { |
| 116 | + path.bezier_curve_to( |
| 117 | + Point::new(control_a.x, -control_a.y) |
| 118 | + + offset, |
| 119 | + Point::new(control_b.x, -control_b.y) |
| 120 | + + offset, |
| 121 | + Point::new(to.x, -to.y) + offset, |
| 122 | + ); |
| 123 | + } |
| 124 | + Command::QuadTo(control, to) => { |
| 125 | + path.quadratic_curve_to( |
| 126 | + Point::new(control.x, -control.y) |
| 127 | + + offset, |
| 128 | + Point::new(to.x, -to.y) + offset, |
| 129 | + ); |
| 130 | + } |
| 131 | + Command::Close => { |
| 132 | + path.close(); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + f(glyph, self.color); |
| 139 | + } else { |
| 140 | + // TODO: Raster image support for `Canvas` |
| 141 | + let [r, g, b, a] = self.color.into_rgba8(); |
| 142 | + |
| 143 | + swash_cache.with_pixels( |
| 144 | + font_system.raw(), |
| 145 | + physical_glyph.cache_key, |
| 146 | + cosmic_text::Color::rgba(r, g, b, a), |
| 147 | + |x, y, color| { |
| 148 | + f( |
| 149 | + Path::rectangle( |
| 150 | + Point::new(x as f32, y as f32) + offset, |
| 151 | + Size::new(1.0, 1.0), |
| 152 | + ), |
| 153 | + Color::from_rgba8( |
| 154 | + color.r(), |
| 155 | + color.g(), |
| 156 | + color.b(), |
| 157 | + color.a() as f32 / 255.0, |
| 158 | + ), |
| 159 | + ); |
| 160 | + }, |
| 161 | + ); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
35 | 168 | impl Default for Text { |
36 | 169 | fn default() -> Text { |
37 | 170 | Text { |
|
0 commit comments