Skip to main content

vello_encoding/
glyph.rs

1// Copyright 2023 the Vello Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use std::ops::Range;
5
6use peniko::{
7    FontData, Style,
8    kurbo::{Diagonal2, Join},
9};
10
11use super::{StreamOffsets, Transform};
12
13/// Positioned glyph.
14#[derive(Copy, Clone, Default, Debug)]
15pub struct Glyph {
16    /// Glyph identifier.
17    pub id: u32,
18    /// X-offset in run, relative to transform.
19    pub x: f32,
20    /// Y-offset in run, relative to transform.
21    pub y: f32,
22}
23
24/// Synthetic embolden settings for a glyph run.
25#[derive(Clone, Copy, Debug)]
26pub struct FontEmbolden {
27    /// Synthetic embolden amount.
28    pub amount: Diagonal2,
29    /// Join style used when expanding outlines.
30    pub join: Join,
31    /// Miter limit used when expanding outlines.
32    pub miter_limit: f64,
33    /// Tolerance used when expanding outlines.
34    pub tolerance: f64,
35}
36
37impl FontEmbolden {
38    /// Create synthetic embolden settings with default expansion controls.
39    pub fn new(amount: Diagonal2) -> Self {
40        Self {
41            amount,
42            ..Self::default()
43        }
44    }
45
46    /// Set the join style used when expanding outlines.
47    pub fn with_join(mut self, join: Join) -> Self {
48        self.join = join;
49        self
50    }
51
52    /// Set the miter limit used when expanding outlines.
53    pub fn with_miter_limit(mut self, miter_limit: f64) -> Self {
54        self.miter_limit = miter_limit;
55        self
56    }
57
58    /// Set the tolerance used when expanding outlines.
59    pub fn with_tolerance(mut self, tolerance: f64) -> Self {
60        self.tolerance = tolerance;
61        self
62    }
63}
64
65impl Default for FontEmbolden {
66    fn default() -> Self {
67        Self {
68            amount: Diagonal2::new(0.0, 0.0),
69            join: Join::Miter,
70            miter_limit: 4.0,
71            tolerance: 0.1,
72        }
73    }
74}
75
76/// Properties for a sequence of glyphs in an encoding.
77#[derive(Clone)]
78pub struct GlyphRun {
79    /// Font for all glyphs in the run.
80    pub font: FontData,
81    /// Global run transform.
82    pub transform: Transform,
83    /// Per-glyph transform.
84    pub glyph_transform: Option<Transform>,
85    /// Additional transform applied to the brush contents after the global run transform.
86    ///
87    /// This is encoded as the run's paint transform and affects transformed
88    /// brushes such as gradients and images; solid colors ignore it.
89    pub brush_transform: Option<Transform>,
90    /// Size of the font in pixels per em.
91    pub font_size: f32,
92    /// Synthetic embolden settings.
93    pub font_embolden: FontEmbolden,
94    /// True if hinting is enabled.
95    pub hint: bool,
96    /// Range of normalized coordinates in the parent encoding.
97    pub normalized_coords: Range<usize>,
98    /// Fill or stroke style.
99    pub style: Style,
100    /// Range of glyphs in the parent encoding.
101    pub glyphs: Range<usize>,
102    /// Stream offsets where this glyph run should be inserted.
103    pub stream_offsets: StreamOffsets,
104}