Fixed a bug with text margins in hwp/hwpx conversion

This commit is contained in:
Green
2025-05-20 13:08:22 +03:00
parent 8ff979c2e9
commit d50f2b3ccc
4 changed files with 90 additions and 16 deletions

View File

@ -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<int>((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"<w:keepNext w:val=\"true\"/>");
if (oParagraphsStyle.IndIsSet() && 0 != oParagraphsStyle.GetInd())
oBuilder.WriteString(L"<w:ind w:firstLine=\"" + std::to_wstring(static_cast<int>(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"<w:ind");
const int nLeftInd {(oParagraphsStyle.LeftIndIsSet()) ? oParagraphsStyle.GetLeftInd() : 0};
if (oParagraphsStyle.FirstLineIsSet() && 0 != oParagraphsStyle.GetFirstLine())
{
if (oParagraphsStyle.GetFirstLine() > 0)
{
oBuilder.WriteString(L" w:firstLine=\"" + std::to_wstring(static_cast<int>(std::round(oParagraphsStyle.GetFirstLine() / 10.))) + L"\"");
if (0 != nLeftInd)
oBuilder.WriteString(L" w:left=\"" + std::to_wstring(static_cast<int>(std::round(nLeftInd / 10.))) + L"\"");
}
else
{
const int nHanging{static_cast<int>(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<int>(std::round(nLeftInd / 10.)) + nHanging) + L"\"");
}
}
else if (0 != nLeftInd)
oBuilder.WriteString(L" w:left=\"" + std::to_wstring(static_cast<int>(std::round(oParagraphsStyle.GetLeftInd() / 10.))) + L"\"");
if (oParagraphsStyle.RightIndIsSet() && 0 != oParagraphsStyle.GetRightInd())
oBuilder.WriteString(L" w:right=\"" + std::to_wstring(static_cast<int>(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;
}
}

View File

@ -65,6 +65,15 @@ struct TLineSpacing
TLineSpacing& operator-=(const TLineSpacing& oLineSpacing);
};
struct TInd
{
CProperty<int> m_nFirstLine;
CProperty<int> m_nLeft;
CProperty<int> 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<bool> m_bKeepNext;
CProperty<int> m_nInd;
CProperty<EJs> m_eJs;
TInd m_oInd;
CProperty<EJs> m_eJs;
CProperty<ETextAlignment> m_eTextAlignment;
TLineSpacing m_oSpacing;
};

View File

@ -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;

View File

@ -91,6 +91,8 @@ public:
int GetMarginNext() const;
int GetIndent() const;
int GetLeftIndent() const;
int GetRightIndent() const;
bool KeepWithNext() const;
};