lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::{collections::HashMap, sync::Arc};

use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashContent, Wrap};
use tiny_skia::{Pixmap, PixmapPaint};

#[cfg(feature = "perf_profile")]
use crate::app::profiler::canvas_text::{self as profile, Stage, Timer};

mod glyph_cache;
#[cfg(all(test, feature = "perf_profile"))]
mod profile_tests;
mod shape_cache;
#[cfg(test)]
mod tests;
use glyph_cache::GlyphCache;
use shape_cache::ShapeCache;

use super::{CanvasError, MAX_PIXELS};
use crate::{
  app::glyph_engine::{FaceWeights, with_letter_spacing},
  layout::text_style::{FontFeatures, FontStyle, FontWeight, TextStyle, alias_name, cosmic_family},
  node::color::Color,
};

#[derive(Clone, PartialEq)]
pub struct CanvasFont {
  pub family: Arc<str>,
  pub size: f32,
  pub weight: FontWeight,
  pub style: FontStyle,
  /// Extra space after every glyph in logical pixels, like
  /// [`TextStyle::letter_spacing`]; scales with the canvas transform like `size`.
  pub letter_spacing: f32,
  /// OpenType feature settings, like [`TextStyle::font_features`].
  pub font_features: FontFeatures,
}
impl CanvasFont {
  pub fn new(family: impl Into<Arc<str>>, size: f32) -> Self {
    Self {
      family: family.into(),
      size,
      weight: FontWeight::Normal,
      style: FontStyle::Normal,
      letter_spacing: 0.0,
      font_features: FontFeatures::default(),
    }
  }
  pub(crate) fn from_style(style: &TextStyle) -> Self {
    Self {
      family: style.font_family.clone(),
      size: style.font_size,
      weight: style.weight,
      style: style.style,
      letter_spacing: style.letter_spacing,
      font_features: style.font_features.clone(),
    }
  }
}
impl Default for CanvasFont {
  fn default() -> Self {
    Self::from_style(&TextStyle::default())
  }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextAlign {
  #[default]
  Left,
  Center,
  Right,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextBaseline {
  #[default]
  Alphabetic,
  Top,
  Middle,
  Bottom,
}

/// Logical-pixel metrics relative to the selected alignment and baseline.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TextMetrics {
  pub width: f32,
  pub actual_bounding_box_left: f32,
  pub actual_bounding_box_right: f32,
  pub actual_bounding_box_ascent: f32,
  pub actual_bounding_box_descent: f32,
  pub font_bounding_box_ascent: f32,
  pub font_bounding_box_descent: f32,
}

pub(crate) struct CanvasTextEngine {
  fonts: FontSystem,
  aliases: HashMap<String, String>,
  // The engine owns a snapshot of the font database, so this never needs clearing.
  face_weights: FaceWeights,
  swash: GlyphCache,
  shaped: ShapeCache,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Output {
  Metrics,
  Rendered,
}

pub(super) struct ShapedText {
  pub pixels: Option<Arc<Pixmap>>,
  pub data: Arc<Vec<u8>>,
  pub asset_id: u64,
  width: f32,
  left: f32,
  top: f32,
  right: f32,
  bottom: f32,
  ascent: f32,
  descent: f32,
}

impl ShapedText {
  fn offset(&self, align: TextAlign, baseline: TextBaseline) -> (f32, f32) {
    let x = match align {
      TextAlign::Left => 0.0,
      TextAlign::Center => -self.width * 0.5,
      TextAlign::Right => -self.width,
    };
    let y = match baseline {
      TextBaseline::Alphabetic => 0.0,
      TextBaseline::Top => self.ascent,
      TextBaseline::Middle => (self.ascent - self.descent) * 0.5,
      TextBaseline::Bottom => -self.descent,
    };
    (x, y)
  }
  pub fn origin(&self, align: TextAlign, baseline: TextBaseline) -> (f32, f32) {
    let (x, y) = self.offset(align, baseline);
    (x + self.left, y + self.top)
  }
  pub fn metrics(&self, align: TextAlign, baseline: TextBaseline) -> TextMetrics {
    let (x, y) = self.offset(align, baseline);
    TextMetrics {
      width: self.width,
      actual_bounding_box_left: -(x + self.left),
      actual_bounding_box_right: x + self.right,
      actual_bounding_box_ascent: -(y + self.top),
      actual_bounding_box_descent: y + self.bottom,
      font_bounding_box_ascent: self.ascent - y,
      font_bounding_box_descent: self.descent + y,
    }
  }
}

impl CanvasTextEngine {
  pub(crate) fn new(fonts: FontSystem, aliases: HashMap<String, String>) -> Self {
    Self::with_shapes(fonts, aliases, ShapeCache::new())
  }
  /// For an engine that replaces one whose frames a GPU renderer ended. Its
  /// canvases are encoded by the same renderer, so its first frame may keep a
  /// page over the shape budget whole, like any later frame, instead of
  /// evicting the page's first texts while drawing its last ones.
  pub(crate) fn framed(fonts: FontSystem, aliases: HashMap<String, String>) -> Self {
    Self::with_shapes(fonts, aliases, ShapeCache::framed())
  }
  fn with_shapes(fonts: FontSystem, aliases: HashMap<String, String>, shaped: ShapeCache) -> Self {
    Self {
      fonts,
      aliases,
      face_weights: FaceWeights::default(),
      swash: GlyphCache::new(),
      shaped,
    }
  }
  /// Whether the engine's shaped text knows where frames are.
  pub(crate) fn is_framed(&self) -> bool {
    self.shaped.is_framed()
  }
  /// Charged bytes of the shaped text the engine keeps, for the profiler.
  #[cfg(feature = "perf_profile")]
  pub(crate) fn shape_cache_bytes(&self) -> usize {
    self.shaped.bytes()
  }

