Skip to main content

rhwp/paint/
font.rs

1pub(crate) use super::font_size::glyph_font_size_is_portable;
2
3/// Export-local identifier for a font blob or collection.
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5pub struct FontBlobKey(pub String);
6
7/// Export-local identifier for an exact font face inside a blob or collection.
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9pub struct FontFaceKey(pub String);
10
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct FontDigest {
13    pub algorithm: String,
14    pub value: String,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub struct BinaryResourceRef {
19    pub kind: BinaryResourceKind,
20    pub id: String,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub enum BinaryResourceKind {
25    FontBlob,
26    ExternalFont,
27}
28
29impl BinaryResourceKind {
30    pub fn as_str(self) -> &'static str {
31        match self {
32            Self::FontBlob => "fontBlob",
33            Self::ExternalFont => "externalFont",
34        }
35    }
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Hash)]
39pub struct FontExternalRef {
40    pub url: String,
41}
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub enum FontResourceSource {
45    Embedded,
46    Bundled,
47    SystemResolved,
48    ExternalUrl,
49    UnresolvedFallback,
50}
51
52impl FontResourceSource {
53    pub fn as_str(self) -> &'static str {
54        match self {
55            Self::Embedded => "embedded",
56            Self::Bundled => "bundled",
57            Self::SystemResolved => "systemResolved",
58            Self::ExternalUrl => "externalUrl",
59            Self::UnresolvedFallback => "unresolvedFallback",
60        }
61    }
62}
63
64/// Whether a font resource is sufficient for portable glyph-id replay.
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub enum FontPortability {
67    PortableBlob {
68        digest: FontDigest,
69        data_ref: BinaryResourceRef,
70    },
71    ExternalVerified {
72        digest: FontDigest,
73        external_ref: FontExternalRef,
74    },
75    ResolvedButNotEmbedded {
76        digest: Option<FontDigest>,
77    },
78    SystemNameOnly,
79    UnresolvedFallback,
80}
81
82impl FontPortability {
83    pub fn kind(&self) -> FontPortabilityKind {
84        match self {
85            Self::PortableBlob { .. } => FontPortabilityKind::PortableBlob,
86            Self::ExternalVerified { .. } => FontPortabilityKind::ExternalVerified,
87            Self::ResolvedButNotEmbedded { .. } => FontPortabilityKind::ResolvedButNotEmbedded,
88            Self::SystemNameOnly => FontPortabilityKind::SystemNameOnly,
89            Self::UnresolvedFallback => FontPortabilityKind::UnresolvedFallback,
90        }
91    }
92
93    pub fn is_self_contained_replayable(&self) -> bool {
94        matches!(self, Self::PortableBlob { .. })
95    }
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
99pub enum FontPortabilityKind {
100    PortableBlob,
101    ExternalVerified,
102    ResolvedButNotEmbedded,
103    SystemNameOnly,
104    UnresolvedFallback,
105}
106
107impl FontPortabilityKind {
108    pub fn as_str(self) -> &'static str {
109        match self {
110            Self::PortableBlob => "portableBlob",
111            Self::ExternalVerified => "externalVerified",
112            Self::ResolvedButNotEmbedded => "resolvedButNotEmbedded",
113            Self::SystemNameOnly => "systemNameOnly",
114            Self::UnresolvedFallback => "unresolvedFallback",
115        }
116    }
117}
118
119#[derive(Debug, Clone, PartialEq, Eq)]
120pub struct LocalizedName {
121    pub locale: Option<String>,
122    pub value: String,
123}
124
125#[derive(Debug, Clone, PartialEq, Eq)]
126pub struct FontBlobResource {
127    pub id: FontBlobKey,
128    pub digest: Option<FontDigest>,
129    pub source: FontResourceSource,
130    pub data_ref: Option<BinaryResourceRef>,
131    pub portability: FontPortability,
132}
133
134#[derive(Debug, Clone, PartialEq, Eq)]
135pub struct FontFaceResource {
136    pub id: FontFaceKey,
137    pub blob_key: FontBlobKey,
138    /// Explicit TTC/OTC face index. This stays visible so backend diagnostics
139    /// and future CanvasKit/native Skia typeface construction are unambiguous.
140    pub face_index: u32,
141    pub postscript_name: Option<String>,
142    pub family_names: Vec<LocalizedName>,
143    pub style_names: Vec<LocalizedName>,
144    pub weight_class: Option<u16>,
145    pub width_class: Option<u16>,
146    pub italic: Option<bool>,
147}
148
149#[derive(Debug, Clone, Default, PartialEq, Eq)]
150pub struct FontResourceTable {
151    pub blobs: Vec<FontBlobResource>,
152    pub faces: Vec<FontFaceResource>,
153}
154
155impl FontResourceTable {
156    pub fn is_empty(&self) -> bool {
157        self.blobs.is_empty() && self.faces.is_empty()
158    }
159}
160
161#[derive(Debug, Clone, PartialEq)]
162pub struct VariationAxisValue {
163    pub tag: String,
164    pub value: f32,
165}
166
167#[derive(Debug, Clone, PartialEq, Eq)]
168pub struct OpenTypeFeatureSetting {
169    pub tag: String,
170    pub enabled: bool,
171    pub value: Option<u32>,
172}
173
174#[derive(Debug, Clone, PartialEq, Eq)]
175pub struct ScriptTag(pub String);
176
177#[derive(Debug, Clone, PartialEq, Eq)]
178pub struct LanguageTag(pub String);
179
180#[derive(Debug, Clone, PartialEq, Eq)]
181pub struct ShapingEngineId(pub String);
182
183#[derive(Debug, Clone, PartialEq, Eq)]
184pub struct FontFallbackPolicyId(pub String);
185
186#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
187pub enum TextDirection {
188    Ltr,
189    Rtl,
190    Auto,
191}
192
193impl TextDirection {
194    pub fn as_str(self) -> &'static str {
195        match self {
196            Self::Ltr => "ltr",
197            Self::Rtl => "rtl",
198            Self::Auto => "auto",
199        }
200    }
201}
202
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
204pub enum WritingMode {
205    HorizontalTb,
206    VerticalRl,
207    VerticalLr,
208}
209
210impl WritingMode {
211    pub fn as_str(self) -> &'static str {
212        match self {
213            Self::HorizontalTb => "horizontal-tb",
214            Self::VerticalRl => "vertical-rl",
215            Self::VerticalLr => "vertical-lr",
216        }
217    }
218}
219
220#[derive(Debug, Clone, PartialEq)]
221pub struct FontInstanceKey {
222    pub face_key: FontFaceKey,
223    pub size_px: f64,
224    pub variations: Vec<VariationAxisValue>,
225    pub synthetic_bold: bool,
226    pub synthetic_italic: bool,
227}
228
229#[derive(Debug, Clone, PartialEq)]
230pub struct ShapeKey {
231    pub font_instance: FontInstanceKey,
232    pub direction: TextDirection,
233    pub writing_mode: WritingMode,
234    pub script: Option<ScriptTag>,
235    pub language: Option<LanguageTag>,
236    pub features: Vec<OpenTypeFeatureSetting>,
237    pub shaping_engine: ShapingEngineId,
238    pub fallback_policy: FontFallbackPolicyId,
239}
240
241#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
242pub enum GlyphRunReplayEligibility {
243    Portable,
244    ConditionalExternalFont,
245    LocalDiagnosticOnly,
246    NotReplayable,
247}
248
249impl GlyphRunReplayEligibility {
250    pub fn as_str(self) -> &'static str {
251        match self {
252            Self::Portable => "portable",
253            Self::ConditionalExternalFont => "conditionalExternalFont",
254            Self::LocalDiagnosticOnly => "localDiagnosticOnly",
255            Self::NotReplayable => "notReplayable",
256        }
257    }
258}
259
260impl From<FontPortabilityKind> for GlyphRunReplayEligibility {
261    fn from(kind: FontPortabilityKind) -> Self {
262        match kind {
263            FontPortabilityKind::PortableBlob => Self::Portable,
264            FontPortabilityKind::ExternalVerified => Self::ConditionalExternalFont,
265            FontPortabilityKind::ResolvedButNotEmbedded | FontPortabilityKind::SystemNameOnly => {
266                Self::LocalDiagnosticOnly
267            }
268            FontPortabilityKind::UnresolvedFallback => Self::NotReplayable,
269        }
270    }
271}
272
273#[cfg(test)]
274mod tests {
275    use super::*;
276
277    #[test]
278    fn font_portability_maps_to_replay_eligibility() {
279        assert_eq!(
280            GlyphRunReplayEligibility::from(FontPortabilityKind::PortableBlob),
281            GlyphRunReplayEligibility::Portable
282        );
283        assert_eq!(
284            GlyphRunReplayEligibility::from(FontPortabilityKind::ExternalVerified),
285            GlyphRunReplayEligibility::ConditionalExternalFont
286        );
287        assert_eq!(
288            GlyphRunReplayEligibility::from(FontPortabilityKind::ResolvedButNotEmbedded),
289            GlyphRunReplayEligibility::LocalDiagnosticOnly
290        );
291        assert_eq!(
292            GlyphRunReplayEligibility::from(FontPortabilityKind::SystemNameOnly),
293            GlyphRunReplayEligibility::LocalDiagnosticOnly
294        );
295        assert_eq!(
296            GlyphRunReplayEligibility::from(FontPortabilityKind::UnresolvedFallback),
297            GlyphRunReplayEligibility::NotReplayable
298        );
299    }
300}