From d50f2b3ccc34044642ca13081c0dbfcbe4e4e4e6 Mon Sep 17 00:00:00 2001 From: Green Date: Tue, 20 May 2025 13:08:22 +0300 Subject: [PATCH] Fixed a bug with text margins in hwp/hwpx conversion --- HwpFile/HwpDoc/Conversion/StyleConverter.cpp | 69 +++++++++++++++---- HwpFile/HwpDoc/Conversion/StyleConverter.h | 16 ++++- .../HwpDoc/HWPElements/HWPRecordParaShape.cpp | 19 ++++- .../HwpDoc/HWPElements/HWPRecordParaShape.h | 2 + 4 files changed, 90 insertions(+), 16 deletions(-) diff --git a/HwpFile/HwpDoc/Conversion/StyleConverter.cpp b/HwpFile/HwpDoc/Conversion/StyleConverter.cpp index f7b122aa68..8e99bb1154 100644 --- a/HwpFile/HwpDoc/Conversion/StyleConverter.cpp +++ b/HwpFile/HwpDoc/Conversion/StyleConverter.cpp @@ -168,10 +168,9 @@ CParagraphsStyle CStyleConverter::GenerateParagraphStyle(const CHWPRecordParaSha if (oParaShape.KeepWithNext()) oParagraphsStyle.SetKeepNext(true); - const int nIndent = oParaShape.GetIndent(); - - if (0 != nIndent) - oParagraphsStyle.SetInd(oParaShape.GetIndent()); + oParagraphsStyle.SetFirstLine(oParaShape.GetIndent()); + oParagraphsStyle.SetLeftInd(oParaShape.GetLeftIndent()); + oParagraphsStyle.SetRightInd(oParaShape.GetRightIndent()); switch(oParaShape.GetHorizantalAlign()) { @@ -208,12 +207,13 @@ CParagraphsStyle CStyleConverter::GenerateParagraphStyle(const CHWPRecordParaSha } case 0x02: case 0x03: - default: { oParagraphsStyle.SetSpacingLineRule(ELineRule::AtLeast); oParagraphsStyle.SetSpacing(static_cast((double)oParaShape.GetLineSpacing() / 10.)); //TODO:: проверить, как найдется пример break; } + default: + break; } if (0 != oParaShape.GetMarginPrev()) @@ -295,8 +295,39 @@ void CStyleConverter::WriteParagraphProperties(const CParagraphsStyle& oParagrap if (oParagraphsStyle.KeepNextIsSet() && oParagraphsStyle.KeepNext()) oBuilder.WriteString(L""); - if (oParagraphsStyle.IndIsSet() && 0 != oParagraphsStyle.GetInd()) - oBuilder.WriteString(L"(std::ceil(oParagraphsStyle.GetInd() / 10.))) + L"\"/>"); + if ((oParagraphsStyle.FirstLineIsSet() && 0 != oParagraphsStyle.GetFirstLine()) || + (oParagraphsStyle.LeftIndIsSet() && 0 != oParagraphsStyle.GetLeftInd()) || + (oParagraphsStyle.RightIndIsSet() && 0 != oParagraphsStyle.GetRightInd())) + { + oBuilder.WriteString(L" 0) + { + oBuilder.WriteString(L" w:firstLine=\"" + std::to_wstring(static_cast(std::round(oParagraphsStyle.GetFirstLine() / 10.))) + L"\""); + + if (0 != nLeftInd) + oBuilder.WriteString(L" w:left=\"" + std::to_wstring(static_cast(std::round(nLeftInd / 10.))) + L"\""); + } + else + { + const int nHanging{static_cast(std::round(-oParagraphsStyle.GetFirstLine() / 10.))}; + + oBuilder.WriteString(L" w:hanging=\"" + std::to_wstring(nHanging) + L"\""); + oBuilder.WriteString(L" w:left=\"" + std::to_wstring(static_cast(std::round(nLeftInd / 10.)) + nHanging) + L"\""); + } + } + else if (0 != nLeftInd) + oBuilder.WriteString(L" w:left=\"" + std::to_wstring(static_cast(std::round(oParagraphsStyle.GetLeftInd() / 10.))) + L"\""); + + if (oParagraphsStyle.RightIndIsSet() && 0 != oParagraphsStyle.GetRightInd()) + oBuilder.WriteString(L" w:right=\"" + std::to_wstring(static_cast(std::round(oParagraphsStyle.GetRightInd() / 10.))) + L"\""); + + oBuilder.WriteString(L"/>"); + } if (oParagraphsStyle.JsIsSet()) { @@ -527,7 +558,7 @@ CParagraphsStyle& CParagraphsStyle::operator-=(const CParagraphsStyle& oParagrap return *this; m_bKeepNext -= oParagraphStyle.m_bKeepNext; - m_nInd -= oParagraphStyle.m_nInd; + m_oInd -= oParagraphStyle.m_oInd; m_eJs -= oParagraphStyle.m_eJs; m_eTextAlignment -= oParagraphStyle.m_eTextAlignment; m_oSpacing -= oParagraphStyle.m_oSpacing; @@ -538,10 +569,13 @@ CParagraphsStyle& CParagraphsStyle::operator-=(const CParagraphsStyle& oParagrap void CParagraphsStyle::Clear() { m_bKeepNext.UnSet(); - m_nInd.UnSet(); m_eJs.UnSet(); m_eTextAlignment.UnSet(); + m_oInd.m_nFirstLine.UnSet(); + m_oInd.m_nLeft.UnSet(); + m_oInd.m_nRight.UnSet(); + m_oSpacing.m_eLineRule.UnSet(); m_oSpacing.m_nLine.UnSet(); m_oSpacing.m_nAfter.UnSet(); @@ -550,7 +584,8 @@ void CParagraphsStyle::Clear() bool CParagraphsStyle::Empty() const { - return !m_bKeepNext.IsSet() && !m_nInd.IsSet() && !m_eJs.IsSet() && !m_eTextAlignment.IsSet() && + return !m_bKeepNext.IsSet() && !m_oInd.m_nFirstLine.IsSet() && !m_oInd.m_nLeft.IsSet() && + !m_oInd.m_nRight.IsSet() && !m_eJs.IsSet() && !m_eTextAlignment.IsSet() && !m_oSpacing.m_eLineRule.IsSet() && !m_oSpacing.m_nLine.IsSet() && !m_oSpacing.m_nAfter.IsSet() && !m_oSpacing.m_nBefore.IsSet(); } @@ -584,7 +619,9 @@ bool CParagraphsStyle::Empty() const } CREATE_BODY_METHODS_FOR_PROPERTY_BOOL(CParagraphsStyle, KeepNext, m_bKeepNext) -CREATE_BODY_METHODS_FOR_PROPERTY(CParagraphsStyle, int, Ind, m_nInd) +CREATE_BODY_METHODS_FOR_PROPERTY(CParagraphsStyle, int, FirstLine, m_oInd.m_nFirstLine) +CREATE_BODY_METHODS_FOR_PROPERTY(CParagraphsStyle, int, LeftInd, m_oInd.m_nLeft) +CREATE_BODY_METHODS_FOR_PROPERTY(CParagraphsStyle, int, RightInd, m_oInd.m_nRight) CREATE_BODY_METHODS_FOR_PROPERTY(CParagraphsStyle, EJs, Js, m_eJs) CREATE_BODY_METHODS_FOR_PROPERTY(CParagraphsStyle, ETextAlignment, TextAlignment, m_eTextAlignment) CREATE_BODY_METHODS_FOR_PROPERTY(CParagraphsStyle, ELineRule, SpacingLineRule, m_oSpacing.m_eLineRule) @@ -714,7 +751,7 @@ bool TU::operator!=(const TU& oU) const EHighlightColors NormalizeHighlightColor(const TColor& oCurrentColor) { - EHighlightColors eSelectedColor; + EHighlightColors eSelectedColor{EHighlightColors::Yellow}; double dMinDistance = 999.; double dDistance; @@ -742,4 +779,12 @@ TLineSpacing& TLineSpacing::operator-=(const TLineSpacing& oLineSpacing) return *this; } +TInd& TInd::operator-=(const TInd& oInd) +{ + m_nFirstLine -= oInd.m_nFirstLine; + m_nLeft -= oInd.m_nLeft; + m_nRight -= oInd.m_nRight; + + return *this; +} } diff --git a/HwpFile/HwpDoc/Conversion/StyleConverter.h b/HwpFile/HwpDoc/Conversion/StyleConverter.h index 98ab0ddc5d..d2e819cdf6 100644 --- a/HwpFile/HwpDoc/Conversion/StyleConverter.h +++ b/HwpFile/HwpDoc/Conversion/StyleConverter.h @@ -65,6 +65,15 @@ struct TLineSpacing TLineSpacing& operator-=(const TLineSpacing& oLineSpacing); }; +struct TInd +{ + CProperty m_nFirstLine; + CProperty m_nLeft; + CProperty m_nRight; + + TInd& operator-=(const TInd& oInd); +}; + #define CREATE_METHODS_FOR_PROPERTY(type, name)\ void Set##name(type oValue);\ bool name##IsSet() const;\ @@ -89,13 +98,16 @@ public: CREATE_METHODS_FOR_PROPERTY(EJs, Js); CREATE_METHODS_FOR_PROPERTY(ETextAlignment, TextAlignment); CREATE_METHODS_FOR_PROPERTY(ELineRule, SpacingLineRule); + CREATE_METHODS_FOR_PROPERTY(int, FirstLine); + CREATE_METHODS_FOR_PROPERTY(int, LeftInd); + CREATE_METHODS_FOR_PROPERTY(int, RightInd); CREATE_METHODS_FOR_PROPERTY(int, Spacing); CREATE_METHODS_FOR_PROPERTY(int, SpacingBefore); CREATE_METHODS_FOR_PROPERTY(int, SpacingAfter); private: CProperty m_bKeepNext; - CProperty m_nInd; - CProperty m_eJs; + TInd m_oInd; + CProperty m_eJs; CProperty m_eTextAlignment; TLineSpacing m_oSpacing; }; diff --git a/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.cpp b/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.cpp index 42b5e4a790..a5d1a06264 100644 --- a/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.cpp +++ b/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.cpp @@ -228,14 +228,19 @@ void CHWPRecordParaShape::RecursiveParaShape(CXMLNode& oNode) m_nMarginPrev = oNode.GetAttributeInt(L"value"); else if (L"hc:next" == oNode.GetName()) m_nMarginNext = oNode.GetAttributeInt(L"value"); - else if (L"hp:switch" == oNode.GetName() || + else if (/*L"hp:switch" == oNode.GetName() || L"hp:case" == oNode.GetName() || - L"hp:default" == oNode.GetName() || + L"hp:default" == oNode.GetName() ||*/ L"hh:margin" == oNode.GetName()) { for (CXMLNode& oChild : oNode.GetChilds()) RecursiveParaShape(oChild); } + else if (L"hp:switch" == oNode.GetName()) + { + for (CXMLNode& oChild : oNode.GetChild(L"hp:default").GetChilds()) + RecursiveParaShape(oChild); + } } EHorizontalAlign CHWPRecordParaShape::GetHorizantalAlign() const @@ -293,6 +298,16 @@ int CHWPRecordParaShape::GetIndent() const return m_nIndent; } +int CHWPRecordParaShape::GetLeftIndent() const +{ + return m_nMarginLeft; +} + +int CHWPRecordParaShape::GetRightIndent() const +{ + return m_nMarginRight; +} + bool CHWPRecordParaShape::KeepWithNext() const { return m_bKeepWithNext; diff --git a/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.h b/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.h index f0498c2ea0..55193ee47d 100644 --- a/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.h +++ b/HwpFile/HwpDoc/HWPElements/HWPRecordParaShape.h @@ -91,6 +91,8 @@ public: int GetMarginNext() const; int GetIndent() const; + int GetLeftIndent() const; + int GetRightIndent() const; bool KeepWithNext() const; };