86 lines
2.5 KiB
Rust
86 lines
2.5 KiB
Rust
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>,
|
|
}
|
|
|
|
/// Fonts using the same encoding resource share its PDF encoding dictionary.
|
|
#[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();
|
|
}
|
|
}
|
|
}
|