Add per-face PDF font resources
This commit is contained in:
1 parent
03025de783
commit
d40595c7e5
5 files changed
+91
-9
No files matched your search
@@ -16,6 +16,7 @@ fn main() {
|
||||
y: 100 * 65_536,
|
||||
size: 12 * 65_536,
|
||||
text: "route-D XObject".to_string(),
|
||||
font: texrra_tex_core::render::FontFace::Roman,
|
||||
});
|
||||
list.push(DisplayItem::Rule {
|
||||
x: 72 * 65_536,
|
||||
|
||||
@@ -1598,6 +1598,23 @@ mod tests {
|
||||
assert!(text.contains("Tj"));
|
||||
assert!(text.trim_end().ends_with("%%EOF"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_writer_uses_a_bold_font_resource() {
|
||||
let mut list = crate::render::DisplayList::default();
|
||||
list.push(crate::render::DisplayItem::Text {
|
||||
x: 10,
|
||||
y: 20,
|
||||
size: 10 * 65_536,
|
||||
text: "bold".to_owned(),
|
||||
font: crate::render::FontFace::Bold,
|
||||
});
|
||||
let pdf = crate::pdf::display_list_to_pdf(&list, 400 * 65_536);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /Times-Bold"));
|
||||
assert!(text.contains("/F2 4 0 R"));
|
||||
assert!(text.contains("BT /F2"));
|
||||
}
|
||||
#[test]
|
||||
fn math_display_list_paints_symbols_and_fraction_rule() {
|
||||
let atom = crate::math::parse_math("x + \\frac{a}{b}");
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::render::{DisplayItem, DisplayList};
|
||||
use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
|
||||
fn escape(text: &str) -> String {
|
||||
let mut out = String::new();
|
||||
@@ -25,13 +25,40 @@ fn pt(value: i64) -> String {
|
||||
format!("{:.3}", value as f64 / 65_536.0)
|
||||
}
|
||||
|
||||
fn font_base(font: FontFace) -> &'static str {
|
||||
match font {
|
||||
FontFace::Roman => "Times-Roman",
|
||||
FontFace::Bold => "Times-Bold",
|
||||
FontFace::Italic => "Times-Italic",
|
||||
FontFace::BoldItalic => "Times-BoldItalic",
|
||||
FontFace::Mono => "Courier",
|
||||
}
|
||||
}
|
||||
|
||||
fn font_resource(font: FontFace) -> &'static str {
|
||||
match font {
|
||||
FontFace::Roman => "F1",
|
||||
FontFace::Bold => "F2",
|
||||
FontFace::Italic => "F3",
|
||||
FontFace::BoldItalic => "F4",
|
||||
FontFace::Mono => "F5",
|
||||
}
|
||||
}
|
||||
|
||||
fn page_content(list: &DisplayList, height: i64, image_names: &BTreeMap<usize, String>) -> String {
|
||||
let mut content = String::from("q\n0 0 0 rg\n");
|
||||
for item in &list.items {
|
||||
match item {
|
||||
DisplayItem::Text { x, y, size, text } => {
|
||||
DisplayItem::Text {
|
||||
x,
|
||||
y,
|
||||
size,
|
||||
text,
|
||||
font,
|
||||
} => {
|
||||
content.push_str(&format!(
|
||||
"BT /F1 {} Tf 1 0 0 1 {} {} Tm ({}) Tj ET\n",
|
||||
"BT /{} {} Tf 1 0 0 1 {} {} Tm ({}) Tj ET\n",
|
||||
font_resource(*font),
|
||||
pt(*size),
|
||||
pt(*x),
|
||||
pt(height - *y),
|
||||
@@ -93,7 +120,21 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
|
||||
let mut objects: Vec<Vec<u8>> = Vec::new();
|
||||
objects.push(b"<< /Type /Catalog /Pages 2 0 R >>".to_vec());
|
||||
objects.push(Vec::new()); // pages object filled below
|
||||
objects.push(b"<< /Type /Font /Subtype /Type1 /BaseFont /Times-Roman >>".to_vec());
|
||||
for font in [
|
||||
FontFace::Roman,
|
||||
FontFace::Bold,
|
||||
FontFace::Italic,
|
||||
FontFace::BoldItalic,
|
||||
FontFace::Mono,
|
||||
] {
|
||||
objects.push(
|
||||
format!(
|
||||
"<< /Type /Font /Subtype /Type1 /BaseFont /{} >>",
|
||||
font_base(font)
|
||||
)
|
||||
.into_bytes(),
|
||||
);
|
||||
}
|
||||
|
||||
let mut kids = Vec::new();
|
||||
for list in pages {
|
||||
@@ -113,10 +154,11 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
|
||||
.map(|(_, slot)| format!("/Im{slot} {} 0 R", image_base + *slot))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let fonts = "/F1 3 0 R /F2 4 0 R /F3 5 0 R /F4 6 0 R /F5 7 0 R";
|
||||
let resources = if used.is_empty() {
|
||||
"<< /Font << /F1 3 0 R >> >>".to_string()
|
||||
format!("<< /Font << {fonts} >> >>")
|
||||
} else {
|
||||
format!("<< /Font << /F1 3 0 R >> /XObject << {xobjects} >> >>")
|
||||
format!("<< /Font << {fonts} >> /XObject << {xobjects} >> >>")
|
||||
};
|
||||
let links = list
|
||||
.items
|
||||
|
||||
@@ -10,6 +10,15 @@ use crate::math::{self, MathAtom};
|
||||
use crate::{FloatPlacement, Halign, HorizontalItem, Page, VerticalItem};
|
||||
|
||||
/// One absolute drawing primitive in scaled points.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum FontFace {
|
||||
Roman,
|
||||
Bold,
|
||||
Italic,
|
||||
BoldItalic,
|
||||
Mono,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum DisplayItem {
|
||||
Text {
|
||||
@@ -17,6 +26,7 @@ pub enum DisplayItem {
|
||||
y: i64,
|
||||
size: i64,
|
||||
text: String,
|
||||
font: FontFace,
|
||||
},
|
||||
Rule {
|
||||
x: i64,
|
||||
@@ -112,7 +122,9 @@ impl DisplayList {
|
||||
);
|
||||
for item in &self.items {
|
||||
match item {
|
||||
DisplayItem::Text { x, y, size, text } => {
|
||||
DisplayItem::Text {
|
||||
x, y, size, text, ..
|
||||
} => {
|
||||
let x = *x as f64 / 65_536.0;
|
||||
let y = *y as f64 / 65_536.0;
|
||||
let size = *size as f64 / 65_536.0;
|
||||
@@ -213,6 +225,7 @@ impl Halign {
|
||||
for (index, cell) in row.cells.iter().enumerate() {
|
||||
if let Some((x, _)) = self.cell_box(row_index, index) {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline,
|
||||
size: self.line_height * 5 / 6,
|
||||
@@ -323,6 +336,7 @@ impl Page {
|
||||
HorizontalItem::Box(box_) => {
|
||||
if let Some(character) = box_.character {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline,
|
||||
size,
|
||||
@@ -435,6 +449,7 @@ impl Page {
|
||||
MathAtom::Symbol(character) => {
|
||||
if !character.is_whitespace() {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline,
|
||||
size,
|
||||
@@ -446,6 +461,7 @@ impl Page {
|
||||
let mut cursor = x;
|
||||
for character in text.chars() {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x: cursor,
|
||||
y: baseline,
|
||||
size,
|
||||
@@ -520,6 +536,7 @@ impl Page {
|
||||
MathAtom::Sqrt { radicand, .. } => {
|
||||
let box_ = math::layout_math(radicand, font);
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline,
|
||||
size,
|
||||
@@ -537,6 +554,7 @@ impl Page {
|
||||
let body_box = math::layout_math(body, font);
|
||||
if *left != '.' {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline,
|
||||
size,
|
||||
@@ -546,6 +564,7 @@ impl Page {
|
||||
Self::paint_math(body, font, x + font.width(*left), baseline, size, list);
|
||||
if *right != '.' {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x: x + font.width(*left) + body_box.width,
|
||||
y: baseline,
|
||||
size,
|
||||
@@ -557,6 +576,7 @@ impl Page {
|
||||
let base_box = math::layout_math(base, font);
|
||||
Self::paint_math(base, font, x, baseline, size, list);
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x: x + base_box.width / 2,
|
||||
y: baseline - base_box.height - 65_536,
|
||||
size,
|
||||
@@ -654,6 +674,7 @@ impl Page {
|
||||
MathAtom::BigOperator { name } => {
|
||||
for (index, character) in name.chars().enumerate() {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x: x + index as i64 * font.width(character),
|
||||
y: baseline,
|
||||
size,
|
||||
@@ -663,6 +684,7 @@ impl Page {
|
||||
}
|
||||
MathAtom::SizedDelimiter { delimiter, .. } => {
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
x,
|
||||
y: baseline,
|
||||
size,
|
||||
|
||||
Reference in new issue
Block a user