1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <editeng/memberids.h>
21 #include <vcl/graph.hxx>
23 #include <sal/log.hxx>
24 #include <xmloff/xmlnamespace.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/XMLBase64ImportContext.hxx>
27 #include <editeng/brushitem.hxx>
28 #include <xmloff/xmluconv.hxx>
30 #include "xmlbrshi.hxx"
31 #include "xmlbrshe.hxx"
33 #include "xmlimpit.hxx"
34 #include "xmlexpit.hxx"
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::xmloff::token
;
40 void SwXMLBrushItemImportContext::ProcessAttrs(
41 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
,
42 const SvXMLUnitConverter
& rUnitConv
)
44 for( auto &aIter
: sax_fastparser::castToFastAttributeList( xAttrList
) )
46 const OUString sValue
= aIter
.toString();
48 switch( aIter
.getToken() )
50 case XML_ELEMENT(XLINK
, XML_HREF
):
51 m_xGraphic
= GetImport().loadGraphicByURL(sValue
);
53 case XML_ELEMENT(XLINK
, XML_TYPE
):
54 case XML_ELEMENT(XLINK
, XML_ACTUATE
):
55 case XML_ELEMENT(XLINK
, XML_SHOW
):
57 case XML_ELEMENT(STYLE
, XML_POSITION
):
58 SvXMLImportItemMapper::PutXMLValue(
59 *m_pItem
, sValue
, MID_GRAPHIC_POSITION
, rUnitConv
);
61 case XML_ELEMENT(STYLE
, XML_REPEAT
):
62 SvXMLImportItemMapper::PutXMLValue(
63 *m_pItem
, sValue
, MID_GRAPHIC_REPEAT
, rUnitConv
);
65 case XML_ELEMENT(STYLE
, XML_FILTER_NAME
):
66 SvXMLImportItemMapper::PutXMLValue(
67 *m_pItem
, sValue
, MID_GRAPHIC_FILTER
, rUnitConv
);
70 XMLOFF_WARN_UNKNOWN("sw", aIter
);
76 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SwXMLBrushItemImportContext::createFastChildContext(
78 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& /*xAttrList*/ )
80 if ((nElement
& TOKEN_MASK
) == xmloff::token::XML_BINARY_DATA
)
82 if (!m_xBase64Stream
.is())
84 m_xBase64Stream
= GetImport().GetStreamForGraphicObjectURLFromBase64();
85 if (m_xBase64Stream
.is())
86 return new XMLBase64ImportContext(GetImport(), m_xBase64Stream
);
89 XMLOFF_WARN_UNKNOWN_ELEMENT("sw", nElement
);
93 void SwXMLBrushItemImportContext::endFastElement(sal_Int32
)
95 if (m_xBase64Stream
.is())
97 m_xGraphic
= GetImport().loadGraphicFromBase64(m_xBase64Stream
);
98 m_xBase64Stream
= nullptr;
103 Graphic
aGraphic(m_xGraphic
);
104 SvxGraphicPosition eOldGraphicPos
= m_pItem
->GetGraphicPos();
105 m_pItem
->SetGraphic(aGraphic
);
106 if (GPOS_NONE
== eOldGraphicPos
&& GPOS_NONE
!= m_pItem
->GetGraphicPos())
107 m_pItem
->SetGraphicPos(GPOS_TILED
);
110 if (!(m_pItem
->GetGraphic()))
111 m_pItem
->SetGraphicPos(GPOS_NONE
);
112 else if (GPOS_NONE
== m_pItem
->GetGraphicPos())
113 m_pItem
->SetGraphicPos(GPOS_TILED
);
116 SwXMLBrushItemImportContext::SwXMLBrushItemImportContext(
117 SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
118 const uno::Reference
< xml::sax::XFastAttributeList
>& xAttrList
,
119 const SvXMLUnitConverter
& rUnitConv
,
120 const SvxBrushItem
& rItem
) :
121 SvXMLImportContext( rImport
),
122 m_pItem( new SvxBrushItem( rItem
) )
124 // delete any graphic that is existing
125 m_pItem
->SetGraphicPos( GPOS_NONE
);
127 ProcessAttrs( xAttrList
, rUnitConv
);
130 SwXMLBrushItemImportContext::SwXMLBrushItemImportContext(
131 SvXMLImport
& rImport
, sal_Int32
/*nElement*/,
132 const uno::Reference
< xml::sax::XFastAttributeList
> & xAttrList
,
133 const SvXMLUnitConverter
& rUnitConv
,
134 sal_uInt16 nWhich
) :
135 SvXMLImportContext( rImport
),
136 m_pItem( new SvxBrushItem( nWhich
) )
138 ProcessAttrs( xAttrList
, rUnitConv
);
141 SwXMLBrushItemImportContext::~SwXMLBrushItemImportContext()
145 SwXMLBrushItemExport::SwXMLBrushItemExport( SwXMLExport
& rExp
) :
150 SwXMLBrushItemExport::~SwXMLBrushItemExport()
154 void SwXMLBrushItemExport::exportXML( const SvxBrushItem
& rItem
)
156 GetExport().CheckAttrList();
158 uno::Reference
<graphic::XGraphic
> xGraphic
;
160 const Graphic
* pGraphic
= rItem
.GetGraphic();
163 xGraphic
= pGraphic
->GetXGraphic();
168 OUString sValue
= GetExport().AddEmbeddedXGraphic(xGraphic
, sMimeType
);
169 if (!sValue
.isEmpty())
171 GetExport().AddAttribute( XML_NAMESPACE_XLINK
, XML_HREF
, sValue
);
172 GetExport().AddAttribute( XML_NAMESPACE_XLINK
, XML_TYPE
, XML_SIMPLE
);
173 GetExport().AddAttribute( XML_NAMESPACE_XLINK
, XML_ACTUATE
, XML_ONLOAD
);
176 const SvXMLUnitConverter
& rUnitConv
= GetExport().GetTwipUnitConverter();
177 if (SvXMLExportItemMapper::QueryXMLValue(rItem
, sValue
, MID_GRAPHIC_POSITION
, rUnitConv
))
178 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_POSITION
, sValue
);
180 if (SvXMLExportItemMapper::QueryXMLValue(rItem
, sValue
, MID_GRAPHIC_REPEAT
, rUnitConv
))
181 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_REPEAT
, sValue
);
183 if (SvXMLExportItemMapper::QueryXMLValue(rItem
, sValue
, MID_GRAPHIC_FILTER
, rUnitConv
))
184 GetExport().AddAttribute( XML_NAMESPACE_STYLE
, XML_FILTER_NAME
, sValue
);
188 SvXMLElementExport
aElem( GetExport(), XML_NAMESPACE_STYLE
, XML_BACKGROUND_IMAGE
,
192 // optional office:binary-data
193 GetExport().AddEmbeddedXGraphicAsBase64(xGraphic
);
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */