mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-18 04:57:05 +00:00
fix(render): resolve TrueType glyphs across Unicode/UCS-4 cmaps, post v2.0 aliases, and truncated FontFlags (#2159) (#2980)
* fix(render): resolve accented TrueType glyphs across Symbolic flags, Unicode cmaps, post v2.0, and ToUnicode (#2159, #2426) * fix(render): separate TrueType glyph selection from extraction (#2980) Restore the existing Symbolic and CFF mapping paths. Keep name-derived Unicode cmap lookup, post-name aliases, known flag bits, and explicit .notdef handling. Replace disputed expectations with synthetic glyph/extraction regressions; the original PDFs in #2159 and #2426 remain unavailable and unverified. * fix(render): check exact post v2.0 glyph name before suffix-stripped cmap lookup (#2980) * fix(render): prefer exact post v2.0 name for multi-codepoint AGL glyph names (#2980) * style: format vendor true_type.rs * fix(render): keep TrueType vendor patch in upstream style Restore unchanged upstream formatting around the glyph-mapping fix and use upstream-default formatting for its new helpers. Preserve the upstream glyph_name_to_text extraction fallback and every existing functional change. Validated workspace cargo fmt, git diff --check, and byte-identical rustfmt canonical output before and after this formatting-only change. The eight TrueType integration tests passed on the prior head in hosted CI; fresh CI will validate this follow-up. Local recompilation and optional negative controls were not attempted because build storage is exhausted.
This commit is contained in:
@@ -0,0 +1,335 @@
|
||||
//! Synthetic regressions for embedded TrueType glyph mapping. No original files from
|
||||
//! issues #2159 or #2426 are available; these do not establish that those files are fixed.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use lopdf::{Document, Object, Stream, dictionary};
|
||||
use pdfcraft_render::{PageRenderer, RenderConfig, RenderRequest, RequestKind};
|
||||
|
||||
const INTER_TTF: &[u8] = include_bytes!("../../../assets/fonts/Inter-Regular.ttf");
|
||||
|
||||
/// Find the byte offset of a table in a TrueType `sfnt` font.
|
||||
fn find_table(ttf: &[u8], tag: &[u8; 4]) -> usize {
|
||||
let num_tables = u16::from_be_bytes([ttf[4], ttf[5]]) as usize;
|
||||
for i in 0..num_tables {
|
||||
let rec = 12 + i * 16;
|
||||
if &ttf[rec..rec + 4] == tag {
|
||||
return u32::from_be_bytes([ttf[rec + 8], ttf[rec + 9], ttf[rec + 10], ttf[rec + 11]]) as usize;
|
||||
}
|
||||
}
|
||||
panic!("missing table {:?}", std::str::from_utf8(tag));
|
||||
}
|
||||
|
||||
/// Rewrite the `cmap` table of a TrueType font so its Windows `(3, 1)` / `(3, 10)` subtable
|
||||
/// headers become Unicode `(0, 3)` subtable headers and its `post` table is version 3.0 (no
|
||||
/// glyph names), exercising a Unicode-only cmap shape without assuming the unavailable
|
||||
/// original font from issue #2159 has this structure.
|
||||
fn make_macos_unicode_cmap_subset(ttf: &[u8]) -> Vec<u8> {
|
||||
let mut out = ttf.to_vec();
|
||||
let cmap = find_table(&out, b"cmap");
|
||||
let num_records = u16::from_be_bytes([out[cmap + 2], out[cmap + 3]]) as usize;
|
||||
let mut changed = false;
|
||||
for i in 0..num_records {
|
||||
let rec = cmap + 4 + i * 8;
|
||||
let platform_id = u16::from_be_bytes([out[rec], out[rec + 1]]);
|
||||
let encoding_id = u16::from_be_bytes([out[rec + 2], out[rec + 3]]);
|
||||
if platform_id == 3 && (encoding_id == 1 || encoding_id == 10) {
|
||||
out[rec..rec + 2].copy_from_slice(&0u16.to_be_bytes());
|
||||
out[rec + 2..rec + 4].copy_from_slice(&3u16.to_be_bytes());
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
assert!(changed, "expected at least one Windows Unicode cmap record to convert");
|
||||
|
||||
// Set `post` version to 3.0 (0x00030000), which carries no PostScript glyph names.
|
||||
let post = find_table(&out, b"post");
|
||||
out[post..post + 4].copy_from_slice(&0x0003_0000u32.to_be_bytes());
|
||||
out
|
||||
}
|
||||
|
||||
/// Build a single-byte string `codes` on a 120x80 page.
|
||||
fn pdf_for_codes(font_bytes: Vec<u8>, flags: i64, encoding: Option<Object>, to_unicode: Option<&[u8]>, codes: &[u8]) -> Vec<u8> {
|
||||
let mut doc = Document::with_version("1.7");
|
||||
let program = doc.add_object(Stream::new(dictionary! {}, font_bytes));
|
||||
let descriptor = doc.add_object(dictionary! {
|
||||
"Type" => "FontDescriptor",
|
||||
"FontName" => "ABCDEF+Inter-Regular",
|
||||
"Flags" => flags,
|
||||
"FontFile2" => program,
|
||||
});
|
||||
let mut font = dictionary! {
|
||||
"Type" => "Font",
|
||||
"Subtype" => "TrueType",
|
||||
"BaseFont" => "ABCDEF+Inter-Regular",
|
||||
"FontDescriptor" => descriptor,
|
||||
};
|
||||
if let Some(enc) = encoding {
|
||||
font.set("Encoding", enc);
|
||||
}
|
||||
if let Some(cmap) = to_unicode {
|
||||
font.set("ToUnicode", doc.add_object(Stream::new(dictionary! {}, cmap.to_vec())));
|
||||
}
|
||||
let font_id = doc.add_object(font);
|
||||
|
||||
let mut ops = b"BT /F1 36 Tf 1 0 0 1 15 25 Tm <".to_vec();
|
||||
for &b in codes {
|
||||
ops.extend_from_slice(format!("{b:02X}").as_bytes());
|
||||
}
|
||||
ops.extend_from_slice(b"> Tj ET");
|
||||
let content = doc.add_object(Stream::new(dictionary! {}, ops));
|
||||
let pages = doc.new_object_id();
|
||||
let page = doc.add_object(dictionary! {
|
||||
"Type" => "Page",
|
||||
"Parent" => pages,
|
||||
"MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(120), Object::Integer(80)],
|
||||
"Resources" => dictionary! { "Font" => dictionary! { "F1" => font_id } },
|
||||
"Contents" => content,
|
||||
});
|
||||
doc.objects.insert(pages, dictionary! { "Type" => "Pages", "Kids" => vec![Object::Reference(page)], "Count" => 1 }.into());
|
||||
let catalog = doc.add_object(dictionary! { "Type" => "Catalog", "Pages" => pages });
|
||||
doc.trailer.set("Root", catalog);
|
||||
|
||||
let mut pdf_bytes = Vec::new();
|
||||
doc.save_to(&mut pdf_bytes).expect("save synthetic PDF");
|
||||
pdf_bytes
|
||||
}
|
||||
|
||||
fn render_codes(font_bytes: Vec<u8>, flags: i64, encoding: Option<Object>, to_unicode: Option<&[u8]>, codes: &[u8]) -> Vec<u8> {
|
||||
let pdf_bytes = pdf_for_codes(font_bytes, flags, encoding, to_unicode, codes);
|
||||
let rendered = PageRenderer::new(Arc::new(pdf_bytes), RenderConfig::default()).render(RenderRequest {
|
||||
page: 0,
|
||||
kind: RequestKind::Pixels,
|
||||
scale: 1.0,
|
||||
..Default::default()
|
||||
});
|
||||
assert!(rendered.error.is_none(), "render failed: {:?}", rendered.error);
|
||||
rendered.rgba.to_vec()
|
||||
}
|
||||
|
||||
/// Reference raster for a single glyph named `glyph_name` in `Inter-Regular.ttf`.
|
||||
fn reference_glyph_rgba(glyph_name: &[u8]) -> Vec<u8> {
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"Type" => "Encoding",
|
||||
"BaseEncoding" => Object::Name(b"WinAnsiEncoding".to_vec()),
|
||||
"Differences" => vec![
|
||||
Object::Integer(65),
|
||||
Object::Name(glyph_name.to_vec()),
|
||||
],
|
||||
});
|
||||
render_codes(INTER_TTF.to_vec(), 32, Some(encoding), None, &[65])
|
||||
}
|
||||
|
||||
fn assert_reference_glyph_is_not_notdef(glyph_name: &[u8], raster: &[u8]) {
|
||||
let notdef = reference_glyph_rgba(b".notdef");
|
||||
assert_ne!(raster, notdef.as_slice(), "{glyph_name:?} resolved to .notdef");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonsymbolic_truetype_with_differences_renders_czech_and_slovak_carons() {
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"Type" => "Encoding",
|
||||
"BaseEncoding" => Object::Name(b"WinAnsiEncoding".to_vec()),
|
||||
"Differences" => vec![
|
||||
Object::Integer(190),
|
||||
Object::Name(b"lcaron".to_vec()),
|
||||
Object::Integer(232),
|
||||
Object::Name(b"ccaron".to_vec()),
|
||||
Object::Integer(236),
|
||||
Object::Name(b"ecaron".to_vec()),
|
||||
Object::Integer(248),
|
||||
Object::Name(b"rcaron".to_vec()),
|
||||
],
|
||||
});
|
||||
for (code, name) in [(190u8, &b"lcaron"[..]), (232u8, &b"ccaron"[..]), (236u8, &b"ecaron"[..]), (248u8, &b"rcaron"[..])] {
|
||||
let expected = reference_glyph_rgba(name);
|
||||
assert_reference_glyph_is_not_notdef(name, &expected);
|
||||
let actual = render_codes(INTER_TTF.to_vec(), 32, Some(encoding.clone()), None, &[code]);
|
||||
assert!(actual == expected, "Nonsymbolic TrueType font with /Differences drew wrong glyph for {}", String::from_utf8_lossy(name));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn macos_unicode_platform_cmap_renders_slovak_carons() {
|
||||
// A synthetic (0, 3) cmap with post v3.0 and no Windows subtable exercises the
|
||||
// name-derived Unicode fallback. The actual font from #2159 is unavailable.
|
||||
let macos_ttf = make_macos_unicode_cmap_subset(INTER_TTF);
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"Type" => "Encoding",
|
||||
"BaseEncoding" => Object::Name(b"WinAnsiEncoding".to_vec()),
|
||||
"Differences" => vec![
|
||||
Object::Integer(190),
|
||||
Object::Name(b"lcaron".to_vec()),
|
||||
Object::Integer(232),
|
||||
Object::Name(b"ccaron".to_vec()),
|
||||
Object::Integer(236),
|
||||
Object::Name(b"ecaron".to_vec()),
|
||||
Object::Integer(248),
|
||||
Object::Name(b"rcaron".to_vec()),
|
||||
],
|
||||
});
|
||||
for (code, name) in [(190u8, &b"lcaron"[..]), (232u8, &b"ccaron"[..]), (236u8, &b"ecaron"[..]), (248u8, &b"rcaron"[..])] {
|
||||
let expected = reference_glyph_rgba(name);
|
||||
assert_reference_glyph_is_not_notdef(name, &expected);
|
||||
let actual = render_codes(macos_ttf.clone(), 32, Some(encoding.clone()), None, &[code]);
|
||||
assert!(actual == expected, "macOS (0, 3) Unicode cmap drew wrong glyph for {}", String::from_utf8_lossy(name));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symbolic_truetype_ignores_encoding_and_differences() {
|
||||
// ISO 32000-1 9.6.6.4: the Symbolic flag makes Encoding inapplicable.
|
||||
let expected = reference_glyph_rgba(b"oslash");
|
||||
assert!(expected != reference_glyph_rgba(b"rcaron"));
|
||||
for name in [b"rcaron".as_slice(), b".notdef"] {
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"BaseEncoding" => "WinAnsiEncoding",
|
||||
"Differences" => vec![Object::Integer(248), Object::Name(name.to_vec())],
|
||||
});
|
||||
for flags in [4, 4 | (1 << 20)] {
|
||||
let actual = render_codes(INTER_TTF.to_vec(), flags, Some(encoding.clone()), None, &[248]);
|
||||
assert!(actual == expected, "Symbolic Encoding must be ignored: {flags}, {name:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tounicode_does_not_select_the_drawn_glyph() {
|
||||
// ToUnicode is an extraction mapping, not a glyph-selection mapping (9.10).
|
||||
let to_unicode = b"/CIDInit /ProcSet findresource begin 12 dict begin begincmap \
|
||||
/CMapName /Extraction def /CMapType 2 def \
|
||||
1 begincodespacerange <00> <FF> endcodespacerange \
|
||||
2 beginbfchar <41> <0042> <F8> <0159> endbfchar \
|
||||
endcmap CMapName currentdict /CMap defineresource pop end end";
|
||||
for (code, name, wrong_name) in [(65, b"A".as_slice(), b"B".as_slice()), (248, b"oslash", b"rcaron")] {
|
||||
for flags in [4, 32] {
|
||||
for encoding in [None, Some(Object::Name(b"WinAnsiEncoding".to_vec()))] {
|
||||
// Retain the existing StandardEncoding fallback for a Nonsymbolic
|
||||
// font without Encoding. Standard code 248 is lslash, not oslash;
|
||||
// this reader fallback differs from MuPDF's raw Unicode lookup.
|
||||
let expected_name = if code == 248 && flags == 32 && encoding.is_none() { b"lslash".as_slice() } else { name };
|
||||
let expected = reference_glyph_rgba(expected_name);
|
||||
assert!(expected != reference_glyph_rgba(wrong_name));
|
||||
let plain = render_codes(INTER_TTF.to_vec(), flags, encoding.clone(), None, &[code]);
|
||||
assert!(plain == expected, "reference without ToUnicode: {flags}, {code}");
|
||||
let bytes = pdf_for_codes(INTER_TTF.to_vec(), flags, encoding.clone(), Some(to_unicode), &[code]);
|
||||
let text = PageRenderer::new(Arc::new(bytes), RenderConfig::default()).render(RenderRequest {
|
||||
page: 0,
|
||||
kind: RequestKind::Text,
|
||||
..Default::default()
|
||||
});
|
||||
assert_eq!(text.text.unwrap().plain_text(), if code == 65 { "B" } else { "ř" });
|
||||
let actual = render_codes(INTER_TTF.to_vec(), flags, encoding, Some(to_unicode), &[code]);
|
||||
assert!(actual == expected, "ToUnicode changed glyph: {flags}, {code}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonsymbolic_explicit_notdef_does_not_fall_back_to_raw_code() {
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"BaseEncoding" => "WinAnsiEncoding",
|
||||
"Differences" => vec![Object::Integer(65), Object::Name(b".notdef".to_vec())],
|
||||
});
|
||||
let expected = reference_glyph_rgba(b".notdef");
|
||||
assert!(expected != reference_glyph_rgba(b"A"));
|
||||
let actual = render_codes(INTER_TTF.to_vec(), 32, Some(encoding), None, &[65]);
|
||||
assert!(actual == expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_ucs4_cmap_resolves_name_derived_unicode() {
|
||||
let mut ttf = INTER_TTF.to_vec();
|
||||
let cmap = find_table(&ttf, b"cmap");
|
||||
let count = u16::from_be_bytes([ttf[cmap + 2], ttf[cmap + 3]]) as usize;
|
||||
let mut retained = false;
|
||||
for i in 0..count {
|
||||
let rec = cmap + 4 + i * 8;
|
||||
let platform = u16::from_be_bytes([ttf[rec], ttf[rec + 1]]);
|
||||
let encoding = u16::from_be_bytes([ttf[rec + 2], ttf[rec + 3]]);
|
||||
if platform == 3 && encoding == 10 {
|
||||
retained = true;
|
||||
} else {
|
||||
ttf[rec..rec + 2].copy_from_slice(&99u16.to_be_bytes());
|
||||
}
|
||||
}
|
||||
assert!(retained);
|
||||
let post = find_table(&ttf, b"post");
|
||||
ttf[post..post + 4].copy_from_slice(&0x0003_0000u32.to_be_bytes());
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"BaseEncoding" => "WinAnsiEncoding",
|
||||
"Differences" => vec![Object::Integer(65), Object::Name(b"rcaron".to_vec())],
|
||||
});
|
||||
let expected = reference_glyph_rgba(b"rcaron");
|
||||
assert_reference_glyph_is_not_notdef(b"rcaron", &expected);
|
||||
assert!(render_codes(ttf, 32, Some(encoding), None, &[65]) == expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn post_v2_uni_and_agl_glyph_names_resolve_without_unicode_cmap() {
|
||||
// Strip all Unicode cmap subtables by rewriting platform_id to 99 so lookup must go through
|
||||
// the TrueType `post` v2.0 glyph name table (including uniXXXX <-> AGL aliasing).
|
||||
let mut post_only_ttf = INTER_TTF.to_vec();
|
||||
let cmap = find_table(&post_only_ttf, b"cmap");
|
||||
let num_records = u16::from_be_bytes([post_only_ttf[cmap + 2], post_only_ttf[cmap + 3]]) as usize;
|
||||
for i in 0..num_records {
|
||||
let rec = cmap + 4 + i * 8;
|
||||
post_only_ttf[rec..rec + 2].copy_from_slice(&99u16.to_be_bytes());
|
||||
}
|
||||
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"Type" => "Encoding",
|
||||
"BaseEncoding" => Object::Name(b"WinAnsiEncoding".to_vec()),
|
||||
"Differences" => vec![
|
||||
Object::Integer(190),
|
||||
Object::Name(b"uni013E".to_vec()),
|
||||
Object::Integer(232),
|
||||
Object::Name(b"uni010D".to_vec()),
|
||||
Object::Integer(236),
|
||||
Object::Name(b"ecaron.salt".to_vec()),
|
||||
Object::Integer(248),
|
||||
Object::Name(b"u0159".to_vec()),
|
||||
],
|
||||
});
|
||||
for (code, name) in [(190u8, &b"lcaron"[..]), (232u8, &b"ccaron"[..]), (236u8, &b"ecaron"[..]), (248u8, &b"rcaron"[..])] {
|
||||
let expected = reference_glyph_rgba(name);
|
||||
assert_reference_glyph_is_not_notdef(name, &expected);
|
||||
let actual = render_codes(post_only_ttf.clone(), 32, Some(encoding.clone()), None, &[code]);
|
||||
assert!(actual == expected, "post v2.0 glyph name resolution drew wrong glyph for {}", String::from_utf8_lossy(name));
|
||||
}
|
||||
|
||||
// When a font has both a Unicode cmap and an exact suffixed alternate in `post` v2.0 (`I.1`),
|
||||
// `/Differences` must select the `post` v2.0 alternate (`I.1`) rather than stripping `.1` and
|
||||
// drawing plain `I` from the Unicode cmap.
|
||||
let alt_encoding = Object::Dictionary(dictionary! {
|
||||
"Type" => "Encoding",
|
||||
"BaseEncoding" => Object::Name(b"WinAnsiEncoding".to_vec()),
|
||||
"Differences" => vec![
|
||||
Object::Integer(73),
|
||||
Object::Name(b"I.1".to_vec()),
|
||||
],
|
||||
});
|
||||
let expected_alt = render_codes(post_only_ttf, 32, Some(alt_encoding.clone()), None, &[73]);
|
||||
let plain_i = reference_glyph_rgba(b"I");
|
||||
assert!(expected_alt != plain_i, "I.1 alternate in Inter must differ from plain I");
|
||||
let actual_with_cmap = render_codes(INTER_TTF.to_vec(), 32, Some(alt_encoding), None, &[73]);
|
||||
assert!(actual_with_cmap == expected_alt, "exact post v2.0 alternate I.1 must win over suffix-stripped cmap lookup");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_descriptor_flags_with_extra_bits_preserve_non_symbolic() {
|
||||
let encoding = Object::Dictionary(dictionary! {
|
||||
"Type" => "Encoding",
|
||||
"BaseEncoding" => Object::Name(b"WinAnsiEncoding".to_vec()),
|
||||
"Differences" => vec![
|
||||
Object::Integer(232),
|
||||
Object::Name(b"ccaron".to_vec()),
|
||||
],
|
||||
});
|
||||
let expected = reference_glyph_rgba(b"ccaron");
|
||||
assert_reference_glyph_is_not_notdef(b"ccaron", &expected);
|
||||
// Bit 6 (32 = NonSymbolic) plus a high producer-specific bit (1 << 20) that previously caused
|
||||
// FontFlags::from_bits to return None and discard all flags.
|
||||
let actual = render_codes(INTER_TTF.to_vec(), 32 | (1 << 20), Some(encoding), None, &[232]);
|
||||
assert!(actual == expected);
|
||||
}
|
||||
Vendored
+7
@@ -43,6 +43,13 @@ licence files unchanged. Every patch is marked `PdfCraft patch:` in the source,
|
||||
PdfCraft test, and should be proposed upstream. Remove the vendored copy once upstream releases
|
||||
the fix. Vendoring copyleft code is never allowed (plan/adr/0001).
|
||||
|
||||
The TrueType name lookup patch probes Unicode `(0, *)` and Windows `(3, 10)` cmap records,
|
||||
recognizes post-name aliases, and retains known descriptor flags when unknown bits are present.
|
||||
`crates/render/tests/truetype_cmap.rs` covers these synthetic cases and verifies that Symbolic
|
||||
Encoding/Differences and ToUnicode do not override the existing glyph-selection policy.
|
||||
ToUnicode still controls extraction. The CFF fallback is unchanged. No original affected PDF
|
||||
from #2159 or #2426 is available, so those reported files remain unverified.
|
||||
|
||||
A PdfCraft patch in `hayro-syntax::Page::base_dimensions` expresses the existing one-point floor as a raw reciprocal of `/UserUnit`, rounded upward only when f32 multiplication would fall below one point. Five `pdfcraft-render` regressions with prefix `raster::tests::raw_crop_` cover the observed dimensions, origins and rotations, units 41/47, bounded vector rasters and the nearly-zero fallback, which is US Letter since patch 44. Accepted page-unit policy and licences are unchanged.
|
||||
|
||||
In `hayro-interpret`, Type 3 glyph bounding boxes are transformed to device space before intersecting clip and shading bounds; `raster::tests::shaded_type3_glyph_draws_at_multiple_positions` covers translated glyphs (#3178).
|
||||
|
||||
+139
-44
@@ -2,8 +2,8 @@ use crate::font::blob::{CffFontBlob, OpenTypeFontBlob};
|
||||
use crate::font::generated::{glyph_names, mac_os_roman, mac_roman, standard, win_ansi};
|
||||
use crate::font::standard_font::StandardKind;
|
||||
use crate::font::{
|
||||
Encoding, FallbackFontQuery, FontFlags, glyph_name_to_text, glyph_name_to_unicode, read_to_unicode,
|
||||
strip_subset_prefix, unicode_from_name,
|
||||
Encoding, FallbackFontQuery, FontFlags, glyph_name_to_text, glyph_name_to_unicode,
|
||||
normalized_glyph_name, read_to_unicode, strip_subset_prefix, unicode_from_name,
|
||||
};
|
||||
use crate::util::OptionLog;
|
||||
use crate::{CMapResolverFn, CacheKey, FontResolverFn};
|
||||
@@ -245,7 +245,7 @@ impl EmbeddedKind {
|
||||
fn new(dict: &Dict<'_>) -> Option<Self> {
|
||||
let descriptor = dict.get::<Dict<'_>>(FONT_DESC).unwrap_or_default();
|
||||
|
||||
let font_flags = descriptor.get::<u32>(FLAGS).and_then(FontFlags::from_bits);
|
||||
let font_flags = descriptor.get::<u32>(FLAGS).map(FontFlags::from_bits_truncate);
|
||||
|
||||
let (widths, missing_width) = read_widths(dict, &descriptor)?;
|
||||
let (encoding, differences) = read_encoding(dict);
|
||||
@@ -372,6 +372,133 @@ impl EmbeddedKind {
|
||||
self.base_font.outline_glyph(glyph)
|
||||
}
|
||||
|
||||
fn map_unicode_in_cmap(&self, c: char) -> Option<GlyphId> {
|
||||
let cmap = self.base_font.font_ref().cmap().ok()?;
|
||||
let mut glyph = None;
|
||||
|
||||
for record in cmap.encoding_records() {
|
||||
if record.platform_id() == PlatformId::Windows
|
||||
&& matches!(record.encoding_id(), 1 | 10)
|
||||
&& let Ok(subtable) = record.subtable(cmap.offset_data())
|
||||
{
|
||||
glyph = glyph
|
||||
.or_else(|| subtable.map_codepoint(c))
|
||||
.filter(|g| *g != GlyphId::NOTDEF);
|
||||
}
|
||||
}
|
||||
|
||||
if glyph.is_none() {
|
||||
for record in cmap.encoding_records() {
|
||||
if record.platform_id() == PlatformId::Unicode
|
||||
&& let Ok(subtable) = record.subtable(cmap.offset_data())
|
||||
{
|
||||
glyph = glyph
|
||||
.or_else(|| subtable.map_codepoint(c))
|
||||
.filter(|g| *g != GlyphId::NOTDEF);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glyph
|
||||
}
|
||||
|
||||
fn map_macintosh_cmap(&self, name: &str, unicode_char: Option<char>) -> Option<GlyphId> {
|
||||
let cmap = self.base_font.font_ref().cmap().ok()?;
|
||||
let mut glyph = None;
|
||||
for record in cmap.encoding_records() {
|
||||
if record.platform_id() == PlatformId::Macintosh
|
||||
&& record.encoding_id() == 0
|
||||
&& let Ok(subtable) = record.subtable(cmap.offset_data())
|
||||
{
|
||||
glyph = glyph.or_else(|| {
|
||||
mac_os_roman::get_inverse(name)
|
||||
.or_else(|| mac_roman::get_inverse(name))
|
||||
.or_else(|| {
|
||||
unicode_char
|
||||
.and_then(glyph_names::get_reverse)
|
||||
.and_then(|agl| {
|
||||
mac_os_roman::get_inverse(agl)
|
||||
.or_else(|| mac_roman::get_inverse(agl))
|
||||
})
|
||||
})
|
||||
.and_then(|c| subtable.map_codepoint(c))
|
||||
.filter(|g| *g != GlyphId::NOTDEF)
|
||||
});
|
||||
}
|
||||
}
|
||||
glyph
|
||||
}
|
||||
|
||||
fn map_name(&self, lookup: &str) -> Option<GlyphId> {
|
||||
let exact_name = self
|
||||
.glyph_names
|
||||
.get(lookup)
|
||||
.copied()
|
||||
.filter(|g| *g != GlyphId::NOTDEF);
|
||||
let exact_unicode = glyph_names::get(lookup)
|
||||
.and_then(|n| n.chars().next())
|
||||
.or_else(|| unicode_from_name(lookup));
|
||||
|
||||
let mut glyph =
|
||||
if glyph_names::get(lookup).is_some_and(|name| name.chars().nth(1).is_some()) {
|
||||
exact_name
|
||||
.or_else(|| exact_unicode.and_then(|c| self.map_unicode_in_cmap(c)))
|
||||
.or_else(|| self.map_macintosh_cmap(lookup, exact_unicode))
|
||||
} else {
|
||||
exact_unicode
|
||||
.and_then(|c| self.map_unicode_in_cmap(c))
|
||||
.or_else(|| self.map_macintosh_cmap(lookup, exact_unicode))
|
||||
.or_else(|| exact_name)
|
||||
};
|
||||
|
||||
if glyph.is_none() {
|
||||
let base_lookup = if !lookup.starts_with('.') {
|
||||
lookup.split('.').next().unwrap_or(lookup)
|
||||
} else {
|
||||
lookup
|
||||
};
|
||||
let unicode_char = exact_unicode.or_else(|| {
|
||||
glyph_names::get(base_lookup)
|
||||
.and_then(|n| n.chars().next())
|
||||
.or_else(|| unicode_from_name(base_lookup))
|
||||
});
|
||||
if base_lookup != lookup {
|
||||
glyph = unicode_char
|
||||
.and_then(|c| self.map_unicode_in_cmap(c))
|
||||
.or_else(|| self.map_macintosh_cmap(base_lookup, unicode_char));
|
||||
}
|
||||
if glyph.is_none() {
|
||||
glyph = self
|
||||
.glyph_names
|
||||
.get(base_lookup)
|
||||
.copied()
|
||||
.or_else(|| {
|
||||
self.glyph_names
|
||||
.get(normalized_glyph_name(base_lookup))
|
||||
.copied()
|
||||
})
|
||||
.or_else(|| {
|
||||
unicode_char
|
||||
.and_then(glyph_names::get_reverse)
|
||||
.and_then(|agl| self.glyph_names.get(agl).copied())
|
||||
})
|
||||
.or_else(|| {
|
||||
unicode_char.and_then(|c| {
|
||||
let cp = c as u32;
|
||||
self.glyph_names
|
||||
.get(&format!("uni{cp:04X}"))
|
||||
.or_else(|| self.glyph_names.get(&format!("u{cp:04X}")))
|
||||
.copied()
|
||||
})
|
||||
})
|
||||
.or_else(|| glyph_num_string(base_lookup).map(GlyphId::new))
|
||||
.filter(|g| *g != GlyphId::NOTDEF);
|
||||
}
|
||||
}
|
||||
|
||||
glyph
|
||||
}
|
||||
|
||||
fn map_code(&self, code: u8) -> GlyphId {
|
||||
if let Some(glyph) = self.cached_mappings.borrow().get(&code) {
|
||||
return *glyph;
|
||||
@@ -384,51 +511,19 @@ impl EmbeddedKind {
|
||||
.unwrap_or(GlyphId::NOTDEF);
|
||||
}
|
||||
|
||||
// PdfCraft patch: keep glyph selection separate from text extraction. Symbolic
|
||||
// TrueType fonts ignore Encoding (ISO 32000-1 9.6.6.4); ToUnicode supplies text
|
||||
// semantics (9.10), not an alternate glyph. Extend only the existing name lookup
|
||||
// with Unicode cmap records and post-name aliases. Keep the raw symbolic fallback
|
||||
// and the CFF path unchanged until affected source files justify a different policy.
|
||||
let mut glyph = None;
|
||||
|
||||
if self.is_non_symbolic() {
|
||||
let Some(lookup) = self.code_to_name(code) else {
|
||||
return GlyphId::NOTDEF;
|
||||
};
|
||||
|
||||
if let Ok(cmap) = self.base_font.font_ref().cmap() {
|
||||
for record in cmap.encoding_records() {
|
||||
if record.platform_id() == PlatformId::Windows
|
||||
&& record.encoding_id() == 1
|
||||
&& let Ok(subtable) = record.subtable(cmap.offset_data())
|
||||
{
|
||||
glyph = glyph.or_else(|| {
|
||||
glyph_names::get(lookup)
|
||||
.map(|n| n.to_string())
|
||||
.or_else(|| unicode_from_name(lookup).map(|n| n.to_string()))
|
||||
.and_then(|n| n.chars().next())
|
||||
.and_then(|c| subtable.map_codepoint(c))
|
||||
.filter(|g| *g != GlyphId::NOTDEF)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for record in cmap.encoding_records() {
|
||||
if record.platform_id() == PlatformId::Macintosh
|
||||
&& record.encoding_id() == 0
|
||||
&& let Ok(subtable) = record.subtable(cmap.offset_data())
|
||||
{
|
||||
glyph = glyph.or_else(|| {
|
||||
mac_os_roman::get_inverse(lookup)
|
||||
.or_else(|| mac_roman::get_inverse(lookup))
|
||||
.and_then(|c| subtable.map_codepoint(c))
|
||||
.filter(|g| *g != GlyphId::NOTDEF)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if glyph.is_none() {
|
||||
if let Some(gid) = self.glyph_names.get(lookup) {
|
||||
glyph = Some(*gid);
|
||||
} else if let Some(gid) = glyph_num_string(lookup) {
|
||||
glyph = Some(GlyphId::new(gid));
|
||||
}
|
||||
// An explicit .notdef is a mapping, not permission to draw the raw code.
|
||||
if lookup != ".notdef" {
|
||||
glyph = self.map_name(lookup);
|
||||
}
|
||||
} else if let Ok(cmap) = self.base_font.font_ref().cmap() {
|
||||
// PdfCraft patch (#4156): the Windows (3, 0) subtable comes first, and the (1, 0) one
|
||||
@@ -658,7 +753,7 @@ pub(crate) fn read_encoding(dict: &Dict<'_>) -> (Encoding, HashMap<u8, String>)
|
||||
if let Ok(code) = code.try_into() {
|
||||
map.insert(code, name.as_str().to_string());
|
||||
}
|
||||
code += 1;
|
||||
code = code.saturating_add(1);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user