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