LCOV - code coverage report
Current view: top level - xmloff/source/style - XMLFontAutoStylePool.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 106 174 60.9 %
Date: 2014-04-11 Functions: 18 19 94.7 %
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             : #include <vcl/embeddedfontshelper.hxx>
      29             : #include <osl/file.hxx>
      30             : 
      31             : #include <com/sun/star/embed/ElementModes.hpp>
      32             : #include <com/sun/star/embed/XTransactedObject.hpp>
      33             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      34             : 
      35             : 
      36             : using namespace ::com::sun::star;
      37             : using namespace ::com::sun::star::uno;
      38             : using namespace ::xmloff::token;
      39             : 
      40        7286 : class XMLFontAutoStylePoolEntry_Impl
      41             : {
      42             :     OUString    sName;
      43             :     OUString    sFamilyName;
      44             :     OUString    sStyleName;
      45             :     FontFamily  nFamily;
      46             :     FontPitch   nPitch;
      47             :     rtl_TextEncoding eEnc;
      48             : 
      49             : public:
      50             : 
      51             :     inline XMLFontAutoStylePoolEntry_Impl(
      52             :             const OUString& rName,
      53             :             const OUString& rFamilyName,
      54             :             const OUString& rStyleName,
      55             :             FontFamily nFamily,
      56             :             FontPitch nPitch,
      57             :             rtl_TextEncoding eEnc );
      58             : 
      59             :     inline XMLFontAutoStylePoolEntry_Impl(
      60             :             const OUString& rFamilyName,
      61             :             const OUString& rStyleName,
      62             :             FontFamily nFamily,
      63             :             FontPitch nPitch,
      64             :             rtl_TextEncoding eEnc );
      65             : 
      66        4557 :     const OUString& GetName() const { return sName; }
      67       23747 :     const OUString& GetFamilyName() const { return sFamilyName; }
      68       16251 :     const OUString& GetStyleName() const { return sStyleName; }
      69       43287 :     FontFamily GetFamily() const {   return nFamily; }
      70       42469 :     FontPitch GetPitch() const { return nPitch; }
      71       38093 :     rtl_TextEncoding GetEncoding() const { return eEnc; }
      72             : };
      73             : 
      74             : 
      75         654 : inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
      76             :         const OUString& rName,
      77             :         const OUString& rFamilyName,
      78             :         const OUString& rStyleName,
      79             :         FontFamily nFam,
      80             :         FontPitch nP,
      81             :         rtl_TextEncoding eE ) :
      82             :     sName( rName ),
      83             :     sFamilyName( rFamilyName ),
      84             :     sStyleName( rStyleName ),
      85             :     nFamily( nFam ),
      86             :     nPitch( nP ),
      87         654 :     eEnc( eE )
      88             : {
      89         654 : }
      90             : 
      91        6632 : inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
      92             :         const OUString& rFamilyName,
      93             :         const OUString& rStyleName,
      94             :         FontFamily nFam,
      95             :         FontPitch nP,
      96             :         rtl_TextEncoding eE ) :
      97             :     sFamilyName( rFamilyName ),
      98             :     sStyleName( rStyleName ),
      99             :     nFamily( nFam ),
     100             :     nPitch( nP ),
     101        6632 :     eEnc( eE )
     102             : {
     103        6632 : }
     104             : 
     105             : struct XMLFontAutoStylePoolEntryCmp_Impl {
     106       18717 :     bool operator()(
     107             :         XMLFontAutoStylePoolEntry_Impl* const& r1,
     108             :         XMLFontAutoStylePoolEntry_Impl* const& r2 ) const
     109             :     {
     110       18717 :         bool nEnc1(r1->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
     111       18717 :         bool nEnc2(r2->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
     112       18717 :         if( nEnc1 != nEnc2 )
     113          50 :             return nEnc1 < nEnc2;
     114       18667 :         else if( r1->GetPitch() != r2->GetPitch() )
     115        2238 :             return r1->GetPitch() < r2->GetPitch();
     116       16429 :         else if( r1->GetFamily() != r2->GetFamily() )
     117        4885 :             return r1->GetFamily() < r2->GetFamily();
     118             :         else
     119             :         {
     120       11544 :             sal_Int32 nCmp = r1->GetFamilyName().compareTo( r2->GetFamilyName() );
     121       11544 :             if( 0 == nCmp )
     122        7796 :                 return r1->GetStyleName().compareTo( r2->GetStyleName() ) < 0;
     123             :             else
     124        3748 :                 return nCmp < 0;
     125             :         }
     126             :     }
     127             : };
     128             : 
     129         357 : class XMLFontAutoStylePool_Impl : public o3tl::sorted_vector<XMLFontAutoStylePoolEntry_Impl*, XMLFontAutoStylePoolEntryCmp_Impl>
     130             : {
     131             : public:
     132         357 :     ~XMLFontAutoStylePool_Impl() { DeleteAndDestroyAll(); }
     133             : };
     134             : 
     135         357 : XMLFontAutoStylePool::XMLFontAutoStylePool( SvXMLExport& rExp, bool _tryToEmbedFonts ) :
     136             :     rExport( rExp ),
     137         357 :     pPool( new XMLFontAutoStylePool_Impl ),
     138         714 :     tryToEmbedFonts( _tryToEmbedFonts )
     139             : {
     140         357 : }
     141             : 
     142         993 : XMLFontAutoStylePool::~XMLFontAutoStylePool()
     143             : {
     144         357 :     delete pPool;
     145         636 : }
     146             : 
     147        3832 : OUString XMLFontAutoStylePool::Add(
     148             :             const OUString& rFamilyName,
     149             :             const OUString& rStyleName,
     150             :             FontFamily nFamily,
     151             :             FontPitch nPitch,
     152             :             rtl_TextEncoding eEnc )
     153             : {
     154        3832 :     OUString sPoolName;
     155             :     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
     156        7664 :                                           nPitch, eEnc );
     157        3832 :     XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
     158        3832 :     if( it != pPool->end() )
     159             :     {
     160        3178 :         sPoolName = (*it)->GetName();
     161             :     }
     162             :     else
     163             :     {
     164         654 :         OUString sName;
     165         654 :         sal_Int32 nLen = rFamilyName.indexOf( ';', 0 );
     166         654 :         if( -1 == nLen )
     167             :         {
     168         650 :             sName = rFamilyName;
     169             :         }
     170           4 :         else if( nLen > 0 )
     171             :         {
     172           4 :             sName = rFamilyName.copy( 0, nLen );
     173           4 :             sName = sName.trim();
     174             :         }
     175             : 
     176         654 :         if( sName.isEmpty() )
     177           3 :             sName = OUString( 'F' );
     178             : 
     179         654 :         if( m_aNames.find(sName) != m_aNames.end() )
     180             :         {
     181          94 :             sal_Int32 nCount = 1;
     182          94 :             OUString sPrefix( sName );
     183          94 :             sName += OUString::number( nCount );
     184         198 :             while( m_aNames.find(sName) != m_aNames.end() )
     185             :             {
     186          10 :                 sName = sPrefix;
     187          10 :                 sName += OUString::number( ++nCount );
     188          94 :             }
     189             :         }
     190             : 
     191             :         XMLFontAutoStylePoolEntry_Impl *pEntry =
     192             :             new XMLFontAutoStylePoolEntry_Impl( sName, rFamilyName, rStyleName,
     193         654 :                                                 nFamily, nPitch, eEnc );
     194         654 :         pPool->insert( pEntry );
     195         654 :         m_aNames.insert(sName);
     196             :     }
     197             : 
     198        7664 :     return sPoolName;
     199             : }
     200             : 
     201        2800 : OUString XMLFontAutoStylePool::Find(
     202             :             const OUString& rFamilyName,
     203             :             const OUString& rStyleName,
     204             :             FontFamily nFamily,
     205             :             FontPitch nPitch,
     206             :             rtl_TextEncoding eEnc ) const
     207             : {
     208        2800 :     OUString sName;
     209             :     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
     210        5600 :                                           nPitch, eEnc );
     211        2800 :     XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
     212        2800 :     if( it != pPool->end() )
     213             :     {
     214         720 :         sName = (*it)->GetName();
     215             :     }
     216             : 
     217        5600 :     return sName;
     218             : }
     219             : 
     220             : 
     221         208 : void XMLFontAutoStylePool::exportXML()
     222             : {
     223         208 :     SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_OFFICE,
     224             :                               XML_FONT_FACE_DECLS,
     225         208 :                               true, true );
     226         416 :     Any aAny;
     227         416 :     OUString sTmp;
     228         416 :     XMLFontFamilyNamePropHdl aFamilyNameHdl;
     229         416 :     XMLFontFamilyPropHdl aFamilyHdl;
     230         416 :     XMLFontPitchPropHdl aPitchHdl;
     231         416 :     XMLFontEncodingPropHdl aEncHdl;
     232         208 :     const SvXMLUnitConverter& rUnitConv = GetExport().GetMM100UnitConverter();
     233             : 
     234         416 :     std::map< OUString, OUString > fontFilesMap; // our url to document url
     235         208 :     sal_uInt32 nCount = pPool->size();
     236         867 :     for( sal_uInt32 i=0; i<nCount; i++ )
     237             :     {
     238         659 :         const XMLFontAutoStylePoolEntry_Impl *pEntry = (*pPool)[ i ];
     239             : 
     240         659 :         GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     241        1318 :                                   XML_NAME, pEntry->GetName() );
     242             : 
     243         659 :         aAny <<= pEntry->GetFamilyName();
     244         659 :         if( aFamilyNameHdl.exportXML( sTmp, aAny, rUnitConv ) )
     245         659 :             GetExport().AddAttribute( XML_NAMESPACE_SVG,
     246         659 :                                       XML_FONT_FAMILY, sTmp );
     247             : 
     248         659 :         const OUString& rStyleName = pEntry->GetStyleName();
     249         659 :         if( !rStyleName.isEmpty() )
     250           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     251             :                                       XML_FONT_ADORNMENTS,
     252           0 :                                       rStyleName );
     253             : 
     254         659 :         aAny <<= (sal_Int16)pEntry->GetFamily();
     255         659 :         if( aFamilyHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     256         632 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     257         632 :                                       XML_FONT_FAMILY_GENERIC, sTmp );
     258             : 
     259         659 :         aAny <<= (sal_Int16)pEntry->GetPitch();
     260         659 :         if( aPitchHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     261         604 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     262         604 :                                       XML_FONT_PITCH, sTmp );
     263             : 
     264         659 :         aAny <<= (sal_Int16)pEntry->GetEncoding();
     265         659 :         if( aEncHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     266           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     267           0 :                                       XML_FONT_CHARSET, sTmp );
     268             : 
     269         659 :         SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
     270             :                                   XML_FONT_FACE,
     271         659 :                                   true, true );
     272             : 
     273         659 :         if( tryToEmbedFonts )
     274             :         {
     275           0 :             std::vector< OUString > fileUrls;
     276             :             static const FontWeight weight[] = { WEIGHT_NORMAL, WEIGHT_BOLD, WEIGHT_NORMAL, WEIGHT_BOLD };
     277             :             static const FontItalic italic[] = { ITALIC_NONE, ITALIC_NONE, ITALIC_NORMAL, ITALIC_NORMAL };
     278             :             assert( SAL_N_ELEMENTS( weight ) == SAL_N_ELEMENTS( italic ));
     279           0 :             for( unsigned int j = 0;
     280             :                  j < SAL_N_ELEMENTS( weight );
     281             :                  ++j )
     282             :             {
     283             :                 // Embed font if at least viewing is allowed (in which case the opening app must check
     284             :                 // the font license rights too and open either read-only or not use the font for editing).
     285           0 :                 OUString fileUrl = EmbeddedFontsHelper::fontFileUrl( pEntry->GetFamilyName(), pEntry->GetFamily(),
     286           0 :                     italic[ j ], weight[ j ], pEntry->GetPitch(), pEntry->GetEncoding(),
     287           0 :                     EmbeddedFontsHelper::ViewingAllowed );
     288           0 :                 if( fileUrl.isEmpty())
     289           0 :                     continue;
     290           0 :                 if( !fontFilesMap.count( fileUrl ))
     291             :                 {
     292           0 :                     OUString docUrl = embedFontFile( fileUrl );
     293           0 :                     if( !docUrl.isEmpty())
     294           0 :                         fontFilesMap[ fileUrl ] = docUrl;
     295             :                     else
     296           0 :                         continue; // --> failed to embed
     297             :                 }
     298           0 :                 fileUrls.push_back( fileUrl );
     299           0 :             }
     300           0 :             if( !fileUrls.empty())
     301             :             {
     302           0 :                 SvXMLElementExport fontFaceSrc( GetExport(), XML_NAMESPACE_SVG,
     303           0 :                     XML_FONT_FACE_SRC, true, true );
     304           0 :                 for( std::vector< OUString >::const_iterator it = fileUrls.begin();
     305           0 :                      it != fileUrls.end();
     306             :                      ++it )
     307             :                 {
     308           0 :                     if( fontFilesMap.count( *it ))
     309             :                     {
     310           0 :                         GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, fontFilesMap[ *it ] );
     311           0 :                         GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, "simple" );
     312           0 :                         SvXMLElementExport fontFaceUri( GetExport(), XML_NAMESPACE_SVG,
     313           0 :                             XML_FONT_FACE_URI, true, true );
     314             :                     }
     315           0 :                 }
     316           0 :             }
     317             :         }
     318         867 :     }
     319         208 : }
     320             : 
     321           0 : OUString XMLFontAutoStylePool::embedFontFile( const OUString& fileUrl )
     322             : {
     323             :     try
     324             :     {
     325           0 :         osl::File file( fileUrl );
     326           0 :         if( file.open( osl_File_OpenFlag_Read ) != osl::File::E_None )
     327           0 :             return OUString();
     328           0 :         uno::Reference< embed::XStorage > storage;
     329           0 :         storage.set( GetExport().GetTargetStorage()->openStorageElement( OUString( "Fonts" ),
     330           0 :             ::embed::ElementModes::WRITE ), uno::UNO_QUERY_THROW );
     331           0 :         int index = 0;
     332           0 :         OUString name;
     333           0 :         do
     334             :         {
     335           0 :             name = "font" + OUString::number( ++index ) + ".ttf";
     336           0 :         } while( storage->hasByName( name ) );
     337           0 :         uno::Reference< io::XOutputStream > outputStream;
     338           0 :         outputStream.set( storage->openStreamElement( name, ::embed::ElementModes::WRITE ), UNO_QUERY_THROW );
     339           0 :         uno::Reference < beans::XPropertySet > propertySet( outputStream, uno::UNO_QUERY );
     340             :         assert( propertySet.is());
     341           0 :         propertySet->setPropertyValue( "MediaType", uno::makeAny( OUString( "application/x-font-ttf" ))); // TODO
     342             :         for(;;)
     343             :         {
     344             :             char buffer[ 4096 ];
     345             :             sal_uInt64 readSize;
     346             :             sal_Bool eof;
     347           0 :             if( file.isEndOfFile( &eof ) != osl::File::E_None )
     348             :             {
     349             :                 SAL_WARN( "xmloff", "Error reading font file " << fileUrl );
     350           0 :                 outputStream->closeOutput();
     351           0 :                 return OUString();
     352             :             }
     353           0 :             if( eof )
     354           0 :                 break;
     355           0 :             if( file.read( buffer, 4096, readSize ) != osl::File::E_None )
     356             :             {
     357             :                 SAL_WARN( "xmloff", "Error reading font file " << fileUrl );
     358           0 :                 outputStream->closeOutput();
     359           0 :                 return OUString();
     360             :             }
     361           0 :             if( readSize == 0 )
     362           0 :                 break;
     363           0 :             outputStream->writeBytes( uno::Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( buffer ), readSize ));
     364           0 :         }
     365           0 :         outputStream->closeOutput();
     366           0 :         if( storage.is() )
     367             :         {
     368           0 :             Reference< embed::XTransactedObject > transaction( storage, UNO_QUERY );
     369           0 :             if( transaction.is())
     370             :             {
     371           0 :                 transaction->commit();
     372           0 :                 return "Fonts/" + name;
     373           0 :             }
     374           0 :         }
     375           0 :     } catch( const Exception& e )
     376             :     {
     377             :         SAL_WARN( "xmloff", "Exception when embedding a font file:" << e.Message );
     378             :     }
     379           0 :     return OUString();
     380             : }
     381             : 
     382             : 
     383             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10