Refactoring and fix bugs

This commit is contained in:
Kirill Polyakov
2024-03-15 15:57:57 +03:00
parent 53ca119a99
commit fb4c8ebf32
7 changed files with 120 additions and 103 deletions

View File

@ -390,20 +390,12 @@ namespace NSCSS
CASE(L"background-color"):
{
m_oBackground.SetColor(pPropertie.second, unLevel, bHardMode);
if (bIsThereBorder)
m_oBackground.InBorder();
break;
}
CASE(L"background"):
CASE(L"bgcolor"):
{
m_oBackground.SetBackground(pPropertie.second, unLevel, bHardMode);
if (bIsThereBorder)
m_oBackground.InBorder();
break;
}
//DISPLAY

View File

@ -471,9 +471,6 @@ namespace NSCSS
if ((CHECK_CONDITIONS && !bHardMode) || (wsValue.empty() && unLevel == m_unLevel))
return false;
if (8 == unLevel)
unLevel = 8;
if (wsValue.empty())
{
SetEmpty(unLevel);
@ -1192,7 +1189,7 @@ namespace NSCSS
}
// BACKGROUND
CBackground::CBackground() : m_bInBorder(false)
CBackground::CBackground()
{}
void CBackground::Equation(CBackground &oFirstBackground, CBackground &oSecondBackground)
@ -1218,30 +1215,24 @@ namespace NSCSS
return false;
}
void CBackground::InBorder()
{
m_bInBorder = true;
}
const CColor& CBackground::GetColor() const
{
return m_oColor;
}
bool CBackground::IsInBorder() const
{
return m_bInBorder;
}
bool CBackground::Empty() const
{
return m_oColor.Empty();
}
bool CBackground::IsNone() const
{
return ColorType::ColorNone == m_oColor.GetType();
}
CBackground &CBackground::operator=(const CBackground &oBackground)
{
m_oColor = oBackground.m_oColor;
m_bInBorder = oBackground.m_bInBorder;
return *this;
}
@ -1250,16 +1241,12 @@ namespace NSCSS
{
m_oColor += oBackground.m_oColor;
if (oBackground.m_bInBorder)
m_bInBorder = true;
return *this;
}
bool CBackground::operator==(const CBackground &oBackground) const
{
return m_oColor == oBackground.m_oColor &&
m_bInBorder == oBackground.m_bInBorder;
return m_oColor == oBackground.m_oColor;
}
// TRANSFORM

View File

@ -378,19 +378,17 @@ namespace NSCSS
bool SetColor (const std::wstring& wsValue, unsigned int unLevel, bool bHardMode = false);
bool SetBackground(const std::wstring& wsValue, unsigned int unLevel, bool bHardMode = false);
void InBorder();
const CColor& GetColor() const;
bool IsInBorder() const;
bool Empty() const;
bool Empty() const;
bool IsNone() const;
CBackground& operator =(const CBackground& oBackground);
CBackground& operator+=(const CBackground& oBackground);
bool operator==(const CBackground& oBackground) const;
private:
CColor m_oColor;
bool m_bInBorder;
};
class CTransform

View File

