Resolve imported PDF Type 1 device fonts in native previews
This commit is contained in:
1 parent
c0970bdbf6
commit
b640b26eff
12 files changed
+586
-106
No files matched your search
@@ -0,0 +1,105 @@
|
||||
//! Device fonts reachable from the selected page's drawing resources.
|
||||
use super::resolved;
|
||||
use lopdf::{Dictionary, Document, Object};
|
||||
use std::collections::{BTreeSet, HashSet};
|
||||
|
||||
pub(super) fn capture(doc: &Document, resources: Option<&Object>) -> Vec<String> {
|
||||
let Some(resources) = resources.and_then(|value| resolved(doc, value).ok()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let mut pending = vec![resources];
|
||||
let mut seen = HashSet::new();
|
||||
let mut names = BTreeSet::new();
|
||||
while let Some(resources) = pending.pop() {
|
||||
let Ok(dict) = resources.as_dict() else {
|
||||
continue;
|
||||
};
|
||||
if !seen.insert(std::ptr::from_ref(dict)) {
|
||||
continue;
|
||||
}
|
||||
if let Some(fonts) = get(doc, dict, b"Font").and_then(|value| value.as_dict().ok()) {
|
||||
for (_, font) in fonts.iter() {
|
||||
if let Some(font) = resolved(doc, font)
|
||||
.ok()
|
||||
.and_then(|value| value.as_dict().ok())
|
||||
{
|
||||
capture_font(doc, font, &mut names, &mut pending);
|
||||
}
|
||||
}
|
||||
}
|
||||
for group in [b"XObject".as_slice(), b"Pattern"] {
|
||||
if let Some(objects) = get(doc, dict, group).and_then(|value| value.as_dict().ok()) {
|
||||
for (_, object) in objects.iter() {
|
||||
if let Ok(Object::Stream(stream)) = resolved(doc, object)
|
||||
&& let Some(resources) = get(doc, &stream.dict, b"Resources")
|
||||
{
|
||||
pending.push(resources);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(states) = get(doc, dict, b"ExtGState").and_then(|value| value.as_dict().ok()) {
|
||||
for (_, state) in states.iter() {
|
||||
let Some(state) = resolved(doc, state)
|
||||
.ok()
|
||||
.and_then(|value| value.as_dict().ok())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if let Some(values) =
|
||||
get(doc, state, b"Font").and_then(|value| value.as_array().ok())
|
||||
&& let Some(font) = values
|
||||
.first()
|
||||
.and_then(|value| resolved(doc, value).ok())
|
||||
.and_then(|value| value.as_dict().ok())
|
||||
{
|
||||
capture_font(doc, font, &mut names, &mut pending);
|
||||
}
|
||||
if let Some(mask) = get(doc, state, b"SMask").and_then(|value| value.as_dict().ok())
|
||||
&& let Some(Object::Stream(form)) = get(doc, mask, b"G")
|
||||
&& let Some(resources) = get(doc, &form.dict, b"Resources")
|
||||
{
|
||||
pending.push(resources);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
names.into_iter().collect()
|
||||
}
|
||||
|
||||
fn get<'a>(doc: &'a Document, dict: &'a Dictionary, key: &[u8]) -> Option<&'a Object> {
|
||||
resolved(doc, dict.get(key).ok()?).ok()
|
||||
}
|
||||
|
||||
fn capture_font<'a>(
|
||||
doc: &'a Document,
|
||||
font: &'a Dictionary,
|
||||
names: &mut BTreeSet<String>,
|
||||
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());
|
||||
}
|
||||
}
|
||||
Some(b"Type3") => {
|
||||
if let Some(resources) = get(doc, font, b"Resources") {
|
||||
pending.push(resources);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -147,6 +147,7 @@ pub(super) fn read(
|
||||
objects: vec![vec![Part::Png(image)]],
|
||||
color_space,
|
||||
imported_fonts: Vec::new(),
|
||||
device_type1_fonts: Vec::new(),
|
||||
pdftex_source: None,
|
||||
restored_pdf_origin: None,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user