Use physical Type 1 glyph widths in XeTeX PDF output

This commit is contained in:
gloamxun committed 2026-11-28 06:23:12 +08:00
1 parent 885911485e
commit b22e127e93
11 files changed
+210 -33

No files matched your search

+67
View File
@@ -707,6 +707,73 @@ fn font_mapping_changes_recover_and_update_the_page_that_used_the_resource() {
fs::remove_dir_all(p).unwrap();
}
#[test]
fn xetex_type1_widths_follow_the_encoded_glyph_program() {
use lopdf::{Document, Object, content::Content};
use texrra_tex_core::execution::InputProfile;
let root = directory("xetex-type1-widths");
let mut job = request(&root);
job.input_profile = InputProfile::XeTeX;
job.world.paths.insert(
ResourceKind::Map,
vec![SearchPath::directory(root.clone(), false)],
);
fs::write(
root.join("pdftex.map"),
b"cmmi10 CMMI10 <cmmi10.pfb\ncmr10 CMR10 <cmr10.pfb\n",
)
.unwrap();
fs::write(
root.join("main.tex"),
br"\catcode123=1 \catcode125=2
\font\italic=cmmi10 \font\roman=cmr10
\shipout\hbox{\italic xYvx\kern10pt\roman AVW}
\end",
)
.unwrap();
let result = NativeCompiler::default().compile(job).unwrap();
let pdf = Document::load_mem(result.pdf.as_deref().unwrap()).unwrap();
let width = |name: &[u8], slot: u8| {
let font = pdf
.objects
.values()
.filter_map(|object| object.as_dict().ok())
.find(|font| {
font.get(b"BaseFont")
.and_then(Object::as_name)
.is_ok_and(|base_font| base_font.rsplit(|&byte| byte == b'+').next() == Some(name))
})
.unwrap();
let first = font.get(b"FirstChar").unwrap().as_i64().unwrap();
let widths = pdf.dereference(font.get(b"Widths").unwrap()).unwrap().1;
match widths.as_array().unwrap()[(i64::from(slot) - first) as usize] {
Object::Integer(width) => width as f64,
Object::Real(width) => width,
ref object => panic!("invalid font width {object:?}"),
}
};
assert_eq!(width(b"CMMI10", b'Y'), 580.0);
assert_eq!(width(b"CMMI10", b'Z'), 0.0);
assert_eq!(width(b"CMMI10", b'v'), 484.0);
assert_eq!(width(b"CMMI10", b'x'), 571.0);
assert_eq!(width(b"CMR10", b'W'), 1027.0);
let page = pdf.get_page_content(pdf.get_pages()[&1]).unwrap();
let content = Content::decode(&page).unwrap();
let operators = content
.operations
.iter()
.filter_map(|operation| match operation.operator.as_str() {
operator @ ("BT" | "ET" | "Tf" | "Td" | "TJ") => Some(operator),
_ => None,
})
.collect::<Vec<_>>();
assert_eq!(operators, ["BT", "Tf", "Td", "TJ", "Tf", "Td", "TJ", "ET"]);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn xetex_unclaimed_special_warns_at_shipout_and_retains_the_whatsit() {
use texrra_tex_core::execution::{Event, InputProfile};