tinymist_world/font/web/
mod.rs

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
use js_sys::ArrayBuffer;
use tinymist_std::error::prelude::*;
use typst::foundations::Bytes;
use typst::text::{
    Coverage, Font, FontBook, FontFlags, FontInfo, FontStretch, FontStyle, FontVariant, FontWeight,
};
use wasm_bindgen::prelude::*;

use super::{BufferFontLoader, FontLoader, FontResolverImpl, FontSlot};
use crate::font::cache::FontInfoCache;
use crate::font::info::typst_typographic_family;

/// Destructures a JS `[key, value]` pair into a tuple of [`Deserializer`]s.
pub(crate) fn convert_pair(pair: JsValue) -> (JsValue, JsValue) {
    let pair = pair.unchecked_into::<js_sys::Array>();
    (pair.get(0), pair.get(1))
}
struct FontBuilder {}

fn font_family_web_to_typst(family: &str, full_name: &str) -> Result<String> {
    let mut family = family;
    if family.starts_with("Noto")
        || family.starts_with("NewCM")
        || family.starts_with("NewComputerModern")
    {
        family = full_name;
    }

    if family.is_empty() {
        return Err(error_once!("font_family_web_to_typst.empty_family"));
    }

    Ok(typst_typographic_family(family).to_string())
}

struct WebFontInfo {
    family: String,
    full_name: String,
    postscript_name: String,
    style: String,
}

fn infer_info_from_web_font(
    WebFontInfo {
        family,
        full_name,
        postscript_name,
        style,
    }: WebFontInfo,
) -> Result<FontInfo> {
    let family = font_family_web_to_typst(&family, &full_name)?;

    let mut full = full_name;
    full.make_ascii_lowercase();

    let mut postscript = postscript_name;
    postscript.make_ascii_lowercase();

    let mut style = style;
    style.make_ascii_lowercase();

    let search_scopes = [style.as_str(), postscript.as_str(), full.as_str()];

    let variant = {
        // Some fonts miss the relevant bits for italic or oblique, so
        // we also try to infer that from the full name.
        let italic = full.contains("italic");
        let oblique = full.contains("oblique") || full.contains("slanted");

        let style = match (italic, oblique) {
            (false, false) => FontStyle::Normal,
            (true, _) => FontStyle::Italic,
            (_, true) => FontStyle::Oblique,
        };

        let weight = {
            let mut weight = None;
            let mut secondary_weight = None;
            'searchLoop: for &search_style in &[
                "thin",
                "extralight",
                "extra light",
                "extra-light",
                "light",
                "regular",
                "medium",
                "semibold",
                "semi bold",
                "semi-bold",
                "bold",
                "extrabold",
                "extra bold",
                "extra-bold",
                "black",
            ] {
                for (idx, &search_scope) in search_scopes.iter().enumerate() {
                    if search_scope.contains(search_style) {
                        let guess_weight = match search_style {
                            "thin" => Some(FontWeight::THIN),
                            "extralight" => Some(FontWeight::EXTRALIGHT),
                            "extra light" => Some(FontWeight::EXTRALIGHT),
                            "extra-light" => Some(FontWeight::EXTRALIGHT),
                            "light" => Some(FontWeight::LIGHT),
                            "regular" => Some(FontWeight::REGULAR),
                            "medium" => Some(FontWeight::MEDIUM),
                            "semibold" => Some(FontWeight::SEMIBOLD),
                            "semi bold" => Some(FontWeight::SEMIBOLD),
                            "semi-bold" => Some(FontWeight::SEMIBOLD),
                            "bold" => Some(FontWeight::BOLD),
                            "extrabold" => Some(FontWeight::EXTRABOLD),
                            "extra bold" => Some(FontWeight::EXTRABOLD),
                            "extra-bold" => Some(FontWeight::EXTRABOLD),
                            "black" => Some(FontWeight::BLACK),
                            _ => unreachable!(),
                        };

                        if let Some(guess_weight) = guess_weight {
                            if idx == 0 {
                                weight = Some(guess_weight);
                                break 'searchLoop;
                            } else {
                                secondary_weight = Some(guess_weight);
                            }
                        }
                    }
                }
            }

            weight.unwrap_or(secondary_weight.unwrap_or(FontWeight::REGULAR))
        };

        let stretch = {
            let mut stretch = None;
            'searchLoop: for &search_style in &[
                "ultracondensed",
                "ultra_condensed",
                "ultra-condensed",
                "extracondensed",
                "extra_condensed",
                "extra-condensed",
                "condensed",
                "semicondensed",
                "semi_condensed",
                "semi-condensed",
                "normal",
                "semiexpanded",
                "semi_expanded",
                "semi-expanded",
                "expanded",
                "extraexpanded",
                "extra_expanded",
                "extra-expanded",
                "ultraexpanded",
                "ultra_expanded",
                "ultra-expanded",
            ] {
                for (idx, &search_scope) in search_scopes.iter().enumerate() {
                    if search_scope.contains(search_style) {
                        let guess_stretch = match search_style {
                            "ultracondensed" => Some(FontStretch::ULTRA_CONDENSED),
                            "ultra_condensed" => Some(FontStretch::ULTRA_CONDENSED),
                            "ultra-condensed" => Some(FontStretch::ULTRA_CONDENSED),
                            "extracondensed" => Some(FontStretch::EXTRA_CONDENSED),
                            "extra_condensed" => Some(FontStretch::EXTRA_CONDENSED),
                            "extra-condensed" => Some(FontStretch::EXTRA_CONDENSED),
                            "condensed" => Some(FontStretch::CONDENSED),
                            "semicondensed" => Some(FontStretch::SEMI_CONDENSED),
                            "semi_condensed" => Some(FontStretch::SEMI_CONDENSED),
                            "semi-condensed" => Some(FontStretch::SEMI_CONDENSED),
                            "normal" => Some(FontStretch::NORMAL),
                            "semiexpanded" => Some(FontStretch::SEMI_EXPANDED),
                            "semi_expanded" => Some(FontStretch::SEMI_EXPANDED),
                            "semi-expanded" => Some(FontStretch::SEMI_EXPANDED),
                            "expanded" => Some(FontStretch::EXPANDED),
                            "extraexpanded" => Some(FontStretch::EXTRA_EXPANDED),
                            "extra_expanded" => Some(FontStretch::EXTRA_EXPANDED),
                            "extra-expanded" => Some(FontStretch::EXTRA_EXPANDED),
                            "ultraexpanded" => Some(FontStretch::ULTRA_EXPANDED),
                            "ultra_expanded" => Some(FontStretch::ULTRA_EXPANDED),
                            "ultra-expanded" => Some(FontStretch::ULTRA_EXPANDED),
                            _ => None,
                        };

                        if let Some(guess_stretch) = guess_stretch {
                            if idx == 0 {
                                stretch = Some(guess_stretch);
                                break 'searchLoop;
                            }
                        }
                    }
                }
            }

            stretch.unwrap_or(FontStretch::NORMAL)
        };

        FontVariant {
            style,
            weight,
            stretch,
        }
    };

    let flags = {
        // guess mono and serif
        let mut flags = FontFlags::empty();

        for search_scope in search_scopes {
            if search_scope.contains("mono") {
                flags |= FontFlags::MONOSPACE;
            } else if search_scope.contains("serif") {
                flags |= FontFlags::SERIF;
            }
        }

        flags
    };
    let coverage = Coverage::from_vec(vec![0, 4294967295]);

    Ok(FontInfo {
        family,
        variant,
        flags,
        coverage,
    })
}

