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 <XMLImageMapExport.hxx>
21 #include <o3tl/any.hxx>
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <tools/debug.hxx>
25 #include <com/sun/star/uno/Reference.h>
26 #include <com/sun/star/uno/Sequence.h>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/container/XIndexContainer.hpp>
30 #include <com/sun/star/document/XEventsSupplier.hpp>
31 #include <com/sun/star/awt/Rectangle.hpp>
32 #include <com/sun/star/awt/Point.hpp>
33 #include <com/sun/star/drawing/PointSequence.hpp>
34 #include <xmloff/xmlexp.hxx>
35 #include <xmloff/xmlnamespace.hxx>
36 #include <xmloff/xmltoken.hxx>
37 #include <xmloff/XMLEventExport.hxx>
38 #include <xmloff/xmluconv.hxx>
39 #include <xexptran.hxx>
40 #include <basegfx/polygon/b2dpolygon.hxx>
41 #include <basegfx/polygon/b2dpolygontools.hxx>
43 using namespace ::com::sun::star
;
44 using namespace ::xmloff::token
;
46 using ::com::sun::star::uno::Any
;
47 using ::com::sun::star::uno::UNO_QUERY
;
48 using ::com::sun::star::uno::Sequence
;
49 using ::com::sun::star::uno::Reference
;
50 using ::com::sun::star::beans::XPropertySet
;
51 using ::com::sun::star::container::XIndexContainer
;
52 using ::com::sun::star::document::XEventsSupplier
;
53 using ::com::sun::star::lang::XServiceInfo
;
54 using ::com::sun::star::drawing::PointSequence
;
56 constexpr OUStringLiteral
gsBoundary(u
"Boundary");
57 constexpr OUStringLiteral
gsCenter(u
"Center");
58 constexpr OUStringLiteral
gsDescription(u
"Description");
59 constexpr OUString
gsImageMap(u
"ImageMap"_ustr
);
60 constexpr OUStringLiteral
gsIsActive(u
"IsActive");
61 constexpr OUStringLiteral
gsName(u
"Name");
62 constexpr OUStringLiteral
gsPolygon(u
"Polygon");
63 constexpr OUStringLiteral
gsRadius(u
"Radius");
64 constexpr OUStringLiteral
gsTarget(u
"Target");
65 constexpr OUStringLiteral
gsURL(u
"URL");
66 constexpr OUStringLiteral
gsTitle(u
"Title");
68 XMLImageMapExport::XMLImageMapExport(SvXMLExport
& rExp
) :
73 void XMLImageMapExport::Export(
74 const Reference
<XPropertySet
> & rPropertySet
)
76 if (rPropertySet
->getPropertySetInfo()->hasPropertyByName(gsImageMap
))
78 Any aAny
= rPropertySet
->getPropertyValue(gsImageMap
);
79 Reference
<XIndexContainer
> aContainer
;
84 // else: no ImageMap property -> nothing to do
87 void XMLImageMapExport::Export(
88 const Reference
<XIndexContainer
> & rContainer
)
93 if (!rContainer
->hasElements())
96 // image map container element
97 SvXMLElementExport
aImageMapElement(
98 mrExport
, XML_NAMESPACE_DRAW
, XML_IMAGE_MAP
,
99 true/*bWhiteSpace*/, true/*bWhiteSpace*/);
101 // iterate over image map elements and call ExportMapEntry(...)
103 sal_Int32 nLength
= rContainer
->getCount();
104 for(sal_Int32 i
= 0; i
< nLength
; i
++)
106 Any aAny
= rContainer
->getByIndex(i
);
107 Reference
<XPropertySet
> rElement
;
110 DBG_ASSERT(rElement
.is(), "Image map element is empty!");
113 ExportMapEntry(rElement
);
116 // else: container is empty -> nothing to do
117 // else: no container -> nothing to do
121 void XMLImageMapExport::ExportMapEntry(
122 const Reference
<XPropertySet
> & rPropertySet
)
124 Reference
<XServiceInfo
> xServiceInfo(rPropertySet
, UNO_QUERY
);
125 if (!xServiceInfo
.is())
128 enum XMLTokenEnum eType
= XML_TOKEN_INVALID
;
130 // distinguish map entries by their service name
131 const Sequence
<OUString
> sServiceNames
=
132 xServiceInfo
->getSupportedServiceNames();
133 for( const OUString
& rName
: sServiceNames
)
135 if ( rName
== "com.sun.star.image.ImageMapRectangleObject" )
137 eType
= XML_AREA_RECTANGLE
;
140 else if ( rName
== "com.sun.star.image.ImageMapCircleObject" )
142 eType
= XML_AREA_CIRCLE
;
145 else if ( rName
== "com.sun.star.image.ImageMapPolygonObject" )
147 eType
= XML_AREA_POLYGON
;
152 // return from method if no proper service is found!
153 DBG_ASSERT(XML_TOKEN_INVALID
!= eType
,
154 "Image map element doesn't support appropriate service!");
155 if (XML_TOKEN_INVALID
== eType
)
158 // now: handle ImageMapObject properties (those for all types)
160 // XLINK (URL property)
161 Any aAny
= rPropertySet
->getPropertyValue(gsURL
);
164 if (!sHref
.isEmpty())
166 mrExport
.AddAttribute(XML_NAMESPACE_XLINK
, XML_HREF
, mrExport
.GetRelativeReference(sHref
));
168 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_TYPE
, XML_SIMPLE
);
170 // Target property (and xlink:show)
171 aAny
= rPropertySet
->getPropertyValue(gsTarget
);
174 if (!sTargt
.isEmpty())
176 mrExport
.AddAttribute(
177 XML_NAMESPACE_OFFICE
, XML_TARGET_FRAME_NAME
, sTargt
);
179 mrExport
.AddAttribute(
180 XML_NAMESPACE_XLINK
, XML_SHOW
,
181 sTargt
== "_blank" ? XML_NEW
: XML_REPLACE
);
185 aAny
= rPropertySet
->getPropertyValue(gsName
);
188 if (!sItemName
.isEmpty())
190 mrExport
.AddAttribute(XML_NAMESPACE_OFFICE
, XML_NAME
, sItemName
);
194 aAny
= rPropertySet
->getPropertyValue(gsIsActive
);
195 if (! *o3tl::doAccess
<bool>(aAny
))
197 mrExport
.AddAttribute(XML_NAMESPACE_DRAW
, XML_NOHREF
, XML_NOHREF
);
200 // call specific rectangle/circle/... method
201 // also prepare element name
204 case XML_AREA_RECTANGLE
:
205 ExportRectangle(rPropertySet
);
207 case XML_AREA_CIRCLE
:
208 ExportCircle(rPropertySet
);
210 case XML_AREA_POLYGON
:
211 ExportPolygon(rPropertySet
);
218 DBG_ASSERT(XML_TOKEN_INVALID
!= eType
,
219 "No name?! How did this happen?");
220 SvXMLElementExport
aAreaElement(mrExport
, XML_NAMESPACE_DRAW
, eType
,
221 true/*bWhiteSpace*/, true/*bWhiteSpace*/);
223 // title property (as <svg:title> element)
225 rPropertySet
->getPropertyValue(gsTitle
) >>= sTitle
;
226 if(!sTitle
.isEmpty())
228 SvXMLElementExport
aEventElemt(mrExport
, XML_NAMESPACE_SVG
, XML_TITLE
, true/*bWhiteSpace*/, false);
229 mrExport
.Characters(sTitle
);
232 // description property (as <svg:desc> element)
233 OUString sDescription
;
234 rPropertySet
->getPropertyValue(gsDescription
) >>= sDescription
;
235 if (!sDescription
.isEmpty())
237 SvXMLElementExport
aDesc(mrExport
, XML_NAMESPACE_SVG
, XML_DESC
, true/*bWhiteSpace*/, false);
238 mrExport
.Characters(sDescription
);
241 // export events attached to this
242 Reference
<XEventsSupplier
> xSupplier(rPropertySet
, UNO_QUERY
);
243 mrExport
.GetEventExport().Export(xSupplier
);
245 // else: no service info -> can't determine type -> ignore entry
248 void XMLImageMapExport::ExportRectangle(
249 const Reference
<XPropertySet
> & rPropertySet
)
251 // get boundary rectangle
252 Any aAny
= rPropertySet
->getPropertyValue(gsBoundary
);
253 awt::Rectangle aRectangle
;
256 // parameters svg:x, svg:y, svg:width, svg:height
257 OUStringBuffer aBuffer
;
258 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aRectangle
.X
);
259 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_X
,
260 aBuffer
.makeStringAndClear() );
261 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aRectangle
.Y
);
262 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_Y
,
263 aBuffer
.makeStringAndClear() );
264 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
,
266 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_WIDTH
,
267 aBuffer
.makeStringAndClear() );
268 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
,
270 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_HEIGHT
,
271 aBuffer
.makeStringAndClear() );
274 void XMLImageMapExport::ExportCircle(
275 const Reference
<XPropertySet
> & rPropertySet
)
277 // get boundary rectangle
278 Any aAny
= rPropertySet
->getPropertyValue(gsCenter
);
282 // parameters svg:cx, svg:cy
283 OUStringBuffer aBuffer
;
284 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aCenter
.X
);
285 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_CX
,
286 aBuffer
.makeStringAndClear() );
287 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aCenter
.Y
);
288 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_CY
,
289 aBuffer
.makeStringAndClear() );
292 aAny
= rPropertySet
->getPropertyValue(gsRadius
);
293 sal_Int32 nRadius
= 0;
295 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, nRadius
);
296 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_R
,
297 aBuffer
.makeStringAndClear() );
300 void XMLImageMapExport::ExportPolygon(const Reference
<XPropertySet
> & rPropertySet
)
302 // polygons get exported as bounding box, viewbox, and coordinate
303 // pair sequence. The bounding box is always the entire image.
305 // get polygon point sequence
306 Any aAny
= rPropertySet
->getPropertyValue(gsPolygon
);
310 const basegfx::B2DPolygon
aPolygon(
311 basegfx::utils::UnoPointSequenceToB2DPolygon(
313 const basegfx::B2DRange
aPolygonRange(aPolygon
.getB2DRange());
315 // parameters svg:x, svg:y, svg:width, svg:height
316 OUStringBuffer aBuffer
;
318 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, 0);
319 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_X
, aBuffer
.makeStringAndClear() );
320 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, 0);
321 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_Y
, aBuffer
.makeStringAndClear() );
322 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, basegfx::fround(aPolygonRange
.getWidth()));
323 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_WIDTH
, aBuffer
.makeStringAndClear() );
324 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, basegfx::fround(aPolygonRange
.getHeight()));
325 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_HEIGHT
, aBuffer
.makeStringAndClear() );
328 SdXMLImExViewBox
aViewBox(0.0, 0.0, aPolygonRange
.getWidth(), aPolygonRange
.getHeight());
329 mrExport
.AddAttribute(XML_NAMESPACE_SVG
, XML_VIEWBOX
, aViewBox
.GetExportString());
331 // export point sequence
332 const OUString
aPointString(
333 basegfx::utils::exportToSvgPoints(
336 mrExport
.AddAttribute(XML_NAMESPACE_DRAW
, XML_POINTS
, aPointString
);
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */