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

Generated by: LCOV version 1.11