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 => {
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 => {
Some(PpcImportAction::Return(0))
}
PpcImportDispatcherTarget::GetSysFont => Some(PpcImportAction::Return(0)),
PpcImportDispatcherTarget::GetAppFont => Some(PpcImportAction::Return(3)),
PpcImportDispatcherTarget::FontToScript => {
let script = crate::text_utils::font_id_to_enabled_script(cpu.gpr[3] as i16);
Some(PpcImportAction::Return(script as u32))
}
PpcImportDispatcherTarget::VisibleLength => {
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))
}
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(¤t_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(¤t_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 => {
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(¤t_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 => {
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))
}
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 => {
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(¤t_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::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 => {
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 => {
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;
}
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;
}
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);
}