Emit native PDF literals at scan and shipout boundaries
This commit is contained in:
1 parent
13ab32247d
commit
432c1d2ba7
21 files changed
+264
-61
No files matched your search
@@ -449,7 +449,7 @@ struct NativeTextRun {
|
||||
text: String,
|
||||
}
|
||||
impl NativeTextRun {
|
||||
fn flush(run: &mut Option<Self>, content: &mut String) {
|
||||
fn flush(run: &mut Option<Self>, content: &mut PageContent) {
|
||||
if let Some(run) = run.take() {
|
||||
content.push_str(&run.text);
|
||||
if run.expansion == 0 {
|
||||
@@ -607,6 +607,48 @@ fn write_type0_font(
|
||||
type0_id
|
||||
}
|
||||
|
||||
/// The PDF stream can contain an 8-bit TeX literal. Keep its bytes alongside
|
||||
/// the device origin used by subsequent positioned text, rules, and images.
|
||||
#[derive(Default)]
|
||||
struct PageContent {
|
||||
bytes: Vec<u8>,
|
||||
origin: [i64; 2],
|
||||
}
|
||||
|
||||
impl PageContent {
|
||||
fn push_str(&mut self, text: &str) {
|
||||
self.bytes.extend_from_slice(text.as_bytes());
|
||||
}
|
||||
|
||||
fn push(&mut self, byte: u8) {
|
||||
self.bytes.push(byte);
|
||||
}
|
||||
|
||||
fn move_origin(&mut self, point: [i64; 2], units: crate::render::DisplayUnits, digits: u8) {
|
||||
let delta = [point[0] - self.origin[0], point[1] - self.origin[1]];
|
||||
if delta == [0, 0] {
|
||||
return;
|
||||
}
|
||||
let (scale, precision) = match units {
|
||||
crate::render::DisplayUnits::TeX => (6_578_176, digits + 2),
|
||||
crate::render::DisplayUnits::PostScript => (65_536, digits),
|
||||
};
|
||||
let [(h, h_sp), (v, v_sp)] = delta.map(|value| {
|
||||
crate::pdf_device::divide_scaled(value, scale, precision)
|
||||
});
|
||||
if h == 0 && v == 0 {
|
||||
return;
|
||||
}
|
||||
self.origin[0] += h_sp;
|
||||
self.origin[1] += v_sp;
|
||||
self.push_str(&format!(
|
||||
"1 0 0 1 {} {} cm\n",
|
||||
crate::pdf_device::print_decimal(h, digits),
|
||||
crate::pdf_device::print_decimal(v, digits),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn page_content(
|
||||
list: &DisplayList,
|
||||
height: i64,
|
||||
@@ -615,7 +657,7 @@ fn page_content(
|
||||
cm_codes: &[Option<&BTreeMap<char, u8>>],
|
||||
fallback_ids: &[usize; FACE_COUNT],
|
||||
origin: PageOrigin,
|
||||
) -> String {
|
||||
) -> Vec<u8> {
|
||||
let digits = list.pdf.as_ref().map_or(3, |p| p.file.decimal_digits);
|
||||
let pt = |n: i64| {
|
||||
if list.units == crate::render::DisplayUnits::TeX {
|
||||
@@ -624,7 +666,11 @@ fn page_content(
|
||||
pt(n)
|
||||
}
|
||||
};
|
||||
let mut content = String::from("q\n0 0 0 rg\n");
|
||||
use crate::pdf_device::{PdfGraphics, PdfLiteralMode};
|
||||
use crate::render::DisplayUnits;
|
||||
|
||||
let mut content = PageContent::default();
|
||||
content.push_str("q\n0 0 0 rg\n");
|
||||
if list.magnification != 1000 {
|
||||
let ratio = f64::from(list.magnification) / 1000.0;
|
||||
content.push_str(&format!("{ratio:.3} 0 0 {ratio:.3} 0 0 cm\n"));
|
||||
@@ -649,7 +695,29 @@ fn page_content(
|
||||
let mut native_text: Option<NativeTextRun> = None;
|
||||
let mut tex_text: Option<native_text::NativeText> = None;
|
||||
for item in &list.items {
|
||||
if list.units == crate::render::DisplayUnits::TeX {
|
||||
if let DisplayItem::PdfGraphics {
|
||||
x,
|
||||
y,
|
||||
operation: PdfGraphics::Literal { mode, bytes },
|
||||
} = item
|
||||
{
|
||||
NativeTextRun::flush(&mut native_text, &mut content);
|
||||
match mode {
|
||||
PdfLiteralMode::Direct => {
|
||||
native_text::NativeText::break_string(&mut tex_text, &mut content);
|
||||
}
|
||||
PdfLiteralMode::Page | PdfLiteralMode::Origin => {
|
||||
native_text::NativeText::finish(&mut tex_text, &mut content);
|
||||
if *mode == PdfLiteralMode::Origin {
|
||||
content.move_origin([*x, height - *y], list.units, digits);
|
||||
}
|
||||
}
|
||||
}
|
||||
content.bytes.extend_from_slice(bytes);
|
||||
content.push(b'\n');
|
||||
continue;
|
||||
}
|
||||
if list.units == DisplayUnits::TeX {
|
||||
if let DisplayItem::NativeGlyph {
|
||||
x,
|
||||
y,
|
||||
@@ -660,6 +728,9 @@ fn page_content(
|
||||
} = item
|
||||
{
|
||||
assert!(paint.is_some(), "native glyph program must be resolved");
|
||||
if tex_text.is_none() {
|
||||
content.move_origin([0, 0], list.units, digits);
|
||||
}
|
||||
native_text::NativeText::glyph(
|
||||
&mut tex_text,
|
||||
&mut content,
|
||||
@@ -681,7 +752,10 @@ fn page_content(
|
||||
height: rule_height,
|
||||
} = item
|
||||
{
|
||||
native_text::rule(&mut content, *x, *y, *width, *rule_height, height, digits);
|
||||
let [h, v] = content.origin;
|
||||
native_text::rule(
|
||||
&mut content, *x - h, *y + v, *width, *rule_height, height, digits,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -692,6 +766,9 @@ fn page_content(
|
||||
NativeTextRun::flush(&mut native_text, &mut content);
|
||||
}
|
||||
}
|
||||
let [h, v] = content.origin;
|
||||
let px = |x| pt(x - h);
|
||||
let py = |y| pt(height - y - v);
|
||||
match item {
|
||||
DisplayItem::NativeGlyph {
|
||||
x,
|
||||
@@ -721,13 +798,13 @@ fn page_content(
|
||||
expansion: font.expansion,
|
||||
size: *size,
|
||||
y: *y,
|
||||
cursor: pt(*x).parse().unwrap(),
|
||||
cursor: px(*x).parse().unwrap(),
|
||||
point_size: point_size * f64::from(1000 + font.expansion) / 1000.0,
|
||||
text: format!(
|
||||
"BT /N{} {point_size:.4} Tf {spacing}1 0 0 1 {} {} Tm [",
|
||||
font.id.index(),
|
||||
pt(*x),
|
||||
pt(height - *y)
|
||||
px(*x),
|
||||
py(*y)
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -745,8 +822,8 @@ fn page_content(
|
||||
"BT /M{} {} Tf 1 0 0 1 {} {} Tm <{slot:02X}> Tj ET\n",
|
||||
face.0,
|
||||
pt(*size),
|
||||
pt(*x),
|
||||
pt(height - *y)
|
||||
px(*x),
|
||||
py(*y)
|
||||
));
|
||||
}
|
||||
DisplayItem::Text {
|
||||
@@ -785,8 +862,8 @@ fn page_content(
|
||||
"BT /{} {} Tf 1 0 0 1 {} {} Tm {} Tj ET\n",
|
||||
resource,
|
||||
pt(*size),
|
||||
pt(*x),
|
||||
pt(height - *y),
|
||||
px(*x),
|
||||
py(*y),
|
||||
operand
|
||||
));
|
||||
}
|
||||
@@ -803,8 +880,8 @@ fn page_content(
|
||||
color[0] as f64 / 255.0,
|
||||
color[1] as f64 / 255.0,
|
||||
color[2] as f64 / 255.0,
|
||||
pt(*x),
|
||||
pt(height - *y - *height),
|
||||
px(*x),
|
||||
py(*y + *height),
|
||||
pt(*width),
|
||||
pt(*height)
|
||||
));
|
||||
@@ -817,8 +894,8 @@ fn page_content(
|
||||
} => {
|
||||
content.push_str(&format!(
|
||||
"{} {} {} {} re f\n",
|
||||
pt(*x),
|
||||
pt(height - *y - *rule_height),
|
||||
px(*x),
|
||||
py(*y + *rule_height),
|
||||
pt(*width),
|
||||
pt(*rule_height)
|
||||
));
|
||||
@@ -833,10 +910,10 @@ fn page_content(
|
||||
content.push_str(&format!(
|
||||
"{} w {} {} m {} {} l S\n",
|
||||
pt(*line_width),
|
||||
pt(*x1),
|
||||
pt(height - *y1),
|
||||
pt(*x2),
|
||||
pt(height - *y2)
|
||||
px(*x1),
|
||||
py(*y1),
|
||||
px(*x2),
|
||||
py(*y2)
|
||||
));
|
||||
}
|
||||
DisplayItem::Image {
|
||||
@@ -851,8 +928,8 @@ fn page_content(
|
||||
"q {} 0 0 {} {} {} cm /{name} Do Q\n",
|
||||
pt(*width),
|
||||
pt(*image_height),
|
||||
pt(*x),
|
||||
pt(height - *y - *image_height)
|
||||
px(*x),
|
||||
py(*y + *image_height)
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -892,8 +969,8 @@ fn page_content(
|
||||
let name = image.id;
|
||||
content.push_str(&format!(
|
||||
"q {sx} 0 0 {sy} {} {} cm /PI{name} Do Q\n",
|
||||
pt(*x),
|
||||
pt(height - *y - *image_height)
|
||||
px(*x),
|
||||
py(*y + *image_height)
|
||||
));
|
||||
}
|
||||
DisplayItem::Frame {
|
||||
@@ -910,8 +987,8 @@ fn page_content(
|
||||
color[1] as f64 / 255.0,
|
||||
color[2] as f64 / 255.0,
|
||||
*thickness as f64 / 65_536.0,
|
||||
pt(*x),
|
||||
pt(list.height - *y - *height),
|
||||
px(*x),
|
||||
pt(list.height - *y - *height - v),
|
||||
pt(*width),
|
||||
pt(*height)
|
||||
));
|
||||
@@ -928,33 +1005,42 @@ fn page_content(
|
||||
}
|
||||
DisplayItem::Special(special) => {
|
||||
content.push_str(special);
|
||||
content.push('\n');
|
||||
content.push(b'\n');
|
||||
}
|
||||
DisplayItem::PdfColor(program) => {
|
||||
content.push_str(program.literal());
|
||||
content.push('\n');
|
||||
content.push(b'\n');
|
||||
}
|
||||
DisplayItem::PdfGraphics { x, y, operation } => {
|
||||
use crate::pdf_device::PdfGraphics;
|
||||
let device_origin = list.units == DisplayUnits::TeX;
|
||||
if device_origin {
|
||||
content.move_origin([*x, height - *y], list.units, digits);
|
||||
}
|
||||
match operation {
|
||||
PdfGraphics::Save => content.push_str("q\n"),
|
||||
PdfGraphics::Restore { shift: [dx, dy] } => {
|
||||
content.push_str("Q\n");
|
||||
if *dx != 0 || *dy != 0 {
|
||||
content.push_str(&format!("1 0 0 1 {} {} cm\n", pt(*dx), pt(-*dy)));
|
||||
if !device_origin && (*dx != 0 || *dy != 0) {
|
||||
content.push_str(&format!(
|
||||
"1 0 0 1 {} {} cm\n", pt(*dx), pt(-*dy),
|
||||
));
|
||||
}
|
||||
}
|
||||
PdfGraphics::Matrix(matrix) => {
|
||||
let point = |coordinate| {
|
||||
let displayed = pt(coordinate);
|
||||
displayed.parse::<f64>().expect("formatted PDF dimension")
|
||||
let coordinates = if device_origin {
|
||||
[0.0, 0.0]
|
||||
} else {
|
||||
[*x, height - *y].map(|coordinate| {
|
||||
pt(coordinate).parse::<f64>().expect("formatted PDF dimension")
|
||||
})
|
||||
};
|
||||
let [a, b, c, d, e, f] = matrix
|
||||
.about(point(*x), point(height - *y), false)
|
||||
.about(coordinates[0], coordinates[1], false)
|
||||
.0
|
||||
.map(crate::pdf_image::real);
|
||||
content.push_str(&format!("{a} {b} {c} {d} {e} {f} cm\n"));
|
||||
}
|
||||
PdfGraphics::Literal { .. } => unreachable!("literal handled before text"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -962,7 +1048,7 @@ fn page_content(
|
||||
NativeTextRun::flush(&mut native_text, &mut content);
|
||||
native_text::NativeText::finish(&mut tex_text, &mut content);
|
||||
content.push_str("Q\n");
|
||||
content
|
||||
content.bytes
|
||||
}
|
||||
|
||||
/// Document information written into the PDF Info dictionary.
|
||||
@@ -2181,7 +2267,7 @@ fn write_pdf(
|
||||
);
|
||||
objects.push(objects::stream(
|
||||
"",
|
||||
content.as_bytes(),
|
||||
&content,
|
||||
list.pdf.as_ref().map_or(0, |p| p.stream_compression),
|
||||
));
|
||||
|
||||
|
||||
Reference in new issue
Block a user