Render imported PDF TrueType device fonts from exact SFNT faces

This commit is contained in:
gloamxun committed 2026-11-28 04:21:30 +08:00
1 parent b640b26eff
commit 84d6f3ae51
14 files changed
+504 -76

No files matched your search

@@ -3,13 +3,19 @@ use super::resolved;
use lopdf::{Dictionary, Document, Object};
use std::collections::{BTreeSet, HashSet};
pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> Vec<String> {
#[derive(Default)]
struct Names {
type1: BTreeSet<String>,
truetype: BTreeSet<String>,
}
pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> (Vec<String>, Vec<String>) {
let Some(resources) = resources.and_then(|value| resolved(doc, value).ok()) else {
return Vec::new();
return (Vec::new(), Vec::new());
};
let mut pending = vec![resources];
let mut seen = HashSet::new();
let mut names = BTreeSet::new();
let mut names = Names::default();
while let Some(resources) = pending.pop() {
let Ok(dict) = resources.as_dict() else {
continue;
@@ -64,7 +70,10 @@ pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> Vec<String>
}
}
}
names.into_iter().collect()
(
names.type1.into_iter().collect(),
names.truetype.into_iter().collect(),
)
}
fn get<'a>(doc: &'a Document, dict: &'a Dictionary, key: &[u8]) -> Option<&'a Object> {
@@ -74,25 +83,33 @@ fn get<'a>(doc: &'a Document, dict: &'a Dictionary, key: &[u8]) -> Option<&'a Ob
fn capture_font<'a>(
doc: &'a Document,
font: &'a Dictionary,
names: &mut BTreeSet<String>,
names: &mut Names,
pending: &mut Vec<&'a Object>,
) {
match get(doc, font, b"Subtype").and_then(|value| value.as_name().ok()) {
Some(b"Type1") => {
let Some(name) = get(doc, font, b"BaseFont")
.and_then(|value| value.as_name().ok())
.and_then(|bytes| std::str::from_utf8(bytes).ok())
else {
return;
};
let descriptor =
get(doc, font, b"FontDescriptor").and_then(|value| value.as_dict().ok());
if !descriptor.is_some_and(|dict| {
[b"FontFile".as_slice(), b"FontFile2", b"FontFile3"]
.into_iter()
.any(|key| dict.has(key))
}) {
names.insert(name.to_owned());
let subtype = get(doc, font, b"Subtype").and_then(|value| value.as_name().ok());
match subtype {
Some(b"Type1" | b"TrueType") => {
capture_name(
doc,
font,
if subtype == Some(b"Type1") {
&mut names.type1
} else {
&mut names.truetype
},
);
}
Some(b"Type0") => {
if let Some(descendants) =
get(doc, font, b"DescendantFonts").and_then(|value| value.as_array().ok())
&& let Some(descendant) = descendants
.first()
.and_then(|value| resolved(doc, value).ok())
.and_then(|value| value.as_dict().ok())
&& get(doc, descendant, b"Subtype").and_then(|value| value.as_name().ok())
== Some(b"CIDFontType2")
{
capture_name(doc, descendant, &mut names.truetype);
}
}
Some(b"Type3") => {
@@ -103,3 +120,20 @@ fn capture_font<'a>(
_ => {}
}
}
fn capture_name(doc: &Document, font: &Dictionary, names: &mut BTreeSet<String>) {
let Some(name) = get(doc, font, b"BaseFont")
.and_then(|value| value.as_name().ok())
.and_then(|bytes| std::str::from_utf8(bytes).ok())
else {
return;
};
let descriptor = get(doc, font, b"FontDescriptor").and_then(|value| value.as_dict().ok());
if !descriptor.is_some_and(|dict| {
[b"FontFile".as_slice(), b"FontFile2", b"FontFile3"]
.into_iter()
.any(|key| dict.has(key))
}) {
names.insert(name.to_owned());
}
}
@@ -148,6 +148,7 @@ pub(super) fn read(
color_space,
imported_fonts: Vec::new(),
device_type1_fonts: Vec::new(),
device_truetype_fonts: Vec::new(),
pdftex_source: None,
restored_pdf_origin: None,
},