blit-fonts 0.13.1

Font discovery and serving for blit
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use std::collections::BTreeSet;

pub fn font_dirs() -> Vec<String> {
    let mut dirs = Vec::new();
    // BLIT_FONT_DIRS: colon-separated list of extra directories to search first
    if let Ok(extra) = std::env::var("BLIT_FONT_DIRS") {
        for d in extra.split(':') {
            let d = d.trim();
            if !d.is_empty() {
                dirs.push(d.to_owned());
            }
        }
    }
    if let Some(home) = std::env::var_os("HOME") {
        let home = home.to_string_lossy();
        dirs.push(format!("{home}/Library/Fonts"));
        dirs.push(format!("{home}/.local/share/fonts"));
        dirs.push(format!("{home}/.fonts"));
    }
    dirs.push("/Library/Fonts".into());
    dirs.push("/System/Library/Fonts".into());
    dirs.push("/usr/share/fonts".into());
    dirs.push("/usr/local/share/fonts".into());
    dirs
}

#[derive(Debug, Clone)]
pub struct FontInfo {
    pub family: String,
    pub subfamily: String,
    pub is_monospace: bool,
}

#[derive(Debug, Clone)]
pub struct FontVariant {
    pub path: String,
    pub weight: String,
    pub style: String,
}

fn sfnt_offset(data: &[u8]) -> Option<usize> {
    if data.len() < 12 {
        return None;
    }
    if &data[0..4] == b"ttcf" {
        if data.len() < 16 {
            return None;
        }
        Some(u32::from_be_bytes([data[12], data[13], data[14], data[15]]) as usize)
    } else {
        Some(0)
    }
}

fn table_slice<'a>(data: &'a [u8], tag: &[u8; 4]) -> Option<&'a [u8]> {
    let offset = sfnt_offset(data)?;
    if offset + 12 > data.len() {
        return None;
    }
    let num_tables = u16::from_be_bytes([data[offset + 4], data[offset + 5]]) as usize;
    if offset + 12 + num_tables * 16 > data.len() {
        return None;
    }
    for i in 0..num_tables {
        let rec = offset + 12 + i * 16;
        if &data[rec..rec + 4] == tag {
            let table_offset =
                u32::from_be_bytes([data[rec + 8], data[rec + 9], data[rec + 10], data[rec + 11]])
                    as usize;
            let table_length = u32::from_be_bytes([
                data[rec + 12],
                data[rec + 13],
                data[rec + 14],
                data[rec + 15],
            ]) as usize;
            let table_end = table_offset.checked_add(table_length)?;
            if table_end > data.len() {
                return None;
            }
            return Some(&data[table_offset..table_end]);
        }
    }
    None
}

fn read_is_monospace(data: &[u8]) -> bool {
    if let Some(post) = table_slice(data, b"post") {
        if post.len() >= 16 {
            let is_fixed_pitch = u32::from_be_bytes([post[12], post[13], post[14], post[15]]);
            if is_fixed_pitch != 0 {
                return true;
            }
        }
    }

    let Some(hhea) = table_slice(data, b"hhea") else {
        return false;
    };
    let Some(hmtx) = table_slice(data, b"hmtx") else {
        return false;
    };
    if hhea.len() < 36 {
        return false;
    }
    let num_long_metrics = u16::from_be_bytes([hhea[34], hhea[35]]) as usize;
    if num_long_metrics == 0 {
        return false;
    }
    let Some(metrics_len) = num_long_metrics.checked_mul(4) else {
        return false;
    };
    if hmtx.len() < metrics_len {
        return false;
    }

    let mut reference_width: Option<u16> = None;
    for i in 0..num_long_metrics {
        let idx = i * 4;
        let advance = u16::from_be_bytes([hmtx[idx], hmtx[idx + 1]]);
        if advance == 0 {
            continue;
        }
        match reference_width {
            Some(width) if width != advance => return false,
            Some(_) => {}
            None => reference_width = Some(advance),
        }
    }

    reference_width.is_some()
}

