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,106 @@
|
||||
//! Resolve an eight-bit TeX font mapped to a name-keyed OpenType CFF face.
|
||||
use super::{MappedOutline, NativeFont, NativeFontPaint, NativeFontProgram, map::MapEntry};
|
||||
use crate::{execution::SourceProvider, resources::ResourceKind};
|
||||
use read_fonts::ps::cff::CffFontRef;
|
||||
use rustybuzz::ttf_parser::{Face, GlyphId, Tag};
|
||||
use std::{collections::BTreeSet, sync::Arc};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct OpenTypeCffProgram {
|
||||
pub(crate) name: String,
|
||||
data: Arc<[u8]>,
|
||||
}
|
||||
|
||||
impl OpenTypeCffProgram {
|
||||
pub(crate) fn face(&self) -> Result<Face<'_>, String> {
|
||||
Face::parse(&self.data, 0)
|
||||
.map_err(|error| format!("invalid OpenType font {}: {error}", self.name))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn resolve(
|
||||
files: &mut impl SourceProvider,
|
||||
outline: &MapEntry,
|
||||
file: &str,
|
||||
font: &NativeFont,
|
||||
slots: BTreeSet<u8>,
|
||||
) -> Result<NativeFontPaint, String> {
|
||||
if outline.subset {
|
||||
return Err(format!("OTF fonts must be included entirely: {file}"));
|
||||
}
|
||||
let MappedOutline {
|
||||
data,
|
||||
encoding,
|
||||
resources,
|
||||
program_index,
|
||||
} = outline.read_outline(files, ResourceKind::OpenType, file)?;
|
||||
let face =
|
||||
Face::parse(&data, 0).map_err(|error| format!("invalid OpenType font {file}: {error}"))?;
|
||||
let table = face
|
||||
.raw_face()
|
||||
.table(Tag::from_bytes(b"CFF "))
|
||||
.ok_or_else(|| format!("OpenType font has no CFF table: {file}"))?;
|
||||
let cff = CffFontRef::new_cff(table, 0, None)
|
||||
.map_err(|error| format!("invalid CFF program {file}: {error:?}"))?;
|
||||
if cff.is_cid() {
|
||||
return Err(format!(
|
||||
"an eight-bit OpenType map needs a name-keyed CFF font: {file}"
|
||||
));
|
||||
}
|
||||
|
||||
let cff = face
|
||||
.tables()
|
||||
.cff
|
||||
.ok_or_else(|| format!("invalid CFF outlines: {file}"))?;
|
||||
let encoded_glyph = |slot| {
|
||||
if let Some(names) = &encoding {
|
||||
cff.glyph_index_by_name(&names[usize::from(slot)])
|
||||
} else {
|
||||
cff.glyph_index(slot)
|
||||
}
|
||||
.unwrap_or(GlyphId(0))
|
||||
};
|
||||
let matrix = cff.matrix();
|
||||
let scale = [
|
||||
matrix.sx, matrix.ky, matrix.kx, matrix.sy, matrix.tx, matrix.ty,
|
||||
]
|
||||
.map(|number| f64::from(number * 1000.));
|
||||
let glyph_paths = slots
|
||||
.into_iter()
|
||||
.map(|slot| {
|
||||
(
|
||||
slot,
|
||||
crate::opentype::outline_matrix(&face, encoded_glyph(slot).0, Some(scale)),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let encoding = encoding.unwrap_or_else(|| {
|
||||
(0..=255)
|
||||
.map(|slot| {
|
||||
cff.glyph_index(slot)
|
||||
.and_then(|glyph| cff.glyph_name(glyph))
|
||||
.unwrap_or(".notdef")
|
||||
.to_owned()
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
let bbox = face.global_bounding_box();
|
||||
let bbox = [bbox.x_min, bbox.y_min, bbox.x_max, bbox.y_max]
|
||||
.map(|value| 1000 * i32::from(value) / i32::from(face.units_per_em()));
|
||||
let program = OpenTypeCffProgram {
|
||||
name: outline.postscript_name().unwrap_or(&font.name).to_owned(),
|
||||
data: data.into(),
|
||||
};
|
||||
Ok(NativeFontPaint {
|
||||
program: NativeFontProgram::OpenTypeCff(program),
|
||||
encoding,
|
||||
glyph_paths,
|
||||
bbox,
|
||||
stem_v: 0,
|
||||
subset: false,
|
||||
descriptor_flags: outline.descriptor_flags,
|
||||
xdv_descriptor: None,
|
||||
resources,
|
||||
program_index: Some(program_index),
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user