diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index b57841ab42..9d025271e3 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -713,7 +713,8 @@ int CPdfFile::GetPagesCount() { if (!m_pInternal->pReader) return 0; - return m_pInternal->pReader->GetPagesCount(); + PDFDoc* pPdfDoc = m_pInternal->pReader->GetPDFDocument(); + return pPdfDoc ? pPdfDoc->getNumPages() : 0; } void CPdfFile::GetPageInfo(int nPageIndex, double* pdWidth, double* pdHeight, double* pdDpiX, double* pdDpiY) { diff --git a/PdfFile/Src/PdfReader.cpp b/PdfFile/Src/PdfReader.cpp index a09526b718..3984c172b5 100644 --- a/PdfFile/Src/PdfReader.cpp +++ b/PdfFile/Src/PdfReader.cpp @@ -59,8 +59,6 @@ CPdfReader::CPdfReader(NSFonts::IApplicationFonts* pAppFonts) { m_wsTempFolder = L""; - m_wsSrcPath = L""; - m_pPDFDocument = NULL; m_nFileLength = 0; @@ -116,20 +114,16 @@ bool CPdfReader::LoadFromFile(NSFonts::IApplicationFonts* pAppFonts, const std:: pMeasurerCache->SetCacheSize(1); ((GlobalParamsAdaptor*)globalParams)->SetFontManager(m_pFontManager); - if (m_pPDFDocument) - delete m_pPDFDocument; + RELEASEOBJECT(m_pPDFDocument); - if (GetTempDirectory() == L"") - { + if (m_wsTempFolder == L"") SetTempDirectory(NSDirectory::GetTempPath()); - } m_eError = errNone; GString* owner_pswd = NSStrings::CreateString(wsOwnerPassword); - GString* user_pswd = NSStrings::CreateString(wsUserPassword); + GString* user_pswd = NSStrings::CreateString(wsUserPassword); // конвертим путь в utf8 - под виндой они сконвертят в юникод, а на остальных - так и надо - m_wsSrcPath = wsSrcPath; std::string sPathUtf8 = U_TO_UTF8(wsSrcPath); m_pPDFDocument = new PDFDoc((char*)sPathUtf8.c_str(), owner_pswd, user_pswd); @@ -143,10 +137,7 @@ bool CPdfReader::LoadFromFile(NSFonts::IApplicationFonts* pAppFonts, const std:: oFile.CloseFile(); } - if (m_pPDFDocument) - m_eError = m_pPDFDocument->getErrorCode(); - else - m_eError = errMemory; + m_eError = m_pPDFDocument ? m_pPDFDocument->getErrorCode() : errMemory; if (!m_pPDFDocument || !m_pPDFDocument->isOk()) { @@ -177,7 +168,7 @@ bool CPdfReader::LoadFromMemory(NSFonts::IApplicationFonts* pAppFonts, BYTE* dat obj.initNull(); // будет освобожден в деструкторе PDFDoc BaseStream *str = new MemStream((char*)data, 0, length, &obj); - m_pPDFDocument = new PDFDoc(str, owner_pswd, user_pswd); + m_pPDFDocument = new PDFDoc(str, owner_pswd, user_pswd); m_nFileLength = length; delete owner_pswd; @@ -210,20 +201,6 @@ int CPdfReader::GetError() return m_pPDFDocument->getErrorCode(); } -int CPdfReader::GetPagesCount() -{ - if (!m_pPDFDocument) - return 0; - - return m_pPDFDocument->getNumPages(); -} -double CPdfReader::GetVersion() -{ - if (!m_pPDFDocument) - return 0; - - return m_pPDFDocument->getPDFVersion(); -} void CPdfReader::GetPageInfo(int _nPageIndex, double* pdWidth, double* pdHeight, double* pdDpiX, double* pdDpiY) { int nPageIndex = _nPageIndex + 1; @@ -232,26 +209,19 @@ void CPdfReader::GetPageInfo(int _nPageIndex, double* pdWidth, double* pdHeight, return; int nRotate = m_pPDFDocument->getPageRotate(nPageIndex); - - while (nRotate >= 360) - nRotate -= 360; - - while (nRotate < 0) - nRotate += 360; - - if (0 != nRotate && 180 != nRotate) - { - *pdHeight = m_pPDFDocument->getPageCropWidth(nPageIndex); - *pdWidth = m_pPDFDocument->getPageCropHeight(nPageIndex); - } - else + if (nRotate % 180 == 0) { *pdWidth = m_pPDFDocument->getPageCropWidth(nPageIndex); *pdHeight = m_pPDFDocument->getPageCropHeight(nPageIndex); } + else + { + *pdHeight = m_pPDFDocument->getPageCropWidth(nPageIndex); + *pdWidth = m_pPDFDocument->getPageCropHeight(nPageIndex); + } - *pdDpiX = 72; - *pdDpiY = 72; + *pdDpiX = 72.0; + *pdDpiY = 72.0; } void CPdfReader::DrawPageOnRenderer(IRenderer* pRenderer, int _nPageIndex, bool* pbBreak) { @@ -262,7 +232,7 @@ void CPdfReader::DrawPageOnRenderer(IRenderer* pRenderer, int _nPageIndex, bool* PdfReader::RendererOutputDev oRendererOut(pRenderer, m_pFontManager, m_pFontList); oRendererOut.NewPDF(m_pPDFDocument->getXRef()); oRendererOut.SetBreak(pbBreak); - m_pPDFDocument->displayPage(&oRendererOut, nPageIndex, 72.0, 72.0, 0, false, true, false); + m_pPDFDocument->displayPage(&oRendererOut, nPageIndex, 72.0, 72.0, 0, gFalse, gTrue, gFalse); } } void CPdfReader::SetTempDirectory(const std::wstring& wsTempFolder) @@ -275,7 +245,7 @@ void CPdfReader::SetTempDirectory(const std::wstring& wsTempFolder) if (!wsTempFolder.empty()) { - std::wstring wsFolderName = std::wstring(wsTempFolder) + L"//pdftemp"; + std::wstring wsFolderName = wsTempFolder + L"/pdftemp"; std::wstring wsFolder = wsFolderName; int nCounter = 0; while (NSDirectory::Exists(wsFolder)) @@ -311,7 +281,7 @@ if (info.dictLookup(sName, &obj1)->isString())\ sRes += L"\":\"";\ std::wstring sValue = NSStringExt::CConverter::GetUnicodeFromUTF32(s->getUnicode(), s->getLength());\ NSStringExt::Replace(sValue, L"\"", L"\\\"");\ - sRes += sValue;\ + sRes += sValue.empty() ? L" " : sValue;\ sRes += L"\",";\ delete s;\ }\ @@ -372,23 +342,23 @@ std::wstring CPdfReader::GetInfo() info.free(); obj1.free(); - std::wstring version = std::to_wstring(GetVersion()); + 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); + if (!version.empty()) + sRes += (L"\"Version\":" + version + L","); - sRes += L"\"Version\":"; - sRes += version; double nW = 0; double nH = 0; double nDpi = 0; GetPageInfo(0, &nW, &nH, &nDpi, &nDpi); - sRes += L",\"PageWidth\":"; + sRes += L"\"PageWidth\":"; sRes += std::to_wstring((int)(nW * 100)); sRes += L",\"PageHeight\":"; sRes += std::to_wstring((int)(nH * 100)); sRes += L",\"NumberOfPages\":"; - sRes += std::to_wstring(GetPagesCount()); + sRes += std::to_wstring(m_pPDFDocument->getNumPages()); sRes += L",\"FastWebView\":"; Object obj2, obj3, obj4, obj5, obj6; @@ -425,11 +395,11 @@ std::wstring CPdfReader::GetInfo() if (xref->getCatalog(&catDict) && catDict.isDict() && catDict.dictLookup("MarkInfo", &markInfoObj) && markInfoObj.isDict()) { Object marked, suspects; - if (markInfoObj.dictLookup("Marked", &marked) && marked.isBool() && marked.getBool()) + if (markInfoObj.dictLookup("Marked", &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()) + if (markInfoObj.dictLookup("Suspects", &suspects) && suspects.isBool() && suspects.getBool() == gTrue) bTagged = false; } marked.free(); @@ -438,8 +408,7 @@ std::wstring CPdfReader::GetInfo() markInfoObj.free(); catDict.free(); - sRes += bTagged ? L"true" : L"false"; - sRes += L"}"; + sRes += bTagged ? L"true}" : L"false}"; return sRes; } @@ -488,8 +457,7 @@ void getBookmars(PDFDoc* pdfDoc, OutlineItem* pOutlineItem, NSWasm::CData& out, GList* pList = pOutlineItem->getKids(); if (!pList) return; - int num = pList->getLength(); - for (int i = 0; i < num; i++) + for (int i = 0, num = pList->getLength(); i < num; i++) { OutlineItem* pOutlineItemKid = (OutlineItem*)pList->get(i); if (!pOutlineItemKid) @@ -588,7 +556,7 @@ BYTE* CPdfReader::GetLinks(int nPageIndex) TextOutputControl textOutControl; textOutControl.mode = textOutReadingOrder; TextOutputDev* pTextOut = new TextOutputDev(NULL, &textOutControl, gFalse); - m_pPDFDocument->displayPage(pTextOut, nPageIndex, 72, 72, 0, gFalse, gTrue, gFalse); + m_pPDFDocument->displayPage(pTextOut, nPageIndex, 72.0, 72.0, 0, gFalse, gTrue, gFalse); m_pPDFDocument->processLinks(pTextOut, nPageIndex); TextWordList* pWordList = pTextOut->makeWordList(); for (int i = 0; i < pWordList->getLength(); i++) diff --git a/PdfFile/Src/PdfReader.h b/PdfFile/Src/PdfReader.h index 21f0cb021f..7df24f9f92 100644 --- a/PdfFile/Src/PdfReader.h +++ b/PdfFile/Src/PdfReader.h @@ -52,13 +52,11 @@ public: std::wstring GetTempDirectory(); void SetTempDirectory(const std::wstring& directory); - int GetPagesCount(); void GetPageInfo(int nPageIndex, double* pdWidth, double* pdHeight, double* pdDpiX, double* pdDpiY); void DrawPageOnRenderer(IRenderer* pRenderer, int nPageIndex, bool* pBreak); std::wstring GetInfo(); int GetError(); - double GetVersion(); NSFonts::IFontManager* GetFontManager() { return m_pFontManager; } PDFDoc* GetPDFDocument() { return m_pPDFDocument; } @@ -70,9 +68,8 @@ public: private: PDFDoc* m_pPDFDocument; std::wstring m_wsTempFolder; - std::wstring m_wsSrcPath; NSFonts::IFontManager* m_pFontManager; - PdfReader::CFontList* m_pFontList; + PdfReader::CFontList* m_pFontList; DWORD m_nFileLength; int m_eError; }; diff --git a/PdfFile/Src/PdfWriter.cpp b/PdfFile/Src/PdfWriter.cpp index 0fde56b7f8..a321ea4da7 100644 --- a/PdfFile/Src/PdfWriter.cpp +++ b/PdfFile/Src/PdfWriter.cpp @@ -453,6 +453,7 @@ CPdfWriter::CPdfWriter(NSFonts::IApplicationFonts* pAppFonts, bool isPDFA) : m_o m_pPage = NULL; m_pFont = NULL; + m_unFieldsCounter = 0; m_bNeedUpdateTextFont = true; m_bNeedUpdateTextColor = true; m_bNeedUpdateTextAlpha = true; @@ -1891,7 +1892,7 @@ HRESULT CPdfWriter::AddFormField(NSFonts::IApplicationFonts* pAppFonts, const CF if (L"" != wsKey) pFieldBase->SetFieldName(wsKey); else - pFieldBase->SetFieldName(m_oFieldsManager.GetNewFieldName()); + pFieldBase->SetFieldName("F" + std::to_string(++m_unFieldsCounter)); } return S_OK; diff --git a/PdfFile/Src/PdfWriter.h b/PdfFile/Src/PdfWriter.h index 80006c9f3c..f328bd48ba 100644 --- a/PdfFile/Src/PdfWriter.h +++ b/PdfFile/Src/PdfWriter.h @@ -1033,25 +1033,25 @@ private: { public: - CFontState() : m_wsPath(L""), m_wsName(L"Arial"), m_lStyle(0), m_bBold(false), m_bItalic(false), m_dCharSpace(0), - m_lFaceIndex(0), m_dSize(10), m_bGid(false), m_bNeedDoBold(false), m_bNeedDoItalic(false) + CFontState() : m_wsName(L"Arial"), m_wsPath(L""), m_dSize(10), m_bGid(false), m_lFaceIndex(0), m_lStyle(0), + m_bBold(false), m_bItalic(false), m_dCharSpace(0), m_bNeedDoItalic(false), m_bNeedDoBold(false) { } void Reset() { - m_wsPath = L""; m_wsName = L"Arial"; + m_wsPath = L""; + m_dSize = 10; + m_bGid = false; + m_lFaceIndex = 0; m_lStyle = 0; m_bBold = false; m_bItalic = false; m_dCharSpace = 0; - m_lFaceIndex = 0; - m_dSize = 10; - m_bGid = false; + m_bNeedDoItalic = false; m_bNeedDoBold = false; - m_bNeedDoItalic = false; } inline std::wstring GetName() @@ -1435,16 +1435,13 @@ private: return false; } - public: std::vector m_vCommands; bool m_bIsMoveTo; }; - class CTransform + struct CTransform { - public: - CTransform() { Reset(); @@ -1489,8 +1486,6 @@ private: dy = dY; } - public: - double m11; double m12; double m21; @@ -1515,14 +1510,6 @@ private: std::vector m_vCommands; CTransform m_oTransform; }; - struct TImageInfo - { - std::wstring wsPath; - BYTE nAlpha; - int nWidth; - int nHeight; - PdfWriter::CImageDict* pImage; - }; struct TDestinationInfo { TDestinationInfo(PdfWriter::CPage* page, const double& x, const double& y, const double& w, const double& h, const double& dx, const double& dy, const unsigned int& undpage) @@ -1546,31 +1533,6 @@ private: double dDestY; unsigned int unDestPage; }; - class CFieldsManager - { - public: - - CFieldsManager() - { - m_unCounter = 0; - } - - std::string GetNewFieldName() - { - std::string sName("F"); - sName.append(std::to_string(++m_unCounter)); - return sName; - } - - private: - - unsigned int m_unCounter; - }; - //---------------------------------------------------------------------------------------- - // - // CMultiLineTextManager - // - //---------------------------------------------------------------------------------------- class CMultiLineTextManager { public: @@ -1775,9 +1737,7 @@ private: return dWidth; } - - private: - + private: inline bool IsSpace(const unsigned int& unPos) const { return (m_pCodes[unPos] == m_ushSpaceCode); @@ -1788,9 +1748,7 @@ private: return (GetLinesCount() * (m_unLineHeight * dKoef) < (dH - (m_nDescent * dKoef))); } - - private: - + private: unsigned short* m_pCodes; unsigned int* m_pWidths; unsigned int m_unLen; @@ -1829,7 +1787,7 @@ private: LONG m_lClipDepth; std::vector m_vFonts; std::vectorm_vDestinations; - CFieldsManager m_oFieldsManager; + unsigned int m_unFieldsCounter; CMultiLineTextManager m_oLinesManager; bool m_bValid;