diff --git a/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp b/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp index 76de8ff5f4..dcefa78387 100644 --- a/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp +++ b/DesktopEditor/graphics/pro/js/wasm/src/pdfwriter.cpp @@ -149,6 +149,7 @@ void CPdfWriter::Sign(const double& dX, const double& dY, const double& dW, cons HRESULT CPdfWriter::EditWidgetParents(NSFonts::IApplicationFonts* pAppFonts, CWidgetsInfo* pFieldInfo, const std::wstring& wsTempDirectory) { return 0; } PdfWriter::CDocument* CPdfWriter::GetDocument() { return NULL; } PdfWriter::CPage* CPdfWriter::GetPage() { return NULL; } +void CPdfWriter::AddFont(const std::wstring& wsFontName, const bool& bBold, const bool& bItalic, const std::wstring& wsFontPath, const LONG& lFaceIndex) {} PdfWriter::CImageDict* CPdfWriter::LoadImage(Aggplus::CImage* pImage, BYTE nAlpha) { return NULL; } PdfWriter::CImageDict* CPdfWriter::DrawImage(Aggplus::CImage* pImage, const double& dX, const double& dY, const double& dW, const double& dH, const BYTE& nAlpha) { return NULL; } bool CPdfWriter::DrawText(unsigned char* pCodes, const unsigned int& unLen, const double& dX, const double& dY) { return false; } diff --git a/PdfFile/PdfEditor.cpp b/PdfFile/PdfEditor.cpp index c06269ab1d..94fbfef4d3 100644 --- a/PdfFile/PdfEditor.cpp +++ b/PdfFile/PdfEditor.cpp @@ -886,16 +886,6 @@ bool CPdfEditor::EditPage(int nPageIndex) oAnnot.free(); oSubtype.free(); continue; } - else if (oSubtype.isName("FreeText")) - { - //TODO добавление шрифтов FreeText в общий m_pAppAplication чтобы writer мог использовать шрифты из reader - oAnnot.free(); oSubtype.free(); - oTemp.arrayGetNF(nIndex, &oAnnot); - std::map mapFont = pReader->AnnotFonts(&oAnnot); - m_mFonts.insert(mapFont.begin(), mapFont.end()); - DictToCDictObject(&oAnnot, pArray, false, ""); - oAnnot.free(); - } oAnnot.free(); oSubtype.free(); oTemp.arrayGetNF(nIndex, &oAnnot); DictToCDictObject(&oAnnot, pArray, false, ""); @@ -1027,7 +1017,11 @@ bool CPdfEditor::EditAnnot(int nPageIndex, int nID) else if (oType.isName("Polygon") || oType.isName("PolyLine")) pAnnot = new PdfWriter::CPolygonLineAnnotation(pXref); else if (oType.isName("FreeText")) + { + std::map mapFont = pReader->AnnotFonts(&oAnnotRef); + m_mFonts.insert(mapFont.begin(), mapFont.end()); pAnnot = new PdfWriter::CFreeTextAnnotation(pXref); + } else if (oType.isName("Caret")) pAnnot = new PdfWriter::CCaretAnnotation(pXref); else if (oType.isName("Popup")) diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index 9a9c2c5ea7..1ee0dcf806 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -58,6 +58,7 @@ public: bool IsEditPage() { return false; } void AddShapeXML(const std::string& sXML) {} void EndMarkedContent() {} + bool IsBase14(const std::wstring& wsFontName, bool& bBold, bool& bItalic, std::wstring& wsFontPath) { return false; } }; #endif // BUILDING_WASM_MODULE diff --git a/PdfFile/PdfWriter.cpp b/PdfFile/PdfWriter.cpp index 1aa95b416b..871259953e 100644 --- a/PdfFile/PdfWriter.cpp +++ b/PdfFile/PdfWriter.cpp @@ -40,6 +40,7 @@ #include "SrcWriter/Font.h" #include "SrcWriter/FontCidTT.h" #include "SrcWriter/FontTT.h" +#include "SrcWriter/Font14.h" #include "SrcWriter/Destination.h" #include "SrcWriter/Field.h" @@ -748,8 +749,16 @@ HRESULT CPdfWriter::CommandDrawTextCHAR2(unsigned int* pUnicodes, const unsigned if (m_pFont14) { + bool bNew = false; + m_pFont14->EncodeUnicode(unGid, *pUnicodes, bNew); + if (bNew) + { + TBBoxAdvance oBox = m_pFontManager->MeasureChar2(*pUnicodes); + double dWidth = oBox.fAdvanceX / m_oFont.GetSize() * 1000.0; + m_pFont14->AddWidth(dWidth); + } unsigned char* pCodes = new unsigned char[2]; - pCodes[0] = 0; + pCodes[0] = (*pUnicodes >> 8) & 0xFF; pCodes[1] = *pUnicodes & 0xFF; return DrawText(pCodes, 2, dX, dY) ? S_OK : S_FALSE; } @@ -2916,7 +2925,10 @@ bool CPdfWriter::DrawText(unsigned char* pCodes, const unsigned int& unLen, cons m_oCommandManager.SetTransform(t.m11, -t.m12, -t.m21, t.m22, MM_2_PT(t.dx + t.m21 * m_dPageHeight), MM_2_PT(m_dPageHeight - m_dPageHeight * t.m22 - t.dy)); CRendererTextCommand* pText = m_oCommandManager.AddText(pCodes, unLen, MM_2_PT(dX), MM_2_PT(m_dPageHeight - dY)); - pText->SetFont(m_pFont14 ? m_pFont : m_pFont); + PdfWriter::CFontDict* pFont = m_pFont; + if (m_pFont14) + pFont = m_pFont14; + pText->SetFont(pFont); pText->SetSize(m_oFont.GetSize()); pText->SetColor(m_oBrush.GetColor1()); pText->SetAlpha((BYTE)m_oBrush.GetAlpha1()); @@ -2990,7 +3002,8 @@ bool CPdfWriter::GetBaseFont14(const std::wstring& wsFontName, int nBase14) return false; if (!m_pFontManager->LoadFontFromFile(wsFontPath, lFaceIndex, m_oFont.GetSize(), 72, 72)) return false; - m_pFont14 = m_pDocument->CreateFont14((PdfWriter::EStandard14Fonts)nBase14); + PdfWriter::EStandard14Fonts nType = (PdfWriter::EStandard14Fonts)nBase14; + m_pFont14 = m_pDocument->CreateFont14(wsFontPath, lFaceIndex, nType); return !!m_pFont14; } bool CPdfWriter::UpdateFont() diff --git a/PdfFile/SrcWriter/Document.cpp b/PdfFile/SrcWriter/Document.cpp index 7e7e983873..6909e6d273 100644 --- a/PdfFile/SrcWriter/Document.cpp +++ b/PdfFile/SrcWriter/Document.cpp @@ -785,9 +785,25 @@ namespace PdfWriter return pForm; } - CFont14* CDocument::CreateFont14(EStandard14Fonts eType) + CFont14* CDocument::CreateFont14(const std::wstring& wsFontPath, unsigned int unIndex, EStandard14Fonts eType) { - return new CFont14(m_pXref, this, eType); + CFont14* pFont = FindFont14(wsFontPath, unIndex); + if (pFont) + return pFont; + pFont = new CFont14(m_pXref, this, eType); + m_vFonts14.push_back(TFontInfo(wsFontPath, unIndex, pFont)); + return pFont; + } + CFont14* CDocument::FindFont14(const std::wstring& wsFontPath, unsigned int unIndex) + { + for (int nIndex = 0, nCount = m_vFonts14.size(); nIndex < nCount; nIndex++) + { + TFontInfo& oInfo = m_vFonts14.at(nIndex); + if (wsFontPath == oInfo.wsPath && unIndex == oInfo.unIndex) + return (CFont14*)oInfo.pFont; + } + + return NULL; } CFontCidTrueType* CDocument::CreateCidTrueTypeFont(const std::wstring& wsFontPath, unsigned int unIndex) { diff --git a/PdfFile/SrcWriter/Document.h b/PdfFile/SrcWriter/Document.h index 1949d27d8f..6d12ee0fc7 100644 --- a/PdfFile/SrcWriter/Document.h +++ b/PdfFile/SrcWriter/Document.h @@ -157,7 +157,8 @@ namespace PdfWriter CImageDict* CreateImage(); CXObject* CreateForm(CImageDict* pImage, const std::string& sName); - CFont14* CreateFont14(EStandard14Fonts eType); + CFont14* CreateFont14(const std::wstring& wsFontPath, unsigned int unIndex, EStandard14Fonts eType); + CFont14* FindFont14 (const std::wstring& wsFontPath, unsigned int unIndex); CFontCidTrueType* CreateCidTrueTypeFont(const std::wstring& wsFontPath, unsigned int unIndex); CFontCidTrueType* FindCidTrueTypeFont(const std::wstring& wsFontPath, unsigned int unIndex); CFontTrueType* CreateTrueTypeFont(const std::wstring& wsFontPath, unsigned int unIndex); @@ -291,6 +292,7 @@ namespace PdfWriter std::vector m_vShadings; std::vector m_vCidTTFonts; std::vector m_vTTFonts; + std::vector m_vFonts14; CFont14* m_pDefaultCheckBoxFont; CDictObject* m_pTransparencyGroup; std::vector m_vFreeTypeFonts; diff --git a/PdfFile/SrcWriter/Font.h b/PdfFile/SrcWriter/Font.h index 65abf3bd81..444880ad21 100644 --- a/PdfFile/SrcWriter/Font.h +++ b/PdfFile/SrcWriter/Font.h @@ -51,6 +51,10 @@ namespace PdfWriter { return fontUnknownType; } + virtual unsigned int GetWidth(unsigned short ushCode) + { + return 0; + } protected: diff --git a/PdfFile/SrcWriter/Font14.cpp b/PdfFile/SrcWriter/Font14.cpp index 355d61490a..da847794fb 100644 --- a/PdfFile/SrcWriter/Font14.cpp +++ b/PdfFile/SrcWriter/Font14.cpp @@ -57,5 +57,46 @@ namespace PdfWriter Add("Type", "Font"); Add("Subtype", "Type1"); Add("BaseFont", c_sStandardFontNames[(int)eType]); + m_ushCodesCount = 0; + } + unsigned int CFont14::GetWidth(unsigned short ushCode) + { + std::map::const_iterator oIter = m_mUnicodeToCode.find(ushCode); + if (oIter == m_mUnicodeToCode.end()) + return 0; + ushCode = oIter->second; + if (ushCode >= m_vWidths.size()) + return 0; + + return m_vWidths.at(ushCode); + } + void CFont14::AddWidth(unsigned int nWidth) + { + m_vWidths.push_back(nWidth); + } + unsigned short CFont14::EncodeUnicode(const unsigned int& unGID, const unsigned int& unUnicode, bool& bNew) + { + std::map::const_iterator oIter = m_mUnicodeToCode.find(unUnicode); + if (oIter != m_mUnicodeToCode.end()) + return oIter->second; + + unsigned short ushCode = EncodeGID(unGID, bNew); + m_mUnicodeToCode.insert(std::pair(unUnicode, ushCode)); + return ushCode; + } + unsigned short CFont14::EncodeGID(const unsigned int& unGID, bool& bNew) + { + for (unsigned short ushCurCode = 0, ushCodesCount = m_vCodeToGid.size(); ushCurCode < ushCodesCount; ushCurCode++) + { + if (unGID == m_vCodeToGid.at(ushCurCode)) + return ushCurCode; + } + + unsigned short ushCode = m_ushCodesCount++; + + m_vCodeToGid.push_back(unGID); + bNew = true; + + return ushCode; } } diff --git a/PdfFile/SrcWriter/Font14.h b/PdfFile/SrcWriter/Font14.h index d8c0dbe27c..89c4a0ee03 100644 --- a/PdfFile/SrcWriter/Font14.h +++ b/PdfFile/SrcWriter/Font14.h @@ -46,6 +46,16 @@ namespace PdfWriter { return fontType1; } + unsigned int GetWidth(unsigned short ushCode); + void AddWidth(unsigned int nWidth); + unsigned short EncodeUnicode(const unsigned int& unGID, const unsigned int& unUnicode, bool& bNew); + unsigned short EncodeGID(const unsigned int& unGID, bool& bNew); + + private: + unsigned short m_ushCodesCount; + std::map m_mUnicodeToCode; + std::vector m_vCodeToGid; + std::vector m_vWidths; }; } diff --git a/PdfFile/SrcWriter/FontTT.h b/PdfFile/SrcWriter/FontTT.h index 95a19f0cec..37483c4571 100644 --- a/PdfFile/SrcWriter/FontTT.h +++ b/PdfFile/SrcWriter/FontTT.h @@ -58,7 +58,6 @@ namespace PdfWriter CFontTrueType(CXref* pXref, CDocument* pDocument, const std::wstring& wsFontPath, unsigned int unIndex); ~CFontTrueType(); - unsigned int GetWidth(unsigned short ushCode); int GetLineHeight() const { return m_nLineHeight; diff --git a/PdfFile/SrcWriter/Pages.cpp b/PdfFile/SrcWriter/Pages.cpp index ba96155897..c347393d13 100644 --- a/PdfFile/SrcWriter/Pages.cpp +++ b/PdfFile/SrcWriter/Pages.cpp @@ -1199,6 +1199,15 @@ namespace PdfWriter m_pStream->WriteBinary(sText, unLen, NULL); m_pStream->WriteChar('>'); } + else if (fontType1 == eType) + { + unLen = unLen / 2; + BYTE* sText2 = new BYTE[unLen]; + for (int i = 0; i < unLen; ++i) + sText2[i] = sText[i * 2 + 1]; + m_pStream->WriteEscapeText(sText2, unLen); + RELEASEARRAYOBJECTS(sText2); + } else { m_pStream->WriteEscapeText(sText, unLen); diff --git a/PdfFile/SrcWriter/States.cpp b/PdfFile/SrcWriter/States.cpp index 98d2f22093..a4ce71d84f 100644 --- a/PdfFile/SrcWriter/States.cpp +++ b/PdfFile/SrcWriter/States.cpp @@ -177,7 +177,7 @@ void CCommandManager::Flush() double dX = pText->GetX(); double dY = pText->GetY(); double dTextSize = pText->GetSize(); - double dWidth = ((PdfWriter::CFontCidTrueType*)pText->GetFont())->GetWidth(ushCode) / 1000.0 * dTextSize; + double dWidth = pText->GetFont()->GetWidth(ushCode) / 1000.0 * dTextSize; if (!oTextLine.Add(pCodes, unLen, dX, dY, dWidth, dTextSize)) {