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 OUStringLiteral
gsImageMap(u
"ImageMap");
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 XMLImageMapExport::~XMLImageMapExport()
78 void XMLImageMapExport::Export(
79 const Reference
<XPropertySet
> & rPropertySet
)
81 if (rPropertySet
->getPropertySetInfo()->hasPropertyByName(gsImageMap
))
83 Any aAny
= rPropertySet
->getPropertyValue(gsImageMap
);
84 Reference
<XIndexContainer
> aContainer
;
89 // else: no ImageMap property -> nothing to do
92 void XMLImageMapExport::Export(
93 const Reference
<XIndexContainer
> & rContainer
)
98 if (!rContainer
->hasElements())
101 // image map container element
102 SvXMLElementExport
aImageMapElement(
103 mrExport
, XML_NAMESPACE_DRAW
, XML_IMAGE_MAP
,
104 true/*bWhiteSpace*/, true/*bWhiteSpace*/);
106 // iterate over image map elements and call ExportMapEntry(...)
108 sal_Int32 nLength
= rContainer
->getCount();
109 for(sal_Int32 i
= 0; i
< nLength
; i
++)
111 Any aAny
= rContainer
->getByIndex(i
);
112 Reference
<XPropertySet
> rElement
;
115 DBG_ASSERT(rElement
.is(), "Image map element is empty!");
118 ExportMapEntry(rElement
);
121 // else: container is empty -> nothing to do
122 // else: no container -> nothing to do
126 void XMLImageMapExport::ExportMapEntry(
127 const Reference
<XPropertySet
> & rPropertySet
)
129 Reference
<XServiceInfo
> xServiceInfo(rPropertySet
, UNO_QUERY
);
130 if (!xServiceInfo
.is())
133 enum XMLTokenEnum eType
= XML_TOKEN_INVALID
;
135 // distinguish map entries by their service name
136 const Sequence
<OUString
> sServiceNames
=
137 xServiceInfo
->getSupportedServiceNames();
138 for( const OUString
& rName
: sServiceNames
)
140 if ( rName
== "com.sun.star.image.ImageMapRectangleObject" )
142 eType
= XML_AREA_RECTANGLE
;
145 else if ( rName
== "com.sun.star.image.ImageMapCircleObject" )
147 eType
= XML_AREA_CIRCLE
;
150 else if ( rName
== "com.sun.star.image.ImageMapPolygonObject" )
152 eType
= XML_AREA_POLYGON
;
157 // return from method if no proper service is found!
158 DBG_ASSERT(XML_TOKEN_INVALID
!= eType
,
159 "Image map element doesn't support appropriate service!");
160 if (XML_TOKEN_INVALID
== eType
)
163 // now: handle ImageMapObject properties (those for all types)
165 // XLINK (URL property)
166 Any aAny
= rPropertySet
->getPropertyValue(gsURL
);
169 if (!sHref
.isEmpty())
171 mrExport
.AddAttribute(XML_NAMESPACE_XLINK
, XML_HREF
, mrExport
.GetRelativeReference(sHref
));
173 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_TYPE
, XML_SIMPLE
);
175 // Target property (and xlink:show)
176 aAny
= rPropertySet
->getPropertyValue(gsTarget
);
179 if (!sTargt
.isEmpty())
181 mrExport
.AddAttribute(
182 XML_NAMESPACE_OFFICE
, XML_TARGET_FRAME_NAME
, sTargt
);
184 mrExport
.AddAttribute(
185 XML_NAMESPACE_XLINK
, XML_SHOW
,
186 sTargt
== "_blank" ? XML_NEW
: XML_REPLACE
);
190 aAny
= rPropertySet
->getPropertyValue(gsName
);
193 if (!sItemName
.isEmpty())
195 mrExport
.AddAttribute(XML_NAMESPACE_OFFICE
, XML_NAME
, sItemName
);
199 aAny
= rPropertySet
->getPropertyValue(gsIsActive
);
200 if (! *o3tl::doAccess
<bool>(aAny
))
202 mrExport
.AddAttribute(XML_NAMESPACE_DRAW
, XML_NOHREF
, XML_NOHREF
);
205 // call specific rectangle/circle/... method
206 // also prepare element name
209 case XML_AREA_RECTANGLE
:
210 ExportRectangle(rPropertySet
);
212 case XML_AREA_CIRCLE
:
213 ExportCircle(rPropertySet
);
215 case XML_AREA_POLYGON
:
216 ExportPolygon(rPropertySet
);
223 DBG_ASSERT(XML_TOKEN_INVALID
!= eType
,
224 "No name?! How did this happen?");
225 SvXMLElementExport
aAreaElement(mrExport
, XML_NAMESPACE_DRAW
, eType
,
226 true/*bWhiteSpace*/, true/*bWhiteSpace*/);
228 // title property (as <svg:title> element)
230 rPropertySet
->getPropertyValue(gsTitle
) >>= sTitle
;
231 if(!sTitle
.isEmpty())
233 SvXMLElementExport
aEventElemt(mrExport
, XML_NAMESPACE_SVG
, XML_TITLE
, true/*bWhiteSpace*/, false);
234 mrExport
.Characters(sTitle
);
237 // description property (as <svg:desc> element)
238 OUString sDescription
;
239 rPropertySet
->getPropertyValue(gsDescription
) >>= sDescription
;
240 if (!sDescription
.isEmpty())
242 SvXMLElementExport
aDesc(mrExport
, XML_NAMESPACE_SVG
, XML_DESC
, true/*bWhiteSpace*/, false);
243 mrExport
.Characters(sDescription
);
246 // export events attached to this
247 Reference
<XEventsSupplier
> xSupplier(rPropertySet
, UNO_QUERY
);
248 mrExport
.GetEventExport().Export(xSupplier
);
250 // else: no service info -> can't determine type -> ignore entry
253 void XMLImageMapExport::ExportRectangle(
254 const Reference
<XPropertySet
> & rPropertySet
)
256 // get boundary rectangle
257 Any aAny
= rPropertySet
->getPropertyValue(gsBoundary
);
258 awt::Rectangle aRectangle
;
261 // parameters svg:x, svg:y, svg:width, svg:height
262 OUStringBuffer aBuffer
;
263 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aRectangle
.X
);
264 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_X
,
265 aBuffer
.makeStringAndClear() );
266 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aRectangle
.Y
);
267 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_Y
,
268 aBuffer
.makeStringAndClear() );
269 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
,
271 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_WIDTH
,
272 aBuffer
.makeStringAndClear() );
273 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
,
275 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_HEIGHT
,
276 aBuffer
.makeStringAndClear() );
279 void XMLImageMapExport::ExportCircle(
280 const Reference
<XPropertySet
> & rPropertySet
)
282 // get boundary rectangle
283 Any aAny
= rPropertySet
->getPropertyValue(gsCenter
);
287 // parameters svg:cx, svg:cy
288 OUStringBuffer aBuffer
;
289 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aCenter
.X
);
290 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_CX
,
291 aBuffer
.makeStringAndClear() );
292 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, aCenter
.Y
);
293 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_CY
,
294 aBuffer
.makeStringAndClear() );
297 aAny
= rPropertySet
->getPropertyValue(gsRadius
);
298 sal_Int32 nRadius
= 0;
300 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, nRadius
);
301 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_R
,
302 aBuffer
.makeStringAndClear() );
305 void XMLImageMapExport::ExportPolygon(const Reference
<XPropertySet
> & rPropertySet
)
307 // polygons get exported as bounding box, viewbox, and coordinate
308 // pair sequence. The bounding box is always the entire image.
310 // get polygon point sequence
311 Any aAny
= rPropertySet
->getPropertyValue(gsPolygon
);
315 const basegfx::B2DPolygon
aPolygon(
316 basegfx::utils::UnoPointSequenceToB2DPolygon(
318 const basegfx::B2DRange
aPolygonRange(aPolygon
.getB2DRange());
320 // parameters svg:x, svg:y, svg:width, svg:height
321 OUStringBuffer aBuffer
;
323 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, 0);
324 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_X
, aBuffer
.makeStringAndClear() );
325 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, 0);
326 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_Y
, aBuffer
.makeStringAndClear() );
327 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, basegfx::fround(aPolygonRange
.getWidth()));
328 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_WIDTH
, aBuffer
.makeStringAndClear() );
329 mrExport
.GetMM100UnitConverter().convertMeasureToXML(aBuffer
, basegfx::fround(aPolygonRange
.getHeight()));
330 mrExport
.AddAttribute( XML_NAMESPACE_SVG
, XML_HEIGHT
, aBuffer
.makeStringAndClear() );
333 SdXMLImExViewBox
aViewBox(0.0, 0.0, aPolygonRange
.getWidth(), aPolygonRange
.getHeight());
334 mrExport
.AddAttribute(XML_NAMESPACE_SVG
, XML_VIEWBOX
, aViewBox
.GetExportString());
336 // export point sequence
337 const OUString
aPointString(
338 basegfx::utils::exportToSvgPoints(
341 mrExport
.AddAttribute(XML_NAMESPACE_DRAW
, XML_POINTS
, aPointString
);
344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */