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 : #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>
42 :
43 : using namespace ::com::sun::star;
44 : using namespace ::xmloff::token;
45 :
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;
55 :
56 23 : 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 23 : mbWhiteSpace(true)
70 : {
71 23 : }
72 :
73 23 : XMLImageMapExport::~XMLImageMapExport()
74 : {
75 :
76 23 : }
77 :
78 87 : void XMLImageMapExport::Export(
79 : const Reference<XPropertySet> & rPropertySet)
80 : {
81 87 : if (rPropertySet->getPropertySetInfo()->hasPropertyByName(msImageMap))
82 : {
83 84 : Any aAny = rPropertySet->getPropertyValue(msImageMap);
84 168 : Reference<XIndexContainer> aContainer;
85 84 : aAny >>= aContainer;
86 :
87 168 : Export(aContainer);
88 : }
89 : // else: no ImageMap property -> nothing to do
90 87 : }
91 :
92 84 : void XMLImageMapExport::Export(
93 : const Reference<XIndexContainer> & rContainer)
94 : {
95 84 : if (rContainer.is())
96 : {
97 84 : if (rContainer->hasElements())
98 : {
99 : // image map container element
100 : SvXMLElementExport aImageMapElement(
101 : mrExport, XML_NAMESPACE_DRAW, XML_IMAGE_MAP,
102 0 : mbWhiteSpace, mbWhiteSpace);
103 :
104 : // iterate over image map elements and call ExportMapEntry(...)
105 : // for each
106 0 : sal_Int32 nLength = rContainer->getCount();
107 0 : for(sal_Int32 i = 0; i < nLength; i++)
108 : {
109 0 : Any aAny = rContainer->getByIndex(i);
110 0 : Reference<XPropertySet> rElement;
111 0 : aAny >>= rElement;
112 :
113 : DBG_ASSERT(rElement.is(), "Image map element is empty!");
114 0 : if (rElement.is())
115 : {
116 0 : ExportMapEntry(rElement);
117 : }
118 0 : }
119 : }
120 : // else: container is empty -> nothing to do
121 : }
122 : // else: no container -> nothing to do
123 84 : }
124 :
125 :
126 0 : void XMLImageMapExport::ExportMapEntry(
127 : const Reference<XPropertySet> & rPropertySet)
128 : {
129 0 : Reference<XServiceInfo> xServiceInfo(rPropertySet, UNO_QUERY);
130 0 : if (xServiceInfo.is())
131 : {
132 0 : enum XMLTokenEnum eType = XML_TOKEN_INVALID;
133 :
134 : // distinguish map entries by their service name
135 : Sequence<OUString> sServiceNames =
136 0 : xServiceInfo->getSupportedServiceNames();
137 0 : sal_Int32 nLength = sServiceNames.getLength();
138 0 : for( sal_Int32 i=0; i<nLength; i++ )
139 : {
140 0 : OUString& rName = sServiceNames[i];
141 :
142 0 : if ( rName == "com.sun.star.image.ImageMapRectangleObject" )
143 : {
144 0 : eType = XML_AREA_RECTANGLE;
145 0 : break;
146 : }
147 0 : else if ( rName == "com.sun.star.image.ImageMapCircleObject" )
148 : {
149 0 : eType = XML_AREA_CIRCLE;
150 0 : break;
151 : }
152 0 : else if ( rName == "com.sun.star.image.ImageMapPolygonObject" )
153 : {
154 0 : eType = XML_AREA_POLYGON;
155 0 : break;
156 : }
157 : }
158 :
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 0 : if (XML_TOKEN_INVALID == eType)
163 0 : return;
164 :
165 : // now: handle ImageMapObject properties (those for all types)
166 :
167 : // XLINK (URL property)
168 0 : Any aAny = rPropertySet->getPropertyValue(msURL);
169 0 : OUString sHref;
170 0 : aAny >>= sHref;
171 0 : if (!sHref.isEmpty())
172 : {
173 0 : mrExport.AddAttribute(XML_NAMESPACE_XLINK, XML_HREF, mrExport.GetRelativeReference(sHref));
174 : }
175 0 : mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
176 :
177 : // Target property (and xlink:show)
178 0 : aAny = rPropertySet->getPropertyValue(msTarget);
179 0 : OUString sTargt;
180 0 : aAny >>= sTargt;
181 0 : if (!sTargt.isEmpty())
182 : {
183 : mrExport.AddAttribute(
184 0 : XML_NAMESPACE_OFFICE, XML_TARGET_FRAME_NAME, sTargt);
185 :
186 : mrExport.AddAttribute(
187 : XML_NAMESPACE_XLINK, XML_SHOW,
188 0 : sTargt == "_blank" ? XML_NEW : XML_REPLACE );
189 : }
190 :
191 : // name
192 0 : aAny = rPropertySet->getPropertyValue(msName);
193 0 : OUString sItemName;
194 0 : aAny >>= sItemName;
195 0 : if (!sItemName.isEmpty())
196 : {
197 0 : mrExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_NAME, sItemName);
198 : }
199 :
200 : // is-active
201 0 : aAny = rPropertySet->getPropertyValue(msIsActive);
202 0 : if (! *static_cast<sal_Bool const *>(aAny.getValue()))
203 : {
204 0 : mrExport.AddAttribute(XML_NAMESPACE_DRAW, XML_NOHREF, XML_NOHREF);
205 : }
206 :
207 : // call specific rectangle/circle/... method
208 : // also prepare element name
209 0 : switch (eType)
210 : {
211 : case XML_AREA_RECTANGLE:
212 0 : ExportRectangle(rPropertySet);
213 0 : break;
214 : case XML_AREA_CIRCLE:
215 0 : ExportCircle(rPropertySet);
216 0 : break;
217 : case XML_AREA_POLYGON:
218 0 : ExportPolygon(rPropertySet);
219 0 : break;
220 : default:
221 0 : break;
222 : }
223 :
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 0 : mbWhiteSpace, mbWhiteSpace);
229 :
230 : // title property (as <svg:title> element)
231 0 : OUString sTitle;
232 0 : rPropertySet->getPropertyValue(msTitle) >>= sTitle;
233 0 : if(!sTitle.isEmpty())
234 : {
235 0 : SvXMLElementExport aEventElemt(mrExport, XML_NAMESPACE_SVG, XML_TITLE, mbWhiteSpace, false);
236 0 : mrExport.Characters(sTitle);
237 : }
238 :
239 : // description property (as <svg:desc> element)
240 0 : OUString sDescription;
241 0 : rPropertySet->getPropertyValue(msDescription) >>= sDescription;
242 0 : if (!sDescription.isEmpty())
243 : {
244 0 : SvXMLElementExport aDesc(mrExport, XML_NAMESPACE_SVG, XML_DESC, mbWhiteSpace, false);
245 0 : mrExport.Characters(sDescription);
246 : }
247 :
248 : // export events attached to this
249 0 : Reference<XEventsSupplier> xSupplier(rPropertySet, UNO_QUERY);
250 0 : mrExport.GetEventExport().Export(xSupplier, mbWhiteSpace);
251 0 : }
252 : // else: no service info -> can't determine type -> ignore entry
253 : }
254 :
255 0 : void XMLImageMapExport::ExportRectangle(
256 : const Reference<XPropertySet> & rPropertySet)
257 : {
258 : // get boundary rectangle
259 0 : Any aAny = rPropertySet->getPropertyValue(msBoundary);
260 0 : awt::Rectangle aRectangle;
261 0 : aAny >>= aRectangle;
262 :
263 : // parameters svg:x, svg:y, svg:width, svg:height
264 0 : OUStringBuffer aBuffer;
265 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aRectangle.X);
266 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X,
267 0 : aBuffer.makeStringAndClear() );
268 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aRectangle.Y);
269 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y,
270 0 : aBuffer.makeStringAndClear() );
271 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer,
272 0 : aRectangle.Width);
273 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH,
274 0 : aBuffer.makeStringAndClear() );
275 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer,
276 0 : aRectangle.Height);
277 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT,
278 0 : aBuffer.makeStringAndClear() );
279 0 : }
280 :
281 0 : void XMLImageMapExport::ExportCircle(
282 : const Reference<XPropertySet> & rPropertySet)
283 : {
284 : // get boundary rectangle
285 0 : Any aAny = rPropertySet->getPropertyValue(msCenter);
286 0 : awt::Point aCenter;
287 0 : aAny >>= aCenter;
288 :
289 : // parameters svg:cx, svg:cy
290 0 : OUStringBuffer aBuffer;
291 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aCenter.X);
292 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CX,
293 0 : aBuffer.makeStringAndClear() );
294 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, aCenter.Y);
295 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_CY,
296 0 : aBuffer.makeStringAndClear() );
297 :
298 : // radius
299 0 : aAny = rPropertySet->getPropertyValue(msRadius);
300 0 : sal_Int32 nRadius = 0;
301 0 : aAny >>= nRadius;
302 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, nRadius);
303 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_R,
304 0 : aBuffer.makeStringAndClear() );
305 0 : }
306 :
307 0 : void XMLImageMapExport::ExportPolygon(const Reference<XPropertySet> & rPropertySet)
308 : {
309 : // polygons get exported as bounding box, viewbox, and coordinate
310 : // pair sequence. The bounding box is always the entire image.
311 :
312 : // get polygon point sequence
313 0 : Any aAny = rPropertySet->getPropertyValue(msPolygon);
314 0 : PointSequence aPoly;
315 0 : aAny >>= aPoly;
316 :
317 : const basegfx::B2DPolygon aPolygon(
318 : basegfx::tools::UnoPointSequenceToB2DPolygon(
319 0 : aPoly));
320 0 : const basegfx::B2DRange aPolygonRange(aPolygon.getB2DRange());
321 :
322 : // parameters svg:x, svg:y, svg:width, svg:height
323 0 : OUStringBuffer aBuffer;
324 :
325 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, 0);
326 0 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_X, aBuffer.makeStringAndClear() );
327 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, 0);
328 0 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_Y, aBuffer.makeStringAndClear() );
329 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, basegfx::fround(aPolygonRange.getWidth()));
330 0 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_WIDTH, aBuffer.makeStringAndClear() );
331 0 : mrExport.GetMM100UnitConverter().convertMeasureToXML(aBuffer, basegfx::fround(aPolygonRange.getHeight()));
332 0 : mrExport.AddAttribute( XML_NAMESPACE_SVG, XML_HEIGHT, aBuffer.makeStringAndClear() );
333 :
334 : // svg:viewbox
335 0 : SdXMLImExViewBox aViewBox(0.0, 0.0, aPolygonRange.getWidth(), aPolygonRange.getHeight());
336 0 : mrExport.AddAttribute(XML_NAMESPACE_SVG, XML_VIEWBOX, aViewBox.GetExportString());
337 :
338 : // export point sequence
339 : const OUString aPointString(
340 : basegfx::tools::exportToSvgPoints(
341 0 : aPolygon));
342 :
343 0 : mrExport.AddAttribute(XML_NAMESPACE_DRAW, XML_POINTS, aPointString);
344 0 : }
345 :
346 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|