systemless 0.122.0

High-Level Emulation for classic Macintosh applications
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
//! Font routing, guest resources, and bundled TrueType outlines.
//! Guest FONT/NFNT/sfnt resources and explicit local overrides take precedence.

mod bundled;
mod compatibility;
pub mod families;
mod resources;
pub mod style;
use resources::*;
pub(crate) use resources::{
    parse_fond_associations, register_resource_font_strike,
    register_resource_font_strike_for_family, register_resource_outline_font, FondAssociation,
};
pub(crate) mod outline;
pub mod override_format;

use std::collections::HashMap;
use std::ffi::OsString;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{LazyLock, Mutex};

pub use self::families::{
    FONT_APPLICATION, FONT_ATHENS, FONT_CAIRO, FONT_CHICAGO, FONT_COURIER, FONT_GENEVA,
    FONT_HELVETICA, FONT_LONDON, FONT_LOSANGELES, FONT_MOBILE, FONT_MONACO, FONT_NEWYORK,
    FONT_PALATINO, FONT_SANFRAN, FONT_SEATTLE, FONT_SYMBOL, FONT_TIMES, FONT_TORONTO, FONT_VENICE,
};

/// Rasterized glyph dimensions and offset into a shared coverage buffer.
#[derive(Clone, Copy)]
pub struct Glyph {
    pub width: u8,
    pub height: u8,
    pub advance: u8,
    pub origin_x: i8,
    pub origin_y: i8,
    pub data_offset: usize,
}

/// Mac-style font metrics for a single (face, size) pair.
/// Returned by `GetFontInfo` ($A88B) via `get_font_metrics`.
#[derive(Copy, Clone)]
pub struct FontMetrics {
    pub ascent: i16,
    pub descent: i16,
    pub wid_max: i16,
    pub leading: i16,
}

/// One rasterized (font_id, size) face: metrics plus a slice of glyph
/// descriptors and a shared coverage-byte buffer that the descriptors'
/// `data_offset` fields index into.
pub struct FontFace {
    pub font_id: i16,
    pub size: i16,
    pub metrics: FontMetrics,
    pub glyphs: &'static [Glyph],
    pub data: &'static [u8],
}

/// Glyph for a Mac Roman extended character (code 0x80..=0xFF).
pub struct MacRomanGlyph {
    pub mac_code: u8,
    pub glyph: Glyph,
}

/// Rasterized Mac Roman extension of a face.
pub struct MacRomanFace {
    pub font_id: i16,
    pub size: i16,
    pub glyphs: &'static [MacRomanGlyph],
    pub data: &'static [u8],
}

/// Coverage threshold used by QuickDraw binary transfer modes.
pub const MONO_COVERAGE_THRESHOLD: u8 = 128;

// --- Font ID ↔ name lookup -----------------------------------------------

pub static FONT_NAMES: &[(i16, &str)] = &[
    (0, "Chicago"),
    (1, "Application"),
    (2, "New York"),
    (3, "Geneva"),
    (4, "Monaco"),
    (5, "Venice"),
    (6, "London"),
    (7, "Athens"),
    (8, "San Francisco"),
    (9, "Toronto"),
    (11, "Cairo"),
    (12, "Los Angeles"),
    (16, "Palatino"),
    (20, "Times"),
    (21, "Helvetica"),
    (22, "Courier"),
    (23, "Symbol"),
    (24, "Mobile"),
];

pub fn font_name_for_id(font_id: i16) -> Option<&'static str> {
    FONT_NAMES
        .iter()
        .find(|(id, _)| *id == font_id)
        .map(|(_, name)| *name)
}

pub fn font_id_for_name(name: &str) -> Option<i16> {
    let needle = name.trim();
    FONT_NAMES
        .iter()
        .find(|(_, n)| n.eq_ignore_ascii_case(needle))
        .map(|(id, _)| *id)
}

#[derive(Default)]
struct OverrideCache {
    env_dir: Option<OsString>,
    faces: HashMap<(i16, i16), &'static FontFace>,
}

/// Optional runtime override map populated from `SYSTEMLESS_ORIGINAL_FONTS_DIR`.
/// Entries here win over the built-in systemless catalogue — the opt-in hook for
/// substituting authentic Mac bitmap glyphs at runtime without committing
/// Apple-copyrighted data into this repo.
///
/// The directory is resolved on the first lookup and reused after that.
/// `get_font_face` sits on the per-glyph drawing path, where consulting
/// the environment means walking it and allocating: a CPU profile of EV
/// Override attributed several percent of the process to exactly that,
/// across the handful of lookups each character performs.
///
/// Embedders and test harnesses that set or clear
/// `SYSTEMLESS_ORIGINAL_FONTS_DIR` after other code has already queried
/// font metrics must call [`refresh_font_overrides`] to pick the change
/// up.
static OVERRIDES: LazyLock<Mutex<OverrideCache>> =
    LazyLock::new(|| Mutex::new(OverrideCache::default()));

