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

Generated by: LCOV version 1.10