Render and embed OpenType CFF fonts selected by pdfTeX maps
This commit is contained in:
1 parent
46ef76f890
commit
fd5ab59f42
11 files changed
+433
-57
No files matched your search
@@ -0,0 +1,85 @@
|
||||
use super::{native_pdf_name, objects, write_sfnt_descriptor};
|
||||
use crate::{
|
||||
native_font::{NativeFontPaint, OpenTypeCffProgram},
|
||||
resources::ResourceVersion,
|
||||
tfm::TfmMetrics,
|
||||
};
|
||||
use rustybuzz::ttf_parser::Tag;
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap},
|
||||
fmt::Write,
|
||||
};
|
||||
|
||||
pub(super) fn write(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
metrics: &TfmMetrics,
|
||||
paint: &NativeFontPaint,
|
||||
program: &OpenTypeCffProgram,
|
||||
compression: u8,
|
||||
) -> Result<(usize, String), String> {
|
||||
let face = program.face()?;
|
||||
let data = face
|
||||
.raw_face()
|
||||
.table(Tag::from_bytes(b"CFF "))
|
||||
.ok_or("missing CFF data in mapped OpenType font")?;
|
||||
let name = native_pdf_name(&program.name);
|
||||
let font_file = objects.len() + 1;
|
||||
objects.push(objects::stream("/Subtype /Type1C", data, compression));
|
||||
let descriptor = write_sfnt_descriptor(
|
||||
objects,
|
||||
metrics,
|
||||
&face,
|
||||
paint,
|
||||
&name,
|
||||
("FontFile3", font_file),
|
||||
);
|
||||
Ok((descriptor, name))
|
||||
}
|
||||
|
||||
struct Encoding<'a> {
|
||||
object: usize,
|
||||
names: BTreeMap<u8, &'a str>,
|
||||
}
|
||||
|
||||
/// A single encoding read is shared by all mapped CFF fonts that use it.
|
||||
#[derive(Default)]
|
||||
pub(super) struct Encodings<'a>(HashMap<&'a ResourceVersion, Encoding<'a>>);
|
||||
|
||||
impl<'a> Encodings<'a> {
|
||||
pub(super) fn mark(
|
||||
&mut self,
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
paint: &'a NativeFontPaint,
|
||||
slots: impl Iterator<Item = u8>,
|
||||
) -> Option<usize> {
|
||||
let version = paint.encoding_version()?;
|
||||
let encoding = self.0.entry(version).or_insert_with(|| {
|
||||
objects.push(Vec::new());
|
||||
Encoding {
|
||||
object: objects.len(),
|
||||
names: BTreeMap::new(),
|
||||
}
|
||||
});
|
||||
for slot in slots {
|
||||
let name = paint.encoding[usize::from(slot)].as_str();
|
||||
encoding.names.insert(slot, name);
|
||||
}
|
||||
Some(encoding.object)
|
||||
}
|
||||
|
||||
pub(super) fn write(self, objects: &mut [Vec<u8>]) {
|
||||
for encoding in self.0.into_values() {
|
||||
let mut value = "<< /Type /Encoding /Differences [".to_owned();
|
||||
let mut next = None;
|
||||
for (slot, name) in encoding.names {
|
||||
if next != Some(slot) {
|
||||
write!(value, " {slot}").unwrap();
|
||||
}
|
||||
write!(value, "/{}", native_pdf_name(name)).unwrap();
|
||||
next = slot.checked_add(1);
|
||||
}
|
||||
value.push_str("] >>");
|
||||
objects[encoding.object - 1] = value.into_bytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
use super::{native_font_units, native_pdf_font_size, native_pdf_name, objects};
|
||||
use super::{native_pdf_name, objects};
|
||||
use crate::{
|
||||
native_font::{NativeFontPaint, TrueTypeProgram},
|
||||
tfm::TfmMetrics,
|
||||
type1_outline::subset_tag,
|
||||
};
|
||||
use rustybuzz::ttf_parser::Tag;
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
pub(super) fn write(
|
||||
@@ -38,23 +37,6 @@ pub(super) fn write(
|
||||
};
|
||||
let data = program.embed(slots.into_iter(), paint.subset)?;
|
||||
let face = program.face()?;
|
||||
let in_pdf_units = |value: i16| 1000 * i32::from(value) / i32::from(face.units_per_em());
|
||||
let ascent = in_pdf_units(face.tables().hhea.ascender);
|
||||
let descent = in_pdf_units(face.tables().hhea.descender);
|
||||
let pclt = face.raw_face().table(Tag::from_bytes(b"PCLT"));
|
||||
let pclt_height = |offset| {
|
||||
let bytes = pclt?.get(offset..offset + 2)?;
|
||||
Some(in_pdf_units(i16::from_be_bytes(bytes.try_into().ok()?)))
|
||||
};
|
||||
let size = native_pdf_font_size(metrics.size_sp());
|
||||
let cap = pclt_height(16)
|
||||
.unwrap_or_else(|| native_font_units(metrics.height('H'), size, 1000) as i32);
|
||||
let x_height =
|
||||
pclt_height(10).unwrap_or_else(|| native_font_units(metrics.x_height(), size, 1000) as i32);
|
||||
let stem = native_font_units(metrics.width('.') / 3, size, 1000);
|
||||
let angle = face.italic_angle().floor() as i32;
|
||||
let [left, bottom, right, top] = paint.bbox;
|
||||
let flags = paint.descriptor_flags.unwrap_or(4);
|
||||
|
||||
let font_file = objects.len() + 1;
|
||||
objects.push(objects::stream(
|
||||
@@ -62,9 +44,13 @@ pub(super) fn write(
|
||||
&data,
|
||||
compression,
|
||||
));
|
||||
let descriptor = objects.len() + 1;
|
||||
objects.push(format!(
|
||||
"<< /Type /FontDescriptor /FontName /{name} /Flags {flags} /FontBBox [{left} {bottom} {right} {top}] /Ascent {ascent} /Descent {descent} /CapHeight {cap} /ItalicAngle {angle} /StemV {stem} /XHeight {x_height} /FontFile2 {font_file} 0 R >>"
|
||||
).into_bytes());
|
||||
let descriptor = super::write_sfnt_descriptor(
|
||||
objects,
|
||||
metrics,
|
||||
&face,
|
||||
paint,
|
||||
&name,
|
||||
("FontFile2", font_file),
|
||||
);
|
||||
Ok((descriptor, name))
|
||||
}
|
||||
Reference in new issue
Block a user