/// Read the monospace advance width as a fraction of the em square.
/// Returns `advance_width / units_per_em` for the first non-zero advance in hmtx,
/// matching how native terminals (Ghostty, kitty) compute cell width.
fn read_advance_ratio(data: &[u8]) -> Option<f64> {
    let head = table_slice(data, b"head")?;
    if head.len() < 20 {
        return None;
    }
    let units_per_em = u16::from_be_bytes([head[18], head[19]]) as f64;
    if units_per_em == 0.0 {
        return None;
    }

    let hhea = table_slice(data, b"hhea")?;
    let hmtx = table_slice(data, b"hmtx")?;
    if hhea.len() < 36 {
        return None;
    }
    let num_long_metrics = u16::from_be_bytes([hhea[34], hhea[35]]) as usize;
    if num_long_metrics == 0 || hmtx.len() < num_long_metrics * 4 {
        return None;
    }

    for i in 0..num_long_metrics {
        let idx = i * 4;
        let advance = u16::from_be_bytes([hmtx[idx], hmtx[idx + 1]]);
        if advance > 0 {
            return Some(advance as f64 / units_per_em);
        }
    }
    None
}

/// Read font family and subfamily from a TTF/OTF/TTC file's `name` table.
fn read_font_info(data: &[u8]) -> Option<FontInfo> {
    let tbl = table_slice(data, b"name")?;
    if tbl.len() < 6 {
        return None;
    }
    let count = u16::from_be_bytes([tbl[2], tbl[3]]) as usize;
    let string_offset = u16::from_be_bytes([tbl[4], tbl[5]]) as usize;
    if tbl.len() < 6 + count * 12 {
        return None;
    }

    // Collect candidates for name IDs 1 (family), 2 (subfamily), 16 (typo family), 17 (typo subfamily).
    // Prefer platform 3 (Windows UTF-16) over 1 (Mac).
    // Prefer typo (16/17) over legacy (1/2).
    let mut family: Option<String> = None;
    let mut family_pri = 0u8;
    let mut subfamily: Option<String> = None;
    let mut subfamily_pri = 0u8;

    for i in 0..count {
        let rec = 6 + i * 12;
        let platform = u16::from_be_bytes([tbl[rec], tbl[rec + 1]]);
        let name_id = u16::from_be_bytes([tbl[rec + 6], tbl[rec + 7]]);
        let length = u16::from_be_bytes([tbl[rec + 8], tbl[rec + 9]]) as usize;
        let str_off = u16::from_be_bytes([tbl[rec + 10], tbl[rec + 11]]) as usize;

        let is_family = name_id == 1 || name_id == 16;
        let is_subfamily = name_id == 2 || name_id == 17;
        if !is_family && !is_subfamily {
            continue;
        }

        let plat_bonus: u8 = if platform == 3 {
            2
        } else if platform == 1 {
            1
        } else {
            0
        };
        if plat_bonus == 0 {
            continue;
        }
        let typo_bonus: u8 = if name_id >= 16 { 4 } else { 0 };
        let priority = plat_bonus + typo_bonus;

        let start = string_offset + str_off;
        if start + length > tbl.len() {
            continue;
        }
        let raw = &tbl[start..start + length];

        let decoded = if platform == 3 {
            let chars: Vec<u16> = raw
                .chunks_exact(2)
                .map(|c| u16::from_be_bytes([c[0], c[1]]))
                .collect();
            String::from_utf16_lossy(&chars)
        } else {
            String::from_utf8_lossy(raw).into_owned()
        };
        let decoded = decoded.trim().to_owned();
        if decoded.is_empty() {
            continue;
        }

        if is_family && priority > family_pri {
            family = Some(decoded);
            family_pri = priority;
        } else if is_subfamily && priority > subfamily_pri {
            subfamily = Some(decoded);
            subfamily_pri = priority;
        }
    }

    Some(FontInfo {
        family: family?,
        subfamily: subfamily.unwrap_or_else(|| "Regular".to_owned()),
        is_monospace: read_is_monospace(data),
    })
}

