LCOV - code coverage report
Current view: top level - xmloff/source/style - DashStyle.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 62 103 60.2 %
Date: 2012-08-25 Functions: 5 6 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 40 135 29.6 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #include <com/sun/star/drawing/DashStyle.hpp>
      31                 :            : #include <com/sun/star/drawing/LineDash.hpp>
      32                 :            : 
      33                 :            : #include <sax/tools/converter.hxx>
      34                 :            : 
      35                 :            : #include "xmloff/DashStyle.hxx"
      36                 :            : #include <xmloff/attrlist.hxx>
      37                 :            : #include <xmloff/nmspmap.hxx>
      38                 :            : #include <xmloff/xmluconv.hxx>
      39                 :            : #include "xmloff/xmlnmspe.hxx"
      40                 :            : #include <xmloff/xmltoken.hxx>
      41                 :            : #include <xmloff/xmlexp.hxx>
      42                 :            : #include <xmloff/xmlimp.hxx>
      43                 :            : #include <rtl/ustrbuf.hxx>
      44                 :            : #include <rtl/ustring.hxx>
      45                 :            : #include <tools/debug.hxx>
      46                 :            : #include <xmloff/xmltkmap.hxx>
      47                 :            : 
      48                 :            : using namespace ::com::sun::star;
      49                 :            : using ::rtl::OUString;
      50                 :            : using ::rtl::OUStringBuffer;
      51                 :            : 
      52                 :            : using namespace ::xmloff::token;
      53                 :            : 
      54                 :            : enum SvXMLTokenMapAttrs
      55                 :            : {
      56                 :            :     XML_TOK_DASH_NAME,
      57                 :            :     XML_TOK_DASH_DISPLAY_NAME,
      58                 :            :     XML_TOK_DASH_STYLE,
      59                 :            :     XML_TOK_DASH_DOTS1,
      60                 :            :     XML_TOK_DASH_DOTS1LEN,
      61                 :            :     XML_TOK_DASH_DOTS2,
      62                 :            :     XML_TOK_DASH_DOTS2LEN,
      63                 :            :     XML_TOK_DASH_DISTANCE,
      64                 :            :     XML_TOK_DASH_END=XML_TOK_UNKNOWN
      65                 :            : };
      66                 :            : 
      67                 :            : static SvXMLTokenMapEntry aDashStyleAttrTokenMap[] =
      68                 :            : {
      69                 :            :     { XML_NAMESPACE_DRAW, XML_NAME,             XML_TOK_DASH_NAME },
      70                 :            :     { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,     XML_TOK_DASH_DISPLAY_NAME },
      71                 :            :     { XML_NAMESPACE_DRAW, XML_STYLE,            XML_TOK_DASH_STYLE },
      72                 :            :     { XML_NAMESPACE_DRAW, XML_DOTS1,            XML_TOK_DASH_DOTS1 },
      73                 :            :     { XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH,     XML_TOK_DASH_DOTS1LEN },
      74                 :            :     { XML_NAMESPACE_DRAW, XML_DOTS2,            XML_TOK_DASH_DOTS2 },
      75                 :            :     { XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH,     XML_TOK_DASH_DOTS2LEN },
      76                 :            :     { XML_NAMESPACE_DRAW, XML_DISTANCE,         XML_TOK_DASH_DISTANCE },
      77                 :            :     XML_TOKEN_MAP_END
      78                 :            : };
      79                 :            : 
      80                 :            : SvXMLEnumMapEntry const pXML_DashStyle_Enum[] =
      81                 :            : {
      82                 :            :     { XML_RECT,         drawing::DashStyle_RECT },
      83                 :            :     { XML_ROUND,        drawing::DashStyle_ROUND },
      84                 :            :     { XML_RECT,         drawing::DashStyle_RECTRELATIVE },
      85                 :            :     { XML_ROUND,        drawing::DashStyle_ROUNDRELATIVE },
      86                 :            :     { XML_TOKEN_INVALID, 0 }
      87                 :            : };
      88                 :            : 
      89                 :            : //-------------------------------------------------------------
      90                 :            : // Import
      91                 :            : //-------------------------------------------------------------
      92                 :            : 
      93                 :       1400 : XMLDashStyleImport::XMLDashStyleImport( SvXMLImport& rImp )
      94                 :       1400 :     : rImport(rImp)
      95                 :            : {
      96                 :       1400 : }
      97                 :            : 
      98                 :       1400 : XMLDashStyleImport::~XMLDashStyleImport()
      99                 :            : {
     100                 :       1400 : }
     101                 :            : 
     102                 :       1400 : sal_Bool XMLDashStyleImport::importXML(
     103                 :            :     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
     104                 :            :     uno::Any& rValue,
     105                 :            :     OUString& rStrName )
     106                 :            : {
     107                 :       1400 :     drawing::LineDash aLineDash;
     108                 :       1400 :     aLineDash.Style = drawing::DashStyle_RECT;
     109                 :       1400 :     aLineDash.Dots = 0;
     110                 :       1400 :     aLineDash.DotLen = 0;
     111                 :       1400 :     aLineDash.Dashes = 0;
     112                 :       1400 :     aLineDash.DashLen = 0;
     113                 :       1400 :     aLineDash.Distance = 20;
     114                 :       1400 :     OUString aDisplayName;
     115                 :            : 
     116                 :       1400 :     sal_Bool bIsRel = sal_False;
     117                 :            : 
     118                 :       1400 :     SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
     119                 :       1400 :     SvXMLUnitConverter& rUnitConverter = rImport.GetMM100UnitConverter();
     120                 :            : 
     121         [ +  - ]:       1400 :     SvXMLTokenMap aTokenMap( aDashStyleAttrTokenMap );
     122                 :            : 
     123 [ +  - ][ +  - ]:       1400 :     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
                 [ +  - ]
     124         [ +  + ]:       9298 :     for( sal_Int16 i=0; i < nAttrCount; i++ )
     125                 :            :     {
     126 [ +  - ][ +  - ]:       7898 :         const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
     127                 :       7898 :         OUString aStrAttrName;
     128         [ +  - ]:       7898 :         sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
     129 [ +  - ][ +  - ]:       7898 :         const OUString& rStrValue = xAttrList->getValueByIndex( i );
     130                 :            : 
     131 [ +  - ][ +  +  :       7898 :         switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
          +  +  +  +  +  
                   +  - ]
     132                 :            :         {
     133                 :            :         case XML_TOK_DASH_NAME:
     134                 :            :             {
     135                 :       1400 :                 rStrName = rStrValue;
     136                 :            :             }
     137                 :       1400 :             break;
     138                 :            :         case XML_TOK_DASH_DISPLAY_NAME:
     139                 :            :             {
     140                 :          3 :                 aDisplayName = rStrValue;
     141                 :            :             }
     142                 :          3 :             break;
     143                 :            :         case XML_TOK_DASH_STYLE:
     144                 :            :             {
     145                 :            :                 sal_uInt16 eValue;
     146 [ +  - ][ +  - ]:       1400 :                 if( rUnitConverter.convertEnum( eValue, rStrValue, pXML_DashStyle_Enum ) )
     147                 :            :                 {
     148                 :       1400 :                     aLineDash.Style = (drawing::DashStyle) eValue;
     149                 :            :                 }
     150                 :            :             }
     151                 :       1400 :             break;
     152                 :            :         case XML_TOK_DASH_DOTS1:
     153                 :       1400 :             aLineDash.Dots = (sal_Int16)rStrValue.toInt32();
     154                 :       1400 :             break;
     155                 :            : 
     156                 :            :         case XML_TOK_DASH_DOTS1LEN:
     157                 :            :             {
     158         [ +  + ]:       1019 :                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
     159                 :            :                 {
     160                 :        508 :                     bIsRel = sal_True;
     161         [ +  - ]:        508 :                     ::sax::Converter::convertPercent(aLineDash.DotLen, rStrValue);
     162                 :            :                 }
     163                 :            :                 else
     164                 :            :                 {
     165                 :            :                     rUnitConverter.convertMeasureToCore( aLineDash.DotLen,
     166         [ +  - ]:        511 :                             rStrValue );
     167                 :            :                 }
     168                 :            :             }
     169                 :       1019 :             break;
     170                 :            : 
     171                 :            :         case XML_TOK_DASH_DOTS2:
     172                 :        765 :             aLineDash.Dashes = (sal_Int16)rStrValue.toInt32();
     173                 :        765 :             break;
     174                 :            : 
     175                 :            :         case XML_TOK_DASH_DOTS2LEN:
     176                 :            :             {
     177         [ -  + ]:        511 :                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
     178                 :            :                 {
     179                 :          0 :                     bIsRel = sal_True;
     180         [ #  # ]:          0 :                     ::sax::Converter::convertPercent(aLineDash.DashLen, rStrValue);
     181                 :            :                 }
     182                 :            :                 else
     183                 :            :                 {
     184                 :            :                     rUnitConverter.convertMeasureToCore( aLineDash.DashLen,
     185         [ +  - ]:        511 :                             rStrValue );
     186                 :            :                 }
     187                 :            :             }
     188                 :        511 :             break;
     189                 :            : 
     190                 :            :         case XML_TOK_DASH_DISTANCE:
     191                 :            :             {
     192         [ +  + ]:       1400 :                 if( rStrValue.indexOf( sal_Unicode('%') ) != -1 ) // its a percentage
     193                 :            :                 {
     194                 :        635 :                     bIsRel = sal_True;
     195         [ +  - ]:        635 :                     ::sax::Converter::convertPercent(aLineDash.Distance, rStrValue);
     196                 :            :                 }
     197                 :            :                 else
     198                 :            :                 {
     199                 :            :                     rUnitConverter.convertMeasureToCore( aLineDash.Distance,
     200         [ +  - ]:        765 :                             rStrValue );
     201                 :            :                 }
     202                 :            :             }
     203                 :       1400 :             break;
     204                 :            :         default:
     205                 :            :             DBG_WARNING( "Unknown token at import gradient style" );
     206                 :            :         }
     207                 :       7898 :     }
     208                 :            : 
     209         [ +  + ]:       1400 :     if( bIsRel )
     210         [ +  - ]:        635 :         aLineDash.Style = aLineDash.Style == drawing::DashStyle_RECT ? drawing::DashStyle_RECTRELATIVE : drawing::DashStyle_ROUNDRELATIVE;
     211                 :            : 
     212         [ +  - ]:       1400 :     rValue <<= aLineDash;
     213                 :            : 
     214         [ +  + ]:       1400 :     if( !aDisplayName.isEmpty() )
     215                 :            :     {
     216                 :            :         rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_STROKE_DASH_ID,
     217         [ +  - ]:          3 :                                      rStrName, aDisplayName );
     218                 :          3 :         rStrName = aDisplayName;
     219                 :            :     }
     220                 :            : 
     221         [ +  - ]:       1400 :     return sal_True;
     222                 :            : }
     223                 :            : 
     224                 :            : 
     225                 :            : //-------------------------------------------------------------
     226                 :            : // Export
     227                 :            : //-------------------------------------------------------------
     228                 :            : 
     229                 :         46 : XMLDashStyleExport::XMLDashStyleExport( SvXMLExport& rExp )
     230                 :         46 :     : rExport(rExp)
     231                 :            : {
     232                 :         46 : }
     233                 :            : 
     234                 :         46 : XMLDashStyleExport::~XMLDashStyleExport()
     235                 :            : {
     236                 :         46 : }
     237                 :            : 
     238                 :          0 : sal_Bool XMLDashStyleExport::exportXML(
     239                 :            :     const OUString& rStrName,
     240                 :            :     const uno::Any& rValue )
     241                 :            : {
     242                 :          0 :     sal_Bool bRet = sal_False;
     243                 :            : 
     244                 :          0 :     SvXMLUnitConverter & rUnitConverter = rExport.GetMM100UnitConverter();
     245                 :            : 
     246                 :          0 :     drawing::LineDash aLineDash;
     247                 :            : 
     248         [ #  # ]:          0 :     if( !rStrName.isEmpty() )
     249                 :            :     {
     250 [ #  # ][ #  # ]:          0 :         if( rValue >>= aLineDash )
     251                 :            :         {
     252 [ #  # ][ #  # ]:          0 :             sal_Bool bIsRel = aLineDash.Style == drawing::DashStyle_RECTRELATIVE || aLineDash.Style == drawing::DashStyle_ROUNDRELATIVE;
     253                 :            : 
     254                 :          0 :             OUString aStrValue;
     255                 :          0 :             OUStringBuffer aOut;
     256                 :            : 
     257                 :            :             // Name
     258                 :          0 :             sal_Bool bEncoded = sal_False;
     259                 :            :             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
     260                 :            :                                   rExport.EncodeStyleName( rStrName,
     261 [ #  # ][ #  # ]:          0 :                                                            &bEncoded ) );
     262         [ #  # ]:          0 :             if( bEncoded )
     263                 :            :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
     264         [ #  # ]:          0 :                                       rStrName );
     265                 :            : 
     266                 :            :             // Style
     267         [ #  # ]:          0 :             rUnitConverter.convertEnum( aOut, aLineDash.Style, pXML_DashStyle_Enum );
     268         [ #  # ]:          0 :             aStrValue = aOut.makeStringAndClear();
     269         [ #  # ]:          0 :             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
     270                 :            : 
     271                 :            : 
     272                 :            :             // dots
     273         [ #  # ]:          0 :             if( aLineDash.Dots )
     274                 :            :             {
     275         [ #  # ]:          0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1, OUString::valueOf( (sal_Int32)aLineDash.Dots ) );
     276                 :            : 
     277         [ #  # ]:          0 :                 if( aLineDash.DotLen )
     278                 :            :                 {
     279                 :            :                     // dashes length
     280         [ #  # ]:          0 :                     if( bIsRel )
     281                 :            :                     {
     282         [ #  # ]:          0 :                         ::sax::Converter::convertPercent(aOut, aLineDash.DotLen);
     283                 :            :                     }
     284                 :            :                     else
     285                 :            :                     {
     286                 :            :                         rUnitConverter.convertMeasureToXML( aOut,
     287         [ #  # ]:          0 :                                 aLineDash.DotLen );
     288                 :            :                     }
     289         [ #  # ]:          0 :                     aStrValue = aOut.makeStringAndClear();
     290         [ #  # ]:          0 :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS1_LENGTH, aStrValue );
     291                 :            :                 }
     292                 :            :             }
     293                 :            : 
     294                 :            :             // dashes
     295         [ #  # ]:          0 :             if( aLineDash.Dashes )
     296                 :            :             {
     297         [ #  # ]:          0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2, OUString::valueOf( (sal_Int32)aLineDash.Dashes ) );
     298                 :            : 
     299         [ #  # ]:          0 :                 if( aLineDash.DashLen )
     300                 :            :                 {
     301                 :            :                     // dashes length
     302         [ #  # ]:          0 :                     if( bIsRel )
     303                 :            :                     {
     304         [ #  # ]:          0 :                         ::sax::Converter::convertPercent(aOut, aLineDash.DashLen);
     305                 :            :                     }
     306                 :            :                     else
     307                 :            :                     {
     308                 :            :                         rUnitConverter.convertMeasureToXML( aOut,
     309         [ #  # ]:          0 :                                 aLineDash.DashLen );
     310                 :            :                     }
     311         [ #  # ]:          0 :                     aStrValue = aOut.makeStringAndClear();
     312         [ #  # ]:          0 :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DOTS2_LENGTH, aStrValue );
     313                 :            :                 }
     314                 :            :             }
     315                 :            : 
     316                 :            :             // distance
     317         [ #  # ]:          0 :             if( bIsRel )
     318                 :            :             {
     319         [ #  # ]:          0 :                 ::sax::Converter::convertPercent( aOut, aLineDash.Distance );
     320                 :            :             }
     321                 :            :             else
     322                 :            :             {
     323                 :            :                 rUnitConverter.convertMeasureToXML( aOut,
     324         [ #  # ]:          0 :                         aLineDash.Distance );
     325                 :            :             }
     326         [ #  # ]:          0 :             aStrValue = aOut.makeStringAndClear();
     327         [ #  # ]:          0 :             rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISTANCE, aStrValue );
     328                 :            : 
     329                 :            : 
     330                 :            :             // do Write
     331                 :            :             SvXMLElementExport rElem( rExport,
     332                 :            :                                       XML_NAMESPACE_DRAW, XML_STROKE_DASH,
     333 [ #  # ][ #  # ]:          0 :                                       sal_True, sal_False );
     334                 :            :         }
     335                 :            :     }
     336                 :          0 :     return bRet;
     337                 :            : }
     338                 :            : 
     339                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10