cranpose-render-common 0.1.164

Common rendering contracts for Cranpose
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
//! Turning app-declared font families into parsed faces.
//!
//! [`FontFamily::FileBacked`] and [`FontFamily::LoadedTypeface`] name faces by
//! file rather than by a name inside the font, so something has to read those
//! files and hand the bytes to the rasterizer. That is this module: it parses
//! each face exactly once, at registration, and produces an immutable
//! [`SoftwareTextFontSet`] that measurement and rasterization then share.
//!
//! Nothing here runs per frame or per string. A registry is built at startup,
//! consumed into a font set, and the font set is cloned (it is `Arc`-backed)
//! into every measurer and rasterizer that needs it.
//!
//! Fonts that are not files on disk come in through
//! [`SoftwareTextFontRegistry::register_face_reader`] or
//! [`SoftwareTextFontRegistry::register_face_bytes`]: an APK asset opened with
//! `AndroidApp::asset_manager()`, or anything `cranpose-assets` resolved out of
//! a desktop bundle. `cranpose-assets` is a filesystem path resolver, so it
//! covers bundles but not APK entries, which are not filesystem paths.

use std::{
    io::Read,
    path::{Path, PathBuf},
    sync::Arc,
};

use cranpose_ui::text::{FontFamily, FontFile, FontStyle, FontWeight};

use crate::software_text_raster::{
    FontFamilyKey, SoftwareTextFont, SoftwareTextFontError, SoftwareTextFontSet,
    default_software_text_font,
};

/// Directory Android keeps its system font files in.
pub const ANDROID_SYSTEM_FONT_DIR: &str = "/system/fonts";

/// The weights [`SoftwareTextFontRegistry::register_system_family`] registers
/// when an app does not name its own: Compose's Regular/Medium/Bold set.
pub const DEFAULT_SYSTEM_FAMILY_WEIGHTS: &[FontWeight] =
    &[FontWeight::NORMAL, FontWeight::MEDIUM, FontWeight::BOLD];

/// Why an app-supplied face could not be registered.
///
/// Every variant is recoverable: the caller logs it and keeps whatever faces
/// did load, and resolution falls back to the default face for families that
/// ended up with none.
#[derive(Debug, thiserror::Error)]
pub enum FontLoadError {
    #[error("font family declares no faces")]
    EmptyFamily,
    #[error("font family is not backed by files, so it has nothing to load")]
    NotFileBacked,
    #[error("no system font file for this family under {directory}")]
    NoSystemFontFile { directory: PathBuf },
    #[error("failed to read font file {path}: {source}")]
    Read {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("failed to parse font file {path}: {source}")]
    Parse {
        path: PathBuf,
        #[source]
        source: SoftwareTextFontError,
    },
    #[error("failed to parse font bytes: {source}")]
    ParseBytes {
        #[source]
        source: SoftwareTextFontError,
    },
}

/// Parsed app-supplied faces, on their way to a [`SoftwareTextFontSet`].
///
/// Register everything an app needs once at startup, then call
/// [`SoftwareTextFontRegistry::into_font_set_or_default`]. Registration is
/// where files are read and faces parsed; nothing after it touches the disk.
#[derive(Clone, Default)]
pub struct SoftwareTextFontRegistry {
    faces: Vec<SoftwareTextFont>,
    system_faces: Vec<(FontFamilyKey, FontWeight, FontStyle)>,
}

#[derive(Default)]
struct TolerantLoad {
    first_error: Option<FontLoadError>,
    loaded: usize,
}

impl TolerantLoad {
    fn record(&mut self, result: Result<(), FontLoadError>) {
        match result {
            Ok(()) => self.loaded += 1,
            Err(error) => self.first_error = self.first_error.take().or(Some(error)),
        }
    }

    fn finish(self) -> Result<(), FontLoadError> {
        match self.first_error {
            Some(error) if self.loaded == 0 => Err(error),
            _ => Ok(()),
        }
    }
}

impl SoftwareTextFontRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    /// Register every face of a file-backed family, reading each file from the
    /// filesystem.
    ///
    /// Each [`FontFile`]'s declared weight and style are what resolution
    /// matches on, so one family can carry Regular/Medium/Bold/Italic faces and
    /// a `FontWeight`/`FontStyle` picks between them. A family whose files all
    /// fail to load registers nothing and reports the first failure; text
    /// asking for it then falls back to the default face rather than
    /// disappearing.
    pub fn register_family(&mut self, family: &FontFamily) -> Result<(), FontLoadError> {
        let files = font_files_for(family)?;
        if files.is_empty() {
            return Err(FontLoadError::EmptyFamily);
        }

        let mut reads = FontFileReads::default();
        let mut load = TolerantLoad::default();
        for file in &files {
            load.record(self.register_read_face(
                &mut reads,
                family,
                file.weight,
                file.style,
                Path::new(&file.path),
                &[],
            ));
        }
        load.finish()
    }

    fn register_read_face(
        &mut self,
        reads: &mut FontFileReads,
        family: &FontFamily,
        weight: FontWeight,
        style: FontStyle,
        path: &Path,
        variations: &[([u8; 4], f32)],
    ) -> Result<(), FontLoadError> {
        let bytes = reads.read(path)?;
        let face = SoftwareTextFont::from_registered_bytes_with_variations(
            family,
            weight,
            style,
            bytes.to_vec(),
            variations,
        )
        .map_err(|source| FontLoadError::Parse {
            path: path.to_path_buf(),
            source,
        })?;
        self.faces.push(face);
        Ok(())
    }

    /// Register one face read from an arbitrary stream.
    ///
    /// This is the seam for fonts that are not files on disk — an APK asset
    /// opened through `AndroidApp::asset_manager()`, an archive entry, a
    /// download cache. It mirrors
    /// `SoftwareTextMeasurer::register_hyphenation_dictionary_reader`.
    pub fn register_face_reader(
        &mut self,
        family: &FontFamily,
        weight: FontWeight,
        style: FontStyle,
        reader: &mut impl Read,
    ) -> Result<(), FontLoadError> {
        let mut bytes = Vec::new();
        reader
            .read_to_end(&mut bytes)
            .map_err(|source| FontLoadError::Read {
                path: PathBuf::new(),
                source,
            })?;
        self.register_face_bytes(family, weight, style, bytes)
    }

    /// Register one face from bytes the app already holds.
    pub fn register_face_bytes(
        &mut self,
        family: &FontFamily,
        weight: FontWeight,
        style: FontStyle,
        bytes: impl Into<Vec<u8>>,
    ) -> Result<(), FontLoadError> {
        self.register_face_bytes_with_variations(family, weight, style, bytes, &[])
    }

    /// Register bytes with explicit OpenType axes, overriding the declared weight/style
    /// coordinates. Invalid axes fail without registering a face.
    pub fn register_face_bytes_with_variations(
        &mut self,
        family: &FontFamily,
        weight: FontWeight,
        style: FontStyle,
        bytes: impl Into<Vec<u8>>,
        variations: &[([u8; 4], f32)],
    ) -> Result<(), FontLoadError> {
        let face = SoftwareTextFont::from_registered_bytes_with_variations(
            family, weight, style, bytes, variations,
        )
        .map_err(|source| FontLoadError::ParseBytes { source })?;
        self.faces.push(face);
        Ok(())
    }

    /// Register a face that belongs to no declared family.
    ///
    /// These are the fallbacks: they are eligible for any request that names no
    /// family, and for a `Named` request their own `name` table decides.
    pub fn register_fallback_bytes(
        &mut self,
        bytes: impl Into<Vec<u8>>,
    ) -> Result<(), FontLoadError> {
        let face = SoftwareTextFont::from_bytes(bytes)
            .map_err(|source| FontLoadError::ParseBytes { source })?;
        self.faces.push(face);
        Ok(())
    }

    /// Register the platform's own face for a generic family alias, at each of
    /// `weights`, so styles keep naming `FontFamily::SansSerif` and get the
    /// real system typeface.
    ///
    /// Android backs `sans-serif` with a single variable `Roboto-Regular.ttf`
    /// and describes each weight as a `wght` axis position on it, so most
    /// devices resolve every weight to one file instanced several ways. Where a
    /// build does ship weight-specific static files (`Roboto-Medium.ttf`), they
    /// are preferred. Each weight is first resolved through
    /// [`system_declared_weight`], so a request the platform's font config does
    /// not declare registers the face Android would have returned for it rather
    /// than a `wght` position Android cannot reach. Faces are registered in
    /// `FontStyle::Normal`; an app that
    /// wants a real italic rather than a synthesized slant should call
    /// [`SoftwareTextFontRegistry::register_system_face`] for it, because each
    /// extra face is another copy of the file's bytes.
    pub fn register_system_family(
        &mut self,
        directory: impl AsRef<Path>,
        family: &FontFamily,
        weights: &[FontWeight],
    ) -> Result<(), FontLoadError> {
        let directory = directory.as_ref();
        let mut reads = FontFileReads::default();
        let mut load = TolerantLoad::default();
        for weight in weights {
            load.record(self.register_read_system_face(
                &mut reads,
                directory,
                family,
                *weight,
                FontStyle::Normal,
                &[],
            ));
        }
        load.finish()
    }

    /// Register one weight/style of a generic family alias from the platform's
    /// font directory.
    ///
    /// `weight` is resolved through [`system_declared_weight`] before anything
    /// is read, so the registered face is one the platform's font config
    /// declares. Use [`Self::register_system_face_with_variations`] when matching
    /// explicit coordinates supplied by the platform's text API.
    pub fn register_system_face(
        &mut self,
        directory: impl AsRef<Path>,
        family: &FontFamily,
        weight: FontWeight,
        style: FontStyle,
    ) -> Result<(), FontLoadError> {
        self.register_system_face_with_variations(directory, family, weight, style, &[])
    }

    /// Register a system face at explicit OpenType coordinates, such as the optical
    /// size and weight returned by a platform text API. The first registration of a
    /// family/weight/style wins, as with [`Self::register_system_face`].
    pub fn register_system_face_with_variations(
        &mut self,
        directory: impl AsRef<Path>,
        family: &FontFamily,
        weight: FontWeight,
        style: FontStyle,
        variations: &[([u8; 4], f32)],
    ) -> Result<(), FontLoadError> {
        self.register_read_system_face(
            &mut FontFileReads::default(),
            directory.as_ref(),
            family,
            weight,
            style,
            variations,
        )
    }

    fn register_read_system_face(
        &mut self,
        reads: &mut FontFileReads,
        directory: &Path,
        family: &FontFamily,
        weight: FontWeight,
        style: FontStyle,
        variations: &[([u8; 4], f32)],
    ) -> Result<(), FontLoadError> {
        let weight = system_declared_weight(family, weight);
        if self.has_system_face(family, weight, style) {
            return Ok(());
        }
        let path = system_font_file(directory, family, weight).ok_or_else(|| {
            FontLoadError::NoSystemFontFile {
                directory: directory.to_path_buf(),
            }
        })?;
        self.register_read_face(reads, family, weight, style, &path, variations)?;
        self.system_faces
            .push((FontFamilyKey::of(family), weight, style));
        Ok(())
    }

    fn has_system_face(&self, family: &FontFamily, weight: FontWeight, style: FontStyle) -> bool {
        self.system_faces
            .contains(&(FontFamilyKey::of(family), weight, style))
    }

    /// The faces registered so far.
    pub fn faces(&self) -> &[SoftwareTextFont] {
        &self.faces
    }

    pub fn is_empty(&self) -> bool {
        self.faces.is_empty()
    }

    /// Finish, folding in the static byte slices from `AppLauncher::with_fonts`
    /// as unregistered fallbacks and the embedded default face when nothing
    /// else loaded.
    ///
    /// Registered faces come first, so when a request names no family and the
    /// scores tie, a face the app declared wins over one it merely handed over
    /// as bytes.
    pub fn into_font_set_or_default(mut self, fonts: &[&[u8]]) -> SoftwareTextFontSet {
        for bytes in fonts {
            let _ = self.register_fallback_bytes((*bytes).to_vec());
        }
        if self.faces.is_empty()
            && let Some(default_font) = default_software_text_font()
        {
            self.faces.push(default_font);
        }
        SoftwareTextFontSet::from_faces(self.faces)
    }
}

