Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 :
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/awt/Size.hpp>
34 : #include <com/sun/star/drawing/PointSequence.hpp>
35 : #include <xmloff/xmlexp.hxx>
36 : #include "xmloff/xmlnmspe.hxx"
37 : #include <xmloff/xmltoken.hxx>
38 : #include <xmloff/XMLEventExport.hxx>
39 : #include <xmloff/xmluconv.hxx>
40 : #include "xexptran.hxx"
41 :
42 :
43 :
44 : using namespace ::com::sun::star;
45 : using namespace ::xmloff::token;
46 :
47 : using ::com::sun::star::uno::Any;
48 : using ::com::sun::star::uno::UNO_QUERY;
49 : using ::com::sun::star::uno::Sequence;
50 : using ::com::sun::star::uno::Reference;
51 : using ::com::sun::star::beans::XPropertySet;
52 : using ::com::sun::star::container::XIndexContainer;
53 : using ::com::sun::star::document::XEventsSupplier;
54 : using ::com::sun::star::lang::XServiceInfo;
55 : using ::com::sun::star::drawing::PointSequence;
56 :
57 :
58 : const sal_Char sAPI_ImageMapRectangleObject[] = "com.sun.star.image.ImageMapRectangleObject";
59 : const sal_Char sAPI_ImageMapCircleObject[] = "com.sun.star.image.ImageMapCircleObject";
60 : const sal_Char sAPI_ImageMapPolygonObject[] = "com.sun.star.image.ImageMapPolygonObject";
61 :
62 3 : XMLImageMapExport::XMLImageMapExport(SvXMLExport& rExp) :
63 : msBoundary("Boundary"),
64 : msCenter("Center"),
65 : msDescription("Description"),
66 : msImageMap("ImageMap"),
67 : msIsActive("IsActive"),
68 : msName("Name"),
69 : msPolygon("Polygon"),
70 : msRadius("Radius"),
71 : msTarget("Target"),
72 : msURL("URL"),
73 : msTitle("Title"),
74 : mrExport(rExp),
75 3 : mbWhiteSpace(sal_True)
76 : {
77 3 : }
78 :
79 3 : XMLImageMapExport::~XMLImageMapExport()
80 : {
81 :
82 3 : }
83 :
84 10 : void XMLImageMapExport::Export(
85 : const Reference<XPropertySet> & rPropertySet)
86 : {
87 10 : if (rPropertySet->getPropertySetInfo()->hasPropertyByName(msImageMap))
88 : {
89 10 : Any aAny = rPropertySet->getPropertyValue(msImageMap);
90 20 : Reference<XIndexContainer> aContainer;
91 10 : aAny >>= aContainer;
92 :
93 20 : Export(aContainer);
94 : }
95 : // else: no ImageMap property -> nothing to do
96 10 : }
97 :
98 10 : void XMLImageMapExport::Export(
99 : const Reference<XIndexContainer> & rContainer)
100 : {
101 10 : if (rContainer.is())
102 : {
103 10 : if (rContainer->hasElements())
104 : {
105 : // image map container element
106 : SvXMLElementExport aImageMapElement(
107 : mrExport, XML_NAMESPACE_DRAW, XML_IMAGE_MAP,
108 0 : mbWhiteSpace, mbWhiteSpace);
109 :
110 : // iterate over image map elements and call ExportMapEntry(...)
111 : // for each
112 0 : sal_Int32 nLength = rContainer->getCount();
113 0 : for(sal_Int32 i = 0; i < nLength; i++)
114 : {
115 0 : Any aAny = rContainer->getByIndex(i);
116 0 : Reference<XPropertySet> rElement;
117 0 : aAny >>= rElement;
118 :
119 : DBG_ASSERT(rElement.is(), "Image map element is empty!");
120 0 : if (rElement.is())
121 : {
122 0 : ExportMapEntry(rElement);
123 : }
124 0 : }
125 : }
126 : // else: container is empty -> nothing to do
127 : }
128 : // else: no container -> nothing to do
129 10 : }
130 :
131 :
132 0 : void XMLImageMapExport::ExportMapEntry(
133 : const Reference<XPropertySet> & rPropertySet)
134 : {
135 0 : Reference<XServiceInfo> xServiceInfo(rPropertySet, UNO_QUERY);
136 0 : if (xServiceInfo.is())
137 : {
138 0 : enum XMLTokenEnum eType = XML_TOKEN_INVALID;
139 :
140 : // distinguish map entries by their service name
141 : Sequence<OUString> sServiceNames =
142 0 : xServiceInfo->getSupportedServiceNames();
143 0 : sal_Int32 nLength = sServiceNames.getLength();
144 0 : for( sal_Int32 i=0; i<nLength; i++ )
145 : {
146 0 : OUString& rName = sServiceNames[i];
147 :
148 0 : if ( rName.equalsAsciiL(sAPI_ImageMapRectangleObject,
149 0 : sizeof(sAPI_ImageMapRectangleObject)-1) )
150 : {
151 0 : eType = XML_AREA_RECTANGLE;
152 0 : break;
153 : }
154 0 : else if ( rName.equalsAsciiL(sAPI_ImageMapCircleObject,
155 0 : sizeof(sAPI_ImageMapCircleObject)-1) )
156 : {
157 0 : eType = XML_AREA_CIRCLE;
158 0 : break;
159 : }
160 0 : else if ( rName.equalsAsciiL(sAPI_ImageMapPolygonObject,
161 0 : sizeof(sAPI_ImageMapPolygonObject)-1))
162 : {
163 0 : eType = XML_AREA_POLYGON;
164 0 : break;
165 : }
166 : }
167 :
168 : // return from method if no proper service is found!
169 : DBG_ASSERT(XML_TOKEN_INVALID != eType,
170 : "Image map element doesn't support appropriate service!");
171 0 : if (XML_TOKEN_INVALID == eType)
172 0 : return;
173 :
174 : // now: handle ImageMapObject properties (those for all types)
175 :
176 : // XLINK (URL property)
177 0 : Any aAny = rPropertySet->getPropertyValue(msURL);
178 0 : OUString sHref;
179 0 : aAny >>= sHref;
180 0 : if (!sHref.isEmpty())
181 : {
182 0 : mrExport.AddAttribute(XML_NAMESPACE_XLINK, XML_HREF, mrExport.GetRelativeReference(sHref));
183 : }
184 0 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
185 :
186 : // Target property (and xlink:show)
187 0 : aAny = rPropertySet->getPropertyValue(msTarget);
188 0 : OUString sTargt;
189 0 : aAny >>= sTargt;
190 0 : if (!sTargt.isEmpty())
191 : {
192 : mrExport.AddAttribute(
193 0 : XML_NAMESPACE_OFFICE, XML_TARGET_FRAME_NAME, sTargt);
194 :
195 : mrExport.AddAttribute(
196 : XML_NAMESPACE_XLINK, XML_SHOW,
197 0 : sTargt.equalsAsciiL( "_blank", sizeof("_blank")-1 )
198 0 : ? XML_NEW : XML_REPLACE );
199 : }
200 :
201 : // name
202 0 : aAny = rPropertySet->getPropertyValue(msName);
203 0 : OUString sItemName;
204 0 : aAny >>= sItemName;
205 0 : if (!sItemName.isEmpty())
206 : {
207 0 : mrExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_NAME, sItemName);
208 : }
209 :
210 : // is-active
211 0 : aAny = rPropertySet->getPropertyValue(msIsActive);
212 0 : if (! *(sal_Bool*)aAny.getValue())
213 : {
214 0 : mrExport.AddAttribute(XML_NAMESPACE_DRAW, XML_NOHREF, XML_NOHREF);
215 : }
216 :
217 : // call specific rectangle/circle/... method
218 : // also prepare element name
219 0 : switch (eType)
220 : {
221 : case XML_AREA_RECTANGLE:
222 0 : ExportRectangle(rPropertySet);
223 0 : break;
224 : case XML_AREA_CIRCLE:
225 0 : ExportCircle(rPropertySet);
226 0 : break;
227 : case XML_AREA_POLYGON:
228 0 : ExportPolygon(rPropertySet);
229 0 : break;
230 : default:
231 0 : break;
232 : }
233 :
234 : // write element
235 : DBG_ASSERT(XML_TOKEN_INVALID != eType,
236 : "No name?! How did this happen?");
237 : SvXMLElementExport aAreaElement(mrExport, XML_NAMESPACE_DRAW, eType,
238 0 : mbWhiteSpace, mbWhiteSpace);
239 :
240 : // title property (as <svg:title> element)
241 0 : OUString sTitle;
242 0 : rPropertySet->getPropertyValue(msTitle) >>= sTitle;
243 0 : if(!sTitle.isEmpty())
244 : {
245 0 : SvXMLElementExport aEventElemt(mrExport, XML_NAMESPACE_SVG, XML_TITLE, mbWhiteSpace, sal_False);
246 0 : mrExport.Characters(sTitle);
247 : }
248 :
249 : // description property (as <svg:desc> element)
250 0 : OUString sDescription;
251 0 : rPropertySet->getPropertyValue(msDescription) >>= sDescription;
252 0 : if (!sDescription.isEmpty())
253 : {
254 0 : SvXMLElementExport aDesc(mrExport, XML_NAMESPACE_SVG, XML_DESC, mbWhiteSpace, sal_False);
255 0 : mrExport.Characters(sDescription);
256 : }
257 :
258 : // export events attached to this
259 0 : Reference<XEventsSupplier> xSupplier(rPropertySet, UNO_QUERY);
260 0 : mrExport.GetEventExport().Export(xSupplier, mbWhiteSpace);
261 0 : }
262 : // else: no service info -> can't determine type -> ignore entry
263 : }
264 :
265 0 : void XMLImageMapExport::ExportRectangle(
266 : const Reference<XPropertySet> & rPropertySet)
267 : {
268 : // get boundary rectangle
269 0 : Any aAny = rPropertySet->getPropertyValue(msBoundary);
270 0 : awt::Rectangle aRectangle;
271 0 : aAny >>= aRectangle;
272 :
273 : // parameters svg:x, svg:y, svg:width, svg:height
274 0 : OUStringBuffer aBuffer;
275 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aRectangle.X);
276 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X,
277 0 : aBuffer.makeStringAndClear() );
278 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aRectangle.Y);
279 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y,
280 0 : aBuffer.makeStringAndClear() );
281 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer,
282 0 : aRectangle.Width);
283 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH,
284 0 : aBuffer.makeStringAndClear() );
285 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer,
286 0 : aRectangle.Height);
287 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT,
288 0 : aBuffer.makeStringAndClear() );
289 0 : }
290 :
291 0 : void XMLImageMapExport::ExportCircle(
292 : const Reference<XPropertySet> & rPropertySet)
293 : {
294 : // get boundary rectangle
295 0 : Any aAny = rPropertySet->getPropertyValue(msCenter);
296 0 : awt::Point aCenter;
297 0 : aAny >>= aCenter;
298 :
299 : // parameters svg:cx, svg:cy
300 0 : OUStringBuffer aBuffer;
301 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aCenter.X);
302 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CX,
303 0 : aBuffer.makeStringAndClear() );
304 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aCenter.Y);
305 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CY,
306 0 : aBuffer.makeStringAndClear() );
307 :
308 : // radius
309 0 : aAny = rPropertySet->getPropertyValue(msRadius);
310 0 : sal_Int32 nRadius = 0;
311 0 : aAny >>= nRadius;
312 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, nRadius);
313 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_R,
314 0 : aBuffer.makeStringAndClear() );
315 0 : }
316 :
317 0 : void XMLImageMapExport::ExportPolygon(
318 : const Reference<XPropertySet> & rPropertySet)
319 : {
320 : // polygons get exported as bounding box, viewbox, and coordinate
321 : // pair sequence. The bounding box is always the entire image.
322 :
323 : // get polygon point sequence
324 0 : Any aAny = rPropertySet->getPropertyValue(msPolygon);
325 0 : PointSequence aPoly;
326 0 : aAny >>= aPoly;
327 :
328 : // get bounding box (assume top-left to be 0,0)
329 0 : sal_Int32 nWidth = 0;
330 0 : sal_Int32 nHeight = 0;
331 0 : sal_Int32 nLength = aPoly.getLength();
332 0 : const struct awt::Point* pPointPtr = aPoly.getConstArray();
333 0 : for ( sal_Int32 i = 0; i < nLength; i++ )
334 : {
335 0 : sal_Int32 nPolyX = pPointPtr->X;
336 0 : sal_Int32 nPolyY = pPointPtr->Y;
337 :
338 0 : if ( nPolyX > nWidth )
339 0 : nWidth = nPolyX;
340 0 : if ( nPolyY > nHeight )
341 0 : nHeight = nPolyY;
342 :
343 0 : pPointPtr++;
344 : }
345 : DBG_ASSERT(nWidth > 0, "impossible Polygon found");
346 : DBG_ASSERT(nHeight > 0, "impossible Polygon found");
347 :
348 : // parameters svg:x, svg:y, svg:width, svg:height
349 0 : OUStringBuffer aBuffer;
350 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, 0);
351 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X,
352 0 : aBuffer.makeStringAndClear() );
353 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, 0);
354 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y,
355 0 : aBuffer.makeStringAndClear() );
356 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, nWidth);
357 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH,
358 0 : aBuffer.makeStringAndClear() );
359 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, nHeight);
360 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT,
361 0 : aBuffer.makeStringAndClear() );
362 :
363 : // svg:viewbox
364 0 : SdXMLImExViewBox aViewBox(0, 0, nWidth, nHeight);
365 : mrExport.AddAttribute(XML_NAMESPACE_SVG, XML_VIEWBOX,
366 0 : aViewBox.GetExportString());
367 :
368 : // export point sequence
369 0 : awt::Point aPoint(0, 0);
370 0 : awt::Size aSize(nWidth, nHeight);
371 0 : SdXMLImExPointsElement aPoints( &aPoly, aViewBox, aPoint, aSize );
372 : mrExport.AddAttribute( XML_NAMESPACE_DRAW, XML_POINTS,
373 0 : aPoints.GetExportString());
374 0 : }
375 :
376 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|