Refactoring

This commit is contained in:
Alexey Nagaev
2024-12-14 11:18:42 +03:00
parent b125c19d02
commit 20d2ff7954
4 changed files with 99 additions and 86 deletions

View File

@ -126,20 +126,7 @@ std::vector<std::wstring> CDocxRenderer::ScanPage(IOfficeDrawingFile* pFile, siz
DrawPage(pFile, nPage);
std::vector<std::wstring> xml_shapes;
for (const auto& shape : m_pInternal->m_oDocument.m_oCurrentPage.m_arShapes)
{
if (!shape) continue;
auto writer = new NSStringUtils::CStringBuilder();
shape->ToXml(*writer);
xml_shapes.push_back(writer->GetData());
delete writer;
}
std::vector<std::wstring>& arComleteObjects = m_pInternal->m_oDocument.m_oCurrentPage.m_arCompleteObjectsXml;
if (!arComleteObjects.empty())
xml_shapes.insert(xml_shapes.end(), arComleteObjects.begin(), arComleteObjects.end());
auto xml_shapes = m_pInternal->m_oDocument.m_oCurrentPage.GetXmlShapes();
m_pInternal->m_oDocument.Clear();
return xml_shapes;
}
@ -154,23 +141,7 @@ std::vector<std::wstring> CDocxRenderer::ScanPagePptx(IOfficeDrawingFile* pFile,
DrawPage(pFile, nPage);
// for drawingml is no tag behind-doc - so we need to reorder shapes
m_pInternal->m_oDocument.m_oCurrentPage.ReorderShapesForPptx();
std::vector<std::wstring> xml_shapes;
for (const auto& shape : m_pInternal->m_oDocument.m_oCurrentPage.m_arShapes)
{
if (!shape) continue;
auto writer = new NSStringUtils::CStringBuilder();
shape->ToXmlPptx(*writer);
xml_shapes.push_back(writer->GetData());
delete writer;
}
std::vector<std::wstring>& arComleteObjects = m_pInternal->m_oDocument.m_oCurrentPage.m_arCompleteObjectsXml;
if (!arComleteObjects.empty())
xml_shapes.insert(xml_shapes.end(), arComleteObjects.begin(), arComleteObjects.end());
auto xml_shapes = m_pInternal->m_oDocument.m_oCurrentPage.GetXmlShapesPptx();
m_pInternal->m_oDocument.Clear();
return xml_shapes;
}
@ -241,7 +212,7 @@ HRESULT CDocxRenderer::AdvancedCommand(IAdvancedCommand* command)
std::string sNewId = "r:embed=\"rId" + std::to_string(pInfo->m_nId + c_iStartingIdForImages) + "\"";
NSStringUtils::string_replaceA(sUtf8Shape, "r:embed=\"\"", sNewId);
}
m_pInternal->m_oDocument.m_oCurrentPage.m_arCompleteObjectsXml.push_back(UTF8_TO_U(sUtf8Shape));
m_pInternal->m_oDocument.m_oCurrentPage.AddCompleteXml(UTF8_TO_U(sUtf8Shape));
return S_OK;
}
case IAdvancedCommand::AdvancedCommandType::ShapeEnd:

View File

