LCOV - code coverage report
Current view: top level - vcl/source/font - PhysicalFontFamily.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 96 152 63.2 %
Date: 2015-06-13 12:38:46 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( ImplFontAttrs& 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 |= ImplFontAttrs::SansSerif;
      37           0 :         else if ( eFamily == FAMILY_ROMAN )
      38           0 :             rType |= ImplFontAttrs::Serif;
      39           0 :         else if ( eFamily == FAMILY_SCRIPT )
      40           0 :             rType |= ImplFontAttrs::Script;
      41           0 :         else if ( eFamily == FAMILY_MODERN )
      42           0 :             rType |= ImplFontAttrs::Fixed;
      43           0 :         else if ( eFamily == FAMILY_DECORATIVE )
      44           0 :             rType |= ImplFontAttrs::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 ImplFontAttrs 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 ImplFontAttrs::CJK|ImplFontAttrs::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 ImplFontAttrs::CJK|ImplFontAttrs::CJK_KR;
      77             : 
      78             :         // chinese
      79           0 :         if ( ((ch >= 0x3400) && (ch <= 0x9FFF)) )
      80           0 :             return ImplFontAttrs::CJK|ImplFontAttrs::CJK_TC|ImplFontAttrs::CJK_SC;
      81             : 
      82             :         // cjk
      83           0 :         if ( ((ch >= 0x3000) && (ch <= 0xD7AF)) ||
      84           0 :              ((ch >= 0xFF00) && (ch <= 0xFFEE)) )
      85           0 :             return ImplFontAttrs::CJK;
      86             : 
      87             :     }
      88             : 
      89           0 :     return ImplFontAttrs::None;
      90             : }
      91             : 
      92      376164 : PhysicalFontFamily::PhysicalFontFamily( const OUString& rSearchName )
      93             : :   mpFirst( NULL ),
      94             :     maSearchName( rSearchName ),
      95             :     mnTypeFaces( 0 ),
      96             :     mnMatchType( ImplFontAttrs::None ),
      97             :     meMatchWeight( WEIGHT_DONTKNOW ),
      98             :     meMatchWidth( WIDTH_DONTKNOW ),
      99             :     meFamily( FAMILY_DONTKNOW ),
     100             :     mePitch( PITCH_DONTKNOW ),
     101      376164 :     mnMinQuality( -1 )
     102      376164 : {}
     103             : 
     104      744932 : PhysicalFontFamily::~PhysicalFontFamily()
     105             : {
     106             :     // release all physical font faces
     107     1710745 :     while( mpFirst )
     108             :     {
     109      965813 :         PhysicalFontFace* pFace = mpFirst;
     110      965813 :         mpFirst = pFace->GetNextFace();
     111      965813 :         delete pFace;
     112             :     }
     113      372466 : }
     114             : 
     115     1011801 : bool PhysicalFontFamily::AddFontFace( PhysicalFontFace* pNewData )
     116             : {
     117     1011801 :     pNewData->mpNext = NULL;
     118             : 
     119     1011801 :     if( !mpFirst )
     120             :     {
     121      376164 :         maName         = pNewData->GetFamilyName();
     122      376164 :         maMapNames     = pNewData->maMapNames;
     123      376164 :         meFamily       = pNewData->GetFamilyType();
     124      376164 :         mePitch        = pNewData->GetPitch();
     125      376164 :         mnMinQuality   = pNewData->mnQuality;
     126             :     }
     127             :     else
     128             :     {
     129      635637 :         if( meFamily == FAMILY_DONTKNOW )
     130      608457 :             meFamily = pNewData->GetFamilyType();
     131      635637 :         if( mePitch == PITCH_DONTKNOW )
     132           0 :             mePitch = pNewData->GetPitch();
     133      635637 :         if( mnMinQuality > pNewData->mnQuality )
     134      203573 :             mnMinQuality = pNewData->mnQuality;
     135             :     }
     136             : 
     137             :     // set attributes for attribute based font matching
     138     1011801 :     if( pNewData->IsScalable() )
     139     1011801 :         mnTypeFaces |= FONT_FAMILY_SCALABLE;
     140             : 
     141     1011801 :     if( pNewData->IsSymbolFont() )
     142       13473 :         mnTypeFaces |= FONT_FAMILY_SYMBOL;
     143             :     else
     144      998328 :         mnTypeFaces |= FONT_FAMILY_NONESYMBOL;
     145             : 
     146     1011801 :     if( pNewData->GetWeight() != WEIGHT_DONTKNOW )
     147             :     {
     148     1011801 :         if( pNewData->GetWeight() >= WEIGHT_SEMIBOLD )
     149      427308 :             mnTypeFaces |= FONT_FAMILY_BOLD;
     150      584493 :         else if( pNewData->GetWeight() <= WEIGHT_SEMILIGHT )
     151       40419 :             mnTypeFaces |= FONT_FAMILY_LIGHT;
     152             :         else
     153      544074 :             mnTypeFaces |= FONT_FAMILY_NORMAL;
     154             :     }
     155             : 
     156     1011801 :     if( pNewData->GetSlant() == ITALIC_NONE )
     157      593475 :         mnTypeFaces |= FONT_FAMILY_NONEITALIC;
     158      836652 :     else if( (pNewData->GetSlant() == ITALIC_NORMAL)
     159      418326 :          ||  (pNewData->GetSlant() == ITALIC_OBLIQUE) )
     160      418326 :         mnTypeFaces |= FONT_FAMILY_ITALIC;
     161             : 
     162             :     // reassign name (sharing saves memory)
     163     1011801 :     if( pNewData->GetFamilyName() == GetFamilyName() )
     164     1011801 :         pNewData->SetFamilyName( GetFamilyName() );
     165             : 
     166             :     // insert new physical font face into linked list
     167             :     // TODO: get rid of linear search?
     168             :     PhysicalFontFace* pData;
     169     1011801 :     PhysicalFontFace** ppHere = &mpFirst;
     170     2179375 :     for(; (pData=*ppHere) != NULL; ppHere=&pData->mpNext )
     171             :     {
     172     1267265 :         sal_Int32 eComp = pNewData->CompareWithSize( *pData );
     173     1267265 :         if( eComp > 0 )
     174     1167574 :             continue;
     175       99691 :         if( eComp < 0 )
     176       63292 :             break;
     177             : 
     178             :         // ignore duplicate if its quality is worse
     179       36399 :         if( pNewData->mnQuality < pData->mnQuality )
     180        9840 :             return false;
     181             : 
     182             :         // keep the device font if its quality is good enough
     183       26559 :         if( (pNewData->mnQuality == pData->mnQuality)
     184       26091 :         &&  (pData->mbDevice || !pNewData->mbDevice) )
     185       26091 :             return false;
     186             : 
     187             :         // replace existing font face with a better one
     188         468 :         pNewData->mpNext = pData->mpNext;
     189         468 :         *ppHere = pNewData;
     190         468 :         delete pData;
     191         468 :         return true;
     192             :     }
     193             : 
     194             :     // insert into or append to list of physical font faces
     195      975402 :     pNewData->mpNext = pData;
     196      975402 :     *ppHere = pNewData;
     197      975402 :     return true;
     198             : }
     199             : 
     200             : // get font attributes using the normalized font family name
     201           0 : void PhysicalFontFamily::InitMatchData( const utl::FontSubstConfiguration& rFontSubst,
     202             :     const OUString& rSearchName )
     203             : {
     204           0 :     OUString aShortName;
     205           0 :     OUString aMatchFamilyName(maMatchFamilyName);
     206             :     // get font attributes from the decorated font name
     207             :     utl::FontSubstConfiguration::getMapName( rSearchName, aShortName, aMatchFamilyName,
     208           0 :                             meMatchWeight, meMatchWidth, mnMatchType );
     209           0 :     maMatchFamilyName = aMatchFamilyName;
     210           0 :     const utl::FontNameAttr* pFontAttr = rFontSubst.getSubstInfo( rSearchName );
     211             :     // eventually use the stripped name
     212           0 :     if( !pFontAttr )
     213           0 :         if( aShortName != rSearchName )
     214           0 :             pFontAttr = rFontSubst.getSubstInfo( aShortName );
     215           0 :     CalcType( mnMatchType, meMatchWeight, meMatchWidth, meFamily, pFontAttr );
     216           0 :     mnMatchType |= lcl_IsCJKFont( maName );
     217           0 : }
     218             : 
     219       13994 : PhysicalFontFace* PhysicalFontFamily::FindBestFontFace( const FontSelectPattern& rFSD ) const
     220             : {
     221       13994 :     if( !mpFirst )
     222           0 :         return NULL;
     223       13994 :     if( !mpFirst->GetNextFace() )
     224        1291 :         return mpFirst;
     225             : 
     226             :     // FontName+StyleName should map to FamilyName+StyleName
     227       12703 :     const OUString& rSearchName = rFSD.maTargetName;
     228       12703 :     OUString aTargetStyleName;
     229       12703 :     const OUString* pTargetStyleName = NULL;
     230       25406 :     if(    (rSearchName.getLength() > maSearchName.getLength())
     231       12703 :         && rSearchName.startsWith( maSearchName ) )
     232             :     {
     233           0 :         aTargetStyleName = rSearchName.copy(maSearchName.getLength() + 1);
     234           0 :         pTargetStyleName = &aTargetStyleName;
     235             :     }
     236             : 
     237             :     // TODO: linear search improve!
     238       12703 :     PhysicalFontFace* pFontFace = mpFirst;
     239       12703 :     PhysicalFontFace* pBestFontFace = pFontFace;
     240       12703 :     FontMatchStatus aFontMatchStatus = {0,0,0, pTargetStyleName};
     241       63905 :     for(; pFontFace; pFontFace = pFontFace->GetNextFace() )
     242       51202 :         if( pFontFace->IsBetterMatch( rFSD, aFontMatchStatus ) )
     243       16398 :             pBestFontFace = pFontFace;
     244             : 
     245       12703 :     return pBestFontFace;
     246             : }
     247             : 
     248             : // update device font list with unique font faces, with uniqueness
     249             : // meaning different font attributes, but not different fonts sizes
     250      394568 : void PhysicalFontFamily::UpdateDevFontList( ImplGetDevFontList& rDevFontList ) const
     251             : {
     252      394568 :     PhysicalFontFace* pPrevFace = NULL;
     253     1417692 :     for( PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
     254             :     {
     255     1023124 :         if( !pPrevFace || pFace->CompareIgnoreSize( *pPrevFace ) )
     256     1023124 :             rDevFontList.Add( pFace );
     257     1023124 :         pPrevFace = pFace;
     258             :     }
     259      394568 : }
     260             : 
     261        1665 : void PhysicalFontFamily::GetFontHeights( std::set<int>& rHeights ) const
     262             : {
     263             :     // add all available font heights
     264        8325 :     for( const PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
     265        6660 :         rHeights.insert( pFace->GetHeight() );
     266        1665 : }
     267             : 
     268      312352 : void PhysicalFontFamily::UpdateCloneFontList( PhysicalFontCollection& rFontCollection,
     269             :                                               bool bScalable, bool bEmbeddable ) const
     270             : {
     271             :     // This is rather expensive to do per face.
     272      312352 :     OUString aFamilyName = GetEnglishSearchFontName( GetFamilyName() );
     273      312352 :     PhysicalFontFamily* pFamily = rFontCollection.FindOrCreateFamily( aFamilyName );
     274             : 
     275     1122288 :     for( PhysicalFontFace* pFace = mpFirst; pFace; pFace = pFace->GetNextFace() )
     276             :     {
     277      809936 :         if( bScalable && !pFace->IsScalable() )
     278           0 :             continue;
     279      809936 :         if( bEmbeddable && !pFace->IsEmbeddable() && !pFace->IsSubsettable() )
     280           0 :             continue;
     281             : 
     282      809936 :         PhysicalFontFace* pClonedFace = pFace->Clone();
     283             : 
     284             :         assert( pClonedFace->GetFamilyName().replaceAll("-", "").trim() == GetFamilyName().replaceAll("-", "").trim() );
     285             :         assert( rFontCollection.FindOrCreateFamily( GetEnglishSearchFontName( pClonedFace->GetFamilyName() ) ) == pFamily );
     286             : 
     287      809936 :         if (! pFamily->AddFontFace( pClonedFace ) )
     288           0 :             delete pClonedFace;
     289      312352 :     }
     290      312352 : }
     291             : 
     292             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11