LCOV - code coverage report
Current view: top level - xmloff/source/draw - XMLImageMapExport.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 140 0.0 %
Date: 2014-04-14 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10