waft_embedder/fonts.rs
1//! The font collection a paragraph shapes against. Flutter's engine owns one per process and
2//! asks the OS for faces; here the collection is the host's [`FontCollection`], filled from
3//! [`Platform::font_source`](crate::Platform::font_source), with the faces of
4//! [`Platform::default_font_families`](crate::Platform::default_font_families) as its fallback
5//! chain, and from whatever the application registers, and handed to
6//! [`ParagraphBuilder::build`](crate::ParagraphBuilder::build).
7
8use valo::{Font, FontAttrs};
9pub use valo::{FontCollection, FontDemand, FontId, FontSource};
10use valo_system_fonts::SystemFonts;
11pub use valo_system_fonts::fontmgr::{FontManager, Typeface};
12
13use crate::TargetPlatform;
14
15/// The engine's font manager: the platform's installed fonts through a [`FontManager`], and
16/// the two family names Flutter's engine defines for the platform's own user-interface font.
17///
18/// `CupertinoSystemText` is that font at text sizes and `CupertinoSystemDisplay` at display
19/// sizes, each answered with every weight the platform has, as the engine registers them
20/// (`txt/platform_mac.mm`: the display face at its 29-point breakpoint, the text face by
21/// the default fallback). Every other family, and character fallback, goes to the manager.
22pub struct SystemFontSource {
23 fonts: SystemFonts,
24}
25
26/// Flutter's `CupertinoSystemText`, answered by the engine's default fallback at text sizes.
27const CUPERTINO_TEXT: &str = "CupertinoSystemText";
28/// Flutter's `CupertinoSystemDisplay`, the system font at and above the display breakpoint.
29const CUPERTINO_DISPLAY: &str = "CupertinoSystemDisplay";
30/// The point size the engine's default text-size lookup is made at.
31const TEXT_SIZE: f32 = 17.0;
32/// The engine's `kSFProDisplayBreakPoint`.
33const DISPLAY_SIZE: f32 = 29.0;
34
35impl SystemFontSource {
36 /// Over `manager`: a host's own lookup, or one that answers across a boundary.
37 pub fn new(manager: Box<dyn FontManager>) -> SystemFontSource {
38 SystemFontSource {
39 fonts: SystemFonts::with_manager(manager),
40 }
41 }
42
43 /// The languages character fallback prefers, BCP 47, most preferred first (Flutter's
44 /// `PlatformDispatcher.locales`).
45 pub fn set_locales(&mut self, locales: Vec<String>) {
46 self.fonts.set_locales(locales);
47 }
48}
49
50impl FontSource for SystemFontSource {
51 fn family(&mut self, name: &str) -> Vec<Font> {
52 match name {
53 CUPERTINO_TEXT => self.fonts.system_family(TEXT_SIZE),
54 CUPERTINO_DISPLAY => self.fonts.system_family(DISPLAY_SIZE),
55 _ => self.fonts.family(name),
56 }
57 }
58
59 fn face_for_codepoint(&mut self, codepoint: char, attrs: FontAttrs) -> Option<Font> {
60 self.fonts.face_for_codepoint(codepoint, attrs)
61 }
62}
63
64/// Flutter `txt::GetDefaultFontFamilies()` of the engine built for `os`, from that OS's file
65/// (`txt/platform_mac.mm`, `platform_linux.cc`, …): the families text is drawn in when its style
66/// names none, first match first.
67///
68/// On Apple's systems Flutter reads `[systemFontOfSize:14].familyName`. That face is what
69/// [`SystemFontSource`] answers as `CupertinoSystemText`, so that is the name given here.
70pub fn default_font_families(os: TargetPlatform) -> Vec<String> {
71 let families: &[&str] = match os {
72 TargetPlatform::Android => &["sans-serif"],
73 TargetPlatform::Fuchsia => &["Roboto"],
74 TargetPlatform::IOS | TargetPlatform::MacOS => &[CUPERTINO_TEXT],
75 TargetPlatform::Linux => &[
76 "Ubuntu",
77 "Adwaita Sans",
78 "Cantarell",
79 "DejaVu Sans",
80 "Liberation Sans",
81 "Arial",
82 ],
83 TargetPlatform::Windows => &["Segoe UI", "Arial"],
84 };
85 families.iter().map(|&family| family.to_owned()).collect()
86}
87
88/// Flutter `skia::textlayout::FontCollection::setDefaultFontManager`: register the faces of
89/// `families`, the platform's default families, as the collection's fallback chain, then keep
90/// `source` for later lookups.
91///
92/// Valo has no default font manager. An empty style `families` list walks this fallback
93/// chain, then `FontId(0)`. Without the chain, a later `register` (the Cupertino icon font)
94/// becomes font 0 and unspecified-family text paints `.notdef`.
95pub fn set_default_font_manager(
96 fonts: &mut FontCollection,
97 mut source: Box<dyn FontSource>,
98 families: &[String],
99) {
100 for name in families {
101 for mut font in source.family(name) {
102 font.add_alias(name);
103 let id = fonts.add(font);
104 fonts.add_fallback(id);
105 }
106 }
107 fonts.add_boxed_source(source);
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113 use crate::Platform;
114
115 #[test]
116 fn a_host_that_answers_nothing_gets_the_engines_generic_family() {
117 assert_eq!(crate::InertPlatform.default_font_families(), vec!["Arial"]);
118 }
119
120 #[test]
121 fn each_os_gets_the_families_of_its_engine_file() {
122 assert_eq!(
123 default_font_families(TargetPlatform::MacOS),
124 vec![CUPERTINO_TEXT]
125 );
126 assert_eq!(
127 default_font_families(TargetPlatform::Android),
128 vec!["sans-serif"]
129 );
130 assert_eq!(
131 default_font_families(TargetPlatform::Windows),
132 vec!["Segoe UI", "Arial"]
133 );
134 }
135}