@ -74,8 +74,6 @@ namespace NSCSS
void CDocumentStyle::CombineStandardStyles(const std::vector<std::wstring>& arStandartedStyles, CXmlElement& oElement)
{
oElement.AddBasicProperties(CSSProperties::BasicProperties::B_BasedOn, L"normal");
if (arStandartedStyles.empty())
return;
@ -153,6 +151,7 @@ namespace NSCSS
if (!oParentStyle.Empty())
{
oParentStyle.AddBasicProperties(BProperties::B_BasedOn, L"normal");
oParentStyle.AddBasicProperties(BProperties::B_StyleId, L"(" + oParentStyle.GetStyleId() + L")");
if (!bIsPStyle)
{
@ -270,7 +269,8 @@ namespace NSCSS
void CDocumentStyle::SetPStyle (const NSCSS::CCompiledStyle& oStyle, CXmlElement& oXmlElement)
{
ConvertStyle(oStyle, oXmlElement, true);
if (oStyle.Empty() && oXmlElement.Empty())
if (oStyle.Empty())
return;
oXmlElement.AddPropertiesInP(PProperties::P_Jc, oStyle.m_oText.GetAlign().ToWString());
@ -317,12 +317,8 @@ namespace NSCSS
oXmlElement.AddPropertiesInP(PProperties::P_ContextualSpacing, L"true");
}
if (!oStyle.m_oBackground.Empty())
{
const std::wstring wsColor = oStyle.m_oBackground.GetColor().ToWString();
if (wsColor != L"ffffff")
oXmlElement.AddPropertiesInP(PProperties::P_Shd, wsColor);
}
if (!oStyle.m_oBackground.Empty() && oStyle.HaveThisParent(L"table"))
oXmlElement.AddPropertiesInP(PProperties::P_Shd, oStyle.m_oBackground.IsNone() ? L"auto" : oStyle.m_oBackground.GetColor().ToWString());
if (!oStyle.m_oBorder.Empty() && !oStyle.HaveThisParent(L"table"))
{
@ -431,12 +427,12 @@ namespace NSCSS
oXmlElement.AddPropertiesInR(RProperties::R_SmallCaps, oStyle.m_oFont.GetVariant().ToWString());
}
void CDocumentStyle::WriteRStyle (const NSCSS::CCompiledStyle& oStyle)
bool CDocumentStyle::WriteRStyle(const NSCSS::CCompiledStyle& oStyle)
{
if(oStyle.GetId().empty())
{
m_sId = L"normal";
return;
return false;
}
CStyleUsed structStyle(oStyle, false);
@ -446,49 +442,59 @@ namespace NSCSS
if (oItem != m_arStyleUsed.end())
{
m_sId = (*oItem).getId();
return;
return false;
}
CXmlElement oXmlElement;
SetRStyle(oStyle, oXmlElement);
if (!oStyle.Empty() || !oXmlElement.Empty())
{
structStyle.setId(oXmlElement.GetStyleId());
m_arStyleUsed.push_back(structStyle);
m_sStyle += oXmlElement.GetRStyle();
}
if (oXmlElement.Empty())
return false;
structStyle.setId(oXmlElement.GetStyleId());
m_arStyleUsed.push_back(structStyle);
m_sStyle += oXmlElement.GetRStyle();
return true;
}
void CDocumentStyle::WriteLitePStyle(const CCompiledStyle &oStyle)
bool CDocumentStyle::WriteLitePStyle(const CCompiledStyle &oStyle)
{
if (oStyle.Empty())
return;
return false;
CXmlElement oXmlElement;
SetPStyle(oStyle, oXmlElement);
if (!oXmlElement.Empty())
m_sStyle += oXmlElement.GetPStyle(true);
if (oXmlElement.Empty())
return false;
m_sStyle += oXmlElement.GetPStyle(true);
return true;
}
void CDocumentStyle::WriteLiteRStyle(const CCompiledStyle &oStyle)
bool CDocumentStyle::WriteLiteRStyle(const CCompiledStyle &oStyle)
{
if (oStyle.Empty())
return;
return false;
CXmlElement oXmlElement;
SetRStyle(oStyle, oXmlElement);
if (!oXmlElement.Empty())
m_sStyle += oXmlElement.GetRStyle(true);
if (oXmlElement.Empty())
return false;
m_sStyle += oXmlElement.GetRStyle(true);
return true;
}
void CDocumentStyle::WritePStyle (const NSCSS::CCompiledStyle& oStyle)
bool CDocumentStyle::WritePStyle(const NSCSS::CCompiledStyle& oStyle)
{
Clear();
if(oStyle.GetId().empty())
{
m_sId = L"normal";
return;
return true;
}
CStyleUsed structStyle(oStyle, true);
@ -497,17 +503,19 @@ namespace NSCSS
if (oItem != m_arStyleUsed.end())
{
m_sId = (*oItem).getId();
return;
return true;
}
CXmlElement oXmlElement;
SetPStyle(oStyle, oXmlElement);
if (!oStyle.Empty() || !oXmlElement.Empty())
{
structStyle.setId(oXmlElement.GetStyleId());
m_arStyleUsed.push_back(structStyle);
m_sStyle += oXmlElement.GetPStyle();
}
if (!oXmlElement.Empty())
return false;
structStyle.setId(oXmlElement.GetStyleId());
m_arStyleUsed.push_back(structStyle);
m_sStyle += oXmlElement.GetPStyle();
return true;
}
}

View File

@ -49,10 +49,10 @@ namespace NSCSS
CDocumentStyle();
~CDocumentStyle();
void WritePStyle(const NSCSS::CCompiledStyle& oStyle);
void WriteRStyle(const NSCSS::CCompiledStyle& oStyle);
void WriteLitePStyle(const NSCSS::CCompiledStyle& oStyle);
void WriteLiteRStyle(const NSCSS::CCompiledStyle& oStyle);
bool WritePStyle(const NSCSS::CCompiledStyle& oStyle);
bool WriteRStyle(const NSCSS::CCompiledStyle& oStyle);
bool WriteLitePStyle(const NSCSS::CCompiledStyle& oStyle);
bool WriteLiteRStyle(const NSCSS::CCompiledStyle& oStyle);
void SetStyle(const std::wstring& sStyle);
void SetId (const std::wstring& sId);

View File

@ -27,7 +27,7 @@ CXmlElement::CXmlElement(const std::wstring& sNameDefaultElement)
bool CXmlElement::Empty() const
{
return m_mBasicValues.empty() && m_mPStyleValues.empty() && m_mRStyleValues.empty();
return m_mPStyleValues.empty() && m_mRStyleValues.empty();
}
void CXmlElement::CreateDefaultElement(const std::wstring& sNameDefaultElement)

View File

@ -829,7 +829,9 @@ private:
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);
if ((bP && !m_oXmlStyle.WritePStyle(oStyle)) || (!bP && !m_oXmlStyle.WriteRStyle(oStyle)))
return L"";
m_oStylesXml.WriteString(m_oXmlStyle.GetStyle());
return m_oXmlStyle.GetIdAndClear();
}
@ -916,11 +918,16 @@ private:
oXml->WriteString(oTS.sPStyle);
oXml->WriteString(L"</w:pPr>");
}
oXml->WriteString(L"<w:r><w:rPr><w:rStyle w:val=\"");
oXml->WriteString(sRStyle);
oXml->WriteString(L"\"/>");
oXml->WriteString(oTS.sRStyle);
oXml->WriteString(L"</w:rPr><w:t xml:space=\"preserve\">");
oXml->WriteString(L"<w:r>");
if (!sRStyle.empty())
{
oXml->WriteString(L"<w:rPr><w:rStyle w:val=\"");
oXml->WriteString(sRStyle);
oXml->WriteString(L"\"/>");
oXml->WriteString(oTS.sRStyle);
oXml->WriteString(L"</w:rPr>");
}
oXml->WriteString(L"<w:t xml:space=\"preserve\">");
sText.erase(0, nAfter + 1);
nAfter = sText.find_first_of(L"\n\r");
}
@ -931,12 +938,12 @@ private:
if (std::iswspace(sText.front()) && m_bWasSpace)
sText.erase(0, 1);
oXml->WriteEncodeXmlString(sText);
oXml->WriteString(L"</w:t></w:r>");
if (!sText.empty())
m_bWasSpace = std::iswspace(sText.back());
oXml->WriteEncodeXmlString(sText);
oXml->WriteString(L"</w:t></w:r>");
return;
}
@ -1101,10 +1108,15 @@ private:
readStream(oXml, sSelectors, oTSR);
wrP(oXml, sSelectors, oTS);
oXml->WriteString(L"<w:r><w:rPr><w:rStyle w:val=\"");
oXml->WriteString(sRStyle);
oXml->WriteString(L"\"/>");
oXml->WriteString(oTS.sRStyle);
oXml->WriteString(L"<w:r>");
if (!sRStyle.empty())
{
oXml->WriteString(L"<w:rPr><w:rStyle w:val=\"");
oXml->WriteString(sRStyle);
oXml->WriteString(L"\"/>");
oXml->WriteString(oTS.sRStyle);
oXml->WriteString(L"</w:rPr>");
}
oXml->WriteString(L"</w:rPr><w:t xml:space=\"preserve\">&quot;</w:t></w:r>");
}
// Текст верхнего регистра
@ -1253,6 +1265,12 @@ private:
oTSPre.bPre = true;
readStream(oXml, sSelectors, oTSPre);
}
else if (sName == L"nobr")
{
CTextSettings oTSPre(oTS);
oTSPre.bPre = true;
readStream(oXml, sSelectors, oTSPre);
}
// Таблицы
else if(sName == L"table")
readTable(oXml, sSelectors, oTS);
@ -1377,10 +1395,11 @@ private:
while(m_oLightReader.MoveToNextAttribute())
{
if(m_oLightReader.GetName() == L"colspan")
nColspan = std::min((MAXCOLUMNSINTABLE - j + 1), NSStringFinder::ToInt(m_oLightReader.GetText(), 1));
nColspan = std::min((MAXCOLUMNSINTABLE - j), NSStringFinder::ToInt(m_oLightReader.GetText(), 1));
else if(m_oLightReader.GetName() == L"rowspan")
nRowspan = std::min((MAXROWSINTABLE - i + 1), NSStringFinder::ToInt(m_oLightReader.GetText(), 1));
nRowspan = std::min((MAXROWSINTABLE - i), NSStringFinder::ToInt(m_oLightReader.GetText(), 1));
}
m_oLightReader.MoveToElement();
// Вставляем ячейки до
@ -1418,7 +1437,7 @@ private:
NSCSS::CCompiledStyle::StyleEquation(oStyle, oStyleSetting);
if (!oStyle.m_oDisplay.GetHeight().Empty())
if (!oStyle.m_oDisplay.GetHeight().Empty() && 1 == nColspan && 1 == nRowspan)
nHeight = std::max(nHeight, oStyle.m_oDisplay.GetHeight().ToInt(NSCSS::Twips, DEFAULT_PAGE_HEIGHT));
std::wstring wsTcPr;
@ -1433,7 +1452,7 @@ private:
else
wsTcPr += L"<w:tcW w:w=\"0\" w:type=\"auto\"/>";
if(nColspan != 1)
if(nColspan > 1)
wsTcPr += L"<w:gridSpan w:val=\"" + std::to_wstring(nColspan) + L"\"/>";
if (!oStyle.m_oBorder.Zero())
@ -1445,8 +1464,11 @@ private:
}
if (!oStyle.m_oBackground.Empty() && !oStyle.m_oBackground.GetColor().Empty())
wsTcPr += L"<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"" + oStyle.m_oBackground.GetColor().ToWString() + L"\"/>";
{
const std::wstring wsShdFill{oStyle.m_oBackground.IsNone() ? L"auto" : oStyle.m_oBackground.GetColor().ToWString()};
wsTcPr += L"<w:shd w:val=\"clear\" w:color=\"auto\" w:fill=\"" + wsShdFill + L"\"/>";
}
std::wstring wsVerticalAlign = oStyle.m_oDisplay.GetVAlign().ToWString();
if (!wsVerticalAlign.empty())
@ -1475,7 +1497,7 @@ private:
"</w:tcMar>";
}
if(nRowspan != 1)
if(nRowspan > 1 && nColspan >= 1 && j < MAXCOLUMNSINTABLE)
{
oTrBody.WriteString(L"<w:vMerge w:val=\"restart\"/>");
std::wstring sColspan = std::to_wstring(nColspan);
@ -1486,7 +1508,7 @@ private:
mTable.push_back({k, j, sColspan, wsTcPr});
}
if(nColspan != 1)
if(nColspan > 1)
j += nColspan;
else
++j;
@ -1506,7 +1528,11 @@ private:
// Читаем td. Ячейка таблицы. Выравнивание вправо
else if(m_oLightReader.GetName() == L"td")
{
readStream(&oTrBody, sSelectors, oTS);
if (!readStream(&oTrBody, sSelectors, oTS))
{
oTrBody.WriteString(L"<w:p><w:pPr><w:rPr><w:sz w:val=\"20\"/><w:szCs w:val=\"20\"/></w:rPr></w:pPr><w:r><w:rPr><w:sz w:val=\"20\"/><w:szCs w:val=\"20\"/></w:rPr></w:r></w:p>");
m_bInP = false;
}
}
sSelectors.pop_back();
@ -1517,7 +1543,7 @@ private:
if (j - 1 == MAXCOLUMNSINTABLE)
{
while (m_oLightReader.ReadNextSiblingNode2(nTrDeath) && L"td" == m_oLightReader.GetName())
while (m_oLightReader.ReadNextSiblingNode(nTrDeath) && L"td" == m_oLightReader.GetName())
{
GetSubClass(&oTrBody, sSelectors);
@ -1527,6 +1553,7 @@ private:
readStream(&oTrBody, sSelectors, oTSTd);
sSelectors.pop_back();
}
}
CloseP(&oTrBody, sSelectors);
@ -2183,8 +2210,6 @@ private:
if (m_bWasPStyle)
return L"";
oXml->WriteString(L"<w:pPr><w:pStyle w:val=\"");
std::vector<std::pair<size_t, NSCSS::CNode>> temporary;
size_t i = 0;
while(i != sSelectors.size())
@ -2204,6 +2229,9 @@ private:
std::wstring sPStyle = GetStyle(oStyle, true);
if (sPStyle.empty())
return L"";
m_oXmlStyle.WriteLitePStyle(oStyleSetting);
std::wstring sPSettings = m_oXmlStyle.GetStyle();
m_oXmlStyle.Clear();
@ -2227,6 +2255,7 @@ private:
}
}
oXml->WriteString(L"<w:pPr><w:pStyle w:val=\"");
oXml->WriteString(sPStyle);
oXml->WriteString(L"\"/>");
oXml->WriteString(oTS.sPStyle + L' ' + sPSettings);
@ -2251,12 +2280,15 @@ private:
const std::wstring sRSettings = m_oXmlStyle.GetStyle();
m_oXmlStyle.Clear();
oXml->WriteString(L"<w:rPr><w:rStyle w:val=\"");
oXml->WriteString(sRStyle);
oXml->WriteString(L"\"/>");
oXml->WriteString(oTS.sRStyle + L' ' + sRSettings);
oXml->WriteString(L"</w:rPr>");
if (!sRStyle.empty())
{
oXml->WriteString(L"<w:rPr><w:rStyle w:val=\"");
oXml->WriteString(sRStyle);
oXml->WriteString(L"\"/>");
oXml->WriteString(oTS.sRStyle + L' ' + sRSettings);
oXml->WriteString(L"</w:rPr>");
}
return sRStyle;
}