Files
core/OOXML/XlsxFormat/Styles/dxf.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

389 lines
10 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 "dxf.h"
#include "Borders.h"
#include "Fills.h"
#include "Fonts.h"
#include "NumFmts.h"
#include "Xfs.h"
#include "../../XlsbFormat/Biff12_unions/DXF.h"
#include "../../XlsbFormat/Biff12_unions/FRTDXF.h"
#include "../../XlsbFormat/Biff12_records/CommonRecords.h"
#include "../../XlsbFormat/Biff12_records/BeginDXFs.h"
#include "../../Common/SimpleTypes_Shared.h"
#include <boost/algorithm/string.hpp>
#include "../../XlsbFormat/Biff12_unions/DXFS.h"
#include "../../../MsBinaryFile/XlsFile/Format/Logic/Biff_records/DXF.h"
namespace OOX
{
namespace Spreadsheet
{
CDxf::CDxf()
{
}
CDxf::~CDxf()
{
}
void CDxf::fromXML(XmlUtils::CXmlNode& node)
{
}
std::wstring CDxf::toXML() const
{
NSStringUtils::CStringBuilder writer;
toXML(writer);
return writer.GetData();
}
void CDxf::toXML(NSStringUtils::CStringBuilder& writer) const
{
toXML2(writer, L"dxf");
}
void CDxf::toXML2(NSStringUtils::CStringBuilder& writer, const std::wstring &node_name) const
{
toXMLWithNS(writer, L"", node_name, L"");
}
void CDxf::toXMLWithNS(NSStringUtils::CStringBuilder& writer, const std::wstring &node_ns, const std::wstring &node_name, const std::wstring &child_ns) const
{
writer.StartNodeWithNS(node_ns, node_name);
writer.StartAttributes();
writer.EndAttributes();
if(m_oFont.IsInit())
m_oFont->toXMLWithNS(writer, child_ns, L"font", child_ns);
if(m_oNumFmt.IsInit())
m_oNumFmt->toXMLWithNS(writer, child_ns, L"numFmt", child_ns);
if(m_oFill.IsInit())
m_oFill->toXMLWithNS(writer, child_ns, L"fill", child_ns);
if(m_oAlignment.IsInit())
m_oAlignment->toXMLWithNS(writer, child_ns, L"alignment", child_ns);
if(m_oBorder.IsInit())
m_oBorder->toXMLWithNS(writer, child_ns, L"border", child_ns);
if(m_oProtection.IsInit())
m_oProtection->toXMLWithNS(writer, child_ns, L"protection", child_ns);
writer.EndNodeWithNS(node_ns, node_name);
}
void CDxf::fromXML(XmlUtils::CXmlLiteReader& oReader)
{
ReadAttributes( oReader );
if ( oReader.IsEmptyNode() )
return;
int nCurDepth = oReader.GetDepth();
while( oReader.ReadNextSiblingNode( nCurDepth ) )
{
std::wstring sName = XmlUtils::GetNameNoNS(oReader.GetName());
if ( _T("alignment") == sName )
m_oAlignment = oReader;
else if ( _T("border") == sName )
m_oBorder = oReader;
else if ( _T("fill") == sName )
m_oFill = oReader;
else if ( _T("font") == sName )
m_oFont = oReader;
else if ( _T("numFmt") == sName )
m_oNumFmt = oReader;
else if ( _T("protection") == sName )
m_oProtection = oReader;
}
}
void CDxf::fromBin(XLS::BaseObjectPtr& obj)
{
if(obj->get_type() == XLS::typeDXF)
{
auto ptrDXF = static_cast<XLSB::DXF*>(obj.get());
if(ptrDXF != nullptr)
{
std::wstringstream strm;
ptrDXF->serialize(strm);
XmlUtils::CXmlLiteReader oReader;
std::wstring str = strm.str();
if ( !oReader.FromString(str))
return;
int nCurDepth = oReader.GetDepth();
while( oReader.ReadNextSiblingNode( nCurDepth ) )
{
std::wstring sName = XmlUtils::GetNameNoNS(oReader.GetName());
if ( _T("alignment") == sName )
m_oAlignment = oReader;
else if ( _T("border") == sName )
m_oBorder = oReader;
else if ( _T("fill") == sName )
m_oFill = oReader;
else if ( _T("font") == sName )
m_oFont = oReader;
else if ( _T("numFmt") == sName )
{
m_oNumFmt = oReader;
if(m_oNumFmt->m_oFormatCode.IsInit())
if(m_oNumFmt->m_oFormatCode.get().find(L"&quot;"))
boost::algorithm::replace_all(m_oNumFmt->m_oFormatCode.get(), L"&quot;", L"\"");
}
else if ( _T("protection") == sName )
m_oProtection = oReader;
}
}
}
else if(obj->get_type() == XLS::typeDXF14)
{
auto ptrDXF14 = static_cast<XLSB::DXF14*>(obj.get());
if(ptrDXF14 != nullptr)
{
std::wstringstream strm;
ptrDXF14->serialize(strm);
XmlUtils::CXmlLiteReader oReader;
std::wstring str = strm.str();
if ( !oReader.FromString(str))
return;
int nCurDepth = oReader.GetDepth();
while( oReader.ReadNextSiblingNode( nCurDepth ) )
{
std::wstring sName = XmlUtils::GetNameNoNS(oReader.GetName());
if ( _T("alignment") == sName )
m_oAlignment = oReader;
else if ( _T("border") == sName )
m_oBorder = oReader;
else if ( _T("fill") == sName )
m_oFill = oReader;
else if ( _T("font") == sName )
m_oFont = oReader;
else if ( _T("numFmt") == sName )
m_oNumFmt = oReader;
else if ( _T("protection") == sName )
m_oProtection = oReader;
}
}
}
}
XLS::BaseObjectPtr CDxf::toBin()
{
auto ptr(new XLSB::DXF);
auto ptr1(new XLSB::FRTDXF);
ptr1->m_BrtDXF = XLS::BaseObjectPtr{ptr};
XLS::BaseObjectPtr objectPtr(ptr1);
NSStringUtils::CStringBuilder writer;
toXML(writer);
XmlUtils::CXmlLiteReader oReader;
auto stringData = writer.GetData();
if ( !oReader.FromString(stringData))
return objectPtr;
ptr->deserialize(oReader);
return objectPtr;
}
XLS::BaseObjectPtr CDxf::toXLS()
{
auto ptr = new XLS::DXF;
XLS::BaseObjectPtr objectPtr(ptr);
NSStringUtils::CStringBuilder writer;
if(m_oNumFmt.IsInit() && m_oNumFmt->m_oFormatCode.IsInit() && m_oNumFmt->m_oNumFmtId.IsInit())
{
m_oNumFmt->m_oNumFmtId.reset();
}
toXML(writer);
XmlUtils::CXmlLiteReader oReader;
auto stringData = writer.GetData();
if ( !oReader.FromString(stringData))
return objectPtr;
ptr->deserialize(oReader);
return objectPtr;
}
EElementType CDxf::getType () const
{
return et_x_Dxf;
}
void CDxf::ReadAttributes(XmlUtils::CXmlLiteReader& oReader)
{
}
CDxfs::CDxfs()
{
}
CDxfs::~CDxfs()
{
}
void CDxfs::fromXML(XmlUtils::CXmlNode& node)
{
}
std::wstring CDxfs::toXML() const
{
return _T("");
}
void CDxfs::toXML(NSStringUtils::CStringBuilder& writer) const
{
writer.WriteString(_T("<dxfs"));
WritingStringNullableAttrInt(L"count", m_oCount, m_oCount->GetValue());
writer.WriteString(_T(">"));
for ( size_t i = 0; i < m_arrItems.size(); ++i)
{
if ( m_arrItems[i] )
{
m_arrItems[i]->toXML(writer);
}
}
writer.WriteString(_T("</dxfs>"));
}
void CDxfs::toXML2(NSStringUtils::CStringBuilder& writer, const std::wstring& sName) const
{
writer.WriteString(_T("<"));
writer.WriteString(sName);
WritingStringNullableAttrInt(L"count", m_oCount, m_oCount->GetValue());
writer.WriteString(_T(">"));
for ( size_t i = 0; i < m_arrItems.size(); ++i)
{
if ( m_arrItems[i] )
{
m_arrItems[i]->toXML(writer);
}
}
writer.WriteString(L"</");
writer.WriteString(sName);
writer.WriteString(L">");
}
void CDxfs::fromXML(XmlUtils::CXmlLiteReader& oReader)
{
ReadAttributes( oReader );
if ( oReader.IsEmptyNode() )
return;
int nCurDepth = oReader.GetDepth();
while( oReader.ReadNextSiblingNode( nCurDepth ) )
{
std::wstring sName = XmlUtils::GetNameNoNS(oReader.GetName());
if ( _T("dxf") == sName )
{
CDxf* pDxf = new CDxf();
*pDxf = oReader;
m_arrItems.push_back( pDxf );
}
}
}
EElementType CDxfs::getType () const
{
return et_x_Dxfs;
}
void CDxfs::fromBin(std::vector<XLS::BaseObjectPtr>& obj)
{
ReadAttributes(obj);
for(auto &dxf : obj)
{
if(dxf->get_type() == XLS::typeuDXF)
{
auto ptruDXF = static_cast<XLSB::uDXF*>(dxf.get());
if(ptruDXF && ptruDXF->m_BrtDXF != nullptr)
{
m_arrItems.push_back(new CDxf(ptruDXF->m_BrtDXF));
}
if(ptruDXF && ptruDXF->m_BrtFRTDXF != nullptr)
{
auto ptr = static_cast<XLSB::FRTDXF*>(ptruDXF->m_BrtFRTDXF.get())->m_BrtDXF;
m_arrItems.push_back(new CDxf(ptr));
}
}
else
m_arrItems.push_back(new CDxf(dxf));
}
}
XLS::BaseObjectPtr CDxfs::toBin()
{
auto ptr(new XLSB::DXFS);
XLS::BaseObjectPtr objectPtr(ptr);
auto ptr1(new XLSB::BeginDXFs);
ptr->m_BrtBeginDXFs = XLS::BaseObjectPtr{ptr1};
for(auto i:m_arrItems)
{
auto udxf(new XLSB::uDXF);
udxf->m_BrtFRTDXF = i->toBin();
ptr->m_aruDXF.push_back(XLS::BaseObjectPtr{udxf});
}
ptr1->cdxfs = ptr->m_aruDXF.size();
return objectPtr;
}
std::vector<XLS::BaseObjectPtr> CDxfs::toXLS()
{
std::vector<XLS::BaseObjectPtr> objectVector;
for(auto i : m_arrItems)
{
objectVector.push_back(i->toXLS());
}
return objectVector;
}
void CDxfs::ReadAttributes(XmlUtils::CXmlLiteReader& oReader)
{
WritingElement_ReadAttributes_Start( oReader )
WritingElement_ReadAttributes_Read_if ( oReader, _T("count"), m_oCount )
WritingElement_ReadAttributes_End( oReader )
}
void CDxfs::ReadAttributes(std::vector<XLS::BaseObjectPtr>& obj)
{
m_oCount = (_UINT32)obj.size();
}
} //Spreadsheet
} // namespace OOX