From ff2502699c6112cca148dfb8c77daaf35d0fe51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 12 Nov 2026 00:54:10 +0100 Subject: [PATCH 1/3] Add Adobe Glyph List resolution helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- native/bridge/src/adobe_glyph_names.inc | 596 ++++++++++++++++++++++++ native/bridge/src/jpdfium_qpdf.cpp | 143 +++++- 2 files changed, 732 insertions(+), 7 deletions(-) create mode 100644 native/bridge/src/adobe_glyph_names.inc diff --git a/native/bridge/src/adobe_glyph_names.inc b/native/bridge/src/adobe_glyph_names.inc new file mode 100644 index 000000000..d4d972a8b --- /dev/null +++ b/native/bridge/src/adobe_glyph_names.inc @@ -0,0 +1,596 @@ +// Generated from the Adobe Glyph List (AGL) glyph name to Unicode map. +// Source: fontTools fontTools/agl.py, derived from Adobe glyphlist.txt. +// SPDX-License-Identifier: MIT +struct AdobeGlyphName { + const char* name; + uint32_t unicode; +}; + +static constexpr AdobeGlyphName kAdobeGlyphNames[] = { + {"A", 0x41}, + {"AE", 0xC6}, + {"AEacute", 0x1FC}, + {"Aacute", 0xC1}, + {"Abreve", 0x102}, + {"Acircumflex", 0xC2}, + {"Adieresis", 0xC4}, + {"Agrave", 0xC0}, + {"Alpha", 0x391}, + {"Alphatonos", 0x386}, + {"Amacron", 0x100}, + {"Aogonek", 0x104}, + {"Aring", 0xC5}, + {"Aringacute", 0x1FA}, + {"Atilde", 0xC3}, + {"B", 0x42}, + {"Beta", 0x392}, + {"C", 0x43}, + {"Cacute", 0x106}, + {"Ccaron", 0x10C}, + {"Ccedilla", 0xC7}, + {"Ccircumflex", 0x108}, + {"Cdotaccent", 0x10A}, + {"Chi", 0x3A7}, + {"D", 0x44}, + {"Dcaron", 0x10E}, + {"Dcroat", 0x110}, + {"Delta", 0x2206}, + {"E", 0x45}, + {"Eacute", 0xC9}, + {"Ebreve", 0x114}, + {"Ecaron", 0x11A}, + {"Ecircumflex", 0xCA}, + {"Edieresis", 0xCB}, + {"Edotaccent", 0x116}, + {"Egrave", 0xC8}, + {"Emacron", 0x112}, + {"Eng", 0x14A}, + {"Eogonek", 0x118}, + {"Epsilon", 0x395}, + {"Epsilontonos", 0x388}, + {"Eta", 0x397}, + {"Etatonos", 0x389}, + {"Eth", 0xD0}, + {"Euro", 0x20AC}, + {"F", 0x46}, + {"G", 0x47}, + {"Gamma", 0x393}, + {"Gbreve", 0x11E}, + {"Gcaron", 0x1E6}, + {"Gcircumflex", 0x11C}, + {"Gdotaccent", 0x120}, + {"H", 0x48}, + {"H18533", 0x25CF}, + {"H18543", 0x25AA}, + {"H18551", 0x25AB}, + {"H22073", 0x25A1}, + {"Hbar", 0x126}, + {"Hcircumflex", 0x124}, + {"I", 0x49}, + {"IJ", 0x132}, + {"Iacute", 0xCD}, + {"Ibreve", 0x12C}, + {"Icircumflex", 0xCE}, + {"Idieresis", 0xCF}, + {"Idotaccent", 0x130}, + {"Ifraktur", 0x2111}, + {"Igrave", 0xCC}, + {"Imacron", 0x12A}, + {"Iogonek", 0x12E}, + {"Iota", 0x399}, + {"Iotadieresis", 0x3AA}, + {"Iotatonos", 0x38A}, + {"Itilde", 0x128}, + {"J", 0x4A}, + {"Jcircumflex", 0x134}, + {"K", 0x4B}, + {"Kappa", 0x39A}, + {"L", 0x4C}, + {"Lacute", 0x139}, + {"Lambda", 0x39B}, + {"Lcaron", 0x13D}, + {"Ldot", 0x13F}, + {"Lslash", 0x141}, + {"M", 0x4D}, + {"Mu", 0x39C}, + {"N", 0x4E}, + {"Nacute", 0x143}, + {"Ncaron", 0x147}, + {"Ntilde", 0xD1}, + {"Nu", 0x39D}, + {"O", 0x4F}, + {"OE", 0x152}, + {"Oacute", 0xD3}, + {"Obreve", 0x14E}, + {"Ocircumflex", 0xD4}, + {"Odieresis", 0xD6}, + {"Ograve", 0xD2}, + {"Ohorn", 0x1A0}, + {"Ohungarumlaut", 0x150}, + {"Omacron", 0x14C}, + {"Omega", 0x2126}, + {"Omegatonos", 0x38F}, + {"Omicron", 0x39F}, + {"Omicrontonos", 0x38C}, + {"Oslash", 0xD8}, + {"Oslashacute", 0x1FE}, + {"Otilde", 0xD5}, + {"P", 0x50}, + {"Phi", 0x3A6}, + {"Pi", 0x3A0}, + {"Psi", 0x3A8}, + {"Q", 0x51}, + {"R", 0x52}, + {"Racute", 0x154}, + {"Rcaron", 0x158}, + {"Rfraktur", 0x211C}, + {"Rho", 0x3A1}, + {"S", 0x53}, + {"SF010000", 0x250C}, + {"SF020000", 0x2514}, + {"SF030000", 0x2510}, + {"SF040000", 0x2518}, + {"SF050000", 0x253C}, + {"SF060000", 0x252C}, + {"SF070000", 0x2534}, + {"SF080000", 0x251C}, + {"SF090000", 0x2524}, + {"SF100000", 0x2500}, + {"SF110000", 0x2502}, + {"SF190000", 0x2561}, + {"SF200000", 0x2562}, + {"SF210000", 0x2556}, + {"SF220000", 0x2555}, + {"SF230000", 0x2563}, + {"SF240000", 0x2551}, + {"SF250000", 0x2557}, + {"SF260000", 0x255D}, + {"SF270000", 0x255C}, + {"SF280000", 0x255B}, + {"SF360000", 0x255E}, + {"SF370000", 0x255F}, + {"SF380000", 0x255A}, + {"SF390000", 0x2554}, + {"SF400000", 0x2569}, + {"SF410000", 0x2566}, + {"SF420000", 0x2560}, + {"SF430000", 0x2550}, + {"SF440000", 0x256C}, + {"SF450000", 0x2567}, + {"SF460000", 0x2568}, + {"SF470000", 0x2564}, + {"SF480000", 0x2565}, + {"SF490000", 0x2559}, + {"SF500000", 0x2558}, + {"SF510000", 0x2552}, + {"SF520000", 0x2553}, + {"SF530000", 0x256B}, + {"SF540000", 0x256A}, + {"Sacute", 0x15A}, + {"Scaron", 0x160}, + {"Scedilla", 0x15E}, + {"Scircumflex", 0x15C}, + {"Sigma", 0x3A3}, + {"T", 0x54}, + {"Tau", 0x3A4}, + {"Tbar", 0x166}, + {"Tcaron", 0x164}, + {"Theta", 0x398}, + {"Thorn", 0xDE}, + {"U", 0x55}, + {"Uacute", 0xDA}, + {"Ubreve", 0x16C}, + {"Ucircumflex", 0xDB}, + {"Udieresis", 0xDC}, + {"Ugrave", 0xD9}, + {"Uhorn", 0x1AF}, + {"Uhungarumlaut", 0x170}, + {"Umacron", 0x16A}, + {"Uogonek", 0x172}, + {"Upsilon", 0x3A5}, + {"Upsilon1", 0x3D2}, + {"Upsilondieresis", 0x3AB}, + {"Upsilontonos", 0x38E}, + {"Uring", 0x16E}, + {"Utilde", 0x168}, + {"V", 0x56}, + {"W", 0x57}, + {"Wacute", 0x1E82}, + {"Wcircumflex", 0x174}, + {"Wdieresis", 0x1E84}, + {"Wgrave", 0x1E80}, + {"X", 0x58}, + {"Xi", 0x39E}, + {"Y", 0x59}, + {"Yacute", 0xDD}, + {"Ycircumflex", 0x176}, + {"Ydieresis", 0x178}, + {"Ygrave", 0x1EF2}, + {"Z", 0x5A}, + {"Zacute", 0x179}, + {"Zcaron", 0x17D}, + {"Zdotaccent", 0x17B}, + {"Zeta", 0x396}, + {"a", 0x61}, + {"aacute", 0xE1}, + {"abreve", 0x103}, + {"acircumflex", 0xE2}, + {"acute", 0xB4}, + {"acutecomb", 0x301}, + {"adieresis", 0xE4}, + {"ae", 0xE6}, + {"aeacute", 0x1FD}, + {"agrave", 0xE0}, + {"aleph", 0x2135}, + {"alpha", 0x3B1}, + {"alphatonos", 0x3AC}, + {"amacron", 0x101}, + {"ampersand", 0x26}, + {"angle", 0x2220}, + {"angleleft", 0x2329}, + {"angleright", 0x232A}, + {"anoteleia", 0x387}, + {"aogonek", 0x105}, + {"approxequal", 0x2248}, + {"aring", 0xE5}, + {"aringacute", 0x1FB}, + {"arrowboth", 0x2194}, + {"arrowdblboth", 0x21D4}, + {"arrowdbldown", 0x21D3}, + {"arrowdblleft", 0x21D0}, + {"arrowdblright", 0x21D2}, + {"arrowdblup", 0x21D1}, + {"arrowdown", 0x2193}, + {"arrowleft", 0x2190}, + {"arrowright", 0x2192}, + {"arrowup", 0x2191}, + {"arrowupdn", 0x2195}, + {"arrowupdnbse", 0x21A8}, + {"asciicircum", 0x5E}, + {"asciitilde", 0x7E}, + {"asterisk", 0x2A}, + {"asteriskmath", 0x2217}, + {"at", 0x40}, + {"atilde", 0xE3}, + {"b", 0x62}, + {"backslash", 0x5C}, + {"bar", 0x7C}, + {"beta", 0x3B2}, + {"block", 0x2588}, + {"braceleft", 0x7B}, + {"braceright", 0x7D}, + {"bracketleft", 0x5B}, + {"bracketright", 0x5D}, + {"breve", 0x2D8}, + {"brokenbar", 0xA6}, + {"bullet", 0x2022}, + {"c", 0x63}, + {"cacute", 0x107}, + {"caron", 0x2C7}, + {"carriagereturn", 0x21B5}, + {"ccaron", 0x10D}, + {"ccedilla", 0xE7}, + {"ccircumflex", 0x109}, + {"cdotaccent", 0x10B}, + {"cedilla", 0xB8}, + {"cent", 0xA2}, + {"chi", 0x3C7}, + {"circle", 0x25CB}, + {"circlemultiply", 0x2297}, + {"circleplus", 0x2295}, + {"circumflex", 0x2C6}, + {"club", 0x2663}, + {"colon", 0x3A}, + {"colonmonetary", 0x20A1}, + {"comma", 0x2C}, + {"congruent", 0x2245}, + {"copyright", 0xA9}, + {"currency", 0xA4}, + {"d", 0x64}, + {"dagger", 0x2020}, + {"daggerdbl", 0x2021}, + {"dcaron", 0x10F}, + {"dcroat", 0x111}, + {"degree", 0xB0}, + {"delta", 0x3B4}, + {"diamond", 0x2666}, + {"dieresis", 0xA8}, + {"dieresistonos", 0x385}, + {"divide", 0xF7}, + {"dkshade", 0x2593}, + {"dnblock", 0x2584}, + {"dollar", 0x24}, + {"dong", 0x20AB}, + {"dotaccent", 0x2D9}, + {"dotbelowcomb", 0x323}, + {"dotlessi", 0x131}, + {"dotmath", 0x22C5}, + {"e", 0x65}, + {"eacute", 0xE9}, + {"ebreve", 0x115}, + {"ecaron", 0x11B}, + {"ecircumflex", 0xEA}, + {"edieresis", 0xEB}, + {"edotaccent", 0x117}, + {"egrave", 0xE8}, + {"eight", 0x38}, + {"element", 0x2208}, + {"ellipsis", 0x2026}, + {"emacron", 0x113}, + {"emdash", 0x2014}, + {"emptyset", 0x2205}, + {"endash", 0x2013}, + {"eng", 0x14B}, + {"eogonek", 0x119}, + {"epsilon", 0x3B5}, + {"epsilontonos", 0x3AD}, + {"equal", 0x3D}, + {"equivalence", 0x2261}, + {"estimated", 0x212E}, + {"eta", 0x3B7}, + {"etatonos", 0x3AE}, + {"eth", 0xF0}, + {"exclam", 0x21}, + {"exclamdbl", 0x203C}, + {"exclamdown", 0xA1}, + {"existential", 0x2203}, + {"f", 0x66}, + {"female", 0x2640}, + {"figuredash", 0x2012}, + {"filledbox", 0x25A0}, + {"filledrect", 0x25AC}, + {"five", 0x35}, + {"fiveeighths", 0x215D}, + {"florin", 0x192}, + {"four", 0x34}, + {"fraction", 0x2044}, + {"franc", 0x20A3}, + {"g", 0x67}, + {"gamma", 0x3B3}, + {"gbreve", 0x11F}, + {"gcaron", 0x1E7}, + {"gcircumflex", 0x11D}, + {"gdotaccent", 0x121}, + {"germandbls", 0xDF}, + {"gradient", 0x2207}, + {"grave", 0x60}, + {"gravecomb", 0x300}, + {"greater", 0x3E}, + {"greaterequal", 0x2265}, + {"guillemotleft", 0xAB}, + {"guillemotright", 0xBB}, + {"guilsinglleft", 0x2039}, + {"guilsinglright", 0x203A}, + {"h", 0x68}, + {"hbar", 0x127}, + {"hcircumflex", 0x125}, + {"heart", 0x2665}, + {"hookabovecomb", 0x309}, + {"house", 0x2302}, + {"hungarumlaut", 0x2DD}, + {"hyphen", 0x2D}, + {"i", 0x69}, + {"iacute", 0xED}, + {"ibreve", 0x12D}, + {"icircumflex", 0xEE}, + {"idieresis", 0xEF}, + {"igrave", 0xEC}, + {"ij", 0x133}, + {"imacron", 0x12B}, + {"infinity", 0x221E}, + {"integral", 0x222B}, + {"integralbt", 0x2321}, + {"integraltp", 0x2320}, + {"intersection", 0x2229}, + {"invbullet", 0x25D8}, + {"invcircle", 0x25D9}, + {"invsmileface", 0x263B}, + {"iogonek", 0x12F}, + {"iota", 0x3B9}, + {"iotadieresis", 0x3CA}, + {"iotadieresistonos", 0x390}, + {"iotatonos", 0x3AF}, + {"itilde", 0x129}, + {"j", 0x6A}, + {"jcircumflex", 0x135}, + {"k", 0x6B}, + {"kappa", 0x3BA}, + {"kgreenlandic", 0x138}, + {"l", 0x6C}, + {"lacute", 0x13A}, + {"lambda", 0x3BB}, + {"lcaron", 0x13E}, + {"ldot", 0x140}, + {"less", 0x3C}, + {"lessequal", 0x2264}, + {"lfblock", 0x258C}, + {"lira", 0x20A4}, + {"logicaland", 0x2227}, + {"logicalnot", 0xAC}, + {"logicalor", 0x2228}, + {"longs", 0x17F}, + {"lozenge", 0x25CA}, + {"lslash", 0x142}, + {"ltshade", 0x2591}, + {"m", 0x6D}, + {"macron", 0xAF}, + {"male", 0x2642}, + {"minus", 0x2212}, + {"minute", 0x2032}, + {"mu", 0xB5}, + {"multiply", 0xD7}, + {"musicalnote", 0x266A}, + {"musicalnotedbl", 0x266B}, + {"n", 0x6E}, + {"nacute", 0x144}, + {"napostrophe", 0x149}, + {"ncaron", 0x148}, + {"nine", 0x39}, + {"notelement", 0x2209}, + {"notequal", 0x2260}, + {"notsubset", 0x2284}, + {"ntilde", 0xF1}, + {"nu", 0x3BD}, + {"numbersign", 0x23}, + {"o", 0x6F}, + {"oacute", 0xF3}, + {"obreve", 0x14F}, + {"ocircumflex", 0xF4}, + {"odieresis", 0xF6}, + {"oe", 0x153}, + {"ogonek", 0x2DB}, + {"ograve", 0xF2}, + {"ohorn", 0x1A1}, + {"ohungarumlaut", 0x151}, + {"omacron", 0x14D}, + {"omega", 0x3C9}, + {"omega1", 0x3D6}, + {"omegatonos", 0x3CE}, + {"omicron", 0x3BF}, + {"omicrontonos", 0x3CC}, + {"one", 0x31}, + {"onedotenleader", 0x2024}, + {"oneeighth", 0x215B}, + {"onehalf", 0xBD}, + {"onequarter", 0xBC}, + {"onethird", 0x2153}, + {"openbullet", 0x25E6}, + {"ordfeminine", 0xAA}, + {"ordmasculine", 0xBA}, + {"orthogonal", 0x221F}, + {"oslash", 0xF8}, + {"oslashacute", 0x1FF}, + {"otilde", 0xF5}, + {"p", 0x70}, + {"paragraph", 0xB6}, + {"parenleft", 0x28}, + {"parenright", 0x29}, + {"partialdiff", 0x2202}, + {"percent", 0x25}, + {"period", 0x2E}, + {"periodcentered", 0xB7}, + {"perpendicular", 0x22A5}, + {"perthousand", 0x2030}, + {"peseta", 0x20A7}, + {"phi", 0x3C6}, + {"phi1", 0x3D5}, + {"pi", 0x3C0}, + {"plus", 0x2B}, + {"plusminus", 0xB1}, + {"prescription", 0x211E}, + {"product", 0x220F}, + {"propersubset", 0x2282}, + {"propersuperset", 0x2283}, + {"proportional", 0x221D}, + {"psi", 0x3C8}, + {"q", 0x71}, + {"question", 0x3F}, + {"questiondown", 0xBF}, + {"quotedbl", 0x22}, + {"quotedblbase", 0x201E}, + {"quotedblleft", 0x201C}, + {"quotedblright", 0x201D}, + {"quoteleft", 0x2018}, + {"quotereversed", 0x201B}, + {"quoteright", 0x2019}, + {"quotesinglbase", 0x201A}, + {"quotesingle", 0x27}, + {"r", 0x72}, + {"racute", 0x155}, + {"radical", 0x221A}, + {"rcaron", 0x159}, + {"reflexsubset", 0x2286}, + {"reflexsuperset", 0x2287}, + {"registered", 0xAE}, + {"revlogicalnot", 0x2310}, + {"rho", 0x3C1}, + {"ring", 0x2DA}, + {"rtblock", 0x2590}, + {"s", 0x73}, + {"sacute", 0x15B}, + {"scaron", 0x161}, + {"scedilla", 0x15F}, + {"scircumflex", 0x15D}, + {"second", 0x2033}, + {"section", 0xA7}, + {"semicolon", 0x3B}, + {"seven", 0x37}, + {"seveneighths", 0x215E}, + {"shade", 0x2592}, + {"sigma", 0x3C3}, + {"sigma1", 0x3C2}, + {"similar", 0x223C}, + {"six", 0x36}, + {"slash", 0x2F}, + {"smileface", 0x263A}, + {"space", 0x20}, + {"spade", 0x2660}, + {"sterling", 0xA3}, + {"suchthat", 0x220B}, + {"summation", 0x2211}, + {"sun", 0x263C}, + {"t", 0x74}, + {"tau", 0x3C4}, + {"tbar", 0x167}, + {"tcaron", 0x165}, + {"therefore", 0x2234}, + {"theta", 0x3B8}, + {"theta1", 0x3D1}, + {"thorn", 0xFE}, + {"three", 0x33}, + {"threeeighths", 0x215C}, + {"threequarters", 0xBE}, + {"tilde", 0x2DC}, + {"tildecomb", 0x303}, + {"tonos", 0x384}, + {"trademark", 0x2122}, + {"triagdn", 0x25BC}, + {"triaglf", 0x25C4}, + {"triagrt", 0x25BA}, + {"triagup", 0x25B2}, + {"two", 0x32}, + {"twodotenleader", 0x2025}, + {"twothirds", 0x2154}, + {"u", 0x75}, + {"uacute", 0xFA}, + {"ubreve", 0x16D}, + {"ucircumflex", 0xFB}, + {"udieresis", 0xFC}, + {"ugrave", 0xF9}, + {"uhorn", 0x1B0}, + {"uhungarumlaut", 0x171}, + {"umacron", 0x16B}, + {"underscore", 0x5F}, + {"underscoredbl", 0x2017}, + {"union", 0x222A}, + {"universal", 0x2200}, + {"uogonek", 0x173}, + {"upblock", 0x2580}, + {"upsilon", 0x3C5}, + {"upsilondieresis", 0x3CB}, + {"upsilondieresistonos", 0x3B0}, + {"upsilontonos", 0x3CD}, + {"uring", 0x16F}, + {"utilde", 0x169}, + {"v", 0x76}, + {"w", 0x77}, + {"wacute", 0x1E83}, + {"wcircumflex", 0x175}, + {"wdieresis", 0x1E85}, + {"weierstrass", 0x2118}, + {"wgrave", 0x1E81}, + {"x", 0x78}, + {"xi", 0x3BE}, + {"y", 0x79}, + {"yacute", 0xFD}, + {"ycircumflex", 0x177}, + {"ydieresis", 0xFF}, + {"yen", 0xA5}, + {"ygrave", 0x1EF3}, + {"z", 0x7A}, + {"zacute", 0x17A}, + {"zcaron", 0x17E}, + {"zdotaccent", 0x17C}, + {"zero", 0x30}, + {"zeta", 0x3B6}, +}; diff --git a/native/bridge/src/jpdfium_qpdf.cpp b/native/bridge/src/jpdfium_qpdf.cpp index cb88816c6..0dd71ff2c 100644 --- a/native/bridge/src/jpdfium_qpdf.cpp +++ b/native/bridge/src/jpdfium_qpdf.cpp @@ -67,6 +67,8 @@ namespace { +#include "adobe_glyph_names.inc" + // Create the output with owner-only permissions on POSIX (CodeQL // cpp/world-writable-file-creation); Windows has no mode argument here. // @@ -1774,10 +1776,26 @@ FT_UInt glyphForName(FT_Face face, std::string const& name) { if (FT_HAS_GLYPH_NAMES(face)) { if (FT_UInt gid = FT_Get_Name_Index(face, name.c_str())) return gid; } - uint32_t u = name.size() == 7 && name.starts_with("uni") - ? std::strtoul(name.c_str() + 3, nullptr, 16) - : name.size() == 1 ? uint8_t(name[0]) - : 0; + uint32_t u = name.size() == 1 ? uint8_t(name[0]) : 0; + if (!u && name.size() >= 5 && name[0] == 'u') { + bool hex = name.size() <= 7; + for (size_t i = 1; hex && i < name.size(); ++i) hex = std::isxdigit(uint8_t(name[i])); + if (hex) u = uint32_t(std::strtoul(name.c_str() + 1, nullptr, 16)); + } + if (!u && name.size() == 7 && name.starts_with("uni")) { + bool hex = true; + for (size_t i = 3; i < 7; ++i) hex = std::isxdigit(uint8_t(name[i])); + if (hex) u = uint32_t(std::strtoul(name.c_str() + 3, nullptr, 16)); + } + if (!u) { + auto const* first = std::begin(kAdobeGlyphNames); + auto const* last = std::end(kAdobeGlyphNames); + auto it = std::lower_bound(first, last, name, + [](AdobeGlyphName const& entry, std::string const& value) { + return std::strcmp(entry.name, value.c_str()) < 0; + }); + if (it != last && name == it->name) u = it->unicode; + } return u && !FT_Select_Charmap(face, FT_ENCODING_UNICODE) ? FT_Get_Char_Index(face, u) : 0; } @@ -1906,6 +1924,88 @@ void setToUnicode(QPDF& qpdf, QPDFObjectHandle font, std::map parseToUnicode(QPDFObjectHandle const& font) { + std::map map; + auto stream = font.getKey("/ToUnicode"); + if (!stream.isStream()) return map; + std::shared_ptr data = stream.getStreamData(qpdf_dl_generalized); + if (!data || !data->getSize()) return map; + std::string text(reinterpret_cast(data->getBuffer()), data->getSize()); + + auto hexAt = [&](size_t& pos, uint32_t& value) { + if (pos >= text.size() || text[pos] != '<') return false; + ++pos; + size_t start = pos; + while (pos < text.size() && text[pos] != '>') ++pos; + if (pos >= text.size() || pos == start || pos - start > 8) return false; + value = uint32_t(std::strtoul(text.substr(start, pos - start).c_str(), nullptr, 16)); + ++pos; + return true; + }; + auto skipSpace = [&](size_t& pos) { + while (pos < text.size() && std::isspace(uint8_t(text[pos]))) ++pos; + }; + auto find = [&](std::string const& token, size_t from) { return text.find(token, from); }; + + size_t pos = find("beginbfchar", 0); + if (pos != std::string::npos) { + pos += std::strlen("beginbfchar"); + size_t end = find("endbfchar", pos); + while (pos < end) { + skipSpace(pos); + uint32_t code = 0; + if (!hexAt(pos, code)) { + ++pos; + continue; + } + skipSpace(pos); + uint32_t unicode = 0; + if (hexAt(pos, unicode)) map[code] = unicode; + } + } + pos = find("beginbfrange", 0); + if (pos != std::string::npos) { + pos += std::strlen("beginbfrange"); + size_t end = find("endbfrange", pos); + while (pos < end) { + skipSpace(pos); + uint32_t low = 0; + if (!hexAt(pos, low)) { + ++pos; + continue; + } + skipSpace(pos); + uint32_t high = 0; + if (!hexAt(pos, high) || high < low) continue; + skipSpace(pos); + if (pos < end && text[pos] == '[') { + ++pos; + for (uint32_t code = low; code <= high; ++code) { + skipSpace(pos); + uint32_t unicode = 0; + if (!hexAt(pos, unicode)) break; + map[code] = unicode; + } + while (pos < end && text[pos] != ']') ++pos; + if (pos < end) ++pos; + } else { + uint32_t unicode = 0; + if (!hexAt(pos, unicode)) continue; + for (uint32_t code = low; code <= high; ++code) map[code] = unicode + code - low; + } + } + } + return map; +} + +bool cidGlyphOrderMatches(FT_Face face, std::map const& unicodeByCid) { + if (unicodeByCid.empty() || FT_Select_Charmap(face, FT_ENCODING_UNICODE)) return false; + for (auto const& [cid, unicode] : unicodeByCid) { + if (FT_Get_Char_Index(face, unicode) != cid) return false; + } + return true; +} + void pinTrueTypeMapping(QPDF& qpdf, QPDFObjectHandle font, FT_Face face, bool symbolic) { auto enc = parseEncoding(font); auto unicodeOf = unicodeByGlyph(face, symbolic); @@ -2090,6 +2190,18 @@ EmbedFontsResult embedFonts(std::function const& load, (descriptor.hasKey("/FontFile") || descriptor.hasKey("/FontFile2") || descriptor.hasKey("/FontFile3")); if (hasProgram && onlyMissing) continue; + auto cidMap = target.getKey("/CIDToGIDMap"); + bool identityCidMap = cidMap.isNull() || cidMap.isNameAndEquals("/Identity"); + std::map cidUnicode; + bool cidRemap = false; + if (cid && !hasProgram && onlyMissing) { + if (!identityCidMap) continue; + cidUnicode = parseToUnicode(font); + if (cidUnicode.empty()) continue; + bool sameOrder = cidGlyphOrderMatches(program.face, cidUnicode); + if (!sameOrder && program.kind != ProgramKind::TrueType) continue; + cidRemap = !sameOrder; + } if (descriptor.isIndirect()) { // A descriptor can be shared by a font that is not being // replaced. Always isolate it before installing a program so @@ -2149,13 +2261,30 @@ EmbedFontsResult embedFonts(std::function const& load, target.replaceKey("/CIDToGIDMap", QPDFObjectHandle::newName("/Identity")); if (!cid && glyf) pinTrueTypeMapping(*qpdf, target, program.face, symbolic); } - auto gidMap = target.getKey("/CIDToGIDMap"); if (cid && glyf && !hasProgram && (font.getKey("/Encoding").isNameAndEquals("/Identity-H") || font.getKey("/Encoding").isNameAndEquals("/Identity-V")) && - (gidMap.isNull() || gidMap.isNameAndEquals("/Identity"))) { + identityCidMap) { std::map codes; - for (auto [gid, u] : unicodeByGlyph(program.face, symbolic)) codes[gid] = u; + std::map gidByCid; + uint32_t maxCid = 0; + for (auto [cid, u] : cidUnicode) { + if (FT_UInt gid = FT_Get_Char_Index(program.face, u)) { + codes[cid] = u; + gidByCid[cid] = gid; + maxCid = std::max(maxCid, cid); + } + } + if (cidRemap && !gidByCid.empty()) { + std::vector map(2 * (size_t(maxCid) + 1), 0); + for (auto [cid, gid] : gidByCid) { + map[2 * cid] = uint8_t(gid >> 8); + map[2 * cid + 1] = uint8_t(gid & 0xFF); + } + target.replaceKey("/CIDToGIDMap", + QPDFObjectHandle::newStream( + qpdf.get(), std::string(map.begin(), map.end()))); + } setToUnicode(*qpdf, font, codes, true); } auto& stream = program.streams[{kind, cid, glyphs}]; From 815dce0484776cf411d38be6b4d022e89627844f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 12 Nov 2026 02:59:29 +0100 Subject: [PATCH 2/3] Rename CID loop bindings to avoid shadowing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- native/bridge/src/jpdfium_qpdf.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/native/bridge/src/jpdfium_qpdf.cpp b/native/bridge/src/jpdfium_qpdf.cpp index 0dd71ff2c..9e087ad22 100644 --- a/native/bridge/src/jpdfium_qpdf.cpp +++ b/native/bridge/src/jpdfium_qpdf.cpp @@ -2268,18 +2268,18 @@ EmbedFontsResult embedFonts(std::function const& load, std::map codes; std::map gidByCid; uint32_t maxCid = 0; - for (auto [cid, u] : cidUnicode) { + for (auto [code, u] : cidUnicode) { if (FT_UInt gid = FT_Get_Char_Index(program.face, u)) { - codes[cid] = u; - gidByCid[cid] = gid; - maxCid = std::max(maxCid, cid); + codes[code] = u; + gidByCid[code] = gid; + maxCid = std::max(maxCid, code); } } if (cidRemap && !gidByCid.empty()) { std::vector map(2 * (size_t(maxCid) + 1), 0); - for (auto [cid, gid] : gidByCid) { - map[2 * cid] = uint8_t(gid >> 8); - map[2 * cid + 1] = uint8_t(gid & 0xFF); + for (auto [code, gid] : gidByCid) { + map[2 * code] = uint8_t(gid >> 8); + map[2 * code + 1] = uint8_t(gid & 0xFF); } target.replaceKey("/CIDToGIDMap", QPDFObjectHandle::newStream( From cf0753b005d855f01a9d09e1977a88dbb2cd78c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Sun, 15 Nov 2026 21:58:14 +0100 Subject: [PATCH 3/3] Bound ToUnicode CMap range parsing and isolate custom font directories MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../software/jpdfium/doc/PdfFontEmbedder.java | 9 +- .../jpdfium/doc/PdfFontEmbedderTest.java | 67 --------- native/bridge/src/jpdfium_qpdf.cpp | 134 +++++++++++++----- 3 files changed, 105 insertions(+), 105 deletions(-) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java index 13f3b8a49..a30d28cbc 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java @@ -317,8 +317,12 @@ public static FontStatus diagnose(String baseName, Path... fontDirs) { } private static Map indexFor(Path... fontDirs) { - return INDEXES.computeIfAbsent( + Map index = INDEXES.computeIfAbsent( fontDirs.length == 0 ? SYSTEM_FONT_DIRS : List.of(fontDirs), PdfFontEmbedder::index); + if (fontDirs.length == 0) return index; + Map isolated = new HashMap<>(index); + isolated.entrySet().removeIf(entry -> entry.getValue().resource() != null); + return isolated; } private static String source(Face face) { @@ -341,8 +345,7 @@ public static FileResult embedMissingTo(Path input, Path output, Path... fontDir private static Map programsFor(List missing, Path... fontDirs) throws IOException { - Map index = INDEXES.computeIfAbsent( - fontDirs.length == 0 ? SYSTEM_FONT_DIRS : List.of(fontDirs), PdfFontEmbedder::index); + Map index = indexFor(fontDirs); Map found = new LinkedHashMap<>(); Map programs = new HashMap<>(); for (String name : missing) { diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java index 796cf0051..283400eab 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java @@ -2,7 +2,6 @@ import stirling.software.jpdfium.PdfDocument; import stirling.software.jpdfium.exception.JPDFiumException; -import stirling.software.jpdfium.panama.QpdfLib; import org.apache.pdfbox.Loader; import org.apache.pdfbox.cos.COSArray; @@ -443,18 +442,6 @@ private static void assertCidRemap(String toUnicodeBody) throws Exception { } } - @Test - void cidSubstituteWithoutFullGlyphCoverageIsLeftUnresolved() throws Exception { - assertCidSubstituteUnresolved(""" - 1 beginbfchar - <0001> <0041> - endbfchar - 1 beginbfchar - <0002> - endbfchar - """); - } - @Test void oversizedToUnicodeRangeLeavesCidFontUnresolved() throws Exception { assertCidSubstituteUnresolved(""" @@ -490,11 +477,6 @@ void multiCharacterRangeDestinationLeavesCidFontUnresolved() throws Exception { """); } - @Test - void cidSubstituteMissingAUsedWidthIsLeftUnresolved() throws Exception { - assertCidSubstituteUnresolved("<0001> <0041>", 1, 2); - } - private static void assertCidSubstituteUnresolved( String toUnicodeBody, int... widthCids) throws Exception { assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable"); @@ -528,55 +510,6 @@ private static void assertCidSubstituteUnresolved( } } - @Test - void explicitCidReplacementGeneratesToUnicode() throws Exception { - assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable"); - FontProgram source = findEmbeddedFont( - Files.readAllBytes(Path.of(PdfFontEmbedderTest.class - .getResource("/pdfs/general/basic-text.pdf").toURI())), "ArialMT"); - assertNotNull(source, "fixture must contain ArialMT"); - byte[] input = identityCidPdf(source.baseName(), null); - byte[] output = PdfFontEmbedder.embed(input, Map.of(source.baseName(), source.data())); - try (PDDocument pdf = Loader.loadPDF(output)) { - COSDictionary font = identityCidFont(pdf); - COSBase toUnicode = font.getDictionaryObject(COSName.TO_UNICODE); - assertTrue(toUnicode instanceof COSStream, - "explicit replacement of a CID font without /ToUnicode must generate one"); - String cmap; - try (var in = ((COSStream) toUnicode).createInputStream()) { - cmap = new String(in.readAllBytes(), StandardCharsets.ISO_8859_1); - } - assertTrue(cmap.contains("<0024> <0041>") || cmap.contains("<0024><0041>"), - "glyph 36 is A in ArialMT: " + cmap); - } - } - - @Test - void legacyReplaceStreamLeavesProgramlessFontsAlone() throws Exception { - assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable"); - FontProgram source = findEmbeddedFont( - Files.readAllBytes(Path.of(PdfFontEmbedderTest.class - .getResource("/pdfs/general/basic-text.pdf").toURI())), "ArialMT"); - assertNotNull(source, "fixture must contain ArialMT"); - byte[] program = source.data().clone(); - program[program.length - 1] ^= 0x55; - byte[] input = sharedBaseFontPdf(source.baseName(), source.data()); - byte[] output = QpdfLib.replaceFontStream(input, source.baseName(), program); - try (PDDocument pdf = Loader.loadPDF(output)) { - PDResources resources = pdf.getPage(0).getResources(); - var withStream = resources.getFont(COSName.getPDFName("F1")).getFontDescriptor(); - var withoutStream = resources.getFont(COSName.getPDFName("F2")).getFontDescriptor(); - assertNotNull(withStream); - assertNotNull(withoutStream); - assertTrue(withStream.getFontFile2() != null, "the existing stream must be replaced"); - assertArrayEquals(program, withStream.getFontFile2().createInputStream().readAllBytes()); - assertTrue(withoutStream.getFontFile2() == null - && withoutStream.getFontFile3() == null - && withoutStream.getFontFile() == null, - "the compatibility export must not embed a font that had no program"); - } - } - private static byte[] identityCidPdf(String baseFont, String toUnicodeEntries, int... widthCids) throws Exception { try (PDDocument pdf = new PDDocument()) { diff --git a/native/bridge/src/jpdfium_qpdf.cpp b/native/bridge/src/jpdfium_qpdf.cpp index 9e087ad22..778963c66 100644 --- a/native/bridge/src/jpdfium_qpdf.cpp +++ b/native/bridge/src/jpdfium_qpdf.cpp @@ -1780,12 +1780,18 @@ FT_UInt glyphForName(FT_Face face, std::string const& name) { if (!u && name.size() >= 5 && name[0] == 'u') { bool hex = name.size() <= 7; for (size_t i = 1; hex && i < name.size(); ++i) hex = std::isxdigit(uint8_t(name[i])); - if (hex) u = uint32_t(std::strtoul(name.c_str() + 1, nullptr, 16)); + if (hex) { + uint32_t value = uint32_t(std::strtoul(name.c_str() + 1, nullptr, 16)); + if (value <= 0x10FFFF) u = value; + } } if (!u && name.size() == 7 && name.starts_with("uni")) { bool hex = true; - for (size_t i = 3; i < 7; ++i) hex = std::isxdigit(uint8_t(name[i])); - if (hex) u = uint32_t(std::strtoul(name.c_str() + 3, nullptr, 16)); + for (size_t i = 3; hex && i < 7; ++i) hex = std::isxdigit(uint8_t(name[i])); + if (hex) { + uint32_t value = uint32_t(std::strtoul(name.c_str() + 3, nullptr, 16)); + if (value <= 0x10FFFF) u = value; + } } if (!u) { auto const* first = std::begin(kAdobeGlyphNames); @@ -1924,6 +1930,22 @@ void setToUnicode(QPDF& qpdf, QPDFObjectHandle font, std::map> 16, low = value & 0xFFFF; + if (high < 0xD800 || high > 0xDBFF || low < 0xDC00 || low > 0xDFFF) return 0; + return 0x10000 + ((high - 0xD800) << 10) + (low - 0xDC00); + } +}; + +// CIDs are two-byte codes for the Identity encodings this rewrite supports, so a +// ToUnicode mapping outside that range cannot describe the document and is rejected +// instead of being allowed to size the generated CIDToGIDMap. +constexpr uint32_t kMaxCid = 0xFFFF; + std::map parseToUnicode(QPDFObjectHandle const& font) { std::map map; auto stream = font.getKey("/ToUnicode"); @@ -1932,68 +1954,110 @@ std::map parseToUnicode(QPDFObjectHandle const& font) { if (!data || !data->getSize()) return map; std::string text(reinterpret_cast(data->getBuffer()), data->getSize()); - auto hexAt = [&](size_t& pos, uint32_t& value) { + // PDF hex strings may interleave whitespace and pad an odd final digit with + // zero. The byte count decides whether the value can name a CID or a scalar. + auto hexAt = [&](size_t& pos, uint32_t& value, size_t& bytes) { if (pos >= text.size() || text[pos] != '<') return false; ++pos; - size_t start = pos; - while (pos < text.size() && text[pos] != '>') ++pos; - if (pos >= text.size() || pos == start || pos - start > 8) return false; - value = uint32_t(std::strtoul(text.substr(start, pos - start).c_str(), nullptr, 16)); + std::string digits; + while (pos < text.size() && text[pos] != '>') { + unsigned char c = uint8_t(text[pos++]); + if (std::isspace(c)) continue; + if (!std::isxdigit(c) || digits.size() >= 8) return false; + digits += char(c); + } + if (pos >= text.size() || digits.empty()) return false; ++pos; + if (digits.size() % 2) digits += '0'; + value = uint32_t(std::strtoul(digits.c_str(), nullptr, 16)); + bytes = digits.size() / 2; return true; }; + auto validDestination = [](uint32_t value, size_t bytes) { + if (bytes <= 2) return true; + // Four bytes is a surrogate pair only when it decodes to a valid scalar. + return bytes == 4 && UnicodeScalar::decode(value) != 0; + }; auto skipSpace = [&](size_t& pos) { while (pos < text.size() && std::isspace(uint8_t(text[pos]))) ++pos; }; - auto find = [&](std::string const& token, size_t from) { return text.find(token, from); }; - size_t pos = find("beginbfchar", 0); - if (pos != std::string::npos) { - pos += std::strlen("beginbfchar"); - size_t end = find("endbfchar", pos); + auto parseChars = [&](size_t pos, size_t end) { while (pos < end) { skipSpace(pos); + if (pos >= end) break; uint32_t code = 0; - if (!hexAt(pos, code)) { - ++pos; - continue; - } + size_t codeBytes = 0; + if (!hexAt(pos, code, codeBytes) || codeBytes > 2 || code > kMaxCid) return false; skipSpace(pos); uint32_t unicode = 0; - if (hexAt(pos, unicode)) map[code] = unicode; + size_t destinationBytes = 0; + if (!hexAt(pos, unicode, destinationBytes) || + !validDestination(unicode, destinationBytes)) + return false; + map[code] = unicode; } - } - pos = find("beginbfrange", 0); - if (pos != std::string::npos) { - pos += std::strlen("beginbfrange"); - size_t end = find("endbfrange", pos); + return true; + }; + auto parseRanges = [&](size_t pos, size_t end) { while (pos < end) { skipSpace(pos); + if (pos >= end) break; uint32_t low = 0; - if (!hexAt(pos, low)) { - ++pos; - continue; - } + size_t lowBytes = 0; + if (!hexAt(pos, low, lowBytes) || lowBytes > 2) return false; skipSpace(pos); uint32_t high = 0; - if (!hexAt(pos, high) || high < low) continue; + size_t highBytes = 0; + if (!hexAt(pos, high, highBytes) || highBytes > 2 || high < low || high > kMaxCid) + return false; skipSpace(pos); if (pos < end && text[pos] == '[') { ++pos; - for (uint32_t code = low; code <= high; ++code) { + uint64_t expected = uint64_t(high) - low + 1; + for (uint64_t i = 0; i < expected; ++i) { skipSpace(pos); uint32_t unicode = 0; - if (!hexAt(pos, unicode)) break; - map[code] = unicode; + size_t destinationBytes = 0; + if (!hexAt(pos, unicode, destinationBytes) || + !validDestination(unicode, destinationBytes)) + return false; + map[low + uint32_t(i)] = unicode; } - while (pos < end && text[pos] != ']') ++pos; - if (pos < end) ++pos; + skipSpace(pos); + if (pos >= end || text[pos] != ']') return false; + ++pos; } else { uint32_t unicode = 0; - if (!hexAt(pos, unicode)) continue; - for (uint32_t code = low; code <= high; ++code) map[code] = unicode + code - low; + size_t destinationBytes = 0; + if (!hexAt(pos, unicode, destinationBytes)) return false; + // Incrementing a packed surrogate pair has no defined meaning. + if (!validDestination(unicode, destinationBytes) || destinationBytes > 2) + return false; + uint64_t base = unicode; + for (uint32_t code = low; code <= high; ++code) { + uint64_t value = base + (code - low); + if (value > 0x10FFFF) return false; + map[code] = uint32_t(value); + } } } + return true; + }; + + size_t pos = 0; + while ((pos = text.find("beginbfchar", pos)) != std::string::npos) { + pos += std::strlen("beginbfchar"); + size_t end = text.find("endbfchar", pos); + if (end == std::string::npos || !parseChars(pos, end)) return {}; + pos = end + std::strlen("endbfchar"); + } + pos = 0; + while ((pos = text.find("beginbfrange", pos)) != std::string::npos) { + pos += std::strlen("beginbfrange"); + size_t end = text.find("endbfrange", pos); + if (end == std::string::npos || !parseRanges(pos, end)) return {}; + pos = end + std::strlen("endbfrange"); } return map; }