PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
XFA: place positioned objects by their anchorType (fixes #1637) (#1639)
Marco Arnulfoa6aa05cd7b13
diff --git a/crates/xfa/src/layout.rs b/crates/xfa/src/layout.rsindex 4d6f0911e..fb9ac5518 100644--- a/crates/xfa/src/layout.rs+++ b/crates/xfa/src/layout.rs@@ -977,9 +977,13 @@ impl Layouter<'_> {let sum: f64 = visible.map(|k| self.size_of(k, avail_w, ctx, depth + 1).0).sum();if sum <= avail_w + 0.05 { sum } else { avail_w }}- Layout::Positioned => {- visible.map(|k| k.common().x.unwrap_or(0.0) + self.size_of(k, avail_w, ctx, depth + 1).0).fold(0.0, f64::max).min(avail_w)- }+ Layout::Positioned => visible+ .map(|k| {+ let (w, h) = self.size_of(k, avail_w, ctx, depth + 1);+ k.common().top_left(w, h).0 + w+ })+ .fold(0.0, f64::max)+ .min(avail_w),Layout::Tb | Layout::Table => visible.map(|k| self.size_of(k, avail_w, ctx, depth + 1).0).fold(0.0, f64::max).min(avail_w),}}@@ -1076,8 +1080,8 @@ impl Layouter<'_> {.filter(|k| k.common().presence.occupies()).map(|k| {let c = k.common();- let (_, h) = self.size_of(k, (inner_w - c.x.unwrap_or(0.0)).max(1.0), ctx, depth + 1);- c.y.unwrap_or(0.0) + h+ let (w, h) = self.size_of(k, (inner_w - c.x.unwrap_or(0.0)).max(1.0), ctx, depth + 1);+ c.top_left(w, h).1 + h}).fold(0.0, f64::max)}@@ -1173,8 +1177,8 @@ impl Layouter<'_> {if !c.presence.occupies() {return Ok(());}- let (x, y) = (c.x.unwrap_or(0.0), c.y.unwrap_or(0.0));- let (w, h) = self.size_of(k, (inner.w - x).max(1.0), ctx, depth + 1);+ let (w, h) = self.size_of(k, (inner.w - c.x.unwrap_or(0.0)).max(1.0), ctx, depth + 1);+ let (x, y) = c.top_left(w, h);self.draw_node(k, Rect::new(inner.x + x, inner.y + y, w, h), ctx, depth + 1, sib)}@@ -1392,7 +1396,8 @@ impl Layouter<'_> {.map(|(i, k)| {let c = k.common();let (w, h) = self.size_of(k, (inner.w - c.x.unwrap_or(0.0)).max(1.0), &ctx, depth + 1);- (i, Rect::new(c.x.unwrap_or(0.0), c.y.unwrap_or(0.0), w, h))+ let (x, y) = c.top_left(w, h);+ (i, Rect::new(x, y, w, h))}).collect(),_ => self.pack(&nodes, inner.w, &ctx, depth).0,diff --git a/crates/xfa/src/model.rs b/crates/xfa/src/model.rsindex 9b4bdc6d3..01a4b9bcf 100644--- a/crates/xfa/src/model.rs+++ b/crates/xfa/src/model.rs@@ -228,6 +228,36 @@ impl VAlign {}}+/// Which point of a positioned object's box its `x` and `y` give (`anchorType`).+#[derive(Clone, Copy, Debug, Default, PartialEq)]+pub struct Anchor {+ /// Fraction of the width left of the point: 0 left, 0.5 centre, 1 right.+ pub h: f64,+ /// Fraction of the height above the point: 0 top, 0.5 middle, 1 bottom.+ pub v: f64,+}++impl Anchor {+ pub fn parse(s: Option<&str>) -> Anchor {+ let s = s.map(str::trim).unwrap_or("");+ let v = if s.starts_with("middle") {+ 0.5+ } else if s.starts_with("bottom") {+ 1.0+ } else {+ 0.0+ };+ let h = if s.ends_with("Center") {+ 0.5+ } else if s.ends_with("Right") {+ 1.0+ } else {+ 0.0+ };+ Anchor { h, v }+ }+}+/// Paragraph properties as written on an element; `None` inherits.#[derive(Clone, Debug, Default, PartialEq)]pub struct ParaSpec {@@ -276,6 +306,7 @@ pub struct Common {pub id: Option<String>,pub x: Option<f64>,pub y: Option<f64>,+ pub anchor: Anchor,pub w: Option<f64>,pub h: Option<f64>,pub min_w: Option<f64>,@@ -300,6 +331,13 @@ pub enum Node {ExclGroup(Box<ExclGroup>),}+impl Common {+ /// The top-left corner of a positioned box of `w` by `h`, from `x`, `y` and the anchor.+ pub fn top_left(&self, w: f64, h: f64) -> (f64, f64) {+ (self.x.unwrap_or(0.0) - w * self.anchor.h, self.y.unwrap_or(0.0) - h * self.anchor.v)+ }+}+impl Node {pub fn common(&self) -> &Common {match self {diff --git a/crates/xfa/src/parse.rs b/crates/xfa/src/parse.rsindex 2de0bb839..f6c69789b 100644--- a/crates/xfa/src/parse.rs+++ b/crates/xfa/src/parse.rs@@ -171,6 +171,7 @@ fn parse_common(n: XmlNode) -> Common {id: n.attribute("id").map(str::to_string),x: attr_measure(n, "x"),y: attr_measure(n, "y"),+ anchor: Anchor::parse(n.attribute("anchorType")),w: attr_measure(n, "w"),h: attr_measure(n, "h"),min_w: attr_measure(n, "minW"),diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rsindex 4681ba01e..e38be22f8 100644--- a/crates/xfa/src/tests.rs+++ b/crates/xfa/src/tests.rs@@ -1788,3 +1788,51 @@ fn imported_data_is_written_with_the_packets_own_prefix_and_never_leaves_it_unrelet form = layout_xml(&many).unwrap();assert!(form.warnings.iter().any(|w| w.contains("60 rows") && w.contains("first 50")), "{:?}", form.warnings);}++#[test]+fn an_anchor_type_places_the_named_point_of_the_box_at_its_x_and_y() {+ // The page label of IRCC IMM 5532E: x is the centre of the box, so its right edge sits+ // 10.795mm right of x, inside the page, not 21.59mm.+ let xml = r##"<?xml version="1.0" encoding="UTF-8"?>+<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">+<template xmlns="http://www.xfa.org/schema/xfa-template/3.3/">+<subform name="form" layout="tb">+ <pageSet><pageArea name="page"><contentArea x="0.5in" y="1.5in" w="7.5in" h="8in"/><medium short="8.5in" long="11in"/>+ <draw anchorType="topCenter" name="PageOf" x="197.485mm" y="13.97mm" w="21.59mm" h="3.81mm"><value><text>PAGE 1 OF 8</text></value><font size="7pt" typeface="Arial"/><para hAlign="right"/></draw>+ <draw anchorType="bottomRight" name="foot" x="8in" y="10.5in" w="1in" h="0.25in"><value><text>Footer</text></value><font size="8pt" typeface="Arial"/></draw>+ <field anchorType="topRight" name="ref" x="8in" y="1in" w="2in" h="0.3in"><ui><textEdit/></ui></field>+ </pageArea></pageSet>+ <draw name="body" w="7.5in" h="0.3in"><value><text>Body</text></value></draw>+</subform>+</template>+</xdp:xdp>+"##;+ let form = layout_xml(xml).unwrap();+ let page = &form.pages[0];+ let span = |t: &str| {+ page.items+ .iter()+ .find_map(|i| {+ if let Item::Text(s) = i+ && s.text == t+ {+ Some(s.clone())+ } else {+ None+ }+ })+ .unwrap()+ };+ let label = span("PAGE 1 OF 8");+ let right = (197.485 + 21.59 / 2.0) * 72.0 / 25.4;+ assert!(+ (label.x + label.face.width(&label.text) - right).abs() < 0.5,+ "label ends at {}, box at {right}",+ label.x + label.face.width(&label.text)+ );+ let foot = span("Footer");+ assert!((foot.x - 7.0 * 72.0).abs() < 0.01, "footer at x {}", foot.x);+ assert!(foot.baseline > 10.25 * 72.0 && foot.baseline < 10.5 * 72.0, "footer baseline {}", foot.baseline);+ let field = widgets(page).into_iter().find(|w| w.name == "ref").unwrap();+ assert!((field.rect.x - 6.0 * 72.0).abs() < 0.01 && (field.rect.y - 72.0).abs() < 0.01, "{:?}", field.rect);+}