#[derive(Default)]
struct FontFileReads {
    entries: Vec<(PathBuf, Arc<[u8]>)>,
}

impl FontFileReads {
    fn read(&mut self, path: &Path) -> Result<Arc<[u8]>, FontLoadError> {
        if let Some((_, bytes)) = self.entries.iter().find(|(read, _)| read == path) {
            return Ok(Arc::clone(bytes));
        }
        let bytes: Arc<[u8]> = std::fs::read(path)
            .map_err(|source| FontLoadError::Read {
                path: path.to_path_buf(),
                source,
            })?
            .into();
        self.entries.push((path.to_path_buf(), Arc::clone(&bytes)));
        Ok(bytes)
    }
}

/// The weight a platform's own matcher resolves `weight` to for a generic
/// family alias — never a value that family's font config does not declare.
///
/// Android's `sans-serif` is one variable `Roboto-Regular.ttf` described by
/// `/system/etc/fonts.xml` as nine `<font weight="…">` entries, one per hundred,
/// each pinning `wght` to that hundred. The `wght` axis is therefore reachable
/// to the *font config*, not to a caller: an app asking `sans-serif` for 450
/// gets whichever declared entry Minikin's matcher picks, and Minikin has no
/// way to express a weight between two entries. Instancing the axis at 450
/// anyway draws a face the platform cannot draw — text that measures and lays
/// out perfectly and is simply heavier than every other app on the device.
///
/// The rule is `computeMatch` in `frameworks/minikin/libs/minikin/FontFamily.cpp`:
///
/// ```text
/// int score = abs(style1.weight() / 100 - style2.weight() / 100);
/// if (style1.slant() != style2.slant()) score += 2;
/// ```
///
/// picked by `getClosestMatch`, which keeps a candidate only on `match <
/// bestMatch`. Two consequences carry the behaviour, and both are load-bearing:
///
/// * The division is **integer**, so the request is truncated to its hundred
///   before anything is compared. Against a full hundreds grid the nearest
///   declared entry is therefore always the hundred *below* — 450 resolves to
///   400 and 550 to 500, not by a tie-break but outright, and 599 resolves to
///   500 too.
/// * Where a request does tie between two declared entries — 500 against a
///   `serif` declaring only 400 and 700 scores 1 and 2, but 600 scores 2 and 1,
///   and a family declaring 300 and 500 ties at 400 — the strict `<` keeps the
///   entry declared **first**. Android declares ascending, so a tie goes to the
///   lighter face.
///
/// A slant mismatch costs 2, i.e. 200 weight units, which is why `style` never
/// competes with `weight` here: this resolves within one slant, as
/// [`SoftwareTextFontRegistry::register_system_face`] registers one.
///
/// Families this crate knows no system files for resolve to `weight` unchanged;
/// there is no declared set to honour, and nothing will register for them.
///
/// This applies to the system-font path alone. A face an app supplies is its
/// own font, not an entry in the platform's config, and stays instanceable at
/// any axis value — that is what a variable font is for.
pub fn system_declared_weight(family: &FontFamily, weight: FontWeight) -> FontWeight {
    let Some(files) = system_family_files(family) else {
        return weight;
    };
    closest_declared_weight(files.declared, weight).unwrap_or(weight)
}

