Scope XDV PDF content at the driver page origin
This commit is contained in:
1 parent
ab6b1093ec
commit
526ec9b3b0
9 files changed
+176
-46
No files matched your search
@@ -690,7 +690,8 @@ impl Printer<'_> {
|
||||
PdfLiteralMode::Origin => {}
|
||||
PdfLiteralMode::Page => self.literal(" page"),
|
||||
PdfLiteralMode::Direct => self.literal(" direct"),
|
||||
PdfLiteralMode::XdvOrigin | PdfLiteralMode::XdvDirect => {}
|
||||
PdfLiteralMode::XdvOrigin | PdfLiteralMode::XdvDirect
|
||||
| PdfLiteralMode::XdvContent => {}
|
||||
}
|
||||
self.tokens(tokens)?;
|
||||
}
|
||||
|
||||
@@ -155,7 +155,14 @@ impl Executor {
|
||||
{
|
||||
return Ok(Some(graphics));
|
||||
}
|
||||
let body = data.strip_prefix(b"pdf:").map(<[u8]>::trim_ascii_start);
|
||||
let body = data.strip_prefix(b"pdf:").map(xdv_skip_white);
|
||||
if let Some(content) = body.and_then(|body| xdv_operand(body, b"content")) {
|
||||
let bytes = xdv_skip_white(content);
|
||||
return Ok((!bytes.is_empty()).then(|| PdfGraphics::Literal {
|
||||
mode: PdfLiteralMode::XdvContent,
|
||||
bytes: Arc::from(bytes),
|
||||
}));
|
||||
}
|
||||
if let Some(dictionary) = body.and_then(|body| xdv_operand(body, b"docinfo")) {
|
||||
use crate::pdf_device::xdv_document::merge_docinfo;
|
||||
let (entries, warnings) = merge_docinfo(&self.state.pdf_info, dictionary)
|
||||
|
||||
@@ -743,9 +743,12 @@ fn page_content(
|
||||
pt(n)
|
||||
}
|
||||
};
|
||||
use crate::pdf_device::{PdfDriver, PdfGraphics, PdfLiteralMode};
|
||||
use crate::pdf_device::{PdfDriver, PdfGraphics, PdfLiteralMode, xdv_page_origin};
|
||||
use crate::render::DisplayUnits;
|
||||
|
||||
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 mut content = PageContent::default();
|
||||
content.push_str("q\n0 0 0 rg\n");
|
||||
if list.magnification != 1000 {
|
||||
@@ -774,12 +777,11 @@ fn page_content(
|
||||
pt(-origin.v_offset)
|
||||
));
|
||||
}
|
||||
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();
|
||||
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);
|
||||
@@ -795,7 +797,7 @@ fn page_content(
|
||||
&& *operation == crate::pdf_device::text_spaces::SpaceOperation::Explicit
|
||||
{
|
||||
if tex_text.is_none() {
|
||||
content.move_origin([0, 0], list.units, digits);
|
||||
content.move_origin(page_origin, list.units, digits);
|
||||
}
|
||||
native_text::NativeText::explicit_space(
|
||||
&mut tex_text, &mut content, *x, *y, height, digits, dedicated,
|
||||
@@ -816,8 +818,8 @@ fn page_content(
|
||||
};
|
||||
if let Some((x, y, mode, bytes)) = literal {
|
||||
NativeTextRun::flush(&mut native_text, &mut content);
|
||||
let previous_origin = content.origin.position();
|
||||
let special_origin = mode.xdv_origin([x, height - y], height, list.magnification);
|
||||
let previous_origin = content.origin;
|
||||
let special_origin = mode.xdv_origin([x, height - y], page_origin);
|
||||
match mode {
|
||||
PdfLiteralMode::Direct => {
|
||||
native_text::NativeText::break_string(&mut tex_text, &mut content);
|
||||
@@ -828,8 +830,11 @@ fn page_content(
|
||||
content.move_origin([x, height - y], list.units, digits);
|
||||
}
|
||||
}
|
||||
PdfLiteralMode::XdvOrigin | PdfLiteralMode::XdvDirect => {
|
||||
PdfLiteralMode::XdvOrigin | PdfLiteralMode::XdvDirect | PdfLiteralMode::XdvContent => {
|
||||
native_text::NativeText::finish(&mut tex_text, &mut content);
|
||||
if mode == PdfLiteralMode::XdvContent {
|
||||
content.push_str("q\n");
|
||||
}
|
||||
if let Some(point) = special_origin {
|
||||
content.move_origin(point, list.units, digits);
|
||||
}
|
||||
@@ -837,8 +842,11 @@ fn page_content(
|
||||
}
|
||||
content.bytes.extend_from_slice(bytes);
|
||||
content.push(b'\n');
|
||||
if special_origin.is_some() {
|
||||
content.move_origin(previous_origin, list.units, digits);
|
||||
if mode == PdfLiteralMode::XdvContent {
|
||||
content.push_str("Q\n");
|
||||
content.origin = previous_origin;
|
||||
} else if special_origin.is_some() {
|
||||
content.move_origin(previous_origin.position(), list.units, digits);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -854,7 +862,7 @@ fn page_content(
|
||||
{
|
||||
assert!(paint.is_some(), "native glyph program must be resolved");
|
||||
if tex_text.is_none() {
|
||||
content.move_origin([0, 0], list.units, digits);
|
||||
content.move_origin(page_origin, list.units, digits);
|
||||
}
|
||||
native_text::NativeText::glyph(
|
||||
&mut tex_text,
|
||||
@@ -865,7 +873,7 @@ fn page_content(
|
||||
paint.as_ref().unwrap(),
|
||||
*slot,
|
||||
height,
|
||||
xdv.then_some(one_inch),
|
||||
xdv.then_some(text_origin[0]),
|
||||
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()),
|
||||
@@ -881,7 +889,7 @@ fn page_content(
|
||||
} = item
|
||||
{
|
||||
let [h, v] = content.origin.position();
|
||||
if list.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv) {
|
||||
if xdv {
|
||||
native_text::xdv_rule(
|
||||
&mut content, *x - h, height - *y - v - *rule_height, *width, *rule_height, digits,
|
||||
);
|
||||
@@ -923,7 +931,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], xdv, !font_opacities.is_empty(),
|
||||
[text_origin[0] - h, text_origin[1] - v], xdv, !font_opacities.is_empty(),
|
||||
);
|
||||
}
|
||||
DisplayItem::NativeGlyph {
|
||||
@@ -1204,12 +1212,11 @@ fn page_content(
|
||||
// The text runs above are closed without changing the graphics transform.
|
||||
DisplayItem::PdfGraphics { operation: PdfGraphics::TextBoundary, .. } => {}
|
||||
DisplayItem::PdfGraphics { x, y, operation } => {
|
||||
let xdv = list.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv);
|
||||
let device_origin = list.units == DisplayUnits::TeX && !xdv;
|
||||
if device_origin {
|
||||
content.move_origin([*x, height - *y], list.units, digits);
|
||||
} else if xdv {
|
||||
content.move_origin([0, 0], list.units, digits);
|
||||
content.move_origin(page_origin, list.units, digits);
|
||||
}
|
||||
match operation {
|
||||
PdfGraphics::Save => content.push_str("q\n"),
|
||||
@@ -1228,7 +1235,8 @@ fn page_content(
|
||||
let coordinates = if device_origin {
|
||||
[0.0, 0.0]
|
||||
} else if xdv {
|
||||
[list.units.bp(*x), list.units.bp(height - *y)]
|
||||
let [h, v] = content.origin.position();
|
||||
[list.units.bp(*x - h), list.units.bp(height - *y - v)]
|
||||
} else {
|
||||
[*x, height - *y].map(|coordinate| {
|
||||
pt(coordinate).parse::<f64>().expect("formatted PDF dimension")
|
||||
|
||||
@@ -131,7 +131,7 @@ pub mod forms;
|
||||
mod graphics;
|
||||
pub(crate) use color::{ColorResult, PdfColorStacks, ShippedColor};
|
||||
pub use color::{PdfColorProgram, PdfColorStackAction};
|
||||
pub(crate) use graphics::{Affine, DeviceOrigin};
|
||||
pub(crate) use graphics::{Affine, DeviceOrigin, xdv_page_origin};
|
||||
pub use graphics::{PdfGraphics, PdfLiteralMode, PdfMatrix};
|
||||
pub mod catalog;
|
||||
pub mod text_spaces;
|
||||
|
||||
@@ -10,26 +10,30 @@ pub enum PdfLiteralMode {
|
||||
Direct,
|
||||
XdvOrigin,
|
||||
XdvDirect,
|
||||
/// XDV page content relative to the special, inside a local graphics state.
|
||||
XdvContent,
|
||||
}
|
||||
|
||||
impl PdfLiteralMode {
|
||||
pub(crate) fn xdv_origin(
|
||||
self,
|
||||
point: [i64; 2],
|
||||
page_height: i64,
|
||||
magnification: i32,
|
||||
page_origin: [i64; 2],
|
||||
) -> Option<[i64; 2]> {
|
||||
match self {
|
||||
Self::XdvOrigin => Some(point),
|
||||
Self::XdvDirect => {
|
||||
let inch = super::unmagnify(4_736_286, magnification);
|
||||
Some([inch, page_height - inch])
|
||||
}
|
||||
Self::XdvOrigin | Self::XdvContent => Some(point),
|
||||
Self::XdvDirect => Some(page_origin),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The XDV device writes content relative to the one-inch page origin.
|
||||
pub(crate) fn xdv_page_origin(units: DisplayUnits, height: i64, magnification: i32) -> [i64; 2] {
|
||||
let inch = (72_000. / (units.bp(1) * f64::from(magnification))).round() as i64;
|
||||
[inch, height - inch]
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum PdfGraphics {
|
||||
Save,
|
||||
@@ -83,7 +87,7 @@ impl PdfMatrix {
|
||||
|
||||
/// Native device origin in logical scaled units, with the same rounding as
|
||||
/// the emitted PDF translation. The returned operands have \`digits\` decimals.
|
||||
#[derive(Default)]
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub(crate) struct DeviceOrigin([i64; 2]);
|
||||
|
||||
impl DeviceOrigin {
|
||||
|
||||
@@ -41,10 +41,11 @@ impl NativeText {
|
||||
let state = text.get_or_insert_with(|| {
|
||||
out.push_str("BT\n");
|
||||
if let Some(origin) = xdv_origin {
|
||||
let [h, v] = out.origin.position();
|
||||
out.push_str(&format!(
|
||||
"1 0 0 1 {} {} Tm\n",
|
||||
print_sp(origin, digits),
|
||||
print_sp(height - origin, digits),
|
||||
print_sp(origin - h, digits),
|
||||
print_sp(height - origin - v, digits),
|
||||
));
|
||||
}
|
||||
Self {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Project TeX paint through the PDF device and any literal graphics state.
|
||||
use crate::{
|
||||
pdf_device::{
|
||||
Affine, DeviceOrigin, PdfDriver, PdfGraphics, PdfLiteralMode, page_resources::check_pdf_nesting,
|
||||
Affine, DeviceOrigin, PdfDriver, PdfGraphics, PdfLiteralMode,
|
||||
page_resources::check_pdf_nesting, xdv_page_origin,
|
||||
},
|
||||
render::{DisplayItem, DisplayList, DisplayUnits},
|
||||
};
|
||||
@@ -18,9 +19,8 @@ pub(super) struct PdfProjection {
|
||||
graphics: Option<GraphicsState>,
|
||||
stack: Vec<GraphicsState>,
|
||||
units: DisplayUnits,
|
||||
xdv: bool,
|
||||
xdv_origin: Option<[i64; 2]>,
|
||||
height: i64,
|
||||
magnification: i32,
|
||||
digits: u8,
|
||||
units_per_bp: f64,
|
||||
text_active: bool,
|
||||
@@ -44,19 +44,23 @@ impl PdfProjection {
|
||||
matrix: Affine::IDENTITY,
|
||||
modified_text_parameters: false,
|
||||
};
|
||||
Some(Self {
|
||||
let xdv_origin = page.pdf.as_ref()
|
||||
.filter(|pdf| pdf.driver == PdfDriver::Xdv)
|
||||
.map(|_| xdv_page_origin(page.units, page.height, page.magnification));
|
||||
let mut projection = Self {
|
||||
origin: DeviceOrigin::default(),
|
||||
graphics: Some(graphics),
|
||||
stack: vec![graphics], // The page writer begins with q.
|
||||
units: page.units,
|
||||
xdv: page.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv),
|
||||
xdv_origin,
|
||||
height: page.height,
|
||||
magnification: page.magnification,
|
||||
digits: page.pdf.as_ref().map_or(3, |page| page.file.decimal_digits),
|
||||
units_per_bp: 1.0 / page.units.bp(1),
|
||||
text_active: false,
|
||||
modified_text_position: false,
|
||||
})
|
||||
};
|
||||
projection.move_origin(xdv_origin.unwrap_or([0, 0]));
|
||||
Some(projection)
|
||||
}
|
||||
|
||||
fn concatenate(&mut self, matrix: Affine) {
|
||||
@@ -136,8 +140,11 @@ impl PdfProjection {
|
||||
if *mode != PdfLiteralMode::Direct {
|
||||
self.text_active = false;
|
||||
}
|
||||
let previous_origin = self.origin.position();
|
||||
let special_origin = mode.xdv_origin([x, y], self.height, self.magnification);
|
||||
let previous_origin = self.origin;
|
||||
if *mode == PdfLiteralMode::XdvContent {
|
||||
self.save();
|
||||
}
|
||||
let special_origin = mode.xdv_origin([x, y], self.xdv_origin.unwrap_or([0, 0]));
|
||||
if *mode == PdfLiteralMode::Origin {
|
||||
self.move_origin([x, y]);
|
||||
} else if let Some(point) = special_origin {
|
||||
@@ -147,8 +154,11 @@ impl PdfProjection {
|
||||
self.graphics = None;
|
||||
self.stack.clear();
|
||||
}
|
||||
if special_origin.is_some() {
|
||||
self.move_origin(previous_origin);
|
||||
if *mode == PdfLiteralMode::XdvContent {
|
||||
self.restore();
|
||||
self.origin = previous_origin;
|
||||
} else if special_origin.is_some() {
|
||||
self.move_origin(previous_origin.position());
|
||||
}
|
||||
return None;
|
||||
}
|
||||
@@ -156,11 +166,11 @@ impl PdfProjection {
|
||||
if matches!(operation, PdfGraphics::TextBoundary) {
|
||||
return None;
|
||||
}
|
||||
let native = self.units == DisplayUnits::TeX && !self.xdv;
|
||||
let native = self.units == DisplayUnits::TeX && self.xdv_origin.is_none();
|
||||
if native {
|
||||
self.move_origin([x, y]);
|
||||
} else if self.xdv {
|
||||
self.move_origin([0, 0]);
|
||||
} else if let Some(origin) = self.xdv_origin {
|
||||
self.move_origin(origin);
|
||||
}
|
||||
match operation {
|
||||
PdfGraphics::Save => self.save(),
|
||||
@@ -174,7 +184,12 @@ impl PdfProjection {
|
||||
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
|
||||
self.save();
|
||||
}
|
||||
let [x, y] = if native { [0, 0] } else { [x, y] };
|
||||
let [x, y] = if native {
|
||||
[0, 0]
|
||||
} else {
|
||||
let [h, v] = self.origin.position();
|
||||
[x - h, y - v]
|
||||
};
|
||||
self.concatenate(matrix.about(x as f64, y as f64, false));
|
||||
}
|
||||
PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary => {
|
||||
@@ -187,7 +202,7 @@ impl PdfProjection {
|
||||
if *operation == crate::pdf_device::text_spaces::SpaceOperation::Explicit
|
||||
&& self.units == DisplayUnits::TeX && !self.text_active
|
||||
{
|
||||
self.move_origin([0, 0]);
|
||||
self.move_origin(self.xdv_origin.unwrap_or([0, 0]));
|
||||
self.text_active = true;
|
||||
self.modified_text_position = false;
|
||||
}
|
||||
@@ -195,7 +210,7 @@ impl PdfProjection {
|
||||
}
|
||||
DisplayItem::NativeGlyph { .. } if self.units == DisplayUnits::TeX => {
|
||||
if !self.text_active {
|
||||
self.move_origin([0, 0]);
|
||||
self.move_origin(self.xdv_origin.unwrap_or([0, 0]));
|
||||
self.text_active = true;
|
||||
self.modified_text_position = false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user