LCOV - code coverage report
Current view: top level - libreoffice/xmloff/source/style - fonthdl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 114 121 94.2 %
Date: 2012-12-27 Functions: 17 17 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include <fonthdl.hxx>
      22             : 
      23             : #include <sax/tools/converter.hxx>
      24             : 
      25             : #include <xmloff/xmltoken.hxx>
      26             : #include <xmloff/xmluconv.hxx>
      27             : #include <rtl/ustrbuf.hxx>
      28             : #include <com/sun/star/uno/Any.hxx>
      29             : #include <tools/fontenum.hxx>
      30             : 
      31             : using ::rtl::OUString;
      32             : using ::rtl::OUStringBuffer;
      33             : 
      34             : using namespace ::com::sun::star;
      35             : using namespace ::xmloff::token;
      36             : 
      37         430 : static const SvXMLEnumMapEntry* lcl_getFontFamilyGenericMapping()
      38             : {
      39             :     static SvXMLEnumMapEntry const aFontFamilyGenericMapping[] =
      40             :     {
      41             :         { XML_DECORATIVE,       FAMILY_DECORATIVE },
      42             : 
      43             :         { XML_MODERN,           FAMILY_MODERN   },
      44             :         { XML_ROMAN,            FAMILY_ROMAN    },
      45             :         { XML_SCRIPT,           FAMILY_SCRIPT   },
      46             :         { XML_SWISS,            FAMILY_SWISS    },
      47             :         { XML_SYSTEM,           FAMILY_SYSTEM   },
      48             :         { XML_TOKEN_INVALID,    0               }
      49             :     };
      50         430 :     return aFontFamilyGenericMapping;
      51             : }
      52             : 
      53             : static SvXMLEnumMapEntry const aFontPitchMapping[] =
      54             : {
      55             :     { XML_FIXED,            PITCH_FIXED     },
      56             :     { XML_VARIABLE,         PITCH_VARIABLE  },
      57             :     { XML_TOKEN_INVALID,    0               }
      58             : };
      59             : ///////////////////////////////////////////////////////////////////////////////
      60             : //
      61             : // class XMLFontFamilyNamePropHdl
      62             : //
      63             : 
      64        1236 : XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
      65             : {
      66             :     // Nothing to do
      67        1236 : }
      68             : 
      69         510 : sal_Bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
      70             : {
      71         510 :     sal_Bool bRet = sal_False;
      72         510 :     OUStringBuffer sValue;
      73         510 :     sal_Int32 nPos = 0;
      74             : 
      75         542 :     do
      76             :     {
      77         542 :         sal_Int32 nFirst = nPos;
      78         542 :         nPos = ::sax::Converter::indexOfComma( rStrImpValue, nPos );
      79         542 :         sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() - 1 : nPos - 1);
      80             : 
      81             :         // skip trailing blanks
      82        1084 :         while( nLast > nFirst && sal_Unicode(' ') == rStrImpValue[nLast] )
      83           0 :             nLast--;
      84             : 
      85             :         // skip leading blanks
      86        1116 :         while(nFirst <= nLast && sal_Unicode(' ') == rStrImpValue[nFirst])
      87          32 :             nFirst++;
      88             : 
      89             :         // remove quotes
      90         542 :         sal_Unicode c = rStrImpValue[nFirst];
      91         542 :         if( nFirst < nLast && (sal_Unicode('\'') == c || sal_Unicode('\"') == c) && rStrImpValue[nLast] == c )
      92             :         {
      93         279 :             nFirst++;
      94         279 :             nLast--;
      95             :         }
      96             : 
      97         542 :         if( nFirst <= nLast )
      98             :         {
      99         540 :             if( sValue.getLength() != 0 )
     100          32 :                 sValue.append(';');
     101             : 
     102         540 :             sValue.append(rStrImpValue.copy( nFirst, nLast-nFirst+1));
     103             :         }
     104             : 
     105         542 :         if( -1 != nPos )
     106          32 :             nPos++;
     107             :     }
     108             :     while( -1 != nPos );
     109             : 
     110         510 :     if (sValue.getLength())
     111             :     {
     112         508 :         rValue <<= sValue.makeStringAndClear();
     113         508 :         bRet = sal_True;
     114             :     }
     115             : 
     116         510 :     return bRet;
     117             : }
     118             : 
     119          44 : sal_Bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
     120             : {
     121          44 :     sal_Bool bRet = sal_False;
     122          44 :     OUString aStrFamilyName;
     123             : 
     124          44 :     if( rValue >>= aStrFamilyName )
     125             :     {
     126          44 :         OUStringBuffer sValue( aStrFamilyName.getLength() + 2L );
     127          44 :         sal_Int32 nPos = 0;
     128          76 :         do
     129             :         {
     130          76 :             sal_Int32 nFirst = nPos;
     131          76 :             nPos = aStrFamilyName.indexOf( sal_Unicode(';'), nPos );
     132          76 :             sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
     133             : 
     134             :             // Set position to the character behind the ';', so we won't
     135             :             // forget this.
     136          76 :             if( -1L != nPos )
     137          32 :                 nPos++;
     138             : 
     139             :             // If the property value was empty, we stop now.
     140             :             // If there is a ';' at the first position, the empty name
     141             :             // at the start will be removed.
     142          76 :             if( 0L == nLast )
     143           0 :                 continue;
     144             : 
     145             :             // nFirst and nLast now denote the first and last character of
     146             :             // one font name.
     147          76 :             nLast--;
     148             : 
     149             :             // skip trailing blanks
     150         152 :             while(  nLast > nFirst && sal_Unicode(' ') == aStrFamilyName[nLast] )
     151           0 :                 nLast--;
     152             : 
     153             :             // skip leading blanks
     154         152 :             while( nFirst <= nLast && sal_Unicode(' ') == aStrFamilyName[nFirst] )
     155           0 :                 nFirst++;
     156             : 
     157          76 :             if( nFirst <= nLast )
     158             :             {
     159          76 :                 if( sValue.getLength() != 0L )
     160             :                 {
     161          32 :                     sValue.append( sal_Unicode( ',' ) );
     162          32 :                     sValue.append( sal_Unicode( ' ' ));
     163             :                 }
     164          76 :                 sal_Int32 nLen = nLast-nFirst+1;
     165          76 :                 OUString sFamily( aStrFamilyName.copy( nFirst, nLen ) );
     166          76 :                 sal_Bool bQuote = sal_False;
     167         580 :                 for( sal_Int32 i=0; i < nLen; i++ )
     168             :                 {
     169         548 :                     sal_Unicode c = sFamily[i];
     170         548 :                     if( sal_Unicode(' ') == c || sal_Unicode(',') == c )
     171             :                     {
     172          44 :                         bQuote = sal_True;
     173          44 :                         break;
     174             :                     }
     175             :                 }
     176          76 :                 if( bQuote )
     177          44 :                     sValue.append( sal_Unicode('\'') );
     178          76 :                 sValue.append( sFamily );
     179          76 :                 if( bQuote )
     180          44 :                     sValue.append( sal_Unicode('\'') );
     181             :             }
     182             :         }
     183             :         while( -1L != nPos );
     184             : 
     185          44 :         rStrExpValue = sValue.makeStringAndClear();
     186             : 
     187          44 :         bRet = sal_True;
     188             :     }
     189             : 
     190          44 :     return bRet;
     191             : }
     192             : 
     193             : ///////////////////////////////////////////////////////////////////////////////
     194             : //
     195             : // class XMLFontFamilyPropHdl
     196             : //
     197             : 
     198        1244 : XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
     199             : {
     200             :     // Nothing to do
     201        1244 : }
     202             : 
     203         386 : sal_Bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
     204             : {
     205             :     sal_uInt16 eNewFamily;
     206         386 :     sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
     207         386 :     if( bRet )
     208         386 :         rValue <<= (sal_Int16)eNewFamily;
     209             : 
     210         386 :     return bRet;
     211             : }
     212             : 
     213          44 : sal_Bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
     214             : {
     215          44 :     sal_Bool bRet = sal_False;
     216          44 :     OUStringBuffer aOut;
     217             : 
     218          44 :     sal_Int16 nFamily = sal_Int16();
     219          44 :     if( rValue >>= nFamily )
     220             :     {
     221          44 :         FontFamily eFamily = (FontFamily)nFamily;
     222          44 :         if( eFamily != FAMILY_DONTKNOW )
     223          44 :             bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
     224             :     }
     225             : 
     226          44 :     rStrExpValue = aOut.makeStringAndClear();
     227             : 
     228          44 :     return bRet;
     229             : }
     230             : 
     231             : ///////////////////////////////////////////////////////////////////////////////
     232             : //
     233             : // class XMLFontEncodingPropHdl
     234             : //
     235             : 
     236        1244 : XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
     237             : {
     238             :     // Nothing to do
     239        1244 : }
     240             : 
     241          42 : sal_Bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
     242             : {
     243          42 :     sal_Bool bRet = sal_True;
     244             : 
     245          42 :     if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
     246          42 :         rValue <<= (sal_Int16) RTL_TEXTENCODING_SYMBOL;
     247             : 
     248          42 :     return bRet;
     249             : }
     250             : 
     251          44 : sal_Bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
     252             : {
     253          44 :     sal_Bool bRet = sal_False;
     254          44 :     OUStringBuffer aOut;
     255          44 :     sal_Int16 nSet = sal_Int16();
     256             : 
     257          44 :     if( rValue >>= nSet )
     258             :     {
     259          44 :         if( (rtl_TextEncoding)nSet == RTL_TEXTENCODING_SYMBOL )
     260             :         {
     261           0 :             aOut.append( GetXMLToken(XML_X_SYMBOL) );
     262           0 :             rStrExpValue = aOut.makeStringAndClear();
     263           0 :             bRet = sal_True;
     264             :         }
     265             :     }
     266             : 
     267          44 :     return bRet;
     268             : }
     269             : 
     270             : ///////////////////////////////////////////////////////////////////////////////
     271             : //
     272             : // class XMLFontPitchPropHdl
     273             : //
     274             : 
     275        1244 : XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
     276             : {
     277             :     // Nothing to do
     278        1244 : }
     279             : 
     280         368 : sal_Bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
     281             : {
     282             :     sal_uInt16 eNewPitch;
     283         368 :     sal_Bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
     284         368 :     if( bRet )
     285         368 :         rValue <<= (sal_Int16)eNewPitch;
     286             : 
     287         368 :     return bRet;
     288             : }
     289             : 
     290          44 : sal_Bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
     291             : {
     292          44 :     sal_Bool bRet = sal_False;
     293          44 :     sal_Int16 nPitch = sal_Int16();
     294          44 :     OUStringBuffer aOut;
     295             : 
     296          44 :     FontPitch ePitch = PITCH_DONTKNOW;
     297          44 :     if( rValue >>= nPitch )
     298          44 :         ePitch =  (FontPitch)nPitch;
     299             : 
     300          44 :     if( PITCH_DONTKNOW != ePitch )
     301             :     {
     302          38 :         bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
     303          38 :         rStrExpValue = aOut.makeStringAndClear();
     304             :     }
     305             : 
     306          44 :     return bRet;
     307             : }
     308             : 
     309             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10