// jpdfium_advanced.cpp - Pattern matching, font analysis, metadata and text utilities. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "jpdfium.h" #include "jpdfium_font_encoding.h" #include "jpdfium_internal.h" // PCRE2 JIT Pattern Engine #ifdef JPDFIUM_HAS_PCRE2 #define PCRE2_CODE_UNIT_WIDTH 8 #include struct Pcre2Pattern8 { pcre2_code* code; pcre2_match_data* match_data; }; int32_t jpdfium_pcre2_compile(const char* pattern, uint32_t flags, int64_t* handle) { try { if (!pattern || !handle) return JPDFIUM_ERR_INVALID; uint32_t pcre2_opts = 0; if (flags & JPDFIUM_PCRE2_CASELESS) pcre2_opts |= PCRE2_CASELESS; if (flags & JPDFIUM_PCRE2_MULTILINE) pcre2_opts |= PCRE2_MULTILINE; if (flags & JPDFIUM_PCRE2_DOTALL) pcre2_opts |= PCRE2_DOTALL; if (flags & JPDFIUM_PCRE2_UTF) pcre2_opts |= PCRE2_UTF; if (flags & JPDFIUM_PCRE2_UCP) pcre2_opts |= PCRE2_UCP; int errcode; PCRE2_SIZE erroffset; pcre2_code* code = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, pcre2_opts, &errcode, &erroffset, nullptr); if (!code) return JPDFIUM_ERR_INVALID; // JIT compile for speed - failure is non-fatal (falls back to interpreter) pcre2_jit_compile(code, PCRE2_JIT_COMPLETE); auto* pw = new Pcre2Pattern8(); pw->code = code; pw->match_data = pcre2_match_data_create_from_pattern(code, nullptr); Pcre2HandleRegistry::instance().add(pw); *handle = reinterpret_cast(pw); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_pcre2_match_all(int64_t pattern_handle, const char* text, char** json_result) { try { if (!text || !json_result) return JPDFIUM_ERR_INVALID; auto* pw = reinterpret_cast(pattern_handle); if (!pw || !Pcre2HandleRegistry::instance().contains(pw)) return JPDFIUM_ERR_INVALID; if (!pw->code) return JPDFIUM_ERR_INVALID; PCRE2_SIZE subject_len = strlen(text); std::string json = "["; bool first = true; PCRE2_SIZE offset = 0; while (offset < subject_len) { int rc = pcre2_match(pw->code, (PCRE2_SPTR)text, subject_len, offset, 0, pw->match_data, nullptr); if (rc < 0) break; PCRE2_SIZE* ovector = pcre2_get_ovector_pointer(pw->match_data); PCRE2_SIZE start = ovector[0]; PCRE2_SIZE end = ovector[1]; if (!first) json += ","; first = false; std::string match_text(text + start, end - start); // Escape JSON special characters in the match text std::string escaped; for (char c : match_text) { switch (c) { case '"': escaped += "\\\""; break; case '\\': escaped += "\\\\"; break; case '\n': escaped += "\\n"; break; case '\r': escaped += "\\r"; break; case '\t': escaped += "\\t"; break; default: escaped += c; } } char buf[256]; snprintf(buf, sizeof(buf), "{\"start\":%zu,\"end\":%zu,\"match\":\"%s\"}", (size_t)start, (size_t)end, escaped.c_str()); json += buf; offset = (end > start) ? end : end + 1; // advance past zero-length matches } json += "]"; *json_result = strdup(json.c_str()); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } void jpdfium_pcre2_free(int64_t pattern_handle) noexcept { // Membership is checked before any dereference, so a fabricated handle is // rejected rather than read. See HandleRegistry for the lifetime contract. auto* pw = reinterpret_cast(pattern_handle); if (!pw || !Pcre2HandleRegistry::instance().contains(pw)) return; Pcre2HandleRegistry::instance().remove(pw); if (pw->match_data) pcre2_match_data_free(pw->match_data); if (pw->code) pcre2_code_free(pw->code); delete pw; } #else // !JPDFIUM_HAS_PCRE2 int32_t jpdfium_pcre2_compile(const char*, uint32_t, int64_t* handle) { if (handle) *handle = 0; return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_pcre2_match_all(int64_t, const char*, char** json_result) { if (json_result) *json_result = strdup("[]"); return JPDFIUM_ERR_NOT_FOUND; } void jpdfium_pcre2_free(int64_t) noexcept {} #endif // JPDFIUM_HAS_PCRE2 // Luhn Algorithm - pure C, no dependencies int32_t jpdfium_luhn_validate(const char* number) { try { if (!number) return 0; int digits[32]; int n = 0; for (const char* p = number; *p && n < 32; ++p) { if (*p >= '0' && *p <= '9') digits[n++] = *p - '0'; else if (*p != ' ' && *p != '-') return 0; // invalid character } // Credit cards: 13-19 digits (Visa 13/16/19, MC/Amex/Discover 15-16) if (n < 13 || n > 19) return 0; int sum = 0; for (int i = n - 1, alt = 0; i >= 0; --i, alt ^= 1) { int d = digits[i]; if (alt) { d *= 2; if (d > 9) d -= 9; } sum += d; } return (sum % 10 == 0) ? 1 : 0; } catch (...) { return JPDFIUM_ERR_NATIVE; } } // FlashText Trie-based Keyword NER - O(n) matching namespace { struct TrieNode { std::unordered_map> children; std::string keyword; // non-empty at terminal nodes std::string label; // entity label at terminal nodes }; struct FlashTextProcessor { std::unique_ptr root; bool case_sensitive; FlashTextProcessor() : root(std::make_unique()), case_sensitive(false) {} void addKeyword(const char* keyword, const char* label) { if (!keyword) return; TrieNode* node = root.get(); for (const char* p = keyword; *p; ++p) { char c = case_sensitive ? *p : static_cast(std::tolower(static_cast(*p))); auto it = node->children.find(c); if (it == node->children.end()) { node->children[c] = std::make_unique(); } node = node->children[c].get(); } node->keyword = keyword; node->label = label ? label : ""; } std::string findAll(const char* text) { if (!text) return "[]"; std::string result = "["; bool first = true; size_t textLen = strlen(text); for (size_t i = 0; i < textLen;) { TrieNode* node = root.get(); size_t matchEnd = 0; std::string matchKeyword, matchLabel; bool found = false; for (size_t j = i; j < textLen; ++j) { char c = case_sensitive ? text[j] : static_cast(std::tolower(static_cast(text[j]))); auto it = node->children.find(c); if (it == node->children.end()) break; node = it->second.get(); if (!node->keyword.empty()) { // Check word boundary: must be at start/end or adjacent to non-alphanumeric bool leftBound = (j - (j - i) == 0) || !isalnum(text[j - (j - i) - 1]); bool rightBound = (j + 1 >= textLen) || !isalnum(text[j + 1]); if (leftBound && rightBound) { matchEnd = j + 1; matchKeyword = node->keyword; matchLabel = node->label; found = true; } } } if (found) { if (!first) result += ","; first = false; char buf[512]; snprintf(buf, sizeof(buf), "{\"start\":%zu,\"end\":%zu,\"keyword\":\"%s\",\"label\":\"%s\"}", i, matchEnd, matchKeyword.c_str(), matchLabel.c_str()); result += buf; i = matchEnd; } else { ++i; } } result += "]"; return result; } }; } // anonymous namespace int32_t jpdfium_flashtext_create(int64_t* handle) { try { if (!handle) return JPDFIUM_ERR_INVALID; auto* ft = new FlashTextProcessor(); FlashTextHandleRegistry::instance().add(ft); *handle = reinterpret_cast(ft); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_flashtext_add_keyword(int64_t handle, const char* keyword, const char* label) { try { auto* ft = reinterpret_cast(handle); if (!ft || !FlashTextHandleRegistry::instance().contains(ft)) return JPDFIUM_ERR_INVALID; if (!keyword) return JPDFIUM_ERR_INVALID; ft->addKeyword(keyword, label); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_flashtext_add_keywords_json(int64_t handle, const char* json) { try { auto* ft = reinterpret_cast(handle); if (!ft || !FlashTextHandleRegistry::instance().contains(ft)) return JPDFIUM_ERR_INVALID; if (!json) return JPDFIUM_ERR_INVALID; // Simple JSON parser for: [{"keyword":"...","label":"..."}, ...] const char* p = json; while (*p) { const char* kw_start = strstr(p, "\"keyword\":\""); if (!kw_start) break; kw_start += 11; // skip "keyword":" const char* kw_end = strchr(kw_start, '"'); if (!kw_end) break; std::string keyword(kw_start, kw_end - kw_start); std::string label; const char* lb_start = strstr(kw_end, "\"label\":\""); if (lb_start) { lb_start += 9; const char* lb_end = strchr(lb_start, '"'); if (lb_end) label.assign(lb_start, lb_end - lb_start); } ft->addKeyword(keyword.c_str(), label.c_str()); p = kw_end + 1; } return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_flashtext_find(int64_t handle, const char* text, char** json_result) { try { auto* ft = reinterpret_cast(handle); if (!ft || !FlashTextHandleRegistry::instance().contains(ft)) return JPDFIUM_ERR_INVALID; if (!json_result) return JPDFIUM_ERR_INVALID; std::string result = ft->findAll(text); *json_result = strdup(result.c_str()); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } void jpdfium_flashtext_free(int64_t handle) noexcept { // Identity checked before the delete; a fabricated handle is a no-op. auto* ft = reinterpret_cast(handle); if (!ft || !FlashTextHandleRegistry::instance().contains(ft)) return; FlashTextHandleRegistry::instance().remove(ft); delete ft; } // Font Normalization Pipeline - FreeType + HarfBuzz hb-subset #if defined(JPDFIUM_HAS_FREETYPE) && defined(JPDFIUM_HAS_HARFBUZZ) #include #include FT_ADVANCES_H #include FT_CID_H #include FT_FONT_FORMATS_H #include FT_FREETYPE_H #include FT_SFNT_NAMES_H #include FT_TRUETYPE_IDS_H #include FT_TRUETYPE_TABLES_H #include #include #include #include #include #include "jpdfium_internal.h" #ifdef JPDFIUM_HAS_QPDF #include #include #include #include #include // Defined further down in this translation unit's qpdf section. Declared here // so the font pipeline below can reuse the same save/reload machinery. static bool qpdf_save_fpdf(FPDF_DOCUMENT doc, std::vector& out); static std::vector qpdf_write(QPDF& pdf); static int32_t docwrapper_reload(DocWrapper* w, const std::vector& newBytes); static int32_t font_normalize_qpdf(DocWrapper* w, int32_t page_index, bool do_fix_tounicode, bool do_repair_widths, int32_t* fonts_processed, int32_t* tuc_fixed, int32_t* w_fixed, int32_t* type1_converted, int32_t* resubset); #endif static FT_Library ft_lib = nullptr; static void ensure_freetype_init() { if (!ft_lib) FT_Init_FreeType(&ft_lib); } static std::string json_escape_fonts(std::string const& value) { std::string out; out.reserve(value.size()); for (unsigned char c : value) { switch (c) { case '"': out += "\\\""; break; case '\\': out += "\\\\"; break; case '\b': out += "\\b"; break; case '\f': out += "\\f"; break; case '\n': out += "\\n"; break; case '\r': out += "\\r"; break; case '\t': out += "\\t"; break; default: if (c < 0x20) { char escaped[7]; std::snprintf(escaped, sizeof escaped, "\\u%04x", c); out += escaped; } else { out += static_cast(c); } } } return out; } int32_t jpdfium_font_get_data(int64_t page, int32_t font_index, uint8_t** data, int64_t* len) { try { PageWrapper* pw = decodePage(page); if (!pw || !pw->page || !data || !len) return JPDFIUM_ERR_INVALID; *data = nullptr; *len = 0; // Enumerate unique fonts on the page by walking text objects. int objCount = FPDFPage_CountObjects(pw->page); std::vector fonts; // ordered list of unique fonts std::set seen; for (int i = 0; i < objCount; i++) { FPDF_PAGEOBJECT obj = FPDFPage_GetObject(pw->page, i); if (FPDFPageObj_GetType(obj) != FPDF_PAGEOBJ_TEXT) continue; FPDF_FONT font = FPDFTextObj_GetFont(obj); if (font && seen.insert(font).second) { fonts.push_back(font); } } if (font_index < 0 || font_index >= static_cast(fonts.size())) return JPDFIUM_ERR_NOT_FOUND; FPDF_FONT font = fonts[font_index]; // Query required buffer size size_t buflen = 0; if (!FPDFFont_GetFontData(font, nullptr, 0, &buflen) || buflen == 0) return JPDFIUM_ERR_NOT_FOUND; // Allocate and fill uint8_t* buf = static_cast(malloc(buflen)); if (!buf) return JPDFIUM_ERR_NATIVE; size_t actual = 0; if (!FPDFFont_GetFontData(font, buf, buflen, &actual)) { free(buf); return JPDFIUM_ERR_NATIVE; } *data = buf; *len = static_cast(actual); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_font_classify(const uint8_t* data, int64_t len, char** json) { try { if (!data || len <= 0 || !json) return JPDFIUM_ERR_INVALID; ensure_freetype_init(); FT_Face face; FT_Error err = FT_New_Memory_Face(ft_lib, data, (FT_Long)len, 0, &face); if (err) return JPDFIUM_ERR_INVALID; if (face->num_glyphs <= 1) { FT_Done_Face(face); return JPDFIUM_ERR_INVALID; } const char* type_str; bool sfnt = FT_IS_SFNT(face); bool has_cmap = (face->num_charmaps > 0); bool has_kerning = FT_HAS_KERNING(face); // Detect font type if (face->face_flags & FT_FACE_FLAG_SFNT) { // Check for CFF: presence of CFF table in sfnt container FT_ULong cff_len = 0; if (FT_Load_Sfnt_Table(face, FT_MAKE_TAG('C', 'F', 'F', ' '), 0, nullptr, &cff_len) == 0) type_str = "CFF"; else if (FT_Load_Sfnt_Table(face, FT_MAKE_TAG('C', 'F', 'F', '2'), 0, nullptr, &cff_len) == 0) type_str = "CFF2"; else type_str = "TrueType"; } else { type_str = "Type1"; } // Detect subset prefix (e.g. "ABCDEF+Arial") bool is_subset = false; if (face->family_name) { const char* plus = strchr(face->family_name, '+'); if (plus && (plus - face->family_name) == 6) { is_subset = true; for (const char* c = face->family_name; c < plus; ++c) { if (*c < 'A' || *c > 'Z') { is_subset = false; break; } } } } std::string family = json_escape_fonts(face->family_name ? face->family_name : ""); char head[384]; snprintf(head, sizeof(head), "{\"type\":\"%s\",\"sfnt\":%s,\"has_cmap\":%s," "\"num_glyphs\":%ld,\"units_per_em\":%d,\"has_kerning\":%s," "\"is_subset\":%s,\"family\":\"", type_str, sfnt ? "true" : "false", has_cmap ? "true" : "false", face->num_glyphs, (int)face->units_per_EM, has_kerning ? "true" : "false", is_subset ? "true" : "false"); std::string jsonText = head; jsonText += family; jsonText += "\"}"; FT_Done_Face(face); char* dup = strdup(jsonText.c_str()); if (!dup) return JPDFIUM_ERR_NATIVE; *json = dup; return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_font_covers_text(const uint8_t* data, int64_t len, const int32_t* codepoints, int32_t count, uint8_t* out_covered) { try { if (!data || len <= 0 || !codepoints || count <= 0 || !out_covered) return JPDFIUM_ERR_INVALID; ensure_freetype_init(); FT_Face face; if (FT_New_Memory_Face(ft_lib, data, (FT_Long)len, 0, &face)) return JPDFIUM_ERR_INVALID; // Map codepoints, not glyph ids: without this a symbol charmap misreports. if (face->num_charmaps > 0) FT_Select_Charmap(face, FT_ENCODING_UNICODE); for (int32_t i = 0; i < count; ++i) { uint32_t cp = (uint32_t)codepoints[i]; // Index 0 is the .notdef placeholder, never a real glyph. out_covered[i] = (cp <= 0x10FFFF && FT_Get_Char_Index(face, cp) != 0) ? 1 : 0; } FT_Done_Face(face); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_text_shape(const uint8_t* font_data, int64_t font_len, const char* utf8_text, float font_size, char** json) { try { // 26.6 scale: reject sizes whose scaled value overflows the int cast. if (!font_data || font_len <= 0 || !utf8_text || !json || !std::isfinite(font_size) || font_size <= 0 || font_size * 64.0 > (double)INT32_MAX) return JPDFIUM_ERR_INVALID; hb_blob_t* blob = hb_blob_create((const char*)font_data, (unsigned int)font_len, HB_MEMORY_MODE_READONLY, nullptr, nullptr); hb_face_t* face = hb_face_create(blob, 0); hb_blob_destroy(blob); // hb_face_create never fails: corrupt bytes yield an empty face whose // every codepoint maps to glyph 0. Reject those, as font_covers_text does. if (hb_face_get_glyph_count(face) <= 1) { hb_face_destroy(face); return JPDFIUM_ERR_INVALID; } hb_font_t* font = hb_font_create(face); hb_face_destroy(face); // Advances come out in 26.6 fractional points at this scale. hb_font_set_scale(font, (int)(font_size * 64), (int)(font_size * 64)); hb_buffer_t* buf = hb_buffer_create(); hb_buffer_add_utf8(buf, utf8_text, -1, 0, -1); // Script/direction/language default from content; same as plain hb-shape. // One buffer is one run: mixed direction/script must be split by the caller. hb_buffer_guess_segment_properties(buf); hb_shape(font, buf, nullptr, 0); unsigned int count = 0; hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buf, &count); hb_glyph_position_t* pos = hb_buffer_get_glyph_positions(buf, &count); std::string out = "["; char entry[128]; for (unsigned int i = 0; i < count; ++i) { if (i) out += ","; snprintf(entry, sizeof(entry), "{\"g\":%u,\"ax\":%d,\"ay\":%d,\"dx\":%d,\"dy\":%d,\"cluster\":%u}", infos[i].codepoint, pos[i].x_advance, pos[i].y_advance, pos[i].x_offset, pos[i].y_offset, infos[i].cluster); out += entry; } out += "]"; hb_buffer_destroy(buf); hb_font_destroy(font); *json = strdup(out.c_str()); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } #ifdef JPDFIUM_HAS_QPDF // UTF-16BE hex for a PDF /ToUnicode destination. Non-BMP codepoints become a // surrogate pair, which is what a UCS-2 CMap consumer expects. static std::string pdf_utf16be_hex(uint32_t codepoint) { char buf[16]; if (codepoint <= 0xFFFF) { std::snprintf(buf, sizeof(buf), "%04X", codepoint); return buf; } uint32_t v = codepoint - 0x10000; std::snprintf(buf, sizeof(buf), "%04X%04X", 0xD800 + (v >> 10), 0xDC00 + (v & 0x3FF)); return buf; } // A /ToUnicode destination can be a sequence of scalars (ligatures). static std::string pdf_utf16be_hex(std::vector const& codepoints) { std::string out; for (uint32_t codepoint : codepoints) out += pdf_utf16be_hex(codepoint); return out; } namespace { using namespace jpdfium_font_encoding; FT_UInt glyphForName(FT_Face face, std::string const& name) { if (!face) return 0; if (FT_HAS_GLYPH_NAMES(face)) { if (FT_UInt gid = FT_Get_Name_Index(face, name.c_str())) return gid; } uint32_t u = unicodeForGlyphName(name); if (u && !FT_Select_Charmap(face, FT_ENCODING_UNICODE)) return FT_Get_Char_Index(face, u); return 0; } FT_UInt gidForUnicode(FT_Face face, uint32_t unicode, bool has_unicode_charmap) { if (!face || !has_unicode_charmap || unicode == 0) return 0; if (FT_Select_Charmap(face, FT_ENCODING_UNICODE) != 0) return 0; return FT_Get_Char_Index(face, unicode); } // TrueType symbolic fonts are normally mapped through cmap (3,0), which // expects byte codes in the 0xF000 private-use range; Apple Roman is the other // common fallback for simple fonts without a usable Unicode cmap. FT_UInt gidFromSymbolicCharmaps(FT_Face face, uint32_t code) { if (!face) return 0; auto tryCharmap = [&](FT_Encoding encoding) -> FT_UInt { if (FT_Select_Charmap(face, encoding) != 0) return 0; if (FT_UInt gid = FT_Get_Char_Index(face, code)) return gid; if (FT_UInt gid = FT_Get_Char_Index(face, 0xF000 | code)) return gid; return 0; }; if (FT_UInt gid = tryCharmap(FT_ENCODING_MS_SYMBOL)) return gid; if (FT_UInt gid = tryCharmap(FT_ENCODING_APPLE_ROMAN)) return gid; for (int i = 0; i < face->num_charmaps; ++i) { FT_CharMap cmap = face->charmaps[i]; if (!cmap || cmap->encoding == FT_ENCODING_UNICODE) continue; if (FT_Set_Charmap(face, cmap) != 0) continue; if (FT_UInt gid = FT_Get_Char_Index(face, code)) return gid; if (FT_UInt gid = FT_Get_Char_Index(face, 0xF000 | code)) return gid; } return 0; } struct SimpleEncoding { BaseEncoding base = BaseEncoding::Standard; bool hasExplicitEncoding = false; bool hasBaseEncoding = false; bool unsupportedEncoding = false; bool symbolic = false; std::map differences; }; SimpleEncoding parseSimpleEncoding(QPDFObjectHandle const& font, QPDFObjectHandle const& descriptor) { SimpleEncoding enc; QPDFObjectHandle encoding = font.getKey("/Encoding"); QPDFObjectHandle base; if (encoding.isName()) { enc.hasExplicitEncoding = true; base = encoding; } else if (encoding.isDictionary()) { enc.hasExplicitEncoding = true; base = encoding.getKey("/BaseEncoding"); QPDFObjectHandle differences = encoding.getKey("/Differences"); if (differences.isArray()) { int code = 0; for (auto& item : differences.getArrayAsVector()) { if (item.isInteger()) { code = item.getIntValueAsInt(); } else if (item.isName()) { if (code >= 0 && code <= 255) enc.differences[code] = item.getName().substr(1); ++code; } } } } if (base.isName()) { if (base.isNameAndEquals("/WinAnsiEncoding")) { enc.base = BaseEncoding::WinAnsi; enc.hasBaseEncoding = true; } else if (base.isNameAndEquals("/MacRomanEncoding")) { enc.base = BaseEncoding::MacRoman; enc.hasBaseEncoding = true; } else if (base.isNameAndEquals("/StandardEncoding")) { enc.base = BaseEncoding::Standard; enc.hasBaseEncoding = true; } else { // MacExpertEncoding and other vendor encodings need a name table we // do not have; never guess glyph mappings for those fonts. enc.unsupportedEncoding = true; } } QPDFObjectHandle flags = descriptor.getKey("/Flags"); enc.symbolic = flags.isInteger() && (flags.getIntValue() & 4) != 0; return enc; } struct SimpleGlyph { FT_UInt gid = 0; uint32_t unicode = 0; }; // Resolve a byte code through the font program's built-in text encoding. // Type 1 fonts without a PDF /BaseEncoding keep their glyph order in the // Adobe custom/standard charmap, or expose it only through glyph names. FT_UInt gidFromBuiltInEncoding(FT_Face face, uint32_t code) { if (!face) return 0; auto tryEncoding = [&](FT_Encoding encoding) -> FT_UInt { if (FT_Select_Charmap(face, encoding) != 0) return 0; return FT_Get_Char_Index(face, code); }; if (FT_UInt gid = tryEncoding(FT_ENCODING_ADOBE_CUSTOM)) return gid; if (FT_UInt gid = tryEncoding(FT_ENCODING_ADOBE_STANDARD)) return gid; return gidFromSymbolicCharmaps(face, code); } uint32_t unicodeFromGlyphNameOnly(FT_Face face, FT_UInt gid) { if (!face || !gid || !FT_HAS_GLYPH_NAMES(face)) return 0; char name[256]; if (FT_Get_Glyph_Name(face, gid, name, sizeof(name)) != 0 || !name[0]) return 0; return unicodeForGlyphName(name); } uint32_t unicodeFromGid(FT_Face face, FT_UInt gid, std::map const& gid_to_unicode) { if (!gid) return 0; auto found = gid_to_unicode.find(gid); if (found != gid_to_unicode.end()) return found->second; return unicodeFromGlyphNameOnly(face, gid); } std::vector resolveSimpleChars(FT_Face face, bool has_unicode_charmap, std::map const& gid_to_unicode, SimpleEncoding const& enc, bool is_type1_family) { std::vector resolved(256); // A Type 1 font with no /BaseEncoding must be mapped through its built-in // encoding: StandardEncoding is only a PDF fallback, not the program order. const bool builtin_encoding = is_type1_family && !enc.hasBaseEncoding; const bool non_symbolic_with_base = !enc.symbolic && (enc.hasBaseEncoding || !is_type1_family); for (uint32_t code = 0; code <= 0xFF; ++code) { SimpleGlyph entry; auto diff = enc.differences.find(static_cast(code)); if (diff != enc.differences.end()) { if (diff->second != ".notdef") { entry.unicode = unicodeForGlyphName(diff->second); entry.gid = glyphForName(face, diff->second); if (entry.gid != 0 && entry.unicode == 0) entry.unicode = unicodeFromGid(face, entry.gid, gid_to_unicode); } } else if (builtin_encoding) { entry.gid = gidFromBuiltInEncoding(face, code); if (entry.gid != 0) entry.unicode = unicodeFromGid(face, entry.gid, gid_to_unicode); } else if (!enc.symbolic || enc.hasExplicitEncoding) { entry.unicode = baseEncodingUnicode(enc.base, static_cast(code)); entry.gid = gidForUnicode(face, entry.unicode, has_unicode_charmap); if (entry.gid == 0 && non_symbolic_with_base) { // The font may lack the non-breaking space/hyphen scalars but // still carry the ASCII glyph. Keep the Unicode value intact. uint32_t fallback = entry.unicode == 0xA0 ? 0x20 : entry.unicode == 0xAD ? 0x2D : 0; if (fallback) entry.gid = gidForUnicode(face, fallback, has_unicode_charmap); } else if (entry.gid == 0) { // Symbolic (or Type 1 without a base encoding) fonts commonly // expose byte codes through a non-Unicode cmap. entry.gid = gidFromSymbolicCharmaps(face, code); } } else { entry.gid = gidFromSymbolicCharmaps(face, code); if (entry.gid != 0) entry.unicode = unicodeFromGid(face, entry.gid, gid_to_unicode); } resolved[code] = entry; } return resolved; } } // namespace static bool visit_stream(std::map& seen_streams, QPDFObjectHandle const& stream, int depth) { if (!stream.isIndirect()) return true; auto [it, inserted] = seen_streams.emplace(stream.getObjGen(), depth); if (!inserted && it->second <= depth) return false; it->second = depth; return true; } static void collect_resource_fonts(QPDFObjectHandle resources, int depth, std::set& seen_resources, std::set& seen_fonts, std::map& seen_streams, std::vector& fonts) { if (!resources.isDictionary() || depth > 32) return; if (resources.isIndirect() && !seen_resources.insert(resources.getObjGen()).second) return; QPDFObjectHandle font_dict = resources.getKey("/Font"); if (font_dict.isDictionary()) { for (auto const& name : font_dict.getKeys()) { QPDFObjectHandle font = font_dict.getKey(name); if (!font.isDictionary()) continue; // Shared fonts are reached through several pages/resources; repair // each dictionary once. if (font.isIndirect() && !seen_fonts.insert(font.getObjGen()).second) continue; fonts.push_back(font); // Type 3 glyph procedures carry their own resources and can embed // further fonts (including Type 3 fonts). if (font.getKey("/Subtype").isNameAndEquals("/Type3")) { collect_resource_fonts(font.getKey("/Resources"), depth + 1, seen_resources, seen_fonts, seen_streams, fonts); QPDFObjectHandle char_procs = font.getKey("/CharProcs"); if (char_procs.isDictionary()) { for (auto const& glyph_name : char_procs.getKeys()) { QPDFObjectHandle glyph = char_procs.getKey(glyph_name); if (!glyph.isStream()) continue; if (!visit_stream(seen_streams, glyph, depth + 1)) continue; collect_resource_fonts(glyph.getDict().getKey("/Resources"), depth + 1, seen_resources, seen_fonts, seen_streams, fonts); } } } } } // /ExtGState can reference fonts directly (soft-mask groups and the // non-stroking transparency group's /Font array). QPDFObjectHandle ext_gstates = resources.getKey("/ExtGState"); if (ext_gstates.isDictionary()) { for (auto const& gs_name : ext_gstates.getKeys()) { QPDFObjectHandle gs = ext_gstates.getKey(gs_name); if (!gs.isDictionary()) continue; QPDFObjectHandle gs_fonts = gs.getKey("/Font"); if (gs_fonts.isArray()) { const int count = gs_fonts.getArrayNItems(); for (int i = 0; i < count; ++i) { QPDFObjectHandle font = gs_fonts.getArrayItem(i); if (!font.isDictionary()) continue; if (font.isIndirect() && !seen_fonts.insert(font.getObjGen()).second) continue; fonts.push_back(font); } } QPDFObjectHandle smask = gs.getKey("/SMask"); if (smask.isDictionary()) { QPDFObjectHandle group = smask.getKey("/G"); if (group.isStream() && visit_stream(seen_streams, group, depth + 1)) collect_resource_fonts(group.getDict().getKey("/Resources"), depth + 1, seen_resources, seen_fonts, seen_streams, fonts); } } } QPDFObjectHandle xobjects = resources.getKey("/XObject"); if (xobjects.isDictionary()) { for (auto const& name : xobjects.getKeys()) { QPDFObjectHandle xobject = xobjects.getKey(name); if (!xobject.isStream()) continue; QPDFObjectHandle subtype = xobject.getDict().getKey("/Subtype"); if (!subtype.isName() || subtype.getName() != "/Form") continue; if (!visit_stream(seen_streams, xobject, depth + 1)) continue; collect_resource_fonts(xobject.getDict().getKey("/Resources"), depth + 1, seen_resources, seen_fonts, seen_streams, fonts); } } QPDFObjectHandle patterns = resources.getKey("/Pattern"); if (patterns.isDictionary()) { for (auto const& name : patterns.getKeys()) { QPDFObjectHandle pattern = patterns.getKey(name); if (!pattern.isStream()) continue; if (!visit_stream(seen_streams, pattern, depth + 1)) continue; collect_resource_fonts(pattern.getDict().getKey("/Resources"), depth + 1, seen_resources, seen_fonts, seen_streams, fonts); } } } // Page /Resources inherit from the /Pages tree. Nested form XObjects, // annotation appearance streams, and patterns carry their own /Resources. static std::vector collect_page_fonts(QPDFObjectHandle page, std::set& seen_resources, std::set& seen_fonts) { std::vector fonts; std::map seen_streams; QPDFObjectHandle resources = page.getKey("/Resources"); if (!resources.isDictionary()) { QPDFObjectHandle parent = page.getKey("/Parent"); for (int depth = 0; depth < 32 && parent.isDictionary() && !resources.isDictionary(); ++depth) { resources = parent.getKey("/Resources"); parent = parent.getKey("/Parent"); } } collect_resource_fonts(resources, 0, seen_resources, seen_fonts, seen_streams, fonts); QPDFObjectHandle annots = page.getKey("/Annots"); if (annots.isArray()) { const int annot_count = annots.getArrayNItems(); for (int i = 0; i < annot_count; ++i) { QPDFObjectHandle annot = annots.getArrayItem(i); if (!annot.isDictionary()) continue; QPDFObjectHandle ap = annot.getKey("/AP"); if (!ap.isDictionary()) continue; for (auto const& key : ap.getKeys()) { QPDFObjectHandle appearance = ap.getKey(key); if (appearance.isStream()) { collect_resource_fonts(appearance.getDict().getKey("/Resources"), 0, seen_resources, seen_fonts, seen_streams, fonts); } else if (appearance.isDictionary()) { for (auto const& state_key : appearance.getKeys()) { QPDFObjectHandle state_stream = appearance.getKey(state_key); if (!state_stream.isStream()) continue; collect_resource_fonts(state_stream.getDict().getKey("/Resources"), 0, seen_resources, seen_fonts, seen_streams, fonts); } } } } } return fonts; } static std::string build_tounicode_cmap(std::map> const& mapping, bool cid) { std::string out; out += "/CIDInit /ProcSet findresource begin\n"; out += "12 dict begin\n"; out += "begincmap\n"; out += "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n"; out += "/CMapName /Adobe-Identity-UCS def\n"; out += "/CMapType 2 def\n"; out += "1 begincodespacerange\n"; out += cid ? "<0000> \n" : "<00> \n"; out += "endcodespacerange\n"; size_t remaining = mapping.size(); auto it = mapping.begin(); while (remaining > 0) { size_t chunk = remaining > 100 ? 100 : remaining; out += std::to_string(chunk) + " beginbfchar\n"; for (size_t i = 0; i < chunk; ++i, ++it) { char code[16]; if (cid) { std::snprintf(code, sizeof(code), "%04X", it->first); } else { std::snprintf(code, sizeof(code), "%02X", it->first); } out += "<"; out += code; out += "> <"; out += pdf_utf16be_hex(it->second); out += ">\n"; } out += "endbfchar\n"; remaining -= chunk; } out += "endcmap\n"; out += "CMapName currentdict /CMap defineresource pop\n"; out += "end\n"; out += "end\n"; return out; } static bool pdf_width_from_glyph(FT_Face face, FT_UInt gid, int& out_width) { if (!face || gid == 0 || face->units_per_EM == 0) return false; FT_Fixed advance = 0; if (FT_Get_Advance(face, gid, FT_LOAD_NO_SCALE, &advance) != 0) return false; const long long units = face->units_per_EM; out_width = static_cast((static_cast(advance) * 1000 + units / 2) / units); return true; } // A /ToUnicode CMap counts as present only when it actually maps a code. Some // producers emit the boilerplate prologue with no bfchar/bfrange entries. static bool cmapSectionHasMapping(std::string const& body) { int hexStrings = 0; size_t open = 0; while ((open = body.find('<', open)) != std::string::npos) { auto close = body.find('>', open + 1); if (close == std::string::npos) break; bool non_empty = false; for (size_t i = open + 1; i < close; ++i) if (!std::isspace(static_cast(body[i]))) { non_empty = true; break; } if (non_empty) ++hexStrings; open = close + 1; } return hexStrings >= 2; } static bool cmapHasMappings(std::string const& text) { for (auto const& section : {std::pair("beginbfchar", "endbfchar"), std::pair("beginbfrange", "endbfrange")}) { size_t begin = text.find(section.first); if (begin == std::string::npos) continue; size_t end = text.find(section.second, begin); if (end == std::string::npos) continue; if (cmapSectionHasMapping(text.substr(begin, end - begin))) return true; } return false; } struct FontNormalizeState { int32_t fonts_processed = 0; int32_t tuc_fixed = 0; int32_t w_fixed = 0; int32_t type1_converted = 0; int32_t resubset = 0; bool changed = false; }; // Synthesize a missing or boilerplate-only /ToUnicode CMap from the resolved // glyph-to-Unicode mapping when the mapping is unambiguous. static void repair_font_tounicode(QPDF& pdf, QPDFObjectHandle& font, FT_Face face, bool cid, QPDFObjectHandle& cid_to_gid, bool cid_identity, std::map const& gid_to_cid, std::map const& gid_to_unicode, std::set const& ambiguous_gids, bool simple_any_gid, std::vector const& simple_glyphs, FontNormalizeState& state) { bool has_tounicode = false; QPDFObjectHandle to_unicode = font.getKey("/ToUnicode"); if (to_unicode.isStream()) { try { std::shared_ptr existing = to_unicode.getStreamData(qpdf_dl_generalized); if (existing && existing->getSize() > 0) { std::string cmap(reinterpret_cast(existing->getBuffer()), existing->getSize()); has_tounicode = cmapHasMappings(cmap); } } catch (...) { has_tounicode = false; } } if (has_tounicode) return; auto reverse_cid_unicode = [&](uint32_t gid) -> uint32_t { if (ambiguous_gids.contains(gid)) return unicodeFromGlyphNameOnly(face, gid); auto found = gid_to_unicode.find(gid); return found != gid_to_unicode.end() ? found->second : 0; }; std::map> code_to_unicode; if (cid) { if (cid_to_gid.isStream()) { try { std::shared_ptr map = cid_to_gid.getStreamData(qpdf_dl_generalized); if (map) { const auto* bytes = static_cast(map->getBuffer()); const size_t entries = map->getSize() / 2; for (size_t c = 0; c < entries && c <= 0xFFFF; ++c) { const uint32_t g = (static_cast(bytes[2 * c]) << 8) | bytes[2 * c + 1]; uint32_t unicode = reverse_cid_unicode(g); if (unicode) code_to_unicode.emplace(static_cast(c), std::vector{unicode}); } } } catch (...) { code_to_unicode.clear(); } } else if (cid_identity) { if (FT_IS_CID_KEYED(face)) { for (auto const& entry : gid_to_cid) { uint32_t unicode = reverse_cid_unicode(entry.first); if (unicode) code_to_unicode.emplace(entry.second, std::vector{unicode}); } } else { for (auto const& entry : gid_to_unicode) { if (entry.first <= 0xFFFF && (!ambiguous_gids.contains(entry.first) || FT_HAS_GLYPH_NAMES(face))) code_to_unicode.emplace(entry.first, std::vector{entry.second}); } } } } else if (simple_any_gid) { // Only synthesize when the encoding actually resolves glyphs; // never fall back to raw code == glyph id for simple fonts. for (uint32_t code = 0; code <= 0xFF; ++code) { uint32_t gid = simple_glyphs[code].gid; std::vector sequence; if (gid != 0 && FT_HAS_GLYPH_NAMES(face)) { char glyph_name[256]; if (FT_Get_Glyph_Name(face, gid, glyph_name, sizeof(glyph_name)) == 0 && glyph_name[0]) sequence = unicodeSequenceForGlyphName(glyph_name); } if (sequence.empty() && simple_glyphs[code].unicode != 0) sequence.push_back(simple_glyphs[code].unicode); if (!sequence.empty()) code_to_unicode.emplace(code, std::move(sequence)); } } if (!code_to_unicode.empty()) { font.replaceKey("/ToUnicode", QPDFObjectHandle::newStream( &pdf, build_tounicode_cmap(code_to_unicode, cid))); ++state.tuc_fixed; state.changed = true; } } // Recompute and repair /Widths on a simple (8-bit) font from FreeType glyph advances, // preserving existing producer widths for unmapped codes in subsetted faces. static void repair_simple_font_widths(QPDFObjectHandle& target, FT_Face face, SimpleEncoding const& simple_encoding, bool simple_any_gid, std::vector const& simple_glyphs, FontNormalizeState& state) { if (simple_encoding.unsupportedEncoding || !simple_any_gid) return; int first_char = 0; int last_char = 255; QPDFObjectHandle existing_first = target.getKey("/FirstChar"); QPDFObjectHandle existing_last = target.getKey("/LastChar"); if (existing_first.isInteger()) { long long value = std::clamp(existing_first.getIntValue(), 0LL, 255LL); first_char = static_cast(value); } if (existing_last.isInteger()) { long long value = std::clamp(existing_last.getIntValue(), static_cast(first_char), 255LL); last_char = static_cast(value); } std::vector widths; std::vector resolved_codes; widths.reserve(static_cast(last_char - first_char + 1)); resolved_codes.reserve(static_cast(last_char - first_char + 1)); bool any_positive = false; QPDFObjectHandle existing_widths = target.getKey("/Widths"); const bool has_widths_array = existing_widths.isArray(); for (int code = first_char; code <= last_char; ++code) { int width = 0; if (simple_glyphs[code].gid != 0 && pdf_width_from_glyph(face, simple_glyphs[code].gid, width)) { if (width > 0) any_positive = true; widths.push_back(width); resolved_codes.push_back(true); continue; } // A re-subset face no longer maps codes whose glyphs were dropped. // Preserve the producer's width for unresolved codes instead of zeroing them. int preserved = 0; if (has_widths_array) { const int index = code - first_char; if (index >= 0 && index < existing_widths.getArrayNItems()) { QPDFObjectHandle item = existing_widths.getArrayItem(index); if (item.isNumber()) preserved = static_cast(std::lround(item.getNumericValue())); } } if (preserved > 0) any_positive = true; widths.push_back(preserved); resolved_codes.push_back(false); } bool differs = !has_widths_array || existing_widths.getArrayNItems() != static_cast(widths.size()); if (!differs) { for (size_t i = 0; i < widths.size(); ++i) { if (!resolved_codes[i]) continue; // Accept real-valued widths within one PDF glyph unit of FreeType's rounded advance. QPDFObjectHandle item = existing_widths.getArrayItem(static_cast(i)); if (!item.isNumber() || std::abs(item.getNumericValue() - widths[i]) > 1.0) { differs = true; break; } } } if (differs && any_positive) { QPDFObjectHandle array = QPDFObjectHandle::newArray(); for (int width : widths) array.appendItem(QPDFObjectHandle::newInteger(width)); target.replaceKey("/FirstChar", QPDFObjectHandle::newInteger(first_char)); target.replaceKey("/LastChar", QPDFObjectHandle::newInteger(last_char)); target.replaceKey("/Widths", array); ++state.w_fixed; state.changed = true; } } // Synthesize a missing /W array on a horizontal CIDFont from FreeType glyph advances. static void repair_cid_font_widths(QPDFObjectHandle& font, QPDFObjectHandle& target, FT_Face face, QPDFObjectHandle& cid_to_gid, bool cid_identity, std::map const& gid_to_cid, FontNormalizeState& state) { // Vertical writing uses /DW2 and /W2, not horizontal /W. if (font.getKey("/Encoding").isNameAndEquals("/Identity-V")) return; QPDFObjectHandle existing_w = target.getKey("/W"); if (existing_w.isArray() && existing_w.getArrayNItems() > 0) return; QPDFObjectHandle dw = target.getKey("/DW"); const int default_dw = dw.isInteger() ? dw.getIntValueAsInt() : 1000; // /W is keyed by CID, not GID. std::map cid_to_width; if (cid_to_gid.isStream()) { try { std::shared_ptr map = cid_to_gid.getStreamData(qpdf_dl_generalized); if (map) { const auto* bytes = static_cast(map->getBuffer()); const size_t entries = map->getSize() / 2; for (size_t c = 1; c < entries && c <= 0xFFFF; ++c) { const uint32_t gid = (static_cast(bytes[2 * c]) << 8) | bytes[2 * c + 1]; if (gid == 0) continue; int width = 0; if (pdf_width_from_glyph(face, gid, width) && width >= 0 && width != default_dw) cid_to_width[static_cast(c)] = width; } } } catch (...) { cid_to_width.clear(); } } else if (cid_identity) { if (FT_IS_CID_KEYED(face)) { for (auto const& entry : gid_to_cid) { int width = 0; if (pdf_width_from_glyph(face, (FT_UInt)entry.first, width) && width >= 0 && width != default_dw) cid_to_width[entry.second] = width; } } else { const int max_gid = static_cast( std::min(face->num_glyphs - 1, static_cast(65535))); for (int gid = 1; gid <= max_gid; ++gid) { int width = 0; if (pdf_width_from_glyph(face, (FT_UInt)gid, width) && width >= 0 && width != default_dw) cid_to_width[static_cast(gid)] = width; } } } if (cid_to_width.empty()) return; std::vector> entries(cid_to_width.begin(), cid_to_width.end()); QPDFObjectHandle widths = QPDFObjectHandle::newArray(); size_t i = 0; while (i < entries.size()) { size_t j = i + 1; bool same_width = true; while (j < entries.size() && entries[j].first == entries[j - 1].first + 1) { if (entries[j].second != entries[i].second) same_width = false; ++j; } if (j - i >= 2 && same_width) { widths.appendItem(QPDFObjectHandle::newInteger(entries[i].first)); widths.appendItem(QPDFObjectHandle::newInteger(entries[j - 1].first)); widths.appendItem(QPDFObjectHandle::newInteger(entries[i].second)); } else { widths.appendItem(QPDFObjectHandle::newInteger(entries[i].first)); QPDFObjectHandle run = QPDFObjectHandle::newArray(); for (size_t k = i; k < j; ++k) run.appendItem(QPDFObjectHandle::newInteger(entries[k].second)); widths.appendItem(run); } i = j; } if (widths.getArrayNItems() > 0) { target.replaceKey("/W", widths); ++state.w_fixed; state.changed = true; } } // Normalize a single PDF font dictionary across ToUnicode, widths, CFF packaging, and subsetting. static void process_font(QPDF& pdf, QPDFObjectHandle font, bool do_fix_tounicode, bool do_repair_widths, FontNormalizeState& state) { QPDFObjectHandle subtype = font.getKey("/Subtype"); if (!subtype.isName()) return; const std::string subtype_name = subtype.getName(); const bool cid = subtype_name == "/Type0"; if (!cid && subtype_name != "/TrueType" && subtype_name != "/Type1" && subtype_name != "/MMType1") return; // Type3 and anything unknown is left untouched. QPDFObjectHandle target = font; if (cid) { QPDFObjectHandle descendants = font.getKey("/DescendantFonts"); if (!descendants.isArray() || descendants.getArrayNItems() < 1) return; target = descendants.getArrayItem(0); if (!target.isDictionary()) return; // Only Identity-H/V uses two-byte Identity CIDs. A predefined or // embedded CMap changes the byte-to-CID mapping, so never assume // identity for those fonts. QPDFObjectHandle cid_encoding = font.getKey("/Encoding"); if (!cid_encoding.isName() || (!cid_encoding.isNameAndEquals("/Identity-H") && !cid_encoding.isNameAndEquals("/Identity-V"))) return; } QPDFObjectHandle descriptor = target.getKey("/FontDescriptor"); if (!descriptor.isDictionary()) return; QPDFObjectHandle stream; for (char const* key : {"/FontFile2", "/FontFile3", "/FontFile"}) { QPDFObjectHandle candidate = descriptor.getKey(key); if (candidate.isStream()) { stream = candidate; break; } } if (!stream.isStream()) return; // not embedded: PdfFontEmbedder handles that case std::shared_ptr data; try { data = stream.getStreamData(qpdf_dl_generalized); } catch (...) { return; } if (!data || data->getSize() == 0) return; ensure_freetype_init(); if (!ft_lib) return; const FT_Long face_index = 0; FT_Face raw = nullptr; if (FT_New_Memory_Face(ft_lib, data->getBuffer(), (FT_Long)data->getSize(), face_index, &raw) != 0 || !raw) return; std::unique_ptr face(raw, FT_Done_Face); if (face->num_glyphs <= 0) return; ++state.fonts_processed; const bool has_unicode_charmap = FT_Select_Charmap(face.get(), FT_ENCODING_UNICODE) == 0; std::map gid_to_unicode; std::set ambiguous_gids; auto is_space_scalar = [](uint32_t value) { return value == 0x20 || value == 0xA0 || value == 0xAD || value == 0x2D; }; if (has_unicode_charmap) { FT_UInt gid = 0; FT_ULong codepoint = FT_Get_First_Char(face.get(), &gid); while (gid != 0) { uint32_t unicode = static_cast(codepoint); auto [it, inserted] = gid_to_unicode.emplace(static_cast(gid), unicode); if (!inserted && it->second != unicode && !is_space_scalar(it->second) && !is_space_scalar(unicode)) ambiguous_gids.insert(static_cast(gid)); codepoint = FT_Get_Next_Char(face.get(), codepoint, &gid); } } // Verify how CIDs map to glyphs before trusting CID == GID. QPDFObjectHandle cid_to_gid; bool cid_identity = false; std::map gid_to_cid; if (cid) { QPDFObjectHandle cid_subtype = target.getKey("/Subtype"); cid_to_gid = target.getKey("/CIDToGIDMap"); if (cid_to_gid.isStream()) { // Explicit mapping stream; decoded by the ToUnicode/width helpers. } else if (cid_to_gid.isNull() || cid_to_gid.isNameAndEquals("/Identity")) { if (FT_IS_CID_KEYED(face.get())) { if (!cid_subtype.isNameAndEquals("/CIDFontType0")) return; for (FT_UInt gid = 0; gid < static_cast(face->num_glyphs); ++gid) { FT_UInt cid_value = 0; if (FT_Get_CID_From_Glyph_Index(face.get(), gid, &cid_value) == 0 && cid_value <= 0xFFFF) gid_to_cid[static_cast(gid)] = static_cast(cid_value); } cid_identity = true; } else { if (!cid_subtype.isNameAndEquals("/CIDFontType2")) return; cid_identity = true; } } else { return; // Unsupported /CIDToGIDMap value. } } const char* font_format = FT_Get_Font_Format(face.get()); const bool is_type1_family = subtype_name == "/Type1" || subtype_name == "/MMType1" || (font_format && (std::strcmp(font_format, "Type 1") == 0 || std::strcmp(font_format, "CFF") == 0)); SimpleEncoding simple_encoding; std::vector simple_glyphs; bool simple_any_gid = false; if (!cid) { simple_encoding = parseSimpleEncoding(font, descriptor); if (!simple_encoding.unsupportedEncoding) { simple_glyphs = resolveSimpleChars(face.get(), has_unicode_charmap, gid_to_unicode, simple_encoding, is_type1_family); for (auto const& glyph : simple_glyphs) { if (glyph.gid != 0) { simple_any_gid = true; break; } } } } if (do_fix_tounicode) { repair_font_tounicode(pdf, font, face.get(), cid, cid_to_gid, cid_identity, gid_to_cid, gid_to_unicode, ambiguous_gids, simple_any_gid, simple_glyphs, state); } if (do_repair_widths && face->units_per_EM > 0) { if (!cid) { repair_simple_font_widths(target, face.get(), simple_encoding, simple_any_gid, simple_glyphs, state); } else { repair_cid_font_widths(font, target, face.get(), cid_to_gid, cid_identity, gid_to_cid, state); } } } static int32_t font_normalize_qpdf(DocWrapper* w, int32_t page_index, bool do_fix_tounicode, bool do_repair_widths, int32_t* fonts_processed, int32_t* tuc_fixed, int32_t* w_fixed, int32_t* type1_converted, int32_t* resubset) { if (!w || !w->core || !w->core->doc || !fonts_processed || !tuc_fixed || !w_fixed || !type1_converted || !resubset) return JPDFIUM_ERR_INVALID; *fonts_processed = 0; *tuc_fixed = 0; *w_fixed = 0; *type1_converted = 0; *resubset = 0; const int page_count = FPDF_GetPageCount(w->core->doc); if (page_index < -1 || page_index >= page_count) return JPDFIUM_ERR_INVALID; std::vector pdf_bytes; if (!qpdf_save_fpdf(w->core->doc, pdf_bytes)) return JPDFIUM_ERR_IO; try { QPDF pdf; pdf.setSuppressWarnings(true); pdf.setAttemptRecovery(true); pdf.processMemoryFile("font_normalize", reinterpret_cast(pdf_bytes.data()), pdf_bytes.size()); std::vector pages = pdf.getAllPages(); if (page_index >= static_cast(pages.size())) return JPDFIUM_ERR_INVALID; FontNormalizeState state; // page_index == -1 means every page in one qpdf pass. Shared resources // and fonts are visited once, and the document is written back once. if (page_index == -1) { std::set seen_resources; std::set seen_fonts; for (auto& page : pages) { std::vector fonts = collect_page_fonts(page, seen_resources, seen_fonts); for (auto& font : fonts) { try { process_font(pdf, font, do_fix_tounicode, do_repair_widths, state); } catch (...) { // One malformed font must not abort the rest of the document. } } } // AcroForm default resources can carry fonts used by form fields // that never appear in a page's /Resources. std::vector form_fonts; std::map form_streams; collect_resource_fonts(pdf.getRoot().getKey("/AcroForm").getKey("/DR"), 0, seen_resources, seen_fonts, form_streams, form_fonts); for (auto& font : form_fonts) { try { process_font(pdf, font, do_fix_tounicode, do_repair_widths, state); } catch (...) { // One malformed font must not abort the rest of the document. } } } else { std::set seen_resources; std::set seen_fonts; std::vector fonts = collect_page_fonts(pages[page_index], seen_resources, seen_fonts); for (auto& font : fonts) { try { process_font(pdf, font, do_fix_tounicode, do_repair_widths, state); } catch (...) { // One malformed font must not abort the rest of the page. } } } *fonts_processed = state.fonts_processed; *tuc_fixed = state.tuc_fixed; *w_fixed = state.w_fixed; *type1_converted = state.type1_converted; *resubset = state.resubset; if (state.changed) return docwrapper_reload(w, qpdf_write(pdf)); return JPDFIUM_OK; } catch (QPDFExc const& e) { return e.getErrorCode() == qpdf_e_password ? JPDFIUM_ERR_PASSWORD : JPDFIUM_ERR_INVALID; } catch (std::exception&) { return JPDFIUM_ERR_INVALID; } } #endif // JPDFIUM_HAS_QPDF int32_t jpdfium_font_fix_tounicode(int64_t doc, int32_t page_index, int32_t* fonts_fixed) { try { int32_t ignored = 0; int32_t* fixed = fonts_fixed ? fonts_fixed : &ignored; *fixed = 0; DocWrapper* w = decodeDoc(doc); if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; #ifdef JPDFIUM_HAS_QPDF int32_t processed = 0; int32_t widths = 0; int32_t type1 = 0; int32_t resubset = 0; return font_normalize_qpdf(w, page_index, true, false, &processed, fixed, &widths, &type1, &resubset); #else (void)page_index; return JPDFIUM_ERR_NOT_FOUND; #endif } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_font_repair_widths(int64_t doc, int32_t page_index, int32_t* fonts_fixed) { try { int32_t ignored = 0; int32_t* fixed = fonts_fixed ? fonts_fixed : &ignored; *fixed = 0; DocWrapper* w = decodeDoc(doc); if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; #ifdef JPDFIUM_HAS_QPDF int32_t processed = 0; int32_t tounicode = 0; int32_t type1 = 0; int32_t resubset = 0; return font_normalize_qpdf(w, page_index, false, true, &processed, &tounicode, fixed, &type1, &resubset); #else (void)page_index; return JPDFIUM_ERR_NOT_FOUND; #endif } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_font_normalize_page(int64_t doc, int32_t page_index, char** json) { try { if (!json) return JPDFIUM_ERR_INVALID; *json = nullptr; DocWrapper* w = decodeDoc(doc); if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; int32_t processed = 0, tuc_fixed = 0, w_fixed = 0; int32_t type1_converted = 0, resubset = 0; #ifdef JPDFIUM_HAS_QPDF int32_t rc = font_normalize_qpdf(w, page_index, true, true, &processed, &tuc_fixed, &w_fixed, &type1_converted, &resubset); if (rc != JPDFIUM_OK) return rc; #else (void)page_index; return JPDFIUM_ERR_NOT_FOUND; #endif char buf[256]; snprintf(buf, sizeof(buf), "{\"fonts_processed\":%d,\"tounicode_fixed\":%d," "\"widths_repaired\":%d,\"type1_converted\":%d,\"resubset\":%d}", processed, tuc_fixed, w_fixed, type1_converted, resubset); *json = strdup(buf); return *json ? JPDFIUM_OK : JPDFIUM_ERR_NATIVE; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_font_subset(const uint8_t* font_data, int64_t font_len, const uint32_t* codepoints, int32_t num_codepoints, int32_t retain_gids, uint8_t** out_data, int64_t* out_len) { try { if (!font_data || font_len <= 0 || !codepoints || num_codepoints <= 0 || !out_data || !out_len) { return JPDFIUM_ERR_INVALID; } // Create HarfBuzz blob from font data hb_blob_t* blob = hb_blob_create((const char*)font_data, (unsigned int)font_len, HB_MEMORY_MODE_READONLY, nullptr, nullptr); hb_face_t* face = hb_face_create(blob, 0); hb_blob_destroy(blob); if (!face) return JPDFIUM_ERR_INVALID; if (hb_face_get_glyph_count(face) <= 1) { hb_face_destroy(face); return JPDFIUM_ERR_INVALID; } // Create subset input hb_subset_input_t* input = hb_subset_input_create_or_fail(); if (!input) { hb_face_destroy(face); return JPDFIUM_ERR_NATIVE; } // Add requested codepoints hb_set_t* unicode_set = hb_subset_input_unicode_set(input); for (int32_t i = 0; i < num_codepoints; i++) { hb_set_add(unicode_set, codepoints[i]); } // Set flags if (retain_gids) { hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_RETAIN_GIDS); } // Perform subsetting hb_face_t* subset_face = hb_subset_or_fail(face, input); hb_subset_input_destroy(input); hb_face_destroy(face); if (!subset_face) return JPDFIUM_ERR_NATIVE; // Extract result hb_blob_t* result_blob = hb_face_reference_blob(subset_face); unsigned int result_len; const char* result_data = hb_blob_get_data(result_blob, &result_len); *out_len = result_len; if (result_len == 0) { hb_blob_destroy(result_blob); hb_face_destroy(subset_face); return JPDFIUM_ERR_NATIVE; } *out_data = (uint8_t*)malloc(result_len); if (!*out_data) { hb_blob_destroy(result_blob); hb_face_destroy(subset_face); return JPDFIUM_ERR_NATIVE; } memcpy(*out_data, result_data, result_len); hb_blob_destroy(result_blob); hb_face_destroy(subset_face); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } #else // !FREETYPE || !HARFBUZZ int32_t jpdfium_font_get_data(int64_t, int32_t, uint8_t** data, int64_t* len) { if (data) *data = nullptr; if (len) *len = 0; return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_font_classify(const uint8_t*, int64_t, char** json) { if (json) *json = strdup("{\"type\":\"unknown\",\"error\":\"FreeType not available\"}"); return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_font_covers_text(const uint8_t*, int64_t, const int32_t*, int32_t, uint8_t*) { return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_text_shape(const uint8_t*, int64_t, const char*, float, char** json) { if (json) *json = nullptr; return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_font_fix_tounicode(int64_t, int32_t, int32_t* f) { if (f) *f = 0; return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_font_repair_widths(int64_t, int32_t, int32_t* f) { if (f) *f = 0; return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_font_normalize_page(int64_t, int32_t, char** json) { if (json) *json = strdup("{\"error\":\"FreeType/HarfBuzz not available\"}"); return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_font_subset(const uint8_t*, int64_t, const uint32_t*, int32_t, int32_t, uint8_t** out, int64_t* len) { if (out) *out = nullptr; if (len) *len = 0; return JPDFIUM_ERR_NOT_FOUND; } #endif // JPDFIUM_HAS_FREETYPE && JPDFIUM_HAS_HARFBUZZ // Glyph-Level Redaction #if defined(JPDFIUM_HAS_HARFBUZZ) && defined(JPDFIUM_HAS_ICU) #include #include #include int32_t jpdfium_redact_glyph_aware(int64_t page, const char** words, int32_t word_count, uint32_t argb, float padding, uint32_t flags, int32_t* match_count, char** result_json) { try { if (!words || word_count <= 0 || !match_count || !result_json) return JPDFIUM_ERR_INVALID; // Real implementation pipeline: // 1. Extract text with FPDFText_GetCharBox() for per-character bounding boxes // 2. If JPDFIUM_GLYPH_LIGATURE_AWARE: use HarfBuzz hb_shape() for cluster mapping // 3. If JPDFIUM_GLYPH_BIDI_AWARE: ICU BiDi for visual order resolution // 4. If JPDFIUM_GLYPH_GRAPHEME_SAFE: grapheme cluster boundaries // 5. Match words against extracted text // 6. Compute precise redact rectangles from glyph advances // 7. If JPDFIUM_GLYPH_REMOVE_STREAM: qpdf structural content removal // 8. Paint filled rectangles (void)page; (void)argb; (void)padding; (void)flags; *match_count = 0; *result_json = strdup("[]"); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } #else int32_t jpdfium_redact_glyph_aware(int64_t, const char**, int32_t, uint32_t, float, uint32_t, int32_t* match_count, char** result_json) { if (match_count) *match_count = 0; if (result_json) *result_json = strdup("[]"); return JPDFIUM_ERR_NOT_FOUND; } #endif // XMP Metadata Redaction + Font Strip - qpdf #ifdef JPDFIUM_HAS_QPDF #include #include #include #include #include #include "jpdfium_qpdf_writer_lock.h" // Save an FPDF_DOCUMENT to a byte vector. static bool qpdf_save_fpdf(FPDF_DOCUMENT doc, std::vector& out) { struct BufWriter : FPDF_FILEWRITE { std::vector* buf; static int Write(FPDF_FILEWRITE* self, const void* data, unsigned long size) { auto* bw = static_cast(self); try { const auto* src = static_cast(data); bw->buf->insert(bw->buf->end(), src, src + size); return 1; } catch (...) { return 0; // never let exceptions cross the C callback } } } bw; bw.version = 1; bw.WriteBlock = BufWriter::Write; bw.buf = &out; return FPDF_SaveAsCopy(doc, &bw, FPDF_NO_INCREMENTAL) != 0; } // Write a QPDF document to a byte vector. static std::vector qpdf_write(QPDF& pdf) { QPDFWriter writer(pdf); writer.setOutputMemory(); writer.setLinearization(false); writer.setCompressStreams(true); // PDF/A validators expect the endstream keyword on its own line. Write it // explicitly instead of relying on the qpdf default. writer.setNewlineBeforeEndstream(true); jpdfium_qpdf_internal::WriterLock lock{-1}; writer.write(); std::shared_ptr buf = writer.getBufferSharedPointer(); std::vector result(buf->getSize()); memcpy(result.data(), buf->getBuffer(), buf->getSize()); return result; } // Replace the FPDF_DOCUMENT inside a DocWrapper with a document loaded from new // bytes. The DocWrapper pointer itself is unchanged, so the Java-side handle // remains valid. // // Strong failure contract: the candidate is fully prepared, loaded, and // validated before anything is retired. If any step fails, the original // document is left untouched and usable. Only after a valid candidate exists // are the old resources retired and the replacement committed. static int32_t docwrapper_reload(DocWrapper* w, const std::vector& newBytes) { if (!w || !w->core || !w->core->doc) return JPDFIUM_ERR_INVALID; if (newBytes.empty() || newBytes.size() > static_cast(INT32_MAX)) return JPDFIUM_ERR_INVALID; size_t sz = newBytes.size(); auto* copy = static_cast(malloc(sz)); if (!copy) return JPDFIUM_ERR_NATIVE; memcpy(copy, newBytes.data(), sz); FPDF_DOCUMENT candidate = FPDF_LoadMemDocument(copy, static_cast(sz), nullptr); if (!candidate) { free(copy); return JPDFIUM_ERR_NATIVE; } // Retire native state tied to the old document before committing the // replacement: loaded FPDF_FONT handles belong to the old FPDF_DOCUMENT // and would otherwise dangle. The redaction audit state is deliberately // preserved: reload (metadata/font stripping) round-trips the same pages // through QPDF, so committed redactions, unapplied-mark counts, and the // literal/zone audit trail stay valid for the replacement bytes. Dropping // them here would silently disable the save-time sanitize pass (which // requires contentRedacted + sanitizeOnSave and scrubs XMP against // redactedLiterals) and the uncommitted-marks save refusal on the very // documents that need them. Only per-document resources are retired: // font handles, the pattern cache, the stale sanitize report, and the // source path (the replacement is memory-backed, so alias protection // against the old backing file no longer applies). for (FPDF_FONT f : w->core->loadedFonts) { FPDFFont_Close(f); } w->core->loadedFonts.clear(); w->core->clearRedactPatternCache(); w->core->touchedFontsSet.clear(); w->core->touchedFontNames.clear(); w->core->sanitizeReport.clear(); w->core->sourcePath.clear(); FPDF_CloseDocument(w->core->doc); free(w->core->buf); // free() ignores NULL w->core->doc = candidate; w->core->buf = copy; w->core->blen = static_cast(sz); // Replacement committed: previously opened pages now point into the freed // document. Their wrappers detect the mismatch and refuse use and release. ++w->core->generation; return JPDFIUM_OK; } int32_t jpdfium_metadata_strip(int64_t doc, const char** keys, int32_t key_count) { DocWrapper* w = decodeDoc(doc); if (!w || !w->core->doc || !keys || key_count <= 0) return JPDFIUM_ERR_INVALID; std::vector pdfBytes; if (!qpdf_save_fpdf(w->core->doc, pdfBytes)) return JPDFIUM_ERR_IO; try { QPDF pdf; pdf.setSuppressWarnings(true); pdf.setAttemptRecovery(true); pdf.processMemoryFile("strip", reinterpret_cast(pdfBytes.data()), pdfBytes.size()); QPDFObjectHandle trailer = pdf.getTrailer(); if (trailer.hasKey("/Info")) { QPDFObjectHandle info = trailer.getKey("/Info"); if (info.isDictionary()) { for (int i = 0; i < key_count; i++) { std::string qkey = std::string("/") + keys[i]; if (info.hasKey(qkey)) info.removeKey(qkey); } } } return docwrapper_reload(w, qpdf_write(pdf)); } catch (std::exception&) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_metadata_strip_all(int64_t doc) { DocWrapper* w = decodeDoc(doc); if (!w || !w->core->doc) return JPDFIUM_ERR_INVALID; std::vector pdfBytes; if (!qpdf_save_fpdf(w->core->doc, pdfBytes)) return JPDFIUM_ERR_IO; try { QPDF pdf; pdf.setSuppressWarnings(true); pdf.setAttemptRecovery(true); pdf.processMemoryFile("strip_all", reinterpret_cast(pdfBytes.data()), pdfBytes.size()); QPDFObjectHandle trailer = pdf.getTrailer(); if (trailer.hasKey("/Info")) trailer.removeKey("/Info"); QPDFObjectHandle root = pdf.getRoot(); if (root.hasKey("/Metadata")) root.removeKey("/Metadata"); if (root.hasKey("/MarkInfo")) root.removeKey("/MarkInfo"); return docwrapper_reload(w, qpdf_write(pdf)); } catch (std::exception&) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_strip_fonts(int64_t doc, int32_t* fonts_removed) { DocWrapper* w = decodeDoc(doc); if (!w || !w->core->doc || !fonts_removed) return JPDFIUM_ERR_INVALID; std::vector pdfBytes; if (!qpdf_save_fpdf(w->core->doc, pdfBytes)) return JPDFIUM_ERR_IO; try { QPDF pdf; pdf.setSuppressWarnings(true); pdf.setAttemptRecovery(true); pdf.processMemoryFile("strip_fonts", reinterpret_cast(pdfBytes.data()), pdfBytes.size()); int count = 0; std::set processed; // object IDs of shared resource dicts already stripped // NOLINTNEXTLINE(performance-for-range-copy) - copy is intentional; hasKey/getKey are // non-const in libqpdf on Ubuntu 24.04 (qpdf < 11.9 in apt) for (auto page : pdf.getAllPages()) { if (!page.hasKey("/Resources")) continue; QPDFObjectHandle resources = page.getKey("/Resources"); if (!resources.isDictionary()) continue; // Shared indirect resource dicts appear on multiple pages; strip only once. if (resources.isIndirect()) { int objid = resources.getObjectID(); if (processed.count(objid)) continue; processed.insert(objid); } if (!resources.hasKey("/Font")) continue; QPDFObjectHandle fonts = resources.getKey("/Font"); if (!fonts.isDictionary()) continue; count += static_cast(fonts.getKeys().size()); resources.removeKey("/Font"); } *fonts_removed = count; return docwrapper_reload(w, qpdf_write(pdf)); } catch (std::exception&) { return JPDFIUM_ERR_NATIVE; } } #else int32_t jpdfium_metadata_strip(int64_t, const char**, int32_t) { return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_metadata_strip_all(int64_t) { return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_strip_fonts(int64_t, int32_t* fonts_removed) { if (fonts_removed) *fonts_removed = 0; return JPDFIUM_ERR_NOT_FOUND; } #endif // XMP pattern redaction requires pugixml in addition to qpdf #if defined(JPDFIUM_HAS_PUGIXML) && defined(JPDFIUM_HAS_QPDF) #include int32_t jpdfium_xmp_redact_patterns(int64_t doc, const char** patterns, int32_t pattern_count, int32_t* fields_redacted) { try { (void)doc; (void)patterns; (void)pattern_count; if (fields_redacted) *fields_redacted = 0; return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } #else int32_t jpdfium_xmp_redact_patterns(int64_t, const char**, int32_t, int32_t* f) { if (f) *f = 0; return JPDFIUM_ERR_NOT_FOUND; } #endif // ICU4C Text Processing #ifdef JPDFIUM_HAS_ICU #include #include #include #include int32_t jpdfium_icu_normalize_nfc(const char* text, char** result) { try { if (!text || !result) return JPDFIUM_ERR_INVALID; UErrorCode status = U_ZERO_ERROR; icu::UnicodeString src = icu::UnicodeString::fromUTF8(text); icu::UnicodeString dst; icu::Normalizer::normalize(src, UNORM_NFC, 0, dst, status); if (U_FAILURE(status)) { *result = strdup(text); // fallback to original return JPDFIUM_OK; } std::string utf8; dst.toUTF8String(utf8); *result = strdup(utf8.c_str()); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_icu_break_sentences(const char* text, char** json_result) { try { if (!text || !json_result) return JPDFIUM_ERR_INVALID; UErrorCode status = U_ZERO_ERROR; std::unique_ptr bi( icu::BreakIterator::createSentenceInstance(icu::Locale::getDefault(), status)); if (U_FAILURE(status)) { *json_result = strdup("[]"); return JPDFIUM_ERR_NATIVE; } icu::UnicodeString utext = icu::UnicodeString::fromUTF8(text); bi->setText(utext); std::string json = "["; bool first = true; int32_t start = bi->first(); int32_t end = bi->next(); while (end != icu::BreakIterator::DONE) { if (!first) json += ","; first = false; icu::UnicodeString segment; utext.extractBetween(start, end, segment); std::string segUtf8; segment.toUTF8String(segUtf8); // Escape for JSON std::string escaped; for (char c : segUtf8) { switch (c) { case '"': escaped += "\\\""; break; case '\\': escaped += "\\\\"; break; case '\n': escaped += "\\n"; break; case '\r': escaped += "\\r"; break; case '\t': escaped += "\\t"; break; default: escaped += c; } } char buf[4096]; snprintf(buf, sizeof(buf), "{\"start\":%d,\"end\":%d,\"text\":\"%s\"}", start, end, escaped.c_str()); json += buf; start = end; end = bi->next(); } json += "]"; *json_result = strdup(json.c_str()); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } int32_t jpdfium_icu_bidi_reorder(const char* text, char** result) { try { if (!text || !result) return JPDFIUM_ERR_INVALID; icu::UnicodeString utext = icu::UnicodeString::fromUTF8(text); int32_t length = utext.length(); if (length == 0) { *result = strdup(""); return JPDFIUM_OK; } UErrorCode status = U_ZERO_ERROR; UBiDi* bidi = ubidi_open(); ubidi_setPara(bidi, utext.getBuffer(), length, UBIDI_DEFAULT_LTR, nullptr, &status); if (U_FAILURE(status)) { ubidi_close(bidi); *result = strdup(text); return JPDFIUM_OK; } // Get visual ordering icu::UnicodeString visual(length, 0, 0); int32_t runCount = ubidi_countRuns(bidi, &status); int32_t visPos = 0; for (int32_t i = 0; i < runCount; i++) { int32_t logStart, runLen; UBiDiDirection dir = ubidi_getVisualRun(bidi, i, &logStart, &runLen); if (dir == UBIDI_RTL) { for (int32_t j = logStart + runLen - 1; j >= logStart; --j) { visual.setCharAt(visPos++, utext.charAt(j)); } } else { for (int32_t j = logStart; j < logStart + runLen; ++j) { visual.setCharAt(visPos++, utext.charAt(j)); } } } ubidi_close(bidi); std::string utf8; visual.toUTF8String(utf8); *result = strdup(utf8.c_str()); return JPDFIUM_OK; } catch (...) { return JPDFIUM_ERR_NATIVE; } } #else // !JPDFIUM_HAS_ICU int32_t jpdfium_icu_normalize_nfc(const char* text, char** result) { if (result) *result = strdup(text ? text : ""); return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_icu_break_sentences(const char*, char** json_result) { if (json_result) *json_result = strdup("[]"); return JPDFIUM_ERR_NOT_FOUND; } int32_t jpdfium_icu_bidi_reorder(const char* text, char** result) { if (result) *result = strdup(text ? text : ""); return JPDFIUM_ERR_NOT_FOUND; } #endif // JPDFIUM_HAS_ICU