LCOV - code coverage report
Current view: top level - xmloff/source/style - XMLFontAutoStylePool.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 174 0.0 %
Date: 2014-04-14 Functions: 0 19 0.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             : #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           0 : 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           0 :     const OUString& GetName() const { return sName; }
      67           0 :     const OUString& GetFamilyName() const { return sFamilyName; }
      68           0 :     const OUString& GetStyleName() const { return sStyleName; }
      69           0 :     FontFamily GetFamily() const {   return nFamily; }
      70           0 :     FontPitch GetPitch() const { return nPitch; }
      71           0 :     rtl_TextEncoding GetEncoding() const { return eEnc; }
      72             : };
      73             : 
      74             : 
      75           0 : 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           0 :     eEnc( eE )
      88             : {
      89           0 : }
      90             : 
      91           0 : 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           0 :     eEnc( eE )
     102             : {
     103           0 : }
     104             : 
     105             : struct XMLFontAutoStylePoolEntryCmp_Impl {
     106           0 :     bool operator()(
     107             :         XMLFontAutoStylePoolEntry_Impl* const& r1,
     108             :         XMLFontAutoStylePoolEntry_Impl* const& r2 ) const
     109             :     {
     110           0 :         bool nEnc1(r1->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
     111           0 :         bool nEnc2(r2->GetEncoding() != RTL_TEXTENCODING_SYMBOL);
     112           0 :         if( nEnc1 != nEnc2 )
     113           0 :             return nEnc1 < nEnc2;
     114           0 :         else if( r1->GetPitch() != r2->GetPitch() )
     115           0 :             return r1->GetPitch() < r2->GetPitch();
     116           0 :         else if( r1->GetFamily() != r2->GetFamily() )
     117           0 :             return r1->GetFamily() < r2->GetFamily();
     118             :         else
     119             :         {
     120           0 :             sal_Int32 nCmp = r1->GetFamilyName().compareTo( r2->GetFamilyName() );
     121           0 :             if( 0 == nCmp )
     122           0 :                 return r1->GetStyleName().compareTo( r2->GetStyleName() ) < 0;
     123             :             else
     124           0 :                 return nCmp < 0;
     125             :         }
     126             :     }
     127             : };
     128             : 
     129           0 : class XMLFontAutoStylePool_Impl : public o3tl::sorted_vector<XMLFontAutoStylePoolEntry_Impl*, XMLFontAutoStylePoolEntryCmp_Impl>
     130             : {
     131             : public:
     132           0 :     ~XMLFontAutoStylePool_Impl() { DeleteAndDestroyAll(); }
     133             : };
     134             : 
     135           0 : XMLFontAutoStylePool::XMLFontAutoStylePool( SvXMLExport& rExp, bool _tryToEmbedFonts ) :
     136             :     rExport( rExp ),
     137           0 :     pPool( new XMLFontAutoStylePool_Impl ),
     138           0 :     tryToEmbedFonts( _tryToEmbedFonts )
     139             : {
     140           0 : }
     141             : 
     142           0 : XMLFontAutoStylePool::~XMLFontAutoStylePool()
     143             : {
     144           0 :     delete pPool;
     145           0 : }
     146             : 
     147           0 : OUString XMLFontAutoStylePool::Add(
     148             :             const OUString& rFamilyName,
     149             :             const OUString& rStyleName,
     150             :             FontFamily nFamily,
     151             :             FontPitch nPitch,
     152             :             rtl_TextEncoding eEnc )
     153             : {
     154           0 :     OUString sPoolName;
     155             :     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
     156           0 :                                           nPitch, eEnc );
     157           0 :     XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
     158           0 :     if( it != pPool->end() )
     159             :     {
     160           0 :         sPoolName = (*it)->GetName();
     161             :     }
     162             :     else
     163             :     {
     164           0 :         OUString sName;
     165           0 :         sal_Int32 nLen = rFamilyName.indexOf( ';', 0 );
     166           0 :         if( -1 == nLen )
     167             :         {
     168           0 :             sName = rFamilyName;
     169             :         }
     170           0 :         else if( nLen > 0 )
     171             :         {
     172           0 :             sName = rFamilyName.copy( 0, nLen );
     173           0 :             sName = sName.trim();
     174             :         }
     175             : 
     176           0 :         if( sName.isEmpty() )
     177           0 :             sName = OUString( 'F' );
     178             : 
     179           0 :         if( m_aNames.find(sName) != m_aNames.end() )
     180             :         {
     181           0 :             sal_Int32 nCount = 1;
     182           0 :             OUString sPrefix( sName );
     183           0 :             sName += OUString::number( nCount );
     184           0 :             while( m_aNames.find(sName) != m_aNames.end() )
     185             :             {
     186           0 :                 sName = sPrefix;
     187           0 :                 sName += OUString::number( ++nCount );
     188           0 :             }
     189             :         }
     190             : 
     191             :         XMLFontAutoStylePoolEntry_Impl *pEntry =
     192             :             new XMLFontAutoStylePoolEntry_Impl( sName, rFamilyName, rStyleName,
     193           0 :                                                 nFamily, nPitch, eEnc );
     194           0 :         pPool->insert( pEntry );
     195           0 :         m_aNames.insert(sName);
     196             :     }
     197             : 
     198           0 :     return sPoolName;
     199             : }
     200             : 
     201           0 : OUString XMLFontAutoStylePool::Find(
     202             :             const OUString& rFamilyName,
     203             :             const OUString& rStyleName,
     204             :             FontFamily nFamily,
     205             :             FontPitch nPitch,
     206             :             rtl_TextEncoding eEnc ) const
     207             : {
     208           0 :     OUString sName;
     209             :     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
     210           0 :                                           nPitch, eEnc );
     211           0 :     XMLFontAutoStylePool_Impl::const_iterator it = pPool->find( &aTmp );
     212           0 :     if( it != pPool->end() )
     213             :     {
     214           0 :         sName = (*it)->GetName();
     215             :     }
     216             : 
     217           0 :     return sName;
     218             : }
     219             : 
     220             : 
     221           0 : void XMLFontAutoStylePool::exportXML()
     222             : {
     223           0 :     SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_OFFICE,
     224             :                               XML_FONT_FACE_DECLS,
     225           0 :                               true, true );
     226           0 :     Any aAny;
     227           0 :     OUString sTmp;
     228           0 :     XMLFontFamilyNamePropHdl aFamilyNameHdl;
     229           0 :     XMLFontFamilyPropHdl aFamilyHdl;
     230           0 :     XMLFontPitchPropHdl aPitchHdl;
     231           0 :     XMLFontEncodingPropHdl aEncHdl;
     232           0 :     const SvXMLUnitConverter& rUnitConv = GetExport().GetMM100UnitConverter();
     233             : 
     234           0 :     std::map< OUString, OUString > fontFilesMap; // our url to document url
     235           0 :     sal_uInt32 nCount = pPool->size();
     236           0 :     for( sal_uInt32 i=0; i<nCount; i++ )
     237             :     {
     238           0 :         const XMLFontAutoStylePoolEntry_Impl *pEntry = (*pPool)[ i ];
     239             : 
     240           0 :         GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     241           0 :                                   XML_NAME, pEntry->GetName() );
     242             : 
     243           0 :         aAny <<= pEntry->GetFamilyName();
     244           0 :         if( aFamilyNameHdl.exportXML( sTmp, aAny, rUnitConv ) )
     245           0 :             GetExport().AddAttribute( XML_NAMESPACE_SVG,
     246           0 :                                       XML_FONT_FAMILY, sTmp );
     247             : 
     248           0 :         const OUString& rStyleName = pEntry->GetStyleName();
     249           0 :         if( !rStyleName.isEmpty() )
     250           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     251             :                                       XML_FONT_ADORNMENTS,
     252           0 :                                       rStyleName );
     253             : 
     254           0 :         aAny <<= (sal_Int16)pEntry->GetFamily();
     255           0 :         if( aFamilyHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     256           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     257           0 :                                       XML_FONT_FAMILY_GENERIC, sTmp );
     258             : 
     259           0 :         aAny <<= (sal_Int16)pEntry->GetPitch();
     260           0 :         if( aPitchHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     261           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     262           0 :                                       XML_FONT_PITCH, sTmp );
     263             : 
     264           0 :         aAny <<= (sal_Int16)pEntry->GetEncoding();
     265           0 :         if( aEncHdl.exportXML( sTmp, aAny, rUnitConv  ) )
     266           0 :             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
     267           0 :                                       XML_FONT_CHARSET, sTmp );
     268             : 
     269           0 :         SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
     270             :                                   XML_FONT_FACE,
     271           0 :                                   true, true );
     272             : 
     273           0 :         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           0 :     }
     319           0 : }
     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