Match XDV PDF text cursor and Type 1 descriptor metrics

This commit is contained in:
gloamxun committed 2026-11-26 13:49:41 +08:00
1 parent eb0afccc03
commit bd840df538
7 files changed
+144 -19

No files matched your search

+15 -3
View File
@@ -743,6 +743,7 @@ fn page_content(
let mut opentype_text = opentype_fonts::TextState::default();
let bp_per_unit = list.units.bp(1);
let one_inch = (72_000. / (bp_per_unit * f64::from(list.magnification))).round() as i64;
let xdv = list.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv);
for (index, item) in list.items.iter().enumerate() {
if !matches!(item, DisplayItem::OpenTypeGlyph { .. }) {
opentype_text.finish(&mut content);
@@ -822,6 +823,7 @@ fn page_content(
paint.as_ref().unwrap(),
*slot,
height,
xdv.then_some(one_inch),
digits,
list.pdf_space_plan.as_ref().and_then(|plan| plan.before_glyph.get(&index)).copied(),
list.pdf_space_plan.as_ref().and_then(|plan| plan.dedicated.as_ref()),
@@ -879,7 +881,7 @@ fn page_content(
opentype[&font.id].paint(
&mut opentype_text, &mut content, font, *glyph, text,
*size, *x - h, height - *y - v, bp_per_unit,
[one_inch - h, height - one_inch - v], !font_opacities.is_empty(),
[one_inch - h, height - one_inch - v], xdv, !font_opacities.is_empty(),
);
}
DisplayItem::NativeGlyph {
@@ -1678,8 +1680,18 @@ fn write_native_type1_descriptor(
} else {
font.italic_angle.parse::<f64>().unwrap_or(0.).round().to_string()
};
let stem_v = paint.stem_v;
objects.push(format!("<< /Type /FontDescriptor /FontName /{name} /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} /Ascent {ascent} /Descent {descent} /CapHeight {cap} /XHeight {x_height} /StemV {stem_v} /FontFile {program} 0 R >>").into_bytes());
let descriptor_bytes = if let Some(xdv) = paint.xdv_descriptor {
let [left, bottom, right, top] = font.bbox;
let [ascent, descent, cap, stem_v] = xdv.metrics();
let flags = flags | (i32::from(!font.name.contains("Sans")) << 1)
| (i32::from(font.name.contains("Caps")) << 17)
| (i32::from(xdv.force_bold) << 18);
format!("<< /Type /FontDescriptor /FontName /{name} /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} /Ascent {ascent} /Descent {descent} /CapHeight {cap} /StemV {stem_v} /FontFile {program} 0 R >>")
} else {
let stem_v = paint.stem_v;
format!("<< /Type /FontDescriptor /FontName /{name} /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /ItalicAngle {angle} /Ascent {ascent} /Descent {descent} /CapHeight {cap} /XHeight {x_height} /StemV {stem_v} /FontFile {program} 0 R >>")
};
objects.push(descriptor_bytes.into_bytes());
objects.push(objects::stream(
&format!(
"/Length1 {} /Length2 {} /Length3 {}",