mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 09:35:43 +00:00
Merge pull request #6871 from dotCooCoo/fix/eps-predefined-cmap-type
EPS import: CIDFonts only named, over a predefined CJK CMap, set their characters as live type
This commit is contained in:
@@ -2682,7 +2682,10 @@ fn arb_ps_token() -> impl Strategy<Value = String> {
|
||||
// Local VM restored: dictionary changes undone, saves restored twice.
|
||||
"/a 1 def save /a 2 def", "restore", "save dup restore", "1 dict save exch /k 1 put", "cachestatus", "/n 0 def save /n n 1 add store",
|
||||
// Executable strings and keys other than names (PLRM 3rd ed., `cvx`, `load`).
|
||||
"(>>) cvx", "(1 2 add) cvx exec", "(x) cvx dup exec", "0 load", "1 { } def", "(mark) cvx cvlit"
|
||||
"(>>) cvx", "(1 2 add) cvx exec", "(x) cvx dup exec", "0 load", "1 { } def", "(mark) cvx cvlit",
|
||||
// CID-keyed fonts found by name over predefined CMaps, horizontal and vertical.
|
||||
"/Ryumin-Light-90ms-RKSJ-H", "/Ryumin-Light-EUC-V", "/Ryumin-Light-H", "/GBT-EUC-H", "/KSCms-UHC-V", "/UniJIS-UTF16-V",
|
||||
"[/Ryumin-Light]", "composefont", "<93fa967b8540fd0d41>", "<c7d1a4a4467c>"
|
||||
])
|
||||
.prop_map(str::to_string),
|
||||
]
|
||||
|
||||
@@ -42,6 +42,7 @@ mod interp;
|
||||
mod lex;
|
||||
mod native;
|
||||
mod obj;
|
||||
mod predefined_cmap;
|
||||
mod preview;
|
||||
mod shading;
|
||||
mod text;
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_pdf::interpret::hayro_cmap::{BfString, CMap, CMapName, load_embedded};
|
||||
use kurbo::{Affine, BezPath, ParamCurve, ParamCurveArclen};
|
||||
use vectorcraft_color::Paint;
|
||||
use vectorcraft_doc::{Burasagari, CharStyle, Justify, NodeKind, ParaStyle, TabAlign, TabStop, TextKind, TextObject, TextRun};
|
||||
use vectorcraft_geom::AnchorKind;
|
||||
|
||||
use super::super::super::predefined_cmap::{self, CMapCodes, composite_font};
|
||||
use super::super::obj::V;
|
||||
use super::art::path_data;
|
||||
use super::{FLIP, GState, HashSet, Kind, Node, NodeId, Reader};
|
||||
@@ -284,259 +284,15 @@ impl TypeState {
|
||||
|
||||
/// How strings in a font with predefined CMap `cmap` decode; `None` when the CMap can't be read.
|
||||
fn cmap_codes(&mut self, cmap: &str) -> Option<Arc<CMapCodes>> {
|
||||
if let Some(form) = unicode_cmap(cmap) {
|
||||
return Some(Arc::new(CMapCodes::Unicode(form)));
|
||||
}
|
||||
if let Some((_, codes)) = self.cmaps.iter().find(|(n, _)| n == cmap) {
|
||||
return codes.clone();
|
||||
}
|
||||
let codes = cid_codes(cmap).map(Arc::new);
|
||||
let codes = predefined_cmap::codes(cmap).map(Arc::new);
|
||||
self.cmaps.push((cmap.to_string(), codes.clone()));
|
||||
codes
|
||||
}
|
||||
}
|
||||
|
||||
/// How a composite font's strings decode.
|
||||
#[derive(Debug)]
|
||||
enum CMapCodes {
|
||||
/// By a predefined CMap's codes to CIDs, then by its character collection's CID → Unicode CMap;
|
||||
/// with `high_bit`, each byte 0x21 to 0x7E gets its high bit set first. `space` is the CMap's
|
||||
/// code space ([`code_space`]).
|
||||
Cids { codes: Box<CMap>, unicode: Box<CMap>, high_bit: bool, space: Option<CodeSpace> },
|
||||
/// A predefined Unicode CMap: the codes are the characters.
|
||||
Unicode(Unicode),
|
||||
}
|
||||
|
||||
/// A CMap's code space: ranges of codes, each given by its lowest and highest code, both with the
|
||||
/// code's length in bytes.
|
||||
type CodeSpace = &'static [(&'static [u8], &'static [u8])];
|
||||
|
||||
/// What control byte `b` is in a composite font's string: `\r` a paragraph break, `\t` a tab.
|
||||
fn control(b: u8) -> Option<char> {
|
||||
match b {
|
||||
b'\r' => Some('\n'),
|
||||
b'\t' => Some('\t'),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl CMapCodes {
|
||||
/// The characters of string `bytes`, leaving out codes the CMaps don't map; a `\r` is a
|
||||
/// paragraph break and a `\t` a tab (also as a byte left over after the last whole code of a
|
||||
/// UTF-16 or UTF-32 string).
|
||||
fn decode(&self, bytes: &[u8]) -> String {
|
||||
let (codes, unicode, high_bit, space) = match self {
|
||||
Self::Unicode(form) => {
|
||||
let unit = match form {
|
||||
Unicode::Utf16 => 2,
|
||||
Unicode::Utf8 => 1,
|
||||
Unicode::Utf32 => 4,
|
||||
};
|
||||
let (whole, rest) = bytes.split_at_checked(bytes.len() - bytes.len() % unit).unwrap_or((bytes, &[]));
|
||||
let mut text = unicode_text(whole, *form).replace('\r', "\n");
|
||||
text.extend(rest.last().copied().and_then(control));
|
||||
return text;
|
||||
}
|
||||
Self::Cids { codes, unicode, high_bit, space } => (codes, unicode, *high_bit, space),
|
||||
};
|
||||
let euc: Vec<u8>;
|
||||
let bytes = if high_bit {
|
||||
euc = bytes.iter().map(|&b| if (0x21..0x7f).contains(&b) { b | 0x80 } else { b }).collect();
|
||||
euc.as_slice()
|
||||
} else {
|
||||
bytes
|
||||
};
|
||||
let mut out = String::new();
|
||||
let mut at = 0;
|
||||
while let Some(&first) = bytes.get(at) {
|
||||
// VectorCraft reads a 0x0D byte as a paragraph break and a 0x09 byte as a tab in every
|
||||
// CMap, as in other fonts' strings, including the row-and-cell CMaps, whose code space
|
||||
// has no one-byte codes.
|
||||
if let Some(control) = control(first) {
|
||||
out.push(control);
|
||||
at += 1;
|
||||
continue;
|
||||
}
|
||||
let rest = bytes.get(at..).unwrap_or_default();
|
||||
let code = |len: u8| rest.get(..usize::from(len)).map(|b| b.iter().fold(0u32, |c, &b| c << 8 | u32::from(b)));
|
||||
let found = match space {
|
||||
// The code is as long as the code space range it falls in; a byte that starts no
|
||||
// code in the code space is skipped.
|
||||
Some(space) => {
|
||||
let Some(len) = code_len(space, rest) else {
|
||||
at += 1;
|
||||
continue;
|
||||
};
|
||||
code(len).map(|c| (len, codes.lookup_cid_code(c, len)))
|
||||
}
|
||||
None => (1..=4u8).find_map(|len| Some((len, Some(codes.lookup_cid_code(code(len)?, len)?)))),
|
||||
};
|
||||
let Some((len, cid)) = found else {
|
||||
at += 1;
|
||||
continue;
|
||||
};
|
||||
at += usize::from(len);
|
||||
// A code the CMap leaves out is dropped.
|
||||
let Some(cid) = cid else { continue };
|
||||
match unicode.lookup_bf_string(cid) {
|
||||
// A one-byte Roman code whose CID the table gives as a combining mark alone (0x7E in
|
||||
// the Mac Shift-JIS CMaps, U+0303): the ASCII character, as Adobe's cid2code.txt lists first.
|
||||
Some(BfString::Char('\u{300}'..='\u{36f}')) if len == 1 && (0x20..0x7f).contains(&first) => out.push(char::from(first)),
|
||||
Some(BfString::Char(c)) if c != char::REPLACEMENT_CHARACTER => out.push(c),
|
||||
Some(BfString::String(s)) => out.extend(s.chars().filter(|c| *c != char::REPLACEMENT_CHARACTER)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// How a predefined Unicode CMap's codes encode their characters.
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
enum Unicode {
|
||||
/// Two bytes a character (`UCS2`) or a UTF-16 surrogate pair, the high byte first.
|
||||
Utf16,
|
||||
Utf8,
|
||||
/// Four bytes, the high byte first.
|
||||
Utf32,
|
||||
}
|
||||
|
||||
/// The encoding of predefined Unicode CMap `name`: `Uni` and a character collection, then `-UCS2`,
|
||||
/// `-UTF8`, `-UTF16` or `-UTF32` (and `-HW` for half-width forms), then `-H` or `-V`
|
||||
/// (`UniJIS-UCS2-H`, `UniGB-UTF16-V`: <https://github.com/adobe-type-tools/cmap-resources>).
|
||||
fn unicode_cmap(name: &str) -> Option<Unicode> {
|
||||
let rest = name.strip_suffix("-H").or_else(|| name.strip_suffix("-V"))?;
|
||||
let rest = rest.strip_suffix("-HW").unwrap_or(rest);
|
||||
let (collection, form) = rest.rsplit_once('-')?;
|
||||
let collections = ["UniJIS", "UniJISPro", "UniJIS2004", "UniJISX0213", "UniJISX02132004", "UniHojo", "UniGB", "UniCNS", "UniKS", "UniAKR"];
|
||||
if !collections.contains(&collection) {
|
||||
return None;
|
||||
}
|
||||
match form {
|
||||
"UCS2" | "UTF16" => Some(Unicode::Utf16),
|
||||
"UTF8" => Some(Unicode::Utf8),
|
||||
"UTF32" => Some(Unicode::Utf32),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The characters string `s` encodes in Unicode form `form` (invalid codes left out).
|
||||
fn unicode_text(s: &[u8], form: Unicode) -> String {
|
||||
match form {
|
||||
Unicode::Utf16 => {
|
||||
let units = s.as_chunks::<2>().0.iter().map(|b| u16::from_be_bytes(*b));
|
||||
char::decode_utf16(units).filter_map(Result::ok).collect()
|
||||
}
|
||||
Unicode::Utf8 => s.utf8_chunks().map(|chunk| chunk.valid()).collect(),
|
||||
Unicode::Utf32 => s.as_chunks::<4>().0.iter().filter_map(|b| char::from_u32(u32::from_be_bytes(*b))).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Predefined CMap `name` among those VectorCraft carries, leaving out the Identity CMaps, whose
|
||||
/// codes are CIDs of no known character collection.
|
||||
fn predefined(name: &str) -> Option<CMapName<'_>> {
|
||||
match CMapName::from_bytes(name.as_bytes()) {
|
||||
CMapName::Custom(_) | CMapName::IdentityH | CMapName::IdentityV => None,
|
||||
name => Some(name),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adobe's predefined CMaps that VectorCraft doesn't carry, by name without `-H` or `-V`
|
||||
/// (<https://github.com/adobe-type-tools/cmap-resources>): the nearest carried CMap with the same
|
||||
/// code space, and whether each byte of a row-and-cell code gets its high bit set first (the EUC
|
||||
/// form of the same code; `GB-H`, `CNS1-H` and `KSC-H` have no one-byte codes, their code space
|
||||
/// is `<2121>` to `<7E7E>`); `None` for those no carried CMap reads.
|
||||
fn stand_in(stem: &str) -> Option<Option<(&'static str, bool)>> {
|
||||
Some(match stem {
|
||||
"RKSJ" | "78-RKSJ" | "78ms-RKSJ" => Some(("90ms-RKSJ-H", false)),
|
||||
"78-EUC" => Some(("EUC-H", false)),
|
||||
"78" | "Add" | "Ext" | "NWP" => Some(("H", false)),
|
||||
"GB" => Some(("GB-EUC-H", true)),
|
||||
"CNS1" => Some(("CNS-EUC-H", true)),
|
||||
"KSC" => Some(("KSC-EUC-H", true)),
|
||||
"B5" => Some(("ETen-B5-H", false)),
|
||||
"ETHK-B5" | "HKdla-B5" | "HKdlb-B5" | "HKgccs-B5" | "HKm314-B5" | "HKm471-B5" => Some(("HKscs-B5-H", false)),
|
||||
"GBT" | "GBT-EUC" | "GBTpc-EUC" | "CNS2" | "KSC-Johab" => None,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Predefined CMap `cmap`'s codes to CIDs, and its character collection's CID → Unicode CMap. A
|
||||
/// vertical CMap's codes are read with its horizontal CMap when VectorCraft carries that: the two
|
||||
/// differ only in the CIDs of vertical glyph forms. A CMap VectorCraft doesn't carry is read with
|
||||
/// its [`stand_in`].
|
||||
fn cid_codes(cmap: &str) -> Option<CMapCodes> {
|
||||
let read = |name: &str| CMap::parse(load_embedded(predefined(name)?)?, load_embedded);
|
||||
let horizontal = cmap.strip_suffix('V').map(|stem| format!("{stem}H"));
|
||||
let stem = cmap.strip_suffix("-H").or_else(|| cmap.strip_suffix("-V"));
|
||||
let (codes, high_bit) = horizontal.iter().map(String::as_str).chain([cmap]).find_map(|n| Some((read(n)?, false))).or_else(|| {
|
||||
let (name, high_bit) = stand_in(stem?).flatten()?;
|
||||
Some((read(name)?, high_bit))
|
||||
})?;
|
||||
let table = codes.metadata().character_collection.as_ref()?.family.ucs2_cmap()?;
|
||||
let unicode = CMap::parse(load_embedded(table)?, load_embedded)?;
|
||||
// A row-and-cell code read in its EUC form falls in the EUC CMap's code space.
|
||||
let space = if high_bit { stand_in(stem?).flatten().and_then(|(name, _)| code_space(name)) } else { code_space(cmap) };
|
||||
Some(CMapCodes::Cids { codes: Box::new(codes), unicode: Box::new(unicode), high_bit, space })
|
||||
}
|
||||
|
||||
/// The code space of predefined CMap `cmap`, as Adobe's CMap files give it
|
||||
/// (<https://github.com/adobe-type-tools/cmap-resources>); a CMap's vertical form has its
|
||||
/// horizontal form's. `None` for a CMap not listed here.
|
||||
fn code_space(cmap: &str) -> Option<CodeSpace> {
|
||||
const RKSJ: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa0", b"\xdf"), (b"\x81\x40", b"\x9f\xfc"), (b"\xe0\x40", b"\xfc\xfc")];
|
||||
const RKSJ_MAC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa0", b"\xdf"), (b"\xfd", b"\xff"), (b"\x81\x40", b"\x9f\xfc"), (b"\xe0\x40", b"\xfc\xfc")];
|
||||
const EUC_JP: CodeSpace = &[(b"\x00", b"\x80"), (b"\x8e\xa0", b"\x8e\xdf"), (b"\xa1\xa1", b"\xfe\xfe")];
|
||||
const ROW_CELL: CodeSpace = &[(b"\x21\x21", b"\x7e\x7e")];
|
||||
const EUC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa1\xa1", b"\xfe\xfe")];
|
||||
const GB_PC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xfd", b"\xff"), (b"\xa1\xa1", b"\xfc\xfe")];
|
||||
const GBK: CodeSpace = &[(b"\x00", b"\x80"), (b"\x81\x40", b"\xfe\xfe")];
|
||||
const GBK2K: CodeSpace = &[(b"\x00", b"\x7f"), (b"\x81\x40", b"\xfe\xfe"), (b"\x81\x30\x81\x30", b"\xfe\x39\xfe\x39")];
|
||||
const B5_PC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xfd", b"\xff"), (b"\xa1\x40", b"\xfc\xfe")];
|
||||
const B5: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa1\x40", b"\xfe\xfe")];
|
||||
const HKSCS: CodeSpace = &[(b"\x00", b"\x80"), (b"\x87\x40", b"\xfe\xfe")];
|
||||
const HKDLB: CodeSpace = &[(b"\x00", b"\x80"), (b"\x8e\x40", b"\xfe\xfe")];
|
||||
const HKGCCS: CodeSpace = &[(b"\x00", b"\x80"), (b"\x8a\x40", b"\xfe\xfe")];
|
||||
const CNS_EUC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa1\xa1", b"\xfe\xfe"), (b"\x8e\xa1\xa1\xa1", b"\x8e\xa3\xfe\xfe")];
|
||||
const KSC_PC: CodeSpace = &[(b"\x00", b"\x84"), (b"\xfe", b"\xff"), (b"\xa1\x41", b"\xfd\xfe")];
|
||||
const UHC: CodeSpace = &[(b"\x00", b"\x80"), (b"\x81\x41", b"\xfe\xfe")];
|
||||
let stem = match cmap {
|
||||
"H" | "V" => "",
|
||||
_ => cmap.strip_suffix("-H").or_else(|| cmap.strip_suffix("-V"))?,
|
||||
};
|
||||
let stem = stem.strip_suffix("-HW").unwrap_or(stem);
|
||||
Some(match stem {
|
||||
"83pv-RKSJ" | "90pv-RKSJ" => RKSJ_MAC,
|
||||
_ if stem.ends_with("RKSJ") => RKSJ,
|
||||
"EUC" | "78-EUC" => EUC_JP,
|
||||
"" | "78" | "Add" | "Ext" | "NWP" => ROW_CELL,
|
||||
"GB-EUC" | "GBT-EUC" | "KSC-EUC" => EUC,
|
||||
"GBpc-EUC" | "GBTpc-EUC" => GB_PC,
|
||||
"GBK-EUC" | "GBKp-EUC" => GBK,
|
||||
"GBK2K" => GBK2K,
|
||||
"B5pc" => B5_PC,
|
||||
"B5" | "ETen-B5" | "ETenms-B5" | "HKdla-B5" | "HKm314-B5" | "HKm471-B5" => B5,
|
||||
"HKscs-B5" | "ETHK-B5" => HKSCS,
|
||||
"HKdlb-B5" => HKDLB,
|
||||
"HKgccs-B5" => HKGCCS,
|
||||
"CNS-EUC" => CNS_EUC,
|
||||
"KSCpc-EUC" => KSC_PC,
|
||||
"KSCms-UHC" => UHC,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The length of the code string `bytes` starts with: the length of the code space range whose
|
||||
/// lowest and highest codes hold each of its bytes between them. `None` when no range does.
|
||||
fn code_len(space: CodeSpace, bytes: &[u8]) -> Option<u8> {
|
||||
space
|
||||
.iter()
|
||||
.filter(|(lo, hi)| bytes.get(..lo.len()).is_some_and(|b| b.iter().zip(lo.iter().zip(hi.iter())).all(|(b, (lo, hi))| (lo..=hi).contains(&b))))
|
||||
.map(|(lo, _)| lo.len())
|
||||
.min()
|
||||
.and_then(|len| u8::try_from(len).ok())
|
||||
}
|
||||
|
||||
/// Encoding pairs (§3.3.1): codes, each followed by the glyph names from it on, into `table`.
|
||||
fn encoding_pairs(table: &mut [Option<char>], vals: &[V]) {
|
||||
let mut code: Option<usize> = None;
|
||||
@@ -556,36 +312,6 @@ fn encoding_pairs(table: &mut [Option<char>], vals: &[V]) {
|
||||
}
|
||||
}
|
||||
|
||||
/// A composite font's name `base-CMap`: the base font's name and its CMap's name, when that is a
|
||||
/// predefined Unicode CMap ([`unicode_cmap`]), one of the predefined CMaps VectorCraft carries
|
||||
/// ([`predefined`]) or one of the others ([`stand_in`]), in its horizontal (`-H`) or vertical
|
||||
/// (`-V`) form. A name ending in an Identity CMap is not read as one, nor is one that ends in an
|
||||
/// unknown CMap name with an encoding part (`UniFoo-UTF8-H`), which would otherwise end in `H`.
|
||||
fn composite_font(name: &str) -> Option<(&str, &str)> {
|
||||
if !(name.ends_with("-H") || name.ends_with("-V")) {
|
||||
return None;
|
||||
}
|
||||
// The longest CMap name first: `83pv-RKSJ-H` before `H`.
|
||||
// No predefined CMap name is longer than 32 bytes.
|
||||
for (at, _) in name.match_indices('-').filter(|(at, _)| name.len() - at <= 33) {
|
||||
let (Some(base), Some(cmap)) = (name.get(..at).filter(|b| !b.is_empty()), name.get(at + 1..)) else { continue };
|
||||
if matches!(cmap, "Identity-H" | "Identity-V") {
|
||||
return None;
|
||||
}
|
||||
let encoding_part =
|
||||
base.rsplit('-').next().is_some_and(|part| ["UCS2", "UTF8", "UTF16", "UTF32", "HW", "EUC", "RKSJ", "B5", "UHC", "Johab"].contains(&part));
|
||||
if matches!(cmap, "H" | "V") && encoding_part {
|
||||
return None;
|
||||
}
|
||||
let vertical_of_predefined = || cmap.strip_suffix('V').is_some_and(|stem| predefined(&format!("{stem}H")).is_some());
|
||||
let other = || cmap.strip_suffix("-H").or_else(|| cmap.strip_suffix("-V")).is_some_and(|stem| stand_in(stem).is_some());
|
||||
if unicode_cmap(cmap).is_some() || predefined(cmap).is_some() || vertical_of_predefined() || other() {
|
||||
return Some((base, cmap));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// An operator's `N` operands, the last `N` values before it, when each is a finite number.
|
||||
fn nums<const N: usize>(vals: &[V]) -> Option<[f64; N]> {
|
||||
let operands = vals.get(vals.len().checked_sub(N)?..)?;
|
||||
|
||||
@@ -18,6 +18,7 @@ use vectorcraft_geom::BezPath;
|
||||
|
||||
use super::interp::{Interp, matrix_obj};
|
||||
use super::obj::{Dict, DictRef, Key, Obj, Op, PsError, Res, Shared, ps_err};
|
||||
use super::predefined_cmap::Unicode;
|
||||
|
||||
/// Most codespace ranges, character and notdef mappings a CMap may hold.
|
||||
const MAX_RANGES: usize = 1 << 16;
|
||||
@@ -210,16 +211,6 @@ fn number(data: &[u8], at: usize, n: usize) -> Option<u64> {
|
||||
(n <= 8).then(|| bytes.iter().fold(0u64, |v, b| v << 8 | u64::from(*b)))
|
||||
}
|
||||
|
||||
/// How a predefined Unicode CMap's codes encode their characters.
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
pub(super) enum Unicode {
|
||||
/// Two bytes a character (`UCS2`) or a UTF-16 surrogate pair, the high byte first.
|
||||
Utf16,
|
||||
Utf8,
|
||||
/// Four bytes, the high byte first.
|
||||
Utf32,
|
||||
}
|
||||
|
||||
/// The encoding of predefined Unicode CMap `name` and whether it is vertical: one of Adobe's
|
||||
/// public CMap resources whose name says its codes are Unicode: `Uni` and a character
|
||||
/// collection, then `-UCS2`, `-UTF8`, `-UTF16` or `-UTF32` (and `-HW` for half-width forms),
|
||||
@@ -246,18 +237,6 @@ pub(super) fn unicode_cmap(name: &str) -> Option<(Unicode, bool)> {
|
||||
Some((form, vertical))
|
||||
}
|
||||
|
||||
/// The characters show string `s` encodes in Unicode form `form` (invalid codes left out).
|
||||
pub(super) fn unicode_text(s: &[u8], form: Unicode) -> String {
|
||||
match form {
|
||||
Unicode::Utf16 => {
|
||||
let units = s.as_chunks::<2>().0.iter().map(|b| u16::from_be_bytes(*b));
|
||||
char::decode_utf16(units).filter_map(Result::ok).collect()
|
||||
}
|
||||
Unicode::Utf8 => String::from_utf8_lossy(s).chars().filter(|c| *c != char::REPLACEMENT_CHARACTER).collect(),
|
||||
Unicode::Utf32 => s.as_chunks::<4>().0.iter().filter_map(|b| char::from_u32(u32::from_be_bytes(*b))).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A predefined Identity CMap: two-byte codes, each the CID of its own value; vertical for
|
||||
/// `Identity-V`.
|
||||
pub(super) fn identity(name: &str, vertical: bool) -> Obj {
|
||||
@@ -417,8 +396,13 @@ impl Interp<'_> {
|
||||
return Ok(d.clone());
|
||||
}
|
||||
let text = name.text().map(|t| t.to_string()).unwrap_or_default();
|
||||
match unicode_cmap(&text) {
|
||||
Some((_, vertical)) => {
|
||||
// Another predefined CMap the file doesn't define, without its mappings: its codes are
|
||||
// read by its name when type is set (see `text`).
|
||||
let predefined = unicode_cmap(&text)
|
||||
.map(|(_, vertical)| vertical)
|
||||
.or_else(|| super::predefined_cmap::known(&text).then(|| super::predefined_cmap::vertical(&text)));
|
||||
match predefined {
|
||||
Some(vertical) => {
|
||||
let mut d = Dict::new();
|
||||
d.insert(Key::name("CMapName"), Obj::name(&text));
|
||||
d.insert(Key::name("CMapType"), Obj::Int(1));
|
||||
|
||||
@@ -92,6 +92,9 @@ pub(crate) struct Interp<'a> {
|
||||
pub cid_subrs: super::cid::CidSubrs,
|
||||
/// The font number `usefont` gave the CMap mappings that follow.
|
||||
pub cmap_font: i64,
|
||||
/// The predefined CMaps type was set in so far, by name, with how their strings decode
|
||||
/// (`None` when one can't be read; see `text`).
|
||||
pub predefined_cmaps: Vec<(Rc<str>, Option<Rc<super::predefined_cmap::CMapCodes>>)>,
|
||||
pub g: GState,
|
||||
pub saved: Vec<GState>,
|
||||
pub out: Out,
|
||||
@@ -232,6 +235,7 @@ impl<'a> Interp<'a> {
|
||||
cff: vec![],
|
||||
cid_subrs: vec![],
|
||||
cmap_font: 0,
|
||||
predefined_cmaps: vec![],
|
||||
type42_counted: HashSet::new(),
|
||||
g,
|
||||
saved: vec![],
|
||||
|
||||
@@ -0,0 +1,358 @@
|
||||
//! Strings in fonts over Adobe's predefined CMaps (<https://github.com/adobe-type-tools/cmap-resources>)
|
||||
//! read as characters: the codes of a predefined Unicode CMap are the characters; those of the
|
||||
//! others map to CIDs of a character collection, whose CID → Unicode CMap gives the characters.
|
||||
//! Used by legacy `.ai` type in composite fonts and by type in CID-keyed fonts found by name.
|
||||
|
||||
use craft_pdf::interpret::hayro_cmap::{BfString, CMap, CMapName, load_embedded};
|
||||
|
||||
/// How a composite font's strings decode.
|
||||
#[derive(Debug)]
|
||||
pub(super) enum CMapCodes {
|
||||
/// By a predefined CMap's codes to CIDs, then by its character collection's CID → Unicode CMap;
|
||||
/// with `high_bit`, each byte 0x21 to 0x7E gets its high bit set first. `space` is the CMap's
|
||||
/// code space ([`code_space`]).
|
||||
Cids { codes: Box<CMap>, unicode: Box<CMap>, high_bit: bool, space: Option<CodeSpace> },
|
||||
/// A predefined Unicode CMap: the codes are the characters.
|
||||
Unicode(Unicode),
|
||||
}
|
||||
|
||||
/// A CMap's code space: ranges of codes, each given by its lowest and highest code, both with the
|
||||
/// code's length in bytes.
|
||||
type CodeSpace = &'static [(&'static [u8], &'static [u8])];
|
||||
|
||||
/// What control byte `b` is in a composite font's string: `\r` a paragraph break, `\t` a tab.
|
||||
fn control(b: u8) -> Option<char> {
|
||||
match b {
|
||||
b'\r' => Some('\n'),
|
||||
b'\t' => Some('\t'),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl CMapCodes {
|
||||
/// The characters of string `bytes`, leaving out codes the CMaps don't map; a `\r` is a
|
||||
/// paragraph break and a `\t` a tab (also as a byte left over after the last whole code of a
|
||||
/// UTF-16 or UTF-32 string).
|
||||
pub(super) fn decode(&self, bytes: &[u8]) -> String {
|
||||
let mut text = String::new();
|
||||
self.each_code(bytes, |_, chars| text.extend(chars));
|
||||
text
|
||||
}
|
||||
|
||||
/// The codes of string `bytes`, each with its value and the characters it stands for (as
|
||||
/// [`Self::decode`] reads them; none for a code the CMaps don't map, or for a byte that starts
|
||||
/// no code, which counts as a code of its own).
|
||||
pub(super) fn codes(&self, bytes: &[u8]) -> Decoded {
|
||||
let mut out = Decoded::default();
|
||||
self.each_code(bytes, |code, chars| out.push(code, chars));
|
||||
out
|
||||
}
|
||||
|
||||
/// Calls `emit` with each code of string `bytes` in turn ([`Self::codes`]): its value and the
|
||||
/// characters it stands for.
|
||||
fn each_code(&self, bytes: &[u8], mut emit: impl FnMut(u32, &mut dyn Iterator<Item = char>)) {
|
||||
let value = |b: &[u8]| b.iter().fold(0u32, |c, &b| c << 8 | u32::from(b));
|
||||
let (codes, unicode, high_bit, space) = match self {
|
||||
Self::Unicode(form) => {
|
||||
let unit = match form {
|
||||
Unicode::Utf16 => 2,
|
||||
Unicode::Utf8 => 1,
|
||||
Unicode::Utf32 => 4,
|
||||
};
|
||||
let (whole, rest) = bytes.split_at_checked(bytes.len() - bytes.len() % unit).unwrap_or((bytes, &[]));
|
||||
unicode_codes(whole, *form, |code, c| emit(value(code), &mut c.map(|c| if c == '\r' { '\n' } else { c }).into_iter()));
|
||||
if let Some(&last) = rest.last() {
|
||||
emit(value(rest), &mut control(last).into_iter());
|
||||
}
|
||||
return;
|
||||
}
|
||||
Self::Cids { codes, unicode, high_bit, space } => (codes, unicode, *high_bit, space),
|
||||
};
|
||||
// A code's value is that of the string's bytes, also where they're read in their EUC form.
|
||||
let original = bytes;
|
||||
let value_at = |at: usize, len: usize| original.get(at..at + len).map_or(0, value);
|
||||
let euc: Vec<u8>;
|
||||
let bytes = if high_bit {
|
||||
euc = bytes.iter().map(|&b| if (0x21..0x7f).contains(&b) { b | 0x80 } else { b }).collect();
|
||||
euc.as_slice()
|
||||
} else {
|
||||
bytes
|
||||
};
|
||||
let mut at = 0;
|
||||
while let Some(&first) = bytes.get(at) {
|
||||
// VectorCraft reads a 0x0D byte as a paragraph break and a 0x09 byte as a tab in every
|
||||
// CMap, as in other fonts' strings, including the row-and-cell CMaps, whose code space
|
||||
// has no one-byte codes.
|
||||
if let Some(control) = control(first) {
|
||||
emit(value_at(at, 1), &mut Some(control).into_iter());
|
||||
at += 1;
|
||||
continue;
|
||||
}
|
||||
let rest = bytes.get(at..).unwrap_or_default();
|
||||
let code = |len: u8| rest.get(..usize::from(len)).map(value);
|
||||
let found = match space {
|
||||
// The code is as long as the code space range it falls in; a byte that starts no
|
||||
// code in the code space is skipped.
|
||||
Some(space) => code_len(space, rest).and_then(|len| code(len).map(|c| (len, codes.lookup_cid_code(c, len)))),
|
||||
None => (1..=4u8).find_map(|len| Some((len, Some(codes.lookup_cid_code(code(len)?, len)?)))),
|
||||
};
|
||||
let Some((len, cid)) = found else {
|
||||
emit(value_at(at, 1), &mut None.into_iter());
|
||||
at += 1;
|
||||
continue;
|
||||
};
|
||||
let value = value_at(at, usize::from(len));
|
||||
at += usize::from(len);
|
||||
// A code the CMap leaves out stands for no character.
|
||||
match cid.and_then(|cid| unicode.lookup_bf_string(cid)) {
|
||||
// A one-byte Roman code whose CID the table gives as a combining mark alone (0x7E in
|
||||
// the Mac Shift-JIS CMaps, U+0303): the ASCII character, as Adobe's cid2code.txt lists first.
|
||||
Some(BfString::Char('\u{300}'..='\u{36f}')) if len == 1 && (0x20..0x7f).contains(&first) => {
|
||||
emit(value, &mut Some(char::from(first)).into_iter())
|
||||
}
|
||||
Some(BfString::Char(c)) if c != char::REPLACEMENT_CHARACTER => emit(value, &mut Some(c).into_iter()),
|
||||
Some(BfString::String(s)) => emit(value, &mut s.chars().filter(|c| *c != char::REPLACEMENT_CHARACTER)),
|
||||
_ => emit(value, &mut None.into_iter()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A string's characters, and its codes: each code's value and how many of the characters it
|
||||
/// stands for, in order.
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct Decoded {
|
||||
pub text: String,
|
||||
pub codes: Vec<(u32, usize)>,
|
||||
}
|
||||
|
||||
impl Decoded {
|
||||
/// Code `code`, standing for characters `chars`.
|
||||
pub(super) fn push(&mut self, code: u32, chars: impl IntoIterator<Item = char>) {
|
||||
let before = self.text.len();
|
||||
self.text.extend(chars);
|
||||
let n = self.text.get(before..).map_or(0, |added| added.chars().count());
|
||||
self.codes.push((code, n));
|
||||
}
|
||||
}
|
||||
|
||||
/// How strings in a font over predefined CMap `cmap` decode; `None` when the CMap can't be read.
|
||||
pub(super) fn codes(cmap: &str) -> Option<CMapCodes> {
|
||||
match unicode_cmap(cmap) {
|
||||
Some(form) => Some(CMapCodes::Unicode(form)),
|
||||
None => cid_codes(cmap),
|
||||
}
|
||||
}
|
||||
|
||||
/// How a predefined Unicode CMap's codes encode their characters.
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
pub(super) enum Unicode {
|
||||
/// Two bytes a character (`UCS2`) or a UTF-16 surrogate pair, the high byte first.
|
||||
Utf16,
|
||||
Utf8,
|
||||
/// Four bytes, the high byte first.
|
||||
Utf32,
|
||||
}
|
||||
|
||||
/// The encoding of predefined Unicode CMap `name`: `Uni` and a character collection, then `-UCS2`,
|
||||
/// `-UTF8`, `-UTF16` or `-UTF32` (and `-HW` for half-width forms), then `-H` or `-V`
|
||||
/// (`UniJIS-UCS2-H`, `UniGB-UTF16-V`: <https://github.com/adobe-type-tools/cmap-resources>).
|
||||
fn unicode_cmap(name: &str) -> Option<Unicode> {
|
||||
let rest = name.strip_suffix("-H").or_else(|| name.strip_suffix("-V"))?;
|
||||
let rest = rest.strip_suffix("-HW").unwrap_or(rest);
|
||||
let (collection, form) = rest.rsplit_once('-')?;
|
||||
let collections = ["UniJIS", "UniJISPro", "UniJIS2004", "UniJISX0213", "UniJISX02132004", "UniHojo", "UniGB", "UniCNS", "UniKS", "UniAKR"];
|
||||
if !collections.contains(&collection) {
|
||||
return None;
|
||||
}
|
||||
match form {
|
||||
"UCS2" | "UTF16" => Some(Unicode::Utf16),
|
||||
"UTF8" => Some(Unicode::Utf8),
|
||||
"UTF32" => Some(Unicode::Utf32),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls `out` with each code of string `s` in Unicode form `form` in turn, and the character it
|
||||
/// encodes (none for an invalid code: a lone surrogate, a byte that starts no UTF-8 character, a
|
||||
/// value past U+10FFFF).
|
||||
fn unicode_codes(s: &[u8], form: Unicode, mut out: impl FnMut(&[u8], Option<char>)) {
|
||||
match form {
|
||||
Unicode::Utf16 => {
|
||||
let unit = |i: usize| s.get(i..i + 2).and_then(|b| <[u8; 2]>::try_from(b).ok()).map(u16::from_be_bytes);
|
||||
let mut at = 0;
|
||||
while let Some(u) = unit(at) {
|
||||
let next = unit(at + 2);
|
||||
let pair = next.filter(|n| (0xd800..0xdc00).contains(&u) && (0xdc00..0xe000).contains(n));
|
||||
let len = if pair.is_some() { 4 } else { 2 };
|
||||
let c = char::decode_utf16([u].into_iter().chain(pair)).next().and_then(Result::ok);
|
||||
out(s.get(at..at + len).unwrap_or_default(), c);
|
||||
at += len;
|
||||
}
|
||||
}
|
||||
Unicode::Utf8 => {
|
||||
let mut at = 0;
|
||||
for chunk in s.utf8_chunks() {
|
||||
for c in chunk.valid().chars() {
|
||||
out(s.get(at..at + c.len_utf8()).unwrap_or_default(), Some(c));
|
||||
at += c.len_utf8();
|
||||
}
|
||||
for _ in chunk.invalid() {
|
||||
out(s.get(at..at + 1).unwrap_or_default(), None);
|
||||
at += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Unicode::Utf32 => {
|
||||
for b in s.as_chunks::<4>().0 {
|
||||
out(b.as_slice(), char::from_u32(u32::from_be_bytes(*b)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Predefined CMap `name` among those VectorCraft carries, leaving out the Identity CMaps, whose
|
||||
/// codes are CIDs of no known character collection.
|
||||
fn predefined(name: &str) -> Option<CMapName<'_>> {
|
||||
match CMapName::from_bytes(name.as_bytes()) {
|
||||
CMapName::Custom(_) | CMapName::IdentityH | CMapName::IdentityV => None,
|
||||
name => Some(name),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adobe's predefined CMaps that VectorCraft doesn't carry, by name without `-H` or `-V`
|
||||
/// (<https://github.com/adobe-type-tools/cmap-resources>): the nearest carried CMap with the same
|
||||
/// code space, and whether each byte of a row-and-cell code gets its high bit set first (the EUC
|
||||
/// form of the same code; `GB-H`, `CNS1-H` and `KSC-H` have no one-byte codes, their code space
|
||||
/// is `<2121>` to `<7E7E>`); `None` for those no carried CMap reads.
|
||||
fn stand_in(stem: &str) -> Option<Option<(&'static str, bool)>> {
|
||||
Some(match stem {
|
||||
"RKSJ" | "78-RKSJ" | "78ms-RKSJ" => Some(("90ms-RKSJ-H", false)),
|
||||
"78-EUC" => Some(("EUC-H", false)),
|
||||
"78" | "Add" | "Ext" | "NWP" => Some(("H", false)),
|
||||
"GB" => Some(("GB-EUC-H", true)),
|
||||
"CNS1" => Some(("CNS-EUC-H", true)),
|
||||
"KSC" => Some(("KSC-EUC-H", true)),
|
||||
"B5" => Some(("ETen-B5-H", false)),
|
||||
"ETHK-B5" | "HKdla-B5" | "HKdlb-B5" | "HKgccs-B5" | "HKm314-B5" | "HKm471-B5" => Some(("HKscs-B5-H", false)),
|
||||
"GBT" | "GBT-EUC" | "GBTpc-EUC" | "CNS2" | "KSC-Johab" => None,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Predefined CMap `cmap`'s codes to CIDs, and its character collection's CID → Unicode CMap. A
|
||||
/// vertical CMap's codes are read with its horizontal CMap when VectorCraft carries that: the two
|
||||
/// differ only in the CIDs of vertical glyph forms. A CMap VectorCraft doesn't carry is read with
|
||||
/// its [`stand_in`].
|
||||
fn cid_codes(cmap: &str) -> Option<CMapCodes> {
|
||||
let read = |name: &str| CMap::parse(load_embedded(predefined(name)?)?, load_embedded);
|
||||
let horizontal = cmap.strip_suffix('V').map(|stem| format!("{stem}H"));
|
||||
let stem = cmap.strip_suffix("-H").or_else(|| cmap.strip_suffix("-V"));
|
||||
let (codes, high_bit) = horizontal.iter().map(String::as_str).chain([cmap]).find_map(|n| Some((read(n)?, false))).or_else(|| {
|
||||
let (name, high_bit) = stand_in(stem?).flatten()?;
|
||||
Some((read(name)?, high_bit))
|
||||
})?;
|
||||
let table = codes.metadata().character_collection.as_ref()?.family.ucs2_cmap()?;
|
||||
let unicode = CMap::parse(load_embedded(table)?, load_embedded)?;
|
||||
// A row-and-cell code read in its EUC form falls in the EUC CMap's code space.
|
||||
let space = if high_bit { stand_in(stem?).flatten().and_then(|(name, _)| code_space(name)) } else { code_space(cmap) };
|
||||
Some(CMapCodes::Cids { codes: Box::new(codes), unicode: Box::new(unicode), high_bit, space })
|
||||
}
|
||||
|
||||
/// The code space of predefined CMap `cmap`, as Adobe's CMap files give it
|
||||
/// (<https://github.com/adobe-type-tools/cmap-resources>); a CMap's vertical form has its
|
||||
/// horizontal form's. `None` for a CMap not listed here.
|
||||
fn code_space(cmap: &str) -> Option<CodeSpace> {
|
||||
const RKSJ: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa0", b"\xdf"), (b"\x81\x40", b"\x9f\xfc"), (b"\xe0\x40", b"\xfc\xfc")];
|
||||
const RKSJ_MAC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa0", b"\xdf"), (b"\xfd", b"\xff"), (b"\x81\x40", b"\x9f\xfc"), (b"\xe0\x40", b"\xfc\xfc")];
|
||||
const EUC_JP: CodeSpace = &[(b"\x00", b"\x80"), (b"\x8e\xa0", b"\x8e\xdf"), (b"\xa1\xa1", b"\xfe\xfe")];
|
||||
const ROW_CELL: CodeSpace = &[(b"\x21\x21", b"\x7e\x7e")];
|
||||
const EUC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa1\xa1", b"\xfe\xfe")];
|
||||
const GB_PC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xfd", b"\xff"), (b"\xa1\xa1", b"\xfc\xfe")];
|
||||
const GBK: CodeSpace = &[(b"\x00", b"\x80"), (b"\x81\x40", b"\xfe\xfe")];
|
||||
const GBK2K: CodeSpace = &[(b"\x00", b"\x7f"), (b"\x81\x40", b"\xfe\xfe"), (b"\x81\x30\x81\x30", b"\xfe\x39\xfe\x39")];
|
||||
const B5_PC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xfd", b"\xff"), (b"\xa1\x40", b"\xfc\xfe")];
|
||||
const B5: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa1\x40", b"\xfe\xfe")];
|
||||
const HKSCS: CodeSpace = &[(b"\x00", b"\x80"), (b"\x87\x40", b"\xfe\xfe")];
|
||||
const HKDLB: CodeSpace = &[(b"\x00", b"\x80"), (b"\x8e\x40", b"\xfe\xfe")];
|
||||
const HKGCCS: CodeSpace = &[(b"\x00", b"\x80"), (b"\x8a\x40", b"\xfe\xfe")];
|
||||
const CNS_EUC: CodeSpace = &[(b"\x00", b"\x80"), (b"\xa1\xa1", b"\xfe\xfe"), (b"\x8e\xa1\xa1\xa1", b"\x8e\xa3\xfe\xfe")];
|
||||
const KSC_PC: CodeSpace = &[(b"\x00", b"\x84"), (b"\xfe", b"\xff"), (b"\xa1\x41", b"\xfd\xfe")];
|
||||
const UHC: CodeSpace = &[(b"\x00", b"\x80"), (b"\x81\x41", b"\xfe\xfe")];
|
||||
let stem = match cmap {
|
||||
"H" | "V" => "",
|
||||
_ => cmap.strip_suffix("-H").or_else(|| cmap.strip_suffix("-V"))?,
|
||||
};
|
||||
let stem = stem.strip_suffix("-HW").unwrap_or(stem);
|
||||
Some(match stem {
|
||||
"83pv-RKSJ" | "90pv-RKSJ" => RKSJ_MAC,
|
||||
_ if stem.ends_with("RKSJ") => RKSJ,
|
||||
"EUC" | "78-EUC" => EUC_JP,
|
||||
"" | "78" | "Add" | "Ext" | "NWP" => ROW_CELL,
|
||||
"GB-EUC" | "GBT-EUC" | "KSC-EUC" => EUC,
|
||||
"GBpc-EUC" | "GBTpc-EUC" => GB_PC,
|
||||
"GBK-EUC" | "GBKp-EUC" => GBK,
|
||||
"GBK2K" => GBK2K,
|
||||
"B5pc" => B5_PC,
|
||||
"B5" | "ETen-B5" | "ETenms-B5" | "HKdla-B5" | "HKm314-B5" | "HKm471-B5" => B5,
|
||||
"HKscs-B5" | "ETHK-B5" => HKSCS,
|
||||
"HKdlb-B5" => HKDLB,
|
||||
"HKgccs-B5" => HKGCCS,
|
||||
"CNS-EUC" => CNS_EUC,
|
||||
"KSCpc-EUC" => KSC_PC,
|
||||
"KSCms-UHC" => UHC,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The length of the code string `bytes` starts with: the length of the code space range whose
|
||||
/// lowest and highest codes hold each of its bytes between them. `None` when no range does.
|
||||
fn code_len(space: CodeSpace, bytes: &[u8]) -> Option<u8> {
|
||||
space
|
||||
.iter()
|
||||
.filter(|(lo, hi)| bytes.get(..lo.len()).is_some_and(|b| b.iter().zip(lo.iter().zip(hi.iter())).all(|(b, (lo, hi))| (lo..=hi).contains(&b))))
|
||||
.map(|(lo, _)| lo.len())
|
||||
.min()
|
||||
.and_then(|len| u8::try_from(len).ok())
|
||||
}
|
||||
|
||||
/// Whether `cmap` names a predefined CMap: a predefined Unicode CMap ([`unicode_cmap`]), one of the
|
||||
/// predefined CMaps VectorCraft carries ([`predefined`]) or one of the others ([`stand_in`]), in
|
||||
/// its horizontal (`-H`) or vertical (`-V`) form. The Identity CMaps are not among them.
|
||||
pub(super) fn known(cmap: &str) -> bool {
|
||||
let vertical_of_predefined = || cmap.strip_suffix('V').is_some_and(|stem| predefined(&format!("{stem}H")).is_some());
|
||||
let other = || cmap.strip_suffix("-H").or_else(|| cmap.strip_suffix("-V")).is_some_and(|stem| stand_in(stem).is_some());
|
||||
unicode_cmap(cmap).is_some() || predefined(cmap).is_some() || vertical_of_predefined() || other()
|
||||
}
|
||||
|
||||
/// Whether predefined CMap `cmap` is a vertical CMap (`-V`, or the row-and-cell `V`).
|
||||
pub(super) fn vertical(cmap: &str) -> bool {
|
||||
cmap == "V" || cmap.ends_with("-V")
|
||||
}
|
||||
|
||||
/// A composite font's name `base-CMap`: the base font's name and its CMap's name, when that is a
|
||||
/// predefined CMap ([`known`]). A name ending in an Identity CMap is not read as one, nor is one
|
||||
/// that ends in an unknown CMap name with an encoding part (`UniFoo-UTF8-H`), which would otherwise
|
||||
/// end in `H`.
|
||||
pub(super) fn composite_font(name: &str) -> Option<(&str, &str)> {
|
||||
if !(name.ends_with("-H") || name.ends_with("-V")) {
|
||||
return None;
|
||||
}
|
||||
// The longest CMap name first: `83pv-RKSJ-H` before `H`.
|
||||
// No predefined CMap name is longer than 32 bytes.
|
||||
for (at, _) in name.match_indices('-').filter(|(at, _)| name.len() - at <= 33) {
|
||||
let (Some(base), Some(cmap)) = (name.get(..at).filter(|b| !b.is_empty()), name.get(at + 1..)) else { continue };
|
||||
if matches!(cmap, "Identity-H" | "Identity-V") {
|
||||
return None;
|
||||
}
|
||||
let encoding_part =
|
||||
base.rsplit('-').next().is_some_and(|part| ["UCS2", "UTF8", "UTF16", "UTF32", "HW", "EUC", "RKSJ", "B5", "UHC", "Johab"].contains(&part));
|
||||
if matches!(cmap, "H" | "V") && encoding_part {
|
||||
return None;
|
||||
}
|
||||
if known(cmap) {
|
||||
return Some((base, cmap));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -403,6 +403,171 @@ fn named_cidfonts_over_unicode_cmaps_set_their_characters_as_type() {
|
||||
check("/Ryumin-Light-UniJIS-UCS2-H findfont 20 scalefont setfont <00410042> stringwidth pop dup 5 gt exch 40 lt and");
|
||||
}
|
||||
|
||||
/// The text objects of an import.
|
||||
fn texts(r: &crate::Imported) -> Vec<vectorcraft_doc::TextObject> {
|
||||
all(&r.document)
|
||||
.iter()
|
||||
.filter_map(|n| match &n.kind {
|
||||
NodeKind::Text(t) => Some((**t).clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A CID-keyed font over one of the other predefined CMaps (Shift-JIS, EUC, GBK, Big Five, UHC,
|
||||
/// JIS row and cell: <https://github.com/adobe-type-tools/cmap-resources>), the file defining
|
||||
/// neither, its CIDFont only named: the codes are read by that CMap and its character
|
||||
/// collection's CIDs to Unicode, and set as type in the font of the CIDFont's name, whether
|
||||
/// found as `CIDFont-CMap` or composed. Codes the CMap doesn't map and control codes are left out.
|
||||
#[test]
|
||||
fn named_cidfonts_over_predefined_cmaps_set_their_characters_as_type() {
|
||||
let cases: &[(&str, &str, &str)] = &[
|
||||
// 日 is 0x93FA and 本 0x967B in Shift-JIS.
|
||||
("83pv-RKSJ-H", "93fa967b41", "日本A"),
|
||||
("90ms-RKSJ-H", "93fa", "日"),
|
||||
("EUC-H", "c6fc", "日"),
|
||||
// JIS row and cell: 日 is 0x467C.
|
||||
("H", "467c", "日"),
|
||||
("GBK-EUC-H", "d6d0", "中"),
|
||||
("B5pc-H", "a4a4", "中"),
|
||||
("ETen-B5-H", "a4a4", "中"),
|
||||
("KSCms-UHC-H", "c7d1", "한"),
|
||||
// A CMap read with the nearest carried one: GB 2312 row and cell in EUC form, 中 0x5650.
|
||||
("GB-H", "5650", "中"),
|
||||
// 0x8540 maps to no CID, 0xFD is in no code space range, 0x0D is a control code.
|
||||
("90ms-RKSJ-H", "854093fa41fd0d42", "日AB"),
|
||||
];
|
||||
for (cmap, hex, want) in cases {
|
||||
let r = super::tests::read(&format!("/Ryumin-Light-{cmap} findfont 20 scalefont setfont 10 20 moveto <{hex}> show"));
|
||||
assert!(super::tests::program_warnings(&r.warnings).is_empty(), "{cmap}: {:?}", r.warnings);
|
||||
let t = texts(&r);
|
||||
assert_eq!(t.iter().map(|t| t.plain_text()).collect::<Vec<_>>(), [*want], "{cmap}");
|
||||
let style = &t[0].runs[0].style;
|
||||
assert_eq!((style.font_family.as_str(), style.font_style.as_str(), style.size), ("Ryumin", "Light", 20.0), "{cmap}");
|
||||
assert!(!t[0].vertical, "{cmap}");
|
||||
}
|
||||
let r = super::tests::read("/X /GBK-EUC-H [/NotInFile] composefont 20 scalefont setfont 10 20 moveto <d6d041> show");
|
||||
assert_eq!(texts(&r).iter().map(|t| t.plain_text()).collect::<String>(), "中A");
|
||||
// The point moves past the type, along the line.
|
||||
check("/Ryumin-Light-90ms-RKSJ-H findfont 20 scalefont setfont <93fa967b> stringwidth abs 0.01 lt exch dup 5 gt exch 60 lt and and");
|
||||
}
|
||||
|
||||
/// Type in a CID-keyed font over a predefined CMap, its CIDFont only named, shown with `ashow`,
|
||||
/// `widthshow` (matching a code by its value) and `xshow`, is spaced as the operator says: the
|
||||
/// point moves that far, and the characters' tracking makes up the difference, as for type in a
|
||||
/// font found by name. Over a vertical CMap the extra space runs down the column.
|
||||
#[test]
|
||||
fn named_cidfonts_over_predefined_cmaps_take_the_show_operators_spacing() {
|
||||
// Measured without painting.
|
||||
let measured = |body: &str| check(&format!("gsave nulldevice {body} grestore"));
|
||||
let set = "/Ryumin-Light-90ms-RKSJ-H findfont 20 scalefont setfont";
|
||||
// `ashow` adds 5 to each of the two codes.
|
||||
measured(&format!(
|
||||
"{set} 10 20 moveto <93fa967b> show currentpoint pop 10 20 moveto 5 0 <93fa967b> ashow currentpoint pop sub -10 sub abs 0.01 lt"
|
||||
));
|
||||
let r = super::tests::read(&format!("{set} 10 20 moveto 5 0 <93fa967b> ashow"));
|
||||
let t = texts(&r);
|
||||
assert_eq!(t.iter().map(|t| t.plain_text()).collect::<Vec<_>>(), ["日本"]);
|
||||
assert!(t[0].runs.iter().all(|run| (run.style.tracking - 250.0).abs() < 0.5), "{:?}", t[0].runs);
|
||||
// `widthshow` adds 7 to the space, code 0x20.
|
||||
measured(&format!(
|
||||
"{set} 10 20 moveto <93fa20967b> show currentpoint pop 10 20 moveto 7 0 32 <93fa20967b> widthshow currentpoint pop sub -7 sub abs 0.01 lt"
|
||||
));
|
||||
// A row-and-cell code read in its EUC form keeps the value of the string's bytes: 中 0x5650.
|
||||
let gb = "/STSong-Light-GB-H findfont 20 scalefont setfont";
|
||||
measured(&format!(
|
||||
"{gb} 10 20 moveto <56505650> show currentpoint pop 10 20 moveto 7 0 16#5650 <56505650> widthshow currentpoint pop sub -14 sub abs 0.01 lt"
|
||||
));
|
||||
// `xshow` moves 30 a code.
|
||||
measured(&format!("{set} 10 20 moveto <93fa967b> [30 30] xshow currentpoint pop 70 sub abs 0.01 lt"));
|
||||
// Down the column: `ashow`'s 5 down each code.
|
||||
let set = "/Ryumin-Light-90ms-RKSJ-V findfont 20 scalefont setfont";
|
||||
measured(&format!(
|
||||
"{set} 10 200 moveto <93fa967b> show currentpoint exch pop 10 200 moveto 0 -5 <93fa967b> ashow currentpoint exch pop sub 10 sub abs 0.01 lt"
|
||||
));
|
||||
let r = super::tests::read(&format!("{set} 10 200 moveto 0 -5 <93fa967b> ashow"));
|
||||
let t = texts(&r);
|
||||
assert_eq!(t.iter().map(|t| t.plain_text()).collect::<String>(), "日本");
|
||||
assert!(t.iter().all(|t| t.vertical), "{t:?}");
|
||||
}
|
||||
|
||||
/// A CID-keyed font over a vertical predefined CMap (`-V`), the file defining neither, its CIDFont
|
||||
/// only named: the type is a vertical column, and the point moves down it, not along a line.
|
||||
#[test]
|
||||
fn named_cidfonts_over_vertical_cmaps_set_vertical_type() {
|
||||
for (cmap, hex) in [("90ms-RKSJ-V", "93fa967b"), ("UniJIS-UCS2-V", "65e5672c")] {
|
||||
let r = super::tests::read(&format!("/Ryumin-Light-{cmap} findfont 20 scalefont setfont 10 20 moveto <{hex}> show"));
|
||||
let t = texts(&r);
|
||||
assert_eq!(t.iter().map(|t| t.plain_text()).collect::<Vec<_>>(), ["日本"], "{cmap}: {:?}", r.warnings);
|
||||
assert!(t[0].vertical, "{cmap}");
|
||||
check(&format!("/Ryumin-Light-{cmap} findfont 20 scalefont setfont <{hex}> stringwidth dup -60 gt exch -5 lt and exch abs 0.01 lt and"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Down a column each code of a CID-keyed font over a vertical predefined CMap moves the point one
|
||||
/// em, the default vertical displacement (PDF Reference 1.7, §5.6.3, `DW2`), also two digits that
|
||||
/// vertical type sets side by side in one em: `show` and `stringwidth` move the point as far as
|
||||
/// showing the codes one at a time.
|
||||
#[test]
|
||||
fn codes_down_a_column_each_move_the_point_one_em() {
|
||||
// Measured without painting.
|
||||
let set = "gsave nulldevice /Ryumin-Light-90ms-RKSJ-V findfont 20 scalefont setfont";
|
||||
check(&format!("{set} (12) stringwidth grestore -40 sub abs 0.01 lt exch abs 0.01 lt and"));
|
||||
check(&format!("{set} 10 100 moveto (12) show currentpoint grestore 60 sub abs 0.01 lt exch 10 sub abs 0.01 lt and"));
|
||||
check(&format!(
|
||||
"{set} 10 100 moveto (1) show (2) show currentpoint 10 100 moveto (12) show currentpoint grestore \
|
||||
3 -1 roll sub abs 0.01 lt 3 1 roll sub abs 0.01 lt and"
|
||||
));
|
||||
}
|
||||
|
||||
/// Type in a CID-keyed font over a predefined CMap is set the same way by `show` and by the show
|
||||
/// operators that space it: characters the font would join in a ligature (`fi` in the bundled
|
||||
/// Source Serif 4) are set without the ligature, and the point moves as far.
|
||||
#[test]
|
||||
fn named_cid_type_is_set_alike_by_every_show_operator() {
|
||||
let set = "/SourceSerif4-Regular-UniJIS-UCS2-H findfont 50 scalefont setfont 10 20 moveto";
|
||||
let features = |show: &str| {
|
||||
let r = super::tests::read(&format!("{set} <00660069> {show}"));
|
||||
let t = texts(&r);
|
||||
assert_eq!(t.iter().map(|t| t.plain_text()).collect::<Vec<_>>(), ["fi"], "{show}");
|
||||
t[0].runs.iter().map(|r| r.style.features.clone()).collect::<Vec<_>>()
|
||||
};
|
||||
let plain = features("show");
|
||||
assert_eq!(plain, features("0 0 3 -1 roll ashow"));
|
||||
assert!(plain.iter().all(|f| f.iter().any(|f| f == "-liga")), "{plain:?}");
|
||||
check(&format!(
|
||||
"gsave nulldevice {set} <00660069> stringwidth 10 20 moveto 0 0 <00660069> ashow currentpoint grestore \
|
||||
20 sub 3 -1 roll sub abs 0.01 lt 3 1 roll 10 sub sub abs 0.01 lt and"
|
||||
));
|
||||
}
|
||||
|
||||
/// A font name ending in the row-and-cell CMap `H` after a name without a hyphen stays a font of
|
||||
/// the whole name, its strings read byte by byte. Type in a predefined CMap whose mappings aren't
|
||||
/// available is left out with a warning, and a long string in a CID-keyed font over a predefined
|
||||
/// CMap shown in a loop runs out of the budget.
|
||||
#[test]
|
||||
fn latin_names_ending_in_h_and_cmaps_that_cannot_be_read() {
|
||||
let r = super::tests::read("/Futura-H findfont 20 scalefont setfont 10 20 moveto (AB) show");
|
||||
assert_eq!(texts(&r).iter().map(|t| t.plain_text()).collect::<Vec<_>>(), ["AB"]);
|
||||
let r = super::tests::read("/Ryumin-Light-GBT-H findfont 20 scalefont setfont 10 20 moveto (AB) show 0 0 10 10 rectfill");
|
||||
assert!(texts(&r).is_empty(), "{:?}", all(&r.document));
|
||||
assert_eq!(super::tests::program_warnings(&r.warnings), [super::text::UNREAD_CMAP], "{:?}", r.warnings);
|
||||
super::tests::runs_out_of_budget("/Ryumin-Light-90ms-RKSJ-H findfont 20 scalefont setfont { 100000 string stringwidth pop pop } loop");
|
||||
}
|
||||
|
||||
/// A string in the file, in a CID-keyed font over a predefined CMap, too long to decode within the
|
||||
/// memory budget stops the program with an error before it is decoded.
|
||||
#[test]
|
||||
fn huge_strings_over_predefined_cmaps_count_against_the_memory_budget() {
|
||||
// Codes the CMap doesn't map, so nothing is laid out when the string is decoded.
|
||||
let huge = "\u{1}".repeat(17 << 20);
|
||||
let r = import(&super::tests::eps(&format!(
|
||||
"0 0 10 10 rectfill /Ryumin-Light-90ms-RKSJ-H findfont 20 scalefont setfont 10 20 moveto ({huge}) stringwidth"
|
||||
)))
|
||||
.unwrap();
|
||||
assert!(r.warnings.iter().any(|w| w.contains("too much memory")), "{:?}", r.warnings);
|
||||
}
|
||||
|
||||
/// A Type 2 CIDFont the file carries (its glyph space one unit an em), over a Unicode CMap the
|
||||
/// file doesn't define, sets its characters as type at the font's size, not a thousand times it.
|
||||
#[test]
|
||||
|
||||
+187
-74
@@ -15,6 +15,7 @@ use super::cid::Base;
|
||||
use super::graphics::MAX_APART;
|
||||
use super::interp::{Interp, matrix_obj, matrix_of};
|
||||
use super::obj::{Dict, DictRef, Key, Obj, Op, PsError, Res, ps_err};
|
||||
use super::predefined_cmap::Decoded;
|
||||
use super::type1::Embedded;
|
||||
|
||||
/// The font a program uses before it sets one.
|
||||
@@ -170,8 +171,8 @@ impl Interp<'_> {
|
||||
ResourceForAll => self.resource_for_all()?,
|
||||
Show | AShow | WidthShow | AWidthShow | XShow | YShow | XYShow | KShow => {
|
||||
let (s, spacing) = self.show_operands(op)?;
|
||||
if let Some((text, font)) = self.unicode_type(&s)? {
|
||||
self.show_unicode(&text, &font)?;
|
||||
if let Some((codes, font, vertical)) = self.named_cid_type(&s)? {
|
||||
self.show_named_cid(&codes, &font, vertical, &spacing)?;
|
||||
} else if let Some(glyphs) = self.composite_glyphs(&s)? {
|
||||
self.show_composite(&glyphs, &spacing)?;
|
||||
} else {
|
||||
@@ -282,9 +283,9 @@ impl Interp<'_> {
|
||||
|
||||
/// How far `s` moves the current point (user space).
|
||||
fn width_of(&mut self, s: &[u8]) -> Res<Vec2> {
|
||||
if let Some((text, font)) = self.unicode_type(s)? {
|
||||
if let Some((codes, font, vertical)) = self.named_cid_type(s)? {
|
||||
let saved = self.g.font.replace(font);
|
||||
let r = self.set_text(&text, Point::ZERO).map(|t| t.2);
|
||||
let r = self.named_cid_width(&codes, vertical);
|
||||
self.g.font = saved;
|
||||
return r;
|
||||
}
|
||||
@@ -395,18 +396,29 @@ impl Interp<'_> {
|
||||
}
|
||||
|
||||
/// The font named `k`: one the program defined; else, for a name `CIDFont-CMap` whose CMap
|
||||
/// is a predefined Unicode CMap, the CID-keyed font of those two (the usual name of a
|
||||
/// CID-keyed font, as `composefont` makes them: PLRM 3rd ed., chapter 8); else a font of
|
||||
/// that name.
|
||||
/// is a predefined CMap, the CID-keyed font of those two (the usual name of a CID-keyed font,
|
||||
/// as `composefont` makes them: PLRM 3rd ed., chapter 8; the CMaps are Adobe's public CMap
|
||||
/// resources, <https://github.com/adobe-type-tools/cmap-resources>); else a font of that name.
|
||||
/// A name an installed face has is that face's, and a name split at a bare `H` or `V` (the
|
||||
/// row-and-cell CMaps) needs a CIDFont name with a hyphen in it (`Ryumin-Light-H`), so
|
||||
/// `Futura-H` stays a font of that name.
|
||||
fn find_font(&mut self, k: &Obj) -> Res<DictRef> {
|
||||
let key = k.key().ok_or(PsError::Ps("typecheck", "findfont".into()))?;
|
||||
if let Some(Obj::Dict(d)) = self.fonts.borrow().get(&key) {
|
||||
return Ok(d.clone());
|
||||
}
|
||||
let text = k.text().map(|t| t.to_string()).unwrap_or_default();
|
||||
let split =
|
||||
text.match_indices('-').map(|(i, _)| i).find(|&i| i > 0 && super::cid::unicode_cmap(text.get(i + 1..).unwrap_or_default()).is_some());
|
||||
if let Some((cid_font, cmap)) = split.and_then(|i| Some((text.get(..i)?, text.get(i + 1..)?))) {
|
||||
let split = text
|
||||
.match_indices('-')
|
||||
.map(|(i, _)| i)
|
||||
.find(|&i| i > 0 && super::cid::unicode_cmap(text.get(i + 1..).unwrap_or_default()).is_some())
|
||||
.and_then(|i| Some((text.get(..i)?, text.get(i + 1..)?)))
|
||||
.or_else(|| {
|
||||
let (cid_font, cmap) = super::predefined_cmap::composite_font(&text)?;
|
||||
let installed = vectorcraft_text::FontDb::global().find_postscript(&text).is_some();
|
||||
(!installed && !(matches!(cmap, "H" | "V") && !cid_font.contains('-'))).then_some((cid_font, cmap))
|
||||
});
|
||||
if let Some((cid_font, cmap)) = split {
|
||||
let cmap = self.find_cmap(&Obj::name(cmap))?;
|
||||
let dep = self.find_cid_font(&Obj::name(cid_font))?;
|
||||
return Ok(self.composed(key, cmap, vec![Obj::Dict(dep)]));
|
||||
@@ -476,6 +488,15 @@ impl Interp<'_> {
|
||||
|
||||
/// [`Self::set_type`] for the characters `text`.
|
||||
fn set_text(&mut self, text: &str, at: Point) -> Res<(TextObject, TextLayout, Vec2)> {
|
||||
self.set_text_in(text, at, false)
|
||||
}
|
||||
|
||||
/// [`Self::set_text`], as vertical type when `vertical`: a column anchored at `at`, the top
|
||||
/// center of its first em box, that moves the current point down the column by the layout's
|
||||
/// advances. In vertical writing the current point is a glyph's origin 1, by default half its
|
||||
/// width across and 880 units of 1000 above its origin 0 (PDF Reference 1.7, §5.6.3 "Glyph
|
||||
/// Metrics in CIDFonts", `DW2`).
|
||||
fn set_text_in(&mut self, text: &str, at: Point, vertical: bool) -> Res<(TextObject, TextLayout, Vec2)> {
|
||||
// Laying out type costs about as much as 300 operations, and 64 more a character
|
||||
// (measured), so `{ (a) stringwidth pop pop } loop` or `cshow` on a long string ends
|
||||
// within the budget like any other loop instead of running for minutes.
|
||||
@@ -493,11 +514,29 @@ impl Interp<'_> {
|
||||
let fill = if self.g.paint.is_none() { Paint::solid(vectorcraft_color::Color::BLACK) } else { self.g.paint.clone() };
|
||||
let style = CharStyle { font_family, font_style, size, fill, stroke: Paint::None, ..CharStyle::default() };
|
||||
let mut t = TextObject::point(Point::ZERO, text, style);
|
||||
t.vertical = vertical;
|
||||
let layout = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t);
|
||||
t.xf = m * Affine::scale_non_uniform(1.0 / size, -1.0 / size);
|
||||
t.cached_bounds = Some(layout.bounds);
|
||||
let adv: f64 = layout.glyphs.iter().map(|g| g.advance).sum();
|
||||
Ok((t, layout, f1 * Point::new(adv / size, 0.0) - f1 * Point::ZERO))
|
||||
let moved = if vertical { Point::new(0.0, -adv / size) } else { Point::new(adv / size, 0.0) };
|
||||
Ok((t, layout, f1 * moved - f1 * Point::ZERO))
|
||||
}
|
||||
|
||||
/// [`Self::set_text_in`] for characters the file places one by one: where the layout joins
|
||||
/// characters in a ligature, the type is set without ligatures, so each character has its own
|
||||
/// advance.
|
||||
fn set_placed_text(&mut self, text: &str, at: Point, vertical: bool) -> Res<(TextObject, TextLayout)> {
|
||||
let (mut t, mut layout, _) = self.set_text_in(text, at, vertical)?;
|
||||
let glyph_starts: std::collections::HashSet<usize> = layout.glyphs.iter().map(|g| g.byte).collect();
|
||||
if text.char_indices().any(|(b, _)| !glyph_starts.contains(&b)) {
|
||||
for run in &mut t.runs {
|
||||
run.style.features.push("-liga".into());
|
||||
}
|
||||
layout = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t);
|
||||
t.cached_bounds = Some(layout.bounds);
|
||||
}
|
||||
Ok((t, layout))
|
||||
}
|
||||
|
||||
/// `show`: point type at the current point, which moves past it.
|
||||
@@ -527,27 +566,30 @@ impl Interp<'_> {
|
||||
return self.show_type42_outlines(data, glyphs, s, spacing, at, f1);
|
||||
}
|
||||
// Each code's character, if it has one that shows (control codes don't).
|
||||
let chars: Vec<Option<char>> = match &plain {
|
||||
let chars = match &plain {
|
||||
Some((_, glyphs)) => glyphs.iter().map(|g| g.ch).collect::<Vec<_>>(),
|
||||
None => self.named_chars(s).into_iter().map(Some).collect(),
|
||||
};
|
||||
let mut codes = Decoded::default();
|
||||
for (c, &code) in chars.into_iter().zip(s) {
|
||||
codes.push(u32::from(code), c.filter(|c| !c.is_control()));
|
||||
}
|
||||
.into_iter()
|
||||
.map(|c| c.filter(|c| !c.is_control()))
|
||||
.collect();
|
||||
let text: String = chars.iter().flatten().collect();
|
||||
let (mut t, mut layout, _) = self.set_text(&text, at)?;
|
||||
let widths: Option<Vec<f64>> = plain.map(|(_, glyphs)| glyphs.iter().map(|g| g.width).collect());
|
||||
self.show_codes(&codes, widths.as_deref(), spacing, false, s)
|
||||
}
|
||||
|
||||
/// The codes `codes` (each standing for some of its characters, or none) shown in the current
|
||||
/// font as [`Self::show_plain`] shows a string's, spaced as `spacing` says, each code `widths`
|
||||
/// wide when given (in glyph space, one unit an em), else as wide as its characters' advances;
|
||||
/// as vertical type when `vertical`, the codes moving the point down the column. `kern` is the
|
||||
/// string `kshow` gives its procedure the codes of.
|
||||
fn show_codes(&mut self, codes: &Decoded, widths: Option<&[f64]>, spacing: &Spacing, vertical: bool, kern: &[u8]) -> Res {
|
||||
let at = self.user_point()?;
|
||||
let (_, f1) = self.font()?;
|
||||
let text = &codes.text;
|
||||
let (mut t, layout) = self.set_placed_text(text, at, vertical)?;
|
||||
let size = t.runs.first().map_or(1.0, |r| r.style.size);
|
||||
let starts: Vec<usize> = text.char_indices().map(|(b, _)| b).collect();
|
||||
// The file places each character's glyph: where the layout joins characters in a
|
||||
// ligature, the type is set without ligatures, so each character has its own advance.
|
||||
let glyph_starts: std::collections::HashSet<usize> = layout.glyphs.iter().map(|g| g.byte).collect();
|
||||
if starts.iter().any(|b| !glyph_starts.contains(b)) {
|
||||
for run in &mut t.runs {
|
||||
run.style.features.push("-liga".into());
|
||||
}
|
||||
layout = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t);
|
||||
t.cached_bounds = Some(layout.bounds);
|
||||
}
|
||||
// Each character's advance in the layout (text space).
|
||||
let mut natural = vec![0.0; starts.len()];
|
||||
for g in &layout.glyphs {
|
||||
@@ -555,29 +597,30 @@ impl Interp<'_> {
|
||||
*n += g.advance;
|
||||
}
|
||||
}
|
||||
// Each code's move (user space) and that move in text space, along the baseline and across.
|
||||
let em = |x: f64| f1 * Point::new(x, 0.0) - f1 * Point::ZERO;
|
||||
// Each code's move (user space) and that move in text space, along the baseline (down the
|
||||
// column) and across.
|
||||
let em = |x: f64| f1 * if vertical { Point::new(0.0, -x) } else { Point::new(x, 0.0) } - f1 * Point::ZERO;
|
||||
let inv = f1.inverse();
|
||||
let mut moves = Vec::with_capacity(s.len());
|
||||
let mut moves = Vec::with_capacity(codes.codes.len());
|
||||
let mut k = 0;
|
||||
// What `kshow`'s procedure draws goes after the type.
|
||||
let start = self.out.drawn.len();
|
||||
let mut pen = at;
|
||||
for (i, (&code, c)) in s.iter().zip(&chars).enumerate() {
|
||||
let own = match &plain {
|
||||
Some((_, glyphs)) => glyphs.get(i).map_or(0.0, |g| g.width),
|
||||
None if c.is_some() => natural.get(k).copied().unwrap_or(0.0) / size,
|
||||
None => 0.0,
|
||||
for (i, &(code, n)) in codes.codes.iter().enumerate() {
|
||||
let own = match widths {
|
||||
Some(widths) => widths.get(i).copied().unwrap_or(0.0),
|
||||
None => natural.get(k..k + n).map_or(0.0, |w| w.iter().sum::<f64>()) / size,
|
||||
};
|
||||
k += usize::from(c.is_some());
|
||||
k += n;
|
||||
let mut d = spacing.advance(i, i64::from(code), em(own));
|
||||
d += self.kern(spacing, s, i, pen + d)?;
|
||||
d += self.kern(spacing, kern, i, pen + d)?;
|
||||
pen += d;
|
||||
let e = inv * Point::new(d.x, d.y) - inv * Point::ZERO;
|
||||
if ![d.x, d.y, e.x * size, e.y * size].iter().all(|v| v.is_finite()) {
|
||||
let (along, across) = if vertical { (-e.y * size, e.x * size) } else { (e.x * size, e.y * size) };
|
||||
if ![d.x, d.y, along, across].iter().all(|v| v.is_finite()) {
|
||||
return ps_err("undefinedresult", "a show");
|
||||
}
|
||||
moves.push((d, e.x * size, e.y * size));
|
||||
moves.push((d, along, across));
|
||||
}
|
||||
let total: Vec2 = moves.iter().map(|m| m.0).sum();
|
||||
if !(total.x.is_finite() && total.y.is_finite()) {
|
||||
@@ -589,12 +632,11 @@ impl Interp<'_> {
|
||||
if across {
|
||||
// Moves across the baseline (`yshow`, `xyshow`): each character where the file puts it.
|
||||
let mut pen = at;
|
||||
for ((d, ..), c) in moves.iter().zip(&chars) {
|
||||
if let Some(c) = c
|
||||
&& painted
|
||||
&& !c.is_whitespace()
|
||||
{
|
||||
let (t, ..) = self.set_text(c.encode_utf8(&mut [0; 4]), pen)?;
|
||||
let mut chars = text.chars();
|
||||
for ((d, ..), &(_, n)) in moves.iter().zip(&codes.codes) {
|
||||
let c: String = chars.by_ref().take(n).collect();
|
||||
if painted && !c.trim().is_empty() {
|
||||
let (t, ..) = self.set_text_in(&c, pen, vertical)?;
|
||||
let clips = self.g.clips.clone();
|
||||
self.out.push(Node::new(NodeId(0), NodeKind::Text(Box::new(t))), &clips);
|
||||
}
|
||||
@@ -605,11 +647,17 @@ impl Interp<'_> {
|
||||
// a character, before the next, are that character's.
|
||||
let mut lead = Vec2::ZERO;
|
||||
let mut target: Vec<f64> = Vec::with_capacity(natural.len());
|
||||
for ((d, along, _), c) in moves.iter().zip(&chars) {
|
||||
match (c, target.last_mut()) {
|
||||
(Some(_), _) => target.push(*along),
|
||||
(None, Some(last)) => *last += along,
|
||||
(None, None) => lead += *d,
|
||||
for ((d, along, _), &(_, n)) in moves.iter().zip(&codes.codes) {
|
||||
if n > 0 {
|
||||
// A code of several characters: those before its last keep their advances.
|
||||
let before = natural.get(target.len()..target.len() + n - 1).unwrap_or_default().to_vec();
|
||||
let rest = *along - before.iter().sum::<f64>();
|
||||
target.extend(before);
|
||||
target.push(rest);
|
||||
} else if let Some(last) = target.last_mut() {
|
||||
*last += along;
|
||||
} else {
|
||||
lead += *d;
|
||||
}
|
||||
}
|
||||
let tracking: Vec<f64> = target
|
||||
@@ -931,23 +979,47 @@ impl Interp<'_> {
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// The characters of `s` and the font to set them in when the current font is a CID-keyed
|
||||
/// font over a predefined Unicode CMap the file doesn't define (`UniJIS-UCS2-H`), whose
|
||||
/// mappings to CIDs aren't known: its codes say the characters, set as type in the font of
|
||||
/// its CIDFont's name.
|
||||
fn unicode_type(&mut self, s: &[u8]) -> Res<Option<(String, DictRef)>> {
|
||||
let Some((text, font, name)) = self.unicode_parts(s) else { return Ok(None) };
|
||||
/// The codes of `s` with the characters each stands for, the font to set them in and whether
|
||||
/// they run down a column, when the current font is a CID-keyed font over a predefined CMap
|
||||
/// the file doesn't define (`UniJIS-UCS2-H`, `90ms-RKSJ-H`), whose mappings to CIDs the file
|
||||
/// doesn't give: a Unicode CMap's codes are the characters, another CMap's are read by the CMap
|
||||
/// of that name and its character collection's CID → Unicode CMap (a code it doesn't map
|
||||
/// stands for none), set as type in the font of its CIDFont's name, vertical over a vertical
|
||||
/// CMap (`WMode` 1). Control codes stand for no character. Type in a CMap VectorCraft can't
|
||||
/// read is left out, with a warning.
|
||||
fn named_cid_type(&mut self, s: &[u8]) -> Res<Option<(Decoded, DictRef, bool)>> {
|
||||
let Some((cmap, font, name, vertical)) = self.named_cid_parts() else { return Ok(None) };
|
||||
// Each code is looked up in the CMap's mappings.
|
||||
self.spend(s.len() as u64)?;
|
||||
// The decoded codes and characters are held while the string is shown: checked against
|
||||
// the memory budget first, so a huge string in the file stops the program with an error.
|
||||
let held = s.len().saturating_mul(DECODED_BYTES);
|
||||
self.alloc(held)?;
|
||||
self.unalloc(held);
|
||||
let decoded = match self.predefined_codes(&cmap) {
|
||||
Some(codes) => codes.codes(s),
|
||||
None => {
|
||||
self.out.warn(UNREAD_CMAP);
|
||||
Decoded::default()
|
||||
}
|
||||
};
|
||||
let mut codes = Decoded::default();
|
||||
let mut chars = decoded.text.chars();
|
||||
for &(code, n) in &decoded.codes {
|
||||
codes.push(code, chars.by_ref().take(n).filter(|c| !c.is_control()));
|
||||
}
|
||||
// The composite font is copied under the CIDFont's name for every string: charged by
|
||||
// its size, so a huge font dictionary shown in a loop runs out of the budget.
|
||||
let entries = font.borrow().len();
|
||||
self.spend(entries as u64)?;
|
||||
let mut named = font.borrow().clone();
|
||||
named.insert(Key::name("FontName"), name);
|
||||
Ok(Some((text, DictRef::new(named))))
|
||||
Ok(Some((codes, DictRef::new(named), vertical)))
|
||||
}
|
||||
|
||||
/// The text, the composite font and the CIDFont's name of [`Self::unicode_type`].
|
||||
fn unicode_parts(&self, s: &[u8]) -> Option<(String, DictRef, Obj)> {
|
||||
/// The CMap's name, the composite font, the CIDFont's name and the writing mode of
|
||||
/// [`Self::named_cid_type`].
|
||||
fn named_cid_parts(&self) -> Option<(String, DictRef, Obj, bool)> {
|
||||
let font = self.g.font.clone()?;
|
||||
let f = font.borrow();
|
||||
if font_type(&f) != Some(0.0) || f.get(&Key::name("FMapType")).and_then(Obj::as_num) != Some(9.0) {
|
||||
@@ -959,29 +1031,63 @@ impl Interp<'_> {
|
||||
if cmap.contains_key(&Key::name("CodeSpaceRange")) {
|
||||
return None;
|
||||
}
|
||||
let (form, _) = super::cid::unicode_cmap(&cmap.get(&Key::name("CMapName")).and_then(Obj::text)?)?;
|
||||
let cmap_name = cmap.get(&Key::name("CMapName")).and_then(Obj::text)?.to_string();
|
||||
if super::cid::unicode_cmap(&cmap_name).is_none() && !super::predefined_cmap::known(&cmap_name) {
|
||||
return None;
|
||||
}
|
||||
let wmode = f.get(&Key::name("WMode")).or_else(|| cmap.get(&Key::name("WMode"))).and_then(Obj::as_num);
|
||||
let Some(Obj::Dict(dep)) = f.get(&Key::name("FDepVector")).and_then(|v| v.items()?.get(0)) else { return None };
|
||||
let name = {
|
||||
let d = dep.borrow();
|
||||
d.get(&Key::name("CIDFontName")).or_else(|| d.get(&Key::name("FontName"))).cloned()?
|
||||
};
|
||||
Some((super::cid::unicode_text(s, form), font.clone(), name))
|
||||
Some((cmap_name, font.clone(), name, wmode == Some(1.0)))
|
||||
}
|
||||
|
||||
/// `text` set as point type at the current point in font `font` (see
|
||||
/// [`Self::unicode_type`]), which moves past it.
|
||||
fn show_unicode(&mut self, text: &str, font: &DictRef) -> Res {
|
||||
let at = self.user_point()?;
|
||||
let saved = self.g.font.replace(font.clone());
|
||||
let r = self.set_text(text, at);
|
||||
self.g.font = saved;
|
||||
let (t, _, adv) = r?;
|
||||
if !self.g.paint.is_none() && !self.g.null && !text.trim().is_empty() {
|
||||
let clips = self.g.clips.clone();
|
||||
self.out.push(Node::new(NodeId(0), NodeKind::Text(Box::new(t))), &clips);
|
||||
/// How strings in predefined CMap `cmap` decode (a CMap other than a Unicode CMap read once a
|
||||
/// file); `None` when VectorCraft can't read it.
|
||||
fn predefined_codes(&mut self, cmap: &str) -> Option<Rc<super::predefined_cmap::CMapCodes>> {
|
||||
if let Some((form, _)) = super::cid::unicode_cmap(cmap) {
|
||||
return Some(Rc::new(super::predefined_cmap::CMapCodes::Unicode(form)));
|
||||
}
|
||||
self.g.cur = Some(self.xf() * (at + adv));
|
||||
Ok(())
|
||||
if let Some((_, codes)) = self.predefined_cmaps.iter().find(|(n, _)| **n == *cmap) {
|
||||
return codes.clone();
|
||||
}
|
||||
let codes = super::predefined_cmap::codes(cmap).map(Rc::new);
|
||||
// Only the names of the other predefined CMaps reach here (`named_cid_parts`), so the
|
||||
// list stays short.
|
||||
self.predefined_cmaps.push((Rc::from(cmap), codes.clone()));
|
||||
codes
|
||||
}
|
||||
|
||||
/// The codes `codes` (with their characters) set as point type at the current point in font
|
||||
/// `font` (see [`Self::named_cid_type`]), running down a column when `vertical`, which moves
|
||||
/// past them, spaced as `spacing` says, as [`Self::show_codes`] shows them. `widthshow` and
|
||||
/// `awidthshow` match a code by its value (font number 0 times 256 plus the code: PLRM 3rd ed.,
|
||||
/// `widthshow` in chapter 8); `kshow`'s procedure isn't run between them (a composite font's
|
||||
/// codes aren't single bytes). Down a column each code moves the point one em, the default
|
||||
/// vertical displacement of a CIDFont whose metrics the file doesn't give (PDF Reference 1.7,
|
||||
/// §5.6.3, `DW2`).
|
||||
fn show_named_cid(&mut self, codes: &Decoded, font: &DictRef, vertical: bool, spacing: &Spacing) -> Res {
|
||||
let widths = vertical.then(|| vec![1.0; codes.codes.len()]);
|
||||
let saved = self.g.font.replace(font.clone());
|
||||
let r = self.show_codes(codes, widths.as_deref(), spacing, vertical, &[]);
|
||||
self.g.font = saved;
|
||||
r
|
||||
}
|
||||
|
||||
/// How far the codes `codes` move the current point (user space) when [`Self::show_named_cid`]
|
||||
/// shows them with no extra spacing.
|
||||
fn named_cid_width(&mut self, codes: &Decoded, vertical: bool) -> Res<Vec2> {
|
||||
let (_, f1) = self.font()?;
|
||||
let moved = if vertical {
|
||||
Point::new(0.0, -(codes.codes.len() as f64))
|
||||
} else {
|
||||
let (t, layout) = self.set_placed_text(&codes.text, Point::ZERO, false)?;
|
||||
let size = t.runs.first().map_or(1.0, |r| r.style.size);
|
||||
Point::new(layout.glyphs.iter().map(|g| g.advance).sum::<f64>() / size, 0.0)
|
||||
};
|
||||
Ok(f1 * moved - f1 * Point::ZERO)
|
||||
}
|
||||
|
||||
/// The characters of a composite font shown at the current point, spaced as `spacing` says.
|
||||
@@ -1282,6 +1388,13 @@ const MAX_TYPE1: usize = 256;
|
||||
/// The warning for CID-keyed type in a CIDFont the file doesn't carry.
|
||||
const MISSING_CID_FONT: &str = "CID-keyed type in a font the file doesn't carry was left out";
|
||||
|
||||
/// Most bytes [`Interp::named_cid_type`] holds for each byte of a string: two lists of codes (16
|
||||
/// bytes a code) and their characters (up to 4 bytes a character, a few characters a code).
|
||||
const DECODED_BYTES: usize = 64;
|
||||
|
||||
/// The warning for type in a predefined CMap whose mappings VectorCraft doesn't have.
|
||||
pub(super) const UNREAD_CMAP: &str = "Type in a CMap that couldn't be read was left out";
|
||||
|
||||
/// Most Type 42 fonts whose TrueType data is kept between glyphs.
|
||||
const MAX_TYPE42: usize = 16;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user