Share size-independent XDV Type 1 font dictionaries
This commit is contained in:
1 parent
01c6f1d4bd
commit
e72104eab3
4 files changed
+147
-11
No files matched your search
@@ -732,6 +732,7 @@ fn page_content(
|
||||
cm_codes: &[Option<&BTreeMap<char, u8>>],
|
||||
fallback_ids: &[usize; FACE_COUNT],
|
||||
origin: PageOrigin,
|
||||
xdv_font_aliases: &BTreeMap<crate::execution::FontId, crate::execution::FontId>,
|
||||
opentype: &BTreeMap<crate::execution::FontId, Arc<opentype_fonts::EmbeddedFont>>,
|
||||
font_opacities: &BTreeMap<u8, String>,
|
||||
) -> Vec<u8> {
|
||||
@@ -870,6 +871,7 @@ fn page_content(
|
||||
*x,
|
||||
*y,
|
||||
font,
|
||||
xdv_font_aliases.get(&font.id).copied().unwrap_or(font.id),
|
||||
paint.as_ref().unwrap(),
|
||||
*slot,
|
||||
height,
|
||||
@@ -2440,10 +2442,20 @@ fn write_pdf(
|
||||
}
|
||||
}
|
||||
|
||||
let xdv_font_aliases: BTreeMap<_, _> = {
|
||||
let mut owners = HashMap::new();
|
||||
native_used.iter().filter_map(|(&id, (_, paint, _))| {
|
||||
paint.xdv_descriptor?;
|
||||
let key = (paint.type1_program_key()?, paint.encoding_version());
|
||||
let owner = *owners.entry(key).or_insert(id);
|
||||
(owner != id).then_some((id, owner))
|
||||
}).collect()
|
||||
};
|
||||
let mut aliases = BTreeMap::new();
|
||||
let mut grouped_fonts = BTreeMap::new();
|
||||
for (id, (face, paint, used)) in native_used {
|
||||
let owner = pdf_font_owner(registry, id);
|
||||
let owner = xdv_font_aliases.get(&id).copied()
|
||||
.unwrap_or_else(|| pdf_font_owner(registry, id));
|
||||
aliases.insert(id, owner);
|
||||
let entry = grouped_fonts.entry(owner).or_insert_with(|| {
|
||||
let face = if owner == id {
|
||||
@@ -2454,10 +2466,29 @@ fn write_pdf(
|
||||
};
|
||||
(face, paint.clone(), BTreeMap::new())
|
||||
});
|
||||
if entry.1.encoding != paint.encoding
|
||||
|| entry.1.type1_program_key() != paint.type1_program_key()
|
||||
{
|
||||
return Err("shared PDF font has inconsistent output encoding or program".into());
|
||||
if !Arc::ptr_eq(&entry.1, &paint) {
|
||||
if entry.1.encoding != paint.encoding
|
||||
|| entry.1.type1_program_key() != paint.type1_program_key()
|
||||
{
|
||||
return Err("shared PDF font has inconsistent output encoding or program".into());
|
||||
}
|
||||
if xdv_font_aliases.contains_key(&id) {
|
||||
// Separate map entries can resolve to the same physical
|
||||
// program with disjoint sets of measured glyph widths.
|
||||
for slot in used.keys() {
|
||||
let width = *paint.xdv_widths.get(slot)
|
||||
.ok_or("shared XDV Type 1 font is missing a glyph width")?;
|
||||
match entry.1.xdv_widths.get(slot) {
|
||||
Some(old) if *old != width => {
|
||||
return Err("shared XDV Type 1 font has inconsistent glyph widths".into());
|
||||
}
|
||||
Some(_) => {}
|
||||
None => {
|
||||
Arc::make_mut(&mut entry.1).xdv_widths.insert(*slot, width);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
entry.2.extend(used);
|
||||
}
|
||||
@@ -2893,7 +2924,9 @@ fn write_pdf(
|
||||
fonts.push_str(&format!(" /M{} {id} 0 R", face.0));
|
||||
}
|
||||
let native_fonts: BTreeSet<_> = list.items.iter().filter_map(|item| match item {
|
||||
DisplayItem::NativeGlyph { font, .. } => Some(font.id),
|
||||
DisplayItem::NativeGlyph { font, .. } => {
|
||||
Some(xdv_font_aliases.get(&font.id).copied().unwrap_or(font.id))
|
||||
}
|
||||
_ => None,
|
||||
}).chain(list.pdf_space_plan.iter().filter_map(|plan| {
|
||||
plan.dedicated.as_ref().map(|font| font.font.id)
|
||||
@@ -2950,7 +2983,7 @@ fn write_pdf(
|
||||
let font_opacities = remapped.as_ref().map_or(&no_font_opacity, |r| &r.font_opacities[slot]);
|
||||
let content = page_content(
|
||||
list, list.height, &names, faces, &cm_views,
|
||||
&fallback_font_ids, PageOrigin::default(), &opentype, font_opacities,
|
||||
&fallback_font_ids, PageOrigin::default(), &xdv_font_aliases, &opentype, font_opacities,
|
||||
);
|
||||
objects[device_objects[&form.form.number] as usize - 1] = objects::stream_bytes(
|
||||
&entries, &content, list.pdf.as_ref().map_or(0, |pdf| pdf.stream_compression),
|
||||
@@ -3128,6 +3161,7 @@ fn write_pdf(
|
||||
&cm_views,
|
||||
&fallback_font_ids,
|
||||
origin,
|
||||
&xdv_font_aliases,
|
||||
&opentype,
|
||||
font_opacities,
|
||||
);
|
||||
|
||||
@@ -29,6 +29,7 @@ impl NativeText {
|
||||
x: i64,
|
||||
y: i64,
|
||||
font: &NativeFont,
|
||||
resource: FontId,
|
||||
paint: &crate::native_font::NativeFontPaint,
|
||||
slot: u8,
|
||||
height: i64,
|
||||
@@ -66,10 +67,10 @@ impl NativeText {
|
||||
}
|
||||
if space == Some(SpaceUse::Dedicated) {
|
||||
let dedicated = dedicated.expect("resolved PDF space font");
|
||||
state.select_font(out, &dedicated.font);
|
||||
state.select_font(out, &dedicated.font, dedicated.font.id);
|
||||
out.push_str("( ) Tj\n");
|
||||
}
|
||||
state.select_font(out, font);
|
||||
state.select_font(out, font, resource);
|
||||
let expansion_changed = state.expansion != font.expansion;
|
||||
if state.xdv && expansion_changed {
|
||||
state.close_string(out);
|
||||
@@ -139,7 +140,7 @@ impl NativeText {
|
||||
state.delta_h += state.char_advance(font, paint, slot, state.effective_size);
|
||||
}
|
||||
|
||||
fn select_font(&mut self, out: &mut PageContent, font: &NativeFont) {
|
||||
fn select_font(&mut self, out: &mut PageContent, font: &NativeFont, resource: FontId) {
|
||||
if self.font != Some(font.id) {
|
||||
self.close_string(out);
|
||||
let (number, size) = divide_scaled(font.size_sp(), 6_578_176, 6);
|
||||
@@ -148,7 +149,7 @@ impl NativeText {
|
||||
if !out.measuring {
|
||||
out.push_str(&format!(
|
||||
"/N{} {} Tf\n",
|
||||
font.id.index(),
|
||||
resource.index(),
|
||||
print_decimal(number, 4)
|
||||
));
|
||||
}
|
||||
@@ -239,6 +240,7 @@ impl NativeText {
|
||||
x,
|
||||
y,
|
||||
&dedicated.font,
|
||||
dedicated.font.id,
|
||||
&dedicated.paint,
|
||||
b' ',
|
||||
height,
|
||||
@@ -425,6 +427,7 @@ pub(crate) fn plan_spaces(
|
||||
*x,
|
||||
*y,
|
||||
font,
|
||||
font.id,
|
||||
paint,
|
||||
*slot,
|
||||
page.height,
|
||||
|
||||
Reference in new issue
Block a user