@ -54,6 +54,7 @@ namespace NSDocxRenderer
m_oEdgeText.SetDefaultParams();
m_oTransform.Reset();
m_oHorVerLinesCollector.Clear();
m_arConts.clear();
m_arTextLines.clear();
m_arDiacriticalSymbols.clear();
@ -413,6 +414,45 @@ namespace NSDocxRenderer
ToXml(oWriter);
WriteSectionToFile(bIsLastPage, oWriter);
}
std::vector<std::wstring> CPage::GetXmlShapes()
{
std::vector<std::wstring> xml_shapes;
for (const auto& shape : m_arShapes)
{
if (!shape) continue;
auto writer = new NSStringUtils::CStringBuilder();
shape->ToXml(*writer);
xml_shapes.push_back(writer->GetData());
delete writer;
}
if (!m_arCompleteObjectsXml.empty())
xml_shapes.insert(xml_shapes.end(), m_arCompleteObjectsXml.begin(), m_arCompleteObjectsXml.end());
return xml_shapes;
}
std::vector<std::wstring> CPage::GetXmlShapesPptx()
{
ReorderShapesForPptx();
std::vector<std::wstring> xml_shapes;
for (const auto& shape : m_arShapes)
{
if (!shape) continue;
auto writer = new NSStringUtils::CStringBuilder();
shape->ToXmlPptx(*writer);
xml_shapes.push_back(writer->GetData());
delete writer;
}
if (!m_arCompleteObjectsXml.empty())
xml_shapes.insert(xml_shapes.end(), m_arCompleteObjectsXml.begin(), m_arCompleteObjectsXml.end());
return xml_shapes;
}
void CPage::AddCompleteXml(const std::wstring oXml)
{
m_arCompleteObjectsXml.push_back(oXml);
}
void CPage::ReorderShapesForPptx()
{
// переместим nullptr в конец и удалим
@ -942,7 +982,7 @@ namespace NSDocxRenderer
}
}
bool CPage::IsLineCrossingText(shape_ptr_t pShape, cont_ptr_t pCont)
bool CPage::IsLineCrossingText(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept
{
auto h_type = pCont->CBaseItem::GetHorizontalCrossingType(pShape.get());
double dTopBorder = pCont->m_dTop + pCont->m_dHeight / 3;
@ -967,7 +1007,7 @@ namespace NSDocxRenderer
return bIf1 && bIf2 && bIf3 && bIf4;
}
bool CPage::IsLineBelowText(shape_ptr_t pShape, cont_ptr_t pCont)
bool CPage::IsLineBelowText(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept
{
auto h_type = pCont->CBaseItem::GetHorizontalCrossingType(pShape.get());
bool bIf1 = (pShape->m_eGraphicsType == eGraphicsType::gtRectangle ||
@ -991,7 +1031,7 @@ namespace NSDocxRenderer
return bIf1 && bIf2 && bIf3 && bIf4;
}
bool CPage::IsHighlight(shape_ptr_t pShape, cont_ptr_t pCont)
bool CPage::IsHighlight(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept
{
auto h_type = pCont->CBaseItem::GetHorizontalCrossingType(pShape.get());
@ -1021,7 +1061,7 @@ namespace NSDocxRenderer
return bIf1 && bIf2 && bIf3 && bIf4 && !bIf5 && bIf6 && bIf7;
}
bool CPage::IsOutline(shape_ptr_t pShape, cont_ptr_t pCont)
bool CPage::IsOutline(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept
{
auto h_type = pCont->CBaseItem::GetHorizontalCrossingType(pShape.get());
auto v_type = pCont->CBaseItem::GetVerticalCrossingType(pShape.get());
@ -1129,7 +1169,7 @@ namespace NSDocxRenderer
}
}
void CPage::ToXml(NSStringUtils::CStringBuilder& oWriter)
void CPage::ToXml(NSStringUtils::CStringBuilder& oWriter) const noexcept
{
bool bIsNeedWP = !m_arShapes.empty();
@ -1893,7 +1933,7 @@ namespace NSDocxRenderer
return pShape;
}
void CPage::WriteSectionToFile(bool bLastPage, NSStringUtils::CStringBuilder& oWriter)
void CPage::WriteSectionToFile(bool bLastPage, NSStringUtils::CStringBuilder& oWriter) const noexcept
{
// section
int lWidthDx = (int)(m_dWidth * c_dMMToDx);

View File

@ -18,51 +18,27 @@ namespace NSDocxRenderer
CManagers(const CManagers& other) = default;
~CManagers() = default;
CImageManager* pImageManager;
CFontStyleManager* pFontStyleManager;
CParagraphStyleManager* pParagraphStyleManager;
CFontManager* pFontManager;
CFontSelector* pFontSelector;
CImageManager* pImageManager;
CFontStyleManager* pFontStyleManager;
CParagraphStyleManager* pParagraphStyleManager;
CFontManager* pFontManager;
CFontSelector* pFontSelector;
};
using shape_ptr_t = std::shared_ptr<CShape>;
using cont_ptr_t = std::shared_ptr<CContText>;
using line_ptr_t = std::shared_ptr<CTextLine>;
using item_ptr_t = std::shared_ptr<CBaseItem>;
using paragraph_ptr_t = std::shared_ptr<CParagraph>;
double m_dWidth {0.0};
double m_dHeight{0.0};
LONG m_lCurrentCommand{0};
LONG m_lClipMode {0};
TextAssociationType m_eTextAssociationType{TextAssociationType::tatPlainParagraph};
TextAssociationType m_eTextAssociationType{TextAssociationType::tatPlainParagraph};
NSFonts::IApplicationFonts* m_pAppFonts{nullptr};
NSStructures::CFont m_oFont;
NSStructures::CPen m_oPen;
NSStructures::CBrush m_oBrush;
NSStructures::CShadow m_oShadow;
NSStructures::CEdgeText m_oEdgeText;
Aggplus::CMatrix m_oTransform;
CManagers m_oManagers;
CVectorGraphics m_oCurrVectorGraphics, m_oClipVectorGraphics;
CContTextBuilder m_oContBuilder;
CHorVerLinesCollector m_oHorVerLinesCollector;
std::vector<cont_ptr_t> m_arConts;
std::vector<line_ptr_t> m_arTextLines;
std::vector<cont_ptr_t> m_arDiacriticalSymbols;
std::vector<shape_ptr_t> m_arShapes;
std::vector<paragraph_ptr_t> m_arParagraphs;
std::vector<item_ptr_t> m_arOutputObjects;
std::vector<std::wstring> m_arCompleteObjectsXml;
NSStructures::CFont m_oFont{};
NSStructures::CPen m_oPen{};
NSStructures::CBrush m_oBrush{};
NSStructures::CShadow m_oShadow{};
NSStructures::CEdgeText m_oEdgeText{};
Aggplus::CMatrix m_oTransform{};
bool m_bIsDeleteTextClipPage{true};
bool m_bIsRecalcFontSize {true};
@ -100,9 +76,18 @@ namespace NSDocxRenderer
void Analyze();
void Record(NSStringUtils::CStringBuilder& oWriter, bool bIsLastPage);
void ReorderShapesForPptx();
std::vector<std::wstring> GetXmlShapes();
std::vector<std::wstring> GetXmlShapesPptx();
void AddCompleteXml(const std::wstring oXml);
private:
using shape_ptr_t = std::shared_ptr<CShape>;
using cont_ptr_t = std::shared_ptr<CContText>;
using line_ptr_t = std::shared_ptr<CTextLine>;
using item_ptr_t = std::shared_ptr<CBaseItem>;
using paragraph_ptr_t = std::shared_ptr<CParagraph>;
// returns std::vector of conts with diac. symbols and remove it from m_arConts
std::vector<cont_ptr_t> MoveDiacriticalSymbols();
@ -166,23 +151,43 @@ namespace NSDocxRenderer
// calc true shapes rotation for ooxml format
void CalcShapesRotation();
// for drawingml is no tag behind-doc - so we need to reorder shapes
void ReorderShapesForPptx();
// get lines by groups by X
std::vector<std::vector<line_ptr_t>> GetLinesByGroups();
bool IsLineCrossingText(shape_ptr_t pShape, cont_ptr_t pCont);
bool IsLineBelowText(shape_ptr_t pShape, cont_ptr_t pCont);
bool IsHighlight(shape_ptr_t pShape, cont_ptr_t pCont);
bool IsOutline(shape_ptr_t pShape, cont_ptr_t pCont);
bool IsLineCrossingText(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept;
bool IsLineBelowText(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept;
bool IsHighlight(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept;
bool IsOutline(shape_ptr_t pShape, cont_ptr_t pCont) const noexcept;
bool IsShapeBorderBetweenVertical(line_ptr_t pFirst, line_ptr_t pSecond) const noexcept;
bool IsShapeBorderBetweenHorizontal(line_ptr_t pFirst, line_ptr_t pSecond) const noexcept;
bool IsShapeBorderTrough(cont_ptr_t pItem, double& dXCrossing, double& dYCrossing) const noexcept;
shape_ptr_t CreateSingleLineShape(line_ptr_t& pLine);
shape_ptr_t CreateSingleParagraphShape(paragraph_ptr_t& pParagraph);
void ToXml(NSStringUtils::CStringBuilder& oWriter) const noexcept;
void WriteSectionToFile(bool bLastPage, NSStringUtils::CStringBuilder& oWriter) const noexcept;
void ToXml(NSStringUtils::CStringBuilder& oWriter);
void WriteSectionToFile(bool bLastPage, NSStringUtils::CStringBuilder& oWriter);
static shape_ptr_t CreateSingleLineShape(line_ptr_t& pLine);
static shape_ptr_t CreateSingleParagraphShape(paragraph_ptr_t& pParagraph);
CManagers m_oManagers;
CVectorGraphics m_oCurrVectorGraphics;
CVectorGraphics m_oClipVectorGraphics;
CContTextBuilder m_oContBuilder;
CHorVerLinesCollector m_oHorVerLinesCollector;
std::vector<cont_ptr_t> m_arConts;
std::vector<line_ptr_t> m_arTextLines;
std::vector<cont_ptr_t> m_arDiacriticalSymbols;
std::vector<shape_ptr_t> m_arShapes;
std::vector<paragraph_ptr_t> m_arParagraphs;
std::vector<item_ptr_t> m_arOutputObjects;
std::vector<std::wstring> m_arCompleteObjectsXml;
size_t m_nShapeOrder = 0;
};

View File

@ -104,10 +104,7 @@ namespace NSDocxRenderer
CHorVerLinesCollector() = default;
~CHorVerLinesCollector() = default;
// after call m_arHorizontal is empty
const std::vector<std::pair<double, double>>& GetHorizontal();
// after call m_arVertical is empty
const std::vector<std::pair<double, double>>& GetVertical();
void AddVector(const CVectorGraphics& oVector);