From 8bbd62b6320bc291fc0f8d9a3c4e7f20a4cf5e59 Mon Sep 17 00:00:00 2001 From: tsoolgee <291792265+tsoolgee@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:44:29 +0200 Subject: [PATCH 1/2] fix(typeset): shape runs with the font they are tagged with A span asking for another family, or for bold, was shaped with the paragraph's font but tagged with the span's family. The PDF then drew the paragraph font's glyph ids with the span's font and the text came out as garbage (issue #40). The bold case only worked because Frank Ruhl Libre Regular and Bold happen to share glyph order. Such runs are now shaped with a sibling session of the requested face (resolved through FontManager::face_for, so a family without a bold face keeps its regular one) and tagged with the font that shaped them. Refs #40 --- crates/tok-typeset/src/engine.rs | 53 ++++++------- crates/tok-typeset/src/font.rs | 18 ++++- .../tests/span_font_glyphs_test.rs | 79 +++++++++++++++++++ 3 files changed, 121 insertions(+), 29 deletions(-) create mode 100644 crates/tok-typeset/tests/span_font_glyphs_test.rs diff --git a/crates/tok-typeset/src/engine.rs b/crates/tok-typeset/src/engine.rs index ef0054a..3997c72 100644 --- a/crates/tok-typeset/src/engine.rs +++ b/crates/tok-typeset/src/engine.rs @@ -6,7 +6,7 @@ use crate::column_flow::{ ColumnFlowConfig, ColumnGeometry, ColumnParagraphInput, ContinuousColumnPaginator, }; -use crate::font::{FontManager, ShapingSession}; +use crate::font::{FontChoice, FontManager, ShapingSession}; use crate::footnote_paginator::{DynamicFootnotePaginator, FootnotePaginatorConfig}; use crate::gematria::GematriaEngine; use crate::geometry::{ @@ -680,6 +680,8 @@ impl TypesettingEngine { let mut meta: Vec = Vec::with_capacity(est_items); let mut word_start: Option = None; let mut gap_offset: Option = None; + let paragraph_family = session.family_of(FontChoice::Primary).map(str::to_string); + let mut span_sessions: HashMap> = HashMap::new(); let mut push_word = |start: usize, end: usize, gap: Option| { if let Some(gap) = gap { @@ -788,7 +790,27 @@ impl TypesettingEngine { .extend(crate::masoretic::convert_opentype_settings(span_features)); } } - let (mut run, choice) = session.shape_with_features( + // A span asking for another family (or bold) is shaped with that font: the + // glyph ids must belong to the font the run is tagged with, or the PDF draws + // them with the wrong font and the text comes out as garbage. + let requested_family: Option = span_opt + .filter(|sp| sp.format.font_family.is_some() || sp.format.font_weight.is_some()) + .and_then(|sp| { + let fam = sp + .format + .font_family + .as_deref() + .or(base_style.map(|s| s.font_family.as_str())) + .or(paragraph_family.as_deref())?; + Some(session.face_for(fam, run_weight)) + }); + let shaper: &ShapingSession<'_> = match requested_family { + Some(fam) if Some(&fam) != paragraph_family.as_ref() => span_sessions + .entry(fam) + .or_insert_with_key(|fam| session.sibling(fam)), + _ => session, + }; + let (mut run, choice) = shaper.shape_with_features( run_text, run_font_size, level % 2 == 1, @@ -798,32 +820,7 @@ impl TypesettingEngine { g.cluster += run_start as u32; g.y_offset += baseline_shift; } - let resolved_family_owned: Option = if let Some(sp) = span_opt { - let is_bold_span = sp.format.font_weight.is_some_and(|w| w >= 600); - if let Some(ref fam) = sp.format.font_family { - if is_bold_span - && !fam.ends_with(" Bold") - && (fam == "Frank Ruhl Libre" || fam == "David Libre") - { - Some(format!("{} Bold", fam)) - } else { - Some(fam.clone()) - } - } else if let Some(base_fam) = session.family_of(choice) { - if is_bold_span - && !base_fam.ends_with(" Bold") - && (base_fam == "Frank Ruhl Libre" || base_fam == "David Libre") - { - Some(format!("{} Bold", base_fam)) - } else { - Some(base_fam.to_string()) - } - } else { - None - } - } else { - session.family_of(choice).map(ToString::to_string) - }; + let resolved_family_owned = shaper.family_of(choice).map(ToString::to_string); items.push(LayoutItem::Box { width: run.total_width_pt, text: text[run_start..run_end].to_string(), diff --git a/crates/tok-typeset/src/font.rs b/crates/tok-typeset/src/font.rs index 940bd65..a3c33e1 100644 --- a/crates/tok-typeset/src/font.rs +++ b/crates/tok-typeset/src/font.rs @@ -353,9 +353,21 @@ pub enum FontChoice { pub struct ShapingSession<'a> { primary: Option<(&'a FontData, Face<'a>)>, fallbacks: Vec<(&'a FontData, Face<'a>)>, + manager: &'a FontManager, } impl<'a> ShapingSession<'a> { + /// A session for another family from the same font manager, for runs whose span + /// asks for a different font than the paragraph (or its bold face). + pub fn sibling(&self, font_family: &str) -> ShapingSession<'a> { + self.manager.session(font_family) + } + + /// See [`FontManager::face_for`]. + pub fn face_for(&self, family: &str, weight: u16) -> String { + self.manager.face_for(family, weight) + } + fn missing_glyphs(face: &Face<'_>, text: &str) -> usize { text.chars() .filter(|c| !c.is_whitespace() && !c.is_control()) @@ -853,7 +865,11 @@ impl FontManager { } } } - ShapingSession { primary, fallbacks } + ShapingSession { + primary, + fallbacks, + manager: self, + } } /// Shapes text with the requested font family, automatically falling back for missing glyphs. diff --git a/crates/tok-typeset/tests/span_font_glyphs_test.rs b/crates/tok-typeset/tests/span_font_glyphs_test.rs new file mode 100644 index 0000000..d282740 --- /dev/null +++ b/crates/tok-typeset/tests/span_font_glyphs_test.rs @@ -0,0 +1,79 @@ +//! A run is drawn with the font it is tagged with (`LineBox::fonts[font_index]`), so its +//! glyph ids must come from that font. Otherwise the PDF shows garbage (issue #40). + +use tok_core::id::FractionalIndex; +use tok_core::model::{DocumentRoot, Flow, FlowType, ParagraphNode, SectionNode}; +use tok_core::styles::CharacterFormat; +use tok_core::{FlowId, TextSpan}; +use tok_typeset::engine::{TypesettingEngine, TypesettingEngineConfig}; + +const TEXT: &str = "וכתב רבינו שכך היא ההלכה"; + +fn typeset_with_span(format: CharacterFormat) -> (TypesettingEngine, Vec<(String, char, u32)>) { + let mut doc = DocumentRoot::new("span fonts"); + doc.sections.clear(); + let mut sec = SectionNode::new("s", "default"); + sec.flows.clear(); + let mut flow = Flow::new(FlowId::new("main"), FlowType::Main); + let mut p = ParagraphNode::new(FractionalIndex::initial(), "body", TEXT); + p.spans.push(TextSpan::new(0, TEXT.chars().count(), format)); + flow.paragraphs.push(p); + sec.flows.push(flow); + doc.sections.push(sec); + + let engine = TypesettingEngine::new(TypesettingEngineConfig::default()); + let pages = engine.typeset_document(&doc); + let mut glyphs = Vec::new(); + for frame in &pages[0].frames { + for line in &frame.lines { + for g in &line.glyphs { + let Some(ch) = g.character.filter(|c| !c.is_whitespace()) else { + continue; + }; + glyphs.push((line.fonts[usize::from(g.font_index)].clone(), ch, g.glyph_id)); + } + } + } + assert!(!glyphs.is_empty()); + (engine, glyphs) +} + +fn assert_ids_belong_to_tagged_font(engine: &TypesettingEngine, glyphs: &[(String, char, u32)]) { + for (family, ch, gid) in glyphs { + let font = engine.font_manager.get_font(family).unwrap(); + let face = font.shaping_face().unwrap(); + let expected = face.glyph_index(*ch).map(|g| u32::from(g.0)); + assert_eq!(expected, Some(*gid), "{ch} tagged {family} has a glyph id of another font"); + } +} + +#[test] +fn span_in_another_family_is_shaped_with_that_family() { + let (engine, glyphs) = typeset_with_span(CharacterFormat { + font_family: Some("David Libre".to_string()), + ..Default::default() + }); + assert!(glyphs.iter().all(|(f, _, _)| f == "David Libre")); + assert_ids_belong_to_tagged_font(&engine, &glyphs); +} + +#[test] +fn bold_span_is_shaped_with_the_bold_face() { + let (engine, glyphs) = typeset_with_span(CharacterFormat { + font_weight: Some(700), + ..Default::default() + }); + assert!(glyphs.iter().all(|(f, _, _)| f == "Frank Ruhl Libre Bold")); + assert_ids_belong_to_tagged_font(&engine, &glyphs); +} + +#[test] +fn bold_span_in_a_family_without_bold_keeps_the_regular_face() { + let (engine, glyphs) = typeset_with_span(CharacterFormat { + font_family: Some("David Libre".to_string()), + font_weight: Some(700), + ..Default::default() + }); + assert!(glyphs.iter().all(|(f, _, _)| f == "David Libre")); + assert_ids_belong_to_tagged_font(&engine, &glyphs); +} From 8013b820103326eaf08b7464651b6b9acc4c56a5 Mon Sep 17 00:00:00 2001 From: Amlaach <259857678+Amlaach@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:07:59 +0200 Subject: [PATCH 2/2] style(typeset): format span_font_glyphs_test with rustfmt --- crates/tok-typeset/tests/span_font_glyphs_test.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/tok-typeset/tests/span_font_glyphs_test.rs b/crates/tok-typeset/tests/span_font_glyphs_test.rs index d282740..27dee30 100644 --- a/crates/tok-typeset/tests/span_font_glyphs_test.rs +++ b/crates/tok-typeset/tests/span_font_glyphs_test.rs @@ -30,7 +30,11 @@ fn typeset_with_span(format: CharacterFormat) -> (TypesettingEngine, Vec<(String let Some(ch) = g.character.filter(|c| !c.is_whitespace()) else { continue; }; - glyphs.push((line.fonts[usize::from(g.font_index)].clone(), ch, g.glyph_id)); + glyphs.push(( + line.fonts[usize::from(g.font_index)].clone(), + ch, + g.glyph_id, + )); } } } @@ -43,7 +47,11 @@ fn assert_ids_belong_to_tagged_font(engine: &TypesettingEngine, glyphs: &[(Strin let font = engine.font_manager.get_font(family).unwrap(); let face = font.shaping_face().unwrap(); let expected = face.glyph_index(*ch).map(|g| u32::from(g.0)); - assert_eq!(expected, Some(*gid), "{ch} tagged {family} has a glyph id of another font"); + assert_eq!( + expected, + Some(*gid), + "{ch} tagged {family} has a glyph id of another font" + ); } }