valo-text 0.1.0

Typographer and paragraph layout for valo: shaping, bidi, wrapping, glyph raster, SDF
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! COLR rasterization — the painting half. skrifa owns the
//! hard half (parsing + paint-graph traversal, emitting [`ColorPainter`]
//! callbacks in FONT UNITS); this module rasterizes those callbacks with
//! tiny-skia into the same premultiplied-RGBA [`GlyphImage`] the CBDT path
//! produces — the atlas and the color text pipeline never know the source.
//! References: Skia's COLRv1 implementation and typf-render-color
//! (Apache-2.0); we differ by rendering sweep gradients for real instead
//! of a solid-color fallback.

use skrifa::color::{Brush, ColorGlyph, ColorPainter, ColorStop, CompositeMode};
use skrifa::outline::{DrawSettings, OutlineGlyphCollection};
use skrifa::prelude::{LocationRef, Size};
use skrifa::raw::types::BoundingBox;
use skrifa::raw::TableProvider;
use skrifa::{GlyphId, MetadataProvider};
use tiny_skia as ts;

use crate::font::Font;
use crate::raster::{GlyphImage, TsPathPen};

/// Rasterize `glyph` at `px` if the font carries a COLR form for it.
/// (The caller tries swash's CBDT/COLRv0 fast paths first.)
pub(crate) fn raster(font: &Font, glyph: u32, px: f32) -> Option<GlyphImage> {
    let font_ref = skrifa::FontRef::from_index(font.data(), font.face_index()).ok()?;
    let color = font_ref.color_glyphs().get(GlyphId::new(glyph))?;
    let bounds = pixel_bounds(font, &color, px)?;
    let location = LocationRef::from(font.variation_location());
    let mut painter = Painter::new(&font_ref, location, &bounds)?;
    color.paint(location, &mut painter).ok()?;
    Some(painter.into_image(&bounds))
}

/// The glyph's pixel box, y-UP around the origin (swash placement space):
/// the COLRv1 clip box when the font declares one, else the font-wide ink
/// box — always padded a pixel so antialiased edges survive the crop.
struct PixelBounds {
    x_min: f32,
    y_max: f32,
    width: u32,
    height: u32,
    /// font units → pixmap pixels (scale + y-flip + origin shift).
    to_pixmap: ts::Transform,
}

fn pixel_bounds(font: &Font, color: &ColorGlyph, px: f32) -> Option<PixelBounds> {
    let unpadded = color
        .bounding_box(LocationRef::from(font.variation_location()), Size::new(px))
        .map(|b| (b.x_min, b.y_min, b.x_max, b.y_max))
        .or_else(|| font.ink_box_px(px))?;
    let (x_min, y_min, x_max, y_max) = unpadded;
    let (x_min, y_min) = (x_min.floor() - 1.0, y_min.floor() - 1.0);
    let (x_max, y_max) = (x_max.ceil() + 1.0, y_max.ceil() + 1.0);
    let (width, height) = ((x_max - x_min) as u32, (y_max - y_min) as u32);
    if width == 0 || height == 0 || width > 4096 || height > 4096 {
        return None;
    }
    let scale = px / font.units_per_em();
    let _ = y_min;
    Some(PixelBounds {
        x_min,
        y_max,
        width,
        height,
        to_pixmap: ts::Transform::from_row(scale, 0.0, 0.0, -scale, -x_min, y_max),
    })
}

/// One compositing layer: its pixels and how it merges down on pop.
struct Layer {
    pixmap: ts::Pixmap,
    mode: CompositeMode,
}

/// A [`ColorPainter`] rasterizing onto a tiny-skia pixmap stack: transforms
/// concatenate, clips intersect as masks, layers composite on pop, brushes
/// become shaders (sweep gradients rasterize manually — tiny-skia has none).
struct Painter<'a> {
    outlines: OutlineGlyphCollection<'a>,
    /// The instance's axis position — layer outlines draw at it.
    location: LocationRef<'a>,
    /// CPAL palette 0, resolved to straight-alpha colors once.
    palette: Vec<ts::Color>,
    layers: Vec<Layer>,
    /// Current = last; base entry maps font units into the pixmap.
    transforms: Vec<ts::Transform>,
    /// Current = last; each entry is already intersected with its parents.
    clips: Vec<ts::Mask>,
    width: u32,
    height: u32,
}

impl<'a> Painter<'a> {
    fn new(
        font_ref: &skrifa::FontRef<'a>,
        location: LocationRef<'a>,
        bounds: &PixelBounds,
    ) -> Option<Self> {
        Some(Painter {
            outlines: font_ref.outline_glyphs(),
            location,
            palette: read_palette(font_ref),
            layers: vec![Layer {
                pixmap: ts::Pixmap::new(bounds.width, bounds.height)?,
                mode: CompositeMode::SrcOver,
            }],
            transforms: vec![bounds.to_pixmap],
            clips: Vec::new(),
            width: bounds.width,
            height: bounds.height,
        })
    }

    fn into_image(mut self, bounds: &PixelBounds) -> GlyphImage {
        GlyphImage {
            width: self.width,
            height: self.height,
            left: bounds.x_min as i32,
            top: bounds.y_max as i32,
            data: self.layers.swap_remove(0).pixmap.take(),
        }
    }

    fn transform(&self) -> ts::Transform {
        *self
            .transforms
            .last()
            .expect("base transform always present")
    }

    /// Push the current clip ∩ `path` (path in font units, like every
    /// callback — the current transform carries it into pixmap space).
    fn push_clip_path(&mut self, path: &ts::Path) {
        let mut mask = match self.clips.last() {
            Some(top) => top.clone(),
            None => full_mask(self.width, self.height),
        };
        mask.intersect_path(path, ts::FillRule::Winding, true, self.transform());
        self.clips.push(mask);
    }

    /// The glyph's outline as a FONT-UNIT path (unscaled — transforms are
    /// applied where the path is consumed).
    fn glyph_path(&self, glyph_id: GlyphId) -> Option<ts::Path> {
        let outline = self.outlines.get(glyph_id)?;
        let mut pen = TsPathPen::default();
        outline
            .draw(
                DrawSettings::unhinted(Size::unscaled(), self.location),
                &mut pen,
            )
            .ok()?;
        pen.builder.finish()
    }

    /// Resolve a CPAL reference to a straight-alpha color. `0xFFFF` is the
    /// spec's "foreground" sentinel — a fixed color glyph has no text color
    /// to inherit, so it resolves to opaque black (documented divergence:
    /// Skia takes the paint color).
    fn palette_color(&self, index: u16, alpha: f32) -> ts::Color {
        let mut color = if index == 0xFFFF {
            ts::Color::BLACK
        } else {
            self.palette
                .get(index as usize)
                .copied()
                .unwrap_or(ts::Color::BLACK)
        };
        color.apply_opacity(alpha.clamp(0.0, 1.0));
        color
    }

    /// Stops resolved to (offset, straight color) pairs — kept in a plain
    /// representation because tiny-skia's `GradientStop` is write-only.
    fn resolved_stops(&self, stops: &[ColorStop]) -> Vec<(f32, ts::Color)> {
        stops
            .iter()
            .map(|s| {
                (
                    s.offset.clamp(0.0, 1.0),
                    self.palette_color(s.palette_index, s.alpha),
                )
            })
            .collect()
    }

    /// Fill the current clip with `brush` on the top layer — the one
    /// operation every leaf of the paint graph reduces to.
    fn fill_with_brush(&mut self, brush: &Brush<'_>) {
        let paint = match self.brush_paint(brush) {
            Some(paint) => paint,
            None => return self.fill_sweep_fallback(brush),
        };
        let rect = match ts::Rect::from_xywh(0.0, 0.0, self.width as f32, self.height as f32) {
            Some(rect) => rect,
            None => return,
        };
        let (clip, layer) = (self.clips.last(), self.layers.last_mut());
        if let Some(layer) = layer {
            layer
                .pixmap
                .fill_rect(rect, &paint, ts::Transform::identity(), clip);
        }
    }

    /// Solid and linear/radial gradients map onto tiny-skia shaders (the
    /// shader's transform carries font units → pixels). Sweep returns
    /// `None` — no such shader exists; it rasterizes manually.
    fn brush_paint(&self, brush: &Brush<'_>) -> Option<ts::Paint<'static>> {
        let mut paint = ts::Paint {
            anti_alias: true,
            ..Default::default()
        };
        paint.shader = match *brush {
            Brush::Solid {
                palette_index,
                alpha,
            } => ts::Shader::SolidColor(self.palette_color(palette_index, alpha)),
            Brush::LinearGradient {
                p0,
                p1,
                color_stops,
                extend,
            } => ts::LinearGradient::new(
                ts::Point::from_xy(p0.x, p0.y),
                ts::Point::from_xy(p1.x, p1.y),
                shader_stops(&self.resolved_stops(color_stops)),
                spread_mode(extend),
                self.transform(),
            )
            .unwrap_or(ts::Shader::SolidColor(last_stop_color(self, color_stops)?)),
            Brush::RadialGradient {
                c0,
                r0,
                c1,
                r1,
                color_stops,
                extend,
            } => radial_shader(self, c0, r0, c1, r1, color_stops, extend)?,
            Brush::SweepGradient { .. } => return None,
        };
        Some(paint)
    }

    /// Sweep gradients, rasterized per pixel: inverse-map the pixel into
    /// font units, take the clockwise angle around the center, normalize
    /// into the stop line, sample. Bounded by the glyph box, so the naive
    /// loop is fine (COLR sweeps are rare and small).
    fn fill_sweep_fallback(&mut self, brush: &Brush<'_>) {
        let Brush::SweepGradient {
            c0,
            start_angle,
            end_angle,
            color_stops,
            extend,
        } = *brush
        else {
            return;
        };
        if color_stops.is_empty() || (end_angle - start_angle).abs() < f32::EPSILON {
            return;
        }
        let Some(inverse) = self.transform().invert() else {
            return;
        };
        let stops = self.resolved_stops(color_stops);
        let clip = self.clips.last().cloned();
        let (width, height) = (self.width, self.height);
        let Some(layer) = self.layers.last_mut() else {
            return;
        };
        let pixels = layer.pixmap.pixels_mut();
        for y in 0..height {
            for x in 0..width {
                let index = (y * width + x) as usize;
                let coverage = clip.as_ref().map_or(255, |m| m.data()[index]);
                if coverage == 0 {
                    continue;
                }
                let mut point = ts::Point::from_xy(x as f32 + 0.5, y as f32 + 0.5);
                inverse.map_point(&mut point);
                // Clockwise angle in degrees, matching the callback's
                // contract; only 0..360 from the start angle is drawn.
                let angle = (point.y - c0.y).atan2(point.x - c0.x).to_degrees();
                let t = (angle - start_angle) / (end_angle - start_angle);
                let Some(t) = apply_extend(t, extend) else {
                    continue;
                };
                let color = sample_stops(&stops, t);
                blend_src_over(&mut pixels[index], color, coverage);
            }
        }
    }
}

impl ColorPainter for Painter<'_> {
    fn push_transform(&mut self, transform: skrifa::color::Transform) {
        let t = ts::Transform::from_row(
            transform.xx,
            transform.yx,
            transform.xy,
            transform.yy,
            transform.dx,
            transform.dy,
        );
        self.transforms.push(self.transform().pre_concat(t));
    }

    fn pop_transform(&mut self) {
        if self.transforms.len() > 1 {
            self.transforms.pop();
        }
    }

    fn push_clip_glyph(&mut self, glyph_id: GlyphId) {
        match self.glyph_path(glyph_id) {
            Some(path) => self.push_clip_path(&path),
            // An unresolvable clip glyph must still push: clip everything
            // (an empty mask) so the matching pop stays balanced.
            None => self
                .clips
                .push(ts::Mask::new(self.width, self.height).expect("mask dims match pixmap")),
        }
    }

    fn push_clip_box(&mut self, clip_box: BoundingBox<f32>) {
        let rect = ts::Rect::from_ltrb(
            clip_box.x_min,
            clip_box.y_min,
            clip_box.x_max,
            clip_box.y_max,
        )
        .map(ts::PathBuilder::from_rect);
        match rect {
            Some(path) => self.push_clip_path(&path),
            None => self
                .clips
                .push(ts::Mask::new(self.width, self.height).expect("mask dims match pixmap")),
        }
    }

    fn pop_clip(&mut self) {
        self.clips.pop();
    }

    fn fill(&mut self, brush: Brush<'_>) {
        self.fill_with_brush(&brush);
    }

    fn push_layer(&mut self, composite_mode: CompositeMode) {
        if let Some(pixmap) = ts::Pixmap::new(self.width, self.height) {
            self.layers.push(Layer {
                pixmap,
                mode: composite_mode,
            })
        }
    }

    fn pop_layer(&mut self) {
        if self.layers.len() <= 1 {
            return;
        }
        let layer = self.layers.pop().expect("checked non-base");
        let paint = ts::PixmapPaint {
            blend_mode: blend_mode(layer.mode),
            ..Default::default()
        };
        if let Some(below) = self.layers.last_mut() {
            below.pixmap.draw_pixmap(
                0,
                0,
                layer.pixmap.as_ref(),
                &paint,
                ts::Transform::identity(),
                None,
            );
        }
    }
}

/// Two-point conical via tiny-skia, which models `r0 = 0`: when the inner
/// radius is real, the stop line is remapped onto `[r0/r1, 1]` — exact for
/// concentric circles, a close approximation otherwise (typf punts to `r1`
/// unremapped; Skia solves the full quadratic).
fn radial_shader(
    painter: &Painter,
    c0: skrifa::raw::types::Point<f32>,
    r0: f32,
    c1: skrifa::raw::types::Point<f32>,
    r1: f32,
    color_stops: &[ColorStop],
    extend: skrifa::color::Extend,
) -> Option<ts::Shader<'static>> {
    if r1 <= 0.0 {
        return Some(ts::Shader::SolidColor(last_stop_color(
            painter,
            color_stops,
        )?));
    }
    let mut stops = painter.resolved_stops(color_stops);
    if r0 > 0.0 && r0 < r1 {
        let base = r0 / r1;
        for (offset, _) in &mut stops {
            *offset = base + *offset * (1.0 - base);
        }
    }
    ts::RadialGradient::new(
        ts::Point::from_xy(c0.x, c0.y),
        ts::Point::from_xy(c1.x, c1.y),
        r1,
        shader_stops(&stops),
        spread_mode(extend),
        painter.transform(),
    )
    .or_else(|| {
        Some(ts::Shader::SolidColor(last_stop_color(
            painter,
            color_stops,
        )?))
    })
}

fn last_stop_color(painter: &Painter, stops: &[ColorStop]) -> Option<ts::Color> {
    stops
        .last()
        .map(|s| painter.palette_color(s.palette_index, s.alpha))
}

/// Normalize a sweep parameter through the gradient's extend mode; `None`
/// outside the drawn range for `Pad` beyond [0, 1] is NOT possible (pad
/// clamps), only non-finite values bail.
fn apply_extend(t: f32, extend: skrifa::color::Extend) -> Option<f32> {
    if !t.is_finite() {
        return None;
    }
    Some(match extend {
        skrifa::color::Extend::Repeat => t.rem_euclid(1.0),
        skrifa::color::Extend::Reflect => {
            let cycle = t.rem_euclid(2.0);
            if cycle > 1.0 {
                2.0 - cycle
            } else {
                cycle
            }
        }
        _ => t.clamp(0.0, 1.0),
    })
}

fn shader_stops(stops: &[(f32, ts::Color)]) -> Vec<ts::GradientStop> {
    stops
        .iter()
        .map(|&(offset, color)| ts::GradientStop::new(offset, color))
        .collect()
}

/// Piecewise-linear sample of a sorted stop line at `t` ∈ [0, 1].
fn sample_stops(stops: &[(f32, ts::Color)], t: f32) -> ts::Color {
    let Some(&(first_offset, first_color)) = stops.first() else {
        return ts::Color::TRANSPARENT;
    };
    if t <= first_offset {
        return first_color;
    }
    for pair in stops.windows(2) {
        let ((a_off, a_color), (b_off, b_color)) = (pair[0], pair[1]);
        if t <= b_off {
            let span = (b_off - a_off).max(f32::EPSILON);
            return lerp_color(a_color, b_color, (t - a_off) / span);
        }
    }
    stops
        .last()
        .map(|&(_, c)| c)
        .unwrap_or(ts::Color::TRANSPARENT)
}