pub fn get_font_face(font_id: i16, size: i16) -> Option<&'static FontFace> {
    let size = if size == 0 { 12 } else { size };
    if let Some(face) = get_override_font_face(font_id, size) {
        return Some(face);
    }
    if let Some(face) = RESOURCE_FACES
        .lock()
        .expect("resource font cache poisoned")
        .get(&(font_id, size))
        .copied()
    {
        return Some(face);
    }
    if let Some(face) = rasterize_resource_outline_face(font_id, size) {
        return Some(face);
    }
    outline::face(font_id, size).map(|(face, _)| face)
}

#[cfg(test)]
fn get_font_face_with_overrides(
    overrides: &HashMap<(i16, i16), &'static FontFace>,
    font_id: i16,
    size: i16,
) -> Option<&'static FontFace> {
    let size = if size == 0 { 12 } else { size };
    if let Some(face) = overrides.get(&(font_id, size)) {
        return Some(*face);
    }
    outline::face(font_id, size).map(|(face, _)| face)
}

/// Re-read `SYSTEMLESS_ORIGINAL_FONTS_DIR` on the next font lookup.
///
/// Call this after setting or clearing the variable at runtime. Lookups
/// otherwise reuse the directory resolved by the first one, because
/// consulting the environment per glyph is measurably expensive.
pub fn refresh_font_overrides() {
    OVERRIDE_RESOLVED.store(false, Ordering::Release);
}

/// Whether the override directory has been resolved, and whether it
/// yielded any faces. Both are read per glyph, so they are plain atomics;
/// when no overrides exist -- the usual case, since the variable is an
/// opt-in hook -- lookups take neither the environment nor the mutex.
static OVERRIDE_RESOLVED: AtomicBool = AtomicBool::new(false);
static OVERRIDE_ANY: AtomicBool = AtomicBool::new(false);

fn get_override_font_face(font_id: i16, size: i16) -> Option<&'static FontFace> {
    if OVERRIDE_RESOLVED.load(Ordering::Acquire) {
        if !OVERRIDE_ANY.load(Ordering::Acquire) {
            return None;
        }
        let cache = OVERRIDES.lock().expect("font override cache poisoned");
        return cache.faces.get(&(font_id, size)).copied();
    }
    let env_dir = std::env::var_os("SYSTEMLESS_ORIGINAL_FONTS_DIR");
    let mut cache = OVERRIDES.lock().expect("font override cache poisoned");
    if cache.env_dir != env_dir {
        cache.faces = env_dir
            .as_ref()
            .map(|dir| override_format::load_directory(Path::new(dir)))
            .unwrap_or_default();
        cache.env_dir = env_dir;
    }
    OVERRIDE_ANY.store(!cache.faces.is_empty(), Ordering::Release);
    OVERRIDE_RESOLVED.store(true, Ordering::Release);
    cache.faces.get(&(font_id, size)).copied()
}

fn closest_font_face(font_id: i16, size: i16) -> Option<&'static FontFace> {
    RESOURCE_FACES
        .lock()
        .expect("resource font cache poisoned")
        .values()
        .copied()
        .filter(|face| face.font_id == font_id)
        .min_by_key(|face| {
            (
                (i32::from(face.size) - i32::from(size)).unsigned_abs(),
                std::cmp::Reverse(face.size),
            )
        })
}

pub fn get_font_face_or_default(font_id: i16, size: i16) -> &'static FontFace {
    let size = if size == 0 { 12 } else { size.max(1) };
    if let Some(face) = get_font_face(font_id, size) {
        return face;
    }
    if let Some(face) = closest_font_face(font_id, size) {
        return face;
    }
    // Rasterize bounded strikes; extreme sizes use the same outline face with
    // the scale ratio below rather than falling back to handwritten bitmaps.
    outline::face(font_id, size.min(96))
        .or_else(|| outline::face(FONT_APPLICATION, size.min(96)))
        .expect("bundled font must be valid")
        .0
}

pub fn get_font_face_scaled(font_id: i16, size: i16) -> (&'static FontFace, i16) {
    let face = get_font_face_or_default(font_id, size);
    (face, (size.max(1) / face.size.max(1)).max(1))
}

pub fn get_font_face_scale_ratio(font_id: i16, size: i16) -> (&'static FontFace, i32, i32) {
    let requested_size = if size == 0 { 12 } else { size }.max(1);
    let (face, _) = get_font_face_scaled(font_id, requested_size);
    (face, i32::from(requested_size), i32::from(face.size.max(1)))
}

