Fit and clip captured XeLaTeX forms in their device coordinates

This commit is contained in:
gloamxun committed 2026-11-28 19:47:20 +08:00
1 parent c0e2346b05
commit 5ad5c40002
5 files changed
+303 -98

No files matched your search

+29 -13
View File
@@ -735,7 +735,7 @@ fn page_content(
xdv_font_aliases: &BTreeMap<crate::execution::FontId, crate::execution::FontId>,
opentype: &BTreeMap<crate::execution::FontId, Arc<opentype_fonts::EmbeddedFont>>,
font_opacities: &BTreeMap<u8, String>,
xdv_form: bool,
form_origin: Option<[i64; 2]>,
) -> Vec<u8> {
let digits = list.pdf.as_ref().map_or(3, |p| p.file.decimal_digits);
let pt = |n: i64| {
@@ -750,10 +750,10 @@ fn page_content(
let text_origin = xdv_page_origin(list.units, height, list.magnification);
let xdv = list.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv);
let page_origin = if xdv { text_origin } else { [0, 0] };
let page_origin = form_origin.unwrap_or(if xdv { text_origin } else { [0, 0] });
let mut content = PageContent::default();
content.push_str("q\n0 0 0 rg\n");
if list.magnification != 1000 {
if list.magnification != 1000 && form_origin.is_none() {
let ratio = f64::from(list.magnification) / 1000.0;
content.push_str(&format!("{ratio:.3} 0 0 {ratio:.3} 0 0 cm\n"));
}
@@ -779,7 +779,7 @@ fn page_content(
pt(-origin.v_offset)
));
}
if xdv_form {
if form_origin.is_some() {
// Captured XDV content uses device-relative coordinates. Keep the
// logical origin used by glyphs and specials without emitting the
// enclosing sheet's translation into the Form stream.
@@ -1224,11 +1224,27 @@ fn page_content(
DisplayItem::Link { .. } | DisplayItem::PdfTextSpace { .. } => {}
DisplayItem::PdfForm { x, y, form } => {
if let Some(placement) = form.xdv {
let [a,b,c,d,e,f] = placement.matrix.map(f64::from_bits);
let tx = px(*x).parse::<f64>().expect("formatted XDV x") + e;
let ty = py(*y).parse::<f64>().expect("formatted XDV y") + f;
let [a,b,c,d,tx,ty] = [a,b,c,d,tx,ty].map(crate::pdf_image::real);
content.push_str(&format!("q {a} {b} {c} {d} {tx} {ty} cm /Fm{} Do Q\n", form.number));
let [a, b, c, d, e, f] = placement.matrix.map(f64::from_bits);
let round = |value: f64, factor: f64| (value * factor).round() / factor;
let [a, b, c, d] = [a, b, c, d].map(|value| {
crate::pdf_image::real(round(value, 100_000.0))
});
let [origin_x, origin_y] =
xdv_content_scopes.last().map_or(page_origin, |&(_, point)| point);
let tx = round(list.units.bp(*x - origin_x) + e, 1000.0)
+ round(list.units.bp(origin_x - h), 1000.0);
let ty = round(list.units.bp(height - *y - origin_y) + f, 1000.0)
+ round(list.units.bp(origin_y - v), 1000.0);
let [tx, ty] = [tx, ty].map(crate::pdf_image::real);
content.push_str(&format!("q {a} {b} {c} {d} {tx} {ty} cm "));
if let Some(clip) = placement.clip {
let [x0, y0, x1, y1] = clip.map(f64::from_bits);
let [x, y, width, height] = [x0, y0, x1 - x0, y1 - y0].map(|value| {
crate::pdf_image::real(round(value, 1000.0))
});
content.push_str(&format!("n {x} {y} {width} {height} re W n "));
}
content.push_str(&format!("/Fm{} Do Q\n", form.number));
continue;
}
content.push_str(&format!(
@@ -3032,14 +3048,14 @@ fn write_pdf(
let mut xobjects = String::new();
append_xobjects(list, &mut xobjects)?;
let fonts = mapped_font_resources(list);
if form.xdv.is_some() {
if let Some(geometry) = form.xdv {
let mut resources = format!("<< /Font << {fonts} >>");
if !xobjects.is_empty() {
resources.push_str(&format!(" /XObject << {xobjects} >>"));
}
resources.push_str(" >>");
let content = page_content(list, list.height, &BTreeMap::new(), faces, &cm_views,
&fallback_font_ids, PageOrigin::default(), &xdv_font_aliases, &opentype, &BTreeMap::new(), true);
&fallback_font_ids, PageOrigin::default(), &xdv_font_aliases, &opentype, &BTreeMap::new(), Some(geometry.origin));
xdv_forms.push((form, resources, content));
continue;
}
@@ -3071,7 +3087,7 @@ fn write_pdf(
let font_opacities = remapped.as_ref().map_or(&no_font_opacity, |r| &r.font_opacities[slot]);
let content = page_content(
list, list.height, &names, faces, &cm_views,
&fallback_font_ids, PageOrigin::default(), &xdv_font_aliases, &opentype, font_opacities, false,
&fallback_font_ids, PageOrigin::default(), &xdv_font_aliases, &opentype, font_opacities, None,
);
objects[device_objects[&form.form.number] as usize - 1] = objects::stream_bytes(
&entries, &content, list.pdf.as_ref().map_or(0, |pdf| pdf.stream_compression),
@@ -3262,7 +3278,7 @@ fn write_pdf(
&xdv_font_aliases,
&opentype,
font_opacities,
false,
None,
);
let stream = objects::stream("", &content, list.pdf.as_ref().map_or(0, |p| p.stream_compression));
if let Some(id) = content_slot {