Measure XeLaTeX graphics links from device placement

This commit is contained in:
gloamxun committed 2026-11-28 20:38:11 +08:00
1 parent d875b69280
commit d627614af8
12 files changed
+295 -149

No files matched your search

@@ -52,8 +52,8 @@ pub enum PdfGraphics {
number: u32,
options: super::xdv_navigation::FormOptions,
source: crate::execution::TokenOrigin,
#[serde(default)]
imported_available: bool,
#[serde(default, alias = "imported_available")]
available: bool,
},
}
@@ -1,7 +1,7 @@
//! Image selection and placement from the xdvipdfmx device specials.
use super::{
Affine,
xdv_navigation::FormOptions,
xdv_navigation::{FormOptions, XObjectTransform},
xdv_object::{
Dictionary, Position, Value, dictionary, identifier, parse_object_prefix, trim_pdf,
},
@@ -266,7 +266,6 @@ pub(crate) enum ImportGeometry {
impl ImportGeometry {
fn page(geometry: PdfPageGeometry, attributes: &Dictionary) -> Self {
let [left, bottom, right, top] = geometry.bounds;
let rect = |points: [[f64; 2]; 4]| {
let [x, y] = points[0];
points[1..]
@@ -276,45 +275,36 @@ impl ImportGeometry {
})
.map(f64::to_bits)
};
let normal = [[left, bottom], [right, bottom], [right, top], [left, top]]
.map(|[x, y]| geometry.matrix.point(x, y));
let mut sizing = normal;
// The reference driver's stored fitting rectangle also samples the
// unshifted lower edge and the transformed lower-right x-axis point.
sizing[0][1] = bottom;
sizing[1] = geometry.matrix.point(right, 0.0);
let [l, b, r, t] = numeric_attribute(attributes, b"BBox").unwrap_or(geometry.bounds);
let matrix = numeric_attribute(attributes, b"Matrix").map_or(geometry.matrix, Affine);
let painted = [[l, b], [r, b], [r, t], [l, t]].map(|[x, y]| matrix.point(x, y));
Self::Page {
bounds: rect(painted),
fitting: rect(sizing),
fitting: geometry.xdv_fitting_box().map(f64::to_bits),
}
}
pub(crate) fn place(
&self,
number: u32,
options: FormOptions,
available: bool,
) -> Result<crate::pdf_device::forms::FormUse, String> {
pub(crate) fn paint_bounds(&self) -> [f64; 4] {
match *self {
Self::Page { bounds, fitting } => options.place_xobject(
number,
bounds.map(f64::from_bits),
available.then(|| fitting.map(f64::from_bits)),
),
Self::Page { bounds, .. } => bounds.map(f64::from_bits),
Self::Raster { .. } => [0.0, 0.0, 1.0, 1.0],
}
}
pub(crate) fn transform(&self, options: FormOptions) -> Result<XObjectTransform, String> {
match *self {
Self::Page { fitting, .. } => {
Ok(options.xobject_transform(Some(fitting.map(f64::from_bits))))
}
Self::Raster { extent, pixels } => {
let bounds = [0.0, 0.0, 1.0, 1.0];
if !available {
return options.place_xobject(number, bounds, None);
}
let (matrix, clip) =
let (matrix, bbox) =
options.raster_transform(extent.map(f64::from_bits), pixels)?;
let clip = clip
.filter(|_| options.clip_enabled())
.map(|[x, y, width, height]| [x, y, x + width, y + height]);
FormOptions::placed(number, bounds, matrix, clip)
let [x, y, width, height] = bbox.unwrap_or([0.0, 0.0, 1.0, 1.0]);
Ok(XObjectTransform {
matrix,
area: [x, y, x + width, y + height],
clip: bbox.is_some() && options.clip_enabled(),
})
}
}
}
@@ -16,6 +16,7 @@ mod page_dictionaries;
mod page_resources;
mod forms;
pub use forms::FormOptions;
pub(crate) use forms::XObjectTransform;
use forms::Forms;
use page_resources::PageResources;
use page_dictionaries::PageDictionaries;
@@ -132,35 +132,8 @@ impl FormOptions {
Ok(bounds)
}
fn place(
self,
number: u32,
captured: [u64; 4],
capture_y: Option<f64>,
) -> Result<FormUse, String> {
let captured = captured.map(f64::from_bits);
let fitting = capture_y.map(|capture_y| {
let [left, bottom, right, top] = captured;
// The reference Form extent uses the unshifted lower edge and
// the capture-origin-adjusted zero and upper edge.
let y = [-capture_y, top - capture_y];
[
left,
y[0].min(y[1]).min(bottom),
right,
y[0].max(y[1]).max(bottom),
]
});
self.place_xobject(number, captured, fitting)
}
pub(crate) fn place_xobject(
self,
number: u32,
captured: [f64; 4],
fitting: Option<[f64; 4]>,
) -> Result<FormUse, String> {
let user_bbox = self.bbox.map(|values| values.map(f64::from_bits));
pub(crate) fn xobject_transform(self, fitting: Option<[f64; 4]>) -> XObjectTransform {
let user_bbox = self.requested_box();
let transform = self.affine();
// A reserved forward reference has no natural dimensions yet.
let transform = if let Some([left, bottom, right, top]) = fitting {
@@ -190,20 +163,29 @@ impl FormOptions {
} else {
transform
};
let clip = user_bbox.filter(|_| self.clip_enabled());
Self::placed(number, captured, transform, clip)
XObjectTransform {
matrix: transform,
area: user_bbox.or(fitting).unwrap_or([0.0, 0.0, 1.0, 1.0]),
clip: user_bbox.is_some() && self.clip_enabled(),
}
}
}
pub(crate) fn placed(
number: u32,
captured: [f64; 4],
transform: Affine,
clip: Option<[f64; 4]>,
) -> Result<FormUse, String> {
#[derive(Clone, Copy)]
pub(crate) struct XObjectTransform {
pub(crate) matrix: Affine,
pub(crate) area: [f64; 4],
pub(crate) clip: bool,
}
impl XObjectTransform {
pub(crate) fn place(self, number: u32, captured: [f64; 4]) -> Result<FormUse, String> {
let transform = self.matrix;
let matrix = transform.0;
if !matrix.iter().all(|value| value.is_finite()) {
return Err("XDV form placement transform out of range".into());
}
let clip = self.clip.then_some(self.area);
let [left, bottom, right, top] = if let Some([x0, y0, x1, y1]) = clip {
let [left, bottom, right, top] = captured;
let left = left.max(x0.min(x1));
@@ -238,7 +220,9 @@ impl FormOptions {
}),
})
}
}
impl FormOptions {
pub(crate) fn affine(self) -> Affine {
if let Some(matrix) = self.matrix {
return Affine(matrix.map(f64::from_bits));
@@ -297,16 +281,36 @@ pub(super) struct Forms {
struct Active {
number: u32,
bounds: [u64; 4],
capture_origin: [u64; 2],
outer_resources: Dictionary,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct Definition {
bounds: [u64; 4],
#[serde(default)]
capture_origin: [u64; 2],
attributes: Dictionary,
resources: Dictionary,
}
impl Definition {
fn fitting_box(&self) -> [f64; 4] {
let [left, bottom, right, top] = self.bounds.map(f64::from_bits);
// The reference's stored fit range samples the unshifted lower
// edge and the capture-origin-adjusted zero and upper edge.
let [x, y] = self
.capture_origin
.map(|coordinate| -f64::from_bits(coordinate));
[
left + x,
y.min(y + top).min(bottom),
right + x,
y.max(y + top).max(bottom),
]
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct ImportedImage {
geometry: ImportGeometry,
@@ -404,7 +408,7 @@ impl Navigation {
number,
options: special.placement,
source,
imported_available: true,
available: true,
}))
}
@@ -460,6 +464,7 @@ impl Navigation {
kind: &[u8],
input: &[u8],
position: Position,
capture_origin: [f64; 2],
source: TokenOrigin,
limit: usize,
) -> Result<(PdfGraphics, Vec<String>), String> {
@@ -482,6 +487,7 @@ impl Navigation {
form.number,
Definition {
bounds: form.bounds,
capture_origin: form.capture_origin,
attributes,
resources,
},
@@ -507,6 +513,7 @@ impl Navigation {
self.forms.active.push(Active {
number,
bounds,
capture_origin: capture_origin.map(f64::to_bits),
outer_resources,
});
PdfGraphics::XdvFormBegin(number)
@@ -515,7 +522,9 @@ impl Navigation {
number,
options,
source,
imported_available: self.forms.imports.contains_key(&number),
available: self.forms.imports.contains_key(&number)
|| self.forms.definitions.contains_key(&number)
|| self.forms.active.iter().any(|form| form.number == number),
}
};
Ok((command, warnings))
@@ -529,6 +538,26 @@ impl Navigation {
}
}
pub(crate) fn xobject_transform(
&self,
number: u32,
options: FormOptions,
available: bool,
) -> Result<XObjectTransform, String> {
if !available {
return Ok(options.xobject_transform(None));
}
if let Some(id) = self.forms.imports.get(&number) {
return self.forms.images[id].geometry.transform(options);
}
let definition = self
.forms
.definitions
.get(&number)
.ok_or("referenced XDV form is undefined")?;
Ok(options.xobject_transform(Some(definition.fitting_box())))
}
pub(crate) fn split_forms(
&self,
pages: &mut [DisplayList],
@@ -537,7 +566,6 @@ impl Navigation {
return Ok(Vec::new());
}
let mut forms = Vec::new();
let mut capture_positions = BTreeMap::new();
for page in pages {
let page_origin = xdv_page_origin(page.units, page.height, page.magnification);
let mut content_origins = Vec::new();
@@ -571,8 +599,6 @@ impl Navigation {
} => {
let bounds = self.forms.definitions[&number].bounds;
let origin = content_origins.last().copied().unwrap_or(page_origin);
capture_positions
.insert(number, page.units.bp(page.height - y - origin[1]));
let list = DisplayList {
width: page.width,
height: page.height,
@@ -603,7 +629,9 @@ impl Navigation {
if number != expected {
return Err("mismatched XDV form capture".into());
}
let form = FormOptions::default().place(number, geometry.bounds, None)?;
let form = FormOptions::default()
.xobject_transform(None)
.place(number, geometry.bounds.map(f64::from_bits))?;
forms.push(FormDisplay {
form,
resource_number: 0,
@@ -620,28 +648,22 @@ impl Navigation {
number,
options,
source,
imported_available,
available,
},
} => {
let form = if let Some(import) = self.forms.imports.get(&number) {
self.forms.images[import].geometry.place(
number,
options,
imported_available,
)?
let bounds = if let Some(import) = self.forms.imports.get(&number) {
self.forms.images[import].geometry.paint_bounds()
} else {
let bounds = self
.forms
self.forms
.definitions
.get(&number)
.ok_or("referenced XDV form is undefined")?
.bounds;
options.place(
number,
bounds,
capture_positions.get(&number).copied(),
)?
.bounds
.map(f64::from_bits)
};
let form = self
.xobject_transform(number, options, available)?
.place(number, bounds)?;
(
DisplayItem::PdfForm { x, y, form },
Some(PaintSource::token(source)),