Render legacy XeLaTeX composite Type 1 fonts
This commit is contained in:
1 parent
ff52a9f66b
commit
e7ab132520
11 files changed
+497
-116
No files matched your search
@@ -8,7 +8,7 @@ use std::collections::BTreeMap;
|
||||
|
||||
use crate::{
|
||||
native_font::{NativeFontPaint, OpenTypeCffProgram},
|
||||
type1_outline::cff::{dict_long, dict_number, index},
|
||||
type1_outline::cff::{CidCff, index},
|
||||
};
|
||||
|
||||
pub(super) fn subset(
|
||||
@@ -56,20 +56,12 @@ pub(super) fn subset(
|
||||
charstrings.ok_or("missing CFF charstrings")?
|
||||
)?))
|
||||
.map_err(|_| invalid())?;
|
||||
let mut programs = vec![source_programs.get(0).ok_or("missing CFF .notdef")?];
|
||||
let mut charset = vec![0]; // Format 0. Each entry is the actual CID, in CharStrings order.
|
||||
let mut programs = vec![(0, source_programs.get(0).ok_or("missing CFF .notdef")?)];
|
||||
for (&cid, &glyph) in glyphs {
|
||||
if cid != 0 {
|
||||
charset.extend(cid.to_be_bytes());
|
||||
programs.push(
|
||||
source_programs
|
||||
.get(usize::from(glyph))
|
||||
.ok_or_else(invalid)?,
|
||||
);
|
||||
programs.push((cid, source_programs.get(usize::from(glyph)).ok_or_else(invalid)?));
|
||||
}
|
||||
}
|
||||
let count = u16::try_from(programs.len()).map_err(|_| "too many glyphs for CFF1")?;
|
||||
let programs = index(&programs)?;
|
||||
let mut private_bytes = Vec::new();
|
||||
let mut private_size = 0;
|
||||
if let Some(range) = private {
|
||||
@@ -98,78 +90,18 @@ pub(super) fn subset(
|
||||
.map(|i| globals.get(i).ok_or_else(invalid))
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
)?;
|
||||
let name = index(&[name])?;
|
||||
let strings = index(&[mapping.registry.as_bytes(), mapping.ordering.as_bytes()])?;
|
||||
let fd_select = vec![0; usize::from(count) + 1];
|
||||
let long = |out: &mut Vec<u8>, value: usize| -> Result<(), String> {
|
||||
dict_long(
|
||||
out,
|
||||
i32::try_from(value).map_err(|_| "CFF offset overflow")?,
|
||||
);
|
||||
Ok(())
|
||||
};
|
||||
let font_dict = |private_offset| -> Result<Vec<u8>, String> {
|
||||
let mut dict = Vec::new();
|
||||
long(&mut dict, private_size)?;
|
||||
long(&mut dict, private_offset)?;
|
||||
dict.push(18);
|
||||
index(&[dict])
|
||||
};
|
||||
let mut attributes = Vec::new();
|
||||
// CFF1 reserves SIDs 0 through 390 for standard strings.
|
||||
long(&mut attributes, 391)?;
|
||||
long(&mut attributes, 392)?;
|
||||
long(&mut attributes, mapping.supplement as usize)?;
|
||||
attributes.extend([12, 30]); // ROS
|
||||
let bbox = face.global_bounding_box();
|
||||
for value in [bbox.x_min, bbox.y_min, bbox.x_max, bbox.y_max] {
|
||||
dict_number(&mut attributes, f64::from(value))?;
|
||||
}
|
||||
attributes.push(5);
|
||||
let matrix = face.tables().cff.ok_or("missing CFF outlines")?.matrix();
|
||||
for value in [
|
||||
matrix.sx, matrix.ky, matrix.kx, matrix.sy, matrix.tx, matrix.ty,
|
||||
] {
|
||||
dict_number(&mut attributes, f64::from(value))?;
|
||||
CidCff {
|
||||
registry: &mapping.registry,
|
||||
ordering: &mapping.ordering,
|
||||
supplement: mapping.supplement,
|
||||
bbox: [bbox.x_min, bbox.y_min, bbox.x_max, bbox.y_max].map(f64::from),
|
||||
matrix: [matrix.sx, matrix.ky, matrix.kx, matrix.sy, matrix.tx, matrix.ty].map(f64::from),
|
||||
programs: &programs,
|
||||
globals: &globals,
|
||||
private: &private_bytes,
|
||||
private_size,
|
||||
}
|
||||
attributes.extend([12, 7]); // FontMatrix
|
||||
long(
|
||||
&mut attributes,
|
||||
usize::from(glyphs.last_key_value().map_or(0, |(&cid, _)| cid)) + 1,
|
||||
)?;
|
||||
attributes.extend([12, 34]); // CIDCount
|
||||
let top = |base| -> Result<Vec<u8>, String> {
|
||||
let programs_offset = base + charset.len();
|
||||
let select_offset = programs_offset + programs.len();
|
||||
let fd_offset = select_offset + fd_select.len();
|
||||
let mut top = attributes.clone();
|
||||
for (offset, opcode) in [
|
||||
(base, &[15][..]),
|
||||
(programs_offset, &[17][..]),
|
||||
(select_offset, &[12, 37][..]),
|
||||
(fd_offset, &[12, 36][..]),
|
||||
] {
|
||||
long(&mut top, offset)?;
|
||||
top.extend(opcode);
|
||||
}
|
||||
index(&[top])
|
||||
};
|
||||
let offset = 4 + name.len() + top(0)?.len() + strings.len() + globals.len();
|
||||
let private_offset =
|
||||
offset + charset.len() + programs.len() + fd_select.len() + font_dict(0)?.len();
|
||||
let mut output = vec![1, 0, 4, 4];
|
||||
for part in [
|
||||
name,
|
||||
top(offset)?,
|
||||
strings,
|
||||
globals,
|
||||
charset,
|
||||
programs,
|
||||
fd_select,
|
||||
font_dict(private_offset)?,
|
||||
private_bytes,
|
||||
] {
|
||||
output.extend(part);
|
||||
}
|
||||
Ok(output)
|
||||
.write(name)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ use std::collections::BTreeSet;
|
||||
pub(super) fn write(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
paint: &NativeFontPaint,
|
||||
metrics: &crate::tfm::TfmMetrics,
|
||||
slots: impl Iterator<Item = u8>,
|
||||
compression: u8,
|
||||
subset_tags: &mut BTreeSet<String>,
|
||||
@@ -19,21 +20,30 @@ pub(super) fn write(
|
||||
use subsetter::GlyphRemapper;
|
||||
|
||||
let mapping = paint.legacy_cid_font().expect("composite program");
|
||||
let (name, face) = match &paint.program {
|
||||
NativeFontProgram::TrueType(program) => (&program.name, program.face()?),
|
||||
NativeFontProgram::OpenTypeCff(program) => (&program.name, program.face()?),
|
||||
let name = paint.program.name();
|
||||
let face = match &paint.program {
|
||||
NativeFontProgram::TrueType(program) => Some(program.face()?),
|
||||
NativeFontProgram::OpenTypeCff(program) => Some(program.face()?),
|
||||
NativeFontProgram::CompositeType1(_) => None,
|
||||
_ => unreachable!("legacy CID program"),
|
||||
};
|
||||
let mut vertical_widths = BTreeMap::new();
|
||||
let mut glyphs = BTreeMap::new();
|
||||
let mut unicode = BTreeMap::new();
|
||||
let mut advances = BTreeMap::new();
|
||||
let mut type1_names = BTreeMap::new();
|
||||
for slot in slots {
|
||||
let cid = mapping.cid(slot);
|
||||
let glyph = match &paint.program {
|
||||
NativeFontProgram::TrueType(program) => program.glyph_id(slot),
|
||||
NativeFontProgram::OpenTypeCff(program) => program.composite_glyphs[usize::from(slot)],
|
||||
NativeFontProgram::CompositeType1(_) => {
|
||||
type1_names.insert(cid, paint.encoding[usize::from(slot)].as_str());
|
||||
cid
|
||||
}
|
||||
_ => unreachable!("legacy CID program"),
|
||||
};
|
||||
advances.insert(cid, mapping.advance(slot));
|
||||
if let Some(metrics) = mapping.vertical_override(slot) {
|
||||
vertical_widths.insert(cid, metrics);
|
||||
}
|
||||
@@ -61,8 +71,10 @@ pub(super) fn write(
|
||||
let max = usize::from(glyphs.keys().next_back().copied().unwrap_or(0));
|
||||
let mut cid_to_gid = vec![0; (max + 1) * 2];
|
||||
let mut cid_set = vec![0; max / 8 + 1];
|
||||
let width = |glyph| cid_width(&face, glyph);
|
||||
let default = width(0);
|
||||
let default = match &paint.program {
|
||||
NativeFontProgram::CompositeType1(program) => program.default_width,
|
||||
_ => cid_width(face.as_ref().expect("sfnt CID font"), 0),
|
||||
};
|
||||
let mut widths = String::new();
|
||||
let mut previous: Option<u16> = None;
|
||||
let mut identity = true;
|
||||
@@ -74,7 +86,7 @@ pub(super) fn write(
|
||||
cid_set[index / 8] |= 1 << (7 - index % 8);
|
||||
}
|
||||
identity &= cid == subset_id;
|
||||
let advance = width(glyph);
|
||||
let advance = advances[&cid];
|
||||
if advance == default {
|
||||
continue;
|
||||
}
|
||||
@@ -119,7 +131,13 @@ pub(super) fn write(
|
||||
"CIDFontType0",
|
||||
"FontFile3",
|
||||
"/Subtype /CIDFontType0C".to_owned(),
|
||||
super::cid_cff::subset(program, &face, paint, &glyphs, &name)?,
|
||||
super::cid_cff::subset(program, face.as_ref().expect("CFF face"), paint, &glyphs, &name)?,
|
||||
),
|
||||
NativeFontProgram::CompositeType1(program) => (
|
||||
"CIDFontType0",
|
||||
"FontFile3",
|
||||
"/Subtype /CIDFontType0C".to_owned(),
|
||||
crate::type1_outline::cff::write_type1_cid(&program.font, &name, &type1_names)?,
|
||||
),
|
||||
_ => unreachable!("legacy CID program"),
|
||||
};
|
||||
@@ -132,7 +150,15 @@ pub(super) fn write(
|
||||
let cid_set_id = objects.len() + 1;
|
||||
objects.push(objects::stream("", &cid_set, compression));
|
||||
let descriptor = objects.len() + 1;
|
||||
let mut contents = super::opentype_fonts::font_descriptor(&face, &name, file_key, fontfile);
|
||||
let mut contents = match &paint.program {
|
||||
NativeFontProgram::CompositeType1(program) => {
|
||||
let parameters = super::native_type1_parameters(&program.font, paint, metrics);
|
||||
format!("<< /Type /FontDescriptor /FontName /{name} {parameters} /{file_key} {fontfile} 0 R >>").into_bytes()
|
||||
}
|
||||
_ => super::opentype_fonts::font_descriptor(
|
||||
face.as_ref().expect("sfnt CID font"), &name, file_key, fontfile,
|
||||
),
|
||||
};
|
||||
assert!(contents.ends_with(b" >>"));
|
||||
contents.truncate(contents.len() - 3);
|
||||
contents.extend_from_slice(format!(" /CIDSet {cid_set_id} 0 R >>").as_bytes());
|
||||
|
||||
Reference in new issue
Block a user