M13.12: EPS text (show variants, charpath, stringwidth, embedded Type 1/CFF, fallback fonts)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-03 09:01:10 -04:00
co-authored by Claude Opus 5.5
parent d0df520bc1
commit 740faf1c1f
5 changed files with 530 additions and 64 deletions
+40 -6
View File
@@ -311,11 +311,9 @@ fn parse_cmap(data: &[u8]) -> CMap {
impl Font {
/// Read a font dictionary. `skipped` collects what could not be read.
pub fn load(file: &File, d: &Dict, skipped: &mut Vec<String>) -> Font {
let subtype = file.get(d, "Subtype").and_then(Obj::name).unwrap_or("Type1").to_string();
let base_name = file.get(d, "BaseFont").and_then(Obj::name).unwrap_or("").to_string();
let mut f = Font {
composite: subtype == "Type0",
fn blank(base_name: &str, composite: bool) -> Font {
Font {
composite,
program: Program::None,
matrix: Affine::scale(0.001),
first_char: 0,
@@ -333,8 +331,44 @@ impl Font {
dw2: (0.88, -1.0),
cache: RefCell::new(HashMap::new()),
has_widths: false,
base_name: base_name.clone(),
base_name: base_name.to_string(),
}
}
/// A simple font outside a PDF (EPS text): `program` is an embedded font program (CFF when
/// the flag is set, else Type 1), otherwise the bundled stand-in for `name` is used;
/// `names` is the encoding (code → glyph name), else the program's built-in encoding or
/// StandardEncoding. Advances are the program's.
pub fn standalone(name: &str, program: Option<(&[u8], bool)>, names: Option<Vec<Option<String>>>) -> Font {
let mut f = Font::blank(name, false);
f.program = match program {
Some((data, true)) => Cff::parse(data).map(|c| Program::Cff(Box::new(c))),
Some((data, false)) => Type1::parse(data).map(|t| Program::Type1(Box::new(t))),
None => None,
}
.unwrap_or_else(|| Program::Fallback(fallback(name)));
match &f.program {
Program::Cff(c) => f.matrix = Affine::new(c.font_matrix),
Program::Type1(t) => f.matrix = t.font_matrix.map(Affine::new).unwrap_or(f.matrix),
_ => {}
}
let symbol = matches!(f.program, Program::Fallback(_)) && (name.contains("Symbol") || name.contains("Dingbats"));
f.names = match names {
// Unencoded codes draw .notdef (not the built-in encoding).
Some(n) => (0..256).map(|c| Some(n.get(c).cloned().flatten().unwrap_or_else(|| ".notdef".into()))).collect(),
None => match &f.program {
Program::Type1(t) if !t.encoding.is_empty() => (0..256).map(|c| t.encoding.get(&(c as u8)).cloned()).collect(),
_ if symbol => vec![None; 256],
_ => (0..256).map(|c| Base::Standard.name(c as u8)).collect(),
},
};
f
}
pub fn load(file: &File, d: &Dict, skipped: &mut Vec<String>) -> Font {
let subtype = file.get(d, "Subtype").and_then(Obj::name).unwrap_or("Type1").to_string();
let base_name = file.get(d, "BaseFont").and_then(Obj::name).unwrap_or("").to_string();
let mut f = Font::blank(&base_name, subtype == "Type0");
if let Some(Obj::Stream(sd, raw)) = file.get(d, "ToUnicode")
&& let Some(data) = decode_stream(file, sd, raw)
{