1// Fallback to the system's fonts (decisions/F10.md, question2// 1A): the platform names the font for a character the style's face3// lacks, `text` reads it. Linux asks fontconfig, Windows DirectWrite's4// system font fallback, macOS Core Text; a web page is given no fonts,5// so a program names files to fetch (`fonts.fetch`), served beside it.6// Fonts made by `Fonts::bundled()` never ask the system.7// A font picked is read whole from its file the first time and kept for8// the program's life, a family of its own whose face is placed at the9// style's weight and size as any variable face is; answers are cached10// per script, language, weight and block of 256 characters (and per11// character where a block's answer lacks one), and a font picked for a12// script is tried first for that script's next characters.1314import std::io;15import std::string;16import std::vector;17import std::option;18import std::map;19import std::hash;20import std::process;21import std::fs;2223import pkg::alloy::text;24import pkg::alloy::text::font::face;25import pkg::alloy::text::unicode::tables;26import pkg::alloy::text::unicode::properties;27import system::fontconfig;28import system::directwrite;29import system::coretext;30import system::browser;3132// A font file the platform names: its path, and its face's index in a33// collection or, when it names the face by its PostScript name, that.34pub type SystemFont = struct {35 pub path: String,36 pub index: u32,37 pub name: String = String::empty(),38};3940// The platform's fallback, opened when first asked.41pub type SystemFonts = enum {42 Unopened,43 Unavailable,44 Fontconfig: Fontconfig,45 DirectWrite: DirectWrite,46 CoreText: CoreText,47};4849// What fallback has learnt: the platform's answers by their key, the50// families it picked, and the files read.51pub type Fallback : FontFallback = struct {52 pub system: SystemFonts = SystemFonts::Unopened,53 answers: Map<u64, Option<u32>> = Map<u64, Option<u32>>::empty(), // by block54 characters: Map<u64, Option<u32>> = Map<u64, Option<u32>>::empty(), // where a block's lacks one55 picked: Vector<Picked> = Vector<Picked>::empty(),56 files: Map<String, u32> = Map<String, u32>::empty(), // `path#index` to its family57 pub fetched: Vector<u32> = Vector<u32>::empty(), // families of fonts the program fetched58};5960// A family fallback picked, and what for.61type Picked = struct {62 family: u32,63 script: Script,64 language: u64, // a hash of the language tag65 emoji: bool,66};6768// A face, at the weight and size, covering the grapheme cluster from69// start to end, from the fonts fetched, the families picked for its70// script, then the platform's answer for its first character the face71// `primary` lacks; None when there is none.72fn face(73 self f: &var Fallback,74 store: &var FontStore,75 text: &[u8],76 start: u64,77 end: u64,78 wanted: &Wanted,79 primary: u3280) -> Option<u32> {81 if (!store.system && f.fetched.length() == 0 && target() != "web-wasm32") {82 return ::None;83 }84 f.take_fetched(&var store);85 const language = hash_bytes(&wanted.language);86 var k: u64 = 0;87 while (k < f.fetched.length() + f.picked.length()) {88 const fetched = k < f.fetched.length();89 var family: u32 = 0;90 if (fetched) {91 family = f.fetched.get(k);92 } else {93 const p = &f.picked.get(k - f.fetched.length());94 if (p.script != wanted.script || p.language != language || p.emoji != wanted.emoji) {95 k += 1;96 continue;97 }98 family = p.family;99 }100 const face = store.face_for(Family { .index = family }, wanted.weight, wanted.size);101 if (store.faces.get(face to u64).covers(&text, start, end)) {102 return ::Some(face);103 }104 k += 1;105 }106 // the cluster's first character the primary face lacks107 const first = &store.faces.get(primary to u64);108 var c: u32 = 0;109 var at = start;110 while (at < end) {111 const d = decode(&text, at);112 if (first.glyph_of(d.c) == 0 && (properties(d.c) & IGNORABLE) == 0) {113 c = d.c;114 break;115 }116 at += d.length;117 }118 if (c == 0) {119 c = decode(&text, start).c;120 }121 const base = (wanted.script to u64 << 56)122 | (if (wanted.emoji) 1 to u64 << 55 else 0)123 | (min_of(wanted.weight, 1023) to u64 << 45);124 var family = f.answer(125 &var store,126 base | ((c >> 8) to u64 << 32) | (language & 0xFFFFFFFF),127 c,128 &wanted,129 language,130 false131 );132 if (family is ::Some |picked|) {133 const face = store.face_for(Family { .index = picked }, wanted.weight, wanted.size);134 if (store.faces.get(face to u64).glyph_of(c) != 0) {135 return ::Some(face);136 }137 // the block's answer lacks this character: the platform's for it alone138 family = f.answer(139 &var store,140 base | (c to u64 << 24) | (language & 0xFFFFFF),141 c,142 &wanted,143 language,144 true145 );146 }147 if (family is ::Some |picked|) {148 return ::Some(store.face_for(Family { .index = picked }, wanted.weight, wanted.size));149 }150 return ::None;151}152153// The family the platform picks for the character, kept by the key, for154// the character's block or, `exact`, for it alone.155fn answer(156 self f: &var Fallback,157 store: &var FontStore,158 key: u64,159 c: u32,160 wanted: &Wanted,161 language: u64,162 exact: bool163) -> Option<u32> {164 if (exact) {165 if (f.characters.try_get(&key) is ::Some |&answer|) {166 return answer;167 }168 } else if (f.answers.try_get(&key) is ::Some |&answer|) {169 return answer;170 }171 var family: Option<u32> = ::None;172 if (store.system && f.system.font_for(c, &wanted) is ::Some |&font|) {173 family = f.system_family(&var store, &font);174 }175 if (exact) {176 f.characters.insert(key, family);177 } else {178 f.answers.insert(key, family);179 }180 if (family is ::Some |picked|) {181 const p = Picked {182 .family = picked,183 .script = wanted.script,184 .language = language,185 .emoji = wanted.emoji186 };187 for (f.picked.view()) |&q| {188 if (189 q.family == p.family190 && q.script == p.script191 && q.language == p.language192 && q.emoji == p.emoji193 ) {194 return family;195 }196 }197 f.picked.push(p);198 }199 return family;200}201202fn min_of(a: u32, b: u32) -> u32 => if (a < b) a else b;203204// The family of the system's font, its file read the first time.205fn system_family(self f: &var Fallback, store: &var FontStore, font: &SystemFont) -> Option<u32> {206 var key = f"{font.path}#{font.index}{font.name}";207 if (f.files.try_get(&key.view()) is ::Some |&found|) {208 return ::Some(found);209 }210 // mapped, as a collection of 20 MB is read in a few places211 var file = if (KeptMappedFile::open(&font.path.view()) is ::Some |move f|) move f else {212 return ::None;213 };214 const index = if (font.name.length() > 0) face_named(215 &file.view(),216 &font.name.view()217 ) else font.index;218 var face = if (219 store.read_face(FaceBytes::Mapped(move file), index) is ::Some |move read|220 ) move read else {221 return ::None;222 };223 const family = store.family_of(move face);224 f.files.insert(move key, family);225 return ::Some(family);226}227228// The index of the collection's face of the PostScript name, 0 when229// none has it.230fn face_named(bytes: &[u8], name: &[u8]) -> u32 {231 var index: u32 = 0;232 while (postscript_name_at(&bytes, index) is ::Some |&found|) {233 if (found.view() == name) {234 return index;235 }236 index += 1;237 }238 return 0;239}240241// The font the platform picks for the character, opening its fallback242// the first time.243fn font_for(self system: &var SystemFonts, c: u32, wanted: &Wanted) -> Option<SystemFont> {244 if (system is ::Unopened) {245 system = open_system();246 }247 // each platform's calls built for its targets alone248 if (target() == "linux-x86_64" || target() == "linux-aarch64") {249 if (system is ::Fontconfig |&fc|) {250 return fc.font_for(c, &wanted.language, wanted.weight, wanted.emoji);251 }252 } else if (target() == "windows-x86_64") {253 if (system is ::DirectWrite |&dw|) {254 return dw.font_for(c, &wanted.language, wanted.weight);255 }256 } else if (target() == "macos-arm64" || target() == "macos-x86_64") {257 if (system is ::CoreText |&ct|) {258 return ct.font_for(c, &wanted.language);259 }260 }261 return ::None;262}263264fn open_system() -> SystemFonts {265 if (target() == "linux-x86_64" || target() == "linux-aarch64") {266 if (Fontconfig::load() is ::Some |move fc|) {267 return SystemFonts::Fontconfig(move fc);268 }269 } else if (target() == "windows-x86_64") {270 if (DirectWrite::load() is ::Some |move dw|) {271 return SystemFonts::DirectWrite(move dw);272 }273 } else if (target() == "macos-arm64" || target() == "macos-x86_64") {274 if (CoreText::load() is ::Some |move ct|) {275 return SystemFonts::CoreText(move ct);276 }277 }278 return SystemFonts::Unavailable;279}280281// Starts fetching a font file the program serves beside it, for fallback282// on the web; elsewhere the file is read now.283fn fetch(self f: &var Fallback, store: &var FontStore, path: &[u8]) {284 if (target() == "web-wasm32") {285 start_fetch(&path);286 return;287 }288 var file = if (KeptMappedFile::open(&path) is ::Some |move m|) move m else {289 return;290 };291 if (store.read_face(FaceBytes::Mapped(move file), 0) is ::Some |move face|) {292 const family = store.family_of(move face);293 f.fetched.push(family);294 }295}296297// Adds the fonts fetched since last asked as fallback families.298fn take_fetched(self f: &var Fallback, store: &var FontStore) {299 if (target() == "web-wasm32") {300 while (next_fetched() is ::Some |move bytes|) {301 if (store.read_face(FaceBytes::Owned(move bytes), 0) is ::Some |move face|) {302 const family = store.family_of(move face);303 f.fetched.push(family);304 }305 }306 }307}