systemless 0.117.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
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
//! Typed Font Manager and text-measurement dispatch for PowerPC imports.

use super::*;

pub(super) struct PpcFontDispatchContext<'a> {
    pub(super) binding: &'a PpcImportBinding,
    pub(super) cpu: &'a mut PpcCpu,
    pub(super) memory: &'a mut PpcSectionMem,
    pub(super) toolbox_startup: &'a mut PpcToolboxStartupState,
    pub(super) gworlds: &'a [PpcGWorldRecord],
    pub(super) current_gworld: u32,
    pub(super) quickdraw_text_mode: &'a mut i16,
    pub(super) quickdraw_text_size: &'a mut i16,
    pub(super) quickdraw_fore_color: &'a PpcRgbColor,
    pub(super) quickdraw_fore_indices: &'a HashMap<u32, u8>,
    pub(super) quickdraw_pen_h: &'a mut i16,
    pub(super) quickdraw_pen_v: &'a mut i16,
    pub(super) vfs_resources: &'a mut Vec<PpcVfsResourceRecord>,
    pub(super) process_memory_manager: &'a mut ProcessNativeMemoryManager,
    pub(super) heap_cursor: &'a mut u32,
    pub(super) heap_limit: u32,
    pub(super) last_mem_error: &'a mut i16,
    pub(super) handles: &'a mut Vec<PpcHandleRecord>,
    pub(super) current_resource_refnum: i16,
    pub(super) last_resource_error: &'a mut i16,
}

