mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-19 10:35:59 +00:00
fix(fonts): cut ToUnicode destinations to the CMap limit
A sequential bfrange stores one destination and builds every covered glyph's text from all of it, so one huge destination and many repeats of its codes made text extraction copy megabytes per glyph. Destinations keep at most 256 UTF-16 units (the format's 512 bytes), and marked-text extraction stops asking for a glyph's text once its id has all the text it keeps.
This commit is contained in:
1 parent
38211588c7
commit
1cc392e5b0
2 files changed
+26
-8
No files matched your search
@@ -285,17 +285,17 @@ pub(crate) fn mcid_texts(doc: &Document, page: &Dict) -> HashMap<i64, String> {
|
||||
};
|
||||
for (code, len) in m.codes(&s.bytes) {
|
||||
let trm = tm.then(&gs.ctm);
|
||||
// A glyph's text is only built while its id still takes more.
|
||||
if let Some(id) = id
|
||||
&& out.get(&id).is_none_or(|t| t.len() < MAX_TITLE * 4)
|
||||
&& let Some(glyph) = m.text_of(code)
|
||||
{
|
||||
let text = out.entry(id).or_default();
|
||||
if text.len() < MAX_TITLE * 4 {
|
||||
let breaks = ends.get(&id).is_some_and(|end| new_word(&trm, *end, gs.size, gs.scale));
|
||||
if breaks && !text.is_empty() && !text.ends_with(' ') && !glyph.starts_with(' ') {
|
||||
text.push(' ');
|
||||
}
|
||||
text.push_str(&glyph);
|
||||
let breaks = ends.get(&id).is_some_and(|end| new_word(&trm, *end, gs.size, gs.scale));
|
||||
if breaks && !text.is_empty() && !text.ends_with(' ') && !glyph.starts_with(' ') {
|
||||
text.push(' ');
|
||||
}
|
||||
text.push_str(&glyph);
|
||||
}
|
||||
let advance = m.width(code) * gs.size + gs.char_spacing + if m.is_space(code, len) { gs.word_spacing } else { 0.0 };
|
||||
tm = Matrix::translate(advance * gs.scale, 0.0).then(&tm);
|
||||
|
||||
+20
-2
@@ -905,9 +905,13 @@ impl Metrics {
|
||||
}
|
||||
}
|
||||
|
||||
/// The UTF-16 code units of CMap hex bytes.
|
||||
/// Most UTF-16 units kept from one ToUnicode destination: the CMap format allows destinations of up
|
||||
/// to 512 bytes, and every glyph a sequential range covers builds its text from the whole of one.
|
||||
const MAX_DESTINATION_UNITS: usize = 256;
|
||||
|
||||
/// The UTF-16 code units of CMap hex bytes, at most [`MAX_DESTINATION_UNITS`] of them.
|
||||
fn code_units(b: &[u8]) -> Vec<u16> {
|
||||
b.chunks(2).map(|c| u16::from_be_bytes([c[0], *c.get(1).unwrap_or(&0)])).collect()
|
||||
b.chunks(2).take(MAX_DESTINATION_UNITS).map(|c| u16::from_be_bytes([c[0], *c.get(1).unwrap_or(&0)])).collect()
|
||||
}
|
||||
|
||||
/// UTF-16BE (with surrogates) from CMap hex bytes.
|
||||
@@ -1262,6 +1266,20 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Every glyph of a sequential range builds its text from the destination, so a long one is cut
|
||||
/// to the CMap limit once, when it is read.
|
||||
#[test]
|
||||
fn unicode_destinations_are_cut_to_the_cmap_limit() {
|
||||
let long = "0041".repeat(4096);
|
||||
let cmap = format!("2 beginbfrange <0010> <0020> <{long}> <0030> <0030> [<{long}>] endbfrange 1 beginbfchar <0040> <{long}> endbfchar");
|
||||
let mut u = Unicode::default();
|
||||
parse_to_unicode(cmap.as_bytes(), &mut u);
|
||||
for code in [0x10, 0x20, 0x30, 0x40] {
|
||||
let text = u.text(code).unwrap_or_default();
|
||||
assert!(!text.is_empty() && text.len() <= MAX_DESTINATION_UNITS * 3, "code {code:#x}: {} bytes", text.len());
|
||||
}
|
||||
}
|
||||
|
||||
/// A font whose ranges cover a few low codes keeps tables that size, not a fixed 64k-entry one.
|
||||
#[test]
|
||||
fn low_code_tables_end_with_the_last_covered_code() {
|
||||
|
||||
Reference in new issue
Block a user