Render Type1 map slant and extension in PDF and SVG

This commit is contained in:
gloamxun committed 2026-11-21 10:04:48 +08:00
1 parent 28dfba5a5e
commit 3caf1eced2
7 files changed
+274 -70

No files matched your search

+44 -16
View File
@@ -3,6 +3,7 @@ use super::{NativeFont, verify_resource};
use crate::{
execution::{FontId, SourceProvider},
resources::{LoadedResource, ResourceKind, ResourceVersion},
type1::Type1Transform,
};
use std::{
collections::{BTreeMap, BTreeSet},
@@ -17,7 +18,8 @@ pub(super) struct MapEntry {
file: Option<String>,
pub encoding: Option<String>,
postscript_name: bool,
unsupported_transform: Option<String>,
#[serde(default, skip_serializing_if = "Type1Transform::is_identity")]
pub transform: Type1Transform,
}
impl MapEntry {
@@ -26,11 +28,6 @@ impl MapEntry {
}
pub(super) fn type1_file(&self, name: &str) -> Result<&str, String> {
if let Some(value) = &self.unsupported_transform {
return Err(format!(
"native font map transform is not implemented: {value}"
));
}
let file = self.file.as_deref().ok_or_else(|| {
format!("font map {name} has no embedded Type1 program; mapped device fonts require their actual support")
})?;
@@ -87,18 +84,28 @@ fn parse_map_line(
file: None,
encoding: None,
postscript_name: false,
unsupported_transform: None,
transform: Type1Transform::default(),
};
for (index, (quote, value)) in fields.into_iter().skip(1).enumerate() {
if quote {
let words: Vec<_> = value.split_whitespace().collect();
// pdfTeX ignores PostScript names and ReEncodeFont words;
// the actual .enc file supplies the encoding vector.
if words
.iter()
.any(|w| matches!(*w, "SlantFont" | "ExtendFont"))
{
entry.unsupported_transform.get_or_insert(value);
let mut previous = "";
for word in value.split_ascii_whitespace() {
let (operation, number) = if let Some(prefix) = word.strip_suffix("SlantFont") {
(&mut entry.transform.slant, prefix)
} else if let Some(prefix) = word.strip_suffix("ExtendFont") {
(&mut entry.transform.extend, prefix)
} else {
previous = word;
continue;
};
let number = if number.is_empty() { previous } else { number };
if let Ok(value) = number.parse::<f32>() {
if !value.is_finite() {
return Err(format!("invalid Type1 map transform for {name}"));
}
*operation = (value * 1000.).round() as i32;
}
previous = word;
}
} else if value.starts_with('<') {
let path = value.trim_start_matches('<').trim_start_matches('[');
@@ -111,6 +118,27 @@ fn parse_map_line(
entry.postscript_name = true;
}
}
if entry.transform.extend == 1000 {
entry.transform.extend = 0;
}
if entry.transform.slant.unsigned_abs() > 1000 || entry.transform.extend.unsigned_abs() > 2000 {
return Err(format!("Type1 map transform out of range for {name}"));
}
if !entry.transform.is_identity()
&& entry.file.as_deref().is_none_or(|file| {
let extension = Path::new(file)
.extension()
.and_then(|ext| ext.to_str())
.unwrap_or("");
["ttf", "ttc", "otf"]
.iter()
.any(|ext| extension.eq_ignore_ascii_case(ext))
})
{
return Err(format!(
"SlantFont/ExtendFont require an embedded Type1 program for {name}"
));
}
Ok(Some((name, entry)))
}
@@ -360,6 +388,6 @@ pub(super) fn dedicated_space_map(font: &NativeFont) -> MapEntry {
file: Some("pdftexspace.pfb".into()),
encoding: None,
postscript_name: false,
unsupported_transform: None,
transform: Type1Transform::default(),
}
}