Embed math italic letters as Computer Modern Type1 outlines
This commit is contained in:
1 parent
f7849f39c2
commit
a95cf8092e
10 files changed
+120
-35
No files matched your search
@@ -16,6 +16,8 @@ use crate::cmr10::Cmr10Metrics;
|
||||
use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
use crate::type1::Type1Font;
|
||||
|
||||
const FACE_COUNT: usize = 7;
|
||||
|
||||
fn deflate(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
||||
encoder.write_all(bytes).expect("deflate font stream");
|
||||
@@ -29,7 +31,7 @@ struct PdfFace {
|
||||
font: OnceLock<Font<'static>>,
|
||||
}
|
||||
|
||||
static FULL_FACES: [PdfFace; 6] = [
|
||||
static FULL_FACES: [PdfFace; FACE_COUNT] = [
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Regular.ttf"),
|
||||
base: "LiberationSerif",
|
||||
@@ -66,9 +68,15 @@ static FULL_FACES: [PdfFace; 6] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset/LiberationSerif-Italic.ttf"),
|
||||
base: "LiberationSerif-MathItalic",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
static MIN_FACES: [PdfFace; 6] = [
|
||||
static MIN_FACES: [PdfFace; FACE_COUNT] = [
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Regular.ttf"),
|
||||
base: "LiberationSerif",
|
||||
@@ -105,6 +113,12 @@ static MIN_FACES: [PdfFace; 6] = [
|
||||
italic_angle: 0,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
PdfFace {
|
||||
bytes: include_bytes!("../../../assets/fonts-subset-min/LiberationSerif-Italic.ttf"),
|
||||
base: "LiberationSerif-MathItalic",
|
||||
italic_angle: -12,
|
||||
font: OnceLock::new(),
|
||||
},
|
||||
];
|
||||
|
||||
fn minimal_text_char(ch: char) -> bool {
|
||||
@@ -134,7 +148,7 @@ fn minimal_text_char(ch: char) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
fn face(font: FontFace, faces: &'static [PdfFace; 6]) -> &'static PdfFace {
|
||||
fn face(font: FontFace, faces: &'static [PdfFace; FACE_COUNT]) -> &'static PdfFace {
|
||||
&faces[font as usize]
|
||||
}
|
||||
|
||||
@@ -233,6 +247,7 @@ fn font_resource(font: FontFace) -> &'static str {
|
||||
FontFace::BoldItalic => "F4",
|
||||
FontFace::Mono => "F5",
|
||||
FontFace::Sans => "F6",
|
||||
FontFace::MathItalic => "F7",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,22 +262,22 @@ enum FontBackend {
|
||||
|
||||
/// Knuth's Computer Modern Type1 programs, in `FontFace` order.
|
||||
fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
|
||||
static PFBS: [Option<&'static [u8]>; 6] = [
|
||||
static PFBS: [Option<&'static [u8]>; FACE_COUNT] = [
|
||||
Some(include_bytes!("../../../assets/fonts/cmr10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmbx10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmti10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmbxti10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmtt10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmss10.pfb")),
|
||||
Some(include_bytes!("../../../assets/fonts/cmmi10.pfb")),
|
||||
];
|
||||
PFBS[slot]
|
||||
}
|
||||
|
||||
fn type1_fonts() -> &'static [Option<Type1Font>; 6] {
|
||||
static FONTS: OnceLock<[Option<Type1Font>; 6]> = OnceLock::new();
|
||||
FONTS.get_or_init(|| {
|
||||
[0usize, 1, 2, 3, 4, 5].map(|slot| type1_bytes(slot).and_then(Type1Font::from_pfb))
|
||||
})
|
||||
fn type1_fonts() -> &'static [Option<Type1Font>; FACE_COUNT] {
|
||||
static FONTS: OnceLock<[Option<Type1Font>; FACE_COUNT]> = OnceLock::new();
|
||||
FONTS
|
||||
.get_or_init(|| std::array::from_fn(|slot| type1_bytes(slot).and_then(Type1Font::from_pfb)))
|
||||
}
|
||||
|
||||
/// TeX page origin offsets (`\hoffset`, `\voffset`) in scaled points.
|
||||
@@ -276,7 +291,7 @@ fn page_content(
|
||||
list: &DisplayList,
|
||||
height: i64,
|
||||
image_names: &BTreeMap<usize, String>,
|
||||
faces: &'static [PdfFace; 6],
|
||||
faces: &'static [PdfFace; FACE_COUNT],
|
||||
cm_codes: &[Option<&BTreeMap<char, u8>>],
|
||||
origin: PageOrigin,
|
||||
) -> String {
|
||||
@@ -452,8 +467,8 @@ fn write_pdf(
|
||||
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
|
||||
let mut used_faces = [false; 6];
|
||||
let mut used_chars: [BTreeSet<char>; 6] = Default::default();
|
||||
let mut used_faces = [false; FACE_COUNT];
|
||||
let mut used_chars: [BTreeSet<char>; FACE_COUNT] = Default::default();
|
||||
for page in pages {
|
||||
for item in &page.items {
|
||||
if let DisplayItem::Text { font, text, .. } = item {
|
||||
@@ -462,7 +477,7 @@ fn write_pdf(
|
||||
}
|
||||
}
|
||||
}
|
||||
let cm_faces: Vec<Option<(&'static Type1Font, BTreeMap<char, u8>)>> = (0..6)
|
||||
let cm_faces: Vec<Option<(&'static Type1Font, BTreeMap<char, u8>)>> = (0..FACE_COUNT)
|
||||
.map(|slot| {
|
||||
if backend != FontBackend::ComputerModern || !used_faces[slot] {
|
||||
return None;
|
||||
@@ -488,8 +503,8 @@ fn write_pdf(
|
||||
_ => true,
|
||||
})
|
||||
});
|
||||
let faces: &'static [PdfFace; 6] = if minimal { &MIN_FACES } else { &FULL_FACES };
|
||||
let mut font_ids = [0usize; 6];
|
||||
let faces: &'static [PdfFace; FACE_COUNT] = if minimal { &MIN_FACES } else { &FULL_FACES };
|
||||
let mut font_ids = [0usize; FACE_COUNT];
|
||||
for (slot, face) in faces.iter().enumerate() {
|
||||
if !used_faces[slot] {
|
||||
continue;
|
||||
@@ -622,7 +637,7 @@ fn write_pdf(
|
||||
.map(|(_, slot)| format!("/Im{slot} {} 0 R", image_base + *slot))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
let fonts = (0..6)
|
||||
let fonts = (0..FACE_COUNT)
|
||||
.filter(|slot| used_faces[*slot])
|
||||
.map(|slot| format!("/F{} {} 0 R", slot + 1, font_ids[slot]))
|
||||
.collect::<Vec<_>>()
|
||||
@@ -827,6 +842,23 @@ mod type1_pdf_tests {
|
||||
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_italic_text_embeds_cmmi10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "x".to_string(),
|
||||
font: FontFace::MathItalic,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMMI10"), "{text}");
|
||||
assert!(text.contains("(\\170)"), "{text}");
|
||||
assert!(!text.contains("/BaseFont /CMR10"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type1_backend_falls_back_for_uncovered_characters() {
|
||||
let mut list = hello_list();
|
||||
|
||||
Reference in new issue
Block a user