fn lerp_color(a: ts::Color, b: ts::Color, k: f32) -> ts::Color {
    let mix = |x: f32, y: f32| x + (y - x) * k;
    ts::Color::from_rgba(
        mix(a.red(), b.red()),
        mix(a.green(), b.green()),
        mix(a.blue(), b.blue()),
        mix(a.alpha(), b.alpha()),
    )
    .unwrap_or(ts::Color::TRANSPARENT)
}

/// src-over one straight-alpha color onto a premultiplied pixel, scaled by
/// the clip's coverage — the sweep path's single blending need.
fn blend_src_over(dst: &mut ts::PremultipliedColorU8, color: ts::Color, coverage: u8) {
    let a = color.alpha() * (coverage as f32 / 255.0);
    let (sr, sg, sb) = (color.red() * a, color.green() * a, color.blue() * a);
    let inv = 1.0 - a;
    let to_u8 = |v: f32| (v * 255.0 + 0.5) as u8;
    *dst = ts::PremultipliedColorU8::from_rgba(
        to_u8(sr + dst.red() as f32 / 255.0 * inv),
        to_u8(sg + dst.green() as f32 / 255.0 * inv),
        to_u8(sb + dst.blue() as f32 / 255.0 * inv),
        to_u8(a + dst.alpha() as f32 / 255.0 * inv),
    )
    .unwrap_or(*dst);
}

/// CPAL palette 0 as straight-alpha colors (records are BGRA bytes).
fn read_palette(font_ref: &skrifa::FontRef) -> Vec<ts::Color> {
    let Ok(cpal) = font_ref.cpal() else {
        return Vec::new();
    };
    let Some(Ok(records)) = cpal.color_records_array() else {
        return Vec::new();
    };
    let count = cpal.num_palette_entries() as usize;
    records
        .iter()
        .take(count)
        .map(|r| ts::Color::from_rgba8(r.red, r.green, r.blue, r.alpha))
        .collect()
}

fn full_mask(width: u32, height: u32) -> ts::Mask {
    let mut mask = ts::Mask::new(width, height).expect("mask dims match pixmap");
    if let Some(rect) = ts::Rect::from_xywh(0.0, 0.0, width as f32, height as f32) {
        mask.fill_path(
            &ts::PathBuilder::from_rect(rect),
            ts::FillRule::Winding,
            false,
            ts::Transform::identity(),
        );
    }
    mask
}

fn spread_mode(extend: skrifa::color::Extend) -> ts::SpreadMode {
    match extend {
        skrifa::color::Extend::Repeat => ts::SpreadMode::Repeat,
        skrifa::color::Extend::Reflect => ts::SpreadMode::Reflect,
        _ => ts::SpreadMode::Pad,
    }
}

/// The full CompositeMode set maps 1:1 onto tiny-skia's blend modes.
fn blend_mode(mode: CompositeMode) -> ts::BlendMode {
    use ts::BlendMode as B;
    use CompositeMode as C;
    match mode {
        C::Clear => B::Clear,
        C::Src => B::Source,
        C::Dest => B::Destination,
        C::SrcOver => B::SourceOver,
        C::DestOver => B::DestinationOver,
        C::SrcIn => B::SourceIn,
        C::DestIn => B::DestinationIn,
        C::SrcOut => B::SourceOut,
        C::DestOut => B::DestinationOut,
        C::SrcAtop => B::SourceAtop,
        C::DestAtop => B::DestinationAtop,
        C::Xor => B::Xor,
        C::Plus => B::Plus,
        C::Screen => B::Screen,
        C::Overlay => B::Overlay,
        C::Darken => B::Darken,
        C::Lighten => B::Lighten,
        C::ColorDodge => B::ColorDodge,
        C::ColorBurn => B::ColorBurn,
        C::HardLight => B::HardLight,
        C::SoftLight => B::SoftLight,
        C::Difference => B::Difference,
        C::Exclusion => B::Exclusion,
        C::Multiply => B::Multiply,
        C::HslHue => B::Hue,
        C::HslSaturation => B::Saturation,
        C::HslColor => B::Color,
        C::HslLuminosity => B::Luminosity,
        _ => B::SourceOver,
    }
}