From 4c1420a3a6e7888cfb80fab84a406ed49e842637 Mon Sep 17 00:00:00 2001 From: Svetlana Kulikova Date: Thu, 1 Feb 2024 17:55:41 +0300 Subject: [PATCH] PdfReader refactoring --- PdfFile/PdfReader.cpp | 455 ++++++++++++++++++++---------------------- PdfFile/PdfReader.h | 13 +- 2 files changed, 217 insertions(+), 251 deletions(-) diff --git a/PdfFile/PdfReader.cpp b/PdfFile/PdfReader.cpp index 32a45a7b07..1762d72702 100644 --- a/PdfFile/PdfReader.cpp +++ b/PdfFile/PdfReader.cpp @@ -60,7 +60,6 @@ CPdfReader::CPdfReader(NSFonts::IApplicationFonts* pAppFonts) { - m_wsTempFolder = L""; m_pPDFDocument = NULL; m_nFileLength = 0; @@ -108,74 +107,75 @@ CPdfReader::~CPdfReader() RELEASEINTERFACE(m_pFontManager); } -bool scanFonts(Dict *pResources, PDFDoc *pDoc, const std::vector& arrCMap, int nDepth) +bool scanFonts(Dict *pResources, const std::vector& arrCMap, int nDepth) { if (nDepth > 5) return false; Object oFonts; - if (pResources->lookup("Font", &oFonts) && oFonts.isDict()) + if (pResources->lookup("Font", &oFonts)->isDict()) { for (int i = 0, nLength = oFonts.dictGetLength(); i < nLength; ++i) { Object oFont, oEncoding; - if (!oFonts.dictGetVal(i, &oFont) || !oFont.isDict() || !oFont.dictLookup("Encoding", &oEncoding) || !oEncoding.isName()) + if (!oFonts.dictGetVal(i, &oFont)->isDict() || !oFont.dictLookup("Encoding", &oEncoding)->isName()) { oFont.free(); oEncoding.free(); continue; } - + oFont.free(); char* sName = oEncoding.getName(); if (std::find(arrCMap.begin(), arrCMap.end(), sName) != arrCMap.end()) { - oEncoding.free(); oFont.free(); oFonts.free(); + oEncoding.free();oFonts.free(); return true; } - oEncoding.free(); oFont.free(); + oEncoding.free(); } } oFonts.free(); -#define SCAN_FONTS(sName)\ -{\ - Object oObject;\ - if (pResources->lookup(sName, &oObject) && oObject.isDict())\ - {\ - for (int i = 0, nLength = oObject.dictGetLength(); i < nLength; ++i)\ - {\ - Object oXObj, oResources;\ - if (!oObject.dictGetVal(i, &oXObj) || !oXObj.isStream() || !oXObj.streamGetDict()->lookup("Resources", &oResources) || !oResources.isDict())\ - {\ - oXObj.free(); oResources.free();\ - continue;\ - }\ - oXObj.free();\ - if (scanFonts(oResources.getDict(), pDoc, arrCMap, nDepth + 1))\ - {\ - oResources.free(); oObject.free();\ - return true;\ - }\ - oResources.free();\ - }\ - }\ - oObject.free();\ -} - SCAN_FONTS("XObject"); - SCAN_FONTS("Pattern"); + auto fScanFonts = [pResources, &arrCMap, &nDepth](const char* sName) + { + Object oObject; + if (pResources->lookup(sName, &oObject)->isDict()) + { + for (int i = 0, nLength = oObject.dictGetLength(); i < nLength; ++i) + { + Object oXObj, oResources; + if (!oObject.dictGetVal(i, &oXObj)->isStream() || !oXObj.streamGetDict()->lookup("Resources", &oResources)->isDict()) + { + oXObj.free(); oResources.free(); + continue; + } + oXObj.free(); + if (scanFonts(oResources.getDict(), arrCMap, nDepth + 1)) + { + oResources.free(); oObject.free(); + return true; + } + oResources.free(); + } + } + oObject.free(); + return false; + }; + + if (fScanFonts("XObject") || fScanFonts("Pattern")) + return true; Object oExtGState; - if (pResources->lookup("ExtGState", &oExtGState) && oExtGState.isDict()) + if (pResources->lookup("ExtGState", &oExtGState)->isDict()) { for (int i = 0, nLength = oExtGState.dictGetLength(); i < nLength; ++i) { Object oGS, oSMask, oSMaskGroup, oResources; - if (!oExtGState.dictGetVal(i, &oGS) || !oGS.isDict() || !oGS.dictLookup("SMask", &oSMask) || !oSMask.isDict() || !oSMask.dictLookup("G", &oSMaskGroup) || !oSMaskGroup.isStream() || - oSMaskGroup.streamGetDict()->lookup("Resources", &oResources) || !oResources.isDict()) + if (!oExtGState.dictGetVal(i, &oGS)->isDict() || !oGS.dictLookup("SMask", &oSMask)->isDict() || !oSMask.dictLookup("G", &oSMaskGroup)->isStream() || !oSMaskGroup.streamGetDict()->lookup("Resources", &oResources)->isDict()) { oGS.free(); oSMask.free(); oSMaskGroup.free(); oResources.free(); continue; } oGS.free(); oSMask.free(); oSMaskGroup.free(); - if (scanFonts(oResources.getDict(), pDoc, arrCMap, nDepth + 1)) + if (scanFonts(oResources.getDict(), arrCMap, nDepth + 1)) { oResources.free(); oExtGState.free(); return true; @@ -187,26 +187,24 @@ bool scanFonts(Dict *pResources, PDFDoc *pDoc, const std::vector& a return false; } -bool scanAPfonts(Object* oAnnot, PDFDoc *pDoc, const std::vector& arrCMap) +bool scanAPfonts(Object* oAnnot, const std::vector& arrCMap) { Object oAP; if (oAnnot->dictLookup("AP", &oAP)->isDict()) { - Object oAPi, oRes; - -#define SCAN_AP_VIEW(sName)\ -{\ -if (oAP.dictLookup(sName, &oAPi)->isStream() && oAPi.streamGetDict()->lookup("Resources", &oRes)->isDict() && scanFonts(oRes.getDict(), pDoc, arrCMap, 0))\ -{\ - oAPi.free(); oAP.free(); oRes.free();\ - return true;\ -}\ -oAPi.free(); oRes.free();\ -} - - SCAN_AP_VIEW("N"); - SCAN_AP_VIEW("D"); - SCAN_AP_VIEW("R"); + auto fScanAPView = [&oAP, &arrCMap](const char* sName) + { + Object oAPi, oRes; + if (oAP.dictLookup(sName, &oAPi)->isStream() && oAPi.streamGetDict()->lookup("Resources", &oRes)->isDict() && scanFonts(oRes.getDict(), arrCMap, 0)) + { + oAPi.free(); oRes.free(); oAP.free(); + return true; + } + oAPi.free(); oRes.free(); + return false; + }; + if (fScanAPView("N") || fScanAPView("D") || fScanAPView("R")) + return true; } oAP.free(); return false; @@ -236,81 +234,80 @@ bool CPdfReader::IsNeedCMap() if (!m_pPDFDocument || !m_pPDFDocument->getCatalog()) return false; - for (int nPage = 0, nLastPage = m_pPDFDocument->getNumPages(); nPage < nLastPage; ++nPage) + for (int nPage = 1, nLastPage = m_pPDFDocument->getNumPages(); nPage <= nLastPage; ++nPage) { - Page* pPage = m_pPDFDocument->getCatalog()->getPage(nPage + 1); + Page* pPage = m_pPDFDocument->getCatalog()->getPage(nPage); Dict* pResources = pPage->getResourceDict(); - if (pResources && scanFonts(pResources, m_pPDFDocument, arrCMap, 0)) + if (pResources && scanFonts(pResources, arrCMap, 0)) return true; Object oAnnots; - if (pPage->getAnnots(&oAnnots)->isArray()) + if (!pPage->getAnnots(&oAnnots)->isArray()) { - for (int i = 0, nNum = oAnnots.arrayGetLength(); i < nNum; ++i) + oAnnots.free(); + continue; + } + for (int i = 0, nNum = oAnnots.arrayGetLength(); i < nNum; ++i) + { + Object oAnnot; + if (!oAnnots.arrayGet(i, &oAnnot)->isDict()) { - Object oAnnot; - if (!oAnnots.arrayGet(i, &oAnnot)->isDict()) - { - oAnnot.free(); - continue; - } - - Object oDR; - if (oAnnot.dictLookup("DR", &oDR)->isDict() && scanFonts(oDR.getDict(), m_pPDFDocument, arrCMap, 0)) - { - oDR.free(); oAnnot.free(); oAnnots.free(); - return true; - } - oDR.free(); - - if (scanAPfonts(&oAnnot, m_pPDFDocument, arrCMap)) - { - oAnnot.free(); oAnnots.free(); - return true; - } - oAnnot.free(); + continue; } + + Object oDR; + if (oAnnot.dictLookup("DR", &oDR)->isDict() && scanFonts(oDR.getDict(), arrCMap, 0)) + { + oDR.free(); oAnnot.free(); oAnnots.free(); + return true; + } + oDR.free(); + + if (scanAPfonts(&oAnnot, arrCMap)) + { + oAnnot.free(); oAnnots.free(); + return true; + } + oAnnot.free(); } oAnnots.free(); } AcroForm* pAcroForms = m_pPDFDocument->getCatalog()->getForm(); - if (pAcroForms) + if (!pAcroForms) + return false; + Object oDR; + Object* oAcroForm = pAcroForms->getAcroFormObj(); + if (oAcroForm->dictLookup("DR", &oDR)->isDict() && scanFonts(oDR.getDict(), arrCMap, 0)) { - Object oDR; - Object* oAcroForm = pAcroForms->getAcroFormObj(); - if (oAcroForm->dictLookup("DR", &oDR)->isDict() && scanFonts(oDR.getDict(), m_pPDFDocument, arrCMap, 0)) + oDR.free(); + return true; + } + oDR.free(); + + for (int i = 0, nNum = pAcroForms->getNumFields(); i < nNum; ++i) + { + AcroFormField* pField = pAcroForms->getField(i); + + if (pField->getResources(&oDR)->isDict() && scanFonts(oDR.getDict(), arrCMap, 0)) { oDR.free(); return true; } oDR.free(); - for (int i = 0, nNum = pAcroForms->getNumFields(); i < nNum; ++i) + Object oWidgetRef, oWidget; + pField->getFieldRef(&oWidgetRef); + oWidgetRef.fetch(m_pPDFDocument->getXRef(), &oWidget); + oWidgetRef.free(); + + if (scanAPfonts(&oWidget, arrCMap)) { - AcroFormField* pField = pAcroForms->getField(i); - - Object oDR; - if (pField->getResources(&oDR)->isDict() && scanFonts(oDR.getDict(), m_pPDFDocument, arrCMap, 0)) - { - oDR.free(); - return true; - } - oDR.free(); - - Object oWidgetRef, oWidget; - pField->getFieldRef(&oWidgetRef); - oWidgetRef.fetch(m_pPDFDocument->getXRef(), &oWidget); - oWidgetRef.free(); - - if (scanAPfonts(&oWidget, m_pPDFDocument, arrCMap)) - { - oWidget.free(); - return true; - } oWidget.free(); + return true; } + oWidget.free(); } return false; @@ -454,24 +451,18 @@ void CPdfReader::GetPageInfo(int _nPageIndex, double* pdWidth, double* pdHeight, } int CPdfReader::GetRotate(int _nPageIndex) { - int nPageIndex = _nPageIndex + 1; - if (!m_pPDFDocument) return 0; - - return m_pPDFDocument->getPageRotate(nPageIndex); + return m_pPDFDocument->getPageRotate(_nPageIndex + 1); } int CPdfReader::GetMaxRefID() { if (!m_pPDFDocument) return 0; - XRef* xref = m_pPDFDocument->getXRef(); - return xref->getNumObjects(); + return m_pPDFDocument->getXRef()->getNumObjects(); } void CPdfReader::DrawPageOnRenderer(IRenderer* pRenderer, int _nPageIndex, bool* pbBreak) { - int nPageIndex = _nPageIndex + 1; - if (m_pPDFDocument && pRenderer) { PdfReader::RendererOutputDev oRendererOut(pRenderer, m_pFontManager, m_pFontList); @@ -481,7 +472,7 @@ void CPdfReader::DrawPageOnRenderer(IRenderer* pRenderer, int _nPageIndex, bool* #if 0 //#ifdef BUILDING_WASM_MODULE nRotate = -m_pPDFDocument->getPageRotate(nPageIndex); #endif - m_pPDFDocument->displayPage(&oRendererOut, nPageIndex, 72.0, 72.0, nRotate, gFalse, gTrue, gFalse); + m_pPDFDocument->displayPage(&oRendererOut, _nPageIndex + 1, 72.0, 72.0, nRotate, gFalse, gTrue, gFalse); } } void CPdfReader::SetTempDirectory(const std::wstring& wsTempFolder) @@ -509,7 +500,7 @@ void CPdfReader::SetTempDirectory(const std::wstring& wsTempFolder) m_wsTempFolder = L""; if (globalParams) - ((GlobalParamsAdaptor*)globalParams)->SetTempFolder(m_wsTempFolder.c_str()); + ((GlobalParamsAdaptor*)globalParams)->SetTempFolder(m_wsTempFolder); } std::wstring CPdfReader::GetTempDirectory() { @@ -554,69 +545,80 @@ std::wstring CPdfReader::GetInfo() std::wstring sRes = L"{"; - Object info, obj1; - m_pPDFDocument->getDocInfo(&info); - if (info.isDict()) + Object oInfo; + m_pPDFDocument->getDocInfo(&oInfo); + if (m_pPDFDocument->getDocInfo(&oInfo)->isDict()) { -#define DICT_LOOKUP(sName, wsName) \ -if (info.dictLookup(sName, &obj1)->isString())\ -{\ - TextString* s = new TextString(obj1.getString());\ - std::wstring sValue = NSStringExt::CConverter::GetUnicodeFromUTF32(s->getUnicode(), s->getLength());\ - delete s;\ - EscapingCharacter(sValue);\ - if (!sValue.empty())\ - {\ - sRes += L"\"";\ - sRes += wsName;\ - sRes += L"\":\"";\ - sRes += sValue;\ - sRes += L"\",";\ - }\ -}\ -obj1.free(); - DICT_LOOKUP("Title", L"Title"); - DICT_LOOKUP("Author", L"Author"); - DICT_LOOKUP("Subject", L"Subject"); - DICT_LOOKUP("Keywords", L"Keywords"); - DICT_LOOKUP("Creator", L"Creator"); - DICT_LOOKUP("Producer", L"Producer"); -#define DICT_LOOKUP_DATE(sName, wsName) \ -if (info.dictLookup(sName, &obj1)->isString())\ -{\ - char* str = obj1.getString()->getCString();\ - if (str)\ - {\ - TextString* s = new TextString(obj1.getString());\ - std::wstring sNoDate = NSStringExt::CConverter::GetUnicodeFromUTF32(s->getUnicode(), s->getLength());\ - if (sNoDate.length() > 16)\ - {\ - std::wstring sDate = sNoDate.substr(2, 4) + L'-' + sNoDate.substr(6, 2) + L'-' + sNoDate.substr(8, 2) + L'T' +\ - sNoDate.substr(10, 2) + L':' + sNoDate.substr(12, 2) + L':' + sNoDate.substr(14, 2);\ - if (sNoDate.length() > 21 && (sNoDate[16] == L'+' || sNoDate[16] == L'-'))\ - sDate += (L".000" + sNoDate.substr(16, 3) + L':' + sNoDate.substr(20, 2));\ - else\ - sDate += L"Z";\ - NSStringExt::Replace(sDate, L"\"", L"\\\"");\ - sRes += L"\"";\ - sRes += wsName;\ - sRes += L"\":\"";\ - sRes += sDate;\ - sRes += L"\",";\ - }\ - delete s;\ - }\ -}\ -obj1.free(); - DICT_LOOKUP_DATE("CreationDate", L"CreationDate"); - DICT_LOOKUP_DATE("ModDate", L"ModDate"); + auto fDictLookup = [&oInfo](const char* sName, const wchar_t* wsName) + { + std::wstring sRes; + Object obj1; + if (oInfo.dictLookup(sName, &obj1)->isString()) + { + TextString* s = new TextString(obj1.getString()); + std::wstring sValue = NSStringExt::CConverter::GetUnicodeFromUTF32(s->getUnicode(), s->getLength()); + delete s; + EscapingCharacter(sValue); + if (!sValue.empty()) + { + sRes += L"\""; + sRes += wsName; + sRes += L"\":\""; + sRes += sValue; + sRes += L"\","; + } + } + obj1.free(); + return sRes; + }; + sRes += fDictLookup("Title", L"Title"); + sRes += fDictLookup("Author", L"Author"); + sRes += fDictLookup("Subject", L"Subject"); + sRes += fDictLookup("Keywords", L"Keywords"); + sRes += fDictLookup("Creator", L"Creator"); + sRes += fDictLookup("Producer", L"Producer"); + + auto fDictLookupDate = [&oInfo](const char* sName, const wchar_t* wsName) + { + std::wstring sRes; + Object obj1; + if (!oInfo.dictLookup(sName, &obj1)->isString() || !obj1.getString()->getLength()) + { + obj1.free(); + return sRes; + } + + TextString* s = new TextString(obj1.getString()); + std::wstring sNoDate = NSStringExt::CConverter::GetUnicodeFromUTF32(s->getUnicode(), s->getLength()); + if (sNoDate.length() > 16) + { + std::wstring sDate = sNoDate.substr(2, 4) + L'-' + sNoDate.substr(6, 2) + L'-' + sNoDate.substr(8, 2) + L'T' + + sNoDate.substr(10, 2) + L':' + sNoDate.substr(12, 2) + L':' + sNoDate.substr(14, 2); + if (sNoDate.length() > 21 && (sNoDate[16] == L'+' || sNoDate[16] == L'-')) + sDate += (L".000" + sNoDate.substr(16, 3) + L':' + sNoDate.substr(20, 2)); + else + sDate += L"Z"; + EscapingCharacter(sDate); + sRes += L"\""; + sRes += wsName; + sRes += L"\":\""; + sRes += sDate; + sRes += L"\","; + } + delete s; + + obj1.free(); + return sRes; + }; + sRes += fDictLookupDate("CreationDate", L"CreationDate"); + sRes += fDictLookupDate("ModDate", L"ModDate"); } - info.free(); + oInfo.free(); std::wstring version = std::to_wstring(m_pPDFDocument->getPDFVersion()); std::wstring::size_type posDot = version.find('.'); if (posDot != std::wstring::npos) - version = version.substr(0, posDot + 2); + version.resize(posDot + 2); if (!version.empty()) sRes += (L"\"Version\":" + version + L","); @@ -632,7 +634,7 @@ obj1.free(); sRes += std::to_wstring(m_pPDFDocument->getNumPages()); sRes += L",\"FastWebView\":"; - Object obj2, obj3, obj4, obj5, obj6; + Object obj1, obj2, obj3, obj4, obj5, obj6; bool bLinearized = false; obj1.initNull(); Parser* parser = new Parser(xref, new Lexer(xref, str->makeSubStream(str->getStart(), gFalse, 0, &obj1)), gTrue); @@ -663,14 +665,14 @@ obj1.free(); bool bTagged = false; Object catDict, markInfoObj; - if (xref->getCatalog(&catDict) && catDict.isDict() && catDict.dictLookup("MarkInfo", &markInfoObj) && markInfoObj.isDict()) + if (xref->getCatalog(&catDict)->isDict() && catDict.dictLookup("MarkInfo", &markInfoObj)->isDict()) { Object marked, suspects; - if (markInfoObj.dictLookup("Marked", &marked) && marked.isBool() && marked.getBool() == gTrue) + if (markInfoObj.dictLookup("Marked", &marked)->isBool() && marked.getBool() == gTrue) { bTagged = true; // If Suspects is true, the document may not completely conform to Tagged PDF conventions. - if (markInfoObj.dictLookup("Suspects", &suspects) && suspects.isBool() && suspects.getBool() == gTrue) + if (markInfoObj.dictLookup("Suspects", &suspects)->isBool() && suspects.getBool() == gTrue) bTagged = false; } marked.free(); @@ -692,10 +694,6 @@ std::wstring CPdfReader::GetFontPath(const std::wstring& wsFontName) } void getBookmars(PDFDoc* pdfDoc, OutlineItem* pOutlineItem, NSWasm::CData& out, int level) { - int nLengthTitle = pOutlineItem->getTitleLength(); - Unicode* pTitle = pOutlineItem->getTitle(); - std::string sTitle = NSStringExt::CConverter::GetUtf8FromUTF32(pTitle, nLengthTitle); - LinkAction* pLinkAction = pOutlineItem->getAction(); if (!pLinkAction) return; @@ -725,6 +723,8 @@ void getBookmars(PDFDoc* pdfDoc, OutlineItem* pOutlineItem, NSWasm::CData& out, if (str) RELEASEOBJECT(pLinkDest); + std::string sTitle = NSStringExt::CConverter::GetUtf8FromUTF32(pOutlineItem->getTitle(), pOutlineItem->getTitleLength()); + out.AddInt(pg - 1); out.AddInt(level); out.AddDouble(dy); @@ -733,13 +733,15 @@ void getBookmars(PDFDoc* pdfDoc, OutlineItem* pOutlineItem, NSWasm::CData& out, pOutlineItem->open(); GList* pList = pOutlineItem->getKids(); if (!pList) + { + pOutlineItem->close(); return; + } for (int i = 0, num = pList->getLength(); i < num; i++) { OutlineItem* pOutlineItemKid = (OutlineItem*)pList->get(i); - if (!pOutlineItemKid) - continue; - getBookmars(pdfDoc, pOutlineItemKid, out, level + 1); + if (pOutlineItemKid) + getBookmars(pdfDoc, pOutlineItemKid, out, level + 1); } pOutlineItem->close(); } @@ -756,8 +758,7 @@ BYTE* CPdfReader::GetStructure() NSWasm::CData oRes; oRes.SkipLen(); - int num = pList->getLength(); - for (int i = 0; i < num; i++) + for (int i = 0, num = pList->getLength(); i < num; i++) { OutlineItem* pOutlineItem = (OutlineItem*)pList->get(i); if (pOutlineItem) @@ -775,7 +776,6 @@ BYTE* CPdfReader::GetLinks(int nPageIndex) return NULL; nPageIndex++; - Page* pPage = m_pPDFDocument->getCatalog()->getPage(nPageIndex); if (!pPage) return NULL; @@ -787,9 +787,7 @@ BYTE* CPdfReader::GetLinks(int nPageIndex) if (pLinks) { PDFRectangle* cropBox = pPage->getCropBox(); - - int num = pLinks->getNumLinks(); - for (int i = 0; i < num; i++) + for (int i = 0, num = pLinks->getNumLinks(); i < num; i++) { Link* pLink = pLinks->getLink(i); if (!pLink) @@ -858,7 +856,6 @@ BYTE* CPdfReader::GetLinks(int nPageIndex) RELEASEOBJECT(str); } } - RELEASEOBJECT(pLinks); int nRotate = 0; @@ -903,10 +900,7 @@ BYTE* CPdfReader::GetWidgets() { if (!m_pPDFDocument || !m_pPDFDocument->getCatalog()) return NULL; - - AcroForm* pAcroForms = m_pPDFDocument->getCatalog()->getForm(); - XRef* xref = m_pPDFDocument->getXRef(); - if (!pAcroForms || !xref) + if (!m_pPDFDocument->getCatalog()->getForm() || !m_pPDFDocument->getXRef()) return NULL; NSWasm::CData oRes; @@ -928,8 +922,7 @@ BYTE* CPdfReader::GetWidgetFonts(int nTypeFonts) return NULL; AcroForm* pAcroForms = m_pPDFDocument->getCatalog()->getForm(); - XRef* xref = m_pPDFDocument->getXRef(); - if (!pAcroForms || !xref) + if (!pAcroForms || !m_pPDFDocument->getXRef()) return NULL; NSWasm::CData oRes; @@ -940,9 +933,9 @@ BYTE* CPdfReader::GetWidgetFonts(int nTypeFonts) oRes.AddInt(nFontsID); std::vector arrFontsRef; - for (int i = 0, nNum = pAcroForms->getNumFields(); i < nNum; ++i) + for (int nField = 0, nNum = pAcroForms->getNumFields(); nField < nNum; ++nField) { - AcroFormField* pField = pAcroForms->getField(i); + AcroFormField* pField = pAcroForms->getField(nField); if (!pField) continue; @@ -1017,9 +1010,11 @@ BYTE* CPdfReader::GetWidgetFonts(int nTypeFonts) std::string sFontName; std::wstring wsFileName; - bool bBold = false, bItalic = false; if (bFindResources) + { + bool bBold = false, bItalic = false; wsFileName = PdfReader::GetFontData(m_pPDFDocument, m_pFontManager, m_pFontList, &oFonts, &oFontRef, nTypeFonts, sFontName, sFontName, bBold, bItalic); + } if (!sFontName.empty()) { @@ -1125,14 +1120,9 @@ BYTE* CPdfReader::GetAPWidget(int nRasterW, int nRasterH, int nBackgroundColor, return NULL; AcroForm* pAcroForms = m_pPDFDocument->getCatalog()->getForm(); - Page* pPage = m_pPDFDocument->getCatalog()->getPage(nPageIndex + 1); - if (!pAcroForms || !pPage) + if (!pAcroForms) return NULL; - double dPageDpiX, dPageDpiY; - double dWidth, dHeight; - GetPageInfo(nPageIndex, &dWidth, &dHeight, &dPageDpiX, &dPageDpiY); - NSWasm::CData oRes; oRes.SkipLen(); @@ -1164,14 +1154,9 @@ BYTE* CPdfReader::GetButtonIcon(int nRasterW, int nRasterH, int nBackgroundColor return NULL; AcroForm* pAcroForms = m_pPDFDocument->getCatalog()->getForm(); - Page* pPage = m_pPDFDocument->getCatalog()->getPage(nPageIndex + 1); - if (!pAcroForms || !pPage) + if (!pAcroForms) return NULL; - double dPageDpiX, dPageDpiY; - double dWidth, dHeight; - GetPageInfo(nPageIndex, &dWidth, &dHeight, &dPageDpiX, &dPageDpiY); - NSWasm::CData oRes; oRes.SkipLen(); @@ -1182,9 +1167,6 @@ BYTE* CPdfReader::GetButtonIcon(int nRasterW, int nRasterH, int nBackgroundColor if (pField->getPageNum() != nPageIndex + 1 || pField->getAcroFormFieldType() != acroFormFieldPushbutton || (nButtonWidget >= 0 && i != nButtonWidget)) continue; - bool bFirst = true; - int nMKPos = -1; - unsigned int nMKLength = 0; Object oMK; if (!pField->fieldLookup("MK", &oMK)->isDict()) { @@ -1192,6 +1174,9 @@ BYTE* CPdfReader::GetButtonIcon(int nRasterW, int nRasterH, int nBackgroundColor continue; } + bool bFirst = true; + int nMKPos = -1; + unsigned int nMKLength = 0; std::vector arrMKName { "I", "RI", "IX" }; for (unsigned int j = 0; j < arrMKName.size(); ++j) { @@ -1219,24 +1204,16 @@ BYTE* CPdfReader::GetButtonIcon(int nRasterW, int nRasterH, int nBackgroundColor bFirst = false; } - oRes.WriteString((BYTE*)sMKName.c_str(), (unsigned int)sMKName.size()); - - // Resources - Dict* oStreamDict = oStr.streamGetDict(); - Object oResources; - oStreamDict->lookup("Resources", &oResources); - Dict* oResourcesDict = oResources.isDict() ? oResources.getDict() : (Dict *)NULL; - oStr.free(); + oRes.WriteString(sMKName); // Получение единственного XObject из Resources, если возможно - Object oXObject, oIm; - if (!oResourcesDict || !oResourcesDict->lookup("XObject", &oXObject)->isDict() || oXObject.dictGetLength() != 1 || !oXObject.dictGetVal(0, &oIm)->isStream()) + Object oResources, oXObject, oIm; + if (!oStr.streamGetDict()->lookup("Resources", &oResources)->isDict() || !oResources.dictLookup("XObject", &oXObject)->isDict() || oXObject.dictGetLength() != 1 || !oXObject.dictGetVal(0, &oIm)->isStream()) { - oXObject.free(); oIm.free(); - oResources.free(); + oStr.free(); oResources.free(); oXObject.free(); oIm.free(); continue; } - oResources.free(); + oStr.free(); oResources.free(); Dict *oImDict = oIm.streamGetDict(); Object oType, oSubtype; @@ -1322,6 +1299,7 @@ BYTE* CPdfReader::GetButtonIcon(int nRasterW, int nRasterH, int nBackgroundColor continue; } + // else BYTE* pBgraData = new BYTE[nWidth * nHeight * 4]; unsigned int nColor = (unsigned int)nBackgroundColor; unsigned int nSize = (unsigned int)(nWidth * nHeight); @@ -1462,21 +1440,20 @@ void GetPageAnnots(PDFDoc* pdfDoc, NSWasm::CData& oRes, int nPageIndex) for (int i = 0, nNum = oAnnots.arrayGetLength(); i < nNum; ++i) { - // Ходовые объекты - Object oObj, oObj2; - - Object oAnnot, oAnnotRef; + Object oAnnot; if (!oAnnots.arrayGet(i, &oAnnot)->isDict()) { oAnnot.free(); continue; } + Object oSubtype; std::string sType; - if (oAnnot.dictLookup("Subtype", &oObj)->isName()) - sType = oObj.getName(); - oObj.free(); oAnnot.free(); + if (oAnnot.dictLookup("Subtype", &oSubtype)->isName()) + sType = oSubtype.getName(); + oSubtype.free(); oAnnot.free(); + Object oAnnotRef; PdfReader::CAnnot* pAnnot = NULL; oAnnots.arrayGetNF(i, &oAnnotRef); if (sType == "Text") @@ -1551,12 +1528,8 @@ BYTE* CPdfReader::GetAnnots(int nPageIndex) if (nPageIndex >= 0) GetPageAnnots(m_pPDFDocument, oRes, nPageIndex); else - { for (int nPage = 0, nLastPage = m_pPDFDocument->getNumPages(); nPage < nLastPage; ++nPage) - { GetPageAnnots(m_pPDFDocument, oRes, nPage); - } - } oRes.WriteLen(); BYTE* bRes = oRes.GetBuffer(); @@ -1582,13 +1555,7 @@ BYTE* CPdfReader::GetAPAnnots(int nRasterW, int nRasterH, int nBackgroundColor, for (int i = 0, nNum = oAnnots.arrayGetLength(); i < nNum; ++i) { Object oAnnotRef; - if (!oAnnots.arrayGetNF(i, &oAnnotRef)->isRef()) - { - oAnnotRef.free(); - continue; - } - - if (nAnnot >= 0 && oAnnotRef.getRefNum() != nAnnot) + if (!oAnnots.arrayGetNF(i, &oAnnotRef)->isRef() || (nAnnot >= 0 && oAnnotRef.getRefNum() != nAnnot)) { oAnnotRef.free(); continue; diff --git a/PdfFile/PdfReader.h b/PdfFile/PdfReader.h index 488eeafbef..29ad9dfb54 100644 --- a/PdfFile/PdfReader.h +++ b/PdfFile/PdfReader.h @@ -66,10 +66,10 @@ public: std::wstring GetInfo(); std::wstring GetFontPath(const std::wstring& wsFontName); - NSFonts::IFontManager* GetFontManager() { return m_pFontManager; } std::wstring ToXml(const std::wstring& wsXmlPath, bool isPrintStreams = false); - PDFDoc* GetPDFDocument() { return m_pPDFDocument; } void ChangeLength(DWORD nLength); + NSFonts::IFontManager* GetFontManager() { return m_pFontManager; } + PDFDoc* GetPDFDocument() { return m_pPDFDocument; } BYTE* GetStructure(); BYTE* GetLinks(int nPageIndex); @@ -82,13 +82,12 @@ public: BYTE* GetAPAnnots (int nRasterW, int nRasterH, int nBackgroundColor, int nPageIndex, int nAnnot = -1, const char* sView = NULL); private: - IOfficeDrawingFile* m_pRenderer; - PDFDoc* m_pPDFDocument; - std::wstring m_wsTempFolder; + PDFDoc* m_pPDFDocument; + std::wstring m_wsTempFolder; NSFonts::IFontManager* m_pFontManager; PdfReader::CFontList* m_pFontList; - DWORD m_nFileLength; - int m_eError; + DWORD m_nFileLength; + int m_eError; std::map m_mFonts; };