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
@@ -128,6 +128,7 @@ impl Cmr10Metrics {
|
||||
3 => Self::BoldItalic,
|
||||
4 => Self::Mono,
|
||||
5 => Self::Sans,
|
||||
6 => Self::Math,
|
||||
_ => Self::Roman,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,14 +290,6 @@ fn atom_class(atom: &MathAtom) -> MathClass {
|
||||
}
|
||||
}
|
||||
|
||||
fn delimiter_width(delimiter: char, font: &Cmr10Metrics) -> i64 {
|
||||
if delimiter == '.' {
|
||||
0
|
||||
} else {
|
||||
font.width(delimiter)
|
||||
}
|
||||
}
|
||||
|
||||
/// TeX grows delimiters with the enclosed formula; this is a simple 1x-3x
|
||||
/// approximation of that rule.
|
||||
/// Pick the delimiter glyph TeX would use for a body of `delta` extent.
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -19,6 +19,8 @@ pub enum FontFace {
|
||||
BoldItalic,
|
||||
Mono,
|
||||
Sans,
|
||||
/// Computer Modern math italic (`cmmi10`).
|
||||
MathItalic,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
@@ -159,7 +161,10 @@ impl DisplayList {
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let style = if matches!(font, FontFace::Italic | FontFace::BoldItalic) {
|
||||
let style = if matches!(
|
||||
font,
|
||||
FontFace::Italic | FontFace::BoldItalic | FontFace::MathItalic
|
||||
) {
|
||||
" font-style=\"italic\""
|
||||
} else {
|
||||
""
|
||||
@@ -261,6 +266,10 @@ fn paragraph_face(font: ParagraphFont) -> FontFace {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_math_letter(character: char) -> bool {
|
||||
matches!(character, '\u{0391}'..='\u{03A9}' | '\u{03B1}'..='\u{03C9}')
|
||||
}
|
||||
|
||||
fn character_text(character: char) -> String {
|
||||
match character {
|
||||
'\u{FB00}' => "ff".to_owned(),
|
||||
@@ -646,8 +655,13 @@ impl Page {
|
||||
MathAtom::Space(_) => {}
|
||||
MathAtom::Symbol(character) => {
|
||||
if !character.is_whitespace() {
|
||||
let face = if character.is_ascii_alphabetic() || is_math_letter(*character) {
|
||||
FontFace::MathItalic
|
||||
} else {
|
||||
FontFace::Roman
|
||||
};
|
||||
list.push(DisplayItem::Text {
|
||||
font: FontFace::Roman,
|
||||
font: face,
|
||||
x,
|
||||
y: baseline,
|
||||
size,
|
||||
|
||||
@@ -51,10 +51,7 @@ impl Type1Font {
|
||||
1 if encrypted.is_empty() => header.extend_from_slice(&bytes[start..end]),
|
||||
1 => trailer.extend_from_slice(&bytes[start..end]),
|
||||
2 => encrypted.extend_from_slice(&bytes[start..end]),
|
||||
3 => {
|
||||
index = end;
|
||||
break;
|
||||
}
|
||||
3 => break,
|
||||
_ => return None,
|
||||
}
|
||||
index = end;
|
||||
@@ -144,6 +141,55 @@ fn glyph_names(ch: char) -> &'static [&'static str] {
|
||||
"s", "t", "u", "v", "w", "x", "y", "z",
|
||||
];
|
||||
match ch {
|
||||
'\u{0391}' => &["Alpha"],
|
||||
'\u{0392}' => &["Beta"],
|
||||
'\u{0393}' => &["Gamma"],
|
||||
'\u{0394}' => &["Delta"],
|
||||
'\u{0395}' => &["Epsilon"],
|
||||
'\u{0396}' => &["Zeta"],
|
||||
'\u{0397}' => &["Eta"],
|
||||
'\u{0398}' => &["Theta"],
|
||||
'\u{0399}' => &["Iota"],
|
||||
'\u{039A}' => &["Kappa"],
|
||||
'\u{039B}' => &["Lambda"],
|
||||
'\u{039C}' => &["Mu"],
|
||||
'\u{039D}' => &["Nu"],
|
||||
'\u{039E}' => &["Xi"],
|
||||
'\u{039F}' => &["Omicron"],
|
||||
'\u{03A0}' => &["Pi"],
|
||||
'\u{03A1}' => &["Rho"],
|
||||
'\u{03A3}' => &["Sigma"],
|
||||
'\u{03A4}' => &["Tau"],
|
||||
'\u{03A5}' => &["Upsilon"],
|
||||
'\u{03A6}' => &["Phi"],
|
||||
'\u{03A7}' => &["Chi"],
|
||||
'\u{03A8}' => &["Psi"],
|
||||
'\u{03A9}' => &["Omega"],
|
||||
'\u{03B1}' => &["alpha"],
|
||||
'\u{03B2}' => &["beta"],
|
||||
'\u{03B3}' => &["gamma"],
|
||||
'\u{03B4}' => &["delta"],
|
||||
'\u{03B5}' => &["epsilon"],
|
||||
'\u{03B6}' => &["zeta"],
|
||||
'\u{03B7}' => &["eta"],
|
||||
'\u{03B8}' => &["theta"],
|
||||
'\u{03B9}' => &["iota"],
|
||||
'\u{03BA}' => &["kappa"],
|
||||
'\u{03BB}' => &["lambda"],
|
||||
'\u{03BC}' => &["mu"],
|
||||
'\u{03BD}' => &["nu"],
|
||||
'\u{03BE}' => &["xi"],
|
||||
'\u{03BF}' => &["omicron"],
|
||||
'\u{03C0}' => &["pi"],
|
||||
'\u{03C1}' => &["rho"],
|
||||
'\u{03C2}' => &["sigma1"],
|
||||
'\u{03C3}' => &["sigma"],
|
||||
'\u{03C4}' => &["tau"],
|
||||
'\u{03C5}' => &["upsilon"],
|
||||
'\u{03C6}' => &["phi"],
|
||||
'\u{03C7}' => &["chi"],
|
||||
'\u{03C8}' => &["psi"],
|
||||
'\u{03C9}' => &["omega"],
|
||||
'0'..='9' => &DIGITS[(ch as u8 - b'0') as usize..][..1],
|
||||
'A'..='Z' => &UPPER[(ch as u8 - b'A') as usize..][..1],
|
||||
'a'..='z' => &LOWER[(ch as u8 - b'a') as usize..][..1],
|
||||
|
||||
Reference in new issue
Block a user