LCOV - code coverage report
Current view: top level - xmloff/source/style - TransGradientStyle.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 63 112 56.2 %
Date: 2012-08-25 Functions: 5 6 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 128 24.2 %

           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 "TransGradientStyle.hxx"
      31                 :            : 
      32                 :            : #include <com/sun/star/awt/Gradient.hpp>
      33                 :            : 
      34                 :            : #include <sax/tools/converter.hxx>
      35                 :            : 
      36                 :            : #include <xmloff/attrlist.hxx>
      37                 :            : #include <xmloff/nmspmap.hxx>
      38                 :            : #include <xmloff/xmluconv.hxx>
      39                 :            : #include "xmloff/xmlnmspe.hxx"
      40                 :            : #include <rtl/ustrbuf.hxx>
      41                 :            : #include <rtl/ustring.hxx>
      42                 :            : #include <tools/color.hxx>
      43                 :            : #include <tools/debug.hxx>
      44                 :            : #include <xmloff/xmltkmap.hxx>
      45                 :            : #include <xmloff/xmlexp.hxx>
      46                 :            : #include <xmloff/xmlimp.hxx>
      47                 :            : 
      48                 :            : 
      49                 :            : using namespace ::com::sun::star;
      50                 :            : using ::rtl::OUString;
      51                 :            : using ::rtl::OUStringBuffer;
      52                 :            : 
      53                 :            : using namespace ::xmloff::token;
      54                 :            : 
      55                 :            : enum SvXMLTokenMapAttrs
      56                 :            : {
      57                 :            :     XML_TOK_GRADIENT_NAME,
      58                 :            :     XML_TOK_GRADIENT_DISPLAY_NAME,
      59                 :            :     XML_TOK_GRADIENT_STYLE,
      60                 :            :     XML_TOK_GRADIENT_CX,
      61                 :            :     XML_TOK_GRADIENT_CY,
      62                 :            :     XML_TOK_GRADIENT_START,
      63                 :            :     XML_TOK_GRADIENT_END,
      64                 :            :     XML_TOK_GRADIENT_ANGLE,
      65                 :            :     XML_TOK_GRADIENT_BORDER,
      66                 :            :     XML_TOK_TABSTOP_END=XML_TOK_UNKNOWN
      67                 :            : };
      68                 :            : 
      69                 :            : 
      70                 :            : SvXMLEnumMapEntry const pXML_GradientStyle_Enum[] =
      71                 :            : {
      72                 :            :     { XML_GRADIENTSTYLE_LINEAR,         awt::GradientStyle_LINEAR },
      73                 :            :     { XML_GRADIENTSTYLE_AXIAL,          awt::GradientStyle_AXIAL },
      74                 :            :     { XML_GRADIENTSTYLE_RADIAL,         awt::GradientStyle_RADIAL },
      75                 :            :     { XML_GRADIENTSTYLE_ELLIPSOID,      awt::GradientStyle_ELLIPTICAL },
      76                 :            :     { XML_GRADIENTSTYLE_SQUARE,         awt::GradientStyle_SQUARE },
      77                 :            :     { XML_GRADIENTSTYLE_RECTANGULAR,    awt::GradientStyle_RECT },
      78                 :            :     { XML_TOKEN_INVALID,                0 }
      79                 :            : };
      80                 :            : 
      81                 :            : 
      82                 :            : //-------------------------------------------------------------
      83                 :            : // Import
      84                 :            : //-------------------------------------------------------------
      85                 :            : 
      86                 :         14 : XMLTransGradientStyleImport::XMLTransGradientStyleImport( SvXMLImport& rImp )
      87                 :         14 :     : rImport(rImp)
      88                 :            : {
      89                 :         14 : }
      90                 :            : 
      91                 :         14 : XMLTransGradientStyleImport::~XMLTransGradientStyleImport()
      92                 :            : {
      93                 :         14 : }
      94                 :            : 
      95                 :         14 : sal_Bool XMLTransGradientStyleImport::importXML(
      96                 :            :     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
      97                 :            :     uno::Any& rValue,
      98                 :            :     OUString& rStrName )
      99                 :            : {
     100                 :         14 :     sal_Bool bRet           = sal_False;
     101                 :         14 :     sal_Bool bHasName       = sal_False;
     102                 :         14 :     sal_Bool bHasStyle      = sal_False;
     103                 :         14 :     OUString aDisplayName;
     104                 :            : 
     105                 :         14 :     awt::Gradient aGradient;
     106                 :         14 :     aGradient.XOffset = 0;
     107                 :         14 :     aGradient.YOffset = 0;
     108                 :         14 :     aGradient.StartIntensity = 100;
     109                 :         14 :     aGradient.EndIntensity = 100;
     110                 :         14 :     aGradient.Angle = 0;
     111                 :         14 :     aGradient.Border = 0;
     112                 :            : 
     113                 :            :     {
     114                 :            :         static SvXMLTokenMapEntry aTrGradientAttrTokenMap[] =
     115                 :            : {
     116                 :            :     { XML_NAMESPACE_DRAW, XML_NAME, XML_TOK_GRADIENT_NAME },
     117                 :            :     { XML_NAMESPACE_DRAW, XML_DISPLAY_NAME, XML_TOK_GRADIENT_DISPLAY_NAME },
     118                 :            :     { XML_NAMESPACE_DRAW, XML_STYLE, XML_TOK_GRADIENT_STYLE },
     119                 :            :     { XML_NAMESPACE_DRAW, XML_CX, XML_TOK_GRADIENT_CX },
     120                 :            :     { XML_NAMESPACE_DRAW, XML_CY, XML_TOK_GRADIENT_CY },
     121                 :            :     { XML_NAMESPACE_DRAW, XML_START, XML_TOK_GRADIENT_START },
     122                 :            :     { XML_NAMESPACE_DRAW, XML_END, XML_TOK_GRADIENT_END },
     123                 :            :     { XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, XML_TOK_GRADIENT_ANGLE },
     124                 :            :     { XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, XML_TOK_GRADIENT_BORDER },
     125                 :            :     XML_TOKEN_MAP_END
     126                 :            : };
     127                 :            : 
     128         [ +  - ]:         14 :     SvXMLTokenMap aTokenMap( aTrGradientAttrTokenMap );
     129                 :         14 :     SvXMLNamespaceMap& rNamespaceMap = rImport.GetNamespaceMap();
     130                 :            : 
     131 [ +  - ][ +  - ]:         14 :     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
                 [ +  - ]
     132         [ +  + ]:        112 :     for( sal_Int16 i=0; i < nAttrCount; i++ )
     133                 :            :     {
     134 [ +  - ][ +  - ]:         98 :         const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
     135                 :         98 :         OUString aStrAttrName;
     136         [ +  - ]:         98 :         sal_uInt16 nPrefix = rNamespaceMap.GetKeyByAttrName( rFullAttrName, &aStrAttrName );
     137 [ +  - ][ +  - ]:         98 :         const OUString& rStrValue = xAttrList->getValueByIndex( i );
     138                 :            : 
     139                 :            :         sal_Int32 nTmpValue;
     140                 :            : 
     141 [ +  - ][ +  +  :         98 :         switch( aTokenMap.Get( nPrefix, aStrAttrName ) )
          +  -  -  +  +  
                +  +  - ]
     142                 :            :         {
     143                 :            :         case XML_TOK_GRADIENT_NAME:
     144                 :            :             {
     145                 :         14 :                 rStrName = rStrValue;
     146                 :         14 :                 bHasName = sal_True;
     147                 :            :             }
     148                 :         14 :             break;
     149                 :            :         case XML_TOK_GRADIENT_DISPLAY_NAME:
     150                 :            :             {
     151                 :         14 :                 aDisplayName = rStrValue;
     152                 :            :             }
     153                 :         14 :             break;
     154                 :            :         case XML_TOK_GRADIENT_STYLE:
     155                 :            :             {
     156                 :            :                 sal_uInt16 eValue;
     157 [ +  - ][ +  - ]:         14 :                 if( SvXMLUnitConverter::convertEnum( eValue, rStrValue, pXML_GradientStyle_Enum ) )
     158                 :            :                 {
     159                 :         14 :                     aGradient.Style = (awt::GradientStyle) eValue;
     160                 :         14 :                     bHasStyle = sal_True;
     161                 :            :                 }
     162                 :            :             }
     163                 :         14 :             break;
     164                 :            :         case XML_TOK_GRADIENT_CX:
     165         [ #  # ]:          0 :             ::sax::Converter::convertPercent( nTmpValue, rStrValue );
     166                 :          0 :             aGradient.XOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
     167                 :          0 :             break;
     168                 :            :         case XML_TOK_GRADIENT_CY:
     169         [ #  # ]:          0 :             ::sax::Converter::convertPercent( nTmpValue, rStrValue );
     170                 :          0 :             aGradient.YOffset = sal::static_int_cast< sal_Int16 >(nTmpValue);
     171                 :          0 :             break;
     172                 :            :         case XML_TOK_GRADIENT_START:
     173                 :            :             {
     174                 :            :                 sal_Int32 aStartTransparency;
     175         [ +  - ]:         14 :                 ::sax::Converter::convertPercent( aStartTransparency, rStrValue );
     176                 :            : 
     177                 :            :                 sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
     178                 :         14 :                     ( (100 - aStartTransparency) * 255 ) / 100 );
     179                 :            : 
     180                 :         14 :                 Color aColor( n, n, n );
     181                 :         14 :                 aGradient.StartColor = (sal_Int32)( aColor.GetColor() );
     182                 :            :             }
     183                 :         14 :             break;
     184                 :            :         case XML_TOK_GRADIENT_END:
     185                 :            :             {
     186                 :            :                 sal_Int32 aEndTransparency;
     187         [ +  - ]:         14 :                 ::sax::Converter::convertPercent( aEndTransparency, rStrValue );
     188                 :            : 
     189                 :            :                 sal_uInt8 n = sal::static_int_cast< sal_uInt8 >(
     190                 :         14 :                     ( (100 - aEndTransparency) * 255 ) / 100 );
     191                 :            : 
     192                 :         14 :                 Color aColor( n, n, n );
     193                 :         14 :                 aGradient.EndColor = (sal_Int32)( aColor.GetColor() );
     194                 :            :             }
     195                 :         14 :             break;
     196                 :            :         case XML_TOK_GRADIENT_ANGLE:
     197                 :            :             {
     198                 :            :                 sal_Int32 nValue;
     199         [ +  - ]:         14 :                 ::sax::Converter::convertNumber( nValue, rStrValue, 0, 3600 );
     200                 :         14 :                 aGradient.Angle = sal_Int16( nValue );
     201                 :            :             }
     202                 :         14 :             break;
     203                 :            :         case XML_TOK_GRADIENT_BORDER:
     204         [ +  - ]:         14 :             ::sax::Converter::convertPercent( nTmpValue, rStrValue );
     205                 :         14 :             aGradient.Border = sal::static_int_cast< sal_Int16 >(nTmpValue);
     206                 :         14 :             break;
     207                 :            : 
     208                 :            :         default:
     209                 :            :             DBG_WARNING( "Unknown token at import transparency gradient style" )
     210                 :            :             ;
     211                 :            :         }
     212                 :         98 :     }
     213                 :            : 
     214         [ +  - ]:         14 :     rValue <<= aGradient;
     215                 :            : 
     216         [ +  - ]:         14 :     if( !aDisplayName.isEmpty() )
     217                 :            :     {
     218                 :            :         rImport.AddStyleDisplayName( XML_STYLE_FAMILY_SD_GRADIENT_ID, rStrName,
     219         [ +  - ]:         14 :                                      aDisplayName );
     220                 :         14 :         rStrName = aDisplayName;
     221                 :            :     }
     222                 :            : 
     223 [ +  - ][ +  - ]:         14 :     bRet = bHasName && bHasStyle;
                 [ +  - ]
     224                 :            : 
     225                 :            :     }
     226                 :            : 
     227                 :         14 :     return bRet;
     228                 :            : }
     229                 :            : 
     230                 :            : 
     231                 :            : //-------------------------------------------------------------
     232                 :            : // Export
     233                 :            : //-------------------------------------------------------------
     234                 :            : 
     235                 :         46 : XMLTransGradientStyleExport::XMLTransGradientStyleExport( SvXMLExport& rExp )
     236                 :         46 :     : rExport(rExp)
     237                 :            : {
     238                 :         46 : }
     239                 :            : 
     240                 :         46 : XMLTransGradientStyleExport::~XMLTransGradientStyleExport()
     241                 :            : {
     242                 :         46 : }
     243                 :            : 
     244                 :            : 
     245                 :          0 : sal_Bool XMLTransGradientStyleExport::exportXML(
     246                 :            :     const OUString& rStrName,
     247                 :            :     const uno::Any& rValue )
     248                 :            : {
     249                 :          0 :     sal_Bool bRet = sal_False;
     250                 :          0 :     awt::Gradient aGradient;
     251                 :            : 
     252         [ #  # ]:          0 :     if( !rStrName.isEmpty() )
     253                 :            :     {
     254 [ #  # ][ #  # ]:          0 :         if( rValue >>= aGradient )
     255                 :            :         {
     256                 :          0 :             OUString aStrValue;
     257                 :          0 :             OUStringBuffer aOut;
     258                 :            : 
     259                 :            :             // Style
     260 [ #  # ][ #  # ]:          0 :             if( !SvXMLUnitConverter::convertEnum( aOut, aGradient.Style, pXML_GradientStyle_Enum ) )
     261                 :            :             {
     262                 :          0 :                 bRet = sal_False;
     263                 :            :             }
     264                 :            :             else
     265                 :            :             {
     266                 :            :                 // Name
     267                 :          0 :                 sal_Bool bEncoded = sal_False;
     268                 :            :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_NAME,
     269                 :            :                                       rExport.EncodeStyleName( rStrName,
     270 [ #  # ][ #  # ]:          0 :                                                                 &bEncoded ) );
     271         [ #  # ]:          0 :                 if( bEncoded )
     272                 :            :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_DISPLAY_NAME,
     273         [ #  # ]:          0 :                                             rStrName );
     274                 :            : 
     275         [ #  # ]:          0 :                 aStrValue = aOut.makeStringAndClear();
     276         [ #  # ]:          0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_STYLE, aStrValue );
     277                 :            : 
     278                 :            :                 // Center x/y
     279 [ #  # ][ #  # ]:          0 :                 if( aGradient.Style != awt::GradientStyle_LINEAR &&
     280                 :            :                     aGradient.Style != awt::GradientStyle_AXIAL   )
     281                 :            :                 {
     282         [ #  # ]:          0 :                     ::sax::Converter::convertPercent(aOut, aGradient.XOffset);
     283         [ #  # ]:          0 :                     aStrValue = aOut.makeStringAndClear();
     284         [ #  # ]:          0 :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CX, aStrValue );
     285                 :            : 
     286         [ #  # ]:          0 :                     ::sax::Converter::convertPercent(aOut, aGradient.YOffset);
     287         [ #  # ]:          0 :                     aStrValue = aOut.makeStringAndClear();
     288         [ #  # ]:          0 :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_CY, aStrValue );
     289                 :            :                 }
     290                 :            : 
     291                 :            : 
     292                 :          0 :                 Color aColor;
     293                 :            : 
     294                 :            :                 // Transparency start
     295                 :          0 :                 aColor.SetColor( aGradient.StartColor );
     296                 :          0 :                 sal_Int32 aStartValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
     297         [ #  # ]:          0 :                 ::sax::Converter::convertPercent( aOut, aStartValue );
     298         [ #  # ]:          0 :                 aStrValue = aOut.makeStringAndClear();
     299         [ #  # ]:          0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_START, aStrValue );
     300                 :            : 
     301                 :            :                 // Transparency end
     302                 :          0 :                 aColor.SetColor( aGradient.EndColor );
     303                 :          0 :                 sal_Int32 aEndValue = 100 - (sal_Int32)(((aColor.GetRed() + 1) * 100) / 255);
     304         [ #  # ]:          0 :                 ::sax::Converter::convertPercent( aOut, aEndValue );
     305         [ #  # ]:          0 :                 aStrValue = aOut.makeStringAndClear();
     306         [ #  # ]:          0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_END, aStrValue );
     307                 :            : 
     308                 :            :                 // Angle
     309         [ #  # ]:          0 :                 if( aGradient.Style != awt::GradientStyle_RADIAL )
     310                 :            :                 {
     311                 :            :                     ::sax::Converter::convertNumber(
     312         [ #  # ]:          0 :                             aOut, sal_Int32(aGradient.Angle));
     313         [ #  # ]:          0 :                     aStrValue = aOut.makeStringAndClear();
     314         [ #  # ]:          0 :                     rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_ANGLE, aStrValue );
     315                 :            :                 }
     316                 :            : 
     317                 :            :                 // Border
     318         [ #  # ]:          0 :                 ::sax::Converter::convertPercent( aOut, aGradient.Border );
     319         [ #  # ]:          0 :                 aStrValue = aOut.makeStringAndClear();
     320         [ #  # ]:          0 :                 rExport.AddAttribute( XML_NAMESPACE_DRAW, XML_GRADIENT_BORDER, aStrValue );
     321                 :            : 
     322                 :            :                 // Do Write
     323                 :            :                 SvXMLElementExport rElem( rExport,
     324                 :            :                                           XML_NAMESPACE_DRAW, XML_OPACITY,
     325 [ #  # ][ #  # ]:          0 :                                           sal_True, sal_False );
     326                 :          0 :             }
     327                 :            :         }
     328                 :            :     }
     329                 :            : 
     330                 :          0 :     return bRet;
     331                 :            : }
     332                 :            : 
     333                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10