Support unembedded pdfTeX Type 1 device fonts in export and preview
This commit is contained in:
1 parent
60d67bbb7b
commit
c0970bdbf6
10 files changed
+466
-34
No files matched your search
@@ -0,0 +1,128 @@
|
||||
//! Unembedded pdfTeX Type 1 fonts retain a device name and optional local outlines.
|
||||
use super::{
|
||||
NativeFont, NativeFontCache, NativeFontPaint, NativeFontProgram, actual_resource,
|
||||
encoding_vector, map::MapEntry, verify_resource,
|
||||
};
|
||||
use crate::{
|
||||
execution::SourceProvider,
|
||||
resources::ResourceKind,
|
||||
type1::{Type1Font, Type1Transform, standard_encoding},
|
||||
};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct DeviceType1Program {
|
||||
pub name: String,
|
||||
pub(crate) reencoded: bool,
|
||||
/// This program is used by previews. The device font stays unembedded.
|
||||
pub preview_program: Option<Arc<Type1Font>>,
|
||||
}
|
||||
|
||||
impl DeviceType1Program {
|
||||
pub(crate) fn standard_flags(&self) -> Option<i32> {
|
||||
standard_flags(&self.name)
|
||||
}
|
||||
}
|
||||
|
||||
fn standard_flags(name: &str) -> Option<i32> {
|
||||
Some(match name {
|
||||
"Courier" => 35,
|
||||
"Courier-Bold" => 35 | (1 << 18),
|
||||
"Courier-Oblique" => 35 | 64,
|
||||
"Courier-BoldOblique" => 35 | 64 | (1 << 18),
|
||||
"Helvetica" => 32,
|
||||
"Helvetica-Bold" => 32 | (1 << 18),
|
||||
"Helvetica-Oblique" => 32 | 64,
|
||||
"Helvetica-BoldOblique" => 32 | 64 | (1 << 18),
|
||||
"Times-Roman" => 34,
|
||||
"Times-Bold" => 34 | (1 << 18),
|
||||
"Times-Italic" => 34 | 64,
|
||||
"Times-BoldItalic" => 34 | 64 | (1 << 18),
|
||||
"Symbol" | "ZapfDingbats" => 4,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn resolve(
|
||||
files: &mut impl SourceProvider,
|
||||
cache: &NativeFontCache,
|
||||
entry: &MapEntry,
|
||||
name: &str,
|
||||
font: &NativeFont,
|
||||
slots: BTreeSet<u8>,
|
||||
) -> Result<NativeFontPaint, String> {
|
||||
let from = entry.from.to_string_lossy();
|
||||
let mut resources: Vec<_> = entry.version.iter().cloned().collect();
|
||||
let mut preview = None;
|
||||
let candidates = [name, font.name.as_str()];
|
||||
for (index, candidate) in candidates.into_iter().enumerate() {
|
||||
if index == 1 && candidate == name {
|
||||
break;
|
||||
}
|
||||
for extension in ["pfb", "pfa"] {
|
||||
let file = format!("{candidate}.{extension}");
|
||||
let Some(resource) = files.optional_font_file(ResourceKind::Type1, &file, &from)?
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let resource = verify_resource(resource, ResourceKind::Type1, &file)?;
|
||||
if let Ok(decoded) = cache.type1(&resource, Type1Transform::default())
|
||||
&& decoded.program.name == name
|
||||
{
|
||||
resources.push(resource.version);
|
||||
preview = Some(decoded);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if preview.is_some() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let encoding = if let Some(file) = &entry.encoding {
|
||||
let encoding = actual_resource(files, ResourceKind::Encoding, file, &from)?;
|
||||
let vector = encoding_vector(&encoding.data)?;
|
||||
resources.push(encoding.version);
|
||||
vector
|
||||
} else {
|
||||
let standard_latin = standard_flags(name).is_some_and(|flags| flags & 32 != 0);
|
||||
(0..=255u8)
|
||||
.map(|slot| {
|
||||
match &preview {
|
||||
Some(font) => font.program.name_for_code(slot),
|
||||
None if standard_latin => standard_encoding(slot),
|
||||
None => None,
|
||||
}
|
||||
.unwrap_or(".notdef")
|
||||
.to_owned()
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
let mut glyph_paths = BTreeMap::new();
|
||||
if let Some(preview) = &preview {
|
||||
for slot in slots {
|
||||
let glyph = &encoding[usize::from(slot)];
|
||||
if preview.outlines.has_glyph(glyph) {
|
||||
glyph_paths.insert(slot, preview.outlines.glyph_path(glyph)?);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(NativeFontPaint {
|
||||
program: NativeFontProgram::DeviceType1(DeviceType1Program {
|
||||
name: name.to_owned(),
|
||||
reencoded: entry.encoding.is_some(),
|
||||
preview_program: preview.map(|font| font.program.clone()),
|
||||
}),
|
||||
encoding,
|
||||
glyph_paths,
|
||||
bbox: [0; 4],
|
||||
stem_v: 0,
|
||||
subset: false,
|
||||
descriptor_flags: entry.descriptor_flags,
|
||||
xdv_descriptor: None,
|
||||
resources,
|
||||
program_index: None,
|
||||
})
|
||||
}
|
||||
@@ -66,6 +66,10 @@ impl MapEntry {
|
||||
self.file.is_some() || self.postscript_name
|
||||
}
|
||||
|
||||
pub(super) fn device_type1_name(&self) -> Option<&str> {
|
||||
self.ps_name.as_deref().filter(|_| self.file.is_none())
|
||||
}
|
||||
|
||||
pub(super) fn true_type_file(&self) -> Option<&str> {
|
||||
self.outline_file(&["ttf", "ttc"])
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user