1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
61 class XMLImageMapObjectContext
: public SvXMLImportContext
66 Reference
<XIndexContainer
> xImageMap
; /// the image map
67 Reference
<XPropertySet
> xMapEntry
; /// one map-entry (one area)
71 OUStringBuffer sDescriptionBuffer
;
72 OUStringBuffer sTitleBuffer
;
80 XMLImageMapObjectContext(
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(
92 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& AttrList
) override
;
96 virtual void ProcessAttribute(const sax_fastparser::FastAttributeList::FastAttributeIter
&);
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
),
113 DBG_ASSERT(nullptr != pServiceName
,
114 "Please supply the image map object service name");
116 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),UNO_QUERY
);
120 Reference
<XInterface
> xIfc
= xFactory
->createInstance(
121 OUString::createFromAscii(pServiceName
));
122 DBG_ASSERT(xIfc
.is(), "can't create image map object!");
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() )
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(
157 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
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
);
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());
189 case XML_ELEMENT(OFFICE
, XML_TARGET_FRAME_NAME
):
190 sTargt
= aIter
.toString();
193 case XML_ELEMENT(DRAW
, XML_NOHREF
):
194 bIsActive
= ! IsXMLToken(aIter
, XML_NOHREF
);
197 case XML_ELEMENT(OFFICE
, XML_NAME
):
198 sNam
= aIter
.toString();
202 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
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
) );
220 class XMLImageMapRectangleContext
: public XMLImageMapObjectContext
222 awt::Rectangle aRectangle
;
231 XMLImageMapRectangleContext(
232 SvXMLImport
& rImport
,
233 css::uno::Reference
<css::container::XIndexContainer
> const & xMap
);
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"),
257 void XMLImageMapRectangleContext::ProcessAttribute(
258 const sax_fastparser::FastAttributeList::FastAttributeIter
& aIter
)
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
,
272 case XML_ELEMENT(SVG
, XML_Y
):
273 case XML_ELEMENT(SVG_COMPAT
, XML_Y
):
274 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp
,
281 case XML_ELEMENT(SVG
, XML_WIDTH
):
282 case XML_ELEMENT(SVG_COMPAT
, XML_WIDTH
):
283 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp
,
286 aRectangle
.Width
= nTmp
;
290 case XML_ELEMENT(SVG
, XML_HEIGHT
):
291 case XML_ELEMENT(SVG_COMPAT
, XML_HEIGHT
):
292 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp
,
295 aRectangle
.Height
= nTmp
;
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
);
317 class XMLImageMapPolygonContext
: public XMLImageMapObjectContext
319 OUString sViewBoxString
;
320 OUString sPointsString
;
327 XMLImageMapPolygonContext(
328 SvXMLImport
& rImport
,
329 css::uno::Reference
<css::container::XIndexContainer
> const & xMap
);
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"),
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();
359 case XML_ELEMENT(SVG
, XML_VIEWBOX
):
360 case XML_ELEMENT(SVG_COMPAT
, XML_VIEWBOX
):
361 sViewBoxString
= aIter
.toString();
365 XMLImageMapObjectContext::ProcessAttribute(aIter
);
369 bValid
= bViewBoxOK
&& bPointsOK
;
372 void XMLImageMapPolygonContext::Prepare(Reference
<XPropertySet
> & rPropertySet
)
375 SdXMLImExViewBox
aViewBox(sViewBoxString
, GetImport().GetMM100UnitConverter());
377 // get polygon sequence
378 basegfx::B2DPolygon aPolygon
;
380 if(basegfx::utils::importFromSvgPoints(aPolygon
, sPointsString
))
384 css::drawing::PointSequence aPointSequence
;
385 basegfx::utils::B2DPolygonToUnoPointSequence(aPolygon
, aPointSequence
);
386 rPropertySet
->setPropertyValue("Polygon", Any(aPointSequence
));
391 XMLImageMapObjectContext::Prepare(rPropertySet
);
396 class XMLImageMapCircleContext
: public XMLImageMapObjectContext
407 XMLImageMapCircleContext(
408 SvXMLImport
& rImport
,
409 css::uno::Reference
<css::container::XIndexContainer
> const & xMap
);
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")
433 void XMLImageMapCircleContext::ProcessAttribute(
434 const sax_fastparser::FastAttributeList::FastAttributeIter
& aIter
)
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
,
448 case XML_ELEMENT(SVG
, XML_CY
):
449 case XML_ELEMENT(SVG_COMPAT
, XML_CY
):
450 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp
,
457 case XML_ELEMENT(SVG
, XML_R
):
458 case XML_ELEMENT(SVG_COMPAT
, XML_R
):
459 if (GetImport().GetMM100UnitConverter().convertMeasureToCore(nTmp
,
467 XMLImageMapObjectContext::ProcessAttribute(aIter
);
470 bValid
= bRadiusOK
&& bXOK
&& bYOK
;
473 void XMLImageMapCircleContext::Prepare(
474 Reference
<XPropertySet
> & rPropertySet
)
477 rPropertySet
->setPropertyValue( "Center", uno::Any(aCenter
) );
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(
513 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
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
);
527 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
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: */