LCOV - code coverage report
Current view: top level - libreoffice/xmloff/source/style - XMLBackgroundImageContext.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 32 139 23.0 %
Date: 2012-12-27 Functions: 6 14 42.9 %
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/io/XOutputStream.hpp>
      22             : 
      23             : #include <tools/debug.hxx>
      24             : 
      25             : #include <sax/tools/converter.hxx>
      26             : 
      27             : #include <xmloff/xmltkmap.hxx>
      28             : #include <xmloff/xmluconv.hxx>
      29             : #include "xmloff/xmlnmspe.hxx"
      30             : #include <xmloff/xmltoken.hxx>
      31             : #include <xmloff/xmlimp.hxx>
      32             : #include <xmloff/nmspmap.hxx>
      33             : #include <xmloff/XMLBase64ImportContext.hxx>
      34             : #include "XMLBackgroundImageContext.hxx"
      35             : 
      36             : using ::rtl::OUString;
      37             : using ::rtl::OUStringBuffer;
      38             : 
      39             : using namespace ::com::sun::star;
      40             : using namespace ::com::sun::star::uno;
      41             : using namespace ::com::sun::star::style;
      42             : using namespace ::com::sun::star::io;
      43             : using namespace ::xmloff::token;
      44             : 
      45             : enum SvXMLTokenMapAttrs
      46             : {
      47             :     XML_TOK_BGIMG_HREF,
      48             :     XML_TOK_BGIMG_TYPE,
      49             :     XML_TOK_BGIMG_ACTUATE,
      50             :     XML_TOK_BGIMG_SHOW,
      51             :     XML_TOK_BGIMG_POSITION,
      52             :     XML_TOK_BGIMG_REPEAT,
      53             :     XML_TOK_BGIMG_FILTER,
      54             :     XML_TOK_BGIMG_OPACITY,
      55             :     XML_TOK_NGIMG_END=XML_TOK_UNKNOWN
      56             : };
      57          47 : static const SvXMLTokenMapEntry* lcl_getBGImgAttributesAttrTokenMap()
      58             : {
      59             :     static SvXMLTokenMapEntry aBGImgAttributesAttrTokenMap[] =
      60             :     {
      61             :         { XML_NAMESPACE_XLINK, XML_HREF,        XML_TOK_BGIMG_HREF      },
      62             :         { XML_NAMESPACE_XLINK, XML_TYPE,        XML_TOK_BGIMG_TYPE      },
      63             :         { XML_NAMESPACE_XLINK, XML_ACTUATE,     XML_TOK_BGIMG_ACTUATE   },
      64             :         { XML_NAMESPACE_XLINK, XML_SHOW,        XML_TOK_BGIMG_SHOW      },
      65             :         { XML_NAMESPACE_STYLE, XML_POSITION,    XML_TOK_BGIMG_POSITION  },
      66             :         { XML_NAMESPACE_STYLE, XML_REPEAT,      XML_TOK_BGIMG_REPEAT    },
      67             :         { XML_NAMESPACE_STYLE, XML_FILTER_NAME, XML_TOK_BGIMG_FILTER    },
      68             :         { XML_NAMESPACE_DRAW,  XML_OPACITY,     XML_TOK_BGIMG_OPACITY   },
      69             :         XML_TOKEN_MAP_END
      70             :     };
      71          47 :     return aBGImgAttributesAttrTokenMap;
      72             : }
      73             : 
      74             : 
      75             : 
      76             : static SvXMLEnumMapEntry psXML_BrushHoriPos[] =
      77             : {
      78             :     { XML_LEFT,         GraphicLocation_LEFT_MIDDLE },
      79             :     { XML_RIGHT,        GraphicLocation_RIGHT_MIDDLE    },
      80             :     { XML_TOKEN_INVALID,                    0           }
      81             : };
      82             : 
      83             : static SvXMLEnumMapEntry psXML_BrushVertPos[] =
      84             : {
      85             :     { XML_TOP,          GraphicLocation_MIDDLE_TOP  },
      86             :     { XML_BOTTOM,       GraphicLocation_MIDDLE_BOTTOM   },
      87             :     { XML_TOKEN_INVALID,                    0           }
      88             : };
      89             : 
      90           0 : static void lcl_xmlbic_MergeHoriPos( GraphicLocation& ePos,
      91             :                                    GraphicLocation eHori )
      92             : {
      93             :     DBG_ASSERT( GraphicLocation_LEFT_MIDDLE==eHori ||
      94             :                 GraphicLocation_MIDDLE_MIDDLE==eHori ||
      95             :                 GraphicLocation_RIGHT_MIDDLE==eHori,
      96             :                 "lcl_xmlbic_MergeHoriPos: vertical pos must be middle" );
      97             : 
      98           0 :     switch( ePos )
      99             :     {
     100             :     case GraphicLocation_LEFT_TOP:
     101             :     case GraphicLocation_MIDDLE_TOP:
     102             :     case GraphicLocation_RIGHT_TOP:
     103             :         ePos = GraphicLocation_LEFT_MIDDLE==eHori
     104             :                 ? GraphicLocation_LEFT_TOP
     105             :                 : (GraphicLocation_MIDDLE_MIDDLE==eHori
     106             :                         ? GraphicLocation_MIDDLE_TOP
     107           0 :                         : GraphicLocation_RIGHT_TOP);
     108           0 :         break;
     109             : 
     110             :     case GraphicLocation_LEFT_MIDDLE:
     111             :     case GraphicLocation_MIDDLE_MIDDLE:
     112             :     case GraphicLocation_RIGHT_MIDDLE:
     113           0 :         ePos = eHori;
     114           0 :         break;
     115             : 
     116             :     case GraphicLocation_LEFT_BOTTOM:
     117             :     case GraphicLocation_MIDDLE_BOTTOM:
     118             :     case GraphicLocation_RIGHT_BOTTOM:
     119             :         ePos = GraphicLocation_LEFT_MIDDLE==eHori
     120             :                 ? GraphicLocation_LEFT_BOTTOM
     121             :                 : (GraphicLocation_MIDDLE_MIDDLE==eHori
     122             :                         ? GraphicLocation_MIDDLE_BOTTOM
     123           0 :                         : GraphicLocation_RIGHT_BOTTOM);
     124           0 :         break;
     125             :     default:
     126           0 :         break;
     127             :     }
     128           0 : }
     129             : 
     130           0 : static void lcl_xmlbic_MergeVertPos( GraphicLocation& ePos,
     131             :                                                    GraphicLocation eVert )
     132             : {
     133             :     DBG_ASSERT( GraphicLocation_MIDDLE_TOP==eVert ||
     134             :                 GraphicLocation_MIDDLE_MIDDLE==eVert ||
     135             :                 GraphicLocation_MIDDLE_BOTTOM==eVert,
     136             :                 "lcl_xmlbic_MergeVertPos: horizontal pos must be middle" );
     137             : 
     138           0 :     switch( ePos )
     139             :     {
     140             :     case GraphicLocation_LEFT_TOP:
     141             :     case GraphicLocation_LEFT_MIDDLE:
     142             :     case GraphicLocation_LEFT_BOTTOM:
     143             :         ePos = GraphicLocation_MIDDLE_TOP==eVert
     144             :                 ? GraphicLocation_LEFT_TOP
     145             :                 : (GraphicLocation_MIDDLE_MIDDLE==eVert
     146             :                         ? GraphicLocation_LEFT_MIDDLE
     147           0 :                         : GraphicLocation_LEFT_BOTTOM);
     148           0 :         break;
     149             : 
     150             :     case GraphicLocation_MIDDLE_TOP:
     151             :     case GraphicLocation_MIDDLE_MIDDLE:
     152             :     case GraphicLocation_MIDDLE_BOTTOM:
     153           0 :         ePos = eVert;
     154           0 :         break;
     155             : 
     156             :     case GraphicLocation_RIGHT_TOP:
     157             :     case GraphicLocation_RIGHT_MIDDLE:
     158             :     case GraphicLocation_RIGHT_BOTTOM:
     159             :         ePos = GraphicLocation_MIDDLE_TOP==eVert
     160             :                 ? GraphicLocation_RIGHT_TOP
     161             :                 : (GraphicLocation_MIDDLE_MIDDLE==eVert
     162             :                         ? GraphicLocation_RIGHT_MIDDLE
     163           0 :                         : GraphicLocation_RIGHT_BOTTOM);
     164           0 :         break;
     165             :     default:
     166           0 :         break;
     167             :     }
     168           0 : }
     169             : 
     170           0 : TYPEINIT1( XMLBackgroundImageContext, XMLElementPropertyContext );
     171             : 
     172          47 : void XMLBackgroundImageContext::ProcessAttrs(
     173             :         const Reference< xml::sax::XAttributeList >& xAttrList )
     174             : {
     175          47 :     SvXMLTokenMap aTokenMap( lcl_getBGImgAttributesAttrTokenMap() );
     176             : 
     177          47 :     ePos = GraphicLocation_NONE;
     178             : 
     179          47 :     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
     180          47 :     for( sal_Int16 i=0; i < nAttrCount; i++ )
     181             :     {
     182           0 :         const OUString& rAttrName = xAttrList->getNameByIndex( i );
     183           0 :         OUString aLocalName;
     184             :         sal_uInt16 nPrefix =
     185           0 :             GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
     186           0 :                                                             &aLocalName );
     187           0 :         const OUString& rValue = xAttrList->getValueByIndex( i );
     188             : 
     189           0 :         switch( aTokenMap.Get( nPrefix, aLocalName ) )
     190             :         {
     191             :         case XML_TOK_BGIMG_HREF:
     192           0 :             sURL = rValue;
     193           0 :             if( GraphicLocation_NONE == ePos )
     194           0 :                 ePos = GraphicLocation_TILED;
     195           0 :             break;
     196             :         case XML_TOK_BGIMG_TYPE:
     197             :         case XML_TOK_BGIMG_ACTUATE:
     198             :         case XML_TOK_BGIMG_SHOW:
     199           0 :             break;
     200             :         case XML_TOK_BGIMG_POSITION:
     201             :             {
     202           0 :                 GraphicLocation eNewPos = GraphicLocation_NONE, eTmp;
     203             :                 sal_uInt16 nTmp;
     204           0 :                 SvXMLTokenEnumerator aTokenEnum( rValue );
     205           0 :                 OUString aToken;
     206           0 :                 sal_Bool bHori = sal_False, bVert = sal_False;
     207           0 :                 sal_Bool bOK = sal_True;
     208           0 :                 while( bOK && aTokenEnum.getNextToken( aToken ) )
     209             :                 {
     210           0 :                     if( bHori && bVert )
     211             :                     {
     212           0 :                         bOK = sal_False;
     213             :                     }
     214           0 :                     else if( -1 != aToken.indexOf( sal_Unicode('%') ) )
     215             :                     {
     216           0 :                         sal_Int32 nPrc = 50;
     217           0 :                         if (::sax::Converter::convertPercent( nPrc, aToken ))
     218             :                         {
     219           0 :                             if( !bHori )
     220             :                             {
     221             :                                 eNewPos = nPrc < 25
     222             :                                     ? GraphicLocation_LEFT_TOP
     223             :                                     : (nPrc < 75 ? GraphicLocation_MIDDLE_MIDDLE
     224           0 :                                                 : GraphicLocation_RIGHT_BOTTOM);
     225           0 :                                 bHori = sal_True;
     226             :                             }
     227             :                             else
     228             :                             {
     229             :                                 eTmp = nPrc < 25
     230             :                                     ? GraphicLocation_LEFT_TOP
     231             :                                     : (nPrc < 75 ? GraphicLocation_LEFT_MIDDLE
     232           0 :                                                  : GraphicLocation_LEFT_BOTTOM);
     233           0 :                                 lcl_xmlbic_MergeVertPos( eNewPos, eTmp );
     234           0 :                                 bVert = sal_True;
     235             :                             }
     236             :                         }
     237             :                         else
     238             :                         {
     239             :                             // wrong percentage
     240           0 :                             bOK = sal_False;
     241             :                         }
     242             :                     }
     243           0 :                     else if( IsXMLToken( aToken, XML_CENTER ) )
     244             :                     {
     245           0 :                         if( bHori )
     246             :                             lcl_xmlbic_MergeVertPos( eNewPos,
     247           0 :                                           GraphicLocation_MIDDLE_MIDDLE );
     248           0 :                         else if ( bVert )
     249             :                             lcl_xmlbic_MergeHoriPos( eNewPos,
     250           0 :                                           GraphicLocation_MIDDLE_MIDDLE );
     251             :                         else
     252           0 :                             eNewPos = GraphicLocation_MIDDLE_MIDDLE;
     253             :                     }
     254           0 :                     else if( SvXMLUnitConverter::convertEnum( nTmp, aToken,
     255           0 :                                                          psXML_BrushHoriPos ) )
     256             :                     {
     257           0 :                         if( bVert )
     258             :                             lcl_xmlbic_MergeHoriPos( eNewPos,
     259           0 :                                         (GraphicLocation)nTmp );
     260           0 :                         else if( !bHori )
     261           0 :                             eNewPos = (GraphicLocation)nTmp;
     262             :                         else
     263           0 :                             bOK = sal_False;
     264           0 :                         bHori = sal_True;
     265             :                     }
     266           0 :                     else if( SvXMLUnitConverter::convertEnum( nTmp, aToken,
     267           0 :                                                          psXML_BrushVertPos ) )
     268             :                     {
     269           0 :                         if( bHori )
     270             :                             lcl_xmlbic_MergeVertPos( eNewPos,
     271           0 :                                             (GraphicLocation)nTmp );
     272           0 :                         else if( !bVert )
     273           0 :                             eNewPos = (GraphicLocation)nTmp;
     274             :                         else
     275           0 :                             bOK = sal_False;
     276           0 :                         bVert = sal_True;
     277             :                     }
     278             :                     else
     279             :                     {
     280           0 :                         bOK = sal_False;
     281             :                     }
     282             :                 }
     283             : 
     284           0 :                 bOK &= GraphicLocation_NONE != eNewPos;
     285           0 :                 if( bOK )
     286           0 :                     ePos = eNewPos;
     287             :             }
     288           0 :             break;
     289             :         case XML_TOK_BGIMG_REPEAT:
     290             :             {
     291           0 :                 sal_uInt16 nPos = GraphicLocation_NONE;
     292             :                 static SvXMLEnumMapEntry psXML_BrushRepeat[] =
     293             :                 {
     294             :                     { XML_BACKGROUND_REPEAT,        GraphicLocation_TILED   },
     295             :                     { XML_BACKGROUND_NO_REPEAT,     GraphicLocation_MIDDLE_MIDDLE       },
     296             :                     { XML_BACKGROUND_STRETCH,       GraphicLocation_AREA    },
     297             :                     { XML_TOKEN_INVALID,            0           }
     298             :                 };
     299           0 :                 if( SvXMLUnitConverter::convertEnum( nPos, rValue,
     300           0 :                                                 psXML_BrushRepeat ) )
     301             :                 {
     302           0 :                     if( GraphicLocation_MIDDLE_MIDDLE != nPos ||
     303             :                         GraphicLocation_NONE == ePos ||
     304             :                         GraphicLocation_AREA == ePos ||
     305             :                         GraphicLocation_TILED == ePos )
     306           0 :                         ePos = (GraphicLocation)nPos;
     307             :                 }
     308             :             }
     309           0 :             break;
     310             :         case XML_TOK_BGIMG_FILTER:
     311           0 :             sFilter = rValue;
     312           0 :             break;
     313             :         case XML_TOK_BGIMG_OPACITY:
     314             :             {
     315             :                 sal_Int32 nTmp;
     316             :                 // convert from percent and clip
     317           0 :                 if (::sax::Converter::convertPercent( nTmp, rValue ))
     318             :                 {
     319           0 :                     if( (nTmp >= 0) && (nTmp <= 100) )
     320           0 :                         nTransparency = static_cast<sal_Int8>( 100-nTmp );
     321             :                 }
     322             :             }
     323           0 :             break;
     324             :         }
     325          47 :     }
     326             : 
     327          47 : }
     328             : 
     329          47 : XMLBackgroundImageContext::XMLBackgroundImageContext(
     330             :         SvXMLImport& rImport, sal_uInt16 nPrfx,
     331             :         const OUString& rLName,
     332             :         const Reference< xml::sax::XAttributeList > & xAttrList,
     333             :         const XMLPropertyState& rProp,
     334             :         sal_Int32 nPosIdx,
     335             :         sal_Int32 nFilterIdx,
     336             :         sal_Int32 nTransparencyIdx,
     337             :         ::std::vector< XMLPropertyState > &rProps ) :
     338             :     XMLElementPropertyContext( rImport, nPrfx, rLName, rProp, rProps ),
     339             :     aPosProp( nPosIdx ),
     340             :     aFilterProp( nFilterIdx ),
     341             :     aTransparencyProp( nTransparencyIdx ),
     342          47 :     nTransparency( 0 )
     343             : {
     344          47 :     ProcessAttrs( xAttrList );
     345          47 : }
     346             : 
     347          94 : XMLBackgroundImageContext::~XMLBackgroundImageContext()
     348             : {
     349          94 : }
     350             : 
     351           0 : SvXMLImportContext *XMLBackgroundImageContext::CreateChildContext(
     352             :         sal_uInt16 nPrefix, const OUString& rLocalName,
     353             :         const Reference< xml::sax::XAttributeList > & xAttrList )
     354             : {
     355           0 :     SvXMLImportContext *pContext = NULL;
     356           0 :     if( (XML_NAMESPACE_OFFICE == nPrefix) &&
     357             :         xmloff::token::IsXMLToken( rLocalName,
     358           0 :                                         xmloff::token::XML_BINARY_DATA ) )
     359             :     {
     360           0 :         if( sURL.isEmpty() && !xBase64Stream.is() )
     361             :         {
     362           0 :             xBase64Stream = GetImport().GetStreamForGraphicObjectURLFromBase64();
     363           0 :             if( xBase64Stream.is() )
     364           0 :                 pContext = new XMLBase64ImportContext( GetImport(), nPrefix,
     365             :                                                     rLocalName, xAttrList,
     366           0 :                                                     xBase64Stream );
     367             :         }
     368             :     }
     369           0 :     if( !pContext )
     370             :     {
     371           0 :         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
     372             :     }
     373             : 
     374           0 :     return pContext;
     375             : }
     376             : 
     377          47 : void XMLBackgroundImageContext::EndElement()
     378             : {
     379          47 :     if( !sURL.isEmpty() )
     380             :     {
     381           0 :         sURL = GetImport().ResolveGraphicObjectURL( sURL, sal_False );
     382             :     }
     383          47 :     else if( xBase64Stream.is() )
     384             :     {
     385           0 :         sURL = GetImport().ResolveGraphicObjectURLFromBase64( xBase64Stream );
     386           0 :         xBase64Stream = 0;
     387             :     }
     388             : 
     389          47 :     if( sURL.isEmpty() )
     390          47 :         ePos = GraphicLocation_NONE;
     391           0 :     else if( GraphicLocation_NONE == ePos )
     392           0 :         ePos = GraphicLocation_TILED;
     393             : 
     394          47 :     aProp.maValue <<= sURL;
     395          47 :     aPosProp.maValue <<= ePos;
     396          47 :     aFilterProp.maValue <<= sFilter;
     397          47 :     aTransparencyProp.maValue <<= nTransparency;
     398             : 
     399          47 :     SetInsert( sal_True );
     400          47 :     XMLElementPropertyContext::EndElement();
     401             : 
     402          47 :     if( -1 != aPosProp.mnIndex )
     403          47 :         rProperties.push_back( aPosProp );
     404          47 :     if( -1 != aFilterProp.mnIndex )
     405          47 :         rProperties.push_back( aFilterProp );
     406          47 :     if( -1 != aTransparencyProp.mnIndex )
     407           0 :         rProperties.push_back( aTransparencyProp );
     408          47 : }
     409             : 
     410             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10