PDF: Latin Extended-A letters in an Arabic line's fallback runs use the extended font

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.
This commit is contained in:
FredApps committed 2026-11-26 23:03:43 +07:00
1 parent e1b87cab01
commit bfe8dd24f9
3 files changed
+71 -10

No files matched your search

+42 -7
View File
@@ -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<char> = 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::<f32>();
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));
+2 -1
View File
@@ -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;
}
+27 -2
View File
@@ -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::<String>()
};