Embed every Computer Modern face as Type1 outlines
This commit is contained in:
1 parent
5f5fc393a4
commit
f7849f39c2
8 files changed
+59
-32
No files matched your search
@@ -245,10 +245,24 @@ enum FontBackend {
|
||||
ComputerModern,
|
||||
}
|
||||
|
||||
fn cmr10_type1() -> Option<&'static Type1Font> {
|
||||
static FONT: OnceLock<Option<Type1Font>> = OnceLock::new();
|
||||
FONT.get_or_init(|| Type1Font::from_pfb(include_bytes!("../../../assets/fonts/cmr10.pfb")))
|
||||
.as_ref()
|
||||
/// Knuth's Computer Modern Type1 programs, in `FontFace` order.
|
||||
fn type1_bytes(slot: usize) -> Option<&'static [u8]> {
|
||||
static PFBS: [Option<&'static [u8]>; 6] = [
|
||||
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")),
|
||||
];
|
||||
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))
|
||||
})
|
||||
}
|
||||
|
||||
/// TeX page origin offsets (`\hoffset`, `\voffset`) in scaled points.
|
||||
@@ -263,7 +277,7 @@ fn page_content(
|
||||
height: i64,
|
||||
image_names: &BTreeMap<usize, String>,
|
||||
faces: &'static [PdfFace; 6],
|
||||
cm_codes: Option<&BTreeMap<char, u8>>,
|
||||
cm_codes: &[Option<&BTreeMap<char, u8>>],
|
||||
origin: PageOrigin,
|
||||
) -> String {
|
||||
let mut content = String::from("q\n0 0 0 rg\n");
|
||||
@@ -283,8 +297,11 @@ fn page_content(
|
||||
text,
|
||||
font,
|
||||
} => {
|
||||
let operand = match cm_codes {
|
||||
Some(codes) if matches!(*font, FontFace::Roman) => {
|
||||
let operand = match cm_codes
|
||||
.get(*font as usize)
|
||||
.and_then(|entry| entry.as_ref())
|
||||
{
|
||||
Some(codes) => {
|
||||
let mut literal = String::from("(");
|
||||
for character in text.chars() {
|
||||
if let Some(code) = codes.get(&character) {
|
||||
@@ -445,23 +462,26 @@ fn write_pdf(
|
||||
}
|
||||
}
|
||||
}
|
||||
let cm_codes: Option<(&Type1Font, BTreeMap<char, u8>)> = if backend
|
||||
== FontBackend::ComputerModern
|
||||
&& used_faces[0]
|
||||
{
|
||||
cmr10_type1().and_then(|font| {
|
||||
if !font.covers(used_chars[0].iter().copied()) {
|
||||
let cm_faces: Vec<Option<(&'static Type1Font, BTreeMap<char, u8>)>> = (0..6)
|
||||
.map(|slot| {
|
||||
if backend != FontBackend::ComputerModern || !used_faces[slot] {
|
||||
return None;
|
||||
}
|
||||
let codes = used_chars[0]
|
||||
let font = type1_fonts()[slot].as_ref()?;
|
||||
if !font.covers(used_chars[slot].iter().copied()) {
|
||||
return None;
|
||||
}
|
||||
let codes = used_chars[slot]
|
||||
.iter()
|
||||
.filter_map(|character| font.code_for(*character).map(|code| (*character, code)))
|
||||
.collect();
|
||||
Some((font, codes))
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
.collect();
|
||||
let cm_views: Vec<Option<&BTreeMap<char, u8>>> = cm_faces
|
||||
.iter()
|
||||
.map(|entry| entry.as_ref().map(|(_, codes)| codes))
|
||||
.collect();
|
||||
let minimal = pages.iter().all(|page| {
|
||||
page.items.iter().all(|item| match item {
|
||||
DisplayItem::Text { text, .. } => text.chars().all(minimal_text_char),
|
||||
@@ -474,9 +494,7 @@ fn write_pdf(
|
||||
if !used_faces[slot] {
|
||||
continue;
|
||||
}
|
||||
if slot == 0
|
||||
&& let Some((font, codes)) = &cm_codes
|
||||
{
|
||||
if let Some((font, codes)) = &cm_faces[slot] {
|
||||
let font_id = objects.len() + 1;
|
||||
let descriptor_id = font_id + 1;
|
||||
let file_id = font_id + 2;
|
||||
@@ -656,14 +674,7 @@ fn write_pdf(
|
||||
for (index, slot) in &used {
|
||||
names.insert(*index, format!("Im{slot}"));
|
||||
}
|
||||
let content = page_content(
|
||||
list,
|
||||
height,
|
||||
&names,
|
||||
faces,
|
||||
cm_codes.as_ref().map(|(_, codes)| codes),
|
||||
origin,
|
||||
);
|
||||
let content = page_content(list, height, &names, faces, &cm_views, origin);
|
||||
objects.push(
|
||||
format!(
|
||||
"<< /Length {} >>\nstream\n{}endstream",
|
||||
@@ -800,6 +811,22 @@ mod type1_pdf_tests {
|
||||
assert!(!text.contains("/Subtype /Type0"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bold_text_embeds_cmbx10() {
|
||||
let mut list = hello_list();
|
||||
list.items[0] = DisplayItem::Text {
|
||||
x: 0,
|
||||
y: 800_000,
|
||||
size: 655_360,
|
||||
text: "Bold".to_string(),
|
||||
font: FontFace::Bold,
|
||||
};
|
||||
let pdf = write(&[list]);
|
||||
let text = String::from_utf8_lossy(&pdf);
|
||||
assert!(text.contains("/BaseFont /CMBX10"), "{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