fn closest_declared_weight(declared: &[u16], requested: FontWeight) -> Option<FontWeight> {
    let mut best: Option<(u16, u16)> = None;
    for candidate in declared {
        let score = weight_match_score(*candidate, requested.value());
        if best.is_none_or(|(_, best_score)| score < best_score) {
            best = Some((*candidate, score));
        }
    }
    best.map(|(candidate, _)| FontWeight(candidate))
}

fn weight_match_score(declared: u16, requested: u16) -> u16 {
    (declared / 100).abs_diff(requested / 100)
}

/// The file a platform backs `family` with at `weight`, if one is present.
///
/// Weight-specific static files win when the build ships them; otherwise the
/// family's regular file is returned and instanced on its `wght` axis at
/// registration. `weight` is expected to be one the family declares — callers
/// on the system path run it through [`system_declared_weight`] first.
pub fn system_font_file(
    directory: &Path,
    family: &FontFamily,
    weight: FontWeight,
) -> Option<PathBuf> {
    let files = system_family_files(family)?;
    files
        .weighted
        .iter()
        .filter(|(candidate_weight, _)| *candidate_weight == weight.value())
        .map(|(_, name)| directory.join(name))
        .chain(files.regular.iter().map(|name| directory.join(name)))
        .find(|path| path.is_file())
}

struct SystemFamilyFiles {
    regular: &'static [&'static str],
    weighted: &'static [(u16, &'static str)],
    declared: &'static [u16],
}

const DECLARED_HUNDREDS: &[u16] = &[100, 200, 300, 400, 500, 600, 700, 800, 900];

fn system_family_files(family: &FontFamily) -> Option<SystemFamilyFiles> {
    match family {
        FontFamily::Default | FontFamily::SansSerif => Some(SystemFamilyFiles {
            regular: &[
                "Roboto-Regular.ttf",
                "RobotoStatic-Regular.ttf",
                "NotoSans-Regular.ttf",
                "DroidSans.ttf",
                "Core/SFUI.ttf",
                "SFNS.ttf",
            ],
            weighted: &[
                (300, "Roboto-Light.ttf"),
                (500, "Roboto-Medium.ttf"),
                (700, "Roboto-Bold.ttf"),
                (900, "Roboto-Black.ttf"),
            ],
            declared: DECLARED_HUNDREDS,
        }),
        FontFamily::Serif | FontFamily::Fantasy => Some(SystemFamilyFiles {
            regular: &["NotoSerif-Regular.ttf", "DroidSerif-Regular.ttf"],
            weighted: &[(700, "NotoSerif-Bold.ttf"), (700, "DroidSerif-Bold.ttf")],
            declared: &[400, 700],
        }),
        FontFamily::Monospace => Some(SystemFamilyFiles {
            regular: &[
                "DroidSansMono.ttf",
                "RobotoMono-Regular.ttf",
                "CutiveMono-Regular.ttf",
            ],
            weighted: &[(700, "RobotoMono-Bold.ttf")],
            declared: &[400, 700],
        }),
        FontFamily::Cursive => Some(SystemFamilyFiles {
            regular: &["DancingScript-Regular.ttf"],
            weighted: &[(700, "DancingScript-Bold.ttf")],
            declared: &[400, 700],
        }),
        FontFamily::Named(_) | FontFamily::FileBacked(_) | FontFamily::LoadedTypeface(_) => None,
    }
}

fn font_files_for(family: &FontFamily) -> Result<Vec<FontFile>, FontLoadError> {
    match family {
        FontFamily::FileBacked(file_backed) => Ok(file_backed.fonts.clone()),
        FontFamily::LoadedTypeface(typeface) => Ok(vec![FontFile::new(typeface.path.clone())]),
        _ => Err(FontLoadError::NotFileBacked),
    }
}

#[cfg(test)]
#[path = "tests/font_source_tests.rs"]
mod tests;