Capture and reuse XeLaTeX PDF form contents
This commit is contained in:
1 parent
009fde04f7
commit
c0e2346b05
16 files changed
+706
-53
No files matched your search
@@ -735,6 +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,
|
||||
) -> Vec<u8> {
|
||||
let digits = list.pdf.as_ref().map_or(3, |p| p.file.decimal_digits);
|
||||
let pt = |n: i64| {
|
||||
@@ -778,7 +779,14 @@ fn page_content(
|
||||
pt(-origin.v_offset)
|
||||
));
|
||||
}
|
||||
content.move_origin(page_origin, list.units, digits);
|
||||
if xdv_form {
|
||||
// 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.
|
||||
content.origin.move_to(page_origin, list.units, digits);
|
||||
} else {
|
||||
content.move_origin(page_origin, list.units, digits);
|
||||
}
|
||||
let mut native_text: Option<NativeTextRun> = None;
|
||||
let mut tex_text: Option<native_text::NativeText> = None;
|
||||
let mut opentype_text = opentype_fonts::TextState::default();
|
||||
@@ -1215,6 +1223,14 @@ 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));
|
||||
continue;
|
||||
}
|
||||
content.push_str(&format!(
|
||||
"q 1 0 0 1 {} {} cm /Fm{} Do Q\n", px(*x), py(*y + form.depth), form.number,
|
||||
));
|
||||
@@ -1275,6 +1291,9 @@ fn page_content(
|
||||
PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary => {
|
||||
unreachable!("handled before graphics transforms")
|
||||
}
|
||||
PdfGraphics::XdvFormBegin(_) | PdfGraphics::XdvFormEnd(_) | PdfGraphics::XdvFormUse { .. } => {
|
||||
unreachable!("XDV form operations were projected before export")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2936,6 +2955,10 @@ fn write_pdf(
|
||||
);
|
||||
(image.id, object)
|
||||
}).collect();
|
||||
for form in forms.iter().filter(|form| form.xdv.is_some()) {
|
||||
objects.push(Vec::new());
|
||||
device_objects.insert(form.form.number, objects.len() as u32);
|
||||
}
|
||||
let append_xobjects = |list: &DisplayList, xobjects: &mut String| -> Result<(), String> {
|
||||
let images = list
|
||||
.items
|
||||
@@ -3002,12 +3025,24 @@ fn write_pdf(
|
||||
}
|
||||
fonts
|
||||
};
|
||||
let mut xdv_forms = Vec::new();
|
||||
for (index, form) in forms.into_iter().enumerate() {
|
||||
let list = &form.list;
|
||||
let slot = pages.len() + index;
|
||||
let mut xobjects = String::new();
|
||||
append_xobjects(list, &mut xobjects)?;
|
||||
let fonts = mapped_font_resources(list);
|
||||
if form.xdv.is_some() {
|
||||
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);
|
||||
xdv_forms.push((form, resources, content));
|
||||
continue;
|
||||
}
|
||||
let digits = list.pdf.as_ref().map_or(3, |pdf| pdf.file.decimal_digits);
|
||||
let width = crate::pdf_device::print_sp(form.form.width, digits);
|
||||
let height = crate::pdf_device::print_sp(form.form.height + form.form.depth, digits);
|
||||
@@ -3036,7 +3071,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,
|
||||
&fallback_font_ids, PageOrigin::default(), &xdv_font_aliases, &opentype, font_opacities, false,
|
||||
);
|
||||
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),
|
||||
@@ -3227,6 +3262,7 @@ fn write_pdf(
|
||||
&xdv_font_aliases,
|
||||
&opentype,
|
||||
font_opacities,
|
||||
false,
|
||||
);
|
||||
let stream = objects::stream("", &content, list.pdf.as_ref().map_or(0, |p| p.stream_compression));
|
||||
if let Some(id) = content_slot {
|
||||
@@ -3565,7 +3601,7 @@ fn write_pdf(
|
||||
destination.collect_references(&mut pending);
|
||||
}
|
||||
}
|
||||
let named_objects = xdv_objects.reserve_objects(pending, &mut objects)?;
|
||||
let named_objects = xdv_objects.reserve_objects(pending, &mut objects, &device_objects)?;
|
||||
let references = crate::pdf_device::xdv_object::References {
|
||||
pages: &navigation.pages,
|
||||
named: &named_objects,
|
||||
@@ -3573,6 +3609,10 @@ fn write_pdf(
|
||||
let compression = pages[0].pdf.as_ref().map_or(0, |pdf| pdf.stream_compression);
|
||||
xdv_objects.write_objects(&references, &mut objects, compression, diagnostics)?;
|
||||
if let Some(xdv) = xdv {
|
||||
for (form, resources, content) in xdv_forms {
|
||||
let id = device_objects[&form.form.number] as usize;
|
||||
objects[id - 1] = xdv.write_form(form, resources.as_bytes(), &content, &references, &mut objects, diagnostics)?;
|
||||
}
|
||||
xdv.write_page_dictionaries(pages, &references, &mut objects)?;
|
||||
xdv.write_page_resources(&references, &mut objects, diagnostics)?;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user