LCOV - code coverage report
Current view: top level - libreoffice/xmloff/source/style - DashStyle.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 57 103 55.3 %
Date: 2012-12-27 Functions: 5 6 83.3 %
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             : 
      21             : #include <com/sun/star/drawing/DashStyle.hpp>
      22             : #include <com/sun/star/drawing/LineDash.hpp>
      23             : 
      24             : #include <sax/tools/converter.hxx>
      25             : 
      26             : #include "xmloff/DashStyle.hxx"
      27             : #include <xmloff/attrlist.hxx>
      28             : #include <xmloff/nmspmap.hxx>
      29             : #include <xmloff/xmluconv.hxx>
      30             : #include "xmloff/xmlnmspe.hxx"
      31             : #include <xmloff/xmltoken.hxx>
      32             : #include <xmloff/xmlexp.hxx>
      33             : #include <xmloff/xmlimp.hxx>
      34             : #include <rtl/ustrbuf.hxx>
      35             : #include <rtl/ustring.hxx>
      36             : #include <tools/debug.hxx>
      37             : #include <xmloff/xmltkmap.hxx>
      38             : 
      39             : using namespace ::com::sun::star;
      40             : using ::rtl::OUString;
      41             : using ::rtl::OUStringBuffer;
      42             : 
      43             : using namespace ::xmloff::token;
      44             : 
      45             : enum SvXMLTokenMapAttrs
      46             : {
      47             :     XML_TOK_DASH_NAME,
      48             :     XML_TOK_DASH_DISPLAY_NAME,
      49             :     XML_TOK_DASH_STYLE,
      50             :     XML_TOK_DASH_DOTS1,
      51             :     XML_TOK_DASH_DOTS1LEN,
      52             :     XML_TOK_DASH_DOTS2,
      53             :     XML_TOK_DASH_DOTS2LEN,
      54             :     XML_TOK_DASH_DISTANCE,
      55             :     XML_TOK_DASH_END=XML_TOK_UNKNOWN
      56             : };
      57             : 
      58             : static SvXMLTokenMapEntry aDashStyleAttrTokenMap[] =
      59             : {
      60             :     { XML_NAMESPACE_DRAW, XML_NAME,             XML_TOK_DASH_NAME },
      61             :     { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,     XML_TOK_DASH_DISPLAY_NAME },
      62             :     { XML_NAMESPACE_DRAW, XML_STYLE,            XML_TOK_DASH_STYLE },
      63             :     { XML_NAMESPACE_DRAW, XML_DOTS1,            XML_TOK_DASH_DOTS1 },
      64             :     { XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH,     XML_TOK_DASH_DOTS1LEN },
      65             :     { XML_NAMESPACE_DRAW, XML_DOTS2,            XML_TOK_DASH_DOTS2 },
      66             :     { XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH,     XML_TOK_DASH_DOTS2LEN },
      67             :     { XML_NAMESPACE_DRAW, XML_DISTANCE,         XML_TOK_DASH_DISTANCE },
      68             :     XML_TOKEN_MAP_END
      69             : };
      70             : 
      71             : SvXMLEnumMapEntry const pXML_DashStyle_Enum[] =
      72             : {
      73             :     { XML_RECT,         drawing::DashStyle_RECT },
      74             :     { XML_ROUND,        drawing::DashStyle_ROUND },
      75             :     { XML_RECT,         drawing::DashStyle_RECTRELATIVE },
      76             :     { XML_ROUND,        drawing::DashStyle_ROUNDRELATIVE },
      77             :     { XML_TOKEN_INVALID, 0 }
      78             : };
      79             : 
      80             : //-------------------------------------------------------------
      81             : // Import
      82             : //-------------------------------------------------------------
      83             : 
      84           1 : XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
      85           1 :     : rImport(rImp)
      86             : {
      87           1 : }
      88             : 
      89           1 : XMLDashStyleImport::~XMLDashStyleImport()
      90             : {
      91           1 : }
      92             : 
      93           1 : sal_Bool XMLDashStyleImport::importXML(
      94             :     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
      95             :     uno::Any& rValue,
      96             :     OUString& rStrName )
      97             : {
      98           1 :     drawing::LineDash aLineDash;
      99           1 :     aLineDash.Style = drawing::DashStyle_RECT;
     100           1 :     aLineDash.Dots = 0;
     101           1 :     aLineDash.DotLen = 0;
     102           1 :     aLineDash.Dashes = 0;
     103           1 :     aLineDash.DashLen = 0;
     104           1 :     aLineDash.Distance = 20;
     105           1 :     OUString aDisplayName;
     106             : 
     107           1 :     sal_Bool bIsRel = sal_False;
     108             : 
     109           1 :     SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
     110           1 :     SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
     111             : 
     112           1 :     SvXMLTokenMap aTokenMap( aDashStyleAttrTokenMap );
     113             : 
     114           1 :     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
     115           9 :     for( sal_Int16 i=0; i < nAttrCount; i++ )
     116             :     {
     117           8 :         const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
     118           8 :         OUString aStrAttrName;
     119           8 :         sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
     120           8 :         const OUString& rStrValue = xAttrList->getValueByIndex( i );
     121             : 
     122           8 :         switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
     123             :         {
     124             :         case XML_TOK_DASH_NAME:
     125             :             {
     126           1 :                 rStrName = rStrValue;
     127             :             }
     128           1 :             break;
     129             :         case XML_TOK_DASH_DISPLAY_NAME:
     130             :             {
     131           1 :                 aDisplayName = rStrValue;
     132             :             }
     133           1 :             break;
     134             :         case XML_TOK_DASH_STYLE:
     135             :             {
     136             :                 sal_uInt16 eValue;
     137           1 :                 if( rUnitConverter.convertEnum( eValue, rStrValue, pXML_DashStyle_Enum ) )
     138             :                 {
     139           1 :                     aLineDash.Style = (drawing::DashStyle) eValue;
     140             :                 }
     141             :             }
     142           1 :             break;
     143             :         case XML_TOK_DASH_DOTS1:
     144           1 :             aLineDash.Dots = (sal_Int16)rStrValue.toInt32();
     145           1 :             break;
     146             : 
     147             :         case XML_TOK_DASH_DOTS1LEN:
     148             :             {
     149           1 :                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
     150             :                 {
     151           0 :                     bIsRel = sal_True;
     152           0 :                     ::sax::Converter::convertPercent(aLineDash.DotLen, rStrValue);
     153             :                 }
     154             :                 else
     155             :                 {
     156             :                     rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
     157           1 :                             rStrValue );
     158             :                 }
     159             :             }
     160           1 :             break;
     161             : 
     162             :         case XML_TOK_DASH_DOTS2:
     163           1 :             aLineDash.Dashes = (sal_Int16)rStrValue.toInt32();
     164           1 :             break;
     165             : 
     166             :         case XML_TOK_DASH_DOTS2LEN:
     167             :             {
     168           1 :                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
     169             :                 {
     170           0 :                     bIsRel = sal_True;
     171           0 :                     ::sax::Converter::convertPercent(aLineDash.DashLen, rStrValue);
     172             :                 }
     173             :                 else
     174             :                 {
     175             :                     rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
     176           1 :                             rStrValue );
     177             :                 }
     178             :             }
     179           1 :             break;
     180             : 
     181             :         case XML_TOK_DASH_DISTANCE:
     182             :             {
     183           1 :                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
     184             :                 {
     185           0 :                     bIsRel = sal_True;
     186           0 :                     ::sax::Converter::convertPercent(aLineDash.Distance, rStrValue);
     187             :                 }
     188             :                 else
     189             :                 {
     190             :                     rUnitConverter.convertMeasureToCore( aLineDash.Distance,
     191           1 :                             rStrValue );
     192             :                 }
     193             :             }
     194           1 :             break;
     195             :         default:
     196             :             DBG_WARNING( "Unknown token at import gradient style" );
     197             :         }
     198           8 :     }
     199             : 
     200           1 :     if( bIsRel )
     201           0 :         aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
     202             : 
     203           1 :     rValue <<= aLineDash;
     204             : 
     205           1 :     if( !aDisplayName.isEmpty() )
     206             :     {
     207             :         rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID,
     208           1 :                                      rStrName, aDisplayName );
     209           1 :         rStrName = aDisplayName;
     210             :     }
     211             : 
     212           1 :     return sal_True;
     213             : }
     214             : 
     215             : 
     216             : //-------------------------------------------------------------
     217             : // Export
     218             : //-------------------------------------------------------------
     219             : 
     220           4 : XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
     221           4 :     : rExport(rExp)
     222             : {
     223           4 : }
     224             : 
     225           4 : XMLDashStyleExport::~XMLDashStyleExport()
     226             : {
     227           4 : }
     228             : 
     229           0 : sal_Bool XMLDashStyleExport::exportXML(
     230             :     const OUString& rStrName,
     231             :     const uno::Any& rValue )
     232             : {
     233           0 :     sal_Bool bRet = sal_False;
     234             : 
     235           0 :     SvXMLUnitConverter & rUnitConverter = rExport.GetMM100UnitConverter();
     236             : 
     237           0 :     drawing::LineDash aLineDash;
     238             : 
     239           0 :     if( !rStrName.isEmpty() )
     240             :     {
     241           0 :         if( rValue >>= aLineDash )
     242             :         {
     243           0 :             sal_Bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
     244             : 
     245           0 :             OUString aStrValue;
     246           0 :             OUStringBuffer aOut;
     247             : 
     248             :             // Name
     249           0 :             sal_Bool bEncoded = sal_False;
     250             :             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
     251             :                                   rExport.EncodeStyleName( rStrName,
     252           0 :                                                            &bEncoded ) );
     253           0 :             if( bEncoded )
     254             :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
     255           0 :                                       rStrName );
     256             : 
     257             :             // Style
     258           0 :             rUnitConverter.convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
     259           0 :             aStrValue = aOut.makeStringAndClear();
     260           0 :             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
     261             : 
     262             : 
     263             :             // dots
     264           0 :             if( aLineDash.Dots )
     265             :             {
     266           0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::valueOf( (sal_Int32)aLineDash.Dots ) );
     267             : 
     268           0 :                 if( aLineDash.DotLen )
     269             :                 {
     270             :                     // dashes length
     271           0 :                     if( bIsRel )
     272             :                     {
     273           0 :                         ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
     274             :                     }
     275             :                     else
     276             :                     {
     277             :                         rUnitConverter.convertMeasureToXML( aOut,
     278           0 :                                 aLineDash.DotLen );
     279             :                     }
     280           0 :                     aStrValue = aOut.makeStringAndClear();
     281           0 :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
     282             :                 }
     283             :             }
     284             : 
     285             :             // dashes
     286           0 :             if( aLineDash.Dashes )
     287             :             {
     288           0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::valueOf( (sal_Int32)aLineDash.Dashes ) );
     289             : 
     290           0 :                 if( aLineDash.DashLen )
     291             :                 {
     292             :                     // dashes length
     293           0 :                     if( bIsRel )
     294             :                     {
     295           0 :                         ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
     296             :                     }
     297             :                     else
     298             :                     {
     299             :                         rUnitConverter.convertMeasureToXML( aOut,
     300           0 :                                 aLineDash.DashLen );
     301             :                     }
     302           0 :                     aStrValue = aOut.makeStringAndClear();
     303           0 :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
     304             :                 }
     305             :             }
     306             : 
     307             :             // distance
     308           0 :             if( bIsRel )
     309             :             {
     310           0 :                 ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
     311             :             }
     312             :             else
     313             :             {
     314             :                 rUnitConverter.convertMeasureToXML( aOut,
     315           0 :                         aLineDash.Distance );
     316             :             }
     317           0 :             aStrValue = aOut.makeStringAndClear();
     318           0 :             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
     319             : 
     320             : 
     321             :             // do Write
     322             :             SvXMLElementExport rElem( rExport,
     323             :                                       XML_NAMESPACE_DRAW, XML_STROKE_DASH,
     324           0 :                                       sal_True, sal_False );
     325             :         }
     326             :     }
     327           0 :     return bRet;
     328             : }
     329             : 
     330             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10