#include "htmlfile2.h" #include #include #include #include #include #include #include #include #include "../Common/3dParty/html/htmltoxhtml.h" #include "../Common/3dParty/html/css/src/CCssCalculator.h" #include "../Common/3dParty/html/css/src/xhtml/CDocumentStyle.h" #include "../Common/FileDownloader/FileDownloader.h" #include "../DesktopEditor/common/Types.h" #include "../DesktopEditor/common/Base64.h" #include "../DesktopEditor/common/SystemUtils.h" #include "../DesktopEditor/common/StringBuilder.h" #include "../DesktopEditor/common/File.h" #include "../DesktopEditor/common/Directory.h" #include "../DesktopEditor/common/Path.h" #include "../DesktopEditor/xml/include/xmlutils.h" #include "../DesktopEditor/raster/BgraFrame.h" #include "../DesktopEditor/graphics/pro/Fonts.h" #include "../DesktopEditor/graphics/pro/Graphics.h" #include "htmlfile2.h" #ifndef VALUE2STR #define VALUE_TO_STRING(x) #x #define VALUE2STR(x) VALUE_TO_STRING(x) #endif std::wstring rStyle = L" a area b strong bdo bdi big br center cite dfn em i var code kbd samp tt del s font img ins u mark q rt sup small sub svg input basefont button label data object noscript output abbr time ruby progress hgroup meter span acronym "; //struct CTree //{ // NSCSS::CNode m_oNode; // std::vector m_arrChild; //}; // Ячейка таблицы struct CTc { int i; int j; std::wstring sGridSpan = L"1"; CTc(int _i, int _j, const std::wstring& sColspan) : i(_i), j(_j), sGridSpan(sColspan) {} bool operator==(const CTc& c2) { return (i == c2.i && j == c2.j && sGridSpan == c2.sGridSpan); } }; // Настройки текста struct CTextSettings { bool bBdo; // Реверс текста bool bPre; // Сохранение форматирования (Сохранение пробелов, табуляций, переносов строк) int nLi; // Уровень списка std::wstring sRStyle; // w:rStyle std::wstring sPStyle; // w:pStyle CTextSettings(bool _bBdo, bool _bPre, int _nLi, const std::wstring& _sRStyle, const std::wstring& _sPStyle) : bBdo(_bBdo), bPre(_bPre), nLi(_nLi), sRStyle(_sRStyle), sPStyle(_sPStyle) {} CTextSettings(const CTextSettings& oTS) : bBdo(oTS.bBdo), bPre(oTS.bPre), nLi(oTS.nLi), sRStyle(oTS.sRStyle), sPStyle(oTS.sPStyle) {} }; class CHtmlFile2_Private { public: XmlUtils::CXmlLiteReader m_oLightReader; // SAX Reader NSCSS::CCssCalculator m_oStylesCalculator; // Css калькулятор NSCSS::CDocumentStyle m_oXmlStyle; // Ooxml стиль std::wstring m_sTmp; // Temp папка std::wstring m_sSrc; // Директория источника std::wstring m_sDst; // Директория назначения std::wstring m_sBase; // Полный базовый адрес NSCSS::CTree m_oTree; // Дерево body html-файла private: int m_nImageId; // ID картинки int m_nFootnoteId; // ID сноски int m_nHyperlinkId; // ID ссылки int m_nCrossId; // ID перекрестной ссылки int m_nNumberingId; // ID списка NSStringUtils::CStringBuilder m_oStylesXml; // styles.xml NSStringUtils::CStringBuilder m_oDocXmlRels; // document.xml.rels NSStringUtils::CStringBuilder m_oDocXml; // document.xml NSStringUtils::CStringBuilder m_oNoteXml; // footnotes.xml NSStringUtils::CStringBuilder m_oNumberXml; // numbering.xml public: CHtmlFile2_Private() : m_nImageId(1), m_nFootnoteId(1), m_nHyperlinkId(1), m_nCrossId(1), m_nNumberingId(1) {} ~CHtmlFile2_Private() { m_oLightReader .Clear(); m_oStylesCalculator.Clear(); m_oXmlStyle .Clear(); m_oStylesXml .Clear(); m_oDocXmlRels .Clear(); m_oDocXml .Clear(); m_oNoteXml .Clear(); m_oNumberXml .Clear(); } // Проверяет наличие тэга html bool isHtml() { return (m_oLightReader.ReadNextNode() ? m_oLightReader.GetName() == L"html" : false); } // Создаёт основу docx void CreateDocxEmpty(CHtmlParams* oParams) { // Создаем пустые папки NSDirectory::CreateDirectory(m_sDst + L"/_rels"); NSDirectory::CreateDirectory(m_sDst + L"/docProps"); NSDirectory::CreateDirectory(m_sDst + L"/word"); NSDirectory::CreateDirectory(m_sDst + L"/word/_rels"); NSDirectory::CreateDirectory(m_sDst + L"/word/media"); NSDirectory::CreateDirectory(m_sDst + L"/word/theme"); // theme1.xml std::wstring sTheme = L""; NSFile::CFileBinary oThemeWriter; if (oThemeWriter.CreateFileW(m_sDst + L"/word/theme/theme1.xml")) { oThemeWriter.WriteStringUTF8(sTheme); oThemeWriter.CloseFile(); } // app.xml std::wstring sApplication = NSSystemUtils::GetEnvVariable(NSSystemUtils::gc_EnvApplicationName); if (sApplication.empty()) sApplication = NSSystemUtils::gc_EnvApplicationNameDefault; #if defined(INTVER) std::string sVersion = VALUE2STR(INTVER); #endif std::wstring sApp = L""; sApp += sApplication + L"/" + UTF8_TO_U(sVersion); sApp += L"0falsefalsefalsefalse"; NSFile::CFileBinary oAppWriter; if (oAppWriter.CreateFileW(m_sDst + L"/docProps/app.xml")) { oAppWriter.WriteStringUTF8(sApp); oAppWriter.CloseFile(); } // .rels std::wstring sRels = L""; NSFile::CFileBinary oRelsWriter; if (oRelsWriter.CreateFileW(m_sDst + L"/_rels/.rels")) { oRelsWriter.WriteStringUTF8(sRels); oRelsWriter.CloseFile(); } // [Content_Types].xml std::wstring sContent = L""; NSFile::CFileBinary oContentWriter; if (oContentWriter.CreateFileW(m_sDst + L"/[Content_Types].xml")) { oContentWriter.WriteStringUTF8(sContent); oContentWriter.CloseFile(); } // footnotes.xml.rels NSFile::CFileBinary oFootRelsWriter; if (oFootRelsWriter.CreateFileW(m_sDst + L"/word/_rels/footnotes.xml.rels")) { oFootRelsWriter.WriteStringUTF8(L""); oFootRelsWriter.CloseFile(); } // fontTable.xml std::wstring sFontTable = L""; NSFile::CFileBinary oFontTableWriter; if (oFontTableWriter.CreateFileW(m_sDst + L"/word/fontTable.xml")) { oFontTableWriter.WriteStringUTF8(sFontTable); oFontTableWriter.CloseFile(); } // settings.xml std::wstring sSettings = L""; NSFile::CFileBinary oSettingsWriter; if (oSettingsWriter.CreateFileW(m_sDst + L"/word/settings.xml")) { oSettingsWriter.WriteStringUTF8(sSettings); oSettingsWriter.CloseFile(); } // webSettings.xml std::wstring sWebSettings = L""; NSFile::CFileBinary oWebWriter; if (oWebWriter.CreateFileW(m_sDst + L"/word/webSettings.xml")) { oWebWriter.WriteStringUTF8(sWebSettings); oWebWriter.CloseFile(); } // numbering.xml // Маркированный список m_oNumberXml += L""; // core.xml std::wstring sCore = L""; if(oParams) { if(!oParams->m_sBookTitle.empty()) { sCore += L""; sCore += oParams->m_sBookTitle; sCore += L""; } if(!oParams->m_sAuthors.empty()) { sCore += L""; sCore += oParams->m_sAuthors; sCore += L""; } if(!oParams->m_sGenres.empty()) { sCore += L""; sCore += oParams->m_sGenres; sCore += L""; } if(!oParams->m_sDate.empty()) { sCore += L""; sCore += oParams->m_sDate; sCore += L""; } if(!oParams->m_sDescription.empty()) { sCore += L""; sCore += oParams->m_sDescription; sCore += L""; } } sCore += L""; NSFile::CFileBinary oCoreWriter; if (oCoreWriter.CreateFileW(m_sDst + L"/docProps/core.xml")) { oCoreWriter.WriteStringUTF8(sCore); oCoreWriter.CloseFile(); } // Начала файлов m_oDocXmlRels += L""; m_oDocXmlRels += L""; m_oDocXmlRels += L""; m_oDocXmlRels += L""; m_oDocXmlRels += L""; m_oDocXmlRels += L""; m_oDocXmlRels += L""; m_oDocXmlRels += L""; m_oDocXml += L""; m_oNoteXml += L""; m_oNoteXml += L""; m_oStylesXml += L""; // docDefaults по умолчанию if(oParams && !oParams->m_sdocDefaults.empty()) m_oStylesXml += oParams->m_sdocDefaults; else m_oStylesXml += L""; // normal по умолчанию if(oParams && !oParams->m_sNormal.empty()) m_oStylesXml += oParams->m_sNormal; else m_oStylesXml += L""; // Маркированный список m_oStylesXml += L""; // Ссылки m_oStylesXml += L""; // Таблицы m_oStylesXml += L""; // Сноски m_oStylesXml += L""; } // Читает файл void readSrc() { // Читаем html m_oLightReader.ReadNextNode(); int nDeath = m_oLightReader.GetDepth(); while(m_oLightReader.ReadNextSiblingNode(nDeath)) { std::wstring sName = m_oLightReader.GetName(); if(sName == L"head") readHead(); else if(sName == L"body") readBody(); } } // Дописывает концы docx void write() { m_oDocXmlRels.WriteString(L""); NSFile::CFileBinary oRelsWriter; if (oRelsWriter.CreateFileW(m_sDst + L"/word/_rels/document.xml.rels")) { oRelsWriter.WriteStringUTF8(m_oDocXmlRels.GetData()); oRelsWriter.CloseFile(); } m_oDocXml.WriteString(L""); NSFile::CFileBinary oDocumentWriter; if (oDocumentWriter.CreateFileW(m_sDst + L"/word/document.xml")) { oDocumentWriter.WriteStringUTF8(m_oDocXml.GetData()); oDocumentWriter.CloseFile(); } m_oNoteXml.WriteString(L""); NSFile::CFileBinary oFootnotesWriter; if (oFootnotesWriter.CreateFileW(m_sDst + L"/word/footnotes.xml")) { oFootnotesWriter.WriteStringUTF8(m_oNoteXml.GetData()); oFootnotesWriter.CloseFile(); } // styles.xml m_oStylesXml.WriteString(L""); NSFile::CFileBinary oStylesWriter; if (oStylesWriter.CreateFileW(m_sDst + L"/word/styles.xml")) { oStylesWriter.WriteStringUTF8(m_oStylesXml.GetData()); oStylesWriter.CloseFile(); } // numbering.xml // Маркированный список m_oNumberXml.WriteString(L""); // Нумерованный список for(int i = 1; i < m_nNumberingId; i++) { m_oNumberXml.WriteString(L""); } m_oNumberXml.WriteString(L""); NSFile::CFileBinary oNumberingWriter; if (oNumberingWriter.CreateFileW(m_sDst + L"/word/numbering.xml")) { oNumberingWriter.WriteStringUTF8(m_oNumberXml.GetData()); oNumberingWriter.CloseFile(); } } // Конвертирует html в xhtml bool htmlXhtml(const std::wstring& sSrc) { std::string sFileContent; if(!NSFile::CFileBinary::ReadAllTextUtf8A(sSrc, sFileContent)) return false; size_t nFind = sFileContent.find("version=\""); if(nFind != std::string::npos) { nFind += 9; size_t nFindEnd = sFileContent.find("\"", nFind); if(nFindEnd != std::string::npos) sFileContent.replace(nFind, nFindEnd - nFind, "1.0"); } if(NSFile::GetFileExtention(sSrc) != L"xhtml") { /* std::wstring sRes = htmlToXhtml(sFileContent); NSFile::CFileBinary oWriter; if (oWriter.CreateFileW(m_sTmp + L"/res.html")) { oWriter.WriteStringUTF8(sRes); oWriter.CloseFile(); } */ return m_oLightReader.FromString(htmlToXhtml(sFileContent)); } return m_oLightReader.FromStringA(sFileContent); } // Конвертирует mht в xhtml bool mhtXhtml(const std::wstring& sSrc) { std::wstring sExtention = NSFile::GetFileExtention(sSrc); if(sExtention == L"mht" || sExtention == L"mhtml") return m_oLightReader.FromString(mhtToXhtml(sSrc)); return htmlXhtml(sSrc); } // Читает стили void readStyle() { if(m_oLightReader.IsEmptyNode()) return; int nDeath = m_oLightReader.GetDepth(); while(m_oLightReader.ReadNextSiblingNode(nDeath)) { std::wstring sName = m_oLightReader.GetName(); if(sName == L"body") readStyle2(m_oTree); else { // Стиль по ссылке if(sName == L"link") { while(m_oLightReader.MoveToNextAttribute()) { if(m_oLightReader.GetName() != L"href") continue; std::wstring sRef = m_oLightReader.GetText(); if(NSFile::GetFileExtention(sRef) != L"css") continue; std::wstring sFName = NSFile::GetFileName(sRef); // Стиль в сети if(sRef.substr(0, 4) == L"http") { sFName = m_sTmp + L'/' + sFName; CFileDownloader oDownloadStyle(sRef, false); oDownloadStyle.SetFilePath(sFName); if(oDownloadStyle.DownloadSync()) { m_oStylesCalculator.AddStylesFromFile(sFName); NSFile::CFileBinary::Remove(sFName); } } else { m_oStylesCalculator.AddStylesFromFile(m_sSrc + L'/' + sFName); m_oStylesCalculator.AddStylesFromFile(m_sSrc + L'/' + sRef); } } m_oLightReader.MoveToElement(); } // тэг style содержит стили для styles.xml else if(sName == L"style") m_oStylesCalculator.AddStyles(m_oLightReader.GetText2()); else readStyle(); } } } void readStyle2(NSCSS::CTree& oTree) { std::wstring sName = m_oLightReader.GetName(); // Стиль по ссылке if(sName == L"link") { while(m_oLightReader.MoveToNextAttribute()) { if(m_oLightReader.GetName() != L"href") continue; std::wstring sRef = m_oLightReader.GetText(); if(NSFile::GetFileExtention(sRef) != L"css") continue; std::wstring sFName = NSFile::GetFileName(sRef); // Стиль в сети if(sRef.substr(0, 4) == L"http") { sFName = m_sTmp + L'/' + sFName; CFileDownloader oDownloadStyle(sRef, false); oDownloadStyle.SetFilePath(sFName); if(oDownloadStyle.DownloadSync()) { m_oStylesCalculator.AddStylesFromFile(sFName); NSFile::CFileBinary::Remove(sFName); } } else { m_oStylesCalculator.AddStylesFromFile(m_sSrc + L'/' + sFName); m_oStylesCalculator.AddStylesFromFile(m_sSrc + L'/' + sRef); } } m_oLightReader.MoveToElement(); } // тэг style содержит стили для styles.xml else if(sName == L"style") m_oStylesCalculator.AddStyles(m_oLightReader.GetText2()); oTree.m_oNode.m_sName = sName; // Стиль по атрибуту while(m_oLightReader.MoveToNextAttribute()) { std::wstring sNameA = m_oLightReader.GetName(); if(sNameA == L"class") oTree.m_oNode.m_sClass = m_oLightReader.GetText(); else if(sNameA == L"id") oTree.m_oNode.m_sId = m_oLightReader.GetText(); else if(sNameA == L"style") oTree.m_oNode.m_sStyle += m_oLightReader.GetText(); else if(sNameA == L"align") oTree.m_oNode.m_sStyle += L"; text-align: " + m_oLightReader.GetText() + L";"; } m_oLightReader.MoveToElement(); int nDeath = m_oLightReader.GetDepth(); while(m_oLightReader.ReadNextSiblingNode(nDeath)) { if(!m_oLightReader.IsEmptyNode()) { NSCSS::CTree oChildTree; readStyle2(oChildTree); oTree.m_arrChild.push_back(oChildTree); } } } void PageBreakBefore() { m_oDocXml.WriteString(L""); } private: std::wstring GetSubClass(NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors) { NSCSS::CNode oNode; std::wstring sNote; oNode.m_sName = m_oLightReader.GetName(); // Стиль по атрибуту while(m_oLightReader.MoveToNextAttribute()) { std::wstring sName = m_oLightReader.GetName(); if(sName == L"class") oNode.m_sClass = m_oLightReader.GetText(); else if(sName == L"id") { oNode.m_sId = m_oLightReader.GetText(); std::wstring sCrossId = std::to_wstring(m_nCrossId++); oXml->WriteString(L"WriteString(sCrossId); oXml->WriteString(L"\" w:name=\""); oXml->WriteEncodeXmlString(oNode.m_sId); oXml->WriteString(L"\"/>WriteString(sCrossId); oXml->WriteString(L"\"/>"); } else if(sName == L"style") oNode.m_sStyle += m_oLightReader.GetText(); else if(sName == L"title") sNote = m_oLightReader.GetText(); else if(sName == L"align") oNode.m_sStyle += L"; text-align: " + m_oLightReader.GetText() + L";"; } m_oLightReader.MoveToElement(); sSelectors.push_back(oNode); return sNote; } std::wstring GetStyle(const NSCSS::CCompiledStyle& oStyle, bool bP) { // NSCSS::CCompiledStyle oStyle = m_oStylesCalculator.GetCompiledStyle(sSelectors); bP ? m_oXmlStyle.WritePStyle(oStyle) : m_oXmlStyle.WriteRStyle(oStyle); m_oStylesXml.WriteString(m_oXmlStyle.GetStyle()); return m_oXmlStyle.GetIdAndClear(); } void readHead() { if(m_oLightReader.IsEmptyNode()) return; int nDeath = m_oLightReader.GetDepth(); while(m_oLightReader.ReadNextSiblingNode(nDeath)) { // Базовый адрес if(m_oLightReader.GetName() == L"base") { while(m_oLightReader.MoveToNextAttribute()) if(m_oLightReader.GetName() == L"href") m_sBase = m_oLightReader.GetText(); m_oLightReader.MoveToElement(); } } } void readBody() { std::vector sSelectors; GetSubClass(&m_oDocXml, sSelectors); /* std::wstring sCrossId = std::to_wstring(m_nCrossId++); m_oDocXml.WriteString(L""); */ bool bWasP = true; readStream(&m_oDocXml, sSelectors, { false, false, -1, L"", L"" }, bWasP); } void readInside (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP, const std::wstring& sName) { if(sName == L"#text") { std::wstring sText = m_oLightReader.GetText(); size_t find = sText.find_first_not_of(L" \n\t\r"); if (find == std::wstring::npos) sText = L" "; else if(!(find == 1 && sText.front() == L' ')) sText.erase(0, find); std::wstring sPStyle = wrP(oXml, sSelectors, oTS, bWasP); oXml->WriteString(L""); std::wstring sRStyle = wrR(oXml, sSelectors, oTS); oXml->WriteString(L""); std::wstring::iterator end; if(oTS.bBdo) std::reverse(sText.begin(), sText.end()); if(oTS.bPre) { size_t nAfter = sText.find_first_of(L"\n\r"); while(nAfter != std::wstring::npos) { oXml->WriteEncodeXmlString(sText.c_str(), nAfter); oXml->WriteString(L""); if(!sPStyle.empty()) { oXml->WriteString(L"WriteString(sPStyle); oXml->WriteString(L"\"/>"); oXml->WriteString(oTS.sPStyle); oXml->WriteString(L""); } oXml->WriteString(L"WriteString(sRStyle); oXml->WriteString(L"\"/>"); oXml->WriteString(oTS.sRStyle); oXml->WriteString(L""); sText.erase(0, nAfter + 1); nAfter = sText.find_first_of(L"\n\r"); } end = sText.end(); } else end = std::unique(sText.begin(), sText.end(), [] (wchar_t l, wchar_t r) { return std::iswspace(l) && std::iswspace(r); }); sText = std::wstring(sText.begin(), end); oXml->WriteEncodeXmlString(sText); oXml->WriteString(L""); bWasP = false; return; } std::wstring sNote = GetSubClass(oXml, sSelectors); // Ссылка // Область ссылки if(sName == L"a" || sName == L"area") readA(oXml, sSelectors, oTS, bWasP, sNote); // Полужирный текст // Акцентированный текст else if(sName == L"b" || sName == L"strong") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Направление текста else if(sName == L"bdo") { std::wstring sDir; while(m_oLightReader.MoveToNextAttribute()) if(m_oLightReader.GetName() == L"dir") sDir = m_oLightReader.GetText(); m_oLightReader.MoveToElement(); CTextSettings oTSBdo(oTS); oTSBdo.bBdo = (sDir == L"rtl"); readStream(oXml, sSelectors, oTSBdo, bWasP); } // Отмена направления текста else if(sName == L"bdi") { CTextSettings oTSBdo(oTS); oTSBdo.bBdo = false; readStream(oXml, sSelectors, oTSBdo, bWasP); } // Увеличивает размер шрифта else if(sName == L"big") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Перенос строки else if(sName == L"br") { wrP(oXml, sSelectors, oTS, bWasP); oXml->WriteString(L""); NSCSS::CCompiledStyle oStyle = m_oStylesCalculator.GetCompiledStyle(sSelectors); if(oStyle.m_pText.GetAlign() == L"both") oXml->WriteString(L""); oXml->WriteString(L""); } else if(sName == L"center") { CTextSettings oTSP(oTS); oTSP.sPStyle += L""; readStream(oXml, sSelectors, oTSP, bWasP); } // Цитата, обычно выделяется курсивом // Новый термин, обычно выделяется курсивом // Акцентированный текст // Курсивный текст // Переменная, обычно выделяется курсивом else if(sName == L"cite" || sName == L"dfn" || sName == L"em" || sName == L"i" || sName == L"var") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Код // Моноширинный шрифт, например, Consolas // Результат скрипта else if(sName == L"code" || sName == L"kbd" || sName == L"samp" || sName == L"tt") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Зачеркнутый текст else if(sName == L"del" || sName == L"s") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } else if(sName == L"font") { while(m_oLightReader.MoveToNextAttribute()) { std::wstring sAName = m_oLightReader.GetName(); if(sAName == L"color") sSelectors.back().m_sStyle += L"; color: " + m_oLightReader.GetText(); else if(sAName == L"face") sSelectors.back().m_sStyle += L"; font-family: " + m_oLightReader.GetText(); else if(sAName == L"size") { int nSize = 3; std::wstring sSize = m_oLightReader.GetText(); if(!sSize.empty()) { if(sSize.front() == L'+') nSize += std::stoi(sSize.substr(1)); else if(sSize.front() == L'-') nSize -= std::stoi(sSize.substr(1)); else nSize = std::stoi(sSize); } sSize = nSize >= 1 && nSize <= 7 ? std::to_wstring(10 + nSize * 5) : L"22"; sSelectors.back().m_sStyle += L"; font-size: " + sSize; } } m_oLightReader.MoveToElement(); readStream(oXml, sSelectors, oTS, bWasP); } // Картинки else if(sName == L"img") readImage(oXml, sSelectors, oTS, bWasP); // Подчеркнутый else if(sName == L"ins" || sName == L"u") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Выделенный текст, обычно выделяется желтым else if(sName == L"mark") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Цитата, выделенная кавычками, обычно выделяется курсивом else if(sName == L"q") { oXml->WriteString(L""); std::wstring sRStyle = wrR(oXml, sSelectors, oTS); oXml->WriteString(L"""); CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); oXml->WriteString(L"WriteString(sRStyle); oXml->WriteString(L"\"/>"); oXml->WriteString(oTS.sRStyle); oXml->WriteString(L"""); bWasP = false; } // Текст верхнего регистра else if(sName == L"rt" || sName == L"sup") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Уменьшает размер шрифта else if(sName == L"small") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Текст нижнего регистра else if(sName == L"sub") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Векторная картинка else if(sName == L"svg") { readSVG(oXml); bWasP = false; } else if(sName == L"input") readInput(oXml, sSelectors, oTS, bWasP); // Игнорируются тэги выполняющие скрипт else if(sName == L"template" || sName == L"canvas" || sName == L"video" || sName == L"math" || sName == L"rp" || sName == L"command" || sName == L"iframe" || sName == L"embed" || sName == L"wbr" || sName == L"audio" || sName == L"bgsound" || sName == L"applet" || sName == L"blink" || sName == L"keygen"|| sName == L"script" || sName == L"comment" || sName == L"title" || sName == L"style") { sSelectors.pop_back(); return; } // Без нового абзаца else if(sName == L"basefont" || sName == L"button" || sName == L"label" || sName == L"data" || sName == L"object" || sName == L"noscript" || sName == L"output" || sName == L"abbr" || sName == L"time" || sName == L"ruby" || sName == L"progress" || sName == L"hgroup" || sName == L"meter" || sName == L"span" || sName == L"acronym") readStream(oXml, sSelectors, oTS, bWasP); // С нового абзаца else { wasP(oXml, sSelectors, bWasP); // Адрес if(sName == L"address") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Определение термина, отступ от левого края else if(sName == L"dd") { CTextSettings oTSP(oTS); oTSP.sPStyle += L""; readStream(oXml, sSelectors, oTSP, bWasP); } // С нового абзаца else if(sName == L"article" || sName == L"header" || sName == L"div" || sName == L"blockquote" || sName == L"main" || sName == L"summary" || sName == L"footer" || sName == L"nav" || sName == L"figcaption" || sName == L"form" || sName == L"details" || sName == L"option" || sName == L"dt" || sName == L"aside" || sName == L"p" || sName == L"section" || sName == L"figure" || sName == L"dl" || sName == L"legend" || sName == L"map" || sName == L"dir" || sName == L"h1" || sName == L"h2" || sName == L"h3" || sName == L"h4" || sName == L"h5" || sName == L"h6") readStream(oXml, sSelectors, oTS, bWasP); // Горизонтальная линия else if(sName == L"hr") { oXml->WriteString(L""); bWasP = false; } // Меню // Маркированный список else if(sName == L"menu" || sName == L"ul" || sName == L"select" || sName == L"datalist") readLi(oXml, sSelectors, oTS, bWasP, true); // Нумерованный список else if(sName == L"ol") readLi(oXml, sSelectors, oTS, bWasP, false); // Предварительно форматированный текст else if(sName == L"pre" || sName == L"xmp") { CTextSettings oTSPre(oTS); oTSPre.bPre = true; oTSPre.sRStyle += L""; oTSPre.sPStyle += L""; readStream(oXml, sSelectors, oTSPre, bWasP); } // Таблицы else if(sName == L"table") { size_t nHyp = 0; for(const NSCSS::CNode& item : sSelectors) { if(item.m_sName == L"a") { oXml->WriteString(L""); nHyp++; } } if(bWasP) oXml->WriteString(L""); oXml->WriteString(L""); readTable(oXml, sSelectors, oTS, bWasP); oXml->WriteString(L""); for(size_t i = 0; i < nHyp; i++) oXml->WriteString(L""); bWasP = true; } // Текст с границами else if(sName == L"textarea" || sName == L"fieldset") { CTextSettings oTSP(oTS); oTSP.sPStyle += L""; readStream(oXml, sSelectors, oTSP, bWasP); } // Неизвестный тэг. Выделять ли его абзацем? else readStream(oXml, sSelectors, oTS, bWasP); readNote(oXml, sNote); sNote = L""; wasP(oXml, sSelectors, bWasP); } readNote(oXml, sNote); sSelectors.pop_back(); } bool readStream (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP) { int nDeath = m_oLightReader.GetDepth(); if(m_oLightReader.IsEmptyNode() || !m_oLightReader.ReadNextSiblingNode2(nDeath)) return false; do { readInside(oXml, sSelectors, oTS, bWasP, m_oLightReader.GetName()); } while(m_oLightReader.ReadNextSiblingNode2(nDeath)); return true; } void readTr (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP, const std::wstring& sBorders) { std::vector mTable; int nDeath = m_oLightReader.GetDepth(); int i = 1; // Строка size_t nHyp = 0; for(const NSCSS::CNode& item : sSelectors) if(item.m_sName == L"a") nHyp++; while(m_oLightReader.ReadNextSiblingNode(nDeath)) { // tr - строки в таблице if(m_oLightReader.GetName() != L"tr") continue; int nTrDeath = m_oLightReader.GetDepth(); if(m_oLightReader.IsEmptyNode() || !m_oLightReader.ReadNextSiblingNode(nTrDeath)) continue; int j = 1; // Столбец oXml->WriteString(L""); do { int nColspan = 1; int nRowspan = 1; while(m_oLightReader.MoveToNextAttribute()) { if(m_oLightReader.GetName() == L"colspan") nColspan = stoi(m_oLightReader.GetText()); else if(m_oLightReader.GetName() == L"rowspan") nRowspan = stoi(m_oLightReader.GetText()); } m_oLightReader.MoveToElement(); // Вставляем ячейки до std::vector::iterator it1 = std::find_if(mTable.begin(), mTable.end(), [i, j](const CTc& item){ return item.i == i && item.j == j; }); std::vector::iterator it2 = std::find_if(mTable.begin(), mTable.end(), [j] (const CTc& item){ return item.i == 0 && item.j == j; }); while(it1 != mTable.end() || it2 != mTable.end()) { oXml->WriteString(L""); oXml->WriteString(!sBorders.empty() ? sBorders : L""); oXml->WriteString(L"sGridSpan : it2->sGridSpan); oXml->WriteString(sCol); oXml->WriteString(L"\"/>"); j += stoi(sCol); it1 = std::find_if(mTable.begin(), mTable.end(), [i, j](const CTc& item){ return item.i == i && item.j == j; }); it2 = std::find_if(mTable.begin(), mTable.end(), [j] (const CTc& item){ return item.i == 0 && item.j == j; }); } oXml->WriteString(L""); oXml->WriteString(!sBorders.empty() ? sBorders : L""); oXml->WriteString(L""); if(nRowspan != 1) { oXml->WriteString(L""); std::wstring sColspan = std::to_wstring(nColspan); if(nRowspan == 0) mTable.push_back({0, j, sColspan}); else for(int k = i + 1; k < i + nRowspan; k++) mTable.push_back({k, j, sColspan}); } if(nColspan != 1) { oXml->WriteString(L"WriteString(std::to_wstring(nColspan)); oXml->WriteString(L"\"/>"); j += nColspan - 1; } oXml->WriteString(L""); for(size_t i = 0; i < nHyp; i++) oXml->WriteString(L""); bWasP = true; GetSubClass(oXml, sSelectors); // Читаем th. Ячейка заголовка таблицы. Выравнивание посередине. Выделяется полужирным if(m_oLightReader.GetName() == L"th") { CTextSettings oTSR(oTS); oTSR.sRStyle += L""; readStream(oXml, sSelectors, oTSR, bWasP); } // Читаем td. Ячейка таблицы. Выравнивание вправо else if(m_oLightReader.GetName() == L"td") readStream(oXml, sSelectors, oTS, bWasP); sSelectors.pop_back(); for(size_t i = 0; i < nHyp; i++) oXml->WriteString(L""); if (bWasP) { wrP(oXml, sSelectors, oTS, bWasP); oXml->WriteString(L""); } oXml->WriteString(L""); j++; // Вставляем ячейки после it1 = std::find_if(mTable.begin(), mTable.end(), [i, j](const CTc& item){ return item.i == i && item.j == j; }); it2 = std::find_if(mTable.begin(), mTable.end(), [j] (const CTc& item){ return item.i == 0 && item.j == j; }); while(it1 != mTable.end() || it2 != mTable.end()) { oXml->WriteString(L""); oXml->WriteString(!sBorders.empty() ? sBorders : L""); oXml->WriteString(L"sGridSpan : it2->sGridSpan); oXml->WriteString(sCol); oXml->WriteString(L"\"/>"); j += stoi(sCol); it1 = std::find_if(mTable.begin(), mTable.end(), [i, j](const CTc& item){ return item.i == i && item.j == j; }); it2 = std::find_if(mTable.begin(), mTable.end(), [j] (const CTc& item){ return item.i == 0 && item.j == j; }); } } while(m_oLightReader.ReadNextSiblingNode(nTrDeath)); oXml->WriteString(L""); i++; } } void readTable (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP) { if(m_oLightReader.IsEmptyNode()) return; std::wstring sBorders; while (m_oLightReader.MoveToNextAttribute()) if (m_oLightReader.GetName() == L"border") sBorders = L""; m_oLightReader.MoveToElement(); NSStringUtils::CStringBuilder oHead; NSStringUtils::CStringBuilder oBody; NSStringUtils::CStringBuilder oFoot; // Начало таблицы oXml->WriteString(L""); NSCSS::CNode oLast = sSelectors.back(); sSelectors.pop_back(); NSCSS::CCompiledStyle oStyle = m_oStylesCalculator.GetCompiledStyle(sSelectors); std::wstring sAlign = oStyle.m_pText.GetAlign(); if(sAlign == L"left" || sAlign == L"center" || sAlign == L"right" || sAlign == L"both") oXml->WriteString(L""); oXml->WriteString(L""); sSelectors.push_back(oLast); // borders oStyle = m_oStylesCalculator.GetCompiledStyle(sSelectors, true); oStyle.m_pBorder.Unlock(); if (oStyle.m_pBorder.Empty()) { sBorders = L""; } else { if (oStyle.m_pBorder.EqualSides()) { std::wstring sColor = oStyle.m_pBorder.GetColorBottomSide(); std::wstring sSz = oStyle.m_pBorder.GetWidthBottomSideW(); sBorders = L"" + L"" + L"" + L"" + L"" + L""; } else { std::wstring sColorLeftSide = oStyle.m_pBorder.GetColorLeftSide(); std::wstring sSzLeftSide = oStyle.m_pBorder.GetWidthLeftSideW(); std::wstring sColorTopSide = oStyle.m_pBorder.GetColorTopSide(); std::wstring sSzTopSide = oStyle.m_pBorder.GetWidthTopSideW(); std::wstring sColorRightSide = oStyle.m_pBorder.GetColorRightSide(); std::wstring sSzRightSide = oStyle.m_pBorder.GetWidthRightSideW(); std::wstring sColorBottomSide = oStyle.m_pBorder.GetColorBottomSide(); std::wstring sSzBottomSide = oStyle.m_pBorder.GetWidthBottomSideW(); sBorders = L"" + L"" + L"" + L""; } } /* NSCSS::CCompiledStyle oStyleSetting = m_oStylesCalculator.GetCompiledStyle(sSelectors, true); oStyle = m_oStylesCalculator.GetCompiledStyle(sSelectors); NSCSS::CCompiledStyle::StyleEquation(oStyle, oStyleSetting); m_oXmlStyle.WriteLitePStyle(oStyleSetting); std::wstring sPSettings = m_oXmlStyle.GetStyle(); m_oXmlStyle.Clear(); size_t nBdr = sPSettings.find(L""); if (nBdr != std::wstring::npos) { nBdr += 8; size_t nBdrEnd = sPSettings.find(L"", nBdr); if (nBdrEnd != std::wstring::npos) { sBorders = sPSettings.substr(nBdr, nBdrEnd - nBdr); size_t nSpace = sBorders.find(L"w:space=\""); while (nSpace != std::wstring::npos) { nSpace += 9; size_t nSpaceEnd = sBorders.find(L'\"', nSpace); sBorders.replace(nSpace, nSpaceEnd - nSpace, L"0"); nSpace = sBorders.find(L"w:space=\"", nSpace); } } } */ int nDeath = m_oLightReader.GetDepth(); while(m_oLightReader.ReadNextSiblingNode(nDeath)) { std::wstring sName = m_oLightReader.GetName(); GetSubClass(oXml, sSelectors); // Заголовок таблицы if(sName == L"caption") { bWasP = true; oXml->WriteString(L""); size_t nHyp = 0; for(const NSCSS::CNode& item : sSelectors) { if(item.m_sName == L"a") { oXml->WriteString(L""); nHyp++; } } CTextSettings oTSP { oTS.bBdo, oTS.bPre, oTS.nLi, oTS.sRStyle, oTS.sPStyle + L"" }; readStream(oXml, sSelectors, oTSP, bWasP); for(size_t i = 0; i < nHyp; i++) oXml->WriteString(L""); oXml->WriteString(L""); bWasP = false; } if(sName == L"thead") readTr(&oHead, sSelectors, oTS, bWasP, sBorders); else if(sName == L"tbody") readTr(&oBody, sSelectors, oTS, bWasP, sBorders); else if(sName == L"tfoot") readTr(&oFoot, sSelectors, oTS, bWasP, sBorders); sSelectors.pop_back(); } // Конец таблицы oXml->WriteString(oHead.GetData()); oXml->WriteString(oBody.GetData()); oXml->WriteString(oFoot.GetData()); oXml->WriteString(L""); // Пустая строка после таблицы, чтобы следующий текст не приклеивался // oXml->WriteString(L""); bWasP = false; } void readInput (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP) { std::wstring sValue; std::wstring sAlt; std::wstring sType; while(m_oLightReader.MoveToNextAttribute()) { std::wstring sName = m_oLightReader.GetName(); if(sName == L"value") sValue = m_oLightReader.GetText(); else if(sName == L"alt") sAlt = m_oLightReader.GetText(); else if(sName == L"type") sType = m_oLightReader.GetText(); } m_oLightReader.MoveToElement(); if(sType == L"hidden") return; if(sValue.empty()) sValue = sAlt; if(!sValue.empty()) { oXml->WriteString(L""); wrR(oXml, sSelectors, oTS); oXml->WriteString(L""); oXml->WriteEncodeXmlString(sValue + L' '); oXml->WriteString(L""); bWasP = false; } readStream(oXml, sSelectors, oTS, bWasP); } void readLi (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP, bool bType) { if(m_oLightReader.IsEmptyNode()) return; std::wstring sStart = L"1"; while(m_oLightReader.MoveToNextAttribute()) if(m_oLightReader.GetName() == L"start") sStart = m_oLightReader.GetText(); m_oLightReader.MoveToElement(); int nDeath = m_oLightReader.GetDepth(); while(m_oLightReader.ReadNextSiblingNode(nDeath)) { std::wstring sName = m_oLightReader.GetName(); GetSubClass(oXml, sSelectors); if(sName == L"optgroup") { while(m_oLightReader.MoveToNextAttribute()) { if(m_oLightReader.GetName() != L"label") continue; wasP(oXml, sSelectors, bWasP); wrP(oXml, sSelectors, oTS, bWasP); oXml->WriteString(L""); wrR(oXml, sSelectors, oTS); oXml->WriteString(L""); oXml->WriteEncodeXmlString(m_oLightReader.GetText()); oXml->WriteString(L""); bWasP = false; wasP(oXml, sSelectors, bWasP); } m_oLightReader.MoveToElement(); readLi(oXml, sSelectors, oTS, bWasP, true); continue; } if(sName != L"li" && sName != L"option") { readInside(oXml, sSelectors, oTS, bWasP, sName); sSelectors.pop_back(); continue; } while(m_oLightReader.MoveToNextAttribute()) if(m_oLightReader.GetName() == L"value") sStart = m_oLightReader.GetText(); m_oLightReader.MoveToElement(); wasP(oXml, sSelectors, bWasP); CTextSettings oTSLiP(oTS); oTSLiP.nLi++; oTSLiP.sPStyle += L""; readStream(oXml, sSelectors, oTSLiP, bWasP); wasP(oXml, sSelectors, bWasP); sSelectors.pop_back(); } // Нумерованный список if(!bType) { m_oNumberXml.WriteString(L""); } } void readA (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP, std::wstring& sNote) { std::wstring sRef; std::wstring sAlt; bool bCross = false; while(m_oLightReader.MoveToNextAttribute()) { std::wstring sName = m_oLightReader.GetName(); if(sName == L"href") { sRef = m_oLightReader.GetText(); if(sRef.find('#') != std::wstring::npos) bCross = true; } else if(sName == L"name") { std::wstring sCrossId = std::to_wstring(m_nCrossId++); oXml->WriteString(L"WriteString(sCrossId); oXml->WriteString(L"\" w:name=\""); oXml->WriteString(m_oLightReader.GetText()); oXml->WriteString(L"\"/>WriteString(sCrossId); oXml->WriteString(L"\"/>"); } else if(sName == L"alt") sAlt = m_oLightReader.GetText(); } m_oLightReader.MoveToElement(); if(sNote.empty()) sNote = sRef; wrP(oXml, sSelectors, oTS, bWasP); // Перекрестная ссылка внутри файла if(bCross) { oXml->WriteString(L"WriteString(NSFile::GetFileName(sRef)); else oXml->WriteString(sRef.c_str() + nSharp + 1); } // Внешняя ссылка else { // Пишем рельсы m_oDocXmlRels.WriteString(L""); // Пишем в document.xml oXml->WriteString(L"WriteEncodeXmlString(sNote); oXml->WriteString(L"\" r:id=\"rHyp"); oXml->WriteString(std::to_wstring(m_nHyperlinkId++)); } oXml->WriteString(L"\">"); if(!readStream(oXml, sSelectors, oTS, bWasP)) { oXml->WriteString(L""); wrR(oXml, sSelectors, oTS); oXml->WriteString(L""); oXml->WriteEncodeXmlString(sAlt); oXml->WriteString(L""); } oXml->WriteString(L""); bWasP = false; sNote = L""; } void readImage (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP) { std::wstring sAlt = L""; bool bRes = false; while(m_oLightReader.MoveToNextAttribute()) { if(m_oLightReader.GetName() == L"alt") { sAlt = m_oLightReader.GetText(); continue; } else if(m_oLightReader.GetName() != L"src") continue; std::wstring sSrcM = m_oLightReader.GetText(); std::wstring sImageName; std::wstring sImageId = std::to_wstring(m_nImageId); // Картинка Base64 if(sSrcM.substr(0, 4) == L"data") { size_t nBase = sSrcM.find(L"/", 4); if(nBase == std::wstring::npos) continue; nBase++; size_t nEndBase = sSrcM.find(L";", nBase); if(nEndBase == std::wstring::npos) continue; std::wstring sType = sSrcM.substr(nBase, nEndBase - nBase); if(sType == L"octet-stream") sType = L"jpg"; sImageName = sImageId + L"." + sType; NSFile::CFileBinary oImageWriter; if(oImageWriter.CreateFileW(m_sDst + L"/word/media/i" + sImageName)) { nBase = sSrcM.find(L"base64", 4); if(nBase == std::wstring::npos) continue; std::string sBase64 = m_oLightReader.GetTextA().substr(nBase + 7); int nSrcLen = (int)sBase64.length(); int nDecodeLen = NSBase64::Base64DecodeGetRequiredLength(nSrcLen); if(nDecodeLen == 0) continue; BYTE* pImageData = new BYTE[nDecodeLen]; if (TRUE == NSBase64::Base64Decode(sBase64.c_str(), nSrcLen, pImageData, &nDecodeLen)) { oImageWriter.WriteFile(pImageData, (DWORD)nDecodeLen); bRes = true; } RELEASEARRAYOBJECTS(pImageData); oImageWriter.CloseFile(); } } // Картинка в сети else if(sSrcM.substr(0, 4) == L"http" || !m_sBase.empty()) { std::wstring sExtention = NSFile::GetFileExtention(sSrcM); std::transform(sExtention.begin(), sExtention.end(), sExtention.begin(), tolower); if(sExtention != L"bmp" && sExtention != L"svg" && sExtention != L"jfif" && sExtention != L"wmf" && sExtention != L"gif" && sExtention != L"jpe" && sExtention != L"png" && sExtention != L"jpeg" && sExtention != L"jpg" ) continue; sImageName = NSFile::GetFileName(sSrcM); sImageName.erase(std::remove_if(sImageName.begin(), sImageName.end(), [] (wchar_t ch) { return std::iswspace(ch) || (ch == L'^'); }), sImageName.end()); CFileDownloader oDownloadImg(m_sBase + sSrcM, false); oDownloadImg.SetFilePath(m_sDst + L"/word/media/i" + sImageName); bRes = oDownloadImg.DownloadSync(); } // Картинка по относительному пути else { sImageName = NSFile::GetFileName(sSrcM); sImageName.erase(std::remove_if(sImageName.begin(), sImageName.end(), [] (wchar_t ch) { return std::iswspace(ch) || (ch == L'^'); }), sImageName.end()); std::wstring sExtention = NSFile::GetFileExtention(sSrcM); std::transform(sExtention.begin(), sExtention.end(), sExtention.begin(), tolower); if(sExtention != L"bmp" && sExtention != L"svg" && sExtention != L"jfif" && sExtention != L"wmf" && sExtention != L"gif" && sExtention != L"jpe" && sExtention != L"png" && sExtention != L"jpeg" && sExtention != L"jpg" ) continue; bRes = NSFile::CFileBinary::Copy(m_sSrc + L"/" + sSrcM, m_sDst + L"/word/media/i" + sImageName); if(!bRes) bRes = NSFile::CFileBinary::Copy(m_sSrc + L"/" + NSFile::GetFileName(sSrcM), m_sDst + L"/word/media/i" + sImageName); if(!bRes) bRes = NSFile::CFileBinary::Copy(sSrcM, m_sDst + L"/word/media/i" + sImageName); } if(bRes) { wrP(oXml, sSelectors, oTS, bWasP); bRes = ImageRels(oXml, sImageId, L"i" + sImageName); } } m_oLightReader.MoveToElement(); if(!bRes) { wrP(oXml, sSelectors, oTS, bWasP); oXml->WriteString(L""); wrR(oXml, sSelectors, oTS); oXml->WriteString(L""); oXml->WriteEncodeXmlString(sAlt); oXml->WriteString(L""); } bWasP = false; } std::wstring wrP(NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS, bool& bWasP) { if(!bWasP) return L""; oXml->WriteString(L"> temporary; size_t i = 0; while(i != sSelectors.size()) { if(rStyle.find(L' ' + sSelectors[i].m_sName + L' ') != std::wstring::npos) { temporary.push_back(std::make_pair(i, sSelectors[i])); sSelectors.erase(sSelectors.begin() + i); } else i++; } NSCSS::CCompiledStyle oStyleSetting = m_oStylesCalculator.GetCompiledStyle(sSelectors, true); NSCSS::CCompiledStyle oStyle = m_oStylesCalculator.GetCompiledStyle(sSelectors); NSCSS::CCompiledStyle::StyleEquation(oStyle, oStyleSetting); std::wstring sPStyle = GetStyle(oStyle, true); m_oXmlStyle.WriteLitePStyle(oStyleSetting); std::wstring sPSettings = m_oXmlStyle.GetStyle(); m_oXmlStyle.Clear(); for(int i = temporary.size() - 1; i >= 0; i--) sSelectors.insert(sSelectors.begin() + temporary[i].first, temporary[i].second); // Если в таблице bool bInTable = false; for (const NSCSS::CNode& item : sSelectors) if (item.m_sName == L"table") bInTable = true; if (bInTable) { size_t nBdr = sPSettings.find(L""); if (nBdr != std::wstring::npos) { size_t nBdrEnd = sPSettings.find(L"", nBdr); if (nBdrEnd != std::wstring::npos) sPSettings.erase(nBdr, nBdrEnd + 9 - nBdr); } } oXml->WriteString(sPStyle); oXml->WriteString(L"\"/>"); oXml->WriteString(oTS.sPStyle + L' ' + sPSettings); oXml->WriteString(L""); bWasP = false; return sPStyle; } std::wstring wrR(NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, const CTextSettings& oTS) { NSCSS::CCompiledStyle oStyleSetting = m_oStylesCalculator.GetCompiledStyle(sSelectors, true); NSCSS::CCompiledStyle oStyle = m_oStylesCalculator.GetCompiledStyle(sSelectors); NSCSS::CCompiledStyle::StyleEquation(oStyle, oStyleSetting); std::wstring sRStyle = GetStyle(oStyle, false); m_oXmlStyle.WriteLiteRStyle(oStyleSetting); const std::wstring sRSettings = m_oXmlStyle.GetStyle(); m_oXmlStyle.Clear(); oXml->WriteString(L"WriteString(sRStyle); oXml->WriteString(L"\"/>"); oXml->WriteString(oTS.sRStyle + L' ' + sRSettings); oXml->WriteString(L""); return sRStyle; } void wasP (NSStringUtils::CStringBuilder* oXml, std::vector& sSelectors, bool& bWasP) { if(bWasP) return; size_t nHyp = 0; for(const NSCSS::CNode& item : sSelectors) { if(item.m_sName == L"a") { oXml->WriteString(L""); nHyp++; } } oXml->WriteString(L""); for(size_t i = 0; i < nHyp; i++) oXml->WriteString(L""); bWasP = true; } bool ImageRels (NSStringUtils::CStringBuilder* oXml, const std::wstring& sImageId, const std::wstring& sImageName) { CBgraFrame oBgraFrame; if(!oBgraFrame.OpenFile(m_sDst + L"/word/media/" + sImageName)) return false; m_nImageId++; // Прописать рельсы m_oDocXmlRels.WriteString(L""); // Получаем размеры картинки int nHy = oBgraFrame.get_Height(); int nWx = oBgraFrame.get_Width(); if(nWx > nHy) { int nW = nWx * 9525; nW = (nW > 7000000 ? 7000000 : nW); nHy = (int)((double)nHy * (double)nW / (double)nWx); nWx = nW; } else { int nH = nHy * 9525; nH = (nH > 9000000 ? 9000000 : nH); int nW = (int)((double)nWx * (double)nH / (double)nHy); if(nW > 7000000) { nW = 7000000; nHy = (int)((double)nHy * (double)nW / (double)nWx); } else nHy = nH; nWx = nW; } // Пишем в document.xml oXml->WriteString(L"WriteString(std::to_wstring(nWx)); oXml->WriteString(L"\" cy=\""); oXml->WriteString(std::to_wstring(nHy)); oXml->WriteString(L"\"/>WriteString(sImageId); oXml->WriteString(L"\" name=\"\"/>WriteString(sImageId); oXml->WriteString(L"\" name=\"\"/>WriteString(sImageId); oXml->WriteString(L"\"/>WriteString(std::to_wstring(nWx)); oXml->WriteString(L"\" cy=\""); oXml->WriteString(std::to_wstring(nHy)); oXml->WriteString(L"\"/>"); return true; } void readNote (NSStringUtils::CStringBuilder* oXml, const std::wstring& sNote) { if(sNote.empty()) return; oXml->WriteString(L"WriteString(std::to_wstring(m_nFootnoteId)); oXml->WriteString(L"\"/>"); m_oNoteXml.WriteString(L""); m_oNoteXml.WriteEncodeXmlString(sNote); m_oNoteXml.WriteString(L""); } void readSVG (NSStringUtils::CStringBuilder* oXml) { // Сохранить как .svg картинку NSStringUtils::CStringBuilder oSVG; oSVG.WriteString(L""); std::wstring sSVG = m_oLightReader.GetInnerXml(); size_t nRef = sSVG.find(L"image"); while(nRef != std::wstring::npos) { size_t nHRef = sSVG.find(L"href", nRef); if(nHRef == std::wstring::npos) break; nHRef += 6; if(sSVG.compare(nHRef, 4, L"http") == 0) { nRef = sSVG.find(L"image", nRef + 5); continue; } size_t nHRefLen = sSVG.find(L"\"", nHRef); if(nHRefLen == std::wstring::npos) break; std::wstring sImageName = sSVG.substr(nHRef, nHRefLen - nHRef); std::wstring sTIN(sImageName); sTIN.erase(std::remove_if(sTIN.begin(), sTIN.end(), [] (wchar_t ch) { return std::iswspace(ch) || (ch == L'^'); }), sTIN.end()); sTIN = NSFile::GetFileName(sTIN); bool bRes = NSFile::CFileBinary::Copy(m_sSrc + L"/" + sImageName, m_sDst + L"/word/media/" + sTIN); if(!bRes) bRes = NSFile::CFileBinary::Copy(m_sSrc + L"/" + NSFile::GetFileName(sImageName), m_sDst + L"/word/media/" + sTIN); if(bRes) sSVG.replace(nHRef, nHRefLen - nHRef, sTIN); nRef = sSVG.find(L"image", nRef + 5); } oSVG.WriteString(sSVG); oSVG.WriteString(L""); std::wstring sImageId = std::to_wstring(m_nImageId); NSFile::CFileBinary oSVGWriter; std::wstring sImageFile = m_sDst + L"/word/media/" + sImageId + L".svg"; if (oSVGWriter.CreateFileW(sImageFile)) { oSVGWriter.WriteStringUTF8(oSVG.GetData()); oSVGWriter.CloseFile(); } // Конвертация из svg в png NSFonts::IApplicationFonts* pFonts = NSFonts::NSApplication::Create(); MetaFile::IMetaFile* pMetafile = MetaFile::Create(pFonts); bool bLoad = pMetafile->LoadFromFile(sImageFile.data()); if(bLoad) { std::wstring sPngFile = m_sDst + L"/word/media/" + sImageId + L".png"; pMetafile->ConvertToRaster(sPngFile.data(), 4, 1000); } pMetafile->Release(); pFonts->Release(); ImageRels(oXml, sImageId, sImageId + L".png"); } }; CHtmlFile2::CHtmlFile2() { m_internal = new CHtmlFile2_Private(); } CHtmlFile2::~CHtmlFile2() { RELEASEOBJECT(m_internal); } bool CHtmlFile2::IsHtmlFile(const std::wstring& sFile) { // Конвертируем в xhtml if(!m_internal->htmlXhtml(sFile)) return false; // Читаем html return m_internal->isHtml(); } bool CHtmlFile2::IsMhtFile(const std::wstring& sFile) { // Конвертируем в xhtml if(!m_internal->mhtXhtml(sFile)) return false; // Читаем html return m_internal->isHtml(); } void CHtmlFile2::SetTmpDirectory(const std::wstring& sFolder) { m_internal->m_sTmp = sFolder; } HRESULT CHtmlFile2::OpenHtml(const std::wstring& sSrc, const std::wstring& sDst, CHtmlParams* oParams) { if(!m_internal->m_oLightReader.IsValid()) if(!IsHtmlFile(sSrc)) return S_FALSE; m_internal->m_sSrc = NSSystemPath::GetDirectoryName(sSrc); m_internal->m_sDst = sDst; m_internal->CreateDocxEmpty(oParams); m_internal->readStyle(); m_internal->m_oStylesCalculator.SetBodyTree(m_internal->m_oTree); // Переходим в начало if(!m_internal->m_oLightReader.MoveToStart()) return S_FALSE; if(oParams && oParams->m_bNeedPageBreakBefore) m_internal->PageBreakBefore(); m_internal->readSrc(); m_internal->write(); return S_OK; } HRESULT CHtmlFile2::OpenMht(const std::wstring& sSrc, const std::wstring& sDst, CHtmlParams* oParams) { if(!m_internal->m_oLightReader.IsValid()) if(!IsMhtFile(sSrc)) return S_FALSE; m_internal->m_sSrc = NSSystemPath::GetDirectoryName(sSrc); m_internal->m_sDst = sDst; m_internal->CreateDocxEmpty(oParams); m_internal->readStyle(); m_internal->m_oStylesCalculator.SetBodyTree(m_internal->m_oTree); // Переходим в начало if(!m_internal->m_oLightReader.MoveToStart()) return S_FALSE; if(oParams && oParams->m_bNeedPageBreakBefore) m_internal->PageBreakBefore(); m_internal->readSrc(); m_internal->write(); return S_OK; } HRESULT CHtmlFile2::OpenBatchHtml(const std::vector& sSrc, const std::wstring& sDst, CHtmlParams* oParams) { m_internal->m_sDst = sDst; m_internal->CreateDocxEmpty(oParams); for(const std::wstring& sS : sSrc) { #ifdef _DEBUG std::wcout << NSFile::GetFileName(sS) << std::endl; #endif m_internal->m_sSrc = NSSystemPath::GetDirectoryName(sS); if(!IsHtmlFile(sS)) continue; m_internal->readStyle(); m_internal->m_oStylesCalculator.SetBodyTree(m_internal->m_oTree); // Переходим в начало if(m_internal->m_oLightReader.MoveToStart()) { if(oParams && oParams->m_bNeedPageBreakBefore) m_internal->PageBreakBefore(); m_internal->readSrc(); m_internal->m_oLightReader.Clear(); m_internal->m_sBase.clear(); } m_internal->m_oStylesCalculator.Clear(); } m_internal->write(); return S_OK; }