fn subfamily_to_weight_style(subfamily: &str) -> (&'static str, &'static str) {
    let s = subfamily.to_lowercase();
    let bold = s.contains("bold") || s.contains("heavy") || s.contains("black");
    let italic = s.contains("italic") || s.contains("oblique");
    match (bold, italic) {
        (true, true) => ("bold", "italic"),
        (true, false) => ("bold", "normal"),
        (false, true) => ("normal", "italic"),
        (false, false) => ("normal", "normal"),
    }
}

pub fn find_font_files(family: &str) -> Vec<FontVariant> {
    if let Some(results) = find_via_fc_match(family) {
        if !results.is_empty() {
            return results;
        }
    }
    let dirs = font_dirs();
    let family_lower = family.to_lowercase();
    let family_nospace = family_lower.replace(' ', "");
    let mut results = Vec::new();
    for dir in &dirs {
        find_in_dir_recursive(dir, &family_lower, &family_nospace, &mut results);
    }
    results
}

fn find_via_fc_match(family: &str) -> Option<Vec<FontVariant>> {
    let output = std::process::Command::new("fc-match")
        .args(["--format", "%{file}\n%{style}\n", "-a", family])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = text.lines().collect();
    let mut results = Vec::new();
    let mut seen = BTreeSet::new();
    for pair in lines.chunks(2) {
        if pair.len() < 2 {
            break;
        }
        let path = pair[0].trim();
        let style_str = pair[1].trim();
        if path.is_empty() || !seen.insert(path.to_owned()) {
            continue;
        }
        if let Ok(data) = std::fs::read(path) {
            if let Some(info) = read_font_info(&data) {
                if !info.family.eq_ignore_ascii_case(family) {
                    continue;
                }
                let (weight, style) = subfamily_to_weight_style(style_str);
                results.push(FontVariant {
                    path: path.to_owned(),
                    weight: weight.to_owned(),
                    style: style.to_owned(),
                });
            }
        }
    }
    if results.is_empty() {
        None
    } else {
        Some(results)
    }
}

fn find_in_dir_recursive(
    dir: &str,
    family_lower: &str,
    family_nospace: &str,
    results: &mut Vec<FontVariant>,
) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            find_in_dir_recursive(
                &path.to_string_lossy(),
                family_lower,
                family_nospace,
                results,
            );
            continue;
        }
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        if !matches!(ext, "ttf" | "otf" | "woff" | "woff2" | "ttc") {
            continue;
        }

        if let Ok(data) = std::fs::read(&path) {
            if let Some(info) = read_font_info(&data) {
                let parsed_lower = info.family.to_lowercase();
                if parsed_lower != family_lower && parsed_lower.replace(' ', "") != family_nospace {
                    continue;
                }
                let (weight, style) = subfamily_to_weight_style(&info.subfamily);
                results.push(FontVariant {
                    path: path.to_string_lossy().into_owned(),
                    weight: weight.to_owned(),
                    style: style.to_owned(),
                });
            }
        }
    }
}

pub fn list_font_families() -> Vec<String> {
    if let Some(families) = list_via_fc_list() {
        return families;
    }
    list_via_name_tables()
}

pub fn list_monospace_font_families() -> Vec<String> {
    if let Some(families) = list_monospace_via_fc_list() {
        return families;
    }
    list_monospace_via_name_tables()
}

