From bfe8dd24f9e7bcbf88081fc2a092a648f2f3c297 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:03:43 +0700 Subject: [PATCH] PDF: Latin Extended-A letters in an Arabic line's fallback runs use the extended font MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A letter the Arabic face lacks (such as the Esperanto Ĉ) fell back to an Item::Latin run in WinAnsi and printed as ?. Such runs are now Item::Ext, set in the face's Latin Extended-A font, and the page declares that font. --- crates/pdf/src/arabic.rs | 49 ++++++++++++++++++++++++++++++++++------ crates/pdf/src/lib.rs | 3 ++- crates/pdf/src/tests.rs | 29 ++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/crates/pdf/src/arabic.rs b/crates/pdf/src/arabic.rs index 96fd2de38b..d072f64c75 100644 --- a/crates/pdf/src/arabic.rs +++ b/crates/pdf/src/arabic.rs @@ -10,7 +10,7 @@ use skrifa::{ }; use unicode_bidi::BidiInfo; -use crate::{CID_FONT, Error, Font, TextDirection, n, needs_cid, pdf_string, pdf_utf16_string}; +use crate::{CID_FONT, EXT_FONT, Error, FONT_COUNT, Font, Run, TextDirection, n, pdf_ext_string, pdf_string, pdf_utf16_string, run_kind}; include!(concat!(env!("OUT_DIR"), "/arabic_fonts.rs")); @@ -197,9 +197,25 @@ fn has_weight_axis(bytes: &[u8]) -> bool { #[derive(Debug)] pub(crate) enum Item { - Glyph { id: u32, x: f32, y: f32, source: String }, - Latin { text: String, x: f32 }, - Cid { text: String, x: f32 }, + Glyph { + id: u32, + x: f32, + y: f32, + source: String, + }, + Latin { + text: String, + x: f32, + }, + /// Latin Extended-A letters WinAnsiEncoding lacks, set in the face's extended font. + Ext { + text: String, + x: f32, + }, + Cid { + text: String, + x: f32, + }, } pub(crate) struct Layout { @@ -250,10 +266,14 @@ pub(crate) fn layout(font: Font, text: &str, direction: TextDirection) -> Result if !embedded { let part = part.chars().map(|ch| if is_line_break(ch) { ' ' } else { ch }); let part: Vec = if rtl { part.rev().map(mirror_ansi).collect() } else { part.collect() }; - for run in part.chunk_by(|a, b| needs_cid(*a) == needs_cid(*b)) { + for run in part.chunk_by(|a, b| run_kind(*a) == run_kind(*b)) { let (text, x) = (run.iter().collect(), layout.width); layout.width += run.iter().map(|&ch| f32::from(font.char_width(ch))).sum::(); - layout.items.push(if run.first().is_some_and(|&ch| needs_cid(ch)) { Item::Cid { text, x } } else { Item::Latin { text, x } }); + layout.items.push(match run.first().map(|&ch| run_kind(ch)) { + Some(Run::Cid) => Item::Cid { text, x }, + Some(Run::Ext) => Item::Ext { text, x }, + _ => Item::Latin { text, x }, + }); } continue; } @@ -325,7 +345,7 @@ pub(crate) fn vertical_extents(font: Font, text: &str, direction: TextDirection) ascent = ascent.max(y + top); descent = descent.max(-y - bottom); } - Item::Latin { .. } | Item::Cid { .. } => { + Item::Latin { .. } | Item::Ext { .. } | Item::Cid { .. } => { ascent = ascent.max(800.0); descent = descent.max(220.0); } @@ -410,6 +430,8 @@ pub(crate) struct Subset { bytes: usize, /// Some text on the page set a run in the CJK CID font, so the page must declare it. pub(crate) uses_cid: bool, + /// The faces (by [`Font::index`]) whose Latin Extended-A font some text on the page used. + pub(crate) uses_ext: [bool; FONT_COUNT], } struct Glyph { @@ -479,6 +501,19 @@ impl Subset { Item::Latin { text, x } => { let _ = writeln!(out, "/F{} {} Tf 1 0 0 1 {} 0 Tm {} Tj", font.index() + 1, n(size), n(x * size / 1000.0), pdf_string(&text)); } + Item::Ext { text, x } => { + if let Some(used) = self.uses_ext.get_mut(font.index()) { + *used = true; + } + let _ = writeln!( + out, + "/F{} {} Tf 1 0 0 1 {} 0 Tm {} Tj", + EXT_FONT + font.index() + 1, + n(size), + n(x * size / 1000.0), + pdf_ext_string(&text) + ); + } Item::Cid { text, x } => { self.uses_cid = true; let _ = writeln!(out, "/F{} {} Tf 1 0 0 1 {} 0 Tm {} Tj", CID_FONT + 1, n(size), n(x * size / 1000.0), pdf_utf16_string(&text)); diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index 6d87e0a02a..9c819ea055 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -893,7 +893,8 @@ impl Page { return; } }; - for slot in [Some(font.index()), self.arabic.uses_cid.then_some(CID_FONT)].into_iter().flatten() { + let ext = self.arabic.uses_ext.get(font.index()).copied().unwrap_or(false).then_some(EXT_FONT + font.index()); + for slot in [Some(font.index()), self.arabic.uses_cid.then_some(CID_FONT), ext].into_iter().flatten() { if let Some(f) = self.fonts.get_mut(slot) { *f = true; } diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index 780ef04738..d35283de69 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -996,6 +996,31 @@ fn oversized_arabic_text_is_an_error_instead_of_partial_pdf() { assert!(doc.finish().unwrap_err().to_string().contains("128 KiB")); } +#[test] +fn arabic_fallback_sets_latin_extended_a_in_the_extended_font() { + if arabic::layout(Font::Helvetica, "مرحبا", TextDirection::Auto).is_err() && arabic::ARABIC_FONTS.is_empty() { + return; + } + // Noto Sans Arabic draws ő and Ş itself but lacks the Esperanto Ĉ and ĥ, which fall back to the standard face. + let text = "سلام ő Ĉu ĥ"; + let items = arabic::layout(Font::Helvetica, text, TextDirection::Auto).unwrap().items; + // Drawn by the face itself or in the extended font, never in WinAnsi. + assert!( + items.iter().any(|item| matches!(item, arabic::Item::Glyph { source: text, .. } | arabic::Item::Ext { text, .. } if text.contains('Ĉ'))), + "{items:?}" + ); + for item in items { + if let arabic::Item::Latin { text: drawn, .. } = &item { + assert!(!drawn.chars().any(|c| ext_code(c).is_some()), "{drawn:?} is set in WinAnsi, where it would print as ?"); + } + } + let mut subset = arabic::Subset::default(); + let body = subset.text(Font::Helvetica, text, 12.0, TextDirection::Auto).unwrap(); + let ext = format!("/F{} ", EXT_FONT + Font::Helvetica.index() + 1); + assert_eq!(body.contains(&ext), subset.uses_ext[Font::Helvetica.index()], "the page declares the extended font it uses"); + assert!(!body.contains("(?)") && !body.contains("?)"), "no letter falls back to ?: {body}"); +} + #[test] fn arabic_joining_marks_styles_and_rotation() { if arabic::layout(Font::Helvetica, "مرحبا", TextDirection::Auto).is_err() && arabic::ARABIC_FONTS.is_empty() { @@ -1071,7 +1096,7 @@ fn line_breaks_in_arabic_text_draw_as_spaces() { { let layout = arabic::layout(Font::Helvetica, text, direction).unwrap(); for item in &layout.items { - if let arabic::Item::Latin { text: drawn, .. } = item { + if let arabic::Item::Latin { text: drawn, .. } | arabic::Item::Ext { text: drawn, .. } = item { assert!(!drawn.chars().any(|ch| matches!(ch, '\n' | '\r' | '\u{2028}' | '\u{2029}')), "{drawn:?} from {text:?}"); } } @@ -1169,7 +1194,7 @@ fn arabic_explicit_base_changes_visual_order_and_preserves_logical_text() { .into_iter() .map(|item| match item { arabic::Item::Glyph { source, .. } => source, - arabic::Item::Latin { text, .. } | arabic::Item::Cid { text, .. } => text, + arabic::Item::Latin { text, .. } | arabic::Item::Ext { text, .. } | arabic::Item::Cid { text, .. } => text, }) .collect::() };