Files
core/OOXML/PPTXFormat/Logic/Ln.cpp
2026-05-19 16:46:09 +03:00

456 lines
11 KiB
C++

/*
* Copyright (C) Ascensio System SIA, 2009-2026
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation, together with the
* additional terms provided in the LICENSE file.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA by email at info@onlyoffice.com
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
* LV-1050, Latvia, European Union.
*
* The interactive user interfaces in modified versions of the Program
* are required to display Appropriate Legal Notices in accordance with
* Section 5 of the GNU AGPL version 3.
*
* No trademark rights are granted under this License.
*
* All non-code elements of the Product, including illustrations,
* icon sets, and technical writing content, are licensed under the
* Creative Commons Attribution-ShareAlike 4.0 International License:
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
*
* This license applies only to such non-code elements and does not
* modify or replace the licensing terms applicable to the Program's
* source code, which remains licensed under the GNU Affero General
* Public License v3.
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "Ln.h"
#include "Colors/SchemeClr.h"
#include "../../../MsBinaryFile/XlsFile/Format/Logic/Biff_records/LineFormat.h"
namespace PPTX
{
namespace Logic
{
Ln::Ln()
{
}
void Ln::fromXML(XmlUtils::CXmlLiteReader& oReader)
{
m_name = oReader.GetName();
ReadAttributes( oReader );
if ( oReader.IsEmptyNode() )
return;
int nCurDepth = oReader.GetDepth();
while ( oReader.ReadNextSiblingNode( nCurDepth ) )
{
std::wstring sName = XmlUtils::GetNameNoNS(oReader.GetName());
if (L"bevel" == sName ||
L"miter" == sName ||
L"round" == sName )
{
join = oReader;
}
else if ( L"tailEnd" == sName )
tailEnd = oReader;
else if ( L"headEnd" == sName )
headEnd = oReader;
else if ( L"gradFill" == sName ||
L"noFill" == sName ||
L"pattFill" == sName ||
L"solidFill"== sName ||
L"blipFill" == sName)
{
Fill.fromXML(oReader);
}
else if ( L"custDash" == sName )
{
//custDash = oReader;
//m_eDashType = OOX::Drawing::linedashtypeCustom;
}
else if ( L"prstDash" == sName )
{
prstDash = oReader;
//m_eDashType = OOX::Drawing::linedashtypePreset;
}
else if ( L"effectDag" == sName ||
L"effectLst" == sName)
{
Effects.fromXML(oReader);
}
else if ( L"extLst" == sName )
{
if ( oReader.IsEmptyNode() )
continue;
int nParentDepth1 = oReader.GetDepth();
while( oReader.ReadNextSiblingNode( nParentDepth1 ) )
{
Ext element;
element.fromXML(oReader);
extLst.push_back (element);
}
}
}
FillParentPointersForChilds();
}
void Ln::fromXML(XmlUtils::CXmlNode& node)
{
m_name = node.GetName();
XmlMacroReadAttributeBase(node, L"algn", algn);
XmlMacroReadAttributeBase(node, L"cap", cap);
XmlMacroReadAttributeBase(node, L"cmpd", cmpd);
XmlMacroReadAttributeBase(node, L"w", w);
std::vector<XmlUtils::CXmlNode> oNodes;
if (node.GetNodes(L"*", oNodes))
{
size_t count = oNodes.size();
for (size_t i = 0; i < count; ++i)
{
XmlUtils::CXmlNode& oNode = oNodes[i];
std::wstring strName = XmlUtils::GetNameNoNS(oNode.GetName());
if (L"headEnd" == strName)
{
headEnd = oNode;
}
else if (L"tailEnd" == strName)
{
tailEnd = oNode;
}
else if (L"round" == strName || L"bevel" == strName || L"miter" == strName)
{
join = oNode;
}
else if (L"prstDash" == strName)
{
prstDash = oNode;
}
else if (L"blipFill" == strName ||
L"gradFill" == strName ||
L"grpFill" == strName ||
L"noFill" == strName ||
L"pattFill" == strName ||
L"solidFill" == strName)
{
Fill.fromXML(oNode);
}
else if (L"effectDag" == strName ||
L"effectLst" == strName)
{
Effects.fromXML(oNode);
}
}
}
FillParentPointersForChilds();
}
void Ln::ReadAttributes(XmlUtils::CXmlLiteReader& oReader)
{
WritingElement_ReadAttributes_Start_No_NS( oReader )
WritingElement_ReadAttributes_Read_if ( oReader, L"algn", algn )
WritingElement_ReadAttributes_Read_else_if( oReader, L"cap", cap )
WritingElement_ReadAttributes_Read_else_if( oReader, L"cmpd", cmpd )
WritingElement_ReadAttributes_Read_else_if( oReader, L"w", w )
WritingElement_ReadAttributes_End_No_NS( oReader )
Normalize();
}
std::wstring Ln::toXML() const
{
std::wstring _name = m_name;
if (_name.empty())
_name = L"a:ln";
std::wstring sAttrNamespace;
if (_name == L"w14:textOutline")
sAttrNamespace = L"w14:";
XmlUtils::CAttribute oAttr;
oAttr.Write (sAttrNamespace + L"w", w);
oAttr.WriteLimitNullable(sAttrNamespace + L"cap", cap);
oAttr.WriteLimitNullable(sAttrNamespace + L"cmpd", cmpd);
oAttr.WriteLimitNullable(sAttrNamespace + L"algn", algn);
XmlUtils::CNodeValue oValue;
oValue.Write(Fill);
oValue.WriteNullable(prstDash);
oValue.WriteNullable(join);
oValue.WriteNullable(headEnd);
oValue.WriteNullable(tailEnd);
return XmlUtils::CreateNode(_name, oAttr, oValue);
}
void Ln::toXmlWriter(NSBinPptxRW::CXmlWriter* pWriter) const
{
std::wstring _name = m_name;
if (_name.empty())
_name = L"a:ln";
std::wstring sAttrNamespace;
if (XMLWRITER_DOC_TYPE_WORDART == pWriter->m_lDocType)
{
_name = L"w14:textOutline";
sAttrNamespace = L"w14:";
}
pWriter->StartNode(_name);
pWriter->StartAttributes();
pWriter->WriteAttribute(sAttrNamespace + L"w", w);
pWriter->WriteAttribute(sAttrNamespace + L"cap", cap);
pWriter->WriteAttribute(sAttrNamespace + L"cmpd", cmpd);
pWriter->WriteAttribute(sAttrNamespace + L"algn", algn);
pWriter->EndAttributes();
Fill.toXmlWriter(pWriter);
pWriter->Write(prstDash);
pWriter->Write(join);
pWriter->Write(headEnd);
pWriter->Write(tailEnd);
pWriter->EndNode(_name);
}
void Ln::toPPTY(NSBinPptxRW::CBinaryFileWriter* pWriter) const
{
pWriter->WriteBYTE(NSBinPptxRW::g_nodeAttributeStart);
pWriter->WriteLimit2(0, algn);
pWriter->WriteLimit2(1, cap);
pWriter->WriteLimit2(2, cmpd);
pWriter->WriteInt2(3, w);
pWriter->WriteBYTE(NSBinPptxRW::g_nodeAttributeEnd);
pWriter->WriteRecord1(0, Fill);
pWriter->WriteRecord2(1, prstDash);
pWriter->WriteRecord2(2, join);
pWriter->WriteRecord2(3, headEnd);
pWriter->WriteRecord2(4, tailEnd);
}
void Ln::fromPPTY(NSBinPptxRW::CBinaryFileReader* pReader)
{
m_name = L"a:ln";
LONG _end_rec = pReader->GetPos() + pReader->GetRecordSize() + 4;
pReader->Skip(1); // start attributes
while (true)
{
BYTE _at = pReader->GetUChar_TypeNode();
if (_at == NSBinPptxRW::g_nodeAttributeEnd)
break;
switch (_at)
{
case 0:
{
algn = pReader->GetUChar();
break;
}
case 1:
{
cap = pReader->GetUChar();
break;
}
case 2:
{
cmpd = pReader->GetUChar();
break;
}
case 3:
{
w = pReader->GetLong();
break;
}
default:
break;
}
}
while (pReader->GetPos() < _end_rec)
{
BYTE _at = pReader->GetUChar();
switch (_at)
{
case 0:
{
Fill.fromPPTY(pReader);
}break;
case 1:
{
prstDash = new Logic::PrstDash();
prstDash->fromPPTY(pReader);
}break;
case 2:
{
join.Init();
join->fromPPTY(pReader);
}break;
case 3:
{
headEnd = new Logic::LineEnd();
headEnd->m_name = L"a:headEnd";
headEnd->fromPPTY(pReader);
}break;
case 4:
{
tailEnd = new Logic::LineEnd();
tailEnd->m_name = L"a:tailEnd";
tailEnd->fromPPTY(pReader);
}break;
default:
break;
}
}
pReader->Seek(_end_rec);
}
XLS::BaseObjectPtr Ln::toXLS()
{
auto ptr = new XLS::LineFormat;
if(w.IsInit())
{
switch(w.get())
{
case 3175: ptr->we = 0xFFFF; break; //Hairline
case 12700: ptr->we = 0; break; //single
case 25400: ptr->we = 1; break; //double
case 38100: ptr->we = 2; break; //triple
}
}
if(Fill.is_init())
{
if(Fill.m_type == UniFill::Type::noFill)
ptr->lns = 5;
else if(prstDash.IsInit() && prstDash->val.IsInit())
{
if(Fill.m_type == UniFill::Type::solidFill)
{
auto solid = dynamic_cast<PPTX::Logic::SolidFill*>(Fill.Fill.GetPointer());
if(solid->Color.Color.IsInit() && solid->Color.is<PPTX::Logic::SchemeClr>())
{
auto scheme = static_cast<PPTX::Logic::SchemeClr*>( solid->Color.Color.GetPointer());
if(scheme->val.get() == L"tx1")
{
ptr->icv = 12;
}
else if(scheme->val.get() == L"bg1")
{
ptr->icv= 0x4E;
ptr->rgb.blue = 0xff;
ptr->rgb.red = 0xff;
ptr->rgb.green = 0xff;
}
else if(scheme->val.get() == L"accent1")
{
ptr->icv = 0x30;
ptr->rgb.blue = 0xC4;
ptr->rgb.red = 0x44;
ptr->rgb.green = 0x72;
}
else if(scheme->val.get() == L"accent2")
{
ptr->icv = 0x35;
ptr->rgb.red = 0xed;
ptr->rgb.green = 0x7d;
ptr->rgb.blue = 0x31;
}
}
}
if(prstDash->val->get() == L"dash")
ptr->lns = 1;
else if(prstDash->val->get() == L"dot")
ptr->lns = 2;
else if(prstDash->val->get() == L"dashDot")
ptr->lns = 3;
else if(prstDash->val->get() == L"sysDashDotDot")
ptr->lns = 4;
else
ptr->lns = 0;
}
else
ptr->lns = 0;
}
return XLS::BaseObjectPtr(ptr);
}
void Ln::Merge(nullable<Ln>& line)const
{
if(!line.IsInit())
line = Ln();
if(Fill.is_init())
line->Fill = Fill;//.fromXML(Fill.toXML());
if(prstDash.is_init())
line->prstDash = *prstDash;
if(join.is_init())
line->join = join;
if(headEnd.is_init())
headEnd->Merge(line->headEnd);
if(tailEnd.is_init())
tailEnd->Merge(line->tailEnd);
if(algn.is_init())
line->algn = *algn;
if(cap.is_init())
line->cap = *cap;
if(cmpd.is_init())
line->cmpd = *cmpd;
if(w.is_init())
line->w = *w;
}
void Ln::Merge(Ln& line)const
{
if(Fill.is_init())
line.Fill = Fill;//.fromXML(Fill.toXML());
if(prstDash.is_init())
line.prstDash = *prstDash;
if(join.is_init())
line.join = join;
if(headEnd.is_init())
headEnd->Merge(line.headEnd);
if(tailEnd.is_init())
tailEnd->Merge(line.tailEnd);
if(algn.is_init())
line.algn = *algn;
if(cap.is_init())
line.cap = *cap;
if(cmpd.is_init())
line.cmpd = *cmpd;
if(w.is_init())
line.w = *w;
}
void Ln::FillParentPointersForChilds()
{
Fill.SetParentPointer(this);
if (join.IsInit())
join->SetParentPointer(this);
if(prstDash.IsInit())
prstDash->SetParentPointer(this);
if(headEnd.IsInit())
headEnd->SetParentPointer(this);
if(tailEnd.IsInit())
tailEnd->SetParentPointer(this);
}
void Ln::Normalize()
{
w.normalize(0, 20116800);
}
} // namespace Logic
} // namespace PPTX