fn list_via_fc_list() -> Option<Vec<String>> {
    let output = std::process::Command::new("fc-list")
        .args(["--format", "%{family}\n"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&output.stdout);
    let mut families = BTreeSet::new();
    for line in text.lines() {
        for name in line.split(',') {
            let name = name.trim();
            if !name.is_empty() {
                families.insert(name.to_owned());
            }
        }
    }
    if families.is_empty() {
        return None;
    }
    Some(families.into_iter().collect())
}

fn list_via_name_tables() -> Vec<String> {
    let dirs = font_dirs();
    let mut families = BTreeSet::new();
    for dir in &dirs {
        scan_dir_recursive(dir, &mut families);
    }
    families.into_iter().collect()
}

fn list_monospace_via_fc_list() -> Option<Vec<String>> {
    let output = std::process::Command::new("fc-list")
        .args(["--format", "%{file}\n"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let text = String::from_utf8_lossy(&output.stdout);
    let mut families = BTreeSet::new();
    let mut seen_paths = BTreeSet::new();
    for line in text.lines() {
        let path = line.trim();
        if path.is_empty() || !seen_paths.insert(path.to_owned()) {
            continue;
        }
        let Ok(data) = std::fs::read(path) else {
            continue;
        };
        let Some(info) = read_font_info(&data) else {
            continue;
        };
        if !info.is_monospace {
            continue;
        }
        // Use the name table family so the name matches what find_font_files expects.
        families.insert(info.family);
    }
    if families.is_empty() {
        return None;
    }
    Some(families.into_iter().collect())
}

fn list_monospace_via_name_tables() -> Vec<String> {
    let dirs = font_dirs();
    let mut families = BTreeSet::new();
    for dir in &dirs {
        scan_monospace_dir_recursive(dir, &mut families);
    }
    families.into_iter().collect()
}

fn scan_dir_recursive(dir: &str, families: &mut BTreeSet<String>) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            scan_dir_recursive(&path.to_string_lossy(), families);
            continue;
        }
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        if !matches!(ext, "ttf" | "otf" | "woff" | "woff2" | "ttc") {
            continue;
        }
        if let Ok(data) = std::fs::read(&path) {
            if let Some(info) = read_font_info(&data) {
                families.insert(info.family);
            }
        }
    }
}

fn scan_monospace_dir_recursive(dir: &str, families: &mut BTreeSet<String>) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            scan_monospace_dir_recursive(&path.to_string_lossy(), families);
            continue;
        }
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        if !matches!(ext, "ttf" | "otf" | "woff" | "woff2" | "ttc") {
            continue;
        }
        if let Ok(data) = std::fs::read(&path) {
            if let Some(info) = read_font_info(&data) {
                if info.is_monospace {
                    families.insert(info.family);
                }
            }
        }
    }
}

pub fn base64_encode(data: &[u8]) -> String {
    const CHARS: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
    for chunk in data.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = chunk.get(1).copied().unwrap_or(0) as u32;
        let b2 = chunk.get(2).copied().unwrap_or(0) as u32;
        let n = (b0 << 16) | (b1 << 8) | b2;
        out.push(CHARS[(n >> 18 & 63) as usize] as char);
        out.push(CHARS[(n >> 12 & 63) as usize] as char);
        if chunk.len() > 1 {
            out.push(CHARS[(n >> 6 & 63) as usize] as char);
        } else {
            out.push('=');
        }
        if chunk.len() > 2 {
            out.push(CHARS[(n & 63) as usize] as char);
        } else {
            out.push('=');
        }
    }
    out
}

pub fn font_face_css(family: &str) -> Option<String> {
    let files = find_font_files_with_data(family);
    if files.is_empty() {
        return None;
    }
    let mut css = String::new();
    for (variant, data) in &files {
        let ext = variant.path.rsplit('.').next().unwrap_or("ttf");
        let mime = match ext {
            "otf" => "font/otf",
            "woff" => "font/woff",
            "woff2" => "font/woff2",
            _ => "font/ttf",
        };
        let b64 = base64_encode(data);
        css.push_str(&format!(
            "@font-face {{ font-family: '{}'; font-weight: {}; font-style: {}; src: url('data:{};base64,{}'); }}\n",
            family, variant.weight, variant.style, mime, b64,
        ));
    }
    if css.is_empty() {
        None
    } else {
        Some(css)
    }
}

