bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / draw / XMLImageMapExport.cxx
blob9ea9b5863e88fba9cce29fb2c04e08d7dadbc4d0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <rtl/ustring.hxx>
22 #include <rtl/ustrbuf.hxx>
23 #include <tools/debug.hxx>
24 #include <com/sun/star/uno/Reference.h>
25 #include <com/sun/star/uno/Sequence.h>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/container/XIndexContainer.hpp>
29 #include <com/sun/star/document/XEventsSupplier.hpp>
30 #include <com/sun/star/awt/Rectangle.hpp>
31 #include <com/sun/star/awt/Point.hpp>
32 #include <com/sun/star/awt/Size.hpp>
33 #include <com/sun/star/drawing/PointSequence.hpp>
34 #include <xmloff/xmlexp.hxx>
35 #include <xmloff/xmlnmspe.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 XMLImageMapExport::XMLImageMapExport(SvXMLExport& rExp) :
57 msBoundary("Boundary"),
58 msCenter("Center"),
59 msDescription("Description"),
60 msImageMap("ImageMap"),
61 msIsActive("IsActive"),
62 msName("Name"),
63 msPolygon("Polygon"),
64 msRadius("Radius"),
65 msTarget("Target"),
66 msURL("URL"),
67 msTitle("Title"),
68 mrExport(rExp),
69 mbWhiteSpace(true)
73 XMLImageMapExport::~XMLImageMapExport()
78 void XMLImageMapExport::Export(
79 const Reference<XPropertySet> & rPropertySet)
81 if (rPropertySet->getPropertySetInfo()->hasPropertyByName(msImageMap))
83 Any aAny = rPropertySet->getPropertyValue(msImageMap);
84 Reference<XIndexContainer> aContainer;
85 aAny >>= aContainer;
87 Export(aContainer);
89 // else: no ImageMap property -> nothing to do
92 void XMLImageMapExport::Export(
93 const Reference<XIndexContainer> & rContainer)
95 if (rContainer.is())
97 if (rContainer->hasElements())
99 // image map container element
100 SvXMLElementExport aImageMapElement(
101 mrExport, XML_NAMESPACE_DRAW, XML_IMAGE_MAP,
102 mbWhiteSpace, mbWhiteSpace);
104 // iterate over image map elements and call ExportMapEntry(...)
105 // for each
106 sal_Int32 nLength = rContainer->getCount();
107 for(sal_Int32 i = 0; i < nLength; i++)
109 Any aAny = rContainer->getByIndex(i);
110 Reference<XPropertySet> rElement;
111 aAny >>= rElement;
113 DBG_ASSERT(rElement.is(), "Image map element is empty!");
114 if (rElement.is())
116 ExportMapEntry(rElement);
120 // 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())
132 enum XMLTokenEnum eType = XML_TOKEN_INVALID;
134 // distinguish map entries by their service name
135 Sequence<OUString> sServiceNames =
136 xServiceInfo->getSupportedServiceNames();
137 sal_Int32 nLength = sServiceNames.getLength();
138 for( sal_Int32 i=0; i<nLength; i++ )
140 OUString& rName = sServiceNames[i];
142 if ( rName == "com.sun.star.image.ImageMapRectangleObject" )
144 eType = XML_AREA_RECTANGLE;
145 break;
147 else if ( rName == "com.sun.star.image.ImageMapCircleObject" )
149 eType = XML_AREA_CIRCLE;
150 break;
152 else if ( rName == "com.sun.star.image.ImageMapPolygonObject" )
154 eType = XML_AREA_POLYGON;
155 break;
159 // return from method if no proper service is found!
160 DBG_ASSERT(XML_TOKEN_INVALID != eType,
161 "Image map element doesn't support appropriate service!");
162 if (XML_TOKEN_INVALID == eType)
163 return;
165 // now: handle ImageMapObject properties (those for all types)
167 // XLINK (URL property)
168 Any aAny = rPropertySet->getPropertyValue(msURL);
169 OUString sHref;
170 aAny >>= sHref;
171 if (!sHref.isEmpty())
173 mrExport.AddAttribute(XML_NAMESPACE_XLINK, XML_HREF, mrExport.GetRelativeReference(sHref));
175 mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
177 // Target property (and xlink:show)
178 aAny = rPropertySet->getPropertyValue(msTarget);
179 OUString sTargt;
180 aAny >>= sTargt;
181 if (!sTargt.isEmpty())
183 mrExport.AddAttribute(
184 XML_NAMESPACE_OFFICE, XML_TARGET_FRAME_NAME, sTargt);
186 mrExport.AddAttribute(
187 XML_NAMESPACE_XLINK, XML_SHOW,
188 sTargt == "_blank" ? XML_NEW : XML_REPLACE );
191 // name
192 aAny = rPropertySet->getPropertyValue(msName);
193 OUString sItemName;
194 aAny >>= sItemName;
195 if (!sItemName.isEmpty())
197 mrExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_NAME, sItemName);
200 // is-active
201 aAny = rPropertySet->getPropertyValue(msIsActive);
202 if (! *static_cast<sal_Bool const *>(aAny.getValue()))
204 mrExport.AddAttribute(XML_NAMESPACE_DRAW, XML_NOHREF, XML_NOHREF);
207 // call specific rectangle/circle/... method
208 // also prepare element name
209 switch (eType)
211 case XML_AREA_RECTANGLE:
212 ExportRectangle(rPropertySet);
213 break;
214 case XML_AREA_CIRCLE:
215 ExportCircle(rPropertySet);
216 break;
217 case XML_AREA_POLYGON:
218 ExportPolygon(rPropertySet);
219 break;
220 default:
221 break;
224 // write element
225 DBG_ASSERT(XML_TOKEN_INVALID != eType,
226 "No name?! How did this happen?");
227 SvXMLElementExport aAreaElement(mrExport, XML_NAMESPACE_DRAW, eType,
228 mbWhiteSpace, mbWhiteSpace);
230 // title property (as <svg:title> element)
231 OUString sTitle;
232 rPropertySet->getPropertyValue(msTitle) >>= sTitle;
233 if(!sTitle.isEmpty())
235 SvXMLElementExport aEventElemt(mrExport, XML_NAMESPACE_SVG, XML_TITLE, mbWhiteSpace, false);
236 mrExport.Characters(sTitle);
239 // description property (as <svg:desc> element)
240 OUString sDescription;
241 rPropertySet->getPropertyValue(msDescription) >>= sDescription;
242 if (!sDescription.isEmpty())
244 SvXMLElementExport aDesc(mrExport, XML_NAMESPACE_SVG, XML_DESC, mbWhiteSpace, false);
245 mrExport.Characters(sDescription);
248 // export events attached to this
249 Reference<XEventsSupplier> xSupplier(rPropertySet, UNO_QUERY);
250 mrExport.GetEventExport().Export(xSupplier, mbWhiteSpace);
252 // else: no service info -> can't determine type -> ignore entry
255 void XMLImageMapExport::ExportRectangle(
256 const Reference<XPropertySet> & rPropertySet)
258 // get boundary rectangle
259 Any aAny = rPropertySet->getPropertyValue(msBoundary);
260 awt::Rectangle aRectangle;
261 aAny >>= aRectangle;
263 // parameters svg:x, svg:y, svg:width, svg:height
264 OUStringBuffer aBuffer;
265 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aRectangle.X);
266 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X,
267 aBuffer.makeStringAndClear() );
268 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aRectangle.Y);
269 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y,
270 aBuffer.makeStringAndClear() );
271 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer,
272 aRectangle.Width);
273 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH,
274 aBuffer.makeStringAndClear() );
275 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer,
276 aRectangle.Height);
277 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT,
278 aBuffer.makeStringAndClear() );
281 void XMLImageMapExport::ExportCircle(
282 const Reference<XPropertySet> & rPropertySet)
284 // get boundary rectangle
285 Any aAny = rPropertySet->getPropertyValue(msCenter);
286 awt::Point aCenter;
287 aAny >>= aCenter;
289 // parameters svg:cx, svg:cy
290 OUStringBuffer aBuffer;
291 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aCenter.X);
292 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CX,
293 aBuffer.makeStringAndClear() );
294 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aCenter.Y);
295 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CY,
296 aBuffer.makeStringAndClear() );
298 // radius
299 aAny = rPropertySet->getPropertyValue(msRadius);
300 sal_Int32 nRadius = 0;
301 aAny >>= nRadius;
302 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, nRadius);
303 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_R,
304 aBuffer.makeStringAndClear() );
307 void XMLImageMapExport::ExportPolygon(const Reference<XPropertySet> & rPropertySet)
309 // polygons get exported as bounding box, viewbox, and coordinate
310 // pair sequence. The bounding box is always the entire image.
312 // get polygon point sequence
313 Any aAny = rPropertySet->getPropertyValue(msPolygon);
314 PointSequence aPoly;
315 aAny >>= aPoly;
317 const basegfx::B2DPolygon aPolygon(
318 basegfx::tools::UnoPointSequenceToB2DPolygon(
319 aPoly));
320 const basegfx::B2DRange aPolygonRange(aPolygon.getB2DRange());
322 // parameters svg:x, svg:y, svg:width, svg:height
323 OUStringBuffer aBuffer;
325 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, 0);
326 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X, aBuffer.makeStringAndClear() );
327 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, 0);
328 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y, aBuffer.makeStringAndClear() );
329 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, basegfx::fround(aPolygonRange.getWidth()));
330 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH, aBuffer.makeStringAndClear() );
331 mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, basegfx::fround(aPolygonRange.getHeight()));
332 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT, aBuffer.makeStringAndClear() );
334 // svg:viewbox
335 SdXMLImExViewBox aViewBox(0.0, 0.0, aPolygonRange.getWidth(), aPolygonRange.getHeight());
336 mrExport.AddAttribute(XML_NAMESPACE_SVG, XML_VIEWBOX, aViewBox.GetExportString());
338 // export point sequence
339 const OUString aPointString(
340 basegfx::tools::exportToSvgPoints(
341 aPolygon));
343 mrExport.AddAttribute(XML_NAMESPACE_DRAW, XML_POINTS, aPointString);
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */