Render Type1 map slant and extension in PDF and SVG
This commit is contained in:
1 parent
28dfba5a5e
commit
3caf1eced2
7 files changed
+274
-70
No files matched your search
@@ -5,7 +5,7 @@ use crate::{
|
||||
execution::{FontId, FontResource},
|
||||
resources::{LoadedResource, ResourceVersion},
|
||||
tfm::TfmMetrics,
|
||||
type1::Type1Font,
|
||||
type1::{Type1Font, Type1Transform},
|
||||
type1_outline::Type1Outlines,
|
||||
};
|
||||
use std::{
|
||||
@@ -30,6 +30,8 @@ pub struct NativeFontPaint {
|
||||
program_index: Option<usize>,
|
||||
}
|
||||
|
||||
pub(crate) type Type1ProgramKey<'a> = (&'a ResourceVersion, Type1Transform);
|
||||
|
||||
impl NativeFontPaint {
|
||||
pub(crate) fn has_pdf_space(&self, font: &NativeFont) -> bool {
|
||||
// pdfTeX requires the space name in an actual map re-encoding.
|
||||
@@ -38,12 +40,10 @@ impl NativeFontPaint {
|
||||
&& self.encoding[32] == "space"
|
||||
&& font.metrics().width(' ') > 65_782
|
||||
}
|
||||
pub(crate) fn type1_resource(&self) -> Option<&crate::resources::ResourceVersion> {
|
||||
pub(crate) fn type1_program_key(&self) -> Option<Type1ProgramKey<'_>> {
|
||||
match &self.program {
|
||||
NativeFontProgram::Type1(_) => {
|
||||
// The physical font follows the map, before any encoding resource.
|
||||
self.resources.get(self.program_index.unwrap_or(1))
|
||||
}
|
||||
NativeFontProgram::Type1(font) => self.resources.get(self.program_index.unwrap_or(1))
|
||||
.map(|version| (version, font.transform)),
|
||||
NativeFontProgram::Pk(_) => None,
|
||||
}
|
||||
}
|
||||
@@ -192,7 +192,7 @@ const MAX_CACHED_PFB_BYTES: usize = 256 * 1024;
|
||||
/// Decoded Type 1 programs shared across revisions in one compiler.
|
||||
#[derive(Default)]
|
||||
pub struct NativeFontCache {
|
||||
type1: Mutex<VecDeque<(ResourceVersion, Arc<DecodedType1>)>>,
|
||||
type1: Mutex<VecDeque<((ResourceVersion, Type1Transform), Arc<DecodedType1>)>>,
|
||||
}
|
||||
|
||||
struct DecodedType1 {
|
||||
@@ -203,16 +203,21 @@ struct DecodedType1 {
|
||||
}
|
||||
|
||||
impl DecodedType1 {
|
||||
fn from_pfb(resource: &LoadedResource) -> Result<Self, String> {
|
||||
let program = Type1Font::from_pfb(&resource.data).ok_or_else(|| {
|
||||
fn from_pfb(resource: &LoadedResource, transform: Type1Transform) -> Result<Self, String> {
|
||||
let mut program = Type1Font::from_pfb(&resource.data).ok_or_else(|| {
|
||||
format!(
|
||||
"invalid Type1 PFB program: {}",
|
||||
resource.version.path.display()
|
||||
)
|
||||
})?;
|
||||
program.apply_map_transform(transform)?;
|
||||
let outlines = Type1Outlines::parse(&program)?;
|
||||
let bbox = outlines.normalized_bbox(program.bbox);
|
||||
let stem_v = outlines.stem_v();
|
||||
let (bbox, stem_v) = if transform.is_identity() {
|
||||
(outlines.normalized_bbox(program.bbox), outlines.stem_v())
|
||||
} else {
|
||||
// The PDF descriptor keeps the Type1 program's own font units.
|
||||
(program.bbox, outlines.raw_stem_v())
|
||||
};
|
||||
Ok(Self {
|
||||
program,
|
||||
outlines,
|
||||
@@ -225,25 +230,25 @@ impl DecodedType1 {
|
||||
impl NativeFontCache {
|
||||
// The caller reads the actual resource and verifies its bytes on this
|
||||
// revision before asking for the decoded representation.
|
||||
fn type1(&self, resource: &LoadedResource) -> Result<Arc<DecodedType1>, String> {
|
||||
fn type1(&self, resource: &LoadedResource, transform: Type1Transform) -> Result<Arc<DecodedType1>, String> {
|
||||
if resource.data.len() > MAX_CACHED_PFB_BYTES {
|
||||
return DecodedType1::from_pfb(resource).map(Arc::new);
|
||||
return DecodedType1::from_pfb(resource, transform).map(Arc::new);
|
||||
}
|
||||
let mut entries = self.type1.lock().expect("native Type 1 cache poisoned");
|
||||
if let Some(index) = entries
|
||||
.iter()
|
||||
.position(|(version, _)| *version == resource.version)
|
||||
.position(|((version, cached_transform), _)| *version == resource.version && *cached_transform == transform)
|
||||
{
|
||||
let entry = entries.remove(index).expect("cached Type 1 index exists");
|
||||
let decoded = entry.1.clone();
|
||||
entries.push_back(entry);
|
||||
return Ok(decoded);
|
||||
}
|
||||
let decoded = Arc::new(DecodedType1::from_pfb(resource)?);
|
||||
let decoded = Arc::new(DecodedType1::from_pfb(resource, transform)?);
|
||||
if entries.len() == MAX_TYPE1_PROGRAMS {
|
||||
entries.pop_front();
|
||||
}
|
||||
entries.push_back((resource.version.clone(), decoded.clone()));
|
||||
entries.push_back(((resource.version.clone(), transform), decoded.clone()));
|
||||
Ok(decoded)
|
||||
}
|
||||
|
||||
@@ -447,11 +452,12 @@ fn resolve_physical_fonts(
|
||||
version.map(|version| version.sha256.clone()),
|
||||
file.to_owned(),
|
||||
outline.encoding.clone(),
|
||||
outline.transform,
|
||||
);
|
||||
if !cache.contains_key(&key) {
|
||||
let from = outline.from.to_string_lossy();
|
||||
let resource = actual_resource(files, ResourceKind::Type1, file, &from)?;
|
||||
let decoded = font_cache.type1(&resource)?;
|
||||
let decoded = font_cache.type1(&resource, outline.transform)?;
|
||||
let program = &decoded.program;
|
||||
let mut resources: Vec<_> = version.cloned().into_iter().collect();
|
||||
let program_index = Some(resources.len());
|
||||
|
||||
Reference in new issue
Block a user