/// Return the advance-width / units-per-em ratio for a font family's regular variant.
/// This is how native terminals compute cell width: `ratio * font_size_px`.
pub fn font_advance_ratio(family: &str) -> Option<f64> {
    let files = find_font_files_with_data(family);
    // Prefer the "normal" weight regular variant
    for (variant, data) in &files {
        if variant.style == "normal" && (variant.weight == "400" || variant.weight == "normal") {
            if let Some(ratio) = read_advance_ratio(data) {
                return Some(ratio);
            }
        }
    }
    // Fall back to any variant
    for (_variant, data) in &files {
        if let Some(ratio) = read_advance_ratio(data) {
            return Some(ratio);
        }
    }
    None
}

/// Like `find_font_files` but returns the file data alongside each variant,
/// avoiding a second read in `font_face_css`.
fn find_font_files_with_data(family: &str) -> Vec<(FontVariant, Vec<u8>)> {
    let variants = find_font_files(family);
    variants
        .into_iter()
        .filter_map(|v| {
            let data = std::fs::read(&v.path).ok()?;
            Some((v, data))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn build_test_font(tables: &[(&[u8; 4], Vec<u8>)]) -> Vec<u8> {
        let header_len = 12 + tables.len() * 16;
        let mut data = vec![0u8; header_len];
        data[0..4].copy_from_slice(&[0, 1, 0, 0]);
        data[4..6].copy_from_slice(&(tables.len() as u16).to_be_bytes());

        let mut offset = header_len;
        for (i, (tag, table)) in tables.iter().enumerate() {
            let rec = 12 + i * 16;
            data[rec..rec + 4].copy_from_slice(*tag);
            data[rec + 8..rec + 12].copy_from_slice(&(offset as u32).to_be_bytes());
            data[rec + 12..rec + 16].copy_from_slice(&(table.len() as u32).to_be_bytes());
            data.extend_from_slice(table);
            offset += table.len();
        }

        data
    }

    #[test]
    fn parse_font_info_from_system_fonts() {
        let families = list_font_families();
        assert!(!families.is_empty(), "no fonts found on system");
        for f in &families {
            assert!(!f.is_empty());
            assert!(!f.contains('\0'));
        }
    }

    #[test]
    fn subfamily_parsing() {
        assert_eq!(subfamily_to_weight_style("Regular"), ("normal", "normal"));
        assert_eq!(subfamily_to_weight_style("Bold"), ("bold", "normal"));
        assert_eq!(subfamily_to_weight_style("Italic"), ("normal", "italic"));
        assert_eq!(subfamily_to_weight_style("Bold Italic"), ("bold", "italic"));
        assert_eq!(
            subfamily_to_weight_style("Bold Oblique"),
            ("bold", "italic")
        );
    }

    #[test]
    fn detects_monospace_from_post_table() {
        let mut post = vec![0u8; 32];
        post[12..16].copy_from_slice(&1u32.to_be_bytes());
        let font = build_test_font(&[(b"post", post)]);
        assert!(read_is_monospace(&font));
    }

    #[test]
    fn detects_monospace_from_uniform_hmtx_widths() {
        let mut hhea = vec![0u8; 36];
        hhea[34..36].copy_from_slice(&2u16.to_be_bytes());

        let mut hmtx = vec![0u8; 8];
        hmtx[0..2].copy_from_slice(&600u16.to_be_bytes());
        hmtx[4..6].copy_from_slice(&600u16.to_be_bytes());

        let font = build_test_font(&[(b"hhea", hhea), (b"hmtx", hmtx)]);
        assert!(read_is_monospace(&font));
    }

    #[test]
    fn rejects_variable_width_fonts() {
        let mut hhea = vec![0u8; 36];
        hhea[34..36].copy_from_slice(&2u16.to_be_bytes());

        let mut hmtx = vec![0u8; 8];
        hmtx[0..2].copy_from_slice(&500u16.to_be_bytes());
        hmtx[4..6].copy_from_slice(&700u16.to_be_bytes());

        let font = build_test_font(&[(b"hhea", hhea), (b"hmtx", hmtx)]);
        assert!(!read_is_monospace(&font));
    }
}