Bump version to 24.04.3.4
[LibreOffice.git] / xmloff / source / draw / XMLImageMapContext.cxx
blobe3a16c08636c1c9a453a53b155de16c4e156241d
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 <XMLImageMapContext.hxx>
21 #include <rtl/ustrbuf.hxx>
22 #include <com/sun/star/uno/Reference.h>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/beans/XPropertySetInfo.hpp>
25 #include <com/sun/star/frame/XModel.hpp>
26 #include <com/sun/star/xml/sax/XAttributeList.hpp>
27 #include <com/sun/star/container/XIndexContainer.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/document/XEventsSupplier.hpp>
30 #include <com/sun/star/awt/Rectangle.hpp>
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmlimp.hxx>
33 #include <xmloff/xmltkmap.hxx>
34 #include <xmloff/xmlnamespace.hxx>
35 #include <xmloff/namespacemap.hxx>
36 #include <xmloff/xmluconv.hxx>
37 #include <xexptran.hxx>
38 #include <xmloff/xmlerror.hxx>
39 #include <xmloff/XMLEventsImportContext.hxx>
40 #include <XMLStringBufferImportContext.hxx>
41 #include <tools/debug.hxx>
42 #include <basegfx/polygon/b2dpolygon.hxx>
43 #include <basegfx/polygon/b2dpolygontools.hxx>
44 #include <sal/log.hxx>
46 using namespace ::com::sun::star;
47 using namespace ::xmloff::token;
49 using ::com::sun::star::beans::XPropertySet;
50 using ::com::sun::star::beans::XPropertySetInfo;
51 using ::com::sun::star::container::XIndexContainer;
52 using ::com::sun::star::lang::XMultiServiceFactory;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::uno::UNO_QUERY;
55 using ::com::sun::star::uno::XInterface;
56 using ::com::sun::star::uno::Any;
57 using ::com::sun::star::document::XEventsSupplier;
59 namespace {
61 class XMLImageMapObjectContext : public SvXMLImportContext
64 protected:
66 Reference<XIndexContainer> xImageMap; /// the image map
67 Reference<XPropertySet> xMapEntry; /// one map-entry (one area)
69 OUString sUrl;
70 OUString sTargt;
71 OUStringBuffer sDescriptionBuffer;
72 OUStringBuffer sTitleBuffer;
73 OUString sNam;
74 bool bIsActive;
76 bool bValid;
78 public:
80 XMLImageMapObjectContext(
81 SvXMLImport& rImport,
82 css::uno::Reference<css::container::XIndexContainer> const & xMap,
83 const char* pServiceName);
85 virtual void SAL_CALL startFastElement( sal_Int32 nElement,
86 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override;
88 virtual void SAL_CALL endFastElement(sal_Int32 nElement) override;
90 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
91 sal_Int32 nElement,
92 const css::uno::Reference< css::xml::sax::XFastAttributeList >& AttrList ) override;
94 protected:
96 virtual void ProcessAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter &);
98 virtual void Prepare(
99 css::uno::Reference<css::beans::XPropertySet> & rPropertySet);
104 XMLImageMapObjectContext::XMLImageMapObjectContext(
105 SvXMLImport& rImport,
106 Reference<XIndexContainer> const & xMap,
107 const char* pServiceName) :
108 SvXMLImportContext(rImport),
109 xImageMap(xMap),
110 bIsActive(true),
111 bValid(false)
113 DBG_ASSERT(nullptr != pServiceName,
114 "Please supply the image map object service name");
116 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
117 if( !xFactory.is() )
118 return;
120 Reference<XInterface> xIfc = xFactory->createInstance(
121 OUString::createFromAscii(pServiceName));
122 DBG_ASSERT(xIfc.is(), "can't create image map object!");
123 if( xIfc.is() )
125 Reference<XPropertySet> xPropertySet( xIfc, UNO_QUERY );
127 xMapEntry = xPropertySet;
129 // else: can't create service -> ignore
130 // else: can't even get factory -> ignore
133 void XMLImageMapObjectContext::startFastElement( sal_Int32 /*nElement*/,
134 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
136 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
137 ProcessAttribute(aIter);
140 void XMLImageMapObjectContext::endFastElement(sal_Int32 )
142 // only create and insert image map object if validity flag is set
143 // (and we actually have an image map)
144 if ( bValid && xImageMap.is() && xMapEntry.is() )
146 // set values
147 Prepare( xMapEntry );
149 // insert into image map
150 xImageMap->insertByIndex( xImageMap->getCount(), Any(xMapEntry) );
152 // else: not valid -> don't create and insert
155 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLImageMapObjectContext::createFastChildContext(
156 sal_Int32 nElement,
157 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
159 switch (nElement)
161 case XML_ELEMENT(OFFICE, XML_EVENT_LISTENERS):
163 Reference<XEventsSupplier> xEvents( xMapEntry, UNO_QUERY );
164 return new XMLEventsImportContext(
165 GetImport(), xEvents);
167 case XML_ELEMENT(SVG, XML_TITLE):
168 case XML_ELEMENT(SVG_COMPAT, XML_TITLE):
169 return new XMLStringBufferImportContext(
170 GetImport(), sTitleBuffer);
171 case XML_ELEMENT(SVG, XML_DESC):
172 case XML_ELEMENT(SVG_COMPAT, XML_DESC):
173 return new XMLStringBufferImportContext(
174 GetImport(), sDescriptionBuffer);
176 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
177 return nullptr;
180 void XMLImageMapObjectContext::ProcessAttribute(
181 const sax_fastparser::FastAttributeList::FastAttributeIter & aIter)
183 switch (aIter.getToken())
185 case XML_ELEMENT(XLINK, XML_HREF):
186 sUrl = GetImport().GetAbsoluteReference(aIter.toString());
187 break;
189 case XML_ELEMENT(OFFICE, XML_TARGET_FRAME_NAME):
190 sTargt = aIter.toString();
191 break;
193 case XML_ELEMENT(DRAW, XML_NOHREF):
194 bIsActive = ! IsXMLToken(aIter, XML_NOHREF);
195 break;
197 case XML_ELEMENT(OFFICE, XML_NAME):
198 sNam = aIter.toString();
199 break;
200 default:
201 // do nothing
202 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
203 break;
207 void XMLImageMapObjectContext::Prepare(
208 Reference<XPropertySet> & rPropertySet)
210 rPropertySet->setPropertyValue( "URL", Any( sUrl ) );
211 rPropertySet->setPropertyValue( "Title", Any( sTitleBuffer.makeStringAndClear() ) );
212 rPropertySet->setPropertyValue( "Description", Any( sDescriptionBuffer.makeStringAndClear() ) );
213 rPropertySet->setPropertyValue( "Target", Any( sTargt ) );
214 rPropertySet->setPropertyValue( "IsActive", Any( bIsActive ) );
215 rPropertySet->setPropertyValue( "Name", Any( sNam ) );
218 namespace {
220 class XMLImageMapRectangleContext : public XMLImageMapObjectContext
222 awt::Rectangle aRectangle;
224 bool bXOK;
225 bool bYOK;
226 bool bWidthOK;
227 bool bHeightOK;
229 public:
231 XMLImageMapRectangleContext(
232 SvXMLImport& rImport,
233 css::uno::Reference<css::container::XIndexContainer> const & xMap);
235 protected:
236 virtual void ProcessAttribute(
237 const sax_fastparser::FastAttributeList::FastAttributeIter &) override;
239 virtual void Prepare(
240 css::uno::Reference<css::beans::XPropertySet> & rPropertySet) override;
245 XMLImageMapRectangleContext::XMLImageMapRectangleContext(
246 SvXMLImport& rImport,
247 Reference<XIndexContainer> const & xMap) :
248 XMLImageMapObjectContext(rImport, xMap,
249 "com.sun.star.image.ImageMapRectangleObject"),
250 bXOK(false),
251 bYOK(false),
252 bWidthOK(false),
253 bHeightOK(false)
257 void XMLImageMapRectangleContext::ProcessAttribute(
258 const sax_fastparser::FastAttributeList::FastAttributeIter & aIter)
260 sal_Int32 nTmp;
261 switch (aIter.getToken())
263 case XML_ELEMENT(SVG, XML_X):
264 case XML_ELEMENT(SVG_COMPAT, XML_X):
265 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp,
266 aIter.toView()))
268 aRectangle.X = nTmp;
269 bXOK = true;
271 break;
272 case XML_ELEMENT(SVG, XML_Y):
273 case XML_ELEMENT(SVG_COMPAT, XML_Y):
274 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp,
275 aIter.toView()))
277 aRectangle.Y = nTmp;
278 bYOK = true;
280 break;
281 case XML_ELEMENT(SVG, XML_WIDTH):
282 case XML_ELEMENT(SVG_COMPAT, XML_WIDTH):
283 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp,
284 aIter.toView()))
286 aRectangle.Width = nTmp;
287 bWidthOK = true;
289 break;
290 case XML_ELEMENT(SVG, XML_HEIGHT):
291 case XML_ELEMENT(SVG_COMPAT, XML_HEIGHT):
292 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp,
293 aIter.toView()))
295 aRectangle.Height = nTmp;
296 bHeightOK = true;
298 break;
299 default:
300 XMLImageMapObjectContext::ProcessAttribute(aIter);
303 bValid = bHeightOK && bXOK && bYOK && bWidthOK;
306 void XMLImageMapRectangleContext::Prepare(
307 Reference<XPropertySet> & rPropertySet)
309 rPropertySet->setPropertyValue( "Boundary", uno::Any(aRectangle) );
311 // common properties handled by super class
312 XMLImageMapObjectContext::Prepare(rPropertySet);
315 namespace {
317 class XMLImageMapPolygonContext : public XMLImageMapObjectContext
319 OUString sViewBoxString;
320 OUString sPointsString;
322 bool bViewBoxOK;
323 bool bPointsOK;
325 public:
327 XMLImageMapPolygonContext(
328 SvXMLImport& rImport,
329 css::uno::Reference<css::container::XIndexContainer> const & xMap);
331 protected:
332 virtual void ProcessAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter &) override;
334 virtual void Prepare(
335 css::uno::Reference<css::beans::XPropertySet> & rPropertySet) override;
340 XMLImageMapPolygonContext::XMLImageMapPolygonContext(
341 SvXMLImport& rImport,
342 Reference<XIndexContainer> const & xMap) :
343 XMLImageMapObjectContext(rImport, xMap,
344 "com.sun.star.image.ImageMapPolygonObject"),
345 bViewBoxOK(false),
346 bPointsOK(false)
350 void XMLImageMapPolygonContext::ProcessAttribute(
351 const sax_fastparser::FastAttributeList::FastAttributeIter & aIter)
353 switch (aIter.getToken())
355 case XML_ELEMENT(DRAW, XML_POINTS):
356 sPointsString = aIter.toString();
357 bPointsOK = true;
358 break;
359 case XML_ELEMENT(SVG, XML_VIEWBOX):
360 case XML_ELEMENT(SVG_COMPAT, XML_VIEWBOX):
361 sViewBoxString = aIter.toString();
362 bViewBoxOK = true;
363 break;
364 default:
365 XMLImageMapObjectContext::ProcessAttribute(aIter);
366 break;
369 bValid = bViewBoxOK && bPointsOK;
372 void XMLImageMapPolygonContext::Prepare(Reference<XPropertySet> & rPropertySet)
374 // process view box
375 SdXMLImExViewBox aViewBox(sViewBoxString, GetImport().GetMM100UnitConverter());
377 // get polygon sequence
378 basegfx::B2DPolygon aPolygon;
380 if(basegfx::utils::importFromSvgPoints(aPolygon, sPointsString))
382 if(aPolygon.count())
384 css::drawing::PointSequence aPointSequence;
385 basegfx::utils::B2DPolygonToUnoPointSequence(aPolygon, aPointSequence);
386 rPropertySet->setPropertyValue("Polygon", Any(aPointSequence));
390 // parent properties
391 XMLImageMapObjectContext::Prepare(rPropertySet);
394 namespace {
396 class XMLImageMapCircleContext : public XMLImageMapObjectContext
398 awt::Point aCenter;
399 sal_Int32 nRadius;
401 bool bXOK;
402 bool bYOK;
403 bool bRadiusOK;
405 public:
407 XMLImageMapCircleContext(
408 SvXMLImport& rImport,
409 css::uno::Reference<css::container::XIndexContainer> const & xMap);
411 protected:
412 virtual void ProcessAttribute(
413 const sax_fastparser::FastAttributeList::FastAttributeIter &) override;
415 virtual void Prepare(
416 css::uno::Reference<css::beans::XPropertySet> & rPropertySet) override;
421 XMLImageMapCircleContext::XMLImageMapCircleContext(
422 SvXMLImport& rImport,
423 Reference<XIndexContainer> const & xMap)
424 : XMLImageMapObjectContext(rImport, xMap,
425 "com.sun.star.image.ImageMapCircleObject")
426 , nRadius(0)
427 , bXOK(false)
428 , bYOK(false)
429 , bRadiusOK(false)
433 void XMLImageMapCircleContext::ProcessAttribute(
434 const sax_fastparser::FastAttributeList::FastAttributeIter & aIter)
436 sal_Int32 nTmp;
437 switch (aIter.getToken())
439 case XML_ELEMENT(SVG, XML_CX):
440 case XML_ELEMENT(SVG_COMPAT, XML_CX):
441 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp,
442 aIter.toView()))
444 aCenter.X = nTmp;
445 bXOK = true;
447 break;
448 case XML_ELEMENT(SVG, XML_CY):
449 case XML_ELEMENT(SVG_COMPAT, XML_CY):
450 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp,
451 aIter.toView()))
453 aCenter.Y = nTmp;
454 bYOK = true;
456 break;
457 case XML_ELEMENT(SVG, XML_R):
458 case XML_ELEMENT(SVG_COMPAT, XML_R):
459 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp,
460 aIter.toView()))
462 nRadius = nTmp;
463 bRadiusOK = true;
465 break;
466 default:
467 XMLImageMapObjectContext::ProcessAttribute(aIter);
470 bValid = bRadiusOK && bXOK && bYOK;
473 void XMLImageMapCircleContext::Prepare(
474 Reference<XPropertySet> & rPropertySet)
476 // center (x,y)
477 rPropertySet->setPropertyValue( "Center", uno::Any(aCenter) );
478 // radius
479 rPropertySet->setPropertyValue( "Radius", uno::Any(nRadius) );
481 // common properties handled by super class
482 XMLImageMapObjectContext::Prepare(rPropertySet);
486 constexpr OUString gsImageMap(u"ImageMap"_ustr);
488 XMLImageMapContext::XMLImageMapContext(
489 SvXMLImport& rImport,
490 Reference<XPropertySet> const & rPropertySet) :
491 SvXMLImportContext(rImport),
492 xPropertySet(rPropertySet)
496 Reference < XPropertySetInfo > xInfo =
497 xPropertySet->getPropertySetInfo();
498 if( xInfo.is() && xInfo->hasPropertyByName( gsImageMap ) )
499 xPropertySet->getPropertyValue(gsImageMap) >>= xImageMap;
501 catch(const css::uno::Exception& e)
503 rImport.SetError( XMLERROR_FLAG_WARNING | XMLERROR_API, {}, e.Message, nullptr );
507 XMLImageMapContext::~XMLImageMapContext()
511 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLImageMapContext::createFastChildContext(
512 sal_Int32 nElement,
513 const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
515 switch (nElement)
517 case XML_ELEMENT(DRAW, XML_AREA_RECTANGLE):
518 return new XMLImageMapRectangleContext(
519 GetImport(), xImageMap);
520 case XML_ELEMENT(DRAW, XML_AREA_POLYGON):
521 return new XMLImageMapPolygonContext(
522 GetImport(), xImageMap);
523 case XML_ELEMENT(DRAW, XML_AREA_CIRCLE):
524 return new XMLImageMapCircleContext(
525 GetImport(), xImageMap);
526 default:
527 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
530 return nullptr;
533 void XMLImageMapContext::endFastElement(sal_Int32 )
535 Reference < XPropertySetInfo > xInfo =
536 xPropertySet->getPropertySetInfo();
537 if( xInfo.is() && xInfo->hasPropertyByName( gsImageMap ) )
538 xPropertySet->setPropertyValue(gsImageMap, uno::Any( xImageMap ) );
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */