Reuse verified Type 1 glyph and PDF subset output across edits
This commit is contained in:
1 parent
6939ff71ff
commit
11c8ad1eaa
12 files changed
+236
-11
No files matched your search
@@ -1924,6 +1924,7 @@ impl State {
|
||||
fonts: &self.fonts,
|
||||
page_tree_attributes: &self.pdf_pages_attributes,
|
||||
xdv_glyph_names: None,
|
||||
font_cache: None,
|
||||
xdv_font_unicode: self.input_profile == super::InputProfile::XeTeX,
|
||||
xdv_keep_destinations: self.xdv_setup.keep_destinations,
|
||||
xdv_outline_open_depth: self.xdv_setup.outline_open_depth.unwrap_or_default(),
|
||||
|
||||
@@ -19,11 +19,13 @@ mod device_type1;
|
||||
mod map;
|
||||
mod sfd;
|
||||
mod truetype;
|
||||
mod type1_cache;
|
||||
mod virtual_paint;
|
||||
pub use cff::OpenTypeCffProgram;
|
||||
pub use composite_type1::CompositeType1Program;
|
||||
pub use device_type1::DeviceType1Program;
|
||||
pub use truetype::TrueTypeProgram;
|
||||
pub use type1_cache::Type1FontWork;
|
||||
pub(crate) use truetype::cid_width;
|
||||
pub use map::PdfFontBindings;
|
||||
pub(crate) use map::XdvFontMatrix;
|
||||
@@ -305,15 +307,17 @@ pub(crate) fn verify_resource(
|
||||
const MAX_TYPE1_PROGRAMS: usize = 16;
|
||||
const MAX_CACHED_TYPE1_BYTES: usize = 256 * 1024;
|
||||
|
||||
/// Decoded Type 1 programs shared across revisions in one compiler.
|
||||
/// Verified Type 1 programs and derived device data shared across revisions.
|
||||
#[derive(Default)]
|
||||
pub struct NativeFontCache {
|
||||
type1: Mutex<VecDeque<((ResourceVersion, Type1Transform), Arc<DecodedType1>)>>,
|
||||
type1_output: type1_cache::Type1OutputCache,
|
||||
}
|
||||
|
||||
struct DecodedType1 {
|
||||
program: Arc<Type1Font>,
|
||||
outlines: Type1Outlines,
|
||||
glyphs: Mutex<type1_cache::GlyphMeasurements>,
|
||||
bbox: [i32; 4],
|
||||
stem_v: i32,
|
||||
xdv_descriptor: OnceLock<Result<XdvDescriptor, String>>,
|
||||
@@ -334,6 +338,7 @@ impl DecodedType1 {
|
||||
Ok(Self {
|
||||
program: Arc::new(program),
|
||||
outlines,
|
||||
glyphs: Mutex::default(),
|
||||
bbox,
|
||||
stem_v,
|
||||
xdv_descriptor: OnceLock::new(),
|
||||
@@ -758,7 +763,7 @@ fn resolve_physical_fonts(
|
||||
font.name
|
||||
));
|
||||
}
|
||||
let (path, width) = outlines.glyph_path_and_width(name)?;
|
||||
let (path, width) = font_cache.type1_glyph(decoded, name)?;
|
||||
paint.glyph_paths.insert(slot, path);
|
||||
if paint.xdv_descriptor.is_some() {
|
||||
paint.xdv_widths.insert(slot, width);
|
||||
|
||||
@@ -62,7 +62,7 @@ pub(super) fn resolve(
|
||||
let mut cells = Vec::with_capacity(256);
|
||||
let mut glyph_paths = BTreeMap::new();
|
||||
let mut measured = BTreeMap::new();
|
||||
let (_, default_width) = outlines.glyph_path_and_width(".notdef")?;
|
||||
let (_, default_width) = cache.type1_glyph(&decoded, ".notdef")?;
|
||||
let width = |value: i64| {
|
||||
value
|
||||
.saturating_add(5)
|
||||
@@ -88,7 +88,7 @@ pub(super) fn resolve(
|
||||
let (path, advance) = match measured.entry(cid) {
|
||||
std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
|
||||
std::collections::btree_map::Entry::Vacant(entry) => {
|
||||
entry.insert(outlines.glyph_path_and_width(name)?)
|
||||
entry.insert(cache.type1_glyph(&decoded, name)?)
|
||||
}
|
||||
};
|
||||
glyph_paths.insert(slot, path.clone());
|
||||
|
||||
@@ -122,7 +122,7 @@ pub(super) fn resolve(
|
||||
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)?);
|
||||
glyph_paths.insert(slot, cache.type1_glyph(preview, glyph)?.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
//! Device output derived from verified Type 1 programs within a session.
|
||||
use super::{DecodedType1, NativeFontCache, Type1ProgramKey};
|
||||
use crate::{
|
||||
resources::ResourceVersion,
|
||||
type1::{Type1Font, Type1Transform},
|
||||
type1_outline::{SubsetProgram, write_subset},
|
||||
};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet, VecDeque},
|
||||
sync::{
|
||||
Arc, Mutex,
|
||||
atomic::{AtomicUsize, Ordering::Relaxed},
|
||||
},
|
||||
};
|
||||
|
||||
const MAX_GLYPH_BYTES_PER_PROGRAM: usize = 256 * 1024;
|
||||
const MAX_GLYPHS_PER_PROGRAM: usize = 512;
|
||||
const MAX_SUBSETS: usize = 32;
|
||||
const MAX_SUBSET_PROGRAM_BYTES: usize = 64 * 1024;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Type1FontWork {
|
||||
pub glyphs_measured: usize,
|
||||
pub glyphs_reused: usize,
|
||||
pub subsets_written: usize,
|
||||
pub subsets_reused: usize,
|
||||
}
|
||||
|
||||
impl Type1FontWork {
|
||||
pub fn since(self, earlier: Self) -> Self {
|
||||
Self {
|
||||
glyphs_measured: self.glyphs_measured.saturating_sub(earlier.glyphs_measured),
|
||||
glyphs_reused: self.glyphs_reused.saturating_sub(earlier.glyphs_reused),
|
||||
subsets_written: self.subsets_written.saturating_sub(earlier.subsets_written),
|
||||
subsets_reused: self.subsets_reused.saturating_sub(earlier.subsets_reused),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Type1OutputCache {
|
||||
glyphs_measured: AtomicUsize,
|
||||
glyphs_reused: AtomicUsize,
|
||||
subsets_written: AtomicUsize,
|
||||
subsets_reused: AtomicUsize,
|
||||
subsets: Mutex<VecDeque<CachedSubset>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct GlyphMeasurements {
|
||||
values: BTreeMap<String, (String, i64)>,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
struct CachedSubset {
|
||||
version: ResourceVersion,
|
||||
transform: Type1Transform,
|
||||
font: Type1Font,
|
||||
glyphs: BTreeSet<String>,
|
||||
all_glyphs: bool,
|
||||
tag: String,
|
||||
subset: Arc<SubsetProgram>,
|
||||
}
|
||||
|
||||
impl NativeFontCache {
|
||||
pub fn type1_work(&self) -> Type1FontWork {
|
||||
let work = &self.type1_output;
|
||||
Type1FontWork {
|
||||
glyphs_measured: work.glyphs_measured.load(Relaxed),
|
||||
glyphs_reused: work.glyphs_reused.load(Relaxed),
|
||||
subsets_written: work.subsets_written.load(Relaxed),
|
||||
subsets_reused: work.subsets_reused.load(Relaxed),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn type1_glyph(
|
||||
&self,
|
||||
decoded: &DecodedType1,
|
||||
name: &str,
|
||||
) -> Result<(String, i64), String> {
|
||||
let mut measurements = decoded.glyphs.lock().expect("Type 1 glyph cache poisoned");
|
||||
if let Some(glyph) = measurements.values.get(name) {
|
||||
self.type1_output.glyphs_reused.fetch_add(1, Relaxed);
|
||||
return Ok(glyph.clone());
|
||||
}
|
||||
let glyph = decoded.outlines.glyph_path_and_width(name)?;
|
||||
self.type1_output.glyphs_measured.fetch_add(1, Relaxed);
|
||||
let needed = name.len().saturating_add(glyph.0.len());
|
||||
if needed <= MAX_GLYPH_BYTES_PER_PROGRAM - measurements.bytes
|
||||
&& measurements.values.len() < MAX_GLYPHS_PER_PROGRAM
|
||||
{
|
||||
measurements.values.insert(name.to_owned(), glyph.clone());
|
||||
measurements.bytes += needed;
|
||||
}
|
||||
Ok(glyph)
|
||||
}
|
||||
|
||||
pub(crate) fn type1_subset(
|
||||
&self,
|
||||
(version, transform): Type1ProgramKey<'_>,
|
||||
font: &Type1Font,
|
||||
glyphs: &BTreeSet<String>,
|
||||
all_glyphs: bool,
|
||||
tag: &str,
|
||||
) -> Result<Arc<SubsetProgram>, String> {
|
||||
let cacheable = font.program.len() <= MAX_SUBSET_PROGRAM_BYTES
|
||||
&& glyphs.len() <= MAX_GLYPHS_PER_PROGRAM
|
||||
&& glyphs.iter().all(|name| name.len() <= 128);
|
||||
let work = &self.type1_output;
|
||||
if !cacheable {
|
||||
let subset = write_subset(font, glyphs, all_glyphs, tag)?;
|
||||
work.subsets_written.fetch_add(1, Relaxed);
|
||||
return Ok(Arc::new(subset));
|
||||
}
|
||||
let mut subsets = work.subsets.lock().expect("Type 1 subset cache poisoned");
|
||||
if let Some(index) = subsets.iter().position(|prior| {
|
||||
prior.version == *version
|
||||
&& prior.transform == transform
|
||||
&& prior.tag == tag
|
||||
&& prior.all_glyphs == all_glyphs
|
||||
&& prior.glyphs == *glyphs
|
||||
&& prior.font == *font
|
||||
}) {
|
||||
let hit = subsets.remove(index).expect("cached Type 1 subset exists");
|
||||
let subset = hit.subset.clone();
|
||||
subsets.push_back(hit);
|
||||
work.subsets_reused.fetch_add(1, Relaxed);
|
||||
return Ok(subset);
|
||||
}
|
||||
let subset = Arc::new(write_subset(font, glyphs, all_glyphs, tag)?);
|
||||
work.subsets_written.fetch_add(1, Relaxed);
|
||||
if subset.program.len() <= MAX_SUBSET_PROGRAM_BYTES {
|
||||
if subsets.len() == MAX_SUBSETS {
|
||||
subsets.pop_front();
|
||||
}
|
||||
subsets.push_back(CachedSubset {
|
||||
version: version.clone(),
|
||||
transform,
|
||||
font: font.clone(),
|
||||
glyphs: glyphs.clone(),
|
||||
all_glyphs,
|
||||
tag: tag.into(),
|
||||
subset: subset.clone(),
|
||||
});
|
||||
}
|
||||
Ok(subset)
|
||||
}
|
||||
}
|
||||
@@ -2658,7 +2658,8 @@ fn write_pdf(
|
||||
|| native_type1_parameters(program, owner_paint, owner.metrics()), glyphs);
|
||||
}
|
||||
let omit_charset = unicode.is_some_and(|options| options.omit_charset);
|
||||
let type1_references = type1_plans.write(&mut objects, compression, omit_charset, |key| {
|
||||
let font_cache = unicode.and_then(|options| options.font_cache);
|
||||
let type1_references = type1_plans.write(&mut objects, compression, omit_charset, font_cache, |key| {
|
||||
let (descriptor, name) = registry?.imported_type1_slots(key)?;
|
||||
let physical = |number| device_objects[&number] as usize;
|
||||
Some((physical(descriptor), name.map(physical)))
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Document-wide physical Type 1 embeddings for TeX glyphs and included PDFs.
|
||||
use super::{native_pdf_name, objects};
|
||||
use crate::{
|
||||
native_font::Type1ProgramKey,
|
||||
native_font::{NativeFontCache, Type1ProgramKey},
|
||||
pdf_image::ImportedType1Use,
|
||||
type1::Type1Font,
|
||||
type1_outline::{subset_tag, write_subset, write_type1c},
|
||||
@@ -9,6 +9,7 @@ use crate::{
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet, HashMap},
|
||||
fmt::Write as _,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
pub(crate) struct Reference {
|
||||
@@ -85,6 +86,7 @@ impl<'a> FontPlans<'a> {
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
compression: u8,
|
||||
omit_charset: bool,
|
||||
font_cache: Option<&NativeFontCache>,
|
||||
reserved: impl Fn(Type1ProgramKey<'a>) -> Option<(usize, Option<usize>)>,
|
||||
) -> Result<References<'a>, String> {
|
||||
let mut plans: Vec<_> = self.0.into_iter().collect();
|
||||
@@ -107,7 +109,11 @@ impl<'a> FontPlans<'a> {
|
||||
.map(|salt| subset_tag(&plan.font.name, &plan.glyphs, salt))
|
||||
.find(|tag| tags.insert(tag.clone()))
|
||||
.ok_or("Type1 subset name collision limit exceeded")?;
|
||||
let subset = write_subset(plan.font, &plan.glyphs, plan.all_glyphs, &tag)?;
|
||||
let subset = if let Some(cache) = font_cache {
|
||||
cache.type1_subset(key, plan.font, &plan.glyphs, plan.all_glyphs, &tag)?
|
||||
} else {
|
||||
Arc::new(write_subset(plan.font, &plan.glyphs, plan.all_glyphs, &tag)?)
|
||||
};
|
||||
let lengths = [subset.length1, subset.program.len() - subset.length1, 0];
|
||||
Reference {
|
||||
object: write_embedding(
|
||||
@@ -120,7 +126,7 @@ impl<'a> FontPlans<'a> {
|
||||
compression,
|
||||
slots,
|
||||
),
|
||||
name: subset.name,
|
||||
name: subset.name.clone(),
|
||||
name_object,
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -139,6 +139,8 @@ pub struct PdfFinalOptions<'a> {
|
||||
pub objects: Option<&'a crate::pdf_device::page_resources::PdfObjects>,
|
||||
pub info: crate::pdf_device::PdfInfo<'a>,
|
||||
pub xdv_glyph_names: Option<&'a XdvGlyphNames>,
|
||||
/// Reuse physical font output after current-job resources were resolved.
|
||||
pub font_cache: Option<&'a crate::native_font::NativeFontCache>,
|
||||
pub(crate) xdv_font_unicode: bool,
|
||||
pub(crate) xdv_keep_destinations: bool,
|
||||
pub(crate) xdv_outline_open_depth: usize,
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::collections::BTreeMap;
|
||||
mod subset;
|
||||
pub(crate) mod cff;
|
||||
pub(crate) use cff::write_type1c;
|
||||
pub(crate) use subset::{subset_tag, write_subset};
|
||||
pub(crate) use subset::{SubsetProgram, subset_tag, write_subset};
|
||||
use subset::{GlyphUse, ProgramRanges, SubrRange};
|
||||
fn decrypt(data: &[u8], mut key: u16, skip: usize) -> Result<Vec<u8>, String> {
|
||||
if data.len() < skip {
|
||||
|
||||
Reference in new issue
Block a user