From aec1220bff1bb91af0b416d92be9f7ec8fd4c8a9 Mon Sep 17 00:00:00 2001 From: Svetlana Kulikova Date: Tue, 16 Dec 2025 15:41:14 +0300 Subject: [PATCH] Create GetGIDByUnicode --- DesktopEditor/doctrenderer/drawingfile.h | 7 +++ DesktopEditor/fontengine/FontFile.cpp | 4 +- .../graphics/pro/js/wasm/js/drawingfile.js | 5 ++ .../pro/js/wasm/js/drawingfile_native.js | 6 ++ .../pro/js/wasm/js/drawingfile_wasm.js | 28 +++++++++ .../graphics/pro/js/wasm/src/drawingfile.cpp | 4 ++ .../pro/js/wasm/src/drawingfile_test.cpp | 30 ++++++++++ PdfFile/PdfFile.cpp | 6 ++ PdfFile/PdfFile.h | 1 + PdfFile/PdfReader.cpp | 58 +++++++++++++++++++ PdfFile/PdfReader.h | 1 + 11 files changed, 148 insertions(+), 2 deletions(-) diff --git a/DesktopEditor/doctrenderer/drawingfile.h b/DesktopEditor/doctrenderer/drawingfile.h index 47bf314044..6b10dc9393 100644 --- a/DesktopEditor/doctrenderer/drawingfile.h +++ b/DesktopEditor/doctrenderer/drawingfile.h @@ -570,6 +570,13 @@ public: } return NULL; } + BYTE* GetGIDByUnicode(const std::string& sPathA) + { + if (m_nType != 0) + return NULL; + std::wstring sFontName = UTF8_TO_U(sPathA); + return ((CPdfFile*)m_pFile)->GetGIDByUnicode(sFontName); + } std::wstring GetFontBinaryNative(const std::wstring& sName) { diff --git a/DesktopEditor/fontengine/FontFile.cpp b/DesktopEditor/fontengine/FontFile.cpp index e3840f70b7..4d843a2cd8 100644 --- a/DesktopEditor/fontengine/FontFile.cpp +++ b/DesktopEditor/fontengine/FontFile.cpp @@ -731,10 +731,10 @@ void CFontFile::CheckHintsSupport() int CFontFile::SetCMapForCharCode(long lUnicode, int *pnCMapIndex) { *pnCMapIndex = -1; - if (!m_pFace) + if (!m_pFace || !m_pFace->num_charmaps) return 0; - if ( m_bStringGID || 0 == m_pFace->num_charmaps ) + if ( m_bStringGID ) return lUnicode; int nCharIndex = 0; diff --git a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile.js b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile.js index ba24184e09..2d226c1c35 100644 --- a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile.js +++ b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile.js @@ -354,6 +354,11 @@ CFile.prototype["getFontByID"] = function(ID) return this._getFontByID(ID); }; +CFile.prototype["getGIDByUnicode"] = function(ID) +{ + return this._getGIDByUnicode(ID); +}; + CFile.prototype["setCMap"] = function(memoryBuffer) { if (!this.nativeFile) diff --git a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_native.js b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_native.js index fb8f660041..ab71ce4199 100644 --- a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_native.js +++ b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_native.js @@ -162,6 +162,12 @@ CFile.prototype._getFontByID = function(ID) return g_native_drawing_file["GetFontBinary"](ID); }; +CFile.prototype._getGIDByUnicode = function(ID) +{ + g_module_pointer.ptr = g_native_drawing_file["GetGIDByUnicode"](ID); + return g_module_pointer; +} + CFile.prototype._getInteractiveFormsFonts = function(type) { g_module_pointer.ptr = g_native_drawing_file["GetInteractiveFormsFonts"](type); diff --git a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_wasm.js b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_wasm.js index be1c6a32d4..f54acfa4b1 100644 --- a/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_wasm.js +++ b/DesktopEditor/graphics/pro/js/wasm/js/drawingfile_wasm.js @@ -263,6 +263,34 @@ CFile.prototype._getFontByID = function(ID) return res; }; +CFile.prototype._getGIDByUnicode = function(ID) +{ + if (ID === undefined) + return null; + + let idBuffer = ID.toUtf8(); + let idPointer = Module["_malloc"](idBuffer.length); + Module["HEAP8"].set(idBuffer, idPointer); + g_module_pointer.ptr = Module["_GetGIDByUnicode"](this.nativeFile, idPointer); + Module["_free"](idPointer); + + let reader = g_module_pointer.getReader(); + if (!reader) + return null; + + let res = {}; + let nFontLength = reader.readInt(); + for (let i = 0; i < nFontLength; i++) + { + let np1 = reader.readInt(); + let np2 = reader.readInt(); + res[np2] = np1; + } + + g_module_pointer.free(); + return res; +} + CFile.prototype._getInteractiveFormsFonts = function(type) { g_module_pointer.ptr = Module["_GetInteractiveFormsFonts"](this.nativeFile, type); diff --git a/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.cpp b/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.cpp index e73a0557b8..3fa678bbf2 100644 --- a/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.cpp +++ b/DesktopEditor/graphics/pro/js/wasm/src/drawingfile.cpp @@ -150,6 +150,10 @@ WASM_EXPORT BYTE* GetFontBinary(CDrawingFile* pFile, char* path) { return pFile->GetFontBinary(std::string(path)); } +WASM_EXPORT BYTE* GetGIDByUnicode(CDrawingFile* pFile, char* path) +{ + return pFile->GetGIDByUnicode(std::string(path)); +} WASM_EXPORT void DestroyTextInfo(CDrawingFile* pFile) { return pFile->DestroyTextInfo(); diff --git a/DesktopEditor/graphics/pro/js/wasm/src/drawingfile_test.cpp b/DesktopEditor/graphics/pro/js/wasm/src/drawingfile_test.cpp index 82fd656dca..0fbb058f35 100644 --- a/DesktopEditor/graphics/pro/js/wasm/src/drawingfile_test.cpp +++ b/DesktopEditor/graphics/pro/js/wasm/src/drawingfile_test.cpp @@ -938,6 +938,36 @@ void ReadInteractiveFormsFonts(CDrawingFile* pGrFile, int nType) std::cout << "font" << j << ".txt"; } + if (pFont) + free(pFont); + + if (false) + continue; + + pFont = GetGIDByUnicode(pGrFile, (char*)sFontName.c_str()); + nLength2 = READ_INT(pFont); + i2 = 4; + nLength2 -= 4; + + while (i2 < nLength2) + { + int nFontLength = READ_INT(pFont + i2); + i2 += 4; + + std::cout << std::endl << "CIDtoUnicode" << std::endl; + + for (int j = 0; j < nFontLength; ++j) + { + unsigned int code = READ_INT(pFont + i2); + i2 += 4; + unsigned int unicode = READ_INT(pFont + i2); + i2 += 4; + std::cout << "cid\t" << code << "\tunicode\t" << unicode << std::endl; + } + + std::cout << std::endl; + } + if (pFont) free(pFont); } diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index a5dac89843..2d0aa9558e 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -445,6 +445,12 @@ BYTE* CPdfFile::GetAnnotStandardFonts() return NULL; return m_pInternal->pReader->GetFonts(true); } +BYTE* CPdfFile::GetGIDByUnicode(const std::wstring& wsFontName) +{ + if (!m_pInternal->pReader) + return NULL; + return m_pInternal->pReader->GetGIDByUnicode(wsFontName); +} std::wstring CPdfFile::GetFontPath(const std::wstring& wsFontName) { if (!m_pInternal->pReader) diff --git a/PdfFile/PdfFile.h b/PdfFile/PdfFile.h index 9c89debeb5..4e9b5ae288 100644 --- a/PdfFile/PdfFile.h +++ b/PdfFile/PdfFile.h @@ -145,6 +145,7 @@ public: BYTE* GetAPWidget (int nRasterW, int nRasterH, int nBackgroundColor, int nPageIndex, int nWidget = -1, const char* sView = NULL, const char* sBView = NULL); BYTE* GetAPAnnots (int nRasterW, int nRasterH, int nBackgroundColor, int nPageIndex, int nAnnot = -1, const char* sView = NULL); BYTE* GetButtonIcon(int nBackgroundColor, int nPageIndex, bool bBase64 = false, int nBWidget = -1, const char* sIView = NULL); + BYTE* GetGIDByUnicode(const std::wstring& wsFontName); std::wstring GetFontPath(const std::wstring& wsFontName); std::wstring GetEmbeddedFontPath(const std::wstring& wsFontName); diff --git a/PdfFile/PdfReader.cpp b/PdfFile/PdfReader.cpp index 733998c52d..b14b1a0eb1 100644 --- a/PdfFile/PdfReader.cpp +++ b/PdfFile/PdfReader.cpp @@ -779,7 +779,10 @@ bool CPdfReader::RedactPage(int _nPageIndex, double* arrRedactBox, int nLengthX8 PDFDoc* pDoc = NULL; int nPageIndex = GetPageIndex(_nPageIndex, &pDoc); if (nPageIndex < 0 || !pDoc) + { + free(pChanges); return false; + } Page* pPage = pDoc->getCatalog()->getPage(nPageIndex); PDFRectangle* cropBox = pPage->getCropBox(); @@ -1099,6 +1102,61 @@ std::wstring CPdfReader::GetInfo() return sRes; } +BYTE* CPdfReader::GetGIDByUnicode(const std::wstring& wsFontName) +{ + std::map::const_iterator oIter = m_mFonts.find(wsFontName); + if (oIter == m_mFonts.end()) + return NULL; + + NSFonts::IFontsMemoryStorage* pStorage = NSFonts::NSApplicationFontStream::GetGlobalMemoryStorage(); + if (!pStorage) + return NULL; + NSFonts::IFontStream* pStream = pStorage->Get(oIter->second); + if (!pStream) + return NULL; + + std::map mGIDbyUnicode; + bool bFind = false; + for (CPdfReaderContext* pPDFContext : m_vPDFContext) + { + PdfReader::CPdfFontList* pFontList = pPDFContext->m_pFontList; + + const std::map& mapFonts = pFontList->GetFonts(); + for (std::map::const_iterator it = mapFonts.begin(); it != mapFonts.end(); ++it) + { + PdfReader::TFontEntry* pEntry = it->second; + if (!pEntry || pEntry->wsFilePath != oIter->second) + continue; + bFind = true; + + for (int i = 0; i < pEntry->unLenUnicode; ++i) + { + if (pEntry->pCodeToUnicode[i]) + mGIDbyUnicode[i] = pEntry->pCodeToUnicode[i]; + } + + break; + } + + if (bFind) + break; + } + + NSWasm::CData oRes; + oRes.SkipLen(); + oRes.AddInt(mGIDbyUnicode.size()); + + for (std::map::const_iterator it = mGIDbyUnicode.begin(); it != mGIDbyUnicode.end(); ++it) + { + oRes.AddInt(it->first); + oRes.AddInt(it->second); + } + + oRes.WriteLen(); + BYTE* bRes = oRes.GetBuffer(); + oRes.ClearWithoutAttack(); + return bRes; +} std::wstring CPdfReader::GetFontPath(const std::wstring& wsFontName, bool bSave) { std::map::const_iterator oIter = m_mFonts.find(wsFontName); diff --git a/PdfFile/PdfReader.h b/PdfFile/PdfReader.h index 4a35f79963..e60a11695c 100644 --- a/PdfFile/PdfReader.h +++ b/PdfFile/PdfReader.h @@ -98,6 +98,7 @@ public: void GetPageInfo(int nPageIndex, double* pdWidth, double* pdHeight, double* pdDpiX, double* pdDpiY); void DrawPageOnRenderer(IRenderer* pRenderer, int nPageIndex, bool* pBreak); std::wstring GetInfo(); + BYTE* GetGIDByUnicode(const std::wstring& wsFontName); std::wstring GetFontPath(const std::wstring& wsFontName, bool bSave = true); std::wstring ToXml(const std::wstring& wsXmlPath, bool isPrintStreams = false); void ChangeLength(DWORD nLength) { m_nFileLength = nLength; }