LCOV - code coverage report
Current view: top level - libreoffice/xmloff/source/style - XMLFontAutoStylePool.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 99 107 92.5 %
Date: 2012-12-27 Functions: 17 18 94.4 %
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             : #include <o3tl/sorted_vector.hxx>
      21             : #include <tools/fontenum.hxx>
      22             : #include "xmloff/xmlnmspe.hxx"
      23             : #include <xmloff/xmltoken.hxx>
      24             : #include <xmloff/xmluconv.hxx>
      25             : #include "fonthdl.hxx"
      26             : #include <xmloff/xmlexp.hxx>
      27             : #include <xmloff/XMLFontAutoStylePool.hxx>
      28             : 
      29             : 
      30             : using ::rtl::OUString;
      31             : using ::rtl::OUStringBuffer;
      32             : 
      33             : using namespace ::com::sun::star::uno;
      34             : using namespace ::xmloff::token;
      35             : 
      36         154 : class XMLFontAutoStylePoolEntry_Impl
      37             : {
      38             :     OUString    sName;
      39             :     OUString    sFamilyName;
      40             :     OUString    sStyleName;
      41             :     sal_Int16   nFamily;
      42             :     sal_Int16   nPitch;
      43             :     rtl_TextEncoding eEnc;
      44             : 
      45             : public:
      46             : 
      47             :     inline XMLFontAutoStylePoolEntry_Impl(
      48             :             const ::rtl::OUString& rName,
      49             :             const ::rtl::OUString& rFamilyName,
      50             :             const ::rtl::OUString& rStyleName,
      51             :             sal_Int16 nFamily,
      52             :             sal_Int16 nPitch,
      53             :             rtl_TextEncoding eEnc );
      54             : 
      55             :     inline XMLFontAutoStylePoolEntry_Impl(
      56             :             const ::rtl::OUString& rFamilyName,
      57             :             const ::rtl::OUString& rStyleName,
      58             :             sal_Int16 nFamily,
      59             :             sal_Int16 nPitch,
      60             :             rtl_TextEncoding eEnc );
      61             : 
      62         104 :     const OUString& GetName() const { return sName; }
      63         524 :     const OUString& GetFamilyName() const { return sFamilyName; }
      64         284 :     const OUString& GetStyleName() const { return sStyleName; }
      65        1004 :     sal_Int16 GetFamily() const {   return nFamily; }
      66        1020 :     sal_Int16 GetPitch() const { return nPitch; }
      67         892 :     rtl_TextEncoding GetEncoding() const { return eEnc; }
      68             : };
      69             : 
      70             : 
      71          44 : inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
      72             :         const ::rtl::OUString& rName,
      73             :         const ::rtl::OUString& rFamilyName,
      74             :         const ::rtl::OUString& rStyleName,
      75             :         sal_Int16 nFam,
      76             :         sal_Int16 nP,
      77             :         rtl_TextEncoding eE ) :
      78             :     sName( rName ),
      79             :     sFamilyName( rFamilyName ),
      80             :     sStyleName( rStyleName ),
      81             :     nFamily( nFam ),
      82             :     nPitch( nP ),
      83          44 :     eEnc( eE )
      84             : {
      85          44 : }
      86             : 
      87         110 : inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
      88             :         const ::rtl::OUString& rFamilyName,
      89             :         const ::rtl::OUString& rStyleName,
      90             :         sal_Int16 nFam,
      91             :         sal_Int16 nP,
      92             :         rtl_TextEncoding eE ) :
      93             :     sFamilyName( rFamilyName ),
      94             :     sStyleName( rStyleName ),
      95             :     nFamily( nFam ),
      96             :     nPitch( nP ),
      97         110 :     eEnc( eE )
      98             : {
      99         110 : }
     100             : 
     101             : struct XMLFontAutoStylePoolEntryCmp_Impl {
     102         424 :     bool operator()(
     103             :         XMLFontAutoStylePoolEntry_Impl* const& r1,
     104             :         XMLFontAutoStylePoolEntry_Impl* const& r2 ) const
     105             :     {
     106         424 :         sal_Int8 nEnc1(r1->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
     107         424 :         sal_Int8 nEnc2(r2->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
     108         424 :         if( nEnc1 != nEnc2 )
     109           0 :             return nEnc1 < nEnc2;
     110         424 :         else if( r1->GetPitch() != r2->GetPitch() )
     111          64 :             return r1->GetPitch() < r2->GetPitch();
     112         360 :         else if( r1->GetFamily() != r2->GetFamily() )
     113         120 :             return r1->GetFamily() < r2->GetFamily();
     114             :         else
     115             :         {
     116         240 :             sal_Int32 nCmp = r1->GetFamilyName().compareTo( r2->GetFamilyName() );
     117         240 :             if( 0 == nCmp )
     118         120 :                 return r1->GetStyleName().compareTo( r2->GetStyleName() ) < 0;
     119             :             else
     120         120 :                 return nCmp < 0;
     121             :         }
     122             :     }
     123             : };
     124             : 
     125           8 : class XMLFontAutoStylePool_Impl : public o3tl::sorted_vector<XMLFontAutoStylePoolEntry_Impl*, XMLFontAutoStylePoolEntryCmp_Impl>
     126             : {
     127             : public:
     128           8 :     ~XMLFontAutoStylePool_Impl() { DeleteAndDestroyAll(); }
     129             : };
     130             : 
     131           8 : XMLFontAutoStylePool::XMLFontAutoStylePool( SvXMLExport& rExp ) :
     132             :     rExport( rExp ),
     133           8 :     pPool( new XMLFontAutoStylePool_Impl )
     134             : {
     135           8 : }
     136             : 
     137          16 : XMLFontAutoStylePool::~XMLFontAutoStylePool()
     138             : {
     139           8 :     delete pPool;
     140           8 : }
     141             : 
     142          66 : OUString XMLFontAutoStylePool::Add(
     143             :             const OUString& rFamilyName,
     144             :             const OUString& rStyleName,
     145             :             sal_Int16 nFamily,
     146             :             sal_Int16 nPitch,
     147             :             rtl_TextEncoding eEnc )
     148             : {
     149          66 :     OUString sPoolName;
     150             :     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
     151          66 :                                           nPitch, eEnc );
     152          66 :     XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
     153          66 :     if( it != pPool->end() )
     154             :     {
     155          22 :         sPoolName = (*it)->GetName();
     156             :     }
     157             :     else
     158             :     {
     159          44 :         OUString sName;
     160          44 :         sal_Int32 nLen = rFamilyName.indexOf( sal_Unicode(';'), 0 );
     161          44 :         if( -1 == nLen )
     162             :         {
     163          42 :             sName = rFamilyName;
     164             :         }
     165           2 :         else if( nLen > 0 )
     166             :         {
     167           2 :             sName = rFamilyName.copy( 0, nLen );
     168           2 :             sName = sName.trim();
     169             :         }
     170             : 
     171          44 :         if( sName.isEmpty() )
     172           0 :             sName = OUString::valueOf( sal_Unicode( 'F' ) );
     173             : 
     174          44 :         if( m_aNames.find(sName) != m_aNames.end() )
     175             :         {
     176           8 :             sal_Int32 nCount = 1;
     177           8 :             OUString sPrefix( sName );
     178           8 :             sName += OUString::valueOf( nCount );
     179          16 :             while( m_aNames.find(sName) != m_aNames.end() )
     180             :             {
     181           0 :                 sName = sPrefix;
     182           0 :                 sName += OUString::valueOf( ++nCount );
     183           8 :             }
     184             :         }
     185             : 
     186             :         XMLFontAutoStylePoolEntry_Impl *pEntry =
     187             :             new XMLFontAutoStylePoolEntry_Impl( sName, rFamilyName, rStyleName,
     188          44 :                                                 nFamily, nPitch, eEnc );
     189          44 :         pPool->insert( pEntry );
     190          44 :         m_aNames.insert(sName);
     191             :     }
     192             : 
     193          66 :     return sPoolName;
     194             : }
     195             : 
     196          44 : ::rtl::OUString XMLFontAutoStylePool::Find(
     197             :             const OUString& rFamilyName,
     198             :             const OUString& rStyleName,
     199             :             sal_Int16 nFamily,
     200             :             sal_Int16 nPitch,
     201             :             rtl_TextEncoding eEnc ) const
     202             : {
     203          44 :     OUString sName;
     204             :     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
     205          44 :                                           nPitch, eEnc );
     206          44 :     XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
     207          44 :     if( it != pPool->end() )
     208             :     {
     209          38 :         sName = (*it)->GetName();
     210             :     }
     211             : 
     212          44 :     return sName;
     213             : }
     214             : 
     215             : 
     216           8 : void XMLFontAutoStylePool::exportXML()
     217             : {
     218           8 :     SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_OFFICE,
     219             :                               XML_FONT_FACE_DECLS,
     220           8 :                               sal_True, sal_True );
     221           8 :     Any aAny;
     222           8 :     OUString sTmp;
     223           8 :     XMLFontFamilyNamePropHdl aFamilyNameHdl;
     224           8 :     XMLFontFamilyPropHdl aFamilyHdl;
     225           8 :     XMLFontPitchPropHdl aPitchHdl;
     226           8 :     XMLFontEncodingPropHdl aEncHdl;
     227           8 :     const SvXMLUnitConverter& rUnitConv = GetExport().GetMM100UnitConverter();
     228             : 
     229           8 :     sal_uInt32 nCount = pPool->size();
     230          52 :     for( sal_uInt32 i=0; i<nCount; i++ )
     231             :     {
     232          44 :         const XMLFontAutoStylePoolEntry_Impl *pEntry = (*pPool)[ i ];
     233             : 
     234          44 :         GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     235          88 :                                   XML_NAME, pEntry->GetName() );
     236             : 
     237          44 :         aAny <<= pEntry->GetFamilyName();
     238          44 :         if( aFamilyNameHdl.exportXML( sTmp, aAny, rUnitConv ) )
     239          44 :             GetExport().AddAttribute( XML_NAMESPACE_SVG,
     240          44 :                                       XML_FONT_FAMILY, sTmp );
     241             : 
     242          44 :         const OUString& rStyleName = pEntry->GetStyleName();
     243          44 :         if( !rStyleName.isEmpty() )
     244           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     245             :                                       XML_FONT_ADORNMENTS,
     246           0 :                                       rStyleName );
     247             : 
     248          44 :         aAny <<= (sal_Int16)pEntry->GetFamily();
     249          44 :         if( aFamilyHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     250          44 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     251          44 :                                       XML_FONT_FAMILY_GENERIC, sTmp );
     252             : 
     253          44 :         aAny <<= (sal_Int16)pEntry->GetPitch();
     254          44 :         if( aPitchHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     255          38 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     256          38 :                                       XML_FONT_PITCH, sTmp );
     257             : 
     258          44 :         aAny <<= (sal_Int16)pEntry->GetEncoding();
     259          44 :         if( aEncHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     260           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     261           0 :                                       XML_FONT_CHARSET, sTmp );
     262             : 
     263          44 :         SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
     264             :                                   XML_FONT_FACE,
     265          44 :                                   sal_True, sal_True );
     266          52 :     }
     267           8 : }
     268             : 
     269             : 
     270             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10