pub fn get_macroman_glyph(
    font_id: i16,
    size: i16,
    mac_code: u8,
) -> Option<(&'static Glyph, &'static [u8])> {
    let resolved = get_font_face_or_default(font_id, size);
    let (font_id, size) = (resolved.font_id, resolved.size);
    if let Some(face) = RESOURCE_MACROMAN_FACES
        .lock()
        .expect("resource Mac Roman font cache poisoned")
        .get(&(font_id, size))
        .copied()
    {
        if let Some(hit) = face.glyphs.iter().find(|entry| entry.mac_code == mac_code) {
            return Some((&hit.glyph, face.data));
        }
    }
    if get_override_font_face(font_id, size).is_none()
        && !RESOURCE_FACES
            .lock()
            .expect("resource font cache poisoned")
            .contains_key(&(font_id, size))
    {
        if let Some((_, face)) = outline::face(font_id, size) {
            return face
                .glyphs
                .iter()
                .find(|entry| entry.mac_code == mac_code)
                .map(|entry| (&entry.glyph, face.data));
        }
    }
    None
}

/// No separate bitmap italic strikes are bundled. QuickDraw callers synthesize
/// the requested style from the selected font.
pub fn get_italic_glyph(
    _font_id: i16,
    _size: i16,
    _ch: char,
) -> Option<(&'static Glyph, &'static [u8])> {
    None
}

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

    pub(super) fn minimal_nfnt() -> Vec<u8> {
        // One encoded character plus the missing-character glyph. The bitmap
        // is one row by one word; location and offset/width tables follow it.
        let mut bytes = vec![0u8; 38];
        bytes[2..4].copy_from_slice(&32u16.to_be_bytes()); // firstChar
        bytes[4..6].copy_from_slice(&32u16.to_be_bytes()); // lastChar
        bytes[6..8].copy_from_slice(&1u16.to_be_bytes()); // widMax
        bytes[14..16].copy_from_slice(&1u16.to_be_bytes()); // fRectHeight
        bytes[16..18].copy_from_slice(&9u16.to_be_bytes()); // owTLoc: 16 + 9*2 = 34
        bytes[18..20].copy_from_slice(&1u16.to_be_bytes()); // ascent
        bytes[24..26].copy_from_slice(&1u16.to_be_bytes()); // rowWords
        bytes[26] = 0xC0; // one pixel for each of the two glyphs
        bytes[28..30].copy_from_slice(&0u16.to_be_bytes());
        bytes[30..32].copy_from_slice(&1u16.to_be_bytes());
        bytes[32..34].copy_from_slice(&2u16.to_be_bytes());
        bytes[34..36].copy_from_slice(&1u16.to_be_bytes()); // offset 0, advance 1
        bytes[36..38].copy_from_slice(&1u16.to_be_bytes());
        bytes
    }

    #[test]
    fn fond_associations_map_arbitrary_bitmap_resource_ids() {
        let mut fond = vec![0u8; 66];
        fond[2..4].copy_from_slice(&1234u16.to_be_bytes()); // redundant ffFamID
        fond[52..54].copy_from_slice(&1u16.to_be_bytes()); // two entries minus one
        fond[54..56].copy_from_slice(&12u16.to_be_bytes());
        fond[56..58].copy_from_slice(&0u16.to_be_bytes());
        fond[58..60].copy_from_slice(&42u16.to_be_bytes());
        fond[60..62].copy_from_slice(&18u16.to_be_bytes());
        fond[62..64].copy_from_slice(&1u16.to_be_bytes());
        fond[64..66].copy_from_slice(&(-32000i16 as u16).to_be_bytes());

        assert_eq!(
            parse_fond_associations(16000, &fond),
            Some(vec![
                FondAssociation {
                    family_id: 16000,
                    size: 12,
                    style: 0,
                    font_resource_id: 42,
                },
                FondAssociation {
                    family_id: 16000,
                    size: 18,
                    style: 1,
                    font_resource_id: -32000,
                },
            ])
        );
    }

    #[test]
    fn resource_strike_can_register_under_fond_family_and_size() {
        let family_id = 30001;
        assert!(register_resource_font_strike_for_family(
            family_id,
            17,
            &minimal_nfnt(),
        ));
        let face = get_font_face(family_id, 17).expect("FOND-associated face should resolve");
        assert_eq!(face.font_id, family_id);
        assert_eq!(face.size, 17);
        assert_eq!(face.metrics.ascent, 1);
    }

    #[test]
    fn resource_font_can_draw_del_character() {
        let mut font = minimal_nfnt();
        font[2..4].copy_from_slice(&127u16.to_be_bytes());
        font[4..6].copy_from_slice(&127u16.to_be_bytes());
        assert!(register_resource_font_strike_for_family(30002, 17, &font));
        let (glyph, data) = crate::quickdraw::text::get_glyph(30002, 17, '\u{7f}').unwrap();
        assert_eq!(glyph.width, 1);
        assert_eq!(data[glyph.data_offset], 255);
    }

    fn distinctive_override_blob(font_id: i16, size: i16) -> override_format::Blob {
        let glyphs: Vec<Glyph> = (0..override_format::GLYPH_COUNT)
            .map(|_| Glyph {
                width: 0,
                height: 0,
                advance: 13,
                origin_x: 0,
                origin_y: 0,
                data_offset: 0,
            })
            .collect();
        override_format::Blob {
            font_id,
            size,
            style: override_format::STYLE_PLAIN,
            metrics: FontMetrics {
                ascent: 99,
                descent: 11,
                wid_max: 13,
                leading: 7,
            },
            glyphs,
            data: vec![],
        }
    }

    #[test]
    fn bundled_families_rasterize_requested_sizes() {
        for id in [
            FONT_CHICAGO,
            FONT_GENEVA,
            FONT_MONACO,
            FONT_PALATINO,
            FONT_HELVETICA,
            FONT_COURIER,
        ] {
            for size in [9, 12, 17, 40, 96] {
                let (face, numerator, denominator) = get_font_face_scale_ratio(id, size);
                assert_eq!((face.font_id, face.size), (id, size));
                assert_eq!(numerator, denominator);
                assert_eq!(face.glyphs.len(), 95);
            }
        }
    }

    #[test]
    fn space_has_advance_and_no_ink() {
        // ASCII 0x20 space: must carry a positive advance (otherwise
        // strings collapse) and must render no ink. Some faces encode
        // space as a minimal empty bitmap rather than a strictly
        // zero-sized one, so assert by scanning the data slice rather
        // than the width/height fields.
        let face = get_font_face(FONT_GENEVA, 12).unwrap();
        let space = &face.glyphs[0];
        assert!(space.advance > 0, "space must advance");
        let len = (space.width as usize) * (space.height as usize);
        let data_slice = &face.data[space.data_offset..space.data_offset + len];
        assert!(
            data_slice.iter().all(|&b| b == 0),
            "space must render no ink"
        );
    }

    #[test]
    fn override_directory_entries_win_over_bundled_faces() {
        let dir = std::env::temp_dir().join(format!(
            "systemless-font-override-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        fs::create_dir_all(&dir).unwrap();

        let blob_path = dir.join("chicago_12_plain.bin");
        let mut buf = Vec::new();
        override_format::write_blob(&mut buf, &distinctive_override_blob(FONT_CHICAGO, 12))
            .unwrap();
        fs::write(&blob_path, &buf).unwrap();

        let overrides = override_format::load_directory(&dir);
        let face = get_font_face_with_overrides(&overrides, FONT_CHICAGO, 12)
            .expect("chicago 12 should resolve");
        assert_eq!(face.metrics.ascent, 99, "override should win over bundled");
        assert_eq!(face.metrics.descent, 11);
        assert_eq!(face.glyphs.len(), override_format::GLYPH_COUNT as usize);
        assert!(
            face.glyphs.iter().all(|g| g.advance == 13),
            "all override glyphs carry the fingerprint advance"
        );

        let geneva = get_font_face_with_overrides(&overrides, FONT_GENEVA, 12)
            .expect("bundled geneva 12 still there");
        assert_ne!(
            geneva.metrics.ascent, 99,
            "non-overridden face must keep built-in systemless metrics"
        );

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn explicit_geneva9_override_keeps_its_own_metrics() {
        let mut overrides = HashMap::new();
        let blob = distinctive_override_blob(FONT_GENEVA, 9);
        let face = Box::leak(Box::new(FontFace {
            font_id: blob.font_id,
            size: blob.size,
            metrics: blob.metrics,
            glyphs: Box::leak(blob.glyphs.into_boxed_slice()),
            data: Box::leak(blob.data.into_boxed_slice()),
        }));
        overrides.insert((FONT_GENEVA, 9), &*face);

        let resolved = get_font_face_with_overrides(&overrides, FONT_GENEVA, 9)
            .expect("explicit Geneva 9 override");
        assert!(resolved.glyphs.iter().all(|glyph| glyph.advance == 13));
        assert_eq!(resolved.metrics.wid_max, 13);
        assert!(std::ptr::eq(resolved, face));
    }
}

/// Original bundled outline bytes for Resource-Manager-visible font handles.
pub(crate) fn bundled_font_bytes(font_id: i16) -> Option<&'static [u8]> {
    bundled::bytes(font_id)
}