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

Generated by: LCOV version 1.11