impl FontBuilder {
    fn to_string(&self, field: &str, val: &JsValue) -> Result<String> {
        Ok(val
            .as_string()
            .ok_or_else(|| JsValue::from_str(&format!("expected string for {field}, got {val:?}")))
            .unwrap())
    }

    fn font_web_to_typst(
        &self,
        val: &JsValue,
    ) -> Result<(JsValue, js_sys::Function, Vec<typst::text::FontInfo>)> {
        let mut postscript_name = String::new();
        let mut family = String::new();
        let mut full_name = String::new();
        let mut style = String::new();
        let mut font_ref = None;
        let mut font_blob_loader = None;
        let mut font_cache: Option<FontInfoCache> = None;

        for (k, v) in
            js_sys::Object::entries(val.dyn_ref().ok_or_else(
                || error_once!("WebFontToTypstFont.entries", val: format!("{:?}", val)),
            )?)
            .iter()
            .map(convert_pair)
        {
            let k = self.to_string("web_font.key", &k)?;
            match k.as_str() {
                "postscriptName" => {
                    postscript_name = self.to_string("web_font.postscriptName", &v)?;
                }
                "family" => {
                    family = self.to_string("web_font.family", &v)?;
                }
                "fullName" => {
                    full_name = self.to_string("web_font.fullName", &v)?;
                }
                "style" => {
                    style = self.to_string("web_font.style", &v)?;
                }
                "ref" => {
                    font_ref = Some(v);
                }
                "info" => {
                    // a previous calculated font info
                    font_cache = serde_wasm_bindgen::from_value(v).ok();
                }
                "blob" => {
                    font_blob_loader = Some(v.clone().dyn_into().map_err(error_once_map!(
                        "web_font.blob_builder",
                        v: format!("{:?}", v)
                    ))?);
                }
                _ => panic!("unknown key for {}: {}", "web_font", k),
            }
        }

        let font_info = match font_cache {
            Some(font_cache) => Some(
                // todo cache invalidatio: font_cache.conditions.iter()
                font_cache.info,
            ),
            None => None,
        };

        let font_info: Vec<FontInfo> = match font_info {
            Some(font_info) => font_info,
            None => {
                vec![infer_info_from_web_font(WebFontInfo {
                    family: family.clone(),
                    full_name,
                    postscript_name,
                    style,
                })?]
            }
        };

        Ok((
            font_ref.ok_or_else(|| error_once!("WebFontToTypstFont.NoFontRef", family: family))?,
            font_blob_loader.ok_or_else(
                || error_once!("WebFontToTypstFont.NoFontBlobLoader", family: family),
            )?,
            font_info,
        ))
    }
}

#[derive(Clone, Debug)]
pub struct WebFont {
    pub info: FontInfo,
    pub context: JsValue,
    pub blob: js_sys::Function,
    pub index: u32,
}

impl WebFont {
    pub fn load(&self) -> Option<ArrayBuffer> {
        self.blob
            .call1(&self.context, &self.index.into())
            .unwrap()
            .dyn_into::<ArrayBuffer>()
            .ok()
    }
}

/// Safety: `WebFont` is only used in the browser environment, and we
/// cannot share data between workers.
unsafe impl Send for WebFont {}

#[derive(Debug)]
pub struct WebFontLoader {
    font: WebFont,
    index: u32,
}

impl WebFontLoader {
    pub fn new(font: WebFont, index: u32) -> Self {
        Self { font, index }
    }
}

impl FontLoader for WebFontLoader {
    fn load(&mut self) -> Option<Font> {
        let font = &self.font;
        web_sys::console::log_3(
            &"dyn init".into(),
            &font.context,
            &format!("{:?}", font.info).into(),
        );
        // let blob = pollster::block_on(JsFuture::from(blob.array_buffer())).unwrap();
        let blob = font.load()?;
        let blob = Bytes::new(js_sys::Uint8Array::new(&blob).to_vec());

        Font::new(blob, self.index)
    }
}

/// Searches for fonts.
pub struct BrowserFontSearcher {
    pub fonts: Vec<(FontInfo, FontSlot)>,
}

impl BrowserFontSearcher {
    /// Create a new, empty browser searcher.
    pub fn new() -> Self {
        let mut searcher = Self { fonts: vec![] };

        if cfg!(feature = "browser-embedded-fonts") {
            searcher.add_embedded();
        }

        searcher
    }

    /// Create a new browser searcher with fonts in a FontResolverImpl.
    pub fn from_resolver(resolver: FontResolverImpl) -> Self {
        let fonts = resolver
            .slots
            .into_iter()
            .enumerate()
            .map(|(idx, slot)| {
                (
                    resolver
                        .book
                        .info(idx)
                        .expect("font should be in font book")
                        .clone(),
                    slot,
                )
            })
            .collect();

        Self { fonts }
    }

    /// Create a new browser searcher with fonts cloned from a FontResolverImpl.
    /// Since FontSlot only holds QueryRef to font data, cloning is cheap.
    pub fn new_with_resolver(resolver: &FontResolverImpl) -> Self {
        let fonts = resolver
            .slots
            .iter()
            .enumerate()
            .map(|(idx, slot)| {
                (
                    resolver
                        .book
                        .info(idx)
                        .expect("font should be in font book")
                        .clone(),
                    slot.clone(),
                )
            })
            .collect();

        Self { fonts }
    }

    /// Build a FontResolverImpl.
    pub fn build(self) -> FontResolverImpl {
        let (info, slots): (Vec<FontInfo>, Vec<FontSlot>) = self.fonts.into_iter().unzip();

        let book = FontBook::from_infos(info);

        FontResolverImpl::new(vec![], book, slots)
    }
}

impl BrowserFontSearcher {
    /// Add fonts that are embedded in the binary.
    pub fn add_embedded(&mut self) {
        for font_data in typst_assets::fonts() {
            let buffer = Bytes::new(font_data);

            self.fonts.extend(
                Font::iter(buffer)
                    .map(|font| (font.info().clone(), FontSlot::new_loaded(Some(font)))),
            );
        }
    }

    pub async fn add_web_fonts(&mut self, fonts: js_sys::Array) -> Result<()> {
        let font_builder = FontBuilder {};

        for v in fonts.iter() {
            let (font_ref, font_blob_loader, font_info) = font_builder.font_web_to_typst(&v)?;

            for (i, info) in font_info.into_iter().enumerate() {
                let index = self.fonts.len();
                self.fonts.push((
                    info.clone(),
                    FontSlot::new(WebFontLoader {
                        font: WebFont {
                            info,
                            context: font_ref.clone(),
                            blob: font_blob_loader.clone(),
                            index: index as u32,
                        },
                        index: i as u32,
                    }),
                ))
            }
        }

        Ok(())
    }

    pub fn add_font_data(&mut self, buffer: Bytes) {
        for (i, info) in FontInfo::iter(buffer.as_slice()).enumerate() {
            let buffer = buffer.clone();
            self.fonts.push((
                info,
                FontSlot::new(BufferFontLoader {
                    buffer: Some(buffer),
                    index: i as u32,
                }),
            ))
        }
    }

    pub fn with_fonts_mut(&mut self, func: impl FnOnce(&mut Vec<(FontInfo, FontSlot)>)) {
        func(&mut self.fonts);
    }
}

impl Default for BrowserFontSearcher {
    fn default() -> Self {
        Self::new()
    }
}