decal 0.6.0

Declarative DSL for describing scenes and rendering them to SVG or PNG
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use crate::{
    builders::TextSpan,
    layout::{
        FontRegistry,
        RenderContext,
        Stencil,
        StencilScope,
        StencilType,
        Typography,
        BASE_FONT_SIZE,
        BASE_LINE_HEIGHT,
        DEFAULT_FONT_FAMILY,
    },
    paint::{
        write_fill_path,
        Iri,
        ResourceIri,
        ScaledRadii,
    },
    primitives::{
        Color,
        FontStyle,
        FontWeight,
        Mask,
        PaintStack,
    },
    utils::{
        encode_image,
        ElementWriter,
        PathWriter,
    },
};
use base64::{
    engine::general_purpose::STANDARD as BASE64,
    Engine,
};
use cosmic_text::{
    Attrs,
    Buffer,
    Command,
    Family,
    Metrics,
    Shaping,
};
use parking_lot::Mutex;
use png::EncodingError;
use std::{
    fmt::{
        Formatter,
        Write,
    },
    sync::Arc,
};
use swash::scale::image::Content;
use taffy::prelude::*;
use thiserror::Error;
use zeno::Point;

const DEFAULT_COLOR: Color = Color::rgb(0, 0, 0);

/// Errors that can occur while vectorizing text nodes.
#[derive(Error, Debug)]
pub enum TextVectorizeError {
    #[error("failed to write to the output stream")]
    Write(#[from] std::fmt::Error),
    #[error("failed to encode emoji image")]
    EncodeEmoji(#[from] EncodingError),
}

/// Controls which kind of glyphs are rendered during text vectorization.
#[derive(Debug, Default)]
pub(crate) enum GlyphRenderMode {
    /// Render both vector and bitmap glyphs.
    #[default]
    All,
    /// Render only vector glyphs.
    Vector,
    /// Render only bitmap glyphs.
    Bitmap,
}

#[derive(Debug, Clone, Default)]
pub(crate) struct TextMeta {
    spans: Vec<TextSpan>,
    buffer: Option<Buffer>,
    typography: Typography,
    stencil: Stencil,
}

impl TextMeta {
    /// Creates a new [`TextMeta`] instance.
    ///
    /// # Arguments
    /// - `spans`: The list of [`TextSpan`] values.
    pub(crate) fn new(spans: Vec<TextSpan>) -> Self {
        Self {
            spans,
            ..Default::default()
        }
    }

    /// Mutates the typography of the current text.
    ///
    /// # Arguments
    /// - `typography`: The next [`Typography`] value.
    pub(crate) fn typography(&mut self, typography: Typography) {
        self.typography = typography;
    }

    /// Sets the paint used for stencil masking.
    ///
    /// The text will be rendered into a stencil mask and the paint will be
    /// applied through that mask.
    ///
    /// # Arguments
    /// - `value`: The [`PaintStack`] source.
    pub(crate) fn stencil_paint(&mut self, value: PaintStack) {
        self.stencil.paint = value;
    }

    /// Sets the scope of the stencil.
    ///
    /// # Arguments
    /// - `value`: The [`StencilScope`] value.
    pub(crate) fn stencil_scope(&mut self, value: StencilScope) {
        self.stencil.scope = value;
    }

    /// Sets the type of the stencil.
    ///
    /// # Arguments
    /// - `value`: The [`StencilType`] value.
    pub(crate) fn stencil_type(&mut self, value: StencilType) {
        self.stencil.r#type = value;
    }

    /// Measures the intrinsic size of the text based on layout constraints.
    ///
    /// # Arguments
    /// - `known_dimensions`: Known size constraints from `taffy`.
    /// - `available_space`: Available layout space from the parent.
    /// - `fonts`: Shared [`FontRegistry`].
    ///
    /// # Returns
    /// - The resolved size of the text node.
    pub(crate) fn measure(
        &mut self,
        known_dimensions: Size<Option<f32>>,
        available_space: Size<AvailableSpace>,
        fonts: Arc<Mutex<FontRegistry>>,
    ) -> Size<f32> {
        if let Size {
            width: Some(width),
            height: Some(height),
        } = known_dimensions
        {
            return Size { width, height };
        }

        let mut fonts = fonts.lock();
        self.init_buffer(&mut fonts);

        let Some(ref mut buffer) = self.buffer else {
            return Size::zero();
        };

        let width_constraint = known_dimensions.width.or(match available_space.width {
            AvailableSpace::MinContent => Some(0.0),
            AvailableSpace::MaxContent => None,
            AvailableSpace::Definite(width) => Some(width),
        });

        buffer.set_size(&mut fonts.system, width_constraint, None);
        buffer.shape_until_scroll(&mut fonts.system, false);

        let (width, total_lines) = buffer
            .layout_runs()
            .fold((0.0, 0usize), |(width, total_lines), run| {
                (run.line_w.max(width), total_lines + 1)
            });
        let height = total_lines as f32 * buffer.metrics().line_height;

        Size { width, height }
    }

    /// Renders the text node.
    ///
    /// # Arguments
    /// - `ctx`: The current [`RenderContext`].
    /// - `layout`: The computed layout for the node.
    ///
    /// # Returns
    /// - Empty tuple on success.
    /// - [`TextVectorizeError`] if rendering or bitmap encoding fails.
    pub(crate) fn render<W>(
        &self,
        ctx: &mut RenderContext<W>,
        layout: Layout,
    ) -> Result<(), TextVectorizeError>
    where
        W: Write,
    {
        if self.stencil.is_none() {
            self.render_text(ctx.out, &ctx.scene.fonts, GlyphRenderMode::All)
        } else {
            let Size { width, height } = layout.size;
            let mask = {
                Mask::build(|out| {
                    self.render_text(
                        out,
                        &ctx.scene.fonts,
                        if matches!(self.stencil.scope, StencilScope::VectorGlyphs) {
                            GlyphRenderMode::Vector
                        } else {
                            GlyphRenderMode::All
                        },
                    )
                    .map_err(|_| std::fmt::Error)
                })?
                .r#type(self.stencil.r#type.into())
            };

            self.render_stencil(ctx, mask.iri(), width, height)?;
            ctx.scene.resources.lock().get_or_add_resource(mask.into());

            // render bitmaps on top
            if matches!(self.stencil.scope, StencilScope::VectorGlyphs) {
                self.render_text(ctx.out, &ctx.scene.fonts, GlyphRenderMode::Bitmap)?;
            }

            Ok(())
        }
    }

    /// Initializes the internal text shaping buffer.
    ///
    /// The buffer is cached after initialization and reused for measurement and
    /// rendering.
    ///
    /// # Arguments
    /// - `fonts`: The mutable [`FontRegistry`] reference.
    fn init_buffer(&mut self, fonts: &mut FontRegistry) {
        if self.buffer.is_some() {
            return;
        }

        let mut spans = Vec::with_capacity(self.spans.len());

        for (idx, span) in self.spans.iter_mut().enumerate() {
            if span.hidden {
                continue;
            }

            span.typography.cascade_from(&self.typography);
            let (attrs, _) = typography_to_attrs(&mut span.typography, fonts);
            spans.push((span.content.as_str(), attrs.metadata(idx)));
        }

        let mut root_tp = self.typography.clone();
        let (root_attrs, root_metrics) = typography_to_attrs(&mut root_tp, fonts);
        let mut buf = Buffer::new_empty(root_metrics);
        let mut brw = buf.borrow_with(&mut fonts.system);

        if let Some(wrap) = self.typography.wrap {
            brw.set_wrap(wrap.into());
        }

        if let Some(ellipsize) = self.typography.ellipsize {
            brw.set_ellipsize(ellipsize.into());
        }

        brw.set_rich_text(
            spans,
            &root_attrs,
            Shaping::Advanced,
            self.typography.align.map(Into::into),
        );

        self.buffer = Some(brw.to_owned());
    }

    /// Renders glyphs according to the specified render `mode`.
    ///
    /// # Arguments
    /// - `out`: The output writer.
    /// - `font_registry`: Shared [`FontRegistry`].
    /// - `mode`: The [`GlyphRenderMode`] value.
    ///
    /// # Returns
    /// - Empty tuple on success.
    /// - [`TextVectorizeError`] if rendering or bitmap encoding fails.
    fn render_text<W>(
        &self,
        out: &mut W,
        font_registry: &Arc<Mutex<FontRegistry>>,
        mode: GlyphRenderMode,
    ) -> Result<(), TextVectorizeError>
    where
        W: Write,
    {
        let Some(ref buffer) = self.buffer else {
            return Ok(());
        };

        let mut font_registry = font_registry.lock();
        let FontRegistry {
            swash_cache: cache,
            system: font_system,
            ..
        } = &mut *font_registry;

        let skip_vector = matches!(mode, GlyphRenderMode::Bitmap);
        let skip_bitmap = matches!(mode, GlyphRenderMode::Vector);

        for run in buffer.layout_runs() {
            let line_y = run.line_y;

            for glyph in run.glyphs.iter() {
                let physical = glyph.physical((0.0, 0.0), 1.0);
                let glyph_x = physical.x as f32;
                let glyph_y = physical.y as f32;
                let cache_key = physical.cache_key;

                if let Some(outline_commands) = cache
                    .get_outline_commands(font_system, cache_key)
                    .filter(|x| is_drawable(*x))
                {
                    if skip_vector {
                        continue;
                    }

                    ElementWriter::new(out, "path")?
                        .attr(
                            "fill",
                            (self
                                .spans
                                .get(glyph.metadata)
                                .and_then(|span| span.typography.color.clone())
                                .unwrap_or(DEFAULT_COLOR.into()),),
                        )?
                        .write_attr("d", |out| {
                            let mut d = PathWriter::new(out);

                            for command in outline_commands.iter() {
                                match *command {
                                    Command::MoveTo(Point { x, y }) => {
                                        d.move_to(x + glyph_x, line_y + glyph_y - y)?;
                                    }
                                    Command::LineTo(Point { x, y }) => {
                                        d.line_to(x + glyph_x, line_y + glyph_y - y)?;
                                    }
                                    Command::CurveTo(
                                        Point { x: x1, y: y1 },
                                        Point { x: x2, y: y2 },
                                        Point { x, y },
                                    ) => {
                                        d.curve_to(
                                            x1 + glyph_x,
                                            line_y + glyph_y - y1,
                                            x2 + glyph_x,
                                            line_y + glyph_y - y2,
                                            x + glyph_x,
                                            line_y + glyph_y - y,
                                        )?;
                                    }
                                    Command::QuadTo(Point { x: cx, y: cy }, Point { x, y }) => {
                                        d.quad_to(
                                            cx + glyph_x,
                                            line_y + glyph_y - cy,
                                            x + glyph_x,
                                            line_y + glyph_y - y,
                                        )?;
                                    }
                                    Command::Close => d.close()?,
                                }
                            }

                            Ok(())
                        })?
                        .close()?;
                } else if let Some(image) = cache.get_image(font_system, cache_key) {
                    // handle emoji/color glyphs
                    if !skip_bitmap && image.content == Content::Color {
                        ElementWriter::new(out, "image")?
                            .attr(
                                "href",
                                (format_args!(
                                    "data:image/png;base64,{}",
                                    BASE64.encode(encode_image(&image)?)
                                ),),
                            )?
                            .attrs([
                                ("x", glyph_x + image.placement.left as f32),
                                ("y", line_y + glyph_y - image.placement.top as f32),
                                ("width", image.placement.width as f32),
                                ("height", image.placement.height as f32),
                            ])?
                            .close()?;
                    }
                }
            }
        }

        Ok(())
    }

    /// Renders the stencil source using the generated text mask.
    ///
    /// # Arguments
    /// - `ctx`: The current [`RenderContext`].
    /// - `mask_iri`: The IRI of the generated stencil mask.
    /// - `width`: The width of the text bounding box.
    /// - `height`: The height of the text bounding box.
    fn render_stencil<W>(
        &self,
        ctx: &mut RenderContext<W>,
        mask_iri: Iri,
        width: f32,
        height: f32,
    ) -> std::fmt::Result
    where
        W: Write,
    {
        self.stencil.paint.render(
            ctx,
            |out| write_fill_path(out, width, height, ScaledRadii::default()),
            |out| write_fill_path(out, width, height, ScaledRadii::default()),
            |layer, is_use_element| {
                if is_use_element {
                    Ok(layer)
                } else {
                    layer.attr("mask", (format_args!("url(#{mask_iri})"),))
                }
            },
            |group| group.attr("mask", (format_args!("url(#{mask_iri})"),)),
        )
    }
}

impl std::fmt::Display for TextMeta {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(
            &self
                .spans
                .iter()
                .map(|x| x.content.clone())
                .collect::<Vec<_>>()
                .join(""),
        )
    }
}

/// Determines whether a glyph outline contains drawable vector geometry.
///
/// Some glyphs, most notably emoji and other color glyphs, often return outline
/// commands consisting only of non-drawing commands. These outlines do not
/// produce any visible vector output and are instead rendered as bitmap images.
///
/// # Arguments
/// - `cmds`: A slice of glyph outline [`Command`] values.
///
/// # Returns
/// - `true` if the outline contains drawable vector segments.
/// - `false` if the outline does not produce visible vector geometry.
fn is_drawable(cmds: &[Command]) -> bool {
    for cmd in cmds {
        match cmd {
            Command::LineTo(_) | Command::QuadTo(_, _) | Command::CurveTo(_, _, _) => return true,
            _ => {}
        }
    }

    false
}

/// Converts a [`Typography`] value into [`cosmic-text`] shaping attributes.
///
/// The resolved font family name is cached into the provided [`Typography`]
/// instance for later reuse.
///
/// # Arguments
/// - `tp`: The mutable reference to [`Typography`] instance.
/// - `fonts`: The [`FontRegistry`] used for resolving font families.
///
/// # Returns
/// - A tuple containing:
///     - [`Attrs`]: Shaping attributes for [`cosmic-text`].
///     - [`Metrics`]: Computed text metrics.
fn typography_to_attrs<'a>(
    tp: &'a mut Typography,
    fonts: &mut FontRegistry,
) -> (Attrs<'a>, Metrics) {
    let metrics = Metrics {
        font_size: tp.size.unwrap_or(BASE_FONT_SIZE),
        line_height: tp.line_height.unwrap_or(BASE_LINE_HEIGHT),
    };

    let alias = tp.family.clone().unwrap_or(fonts.get_default_family());
    tp.resolved_family = fonts
        .resolve_family_name(alias.as_str())
        .unwrap_or(DEFAULT_FONT_FAMILY.to_string());

    let family = match tp.resolved_family.as_str() {
        "sans-serif" => Family::SansSerif,
        "serif" => Family::Serif,
        "mono" | "monospace" => Family::Monospace,
        name => Family::Name(name),
    };

    let mut attrs = Attrs::new()
        .family(family)
        .metrics(metrics)
        .style(tp.style.unwrap_or(FontStyle::Normal).into())
        .weight(tp.weight.unwrap_or(FontWeight::Normal).into());

    if let Some(letter_spacing) = tp.letter_spacing {
        attrs = attrs.letter_spacing(letter_spacing);
    }

    (attrs, metrics)
}