merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / draw / XMLImageMapExport.cxx
blob297531c613ddd3d5a51622091c0f82a3fba42754
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLImageMapExport.cxx,v $
10 * $Revision: 1.16 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include "XMLImageMapExport.hxx"
34 #include <rtl/ustring.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <tools/debug.hxx>
37 #include <com/sun/star/uno/Reference.h>
38 #include <com/sun/star/uno/Sequence.h>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/container/XIndexContainer.hpp>
43 #ifndef _COM_SUN_STAR_DOCUMENT_XEVENTSSUPPLIER_HPP
44 #include <com/sun/star/document/XEventsSupplier.hpp>
45 #endif
46 #include <com/sun/star/awt/Rectangle.hpp>
47 #include <com/sun/star/awt/Point.hpp>
48 #include <com/sun/star/awt/Size.hpp>
49 #include <com/sun/star/drawing/PointSequence.hpp>
50 #include <xmloff/xmlexp.hxx>
51 #include "xmlnmspe.hxx"
52 #include <xmloff/xmltoken.hxx>
53 #include <xmloff/XMLEventExport.hxx>
54 #include <xmloff/xmluconv.hxx>
55 #include "xexptran.hxx"
59 using namespace ::com::sun::star;
60 using namespace ::xmloff::token;
62 using ::rtl::OUString;
63 using ::rtl::OUStringBuffer;
64 using ::com::sun::star::uno::Any;
65 using ::com::sun::star::uno::UNO_QUERY;
66 using ::com::sun::star::uno::Sequence;
67 using ::com::sun::star::uno::Reference;
68 using ::com::sun::star::beans::XPropertySet;
69 using ::com::sun::star::container::XIndexContainer;
70 using ::com::sun::star::document::XEventsSupplier;
71 using ::com::sun::star::lang::XServiceInfo;
72 using ::com::sun::star::drawing::PointSequence;
75 const sal_Char sAPI_ImageMapRectangleObject[] = "com.sun.star.image.ImageMapRectangleObject";
76 const sal_Char sAPI_ImageMapCircleObject[] = "com.sun.star.image.ImageMapCircleObject";
77 const sal_Char sAPI_ImageMapPolygonObject[] = "com.sun.star.image.ImageMapPolygonObject";
79 XMLImageMapExport::XMLImageMapExport(SvXMLExport& rExp) :
80 msBoundary(RTL_CONSTASCII_USTRINGPARAM("Boundary")),
81 msCenter(RTL_CONSTASCII_USTRINGPARAM("Center")),
82 msDescription(RTL_CONSTASCII_USTRINGPARAM("Description")),
83 msImageMap(RTL_CONSTASCII_USTRINGPARAM("ImageMap")),
84 msIsActive(RTL_CONSTASCII_USTRINGPARAM("IsActive")),
85 msName(RTL_CONSTASCII_USTRINGPARAM("Name")),
86 msPolygon(RTL_CONSTASCII_USTRINGPARAM("Polygon")),
87 msRadius(RTL_CONSTASCII_USTRINGPARAM("Radius")),
88 msTarget(RTL_CONSTASCII_USTRINGPARAM("Target")),
89 msURL(RTL_CONSTASCII_USTRINGPARAM("URL")),
90 msTitle(RTL_CONSTASCII_USTRINGPARAM("Title")),
91 mrExport(rExp),
92 mbWhiteSpace(sal_True)
96 XMLImageMapExport::~XMLImageMapExport()
101 void XMLImageMapExport::Export(
102 const Reference<XPropertySet> & rPropertySet)
104 if (rPropertySet->getPropertySetInfo()->hasPropertyByName(msImageMap))
106 Any aAny = rPropertySet->getPropertyValue(msImageMap);
107 Reference<XIndexContainer> aContainer;
108 aAny >>= aContainer;
110 Export(aContainer);
112 // else: no ImageMap property -> nothing to do
115 void XMLImageMapExport::Export(
116 const Reference<XIndexContainer> & rContainer)
118 if (rContainer.is())
120 if (rContainer->hasElements())
122 // image map container element
123 SvXMLElementExport aImageMapElement(
124 mrExport, XML_NAMESPACE_DRAW, XML_IMAGE_MAP,
125 mbWhiteSpace, mbWhiteSpace);
127 // iterate over image map elements and call ExportMapEntry(...)
128 // for each
129 sal_Int32 nLength = rContainer->getCount();
130 for(sal_Int32 i = 0; i < nLength; i++)
132 Any aAny = rContainer->getByIndex(i);
133 Reference<XPropertySet> rElement;
134 aAny >>= rElement;
136 DBG_ASSERT(rElement.is(), "Image map element is empty!");
137 if (rElement.is())
139 ExportMapEntry(rElement);
143 // else: container is empty -> nothing to do
145 // else: no container -> nothign to do
149 void XMLImageMapExport::ExportMapEntry(
150 const Reference<XPropertySet> & rPropertySet)
152 Reference<XServiceInfo> xServiceInfo(rPropertySet, UNO_QUERY);
153 if (xServiceInfo.is())
155 enum XMLTokenEnum eType = XML_TOKEN_INVALID;
157 // distinguish map entries by their service name
158 Sequence<OUString> sServiceNames =
159 xServiceInfo->getSupportedServiceNames();
160 sal_Int32 nLength = sServiceNames.getLength();
161 for( sal_Int32 i=0; i<nLength; i++ )
163 OUString& rName = sServiceNames[i];
165 if ( rName.equalsAsciiL(sAPI_ImageMapRectangleObject,
166 sizeof(sAPI_ImageMapRectangleObject)-1) )
168 eType = XML_AREA_RECTANGLE;
169 break;
171 else if ( rName.equalsAsciiL(sAPI_ImageMapCircleObject,
172 sizeof(sAPI_ImageMapCircleObject)-1) )
174 eType = XML_AREA_CIRCLE;
175 break;
177 else if ( rName.equalsAsciiL(sAPI_ImageMapPolygonObject,
178 sizeof(sAPI_ImageMapPolygonObject)-1))
180 eType = XML_AREA_POLYGON;
181 break;
185 // return from method if no proper service is found!
186 DBG_ASSERT(XML_TOKEN_INVALID != eType,
187 "Image map element doesn't support appropriate service!");
188 if (XML_TOKEN_INVALID == eType)
189 return;
191 // now: handle ImageMapObject properties (those for all types)
193 // XLINK (URL property)
194 Any aAny = rPropertySet->getPropertyValue(msURL);
195 OUString sHref;
196 aAny >>= sHref;
197 if (sHref.getLength() > 0)
199 mrExport.AddAttribute(XML_NAMESPACE_XLINK, XML_HREF, mrExport.GetRelativeReference(sHref));
201 mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
203 // Target property (and xlink:show)
204 aAny = rPropertySet->getPropertyValue(msTarget);
205 OUString sTargt;
206 aAny >>= sTargt;
207 if (sTargt.getLength() > 0)
209 mrExport.AddAttribute(
210 XML_NAMESPACE_OFFICE, XML_TARGET_FRAME_NAME, sTargt);
212 mrExport.AddAttribute(
213 XML_NAMESPACE_XLINK, XML_SHOW,
214 sTargt.equalsAsciiL( "_blank", sizeof("_blank")-1 )
215 ? XML_NEW : XML_REPLACE );
218 // name
219 aAny = rPropertySet->getPropertyValue(msName);
220 OUString sItemName;
221 aAny >>= sItemName;
222 if (sItemName.getLength() > 0)
224 mrExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_NAME, sItemName);
227 // is-active
228 aAny = rPropertySet->getPropertyValue(msIsActive);
229 if (! *(sal_Bool*)aAny.getValue())
231 mrExport.AddAttribute(XML_NAMESPACE_DRAW, XML_NOHREF, XML_NOHREF);
234 // call specific rectangle/circle/... method
235 // also prepare element name
236 switch (eType)
238 case XML_AREA_RECTANGLE:
239 ExportRectangle(rPropertySet);
240 break;
241 case XML_AREA_CIRCLE:
242 ExportCircle(rPropertySet);
243 break;
244 case XML_AREA_POLYGON:
245 ExportPolygon(rPropertySet);
246 break;
247 default:
248 break;
251 // write element
252 DBG_ASSERT(XML_TOKEN_INVALID != eType,
253 "No name?! How did this happen?");
254 SvXMLElementExport aAreaElement(mrExport, XML_NAMESPACE_DRAW, eType,
255 mbWhiteSpace, mbWhiteSpace);
257 // title property (as <svg:title> element)
258 OUString sTitle;
259 rPropertySet->getPropertyValue(msTitle) >>= sTitle;
260 if(sTitle.getLength())
262 SvXMLElementExport aEventElemt(mrExport, XML_NAMESPACE_SVG, XML_TITLE, mbWhiteSpace, sal_False);
263 mrExport.Characters(sTitle);
266 // description property (as <svg:desc> element)
267 OUString sDescription;
268 rPropertySet->getPropertyValue(msDescription) >>= sDescription;
269 if (sDescription.getLength() > 0)
271 SvXMLElementExport aDesc(mrExport, XML_NAMESPACE_SVG, XML_DESC, mbWhiteSpace, sal_False);
272 mrExport.Characters(sDescription);
275 // export events attached to this
276 Reference<XEventsSupplier> xSupplier(rPropertySet, UNO_QUERY);
277 mrExport.GetEventExport().Export(xSupplier, mbWhiteSpace);
279 // else: no service info -> can't determine type -> ignore entry
282 void XMLImageMapExport::ExportRectangle(
283 const Reference<XPropertySet> & rPropertySet)
285 // get boundary rectangle
286 Any aAny = rPropertySet->getPropertyValue(msBoundary);
287 awt::Rectangle aRectangle;
288 aAny >>= aRectangle;
290 // parameters svg:x, svg:y, svg:width, svg:height
291 OUStringBuffer aBuffer;
292 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, aRectangle.X);
293 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X,
294 aBuffer.makeStringAndClear() );
295 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, aRectangle.Y);
296 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y,
297 aBuffer.makeStringAndClear() );
298 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, aRectangle.Width);
299 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH,
300 aBuffer.makeStringAndClear() );
301 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, aRectangle.Height);
302 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT,
303 aBuffer.makeStringAndClear() );
306 void XMLImageMapExport::ExportCircle(
307 const Reference<XPropertySet> & rPropertySet)
309 // get boundary rectangle
310 Any aAny = rPropertySet->getPropertyValue(msCenter);
311 awt::Point aCenter;
312 aAny >>= aCenter;
314 // parameters svg:cx, svg:cy
315 OUStringBuffer aBuffer;
316 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, aCenter.X);
317 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CX,
318 aBuffer.makeStringAndClear() );
319 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, aCenter.Y);
320 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CY,
321 aBuffer.makeStringAndClear() );
323 // radius
324 aAny = rPropertySet->getPropertyValue(msRadius);
325 sal_Int32 nRadius = 0;
326 aAny >>= nRadius;
327 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, nRadius);
328 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_R,
329 aBuffer.makeStringAndClear() );
332 void XMLImageMapExport::ExportPolygon(
333 const Reference<XPropertySet> & rPropertySet)
335 // polygons get exported as bounding box, viewbox, and coordinate
336 // pair sequence. The bounding box is always the entire image.
338 // get polygon point sequence
339 Any aAny = rPropertySet->getPropertyValue(msPolygon);
340 PointSequence aPoly;
341 aAny >>= aPoly;
343 // get bounding box (assume top-left to be 0,0)
344 sal_Int32 nWidth = 0;
345 sal_Int32 nHeight = 0;
346 sal_Int32 nLength = aPoly.getLength();
347 const struct awt::Point* pPointPtr = aPoly.getConstArray();
348 for ( sal_Int32 i = 0; i < nLength; i++ )
350 sal_Int32 nPolyX = pPointPtr->X;
351 sal_Int32 nPolyY = pPointPtr->Y;
353 if ( nPolyX > nWidth )
354 nWidth = nPolyX;
355 if ( nPolyY > nHeight )
356 nHeight = nPolyY;
358 pPointPtr++;
360 DBG_ASSERT(nWidth > 0, "impossible Polygon found");
361 DBG_ASSERT(nHeight > 0, "impossible Polygon found");
363 // parameters svg:x, svg:y, svg:width, svg:height
364 OUStringBuffer aBuffer;
365 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, 0);
366 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X,
367 aBuffer.makeStringAndClear() );
368 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, 0);
369 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y,
370 aBuffer.makeStringAndClear() );
371 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, nWidth);
372 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH,
373 aBuffer.makeStringAndClear() );
374 mrExport.GetMM100UnitConverter().convertMeasure(aBuffer, nHeight);
375 mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT,
376 aBuffer.makeStringAndClear() );
378 // svg:viewbox
379 SdXMLImExViewBox aViewBox(0, 0, nWidth, nHeight);
380 mrExport.AddAttribute(XML_NAMESPACE_SVG, XML_VIEWBOX,
381 aViewBox.GetExportString());
383 // export point sequence
384 awt::Point aPoint(0, 0);
385 awt::Size aSize(nWidth, nHeight);
386 SdXMLImExPointsElement aPoints( &aPoly, aViewBox, aPoint, aSize );
387 mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_POINTS,
388 aPoints.GetExportString());