LCOV - code coverage report
Current view: top level - vcl/source/font - PhysicalFontFamily.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 97 155 62.6 %
Date: 2014-11-03 Functions: 7 10 70.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 <sal/types.h>
      21             : 
      22             : #include <rtl/ustring.hxx>
      23             : 
      24             : #include "outdev.h"
      25             : #include "PhysicalFontFace.hxx"
      26             : #include "PhysicalFontCollection.hxx"
      27             : 
      28             : #include "PhysicalFontFamily.hxx"
      29             : 
      30           0 : void PhysicalFontFamily::CalcType( sal_uLong& rType, FontWeight& rWeight, FontWidth& rWidth,
      31             :                                    FontFamily eFamily, const utl::FontNameAttr* pFontAttr )
      32             : {
      33           0 :     if ( eFamily != FAMILY_DONTKNOW )
      34             :     {
      35           0 :         if ( eFamily == FAMILY_SWISS )
      36           0 :             rType |= IMPL_FONT_ATTR_SANSSERIF;
      37           0 :         else if ( eFamily == FAMILY_ROMAN )
      38           0 :             rType |= IMPL_FONT_ATTR_SERIF;
      39           0 :         else if ( eFamily == FAMILY_SCRIPT )
      40           0 :             rType |= IMPL_FONT_ATTR_SCRIPT;
      41           0 :         else if ( eFamily == FAMILY_MODERN )
      42           0 :             rType |= IMPL_FONT_ATTR_FIXED;
      43           0 :         else if ( eFamily == FAMILY_DECORATIVE )
      44           0 :             rType |= IMPL_FONT_ATTR_DECORATIVE;
      45             :     }
      46             : 
      47           0 :     if ( pFontAttr )
      48             :     {
      49           0 :         rType |= pFontAttr->Type;
      50             : 
      51           0 :         if ( ((rWeight == WEIGHT_DONTKNOW) || (rWeight == WEIGHT_NORMAL)) &&
      52           0 :              (pFontAttr->Weight != WEIGHT_DONTKNOW) )
      53           0 :             rWeight = pFontAttr->Weight;
      54           0 :         if ( ((rWidth == WIDTH_DONTKNOW) || (rWidth == WIDTH_NORMAL)) &&
      55           0 :              (pFontAttr->Width != WIDTH_DONTKNOW) )
      56           0 :             rWidth = pFontAttr->Width;
      57             :     }
      58           0 : }
      59             : 
      60           0 : static unsigned lcl_IsCJKFont( const OUString& rFontName )
      61             : {
      62             :     // Test, if Fontname includes CJK characters --> In this case we
      63             :     // mention that it is a CJK font
      64           0 :     for(int i = 0; i < rFontName.getLength(); i++)
      65             :     {
      66           0 :         const sal_Unicode ch = rFontName[i];
      67             :         // japanese
      68           0 :         if ( ((ch >= 0x3040) && (ch <= 0x30FF)) ||
      69           0 :              ((ch >= 0x3190) && (ch <= 0x319F)) )
      70           0 :             return IMPL_FONT_ATTR_CJK|IMPL_FONT_ATTR_CJK_JP;
      71             : 
      72             :         // korean
      73           0 :         if ( ((ch >= 0xAC00) && (ch <= 0xD7AF)) ||
      74           0 :              ((ch >= 0x3130) && (ch <= 0x318F)) ||
      75           0 :              ((ch >= 0x1100) && (ch <= 0x11FF)) )
      76           0 :             return IMPL_FONT_ATTR_CJK|IMPL_FONT_ATTR_CJK_KR;
      77             : 
      78             :         // chinese
      79           0 :         if ( ((ch >= 0x3400) && (ch <= 0x9FFF)) )
      80           0 :             return IMPL_FONT_ATTR_CJK|IMPL_FONT_ATTR_CJK_TC|IMPL_FONT_ATTR_CJK_SC;
      81             : 
      82             :         // cjk
      83           0 :         if ( ((ch >= 0x3000) && (ch <= 0xD7AF)) ||
      84           0 :              ((ch >= 0xFF00) && (ch <= 0xFFEE)) )
      85           0 :             return IMPL_FONT_ATTR_CJK;
      86             : 
      87             :     }
      88             : 
      89           0 :     return 0;
      90             : }
      91             : 
      92      358600 : PhysicalFontFamily::PhysicalFontFamily( const OUString& rSearchName )
      93             : :   mpFirst( NULL ),
      94             :     maSearchName( rSearchName ),
      95             :     mnTypeFaces( 0 ),
      96             :     mnMatchType( 0 ),
      97             :     meMatchWeight( WEIGHT_DONTKNOW ),
      98             :     meMatchWidth( WIDTH_DONTKNOW ),
      99             :     meFamily( FAMILY_DONTKNOW ),
     100             :     mePitch( PITCH_DONTKNOW ),
     101      358600 :     mnMinQuality( -1 )
     102      358600 : {}
     103             : 
     104      712600 : PhysicalFontFamily::~PhysicalFontFamily()
     105             : {
     106             :     // release all physical font faces
     107     1738744 :     while( mpFirst )
     108             :     {
     109     1026144 :         PhysicalFontFace* pFace = mpFirst;
     110     1026144 :         mpFirst = pFace->GetNextFace();
     111     1026144 :         delete pFace;
     112             :     }
     113      356300 : }
     114             : 
     115     1041504 : bool PhysicalFontFamily::AddFontFace( PhysicalFontFace* pNewData )
     116             : {
     117     1041504 :     pNewData->mpNext = NULL;
     118             : 
     119     1041504 :     if( !mpFirst )
     120             :     {
     121      358600 :         maName         = pNewData->GetFamilyName();
     122      358600 :         maMapNames     = pNewData->maMapNames;
     123      358600 :         meFamily       = pNewData->GetFamilyType();
     124      358600 :         mePitch        = pNewData->GetPitch();
     125      358600 :         mnMinQuality   = pNewData->mnQuality;
     126             :     }
     127             :     else
     128             :     {
     129      682904 :         if( meFamily == FAMILY_DONTKNOW )
     130      661388 :             meFamily = pNewData->GetFamilyType();
     131      682904 :         if( mePitch == PITCH_DONTKNOW )
     132           0 :             mePitch = pNewData->GetPitch();
     133      682904 :         if( mnMinQuality > pNewData->mnQuality )
     134      190048 :             mnMinQuality = pNewData->mnQuality;
     135             :     }
     136             : 
     137             :     // set attributes for attribute based font matching
     138     1041504 :     if( pNewData->IsScalable() )
     139     1041504 :         mnTypeFaces |= FONT_FAMILY_SCALABLE;
     140             : 
     141     1041504 :     if( pNewData->IsSymbolFont() )
     142       21516 :         mnTypeFaces |= FONT_FAMILY_SYMBOL;
     143             :     else
     144     1019988 :         mnTypeFaces |= FONT_FAMILY_NONESYMBOL;
     145             : 
     146     1041504 :     if( pNewData->GetWeight() != WEIGHT_DONTKNOW )
     147             :     {
     148     1041504 :         if( pNewData->GetWeight() >= WEIGHT_SEMIBOLD )
     149      463376 :             mnTypeFaces |= FONT_FAMILY_BOLD;
     150      578128 :         else if( pNewData->GetWeight() <= WEIGHT_SEMILIGHT )
     151       50204 :             mnTypeFaces |= FONT_FAMILY_LIGHT;
     152             :         else
     153      527924 :             mnTypeFaces |= FONT_FAMILY_NORMAL;
     154             :     }
     155             : 
     156     1041504 :     if( pNewData->GetSlant() == ITALIC_NONE )
     157      578128 :         mnTypeFaces |= FONT_FAMILY_NONEITALIC;
     158      926752 :     else if( (pNewData->GetSlant() == ITALIC_NORMAL)
     159      463376 :          ||  (pNewData->GetSlant() == ITALIC_OBLIQUE) )
     160      463376 :         mnTypeFaces |= FONT_FAMILY_ITALIC;
     161             : 
     162     1041504 :     if( (meMatchWeight == WEIGHT_DONTKNOW)
     163           0 :     ||  (meMatchWidth  == WIDTH_DONTKNOW)
     164           0 :     ||  (mnMatchType   == 0) )
     165             :     {
     166             :         // TODO: is it cheaper to calc matching attributes now or on demand?
     167             :         // calc matching attributes if other entries are already initialized
     168             : 
     169             :         // Do lazy, quite expensive, not needed in start-up!
     170             :         // const FontSubstConfiguration& rFontSubst = *FontSubstConfiguration::get();
     171             :         // InitMatchData( rFontSubst, maSearchName );
     172             :         // mbMatchData=true; // Somewhere else???
     173             :     }
     174             : 
     175             :     // reassign name (sharing saves memory)
     176     1041504 :     if( pNewData->GetFamilyName() == GetFamilyName() )
     177     1041504 :         pNewData->SetFamilyName( GetFamilyName() );
     178             : 
     179             :     // insert new physical font face into linked list
     180             :     // TODO: get rid of linear search?
     181             :     PhysicalFontFace* pData;
     182     1041504 :     PhysicalFontFace** ppHere = &mpFirst;
     183     2385888 :     for(; (pData=*ppHere) != NULL; ppHere=&pData->mpNext )
     184             :     {
     185     1412712 :         sal_Int32 eComp = pNewData->CompareWithSize( *pData );
     186     1412712 :         if( eComp > 0 )
     187     1344384 :             continue;
     188       68328 :         if( eComp < 0 )
     189       59592 :             break;
     190             : 
     191             :         // ignore duplicate if its quality is worse
     192        8736 :         if( pNewData->mnQuality < pData->mnQuality )
     193        1872 :             return false;
     194             : 
     195             :         // keep the device font if its quality is good enough
     196        6864 :         if( (pNewData->mnQuality == pData->mnQuality)
     197        6240 :         &&  (pData->mbDevice || !pNewData->mbDevice) )
     198        6240 :             return false;
     199             : 
     200             :         // replace existing font face with a better one
     201         624 :         pNewData->mpNext = pData->mpNext;
     202         624 :         *ppHere = pNewData;
     203         624 :         delete pData;
     204         624 :         return true;
     205             :     }
     206             : 
     207             :     // insert into or append to list of physical font faces
     208     1032768 :     pNewData->mpNext = pData;
     209     1032768 :     *ppHere = pNewData;
     210     1032768 :     return true;
     211             : }
     212             : 
     213             : // get font attributes using the normalized font family name
     214           0 : void PhysicalFontFamily::InitMatchData( const utl::FontSubstConfiguration& rFontSubst,
     215             :     const OUString& rSearchName )
     216             : {
     217           0 :     OUString aShortName;
     218           0 :     OUString aMatchFamilyName(maMatchFamilyName);
     219             :     // get font attributes from the decorated font name
     220             :     utl::FontSubstConfiguration::getMapName( rSearchName, aShortName, aMatchFamilyName,
     221           0 :                             meMatchWeight, meMatchWidth, mnMatchType );
     222           0 :     maMatchFamilyName = aMatchFamilyName;
     223           0 :     const utl::FontNameAttr* pFontAttr = rFontSubst.getSubstInfo( rSearchName );
     224             :     // eventually use the stripped name
     225           0 :     if( !pFontAttr )
     226           0 :         if( aShortName != rSearchName )
     227           0 :             pFontAttr = rFontSubst.getSubstInfo( aShortName );
     228           0 :     CalcType( mnMatchType, meMatchWeight, meMatchWidth, meFamily, pFontAttr );
     229           0 :     mnMatchType |= lcl_IsCJKFont( maName );
     230           0 : }
     231             : 
     232       23884 : PhysicalFontFace* PhysicalFontFamily::FindBestFontFace( const FontSelectPattern& rFSD ) const
     233             : {
     234       23884 :     if( !mpFirst )
     235           0 :         return NULL;
     236       23884 :     if( !mpFirst->GetNextFace() )
     237        2050 :         return mpFirst;
     238             : 
     239             :     // FontName+StyleName should map to FamilyName+StyleName
     240       21834 :     const OUString& rSearchName = rFSD.maTargetName;
     241       21834 :     OUString aTargetStyleName;
     242       21834 :     const OUString* pTargetStyleName = NULL;
     243       43668 :     if(    (rSearchName.getLength() > maSearchName.getLength())
     244       21834 :         && rSearchName.startsWith( maSearchName ) )
     245             :     {
     246           0 :         aTargetStyleName = rSearchName.copy(maSearchName.getLength() + 1);
     247           0 :         pTargetStyleName = &aTargetStyleName;
     248             :     }
     249             : 
     250             :     // TODO: linear search improve!
     251       21834 :     PhysicalFontFace* pFontFace = mpFirst;
     252       21834 :     PhysicalFontFace* pBestFontFace = pFontFace;
     253       21834 :     FontMatchStatus aFontMatchStatus = {0,0,0, pTargetStyleName};
     254      109904 :     for(; pFontFace; pFontFace = pFontFace->GetNextFace() )
     255       88070 :         if( pFontFace->IsBetterMatch( rFSD, aFontMatchStatus ) )
     256       28360 :             pBestFontFace = pFontFace;
     257             : 
     258       21834 :     return pBestFontFace;
     259             : }
     260             : 
     261             : // update device font list with unique font faces, with uniqueness
     262             : // meaning different font attributes, but not different fonts sizes
     263      400200 : void PhysicalFontFamily::UpdateDevFontList( ImplGetDevFontList& rDevFontList ) const
     264             : {
     265      400200 :     PhysicalFontFace* pPrevFace = NULL;
     266     1552776 :     for( PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
     267             :     {
     268     1152576 :         if( !pPrevFace || pFace->CompareIgnoreSize( *pPrevFace ) )
     269     1152576 :             rDevFontList.Add( pFace );
     270     1152576 :         pPrevFace = pFace;
     271             :     }
     272      400200 : }
     273             : 
     274        2048 : void PhysicalFontFamily::GetFontHeights( std::set<int>& rHeights ) const
     275             : {
     276             :     // add all available font heights
     277       10240 :     for( const PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
     278        8192 :         rHeights.insert( pFace->GetHeight() );
     279        2048 : }
     280             : 
     281      304000 : void PhysicalFontFamily::UpdateCloneFontList( PhysicalFontCollection& rFontCollection,
     282             :                                               bool bScalable, bool bEmbeddable ) const
     283             : {
     284             :     // This is rather expensive to do per face.
     285      304000 :     OUString aFamilyName = GetEnglishSearchFontName( GetFamilyName() );
     286      304000 :     PhysicalFontFamily* pFamily = rFontCollection.FindOrCreateFamily( aFamilyName );
     287             : 
     288     1179520 :     for( PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
     289             :     {
     290      875520 :         if( bScalable && !pFace->IsScalable() )
     291           0 :             continue;
     292      875520 :         if( bEmbeddable && !pFace->IsEmbeddable() && !pFace->IsSubsettable() )
     293           0 :             continue;
     294             : 
     295      875520 :         PhysicalFontFace* pClonedFace = pFace->Clone();
     296             : 
     297             :         assert( pClonedFace->GetFamilyName().replaceAll("-", "").trim() == GetFamilyName().replaceAll("-", "").trim() );
     298             :         assert( rFontCollection.FindOrCreateFamily( GetEnglishSearchFontName( pClonedFace->GetFamilyName() ) ) == pFamily );
     299             : 
     300      875520 :         if (! pFamily->AddFontFace( pClonedFace ) )
     301           0 :             delete pClonedFace;
     302      304000 :     }
     303      304000 : }
     304             : 
     305             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10