  pub(super) fn shape(
    &mut self,
    text: &str,
    font: &CanvasFont,
    scale: f32,
    color: Color,
  ) -> Result<Arc<ShapedText>, CanvasError> {
    self.prepare(text, font, scale, color, Output::Rendered)
  }
  pub(super) fn measure(
    &mut self,
    text: &str,
    font: &CanvasFont,
    color: Color,
  ) -> Result<Arc<ShapedText>, CanvasError> {
    self.prepare(text, font, 1.0, color, Output::Metrics)
  }
  fn prepare(
    &mut self,
    text: &str,
    font: &CanvasFont,
    scale: f32,
    color: Color,
    output: Output,
  ) -> Result<Arc<ShapedText>, CanvasError> {
    #[cfg(feature = "perf_profile")]
    let _total = Timer::new(Stage::Total);
    if let Some(result) = self.shaped.get(text, font, scale, color, output) {
      #[cfg(feature = "perf_profile")]
      profile::shape_hit(true);
      return Ok(result);
    }
    #[cfg(feature = "perf_profile")]
    profile::shape_hit(false);
    let result = self.shape_uncached(text, font, scale, color, output);
    // The engine keeps whole shaped texts (`ShapeCache`); the words
    // cosmic-text keeps beside them would only grow with every new text.
    self.fonts.shape_run_cache.trim(0);
    let result = Arc::new(result?);
    #[cfg(feature = "perf_profile")]
    profile::produced(result.data.len());
    self.shaped.insert(text, font, scale, color, output, result.clone());
    Ok(result)
  }
  fn shape_uncached(
    &mut self,
    text: &str,
    font: &CanvasFont,
    scale: f32,
    color: Color,
    output: Output,
  ) -> Result<ShapedText, CanvasError> {
    if text.len() > 65_536 || font.size * scale > 4096.0 {
      return Err(CanvasError::TextTooLarge);
    }
    #[cfg(feature = "perf_profile")]
    let _buffer = Timer::new(Stage::BufferFontShape);
    let mut buffer = Buffer::new(&mut self.fonts, Metrics::new(font.size, font.size * 1.2));
    buffer.set_size(None, None);
    buffer.set_wrap(Wrap::None);
    let family = self
      .aliases
      .get(alias_name(&font.family))
      .map(String::as_str)
      .unwrap_or(&font.family);
    let weight = self
      .face_weights
      .resolve(self.fonts.db(), family, font.weight, font.style);
    let mut attrs = Attrs::new()
      .family(cosmic_family(family))
      .weight(weight)
      .style(font.style.to_cosmic());
    if !font.font_features.is_empty() {
      attrs = attrs.font_features(font.font_features.to_cosmic());
    }
    let attrs = with_letter_spacing(attrs, font.letter_spacing, font.size);
    let text = text.replace(['\n', '\r', '\t'], " ");
    buffer.set_text(&text, &attrs, Shaping::Advanced, None);
    buffer.shape_until_scroll(&mut self.fonts, false);
    #[cfg(feature = "perf_profile")]
    drop(_buffer);
    #[cfg(feature = "perf_profile")]
    let _glyph = Timer::new(Stage::GlyphPrepare);
    let mut glyphs = Vec::new();
    let mut has_ink = false;
    let mut glyph_bytes = 0usize;
    let (mut width, mut ascent, mut descent) = (0.0f32, 0.0f32, 0.0f32);
    let (mut left, mut top, mut right, mut bottom) = (i32::MAX, i32::MAX, i32::MIN, i32::MIN);
    for run in buffer.layout_runs() {
      width = width.max(run.line_w);
      for glyph in run.glyphs {
        if let Some(face) = self.fonts.get_font(glyph.font_id, glyph.font_weight) {
          let metrics = face.as_swash().metrics(&[]).scale(font.size);
          ascent = ascent.max(metrics.ascent);
          descent = descent.max(metrics.descent.abs());
        }
        let physical = glyph.physical((0.0, 0.0), scale);
        let Some(image) = self.swash.image(&mut self.fonts, physical.cache_key).as_ref() else {
          continue;
        };
        if image.placement.width == 0 || image.placement.height == 0 {
          continue;
        }
        if u64::from(image.placement.width) * u64::from(image.placement.height) > MAX_PIXELS {
          return Err(CanvasError::TextTooLarge);
        }
        glyph_bytes += image.data.len();
        if glyph_bytes > 64 * 1024 * 1024 {
          return Err(CanvasError::TextTooLarge);
        }
        let (Some(x), Some(y)) = (
          physical.x.checked_add(image.placement.left),
          physical.y.checked_sub(image.placement.top),
        ) else {
          return Err(CanvasError::TextTooLarge);
        };
        left = left.min(x);
        top = top.min(y);
        let (Some(r), Some(b)) = (
          x.checked_add(image.placement.width as i32),
          y.checked_add(image.placement.height as i32),
        ) else {
          return Err(CanvasError::TextTooLarge);
        };
        right = right.max(r);
        bottom = bottom.max(b);
        if (i64::from(right) - i64::from(left)).saturating_mul(i64::from(bottom) - i64::from(top)) > MAX_PIXELS as i64 {
          return Err(CanvasError::TextTooLarge);
        }
        has_ink = true;
        if output == Output::Rendered {
          glyphs.push((x, y, image.clone()));
        }
      }
    }
    #[cfg(feature = "perf_profile")]
    drop(_glyph);
    if !has_ink {
      return Ok(ShapedText {
        pixels: None,
        data: Arc::new(Vec::new()),
        asset_id: 0,
        width,
        left: 0.0,
        top: 0.0,
        right: 0.0,
        bottom: 0.0,
        ascent,
        descent,
      });
    }
    let (w, h) = (i64::from(right) - i64::from(left), i64::from(bottom) - i64::from(top));
    if w <= 0 || h <= 0 || w * h > MAX_PIXELS as i64 {
      return Err(CanvasError::TextTooLarge);
    }
    if output == Output::Metrics {
      return Ok(ShapedText {
        pixels: None,
        data: Arc::new(Vec::new()),
        asset_id: 0,
        width,
        left: left as f32 / scale,
        top: top as f32 / scale,
        right: right as f32 / scale,
        bottom: bottom as f32 / scale,
        ascent,
        descent,
      });
    }
    #[cfg(feature = "perf_profile")]
    let _bitmap = Timer::new(Stage::BitmapComposition);
    let mut pixels = Pixmap::new(w as u32, h as u32).ok_or(CanvasError::TextTooLarge)?;
    for (x, y, image) in glyphs {
      let mut glyph = Pixmap::new(image.placement.width, image.placement.height).ok_or(CanvasError::TextTooLarge)?;
      for (index, p) in glyph.data_mut().chunks_exact_mut(4).enumerate() {
        let (r, g, b, a) = match image.content {
          SwashContent::Mask => (color.r(), color.g(), color.b(), multiply(image.data[index], color.a())),
          SwashContent::Color => {
            let v = &image.data[index * 4..index * 4 + 4];
            (v[0], v[1], v[2], multiply(v[3], color.a()))
          }
          SwashContent::SubpixelMask => {
            let v = &image.data[index * 4..index * 4 + 4];
            (
              color.r(),
              color.g(),
              color.b(),
              multiply(v[0].max(v[1]).max(v[2]), color.a()),
            )
          }
        };
        p.copy_from_slice(&[multiply(r, a), multiply(g, a), multiply(b, a), a]);
      }
      pixels.draw_pixmap(
        x - left,
        y - top,
        glyph.as_ref(),
        &PixmapPaint::default(),
        tiny_skia::Transform::identity(),
        None,
      );
    }
    Ok(ShapedText {
      asset_id: super::NEXT_CANVAS_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) | (1u64 << 63),
      data: Arc::new(pixels.data().to_vec()),
      pixels: Some(Arc::new(pixels)),
      width,
      left: left as f32 / scale,
      top: top as f32 / scale,
      right: right as f32 / scale,
      bottom: bottom as f32 / scale,
      ascent,
      descent,
    })
  }
}

fn multiply(a: u8, b: u8) -> u8 {
  ((u16::from(a) * u16::from(b) + 127) / 255) as u8
}