Files
core/OOXML/PPTXFormat/Logic/Path2D.cpp
Alexander Trofimov 76ee07f61c [copyright] Update copyright header
Co-authored-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com>
Co-committed-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com>
2026-05-14 08:23:56 +00:00

323 lines
8.3 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 "Path2D.h"
namespace PPTX
{
namespace Logic
{
Path2D& Path2D::operator=(const Path2D& oSrc)
{
parentFile = oSrc.parentFile;
parentElement = oSrc.parentElement;
extrusionOk = oSrc.extrusionOk;
fill = oSrc.fill;
h = oSrc.h;
stroke = oSrc.stroke;
w = oSrc.w;
Paths = oSrc.Paths;
return *this;
}
OOX::EElementType Path2D::getType() const
{
return OOX::et_a_path;
}
void Path2D::fromXML(XmlUtils::CXmlLiteReader& oReader)
{
ReadAttributes( oReader );
Paths.clear();
if ( oReader.IsEmptyNode() )
return;
int nParentDepth = oReader.GetDepth();
while( oReader.ReadNextSiblingNode( nParentDepth ) )
{
std::wstring sName = oReader.GetName();
UniPath2D p;
Paths.push_back(p);
Paths.back().fromXML(oReader);
}
FillParentPointersForChilds();
}
void Path2D::ReadAttributes(XmlUtils::CXmlLiteReader& oReader)
{
WritingElement_ReadAttributes_Start( oReader )
WritingElement_ReadAttributes_Read_if ( oReader, _T("extrusionOk"), extrusionOk)
WritingElement_ReadAttributes_Read_else_if( oReader, _T("fill"), fill )
WritingElement_ReadAttributes_Read_else_if( oReader, _T("h"), h )
WritingElement_ReadAttributes_Read_else_if( oReader, _T("stroke"), stroke )
WritingElement_ReadAttributes_Read_else_if( oReader, _T("w"), w )
WritingElement_ReadAttributes_End( oReader )
}
void Path2D::fromXML(XmlUtils::CXmlNode& node)
{
XmlMacroReadAttributeBase(node, L"extrusionOk", extrusionOk);
XmlMacroReadAttributeBase(node, L"fill", fill);
XmlMacroReadAttributeBase(node, L"h", h);
XmlMacroReadAttributeBase(node, L"stroke", stroke);
XmlMacroReadAttributeBase(node, L"w", w);
Paths.clear();
XmlMacroLoadArray(node, _T("*"), Paths, UniPath2D);
FillParentPointersForChilds();
}
std::wstring Path2D::toXML() const
{
XmlUtils::CAttribute oAttr;
oAttr.Write(_T("w"), w);
oAttr.Write(_T("h"), h);
oAttr.WriteLimitNullable(_T("fill"), fill);
oAttr.Write(_T("stroke"), stroke);
oAttr.Write(_T("extrusionOk"), extrusionOk);
XmlUtils::CNodeValue oValue;
oValue.WriteArray(Paths);
return XmlUtils::CreateNode(_T("a:path"), oAttr, oValue);
}
void Path2D::toXmlWriter(NSBinPptxRW::CXmlWriter* pWriter) const
{
pWriter->StartNode(_T("a:path"));
pWriter->StartAttributes();
pWriter->WriteAttribute(_T("w"), w);
pWriter->WriteAttribute(_T("h"), h);
pWriter->WriteAttribute(_T("fill"), fill);
pWriter->WriteAttribute(_T("stroke"), stroke);
pWriter->WriteAttribute(_T("extrusionOk"), extrusionOk);
pWriter->EndAttributes();
size_t nCount = Paths.size();
for (size_t i = 0; i < nCount; ++i)
Paths[i].toXmlWriter(pWriter);
pWriter->EndNode(_T("a:path"));
}
void Path2D::toPPTY(NSBinPptxRW::CBinaryFileWriter* pWriter) const
{
pWriter->WriteBYTE(NSBinPptxRW::g_nodeAttributeStart);
pWriter->WriteBool2(0, extrusionOk);
pWriter->WriteLimit2(1, fill);
pWriter->WriteInt2(2, h);
pWriter->WriteBool2(3, stroke);
pWriter->WriteInt2(4, w);
pWriter->WriteBYTE(NSBinPptxRW::g_nodeAttributeEnd);
pWriter->WriteRecordArray(0, 1, Paths);
}
void Path2D::fromPPTY(NSBinPptxRW::CBinaryFileReader* pReader)
{
LONG _e = 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:
{
extrusionOk = pReader->GetBool();
break;
}
case 1:
{
fill = pReader->GetUChar();
break;
}
case 2:
{
h = pReader->GetLong();
break;
}
case 3:
{
stroke = pReader->GetBool();
break;
}
case 4:
{
w = pReader->GetLong();
break;
}
default:
break;
}
}
while (pReader->GetPos() < _e)
{
BYTE _at = pReader->GetUChar();
switch (_at)
{
case 0:
{
pReader->Skip(4); // len
ULONG _c = pReader->GetULong();
for (ULONG j = 0; j < _c; ++j)
{
pReader->Skip(5); // type + len
BYTE _type = pReader->GetUChar();
LONG _end = pReader->GetPos() + pReader->GetRecordSize() + 4;
Paths.push_back(UniPath2D());
UniPath2D& oPath = Paths[j];
if (_type == GEOMETRY_TYPE_PATH_CLOZE)
{
Logic::Close* p = new Logic::Close();
oPath.Path2D.reset(p);
pReader->Seek(_end);
continue;
}
pReader->Skip(1);
std::wstring arr[6];
while (true)
{
BYTE _at2 = pReader->GetUChar_TypeNode();
if (_at2 == NSBinPptxRW::g_nodeAttributeEnd)
break;
arr[_at2] = pReader->GetString2();
}
switch (_type)
{
case GEOMETRY_TYPE_PATH_MOVETO:
{
Logic::MoveTo* p = new Logic::MoveTo();
p->x = arr[0];
p->y = arr[1];
oPath.Path2D.reset(p);
break;
}
case GEOMETRY_TYPE_PATH_LINETO:
{
Logic::LineTo* p = new Logic::LineTo();
p->x = arr[0];
p->y = arr[1];
oPath.Path2D.reset(p);
break;
}
case GEOMETRY_TYPE_PATH_CUBICBEZTO:
{
Logic::CubicBezTo* p = new Logic::CubicBezTo();
p->x[0] = arr[0];
p->y[0] = arr[1];
p->x[1] = arr[2];
p->y[1] = arr[3];
p->x[2] = arr[4];
p->y[2] = arr[5];
oPath.Path2D.reset(p);
break;
}
case GEOMETRY_TYPE_PATH_ARCTO:
{
Logic::ArcTo* p = new Logic::ArcTo();
p->wR = arr[0];
p->hR = arr[1];
p->stAng = arr[2];
p->swAng = arr[3];
oPath.Path2D.reset(p);
break;
}
case GEOMETRY_TYPE_PATH_QUADBEZTO:
{
Logic::QuadBezTo* p = new Logic::QuadBezTo();
p->x[0] = arr[0];
p->y[0] = arr[1];
p->x[1] = arr[2];
p->y[1] = arr[3];
oPath.Path2D.reset(p);
break;
}
default:
break;
}
pReader->Seek(_end);
}
break;
}
default:
break;
}
}
pReader->Seek(_e);
}
void Path2D::FillParentPointersForChilds()
{
size_t count = Paths.size();
for (size_t i = 0; i < count; ++i)
Paths[i].SetParentPointer(this);
}
std::wstring Path2D::GetODString()const
{
XmlUtils::CAttribute oAttr;
oAttr.Write(_T("w"), w);
oAttr.Write(_T("h"), h);
oAttr.WriteLimitNullable(_T("fill"), fill);
oAttr.Write(_T("stroke"), stroke);
oAttr.Write(_T("extrusionOk"), extrusionOk);
std::wstring strXml = _T("");
size_t nCount = Paths.size();
for (size_t i = 0; i < nCount; ++i)
strXml += Paths[i].GetODString();
return XmlUtils::CreateNode(_T("path"), oAttr, strXml);
}
} // namespace Logic
} // namespace PPTX