pub(super) fn dispatch_font_import(context: PpcFontDispatchContext<'_>) -> Option<PpcImportAction> {
    let PpcFontDispatchContext {
        binding,
        cpu,
        memory,
        toolbox_startup,
        gworlds,
        current_gworld,
        quickdraw_text_mode,
        quickdraw_text_size,
        quickdraw_fore_color,
        quickdraw_fore_indices,
        quickdraw_pen_h,
        quickdraw_pen_v,
        vfs_resources,
        process_memory_manager,
        heap_cursor,
        heap_limit,
        last_mem_error,
        handles,
        current_resource_refnum,
        last_resource_error,
    } = context;

    match binding.dispatcher_target {
        PpcImportDispatcherTarget::FMSwapFont => {
            let result = font_swap::swap_font(
                cpu.gpr[3],
                memory,
                process_memory_manager,
                heap_cursor,
                heap_limit,
                last_mem_error,
                handles,
                vfs_resources,
                current_resource_refnum,
                last_resource_error,
            );
            Some(PpcImportAction::Return(result))
        }
        PpcImportDispatcherTarget::SetPreserveGlyph => {
            // Inside Macintosh: Text (1993), 4-62 through 4-63.
            toolbox_startup.preserve_glyph = cpu.gpr[3] != 0;
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::GetPreserveGlyph => {
            Some(PpcImportAction::Return(u32::from(toolbox_startup.preserve_glyph)))
        }
        PpcImportDispatcherTarget::SetOutlinePreferred => {
            toolbox_startup.outline_preferred = cpu.gpr[3] != 0;
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::GetOutlinePreferred => Some(PpcImportAction::Return(u32::from(
            toolbox_startup.outline_preferred,
        ))),
        PpcImportDispatcherTarget::IsMetric => {
            // The active Roman numeric-format resource has metricSys = 0.
            // Inside Macintosh: Operating System Utilities (1994), 4-48.
            Some(PpcImportAction::Return(0))
        }
        // Inside Macintosh: Text (1993), p. 4-53: GetSysFont returns the
        // system font ID. Systemless models the standard systemFont value.
        PpcImportDispatcherTarget::GetSysFont => Some(PpcImportAction::Return(0)),
        // Inside Macintosh: Text (1993), p. 4-54: GetAppFont returns ApFontID.
        // The standard Roman application font is Geneva (family ID 3).
        PpcImportDispatcherTarget::GetAppFont => Some(PpcImportAction::Return(3)),
        PpcImportDispatcherTarget::FontToScript => {
            // FontToScript maps a font family ID to an enabled script.
            // Inside Macintosh: Text (1993), pp. 6-22 to 6-24, 6-82 to 6-83.
            let script = crate::text_utils::font_id_to_enabled_script(cpu.gpr[3] as i16);
            Some(PpcImportAction::Return(script as u32))
        }
        PpcImportDispatcherTarget::VisibleLength => {
            // VisibleLength excludes whitespace at the display end of a
            // Roman, left-to-right style run without changing its contents.
            // Inside Macintosh: Text (1993), pp. 3-36 to 3-38, 3-82.
            let len = (cpu.gpr[4] as i32).max(0) as usize;
            if len == 0 {
                return Some(PpcImportAction::Return(0));
            }
            if !ppc_memory_can_read_bytes(memory, cpu.gpr[3], len as u32) {
                return None;
            }
            let bytes = ppc_memory_read_bytes(memory, cpu.gpr[3], len as u32)?;
            let visible = crate::text_utils::evaluate_visible_length(&bytes) as u32;
            Some(PpcImportAction::Return(visible))
        }
        // The same reference specifies that GetDefFontSize returns
        // SysFontSize, using 12 points when that low-memory value is zero.
        // Systemless currently models the default system font at 12 points.
        PpcImportDispatcherTarget::GetDefFontSize => Some(PpcImportAction::Return(12)),
        PpcImportDispatcherTarget::GetFontName => {
            let font_id = cpu.gpr[3] as u16 as i16;
            let name = font_name_for_id(font_id)
                .map(str::to_owned)
                .or_else(|| ppc_vfs_font_name_for_id(vfs_resources, font_id))
                .unwrap_or_default();
            let _ = ppc_write_pstring_bytes(memory, cpu.gpr[4], name.as_bytes());
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::InitFonts => {
            toolbox_startup.fonts_initialized = true;
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::MeasureText => {
            let count = cpu.gpr[3] as u16 as i16;
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_face = ppc_current_text_style(memory, current_gworld);
            let char_extra = ppc_port_char_extra_packed(memory, current_gworld);
            ppc_measure_text(
                memory,
                count,
                cpu.gpr[4],
                cpu.gpr[5],
                text_font,
                *quickdraw_text_size,
                text_face,
                char_extra,
            );
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::RealFont => {
            let font = cpu.gpr[3] as u16 as i16;
            let size = cpu.gpr[4] as u16 as i16;
            Some(PpcImportAction::Return(u32::from(
                font != FONT_APPLICATION
                    && get_font_face(font, ppc_te_font_lookup_size(size)).is_some(),
            )))
        }
        PpcImportDispatcherTarget::TextWidth => {
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_face = ppc_current_text_style(memory, current_gworld);
            Some(PpcImportAction::Return(ppc_text_width(
                cpu,
                memory,
                text_font,
                *quickdraw_text_size,
                text_face,
                current_gworld,
            )))
        }
        PpcImportDispatcherTarget::TruncString => {
            let font = ppc_current_text_font(memory, current_gworld);
            let style = ppc_current_text_style(memory, current_gworld);
            let char_extra = ppc_port_char_extra_packed(memory, current_gworld);
            Some(PpcImportAction::Return(ppc_i16_result(ppc_trunc_string(
                memory,
                cpu.gpr[4],
                cpu.gpr[3] as u16 as i16,
                cpu.gpr[5] as u16,
                font,
                *quickdraw_text_size,
                style,
                char_extra,
            ))))
        }
        PpcImportDispatcherTarget::StringWidth => {
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_face = ppc_current_text_style(memory, current_gworld);
            let width = ppc_read_pascal_string(memory, cpu.gpr[3])
                .map(|bytes| {
                    (i32::from(ppc_text_width_bytes(
                        text_font,
                        *quickdraw_text_size,
                        text_face,
                        &bytes,
                    )) + ppc_char_extra_width(
                        ppc_port_char_extra_packed(memory, current_gworld),
                        *quickdraw_text_size,
                        &bytes,
                    ))
                    .max(0) as u32
                })
                .unwrap_or(0);
            Some(PpcImportAction::Return(width))
        }
        PpcImportDispatcherTarget::CharWidth => {
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_face = ppc_current_text_style(memory, current_gworld);
            let bytes = [(cpu.gpr[3] & 0xff) as u8];
            Some(PpcImportAction::Return(
                (i32::from(ppc_text_width_bytes(
                    text_font,
                    *quickdraw_text_size,
                    text_face,
                    &bytes,
                )) + ppc_char_extra_width(
                    ppc_port_char_extra_packed(memory, current_gworld),
                    *quickdraw_text_size,
                    &bytes,
                ))
                .max(0) as u32,
            ))
        }
        PpcImportDispatcherTarget::GetFontInfo => {
            let text_font = ppc_current_text_font(memory, current_gworld);
            ppc_get_font_info(memory, cpu.gpr[3], text_font, *quickdraw_text_size);
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::FontMetrics => {
            let text_font = ppc_current_text_font(memory, current_gworld);
            ppc_font_metrics(memory, cpu.gpr[3], text_font, *quickdraw_text_size);
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::DrawChar => {
            let ch = (cpu.gpr[3] & 0xff) as u8;
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_style = ppc_current_text_style(memory, current_gworld);
            let advance = ppc_draw_text_bytes_styled(
                memory,
                gworlds,
                current_gworld,
                (*quickdraw_pen_h, *quickdraw_pen_v),
                text_font,
                *quickdraw_text_size,
                *quickdraw_text_mode,
                *quickdraw_fore_color,
                quickdraw_fore_indices.get(&current_gworld).copied(),
                text_style,
                &[ch],
            );
            *quickdraw_pen_h = (*quickdraw_pen_h).saturating_add(advance);
            ppc_sync_gworld_pen(memory, current_gworld, *quickdraw_pen_h, *quickdraw_pen_v);
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::DrawText => {
            let text_ptr = cpu.gpr[3];
            let first_byte = cpu.gpr[4];
            let byte_count = cpu.gpr[5];
            let mut bytes = Vec::with_capacity(byte_count.min(i16::MAX as u32) as usize);
            for offset in 0..byte_count {
                let Some(addr) = text_ptr
                    .checked_add(first_byte)
                    .and_then(|base| base.checked_add(offset))
                else {
                    break;
                };
                let Some(byte) = memory.read_u8(addr) else {
                    break;
                };
                bytes.push(byte);
            }
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_style = ppc_current_text_style(memory, current_gworld);
            let advance = ppc_draw_text_bytes_styled(
                memory,
                gworlds,
                current_gworld,
                (*quickdraw_pen_h, *quickdraw_pen_v),
                text_font,
                *quickdraw_text_size,
                *quickdraw_text_mode,
                *quickdraw_fore_color,
                quickdraw_fore_indices.get(&current_gworld).copied(),
                text_style,
                &bytes,
            );
            *quickdraw_pen_h = (*quickdraw_pen_h).saturating_add(advance);
            ppc_sync_gworld_pen(memory, current_gworld, *quickdraw_pen_h, *quickdraw_pen_v);
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::StdText => {
            // Inside Macintosh: Imaging With QuickDraw (StdText): the text
            // bottleneck draws byteCount bytes at the pen, scaled by
            // numer/denom. Text is drawn at the vertically scaled size.
            let count = u32::from((cpu.gpr[3] as u16 as i16).max(0) as u16);
            let bytes = ppc_memory_read_bytes(memory, cpu.gpr[4], count).unwrap_or_default();
            let (numer, denom) = (
                (cpu.gpr[5] >> 16) as u16 as i16,
                (cpu.gpr[6] >> 16) as u16 as i16,
            );
            let size = if numer > 0 && denom > 0 && numer != denom {
                let size = i32::from(*quickdraw_text_size).max(1);
                (size * i32::from(numer) / i32::from(denom)).clamp(1, i32::from(i16::MAX)) as i16
            } else {
                *quickdraw_text_size
            };
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_style = ppc_current_text_style(memory, current_gworld);
            let advance = ppc_draw_text_bytes_styled(
                memory,
                gworlds,
                current_gworld,
                (*quickdraw_pen_h, *quickdraw_pen_v),
                text_font,
                size,
                *quickdraw_text_mode,
                *quickdraw_fore_color,
                quickdraw_fore_indices.get(&current_gworld).copied(),
                text_style,
                &bytes,
            );
            *quickdraw_pen_h = (*quickdraw_pen_h).saturating_add(advance);
            ppc_sync_gworld_pen(memory, current_gworld, *quickdraw_pen_h, *quickdraw_pen_v);
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::StdTxMeas => {
            // Inside Macintosh: Imaging With QuickDraw (StdTxMeas): returns
            // the unscaled width of byteCount bytes and fills FontInfo.
            // numer and denom are left as the scaling the caller still has
            // to apply.
            let (count, text, info) = (cpu.gpr[3] as u16 as i16, cpu.gpr[4], cpu.gpr[7]);
            let mut call = cpu.clone();
            call.gpr[3] = text;
            call.gpr[4] = 0;
            call.gpr[5] = u32::from(count.max(0) as u16);
            let text_font = ppc_current_text_font(memory, current_gworld);
            let text_face = ppc_current_text_style(memory, current_gworld);
            let width = ppc_text_width(
                &mut call,
                memory,
                text_font,
                *quickdraw_text_size,
                text_face,
                current_gworld,
            );
            ppc_get_font_info(memory, info, text_font, *quickdraw_text_size);
            Some(PpcImportAction::Return(width))
        }
        // Inside Macintosh: Text (IsOutline, OutlineMetrics): the Font
        // Manager gives applications bitmap strikes for each size, so the
        // current font is never reported as an outline font, and the
        // outline-only metrics call returns fontNotOutlineErr.
        PpcImportDispatcherTarget::IsOutline => Some(PpcImportAction::Return(0)),
        PpcImportDispatcherTarget::OutlineMetrics => {
            const FONT_NOT_OUTLINE_ERR: i16 = -32615;
            Some(PpcImportAction::Return(ppc_i16_result(
                FONT_NOT_OUTLINE_ERR,
            )))
        }
        PpcImportDispatcherTarget::DrawString | PpcImportDispatcherTarget::DrawCString => {
            // drawstring is the C glue for DrawString: it converts the caller's
            // string to Pascal form in place for the call and back afterwards.
            let c_string = binding.dispatcher_target == PpcImportDispatcherTarget::DrawCString;
            if c_string {
                ppc_c2pstr(cpu, memory);
            }
            if let Some(bytes) = ppc_read_pascal_string(memory, cpu.gpr[3]) {
                let text_font = ppc_current_text_font(memory, current_gworld);
                let text_style = ppc_current_text_style(memory, current_gworld);
                if std::env::var_os("SYSTEMLESS_TRACE_FONT_TRAPS").is_some() {
                    eprintln!(
                        "[FONT] PPC DrawString port=${:08X} font={} size={} text={:?}",
                        current_gworld,
                        text_font,
                        *quickdraw_text_size,
                        decode_mac_roman(&bytes),
                    );
                }
                let advance = if let Some(commands) =
                    ppc_open_picture_commands(toolbox_startup, current_gworld)
                {
                    pict::recording_push_long_text(
                        commands,
                        *quickdraw_pen_v,
                        *quickdraw_pen_h,
                        &bytes,
                    );
                    let base =
                        ppc_text_width_bytes(text_font, *quickdraw_text_size, text_style, &bytes);
                    (i32::from(base)
                        + ppc_char_extra_width(
                            ppc_port_char_extra_packed(memory, current_gworld),
                            *quickdraw_text_size,
                            &bytes,
                        ))
                    .clamp(i32::from(i16::MIN), i32::from(i16::MAX)) as i16
                } else {
                    ppc_draw_text_bytes_styled(
                        memory,
                        gworlds,
                        current_gworld,
                        (*quickdraw_pen_h, *quickdraw_pen_v),
                        text_font,
                        *quickdraw_text_size,
                        *quickdraw_text_mode,
                        *quickdraw_fore_color,
                        quickdraw_fore_indices.get(&current_gworld).copied(),
                        text_style,
                        &bytes,
                    )
                };
                *quickdraw_pen_h = (*quickdraw_pen_h).saturating_add(advance);
                ppc_sync_gworld_pen(memory, current_gworld, *quickdraw_pen_h, *quickdraw_pen_v);
            }
            if c_string {
                ppc_p2cstr(cpu, memory);
            }
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::TextFont => {
            if std::env::var_os("SYSTEMLESS_TRACE_FONT_TRAPS").is_some() {
                eprintln!(
                    "[FONT] PPC TextFont port=${:08X} font={}",
                    current_gworld, cpu.gpr[3] as u16 as i16,
                );
            }
            if current_gworld != 0 {
                let _ = memory.write_u16_be(
                    current_gworld + PPC_CGRAF_PORT_TX_FONT_OFFSET,
                    cpu.gpr[3] as u16,
                );
            }
            if let Some(commands) = ppc_open_picture_commands(toolbox_startup, current_gworld) {
                pict::recording_push_word(commands, 0x0003);
                pict::recording_push_word(commands, cpu.gpr[3] as u16);
            }
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::TextFace => {
            if current_gworld != 0 {
                let _ = memory.write_u8(
                    current_gworld + PPC_CGRAF_PORT_TX_FACE_OFFSET,
                    cpu.gpr[3] as u8,
                );
            }
            if let Some(commands) = ppc_open_picture_commands(toolbox_startup, current_gworld) {
                pict::recording_push_word(commands, 0x0004);
                commands.extend_from_slice(&[cpu.gpr[3] as u8, 0]);
            }
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::TextMode => {
            *quickdraw_text_mode = cpu.gpr[3] as u16 as i16;
            ppc_sync_gworld_text(
                memory,
                current_gworld,
                *quickdraw_text_mode,
                *quickdraw_text_size,
            );
            if let Some(commands) = ppc_open_picture_commands(toolbox_startup, current_gworld) {
                pict::recording_push_word(commands, 0x0005);
                pict::recording_push_word(commands, *quickdraw_text_mode as u16);
            }
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::TextSize => {
            if std::env::var_os("SYSTEMLESS_TRACE_FONT_TRAPS").is_some() {
                eprintln!(
                    "[FONT] PPC TextSize port=${:08X} size={}",
                    current_gworld, cpu.gpr[3] as u16 as i16,
                );
            }
            *quickdraw_text_size = cpu.gpr[3] as u16 as i16;
            ppc_sync_gworld_text(
                memory,
                current_gworld,
                *quickdraw_text_mode,
                *quickdraw_text_size,
            );
            if let Some(commands) = ppc_open_picture_commands(toolbox_startup, current_gworld) {
                pict::recording_push_word(commands, 0x000D);
                pict::recording_push_word(commands, *quickdraw_text_size as u16);
            }
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::CharExtra => {
            // Inside Macintosh: Text (1993), p. 3-23: CharExtra stores the
            // Fixed pixel amount as signed 4.12 per txSize in a CGrafPort.
            // A monochrome GrafPort has no chExtra field.
            if current_gworld != 0
                && memory
                    .read_u16_be(current_gworld + 6)
                    .is_some_and(|version| version & 0xc000 == 0xc000)
            {
                let text_size = if *quickdraw_text_size == PPC_QD_TEXT_SIZE_SYSTEM {
                    12
                } else {
                    i32::from((*quickdraw_text_size).max(1))
                };
                let packed = ((cpu.gpr[3] as i32) / (text_size * 16))
                    .clamp(i32::from(i16::MIN), i32::from(i16::MAX))
                    as i16;
                let _ = memory.write_u16_be(
                    current_gworld + PPC_CGRAF_PORT_CH_EXTRA_OFFSET,
                    packed as u16,
                );
            }
            Some(PpcImportAction::ReturnPreserve)
        }
        PpcImportDispatcherTarget::GetFNum => {
            // Inside Macintosh: Text (1993), 4-52: GetFNum maps a Str255 font
            // family name to its ID and returns zero when no family matches.
            let font_id = ppc_read_pstring_bytes(memory, cpu.gpr[3])
                .map(|name| decode_mac_roman(&name))
                .and_then(|name| {
                    font_id_for_name(&name)
                        .or_else(|| ppc_vfs_font_id_for_name(vfs_resources, &name))
                })
                .unwrap_or(0);
            if cpu.gpr[4] != 0 {
                let _ = memory.write_u16_be(cpu.gpr[4], font_id as u16);
            }
            Some(PpcImportAction::ReturnPreserve)
        }
        _ => None,
    }
}

fn ppc_text_width(
    cpu: &mut PpcCpu,
    memory: &mut PpcSectionMem,
    text_font: i16,
    text_size: i16,
    text_face: u8,
    current_gworld: u32,
) -> u32 {
    let text_ptr = cpu.gpr[3];
    let first_byte = cpu.gpr[4];
    let byte_count = cpu.gpr[5];
    let mut bytes = Vec::with_capacity(byte_count.min(i16::MAX as u32) as usize);
    for offset in 0..byte_count {
        let Some(addr) = text_ptr
            .checked_add(first_byte)
            .and_then(|base| base.checked_add(offset))
        else {
            break;
        };
        bytes.push(memory.read_u8(addr).unwrap_or(0));
    }
    (i32::from(ppc_text_width_bytes(
        text_font, text_size, text_face, &bytes,
    )) + ppc_char_extra_width(
        ppc_port_char_extra_packed(memory, current_gworld),
        text_size,
        &bytes,
    ))
    .max(0) as u32
}

fn ppc_get_font_info(memory: &mut PpcSectionMem, info_ptr: u32, text_font: i16, text_size: i16) {
    if info_ptr == 0 || !ppc_memory_can_write_bytes(memory, info_ptr, 8) {
        return;
    }
    // Inside Macintosh Volume I (1985), p. I-173: FontInfo contains the
    // current port font's ascent, descent, maximum advance, and leading.
    let (face, numerator, denominator) = get_font_face_scale_ratio(text_font, text_size);
    let metrics = face.metrics;
    let _ = memory.write_u16_be(
        info_ptr,
        ppc_scale_font_value(i32::from(metrics.ascent), numerator, denominator) as u16,
    );
    let _ = memory.write_u16_be(
        info_ptr + 2,
        ppc_scale_font_value(i32::from(metrics.descent), numerator, denominator) as u16,
    );
    let _ = memory.write_u16_be(
        info_ptr + 4,
        ppc_scale_font_value(i32::from(metrics.wid_max), numerator, denominator) as u16,
    );
    let _ = memory.write_u16_be(
        info_ptr + 6,
        ppc_scale_font_value(i32::from(metrics.leading), numerator, denominator) as u16,
    );
}

fn ppc_font_metrics(memory: &mut PpcSectionMem, metrics_ptr: u32, text_font: i16, text_size: i16) {
    if metrics_ptr == 0 || !ppc_memory_can_write_bytes(memory, metrics_ptr, 20) {
        return;
    }
    // Inside Macintosh: Text (1993), pp. 4-54--4-55: FMetricRec stores
    // ascent, descent, leading, and maximum width as Fixed values, followed
    // by a handle to the global width table. Systemless does not model that
    // table, matching the 68k HLE by returning NIL for its handle.
    let (face, numerator, denominator) = get_font_face_scale_ratio(text_font, text_size);
    let metrics = face.metrics;
    let to_fixed = |value: i16| -> u32 { (i32::from(value) as u32) << 16 };
    let _ = memory.write_u32_be(
        metrics_ptr,
        to_fixed(ppc_scale_font_value(
            i32::from(metrics.ascent),
            numerator,
            denominator,
        )),
    );
    let _ = memory.write_u32_be(
        metrics_ptr + 4,
        to_fixed(ppc_scale_font_value(
            i32::from(metrics.descent),
            numerator,
            denominator,
        )),
    );
    let _ = memory.write_u32_be(
        metrics_ptr + 8,
        to_fixed(ppc_scale_font_value(
            i32::from(metrics.leading),
            numerator,
            denominator,
        )),
    );
    let _ = memory.write_u32_be(
        metrics_ptr + 12,
        to_fixed(ppc_scale_font_value(
            i32::from(metrics.wid_max),
            numerator,
            denominator,
        )),
    );
    let _ = memory.write_u32_be(metrics_ptr + 16, 0);
}