LCOV - code coverage report
Current view: top level - libreoffice/vcl/generic/glyphs - gcach_ftyp.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 606 1187 51.1 %
Date: 2012-12-27 Functions: 52 70 74.3 %
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             : 
      21             : #ifdef WNT
      22             : #include <svsys.h>
      23             : #undef CreateFont
      24             : #endif
      25             : 
      26             : #include "gcach_ftyp.hxx"
      27             : 
      28             : #include "vcl/svapp.hxx"
      29             : #include <outfont.hxx>
      30             : #include <impfont.hxx>
      31             : #include <config_graphite.h>
      32             : #ifdef ENABLE_GRAPHITE
      33             : #include <graphite2/Font.h>
      34             : #include <graphite_layout.hxx>
      35             : #endif
      36             : 
      37             : #include "tools/poly.hxx"
      38             : #include "basegfx/matrix/b2dhommatrix.hxx"
      39             : #include "basegfx/matrix/b2dhommatrixtools.hxx"
      40             : #include "basegfx/polygon/b2dpolypolygon.hxx"
      41             : 
      42             : #include "osl/file.hxx"
      43             : #include "osl/thread.hxx"
      44             : 
      45             : #include "langboost.hxx"
      46             : #include "sft.hxx"
      47             : 
      48             : #include <ft2build.h>
      49             : #include FT_FREETYPE_H
      50             : #include FT_GLYPH_H
      51             : #include FT_OUTLINE_H
      52             : #include FT_TRUETYPE_TABLES_H
      53             : #include FT_TRUETYPE_TAGS_H
      54             : #include FT_TRUETYPE_IDS_H
      55             : 
      56             : #ifndef FT_RENDER_MODE_MONO  // happens in the MACOSX build
      57             :     #define FT_RENDER_MODE_MONO ft_render_mode_mono
      58             : #endif
      59             : #include "rtl/instance.hxx"
      60             : 
      61             : #ifndef FREETYPE_PATCH
      62             :     // VERSION_MINOR in freetype.h is too coarse
      63             :     // if patch-level is not available we need to fine-tune the version ourselves
      64             :     #define FTVERSION 2005
      65             : #else
      66             :     #define FTVERSION (1000*FREETYPE_MAJOR + 100*FREETYPE_MINOR + FREETYPE_PATCH)
      67             : #endif
      68             : #if FTVERSION >= 2200
      69             : typedef const FT_Vector* FT_Vector_CPtr;
      70             : #else // FTVERSION < 2200
      71             : typedef FT_Vector* FT_Vector_CPtr;
      72             : #endif
      73             : 
      74             : #include <vector>
      75             : 
      76             : // TODO: move file mapping stuff to OSL
      77             : #if defined(UNX)
      78             :     // PORTERS: dlfcn is used for getting symbols from FT versions newer than baseline
      79             :     #include <dlfcn.h>
      80             :     #include <unistd.h>
      81             :     #include <fcntl.h>
      82             :     #include <sys/stat.h>
      83             :     #include <sys/mman.h>
      84             :     #include "vcl/fontmanager.hxx"
      85             : #elif defined(WNT)
      86             :     #include <io.h>
      87             :     #define strncasecmp strnicmp
      88             : #endif
      89             : 
      90             : typedef const unsigned char* CPU8;
      91           0 : inline sal_uInt16 NEXT_U16( CPU8& p ) { p+=2; return (p[-2]<<8)|p[-1]; }
      92           0 : inline sal_Int16  NEXT_S16( CPU8& p ) { return (sal_Int16)NEXT_U16(p); }
      93           0 : inline sal_uInt32 NEXT_U32( CPU8& p ) { p+=4; return (p[-4]<<24)|(p[-3]<<16)|(p[-2]<<8)|p[-1]; }
      94             : //inline sal_Int32 NEXT_S32( U8*& p ) { return (sal_Int32)NEXT_U32(p); }
      95             : 
      96             : // -----------------------------------------------------------------------
      97             : 
      98             : // the gamma table makes artificial bold look better for CJK glyphs
      99             : static unsigned char aGammaTable[257];
     100             : 
     101          20 : static void InitGammaTable()
     102             : {
     103             :     static const int M_MAX = 255;
     104             :     static const int M_X   = 128;
     105             :     static const int M_Y   = 208;
     106             : 
     107             :     int x, a;
     108        5140 :     for( x = 0; x < 256; x++)
     109             :     {
     110        5120 :         if ( x <= M_X )
     111        2580 :             a = ( x * M_Y + M_X / 2) / M_X;
     112             :         else
     113             :             a = M_Y + ( ( x - M_X ) * ( M_MAX - M_Y ) +
     114        2540 :                 ( M_MAX - M_X ) / 2 ) / ( M_MAX - M_X );
     115             : 
     116        5120 :         aGammaTable[x] = (unsigned char)a;
     117             :     }
     118          20 : }
     119             : 
     120             : // -----------------------------------------------------------------------
     121             : 
     122             : static FT_Library aLibFT = 0;
     123             : 
     124             : // #110607# enable linking with old FT versions
     125             : static int nFTVERSION = 0;
     126             : static FT_Error (*pFTNewSize)(FT_Face,FT_Size*);
     127             : static FT_Error (*pFTActivateSize)(FT_Size);
     128             : static FT_Error (*pFTDoneSize)(FT_Size);
     129             : FT_Error (*pFTEmbolden)(FT_GlyphSlot);
     130             : FT_Error (*pFTOblique)(FT_GlyphSlot);
     131             : static bool bEnableSizeFT = false;
     132             : 
     133         100 : struct EqStr{ bool operator()(const char* a, const char* b) const { return !strcmp(a,b); } };
     134        4440 : struct HashStr { size_t operator()( const char* s ) const { return rtl_str_hashCode(s); } };
     135             : typedef ::boost::unordered_map<const char*,boost::shared_ptr<FtFontFile>,HashStr, EqStr> FontFileList;
     136             : namespace { struct vclFontFileList : public rtl::Static< FontFileList, vclFontFileList > {}; }
     137             : 
     138             : // -----------------------------------------------------------------------
     139             : 
     140             : // TODO: remove when the priorities are selected by UI
     141             : // if (AH==0) => disable autohinting
     142             : // if (AA==0) => disable antialiasing
     143             : // if (EB==0) => disable embedded bitmaps
     144             : // if (AA prio <= AH prio) => antialias + autohint
     145             : // if (AH<AA) => do not autohint when antialiasing
     146             : // if (EB<AH) => do not autohint for monochrome
     147             : static int nDefaultPrioEmbedded    = 2;
     148             : static int nDefaultPrioAutoHint    = 1;
     149             : static int nDefaultPrioAntiAlias   = 1;
     150             : 
     151             : // =======================================================================
     152             : // FreetypeManager
     153             : // =======================================================================
     154             : 
     155        2180 : FtFontFile::FtFontFile( const ::rtl::OString& rNativeFileName )
     156             : :   maNativeFileName( rNativeFileName ),
     157             :     mpFileMap( NULL ),
     158             :     mnFileSize( 0 ),
     159             :     mnRefCount( 0 ),
     160        2180 :     mnLangBoost( 0 )
     161             : {
     162             :     // boost font preference if UI language is mentioned in filename
     163        2180 :     int nPos = maNativeFileName.lastIndexOf( '_' );
     164        2180 :     if( nPos == -1 || maNativeFileName[nPos+1] == '.' )
     165        2120 :         mnLangBoost += 0x1000;     // no langinfo => good
     166             :     else
     167             :     {
     168             :         static const char* pLangBoost = NULL;
     169             :         static bool bOnce = true;
     170          60 :         if( bOnce )
     171             :         {
     172          20 :             bOnce = false;
     173          20 :             pLangBoost = vcl::getLangBoost();
     174             :         }
     175             : 
     176          60 :         if( pLangBoost && !strncasecmp( pLangBoost, &maNativeFileName.getStr()[nPos+1], 3 ) )
     177           0 :            mnLangBoost += 0x2000;     // matching langinfo => better
     178             :     }
     179        2180 : }
     180             : 
     181             : // -----------------------------------------------------------------------
     182             : 
     183        2280 : FtFontFile* FtFontFile::FindFontFile( const ::rtl::OString& rNativeFileName )
     184             : {
     185             :     // font file already known? (e.g. for ttc, synthetic, aliased fonts)
     186        2280 :     const char* pFileName = rNativeFileName.getStr();
     187        2280 :     FontFileList &rFontFileList = vclFontFileList::get();
     188        2280 :     FontFileList::const_iterator it = rFontFileList.find( pFileName );
     189        2280 :     if( it != rFontFileList.end() )
     190         100 :         return it->second.get();
     191             : 
     192             :     // no => create new one
     193        2180 :     FtFontFile* pFontFile = new FtFontFile( rNativeFileName );
     194        2180 :     pFileName = pFontFile->maNativeFileName.getStr();
     195        2180 :     rFontFileList[pFileName].reset(pFontFile);
     196        2180 :     return pFontFile;
     197             : }
     198             : 
     199             : // -----------------------------------------------------------------------
     200             : 
     201          90 : bool FtFontFile::Map()
     202             : {
     203          90 :     if( mnRefCount++ <= 0 )
     204             :     {
     205          90 :         const char* pFileName = maNativeFileName.getStr();
     206             : #if defined(UNX)
     207          90 :         int nFile = open( pFileName, O_RDONLY );
     208          90 :         if( nFile < 0 )
     209           0 :             return false;
     210             : 
     211             :         struct stat aStat;
     212          90 :         fstat( nFile, &aStat );
     213          90 :         mnFileSize = aStat.st_size;
     214             :         mpFileMap = (const unsigned char*)
     215          90 :             mmap( NULL, mnFileSize, PROT_READ, MAP_SHARED, nFile, 0 );
     216          90 :         if( mpFileMap == MAP_FAILED )
     217           0 :             mpFileMap = NULL;
     218          90 :         close( nFile );
     219             : #elif defined(WNT)
     220             :         void* pFileDesc = ::CreateFile( pFileName, GENERIC_READ, FILE_SHARE_READ,
     221             :                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
     222             :         if( pFileDesc == INVALID_HANDLE_VALUE)
     223             :             return false;
     224             : 
     225             :         mnFileSize = ::GetFileSize( pFileDesc, NULL );
     226             :         HANDLE aHandle = ::CreateFileMapping( pFileDesc, NULL, PAGE_READONLY, 0, mnFileSize, "TTF" );
     227             :         mpFileMap = (const unsigned char*)::MapViewOfFile( aHandle, FILE_MAP_READ, 0, 0, mnFileSize );
     228             :         ::CloseHandle( pFileDesc );
     229             : #else
     230             :         FILE* pFile = fopen( pFileName, "rb" );
     231             :         if( !pFile )
     232             :             return false;
     233             : 
     234             :         struct stat aStat;
     235             :         stat( pFileName, &aStat );
     236             :         mnFileSize = aStat.st_size;
     237             :         mpFileMap = new unsigned char[ mnFileSize ];
     238             :         if( mnFileSize != fread( mpFileMap, 1, mnFileSize, pFile ) )
     239             :         {
     240             :             delete[] mpFileMap;
     241             :             mpFileMap = NULL;
     242             :         }
     243             :         fclose( pFile );
     244             : #endif
     245             :     }
     246             : 
     247          90 :     return (mpFileMap != NULL);
     248             : }
     249             : 
     250             : // -----------------------------------------------------------------------
     251             : 
     252          90 : void FtFontFile::Unmap()
     253             : {
     254          90 :     if( (--mnRefCount > 0) || (mpFileMap == NULL) )
     255          90 :         return;
     256             : 
     257             : #if defined(UNX)
     258          90 :     munmap( (char*)mpFileMap, mnFileSize );
     259             : #elif defined(WNT)
     260             :     UnmapViewOfFile( (LPCVOID)mpFileMap );
     261             : #else
     262             :     delete[] mpFileMap;
     263             : #endif
     264             : 
     265          90 :     mpFileMap = NULL;
     266             : }
     267             : 
     268             : #ifdef ENABLE_GRAPHITE
     269             : // wrap FtFontInfo's table function
     270           0 : const void * graphiteFontTable(const void* appFaceHandle, unsigned int name, size_t *len)
     271             : {
     272           0 :     const FtFontInfo * pFontInfo = reinterpret_cast<const FtFontInfo*>(appFaceHandle);
     273             :     typedef union {
     274             :         char m_c[5];
     275             :         unsigned int m_id;
     276             :     } TableId;
     277             :     TableId tableId;
     278           0 :     tableId.m_id = name;
     279             : #ifndef WORDS_BIGENDIAN
     280             :     TableId swapped;
     281           0 :     swapped.m_c[3] = tableId.m_c[0];
     282           0 :     swapped.m_c[2] = tableId.m_c[1];
     283           0 :     swapped.m_c[1] = tableId.m_c[2];
     284           0 :     swapped.m_c[0] = tableId.m_c[3];
     285           0 :     tableId.m_id = swapped.m_id;
     286             : #endif
     287           0 :     tableId.m_c[4] = '\0';
     288           0 :     sal_uLong nLength = 0;
     289           0 :     const void * pTable = static_cast<const void*>(pFontInfo->GetTable(tableId.m_c, &nLength));
     290           0 :     if (len) *len = static_cast<size_t>(nLength);
     291           0 :     return pTable;
     292             : }
     293             : #endif
     294             : 
     295             : // =======================================================================
     296             : 
     297        2280 : FtFontInfo::FtFontInfo( const ImplDevFontAttributes& rDevFontAttributes,
     298             :     const ::rtl::OString& rNativeFileName, int nFaceNum, sal_IntPtr nFontId, int nSynthetic,
     299             :     const ExtraKernInfo* pExtraKernInfo )
     300             : :
     301             :     maFaceFT( NULL ),
     302        2280 :     mpFontFile( FtFontFile::FindFontFile( rNativeFileName ) ),
     303             :     mnFaceNum( nFaceNum ),
     304             :     mnRefCount( 0 ),
     305             :     mnSynthetic( nSynthetic ),
     306             : #ifdef ENABLE_GRAPHITE
     307             :     mbCheckedGraphite(false),
     308             :     mpGraphiteFace(NULL),
     309             : #endif
     310             :     mnFontId( nFontId ),
     311             :     maDevFontAttributes( rDevFontAttributes ),
     312             :     mpFontCharMap( NULL ),
     313             :     mpChar2Glyph( NULL ),
     314             :     mpGlyph2Char( NULL ),
     315        4560 :     mpExtraKernInfo( pExtraKernInfo )
     316             : {
     317             :     // prefer font with low ID
     318        2280 :     maDevFontAttributes.mnQuality += 10000 - nFontId;
     319             :     // prefer font with matching file names
     320        2280 :     maDevFontAttributes.mnQuality += mpFontFile->GetLangBoost();
     321             :     // prefer font with more external info
     322        2280 :     if( pExtraKernInfo )
     323         700 :         maDevFontAttributes.mnQuality += 100;
     324        2280 : }
     325             : 
     326             : // -----------------------------------------------------------------------
     327             : 
     328        4560 : FtFontInfo::~FtFontInfo()
     329             : {
     330        2280 :     if( mpFontCharMap )
     331          22 :         mpFontCharMap->DeReference();
     332        2280 :     delete mpExtraKernInfo;
     333        2280 :     delete mpChar2Glyph;
     334        2280 :     delete mpGlyph2Char;
     335             : #ifdef ENABLE_GRAPHITE
     336        2280 :     delete mpGraphiteFace;
     337             : #endif
     338        2280 : }
     339             : 
     340          88 : void FtFontInfo::InitHashes() const
     341             : {
     342             :     // TODO: avoid pointers when empty stl::hash_* objects become cheap
     343          88 :     mpChar2Glyph = new Int2IntMap();
     344          88 :     mpGlyph2Char = new Int2IntMap();
     345          88 : }
     346             : 
     347             : // -----------------------------------------------------------------------
     348             : 
     349         974 : FT_FaceRec_* FtFontInfo::GetFaceFT()
     350             : {
     351             :     // get faceFT once/multiple depending on availability of SizeFT APIs
     352         974 :     if( (mnRefCount++ <= 0) || !bEnableSizeFT )
     353             :     {
     354          90 :         if( !mpFontFile->Map() )
     355           0 :             return NULL;
     356             :         FT_Error rc = FT_New_Memory_Face( aLibFT,
     357          90 :             (FT_Byte*)mpFontFile->GetBuffer(),
     358         180 :             mpFontFile->GetFileSize(), mnFaceNum, &maFaceFT );
     359          90 :         if( (rc != FT_Err_Ok) || (maFaceFT->num_glyphs <= 0) )
     360           0 :             maFaceFT = NULL;
     361             :     }
     362             : 
     363         974 :    return maFaceFT;
     364             : }
     365             : 
     366             : #ifdef ENABLE_GRAPHITE
     367           0 : GraphiteFaceWrapper * FtFontInfo::GetGraphiteFace()
     368             : {
     369           0 :     if (mbCheckedGraphite)
     370           0 :         return mpGraphiteFace;
     371             :     // test for graphite here so that it is cached most efficiently
     372           0 :     if (GetTable("Silf", 0))
     373             :     {
     374           0 :         static const char* pGraphiteCacheStr = getenv( "SAL_GRAPHITE_CACHE_SIZE" );
     375           0 :         int graphiteSegCacheSize = pGraphiteCacheStr ? (atoi(pGraphiteCacheStr)) : 0;
     376             :         gr_face * pGraphiteFace;
     377           0 :         if (graphiteSegCacheSize > 500)
     378           0 :             pGraphiteFace = gr_make_face_with_seg_cache(this, graphiteFontTable, graphiteSegCacheSize, gr_face_cacheCmap);
     379             :         else
     380           0 :             pGraphiteFace = gr_make_face(this, graphiteFontTable, gr_face_cacheCmap);
     381           0 :         if (pGraphiteFace)
     382           0 :             mpGraphiteFace = new GraphiteFaceWrapper(pGraphiteFace);
     383             :     }
     384           0 :     mbCheckedGraphite = true;
     385           0 :     return mpGraphiteFace;
     386             : }
     387             : #endif
     388             : 
     389             : // -----------------------------------------------------------------------
     390             : 
     391         974 : void FtFontInfo::ReleaseFaceFT( FT_FaceRec_* pFaceFT )
     392             : {
     393             :     // release last/each depending on SizeFT availability
     394         974 :     if( (--mnRefCount <= 0) || !bEnableSizeFT )
     395             :     {
     396          90 :         FT_Done_Face( pFaceFT );
     397          90 :         maFaceFT = NULL;
     398          90 :         mpFontFile->Unmap();
     399             :     }
     400         974 : }
     401             : 
     402             : // -----------------------------------------------------------------------
     403             : 
     404        4538 : bool FtFontInfo::HasExtraKerning() const
     405             : {
     406        4538 :     if( !mpExtraKernInfo )
     407         819 :         return false;
     408             :     // TODO: how to enable the line below without getting #i29881# back?
     409             :     // on the other hand being too optimistic doesn't cause problems
     410             :     // return mpExtraKernInfo->HasKernPairs();
     411        3719 :     return true;
     412             : }
     413             : 
     414             : // -----------------------------------------------------------------------
     415             : 
     416           0 : int FtFontInfo::GetExtraKernPairs( ImplKernPairData** ppKernPairs ) const
     417             : {
     418           0 :     if( !mpExtraKernInfo )
     419           0 :         return 0;
     420           0 :     return mpExtraKernInfo->GetUnscaledKernPairs( ppKernPairs );
     421             : }
     422             : 
     423             : // -----------------------------------------------------------------------
     424             : 
     425        8404 : static unsigned GetUInt( const unsigned char* p ) { return((p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3]);}
     426        2622 : static unsigned GetUShort( const unsigned char* p ){ return((p[0]<<8)+p[1]);}
     427             : //static signed GetSShort( const unsigned char* p ){ return((short)((p[0]<<8)+p[1]));}
     428             : 
     429             : static const sal_uInt32 T_true = 0x74727565;        /* 'true' */
     430             : static const sal_uInt32 T_ttcf = 0x74746366;        /* 'ttcf' */
     431             : static const sal_uInt32 T_otto = 0x4f54544f;        /* 'OTTO' */
     432             : 
     433        3220 : const unsigned char* FtFontInfo::GetTable( const char* pTag, sal_uLong* pLength ) const
     434             : {
     435        3220 :     const unsigned char* pBuffer = mpFontFile->GetBuffer();
     436        3220 :     int nFileSize = mpFontFile->GetFileSize();
     437        3220 :     if( !pBuffer || nFileSize<1024 )
     438           0 :         return NULL;
     439             : 
     440             :     // we currently handle TTF, TTC and OTF headers
     441        3220 :     unsigned nFormat = GetUInt( pBuffer );
     442             : 
     443        3220 :     const unsigned char* p = pBuffer + 12;
     444        3220 :     if( nFormat == T_ttcf )         // TTC_MAGIC
     445          10 :         p += GetUInt( p + 4 * mnFaceNum );
     446        3210 :     else if( nFormat != 0x00010000 && nFormat != T_true && nFormat != T_otto) // TTF_MAGIC and Apple TTF Magic and PS-OpenType font
     447         598 :         return NULL;
     448             : 
     449             :     // walk table directory until match
     450        2622 :     int nTables = GetUShort( p - 8 );
     451        2622 :     if( nTables >= 64 )  // something fishy?
     452           0 :         return NULL;
     453        8370 :     for( int i = 0; i < nTables; ++i, p+=16 )
     454             :     {
     455        8335 :         if( p[0]==pTag[0] && p[1]==pTag[1] && p[2]==pTag[2] && p[3]==pTag[3] )
     456             :         {
     457        2587 :             sal_uLong nLength = GetUInt( p + 12 );
     458        2587 :             if( pLength != NULL )
     459        2587 :                 *pLength = nLength;
     460        2587 :             const unsigned char* pTable = pBuffer + GetUInt( p + 8 );
     461        2587 :             if( (pTable + nLength) <= (mpFontFile->GetBuffer() + nFileSize) )
     462        2587 :                 return pTable;
     463             :         }
     464             :     }
     465             : 
     466          35 :     return NULL;
     467             : }
     468             : 
     469             : // -----------------------------------------------------------------------
     470             : 
     471        2622 : void FtFontInfo::AnnounceFont( ImplDevFontList* pFontList )
     472             : {
     473        2622 :     ImplFTSFontData* pFD = new ImplFTSFontData( this, maDevFontAttributes );
     474        2622 :     pFontList->Add( pFD );
     475        2622 : }
     476             : 
     477             : // =======================================================================
     478             : 
     479          20 : FreetypeManager::FreetypeManager()
     480          20 : :   mnMaxFontId( 0 )
     481             : {
     482          20 :     /*FT_Error rcFT =*/ FT_Init_FreeType( &aLibFT );
     483             : 
     484             : #ifdef RTLD_DEFAULT // true if a good dlfcn.h header was included
     485             :     // Get version of freetype library to enable workarounds.
     486             :     // Freetype <= 2.0.9 does not have FT_Library_Version().
     487             :     // Using dl_sym() instead of osl_getSymbol() because latter
     488             :     // isn't designed to work with oslModule=NULL
     489             :     void (*pFTLibraryVersion)(FT_Library library,
     490             :         FT_Int *amajor, FT_Int *aminor, FT_Int *apatch);
     491             :     pFTLibraryVersion = (void (*)(FT_Library library,
     492          20 :         FT_Int *amajor, FT_Int *aminor, FT_Int *apatch))(sal_IntPtr)dlsym( RTLD_DEFAULT, "FT_Library_Version" );
     493             : 
     494          20 :     pFTNewSize      = (FT_Error(*)(FT_Face,FT_Size*))(sal_IntPtr)dlsym( RTLD_DEFAULT, "FT_New_Size" );
     495          20 :     pFTActivateSize = (FT_Error(*)(FT_Size))(sal_IntPtr)dlsym( RTLD_DEFAULT, "FT_Activate_Size" );
     496          20 :     pFTDoneSize     = (FT_Error(*)(FT_Size))(sal_IntPtr)dlsym( RTLD_DEFAULT, "FT_Done_Size" );
     497          20 :     pFTEmbolden     = (FT_Error(*)(FT_GlyphSlot))(sal_IntPtr)dlsym( RTLD_DEFAULT, "FT_GlyphSlot_Embolden" );
     498          20 :     pFTOblique      = (FT_Error(*)(FT_GlyphSlot))(sal_IntPtr)dlsym( RTLD_DEFAULT, "FT_GlyphSlot_Oblique" );
     499             : 
     500          20 :     bEnableSizeFT = (pFTNewSize!=NULL) && (pFTActivateSize!=NULL) && (pFTDoneSize!=NULL);
     501             : 
     502          20 :     FT_Int nMajor = 0, nMinor = 0, nPatch = 0;
     503          20 :     if( pFTLibraryVersion )
     504          20 :         pFTLibraryVersion( aLibFT, &nMajor, &nMinor, &nPatch );
     505          20 :     nFTVERSION = nMajor * 1000 + nMinor * 100 + nPatch;
     506             : 
     507             :     // disable embedded bitmaps for Freetype-2.1.3 unless explicitly
     508             :     // requested by env var below because it crashes StarOffice on RH9
     509             :     // reason: double free in freetype's embedded bitmap handling
     510          20 :     if( nFTVERSION == 2103 )
     511           0 :         nDefaultPrioEmbedded = 0;
     512             :     // disable artificial emboldening with the Freetype API for older versions
     513          20 :     if( nFTVERSION < 2110 )
     514           0 :         pFTEmbolden = NULL;
     515             : 
     516             : #else // RTLD_DEFAULT
     517             :     // assume systems where dlsym is not possible use supplied library
     518             :     nFTVERSION = FTVERSION;
     519             : #endif
     520             : 
     521             :     // TODO: remove when the priorities are selected by UI
     522             :     char* pEnv;
     523          20 :     pEnv = ::getenv( "SAL_EMBEDDED_BITMAP_PRIORITY" );
     524          20 :     if( pEnv )
     525           0 :         nDefaultPrioEmbedded  = pEnv[0] - '0';
     526          20 :     pEnv = ::getenv( "SAL_ANTIALIASED_TEXT_PRIORITY" );
     527          20 :     if( pEnv )
     528           0 :         nDefaultPrioAntiAlias = pEnv[0] - '0';
     529          20 :     pEnv = ::getenv( "SAL_AUTOHINTING_PRIORITY" );
     530          20 :     if( pEnv )
     531           0 :         nDefaultPrioAutoHint  = pEnv[0] - '0';
     532             : 
     533          20 :     InitGammaTable();
     534          20 :     vclFontFileList::get();
     535          20 : }
     536             : 
     537             : // -----------------------------------------------------------------------
     538             : 
     539           0 : FT_Face ServerFont::GetFtFace() const
     540             : {
     541           0 :     if( maSizeFT )
     542           0 :         pFTActivateSize( maSizeFT );
     543             : 
     544           0 :     return maFaceFT;
     545             : }
     546             : 
     547             : // -----------------------------------------------------------------------
     548             : 
     549          40 : FreetypeManager::~FreetypeManager()
     550             : {
     551          20 :     ClearFontList();
     552          20 : }
     553             : 
     554             : // -----------------------------------------------------------------------
     555             : 
     556        2622 : void FreetypeManager::AddFontFile( const rtl::OString& rNormalizedName,
     557             :     int nFaceNum, sal_IntPtr nFontId, const ImplDevFontAttributes& rDevFontAttr,
     558             :     const ExtraKernInfo* pExtraKernInfo )
     559             : {
     560        2622 :     if( rNormalizedName.isEmpty() )
     561           0 :         return;
     562             : 
     563        2622 :     if( maFontList.find( nFontId ) != maFontList.end() )
     564         342 :         return;
     565             : 
     566             :     FtFontInfo* pFontInfo = new FtFontInfo( rDevFontAttr,
     567        2280 :         rNormalizedName, nFaceNum, nFontId, 0, pExtraKernInfo );
     568        2280 :     maFontList[ nFontId ] = pFontInfo;
     569        2280 :     if( mnMaxFontId < nFontId )
     570          20 :         mnMaxFontId = nFontId;
     571             : }
     572             : 
     573             : // -----------------------------------------------------------------------
     574             : 
     575          23 : void FreetypeManager::AnnounceFonts( ImplDevFontList* pToAdd ) const
     576             : {
     577        2645 :     for( FontList::const_iterator it = maFontList.begin(); it != maFontList.end(); ++it )
     578             :     {
     579        2622 :         FtFontInfo* pFtFontInfo = it->second;
     580        2622 :         pFtFontInfo->AnnounceFont( pToAdd );
     581             :     }
     582          23 : }
     583             : 
     584             : // -----------------------------------------------------------------------
     585             : 
     586          20 : void FreetypeManager::ClearFontList( )
     587             : {
     588        2300 :     for( FontList::iterator it = maFontList.begin(); it != maFontList.end(); ++it )
     589             :     {
     590        2280 :         FtFontInfo* pFtFontInfo = it->second;
     591        2280 :         delete pFtFontInfo;
     592             :     }
     593          20 :     maFontList.clear();
     594          20 : }
     595             : 
     596             : // -----------------------------------------------------------------------
     597             : 
     598         974 : ServerFont* FreetypeManager::CreateFont( const FontSelectPattern& rFSD )
     599             : {
     600         974 :     FtFontInfo* pFontInfo = NULL;
     601             : 
     602             :     // find a FontInfo matching to the font id
     603         974 :     sal_IntPtr nFontId = reinterpret_cast<sal_IntPtr>( rFSD.mpFontData );
     604         974 :     FontList::iterator it = maFontList.find( nFontId );
     605         974 :     if( it != maFontList.end() )
     606         974 :         pFontInfo = it->second;
     607             : 
     608         974 :     if( !pFontInfo )
     609           0 :         return NULL;
     610             : 
     611         974 :     ServerFont* pNew = new ServerFont( rFSD, pFontInfo );
     612             : 
     613         974 :     return pNew;
     614             : }
     615             : 
     616             : // =======================================================================
     617             : 
     618        2622 : ImplFTSFontData::ImplFTSFontData( FtFontInfo* pFI, const ImplDevFontAttributes& rDFA )
     619             : :   PhysicalFontFace( rDFA, IFTSFONT_MAGIC ),
     620        2622 :     mpFtFontInfo( pFI )
     621             : {
     622        2622 :     mbDevice        = false;
     623        2622 :     mbOrientation   = true;
     624        2622 : }
     625             : 
     626             : // -----------------------------------------------------------------------
     627             : 
     628        1865 : ImplFontEntry* ImplFTSFontData::CreateFontInstance( FontSelectPattern& rFSD ) const
     629             : {
     630        1865 :     ImplServerFontEntry* pEntry = new ImplServerFontEntry( rFSD );
     631        1865 :     return pEntry;
     632             : }
     633             : 
     634             : // =======================================================================
     635             : // ServerFont
     636             : // =======================================================================
     637             : 
     638         974 : ServerFont::ServerFont( const FontSelectPattern& rFSD, FtFontInfo* pFI )
     639             : :   maGlyphList( 0),
     640             :     maFontSelData(rFSD),
     641             :     mnExtInfo(0),
     642             :     mnRefCount(1),
     643             :     mnBytesUsed( sizeof(ServerFont) ),
     644             :     mpPrevGCFont( NULL ),
     645             :     mpNextGCFont( NULL ),
     646             :     mnCos( 0x10000),
     647             :     mnSin( 0 ),
     648             :     mnZWJ( 0 ),
     649             :     mnZWNJ( 0 ),
     650             :     mbCollectedZW( false ),
     651             :     mnPrioEmbedded(nDefaultPrioEmbedded),
     652             :     mnPrioAntiAlias(nDefaultPrioAntiAlias),
     653             :     mnPrioAutoHint(nDefaultPrioAutoHint),
     654             :     mpFontInfo( pFI ),
     655             :     maFaceFT( NULL ),
     656             :     maSizeFT( NULL ),
     657             :     mbFaceOk( false ),
     658             :     maRecodeConverter( NULL ),
     659         974 :     mpLayoutEngine( NULL )
     660             : {
     661             :     // TODO: move update of mpFontEntry into FontEntry class when
     662             :     // it becomes reponsible for the ServerFont instantiation
     663         974 :     ((ImplServerFontEntry*)rFSD.mpFontEntry)->SetServerFont( this );
     664             : 
     665         974 :     if( rFSD.mnOrientation != 0 )
     666             :     {
     667           7 :         const double dRad = rFSD.mnOrientation * ( F_2PI / 3600.0 );
     668           7 :         mnCos = static_cast<long>( 0x10000 * cos( dRad ) + 0.5 );
     669           7 :         mnSin = static_cast<long>( 0x10000 * sin( dRad ) + 0.5 );
     670             :     }
     671             : 
     672         974 :     maFaceFT = pFI->GetFaceFT();
     673             : 
     674         974 :     if( !maFaceFT )
     675           0 :         return;
     676             : 
     677             :     // set the pixel size of the font instance
     678         974 :     mnWidth = rFSD.mnWidth;
     679         974 :     if( !mnWidth )
     680         672 :         mnWidth = rFSD.mnHeight;
     681         974 :     mfStretch = (double)mnWidth / rFSD.mnHeight;
     682             :     // sanity check (e.g. #i66394#, #i66244#, #66537#)
     683         974 :     if( (mnWidth < 0) || (mfStretch > +64.0) || (mfStretch < -64.0) )
     684           0 :         return;
     685             : 
     686             :     // perf: use maSizeFT if available
     687         974 :     if( bEnableSizeFT )
     688             :     {
     689         974 :         pFTNewSize( maFaceFT, &maSizeFT );
     690         974 :         pFTActivateSize( maSizeFT );
     691             :     }
     692         974 :     FT_Error rc = FT_Set_Pixel_Sizes( maFaceFT, mnWidth, rFSD.mnHeight );
     693         974 :     if( rc != FT_Err_Ok )
     694           0 :         return;
     695             : 
     696             :     // prepare for font encodings other than unicode or symbol
     697         974 :     FT_Encoding eEncoding = FT_ENCODING_UNICODE;
     698         974 :     if( mpFontInfo->IsSymbolFont() )
     699             :     {
     700             : #if (FTVERSION < 2000)
     701             :         eEncoding = FT_ENCODING_NONE;
     702             : #else
     703         281 :         if( FT_IS_SFNT( maFaceFT ) )
     704           0 :             eEncoding = ft_encoding_symbol;
     705             :         else
     706         281 :             eEncoding = FT_ENCODING_ADOBE_CUSTOM; // freetype wants this for PS symbol fonts
     707             : #endif
     708             :     }
     709         974 :     rc = FT_Select_Charmap( maFaceFT, eEncoding );
     710             :     // no standard encoding applies => we need an encoding converter
     711         974 :     if( rc != FT_Err_Ok )
     712             :     {
     713           0 :         rtl_TextEncoding eRecodeFrom = RTL_TEXTENCODING_UNICODE;
     714           0 :         for( int i = maFaceFT->num_charmaps; --i >= 0; )
     715             :         {
     716           0 :             const FT_CharMap aCM = maFaceFT->charmaps[i];
     717           0 :             if( aCM->platform_id == TT_PLATFORM_MICROSOFT )
     718             :             {
     719           0 :                 switch( aCM->encoding_id )
     720             :                 {
     721             :                     case TT_MS_ID_SJIS:
     722           0 :                         eEncoding = FT_ENCODING_SJIS;
     723           0 :                         eRecodeFrom = RTL_TEXTENCODING_SHIFT_JIS;
     724           0 :                         break;
     725             :                     case TT_MS_ID_GB2312:
     726           0 :                         eEncoding = FT_ENCODING_GB2312;
     727           0 :                         eRecodeFrom = RTL_TEXTENCODING_GB_2312;
     728           0 :                         break;
     729             :                     case TT_MS_ID_BIG_5:
     730           0 :                         eEncoding = FT_ENCODING_BIG5;
     731           0 :                         eRecodeFrom = RTL_TEXTENCODING_BIG5;
     732           0 :                         break;
     733             :                     case TT_MS_ID_WANSUNG:
     734           0 :                         eEncoding = FT_ENCODING_WANSUNG;
     735           0 :                         eRecodeFrom = RTL_TEXTENCODING_MS_949;
     736           0 :                         break;
     737             :                     case TT_MS_ID_JOHAB:
     738           0 :                         eEncoding = FT_ENCODING_JOHAB;
     739           0 :                         eRecodeFrom = RTL_TEXTENCODING_MS_1361;
     740           0 :                         break;
     741             :                 }
     742             :             }
     743           0 :             else if( aCM->platform_id == TT_PLATFORM_MACINTOSH )
     744             :             {
     745           0 :                 switch( aCM->encoding_id )
     746             :                 {
     747             :                     case TT_MAC_ID_ROMAN:
     748           0 :                         eEncoding = FT_ENCODING_APPLE_ROMAN;
     749           0 :                         eRecodeFrom = RTL_TEXTENCODING_UNICODE; // TODO: use better match
     750           0 :                         break;
     751             :                     // TODO: add other encodings when Mac-only
     752             :                     //       non-unicode fonts show up
     753             :                 }
     754             :             }
     755           0 :             else if( aCM->platform_id == TT_PLATFORM_ADOBE )
     756             :             {
     757           0 :                 switch( aCM->encoding_id )
     758             :                 {
     759             : #ifdef TT_ADOBE_ID_LATIN1
     760             :                     case TT_ADOBE_ID_LATIN1:   // better unicode than nothing
     761             :                         eEncoding = FT_ENCODING_ADOBE_LATIN_1;
     762             :                         eRecodeFrom = RTL_TEXTENCODING_ISO_8859_1;
     763             :                         break;
     764             : #endif // TT_ADOBE_ID_LATIN1
     765             :                     case TT_ADOBE_ID_STANDARD:   // better unicode than nothing
     766           0 :                         eEncoding = FT_ENCODING_ADOBE_STANDARD;
     767           0 :                         eRecodeFrom = RTL_TEXTENCODING_UNICODE; // TODO: use better match
     768           0 :                         break;
     769             :                 }
     770             :             }
     771             :         }
     772             : 
     773           0 :         if( FT_Err_Ok != FT_Select_Charmap( maFaceFT, eEncoding ) )
     774           0 :             return;
     775             : 
     776           0 :         if( eRecodeFrom != RTL_TEXTENCODING_UNICODE )
     777           0 :             maRecodeConverter = rtl_createUnicodeToTextConverter( eRecodeFrom );
     778             :     }
     779             : 
     780         974 :     mbFaceOk = true;
     781             : 
     782         974 :     ApplyGSUB( rFSD );
     783             : 
     784             :     // TODO: query GASP table for load flags
     785         974 :     mnLoadFlags = FT_LOAD_DEFAULT;
     786             : #if 1 // #i97326# cairo sometimes uses FT_Set_Transform() on our FT_FACE
     787             :     // we are not using FT_Set_Transform() yet, so just ignore it for now
     788         974 :     mnLoadFlags |= FT_LOAD_IGNORE_TRANSFORM;
     789             : #endif
     790             : 
     791         974 :     mbArtItalic = (rFSD.meItalic != ITALIC_NONE && pFI->GetFontAttributes().GetSlant() == ITALIC_NONE);
     792         974 :     mbArtBold = (rFSD.meWeight > WEIGHT_MEDIUM && pFI->GetFontAttributes().GetWeight() <= WEIGHT_MEDIUM);
     793         974 :     mbUseGamma = false;
     794         974 :     if( mbArtBold )
     795             :     {
     796             :         //static const int TT_CODEPAGE_RANGE_874  = (1L << 16); // Thai
     797             :         //static const int TT_CODEPAGE_RANGE_932  = (1L << 17); // JIS/Japan
     798             :         //static const int TT_CODEPAGE_RANGE_936  = (1L << 18); // Chinese: Simplified
     799             :         //static const int TT_CODEPAGE_RANGE_949  = (1L << 19); // Korean Wansung
     800             :         //static const int TT_CODEPAGE_RANGE_950  = (1L << 20); // Chinese: Traditional
     801             :         //static const int TT_CODEPAGE_RANGE_1361 = (1L << 21); // Korean Johab
     802             :         static const int TT_CODEPAGE_RANGES1_CJKT = 0x3F0000; // all of the above
     803           4 :         const TT_OS2* pOs2 = (const TT_OS2*)FT_Get_Sfnt_Table( maFaceFT, ft_sfnt_os2 );
     804           4 :         if ((pOs2) && (pOs2->ulCodePageRange1 & TT_CODEPAGE_RANGES1_CJKT )
     805             :         && rFSD.mnHeight < 20)
     806           0 :         mbUseGamma = true;
     807             :     }
     808             : 
     809         974 :     if( ((mnCos != 0) && (mnSin != 0)) || (mnPrioEmbedded <= 0) )
     810           0 :         mnLoadFlags |= FT_LOAD_NO_BITMAP;
     811             : }
     812             : 
     813           0 : void ServerFont::SetFontOptions( boost::shared_ptr<ImplFontOptions> pFontOptions)
     814             : {
     815           0 :     mpFontOptions = pFontOptions;
     816             : 
     817           0 :     if (!mpFontOptions)
     818           0 :         return;
     819             : 
     820           0 :     FontAutoHint eHint = mpFontOptions->GetUseAutoHint();
     821           0 :     if( eHint == AUTOHINT_DONTKNOW )
     822           0 :         eHint = mbUseGamma ? AUTOHINT_TRUE : AUTOHINT_FALSE;
     823             : 
     824           0 :     if( eHint == AUTOHINT_TRUE )
     825           0 :         mnLoadFlags |= FT_LOAD_FORCE_AUTOHINT;
     826             : 
     827           0 :     if( (mnSin != 0) && (mnCos != 0) ) // hinting for 0/90/180/270 degrees only
     828           0 :         mnLoadFlags |= FT_LOAD_NO_HINTING;
     829           0 :     mnLoadFlags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH; //#88334#
     830             : 
     831           0 :     if( mpFontOptions->DontUseAntiAlias() )
     832           0 :       mnPrioAntiAlias = 0;
     833           0 :     if( mpFontOptions->DontUseEmbeddedBitmaps() )
     834           0 :       mnPrioEmbedded = 0;
     835           0 :     if( mpFontOptions->DontUseHinting() )
     836           0 :       mnPrioAutoHint = 0;
     837             : 
     838             : #if (FTVERSION >= 2005) || defined(TT_CONFIG_OPTION_BYTECODE_INTERPRETER)
     839           0 :     if( mnPrioAutoHint <= 0 )
     840             : #endif
     841           0 :         mnLoadFlags |= FT_LOAD_NO_HINTING;
     842             : 
     843             : #if defined(FT_LOAD_TARGET_LIGHT) && defined(FT_LOAD_TARGET_NORMAL)
     844           0 :     if( !(mnLoadFlags & FT_LOAD_NO_HINTING) && (nFTVERSION >= 2103))
     845             :     {
     846           0 :        mnLoadFlags |= FT_LOAD_TARGET_NORMAL;
     847           0 :        switch( mpFontOptions->GetHintStyle() )
     848             :        {
     849             :            case HINT_NONE:
     850           0 :                 mnLoadFlags |= FT_LOAD_NO_HINTING;
     851           0 :                 break;
     852             :            case HINT_SLIGHT:
     853           0 :                 mnLoadFlags |= FT_LOAD_TARGET_LIGHT;
     854           0 :                 break;
     855             :            case HINT_MEDIUM:
     856           0 :                 break;
     857             :            case HINT_FULL:
     858             :            default:
     859           0 :                 break;
     860             :        }
     861             :     }
     862             : #endif
     863             : 
     864           0 :     if( mnPrioEmbedded <= 0 )
     865           0 :         mnLoadFlags |= FT_LOAD_NO_BITMAP;
     866             : }
     867             : 
     868           0 : boost::shared_ptr<ImplFontOptions> ServerFont::GetFontOptions() const
     869             : {
     870           0 :     return mpFontOptions;
     871             : }
     872             : 
     873           0 : const ::rtl::OString* ServerFont::GetFontFileName() const
     874             : {
     875           0 :     return mpFontInfo->GetFontFileName();
     876             : }
     877             : 
     878       19772 : bool ServerFont::TestFont() const
     879             : {
     880       19772 :     return mbFaceOk;
     881             : }
     882             : 
     883             : // -----------------------------------------------------------------------
     884             : 
     885        2922 : ServerFont::~ServerFont()
     886             : {
     887         974 :     if( mpLayoutEngine )
     888         872 :         delete mpLayoutEngine;
     889             : 
     890         974 :     if( maRecodeConverter )
     891           0 :         rtl_destroyUnicodeToTextConverter( maRecodeConverter );
     892             : 
     893         974 :     if( maSizeFT )
     894         974 :         pFTDoneSize( maSizeFT );
     895             : 
     896         974 :     mpFontInfo->ReleaseFaceFT( maFaceFT );
     897             : 
     898         974 :     ReleaseFromGarbageCollect();
     899        1948 : }
     900             : 
     901             :  // -----------------------------------------------------------------------
     902             : 
     903      897140 : int ServerFont::GetEmUnits() const
     904             : {
     905      897140 :     return maFaceFT->units_per_EM;
     906             : }
     907             : 
     908             : // -----------------------------------------------------------------------
     909             : 
     910        4720 : void ServerFont::FetchFontMetric( ImplFontMetricData& rTo, long& rFactor ) const
     911             : {
     912        4720 :     const int UNDETERMINED = 0xFEED;
     913             :     static int nUseNewLineHeight = UNDETERMINED;
     914        4720 :     if (nUseNewLineHeight == UNDETERMINED)
     915             :     {
     916          19 :         osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex());
     917          19 :         if (nUseNewLineHeight == UNDETERMINED)
     918             :         {
     919          19 :             const char* pEnv = getenv( "SAL_USE_NEW_LINEHEIGHT");
     920          19 :             nUseNewLineHeight = (pEnv ? atoi(pEnv) : 0);
     921          19 :         }
     922             :     }
     923             : 
     924        4720 :     static_cast<ImplFontAttributes&>(rTo) = mpFontInfo->GetFontAttributes();
     925             : 
     926        4720 :     rTo.mbScalableFont  = true;
     927        4720 :     rTo.mbDevice        = true;
     928        4720 :     rTo.mbKernableFont  = (FT_HAS_KERNING( maFaceFT ) != 0) || mpFontInfo->HasExtraKerning();
     929        4720 :     rTo.mnOrientation = GetFontSelData().mnOrientation;
     930             : 
     931             :     //Always consider [star]symbol as symbol fonts
     932        9440 :     if (
     933        4720 :          (rTo.GetFamilyName().EqualsAscii("OpenSymbol")) ||
     934        4720 :          (rTo.GetFamilyName().EqualsAscii("StarSymbol"))
     935             :        )
     936             :     {
     937           0 :         rTo.mbSymbolFlag = true;
     938             :     }
     939             : 
     940        4720 :     if( maSizeFT )
     941        4720 :         pFTActivateSize( maSizeFT );
     942             : 
     943        4720 :     rFactor = 0x100;
     944             : 
     945        4720 :     rTo.mnWidth             = mnWidth;
     946             : 
     947        4720 :     const FT_Size_Metrics& rMetrics = maFaceFT->size->metrics;
     948        4720 :     rTo.mnAscent            = (+rMetrics.ascender + 32) >> 6;
     949        4720 :     rTo.mnDescent           = (-rMetrics.descender + 32) >> 6;
     950        4720 :     if (nUseNewLineHeight)
     951             :     {
     952           0 :         rTo.mnExtLeading    = ((rMetrics.height + 32) >> 6) - (rTo.mnAscent + rTo.mnDescent);
     953           0 :         rTo.mnIntLeading    = (rTo.mnAscent + rTo.mnDescent) - ((maFaceFT->units_per_EM + 32) >> 6);
     954             :     }
     955             :     else
     956             :     {
     957        4720 :         rTo.mnIntLeading    = ((rMetrics.height + 32) >> 6) - (rTo.mnAscent + rTo.mnDescent);
     958             :     }
     959        4720 :     rTo.mnSlant             = 0;
     960             : 
     961        4720 :     const TT_OS2* pOS2 = (const TT_OS2*)FT_Get_Sfnt_Table( maFaceFT, ft_sfnt_os2 );
     962        4720 :     if( pOS2 && (pOS2->version != 0xFFFF) )
     963             :     {
     964             :         // map the panose info from the OS2 table to their VCL counterparts
     965        1001 :         switch( pOS2->panose[0] )
     966             :         {
     967           0 :             case 1: rTo.meFamily = FAMILY_ROMAN; break;
     968        1001 :             case 2: rTo.meFamily = FAMILY_SWISS; break;
     969           0 :             case 3: rTo.meFamily = FAMILY_MODERN; break;
     970           0 :             case 4: rTo.meFamily = FAMILY_SCRIPT; break;
     971           0 :             case 5: rTo.meFamily = FAMILY_DECORATIVE; break;
     972             :             // TODO: is it reasonable to override the attribute with DONTKNOW?
     973             :             case 0: // fall through
     974           0 :             default: rTo.meFamilyType = FAMILY_DONTKNOW; break;
     975             :         }
     976             : 
     977        1001 :         switch( pOS2->panose[3] )
     978             :         {
     979             :             case 2: // fall through
     980             :             case 3: // fall through
     981             :             case 4: // fall through
     982             :             case 5: // fall through
     983             :             case 6: // fall through
     984             :             case 7: // fall through
     985         988 :             case 8: rTo.mePitch = PITCH_VARIABLE; break;
     986           4 :             case 9: rTo.mePitch = PITCH_FIXED; break;
     987             :             // TODO: is it reasonable to override the attribute with DONTKNOW?
     988             :             case 0: // fall through
     989             :             case 1: // fall through
     990           9 :             default: rTo.mePitch = PITCH_DONTKNOW; break;
     991             :         }
     992             : 
     993        1001 :         const double fScale = (double)GetFontSelData().mnHeight / maFaceFT->units_per_EM;
     994        1001 :         if (nUseNewLineHeight)
     995             :         {
     996           0 :             if( pOS2->sTypoAscender || pOS2->sTypoDescender )
     997             :             {
     998           0 :                 rTo.mnAscent     = (long)(  pOS2->sTypoAscender  * fScale + 0.5 );
     999           0 :                 rTo.mnDescent    = (long)( -pOS2->sTypoDescender * fScale + 0.5 );
    1000           0 :                 rTo.mnExtLeading = (long)(  pOS2->sTypoLineGap   * fScale + 0.5 );
    1001           0 :                 rTo.mnIntLeading = (long)( (pOS2->sTypoAscender - pOS2->sTypoDescender - maFaceFT->units_per_EM) * fScale + 0.5 );
    1002             :             }
    1003             :         }
    1004             :         else
    1005             :         {
    1006             :             // #108862# sanity check, some fonts treat descent as signed !!!
    1007        1001 :             int nDescent = pOS2->usWinDescent;
    1008        1001 :             if( nDescent > 5*maFaceFT->units_per_EM )
    1009           0 :                 nDescent = (short)pOS2->usWinDescent; // interpret it as signed!
    1010             : 
    1011        1001 :             if( pOS2->usWinAscent || pOS2->usWinDescent ) // #i30551#
    1012             :             {
    1013        1001 :                 rTo.mnAscent     = (long)( +pOS2->usWinAscent * fScale + 0.5 );
    1014        1001 :                 rTo.mnDescent    = (long)( +nDescent * fScale + 0.5 );
    1015        1001 :                 rTo.mnIntLeading = (long)( (+pOS2->usWinAscent + pOS2->usWinDescent - maFaceFT->units_per_EM) * fScale + 0.5 );
    1016             :             }
    1017        1001 :             rTo.mnExtLeading = 0;
    1018        1001 :             const TT_HoriHeader* pHHEA = (const TT_HoriHeader*)FT_Get_Sfnt_Table( maFaceFT, ft_sfnt_hhea );
    1019        1001 :             if( (pHHEA != NULL) && (pOS2->usWinAscent || pOS2->usWinDescent) )
    1020             :             {
    1021        1001 :                 int nExtLeading = pHHEA->Line_Gap;
    1022        1001 :                 nExtLeading -= (pOS2->usWinAscent + pOS2->usWinDescent);
    1023        1001 :                 nExtLeading += (pHHEA->Ascender - pHHEA->Descender);
    1024        1001 :                 if( nExtLeading > 0 )
    1025         814 :                     rTo.mnExtLeading = (long)(nExtLeading * fScale + 0.5);
    1026             :             }
    1027             : 
    1028             :             // Check for CJK capabilities of the current font
    1029             :             // #107888# workaround for Asian...
    1030             :             // TODO: remove when ExtLeading fully implemented
    1031        1001 :             sal_Bool bCJKCapable = ((pOS2->ulUnicodeRange2 & 0x2DF00000) != 0);
    1032             : 
    1033        1001 :             if ( bCJKCapable && (pOS2->usWinAscent || pOS2->usWinDescent) )
    1034             :             {
    1035           4 :                 rTo.mnIntLeading += rTo.mnExtLeading;
    1036             : 
    1037             :                 // #109280# The line height for Asian fonts is too small.
    1038             :                 // Therefore we add half of the external leading to the
    1039             :                 // ascent, the other half is added to the descent.
    1040           4 :                 const long nHalfTmpExtLeading = rTo.mnExtLeading / 2;
    1041           4 :                 const long nOtherHalfTmpExtLeading = rTo.mnExtLeading - nHalfTmpExtLeading;
    1042             : 
    1043             :                 // #110641# external leading for Asian fonts.
    1044             :                 // The factor 0.3 has been verified during experiments.
    1045           4 :                 const long nCJKExtLeading = (long)(0.30 * (rTo.mnAscent + rTo.mnDescent));
    1046             : 
    1047           4 :                 if ( nCJKExtLeading > rTo.mnExtLeading )
    1048           4 :                     rTo.mnExtLeading = nCJKExtLeading - rTo.mnExtLeading;
    1049             :                 else
    1050           0 :                     rTo.mnExtLeading = 0;
    1051             : 
    1052           4 :                 rTo.mnAscent += nHalfTmpExtLeading;
    1053           4 :                 rTo.mnDescent += nOtherHalfTmpExtLeading;
    1054             :             }
    1055             :         }
    1056             :     }
    1057             : 
    1058             :     // initialize kashida width
    1059             :     // TODO: what if there are different versions of this glyph available
    1060        4720 :     const int nKashidaGlyphId = GetRawGlyphIndex( 0x0640 );
    1061        4720 :     if( nKashidaGlyphId )
    1062             :     {
    1063         182 :         GlyphData aGlyphData;
    1064         182 :         InitGlyphData( nKashidaGlyphId, aGlyphData );
    1065         182 :         rTo.mnMinKashida = aGlyphData.GetMetric().GetCharWidth();
    1066             :     }
    1067        4720 : }
    1068             : 
    1069             : // -----------------------------------------------------------------------
    1070             : 
    1071       23840 : static inline void SplitGlyphFlags( const ServerFont& rFont, int& nGlyphIndex, int& nGlyphFlags )
    1072             : {
    1073       23840 :     nGlyphFlags = nGlyphIndex & GF_FLAGMASK;
    1074       23840 :     nGlyphIndex &= GF_IDXMASK;
    1075             : 
    1076       23840 :     if( nGlyphIndex & GF_ISCHAR )
    1077           0 :         nGlyphIndex = rFont.GetRawGlyphIndex( nGlyphIndex );
    1078       23840 : }
    1079             : 
    1080             : // -----------------------------------------------------------------------
    1081             : 
    1082       23282 : int ServerFont::ApplyGlyphTransform( int nGlyphFlags,
    1083             :     FT_Glyph pGlyphFT, bool bForBitmapProcessing ) const
    1084             : {
    1085       23282 :     int nAngle = GetFontSelData().mnOrientation;
    1086             :     // shortcut most common case
    1087       23282 :     if( !nAngle && !nGlyphFlags )
    1088       21080 :         return nAngle;
    1089             : 
    1090        2202 :     const FT_Size_Metrics& rMetrics = maFaceFT->size->metrics;
    1091             :     FT_Vector aVector;
    1092             :     FT_Matrix aMatrix;
    1093             : 
    1094        2202 :     bool bStretched = false;
    1095             : 
    1096        2202 :     switch( nGlyphFlags & GF_ROTMASK )
    1097             :     {
    1098             :     default:    // straight
    1099        2202 :         aVector.x = 0;
    1100        2202 :         aVector.y = 0;
    1101        2202 :         aMatrix.xx = +mnCos;
    1102        2202 :         aMatrix.yy = +mnCos;
    1103        2202 :         aMatrix.xy = -mnSin;
    1104        2202 :         aMatrix.yx = +mnSin;
    1105        2202 :         break;
    1106             :     case GF_ROTL:    // left
    1107           0 :         nAngle += 900;
    1108           0 :         bStretched = (mfStretch != 1.0);
    1109           0 :         aVector.x  = (FT_Pos)(+rMetrics.descender * mfStretch);
    1110           0 :         aVector.y  = -rMetrics.ascender;
    1111           0 :         aMatrix.xx = (FT_Pos)(-mnSin / mfStretch);
    1112           0 :         aMatrix.yy = (FT_Pos)(-mnSin * mfStretch);
    1113           0 :         aMatrix.xy = (FT_Pos)(-mnCos * mfStretch);
    1114           0 :         aMatrix.yx = (FT_Pos)(+mnCos / mfStretch);
    1115           0 :         break;
    1116             :     case GF_ROTR:    // right
    1117           0 :         nAngle -= 900;
    1118           0 :         bStretched = (mfStretch != 1.0);
    1119           0 :         aVector.x = -maFaceFT->glyph->metrics.horiAdvance;
    1120           0 :         aVector.x += (FT_Pos)(rMetrics.descender * mnSin/65536.0);
    1121           0 :         aVector.y  = (FT_Pos)(-rMetrics.descender * mfStretch * mnCos/65536.0);
    1122           0 :         aMatrix.xx = (FT_Pos)(+mnSin / mfStretch);
    1123           0 :         aMatrix.yy = (FT_Pos)(+mnSin * mfStretch);
    1124           0 :         aMatrix.xy = (FT_Pos)(+mnCos * mfStretch);
    1125           0 :         aMatrix.yx = (FT_Pos)(-mnCos / mfStretch);
    1126           0 :         break;
    1127             :     }
    1128             : 
    1129        4404 :     while( nAngle < 0 )
    1130           0 :         nAngle += 3600;
    1131             : 
    1132        2202 :     if( pGlyphFT->format != FT_GLYPH_FORMAT_BITMAP )
    1133             :     {
    1134        2202 :         FT_Glyph_Transform( pGlyphFT, NULL, &aVector );
    1135             : 
    1136             :         // orthogonal transforms are better handled by bitmap operations
    1137        2202 :         if( bStretched || (bForBitmapProcessing && (nAngle % 900) != 0) )
    1138             :         {
    1139             :             // workaround for compatibility with older FT versions
    1140           0 :             if( nFTVERSION < 2102 )
    1141             :             {
    1142           0 :                 FT_Fixed t = aMatrix.xy;
    1143           0 :                 aMatrix.xy = aMatrix.yx;
    1144           0 :                 aMatrix.yx = t;
    1145             :             }
    1146             : 
    1147             :             // apply non-orthogonal or stretch transformations
    1148           0 :             FT_Glyph_Transform( pGlyphFT, &aMatrix, NULL );
    1149           0 :             nAngle = 0;
    1150             :         }
    1151             :     }
    1152             :     else
    1153             :     {
    1154             :         // FT<=2005 ignores transforms for bitmaps, so do it manually
    1155           0 :         FT_BitmapGlyph pBmpGlyphFT = reinterpret_cast<FT_BitmapGlyph>(pGlyphFT);
    1156           0 :         pBmpGlyphFT->left += (aVector.x + 32) >> 6;
    1157           0 :         pBmpGlyphFT->top  += (aVector.y + 32) >> 6;
    1158             :     }
    1159             : 
    1160        2202 :     return nAngle;
    1161             : }
    1162             : 
    1163             : // -----------------------------------------------------------------------
    1164             : 
    1165      334560 : int ServerFont::GetRawGlyphIndex( sal_UCS4 aChar ) const
    1166             : {
    1167      334560 :     if( mpFontInfo->IsSymbolFont() )
    1168             :     {
    1169       65774 :         if( !FT_IS_SFNT( maFaceFT ) )
    1170             :         {
    1171       65774 :             if( (aChar & 0xFF00) == 0xF000 )
    1172         148 :                 aChar &= 0xFF;    // PS font symbol mapping
    1173       65626 :             else if( aChar > 0xFF )
    1174       29338 :                 return 0;
    1175             :         }
    1176             :     }
    1177             : 
    1178             :     // if needed recode from unicode to font encoding
    1179      305222 :     if( maRecodeConverter )
    1180             :     {
    1181             :         sal_Char aTempArray[8];
    1182             :         sal_Size nTempSize;
    1183             :         sal_uInt32 nCvtInfo;
    1184             : 
    1185             :         // assume that modern UCS4 fonts have unicode CMAPs
    1186             :     // => no encoding remapping to unicode is needed
    1187           0 :         if( aChar > 0xFFFF )
    1188           0 :             return 0;
    1189             : 
    1190           0 :         sal_Unicode aUCS2Char = static_cast<sal_Unicode>(aChar);
    1191           0 :         rtl_UnicodeToTextContext aContext = rtl_createUnicodeToTextContext( maRecodeConverter );
    1192             :         int nChars = rtl_convertUnicodeToText( maRecodeConverter, aContext,
    1193             :             &aUCS2Char, 1, aTempArray, sizeof(aTempArray),
    1194             :             RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK
    1195             :             | RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK,
    1196           0 :             &nCvtInfo, &nTempSize );
    1197           0 :         rtl_destroyUnicodeToTextContext( maRecodeConverter, aContext );
    1198             : 
    1199           0 :         aChar = 0;
    1200           0 :         for( int i = 0; i < nChars; ++i )
    1201           0 :             aChar = aChar*256 + (aTempArray[i] & 0xFF);
    1202             :     }
    1203             : 
    1204             :     // cache glyph indexes in font info to share between different sizes
    1205      305222 :     int nGlyphIndex = mpFontInfo->GetGlyphIndex( aChar );
    1206      305222 :     if( nGlyphIndex < 0 )
    1207             :     {
    1208        2060 :         nGlyphIndex = FT_Get_Char_Index( maFaceFT, aChar );
    1209        2060 :         if( !nGlyphIndex)
    1210             :         {
    1211             :             // check if symbol aliasing helps
    1212         112 :             if( (aChar <= 0x00FF) && mpFontInfo->IsSymbolFont() )
    1213           0 :                 nGlyphIndex = FT_Get_Char_Index( maFaceFT, aChar | 0xF000 );
    1214             :         }
    1215        2060 :         mpFontInfo->CacheGlyphIndex( aChar, nGlyphIndex );
    1216             :     }
    1217             : 
    1218      305222 :     return nGlyphIndex;
    1219             : }
    1220             : 
    1221             : // -----------------------------------------------------------------------
    1222             : 
    1223      225346 : int ServerFont::FixupGlyphIndex( int nGlyphIndex, sal_UCS4 aChar ) const
    1224             : {
    1225      225346 :     int nGlyphFlags = GF_NONE;
    1226             : 
    1227             :     // do glyph substitution if necessary
    1228             :     // CJK vertical writing needs special treatment
    1229      225346 :     if( GetFontSelData().mbVertical )
    1230             :     {
    1231             :         // TODO: rethink when GSUB is used for non-vertical case
    1232           0 :         GlyphSubstitution::const_iterator it = maGlyphSubstitution.find( nGlyphIndex );
    1233           0 :         if( it == maGlyphSubstitution.end() )
    1234             :         {
    1235           0 :             int nTemp = GetVerticalChar( aChar );
    1236           0 :             if( nTemp ) // is substitution possible
    1237           0 :                 nTemp = GetRawGlyphIndex( nTemp );
    1238           0 :             if( nTemp ) // substitute manually if sensible
    1239           0 :                 nGlyphIndex = nTemp | (GF_GSUB | GF_ROTL);
    1240             :             else
    1241           0 :                 nGlyphFlags |= GetVerticalFlags( aChar );
    1242             :         }
    1243             :         else
    1244             :         {
    1245             :             // for vertical GSUB also compensate for nOrientation=2700
    1246           0 :             nGlyphIndex = (*it).second;
    1247           0 :             nGlyphFlags |= GF_GSUB | GF_ROTL;
    1248             :         }
    1249             :     }
    1250             : 
    1251      225346 :     if( nGlyphIndex != 0 )
    1252      221927 :         nGlyphIndex |= nGlyphFlags;
    1253             : 
    1254      225346 :     return nGlyphIndex;
    1255             : }
    1256             : 
    1257             : 
    1258             : // -----------------------------------------------------------------------
    1259             : 
    1260           0 : int ServerFont::GetGlyphIndex( sal_UCS4 aChar ) const
    1261             : {
    1262           0 :     int nGlyphIndex = GetRawGlyphIndex( aChar );
    1263           0 :     nGlyphIndex = FixupGlyphIndex( nGlyphIndex, aChar );
    1264           0 :     return nGlyphIndex;
    1265             : }
    1266             : 
    1267             : // -----------------------------------------------------------------------
    1268             : 
    1269        6234 : static int lcl_GetCharWidth( FT_FaceRec_* pFaceFT, double fStretch, int nGlyphFlags )
    1270             : {
    1271        6234 :     int nCharWidth = pFaceFT->glyph->metrics.horiAdvance;
    1272             : 
    1273        6234 :     if( nGlyphFlags & GF_ROTMASK )  // for bVertical rotated glyphs
    1274             :     {
    1275           0 :         const FT_Size_Metrics& rMetrics = pFaceFT->size->metrics;
    1276             : #if (FTVERSION < 2000)
    1277             :         nCharWidth = (int)((rMetrics.height - rMetrics.descender) * fStretch);
    1278             : #else
    1279           0 :         nCharWidth = (int)((rMetrics.height + rMetrics.descender) * fStretch);
    1280             : #endif
    1281             :     }
    1282             : 
    1283        6234 :     return (nCharWidth + 32) >> 6;
    1284             : }
    1285             : 
    1286             : // -----------------------------------------------------------------------
    1287             : 
    1288        6572 : void ServerFont::InitGlyphData( int nGlyphIndex, GlyphData& rGD ) const
    1289             : {
    1290        6572 :     if( maSizeFT )
    1291        6572 :         pFTActivateSize( maSizeFT );
    1292             : 
    1293             :     int nGlyphFlags;
    1294        6572 :     SplitGlyphFlags( *this, nGlyphIndex, nGlyphFlags );
    1295             : 
    1296        6572 :     int nLoadFlags = mnLoadFlags;
    1297             : 
    1298             : //  if( mbArtItalic )
    1299             : //      nLoadFlags |= FT_LOAD_NO_BITMAP;
    1300             : 
    1301        6572 :     FT_Error rc = -1;
    1302             : #if (FTVERSION <= 2008)
    1303             :     // #88364# freetype<=2005 prefers autohinting to embedded bitmaps
    1304             :     // => first we have to try without hinting
    1305             :     if( (nLoadFlags & (FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP)) == 0 )
    1306             :     {
    1307             :         rc = FT_Load_Glyph( maFaceFT, nGlyphIndex, nLoadFlags|FT_LOAD_NO_HINTING );
    1308             :         if( (rc==FT_Err_Ok) && (maFaceFT->glyph->format!=FT_GLYPH_FORMAT_BITMAP) )
    1309             :             rc = -1; // mark as "loading embedded bitmap" was unsuccessful
    1310             :         nLoadFlags |= FT_LOAD_NO_BITMAP;
    1311             :     }
    1312             : 
    1313             :     if( rc != FT_Err_Ok )
    1314             : #endif
    1315        6572 :         rc = FT_Load_Glyph( maFaceFT, nGlyphIndex, nLoadFlags );
    1316             : 
    1317        6572 :     if( rc != FT_Err_Ok )
    1318             :     {
    1319             :         // we get here e.g. when a PS font lacks the default glyph
    1320         253 :         rGD.SetCharWidth( 0 );
    1321         253 :         rGD.SetDelta( 0, 0 );
    1322         253 :         rGD.SetOffset( 0, 0 );
    1323         253 :         rGD.SetSize( Size( 0, 0 ) );
    1324        6572 :         return;
    1325             :     }
    1326             : 
    1327        6319 :     const bool bOriginallyZeroWidth = (maFaceFT->glyph->metrics.horiAdvance == 0);
    1328        6319 :     if( mbArtBold && pFTEmbolden )
    1329           5 :         (*pFTEmbolden)( maFaceFT->glyph );
    1330             : 
    1331        6319 :     const int nCharWidth = bOriginallyZeroWidth ? 0 : lcl_GetCharWidth( maFaceFT, mfStretch, nGlyphFlags );
    1332        6319 :     rGD.SetCharWidth( nCharWidth );
    1333             : 
    1334             :     FT_Glyph pGlyphFT;
    1335        6319 :     rc = FT_Get_Glyph( maFaceFT->glyph, &pGlyphFT );
    1336             : 
    1337        6319 :     ApplyGlyphTransform( nGlyphFlags, pGlyphFT, false );
    1338        6319 :     if( mbArtBold && pFTEmbolden && (nFTVERSION < 2200) ) // #i71094# workaround staircase bug
    1339           0 :         pGlyphFT->advance.y = 0;
    1340        6319 :     rGD.SetDelta( (pGlyphFT->advance.x + 0x8000) >> 16, -((pGlyphFT->advance.y + 0x8000) >> 16) );
    1341             : 
    1342             :     FT_BBox aBbox;
    1343        6319 :     FT_Glyph_Get_CBox( pGlyphFT, FT_GLYPH_BBOX_PIXELS, &aBbox );
    1344        6319 :     if( aBbox.yMin > aBbox.yMax )   // circumvent freetype bug
    1345             :     {
    1346           0 :         int t=aBbox.yMin; aBbox.yMin=aBbox.yMax, aBbox.yMax=t;
    1347             :     }
    1348             : 
    1349        6319 :     rGD.SetOffset( aBbox.xMin, -aBbox.yMax );
    1350        6319 :     rGD.SetSize( Size( (aBbox.xMax-aBbox.xMin+1), (aBbox.yMax-aBbox.yMin) ) );
    1351             : 
    1352        6319 :     FT_Done_Glyph( pGlyphFT );
    1353             : }
    1354             : 
    1355             : // -----------------------------------------------------------------------
    1356             : 
    1357           0 : bool ServerFont::GetAntialiasAdvice( void ) const
    1358             : {
    1359           0 :     if( GetFontSelData().mbNonAntialiased || (mnPrioAntiAlias<=0) )
    1360           0 :         return false;
    1361           0 :     bool bAdviseAA = true;
    1362             :     // TODO: also use GASP info
    1363           0 :     return bAdviseAA;
    1364             : }
    1365             : 
    1366             : // -----------------------------------------------------------------------
    1367             : 
    1368          70 : bool ServerFont::GetGlyphBitmap1( int nGlyphIndex, RawBitmap& rRawBitmap ) const
    1369             : {
    1370          70 :     if( maSizeFT )
    1371          70 :         pFTActivateSize( maSizeFT );
    1372             : 
    1373             :     int nGlyphFlags;
    1374          70 :     SplitGlyphFlags( *this, nGlyphIndex, nGlyphFlags );
    1375             : 
    1376          70 :     FT_Int nLoadFlags = mnLoadFlags;
    1377             :     // #i70930# force mono-hinting for monochrome text
    1378          70 :     if( nFTVERSION >= 2110 ) //#i71947# unless it looks worse
    1379             :     {
    1380          70 :         nLoadFlags &= ~0xF0000;
    1381          70 :         nLoadFlags |= FT_LOAD_TARGET_MONO;
    1382             :     }
    1383             : 
    1384          70 :     if( mbArtItalic )
    1385           0 :         nLoadFlags |= FT_LOAD_NO_BITMAP;
    1386             : 
    1387             : #if (FTVERSION >= 2002)
    1388             :     // for 0/90/180/270 degree fonts enable hinting even if not advisable
    1389             :     // non-hinted and non-antialiased bitmaps just look too ugly
    1390          70 :     if( (mnCos==0 || mnSin==0) && (mnPrioAutoHint > 0) )
    1391          70 :         nLoadFlags &= ~FT_LOAD_NO_HINTING;
    1392             : #endif
    1393             : 
    1394          70 :     if( mnPrioEmbedded <= mnPrioAutoHint )
    1395           0 :         nLoadFlags |= FT_LOAD_NO_BITMAP;
    1396             : 
    1397          70 :     FT_Error rc = -1;
    1398             : #if (FTVERSION <= 2008)
    1399             :     // #88364# freetype<=2005 prefers autohinting to embedded bitmaps
    1400             :     // => first we have to try without hinting
    1401             :     if( (nLoadFlags & (FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP)) == 0 )
    1402             :     {
    1403             :         rc = FT_Load_Glyph( maFaceFT, nGlyphIndex, nLoadFlags|FT_LOAD_NO_HINTING );
    1404             :         if( (rc==FT_Err_Ok) && (maFaceFT->glyph->format != FT_GLYPH_FORMAT_BITMAP) )
    1405             :             rc = -1; // mark as "loading embedded bitmap" was unsuccessful
    1406             :         nLoadFlags |= FT_LOAD_NO_BITMAP;
    1407             :     }
    1408             : 
    1409             :     if( rc != FT_Err_Ok )
    1410             : #endif
    1411          70 :         rc = FT_Load_Glyph( maFaceFT, nGlyphIndex, nLoadFlags );
    1412          70 :     if( rc != FT_Err_Ok )
    1413          35 :         return false;
    1414             : 
    1415          35 :     if( mbArtBold && pFTEmbolden )
    1416           0 :         (*pFTEmbolden)( maFaceFT->glyph );
    1417             : 
    1418             :     FT_Glyph pGlyphFT;
    1419          35 :     rc = FT_Get_Glyph( maFaceFT->glyph, &pGlyphFT );
    1420          35 :     if( rc != FT_Err_Ok )
    1421           0 :         return false;
    1422             : 
    1423          35 :     int nAngle = ApplyGlyphTransform( nGlyphFlags, pGlyphFT, true );
    1424             : 
    1425          35 :     if( mbArtItalic )
    1426             :     {
    1427             :         FT_Matrix aMatrix;
    1428           0 :         aMatrix.xx = aMatrix.yy = 0x10000L;
    1429           0 :         if( nFTVERSION >= 2102 )    // Freetype 2.1.2 API swapped xy with yx
    1430           0 :             aMatrix.xy = 0x6000L, aMatrix.yx = 0;
    1431             :         else
    1432           0 :             aMatrix.yx = 0x6000L, aMatrix.xy = 0;
    1433           0 :         FT_Glyph_Transform( pGlyphFT, &aMatrix, NULL );
    1434             :     }
    1435             : 
    1436             :     // Check for zero area bounding boxes as this crashes some versions of FT.
    1437             :     // This also provides a handy short cut as much of the code following
    1438             :     //  becomes an expensive nop when a glyph covers no pixels.
    1439             :     FT_BBox cbox;
    1440          35 :     FT_Glyph_Get_CBox(pGlyphFT, ft_glyph_bbox_unscaled, &cbox);
    1441             : 
    1442          35 :     if( (cbox.xMax - cbox.xMin) == 0 || (cbox.yMax - cbox.yMin == 0) )
    1443             :     {
    1444          34 :         nAngle = 0;
    1445          34 :         memset(&rRawBitmap, 0, sizeof rRawBitmap);
    1446          34 :         FT_Done_Glyph( pGlyphFT );
    1447          34 :         return true;
    1448             :     }
    1449             : 
    1450           1 :     if( pGlyphFT->format != FT_GLYPH_FORMAT_BITMAP )
    1451             :     {
    1452           1 :         if( pGlyphFT->format == FT_GLYPH_FORMAT_OUTLINE )
    1453           1 :             ((FT_OutlineGlyphRec*)pGlyphFT)->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
    1454             :         // #i15743# freetype API 2.1.3 changed the FT_RENDER_MODE_MONO constant
    1455           1 :         FT_Render_Mode nRenderMode = (FT_Render_Mode)((nFTVERSION<2103) ? 1 : FT_RENDER_MODE_MONO);
    1456             : 
    1457           1 :         rc = FT_Glyph_To_Bitmap( &pGlyphFT, nRenderMode, NULL, sal_True );
    1458           1 :         if( rc != FT_Err_Ok )
    1459             :         {
    1460           0 :             FT_Done_Glyph( pGlyphFT );
    1461           0 :             return false;
    1462             :         }
    1463             :     }
    1464             : 
    1465           1 :     const FT_BitmapGlyph pBmpGlyphFT = reinterpret_cast<const FT_BitmapGlyph>(pGlyphFT);
    1466             :     // NOTE: autohinting in FT<=2.0.2 miscalculates the offsets below by +-1
    1467           1 :     rRawBitmap.mnXOffset        = +pBmpGlyphFT->left;
    1468           1 :     rRawBitmap.mnYOffset        = -pBmpGlyphFT->top;
    1469             : 
    1470           1 :     const FT_Bitmap& rBitmapFT  = pBmpGlyphFT->bitmap;
    1471           1 :     rRawBitmap.mnHeight         = rBitmapFT.rows;
    1472           1 :     rRawBitmap.mnBitCount       = 1;
    1473           1 :     if( mbArtBold && !pFTEmbolden )
    1474             :     {
    1475           0 :         rRawBitmap.mnWidth = rBitmapFT.width + 1;
    1476           0 :         int nLineBytes = (rRawBitmap.mnWidth + 7) >> 3;
    1477           0 :         rRawBitmap.mnScanlineSize  = (nLineBytes > rBitmapFT.pitch) ? nLineBytes : rBitmapFT.pitch;
    1478             :     }
    1479             :     else
    1480             :     {
    1481           1 :         rRawBitmap.mnWidth          = rBitmapFT.width;
    1482           1 :         rRawBitmap.mnScanlineSize   = rBitmapFT.pitch;
    1483             :     }
    1484             : 
    1485           1 :     const sal_uLong nNeededSize = rRawBitmap.mnScanlineSize * rRawBitmap.mnHeight;
    1486             : 
    1487           1 :     if( rRawBitmap.mnAllocated < nNeededSize )
    1488             :     {
    1489           1 :         delete[] rRawBitmap.mpBits;
    1490           1 :         rRawBitmap.mnAllocated = 2*nNeededSize;
    1491           1 :         rRawBitmap.mpBits = new unsigned char[ rRawBitmap.mnAllocated ];
    1492             :     }
    1493             : 
    1494           1 :     if( !mbArtBold || pFTEmbolden )
    1495             :     {
    1496           1 :         memcpy( rRawBitmap.mpBits, rBitmapFT.buffer, nNeededSize );
    1497             :     }
    1498             :     else
    1499             :     {
    1500           0 :         memset( rRawBitmap.mpBits, 0, nNeededSize );
    1501           0 :         const unsigned char* pSrcLine = rBitmapFT.buffer;
    1502           0 :         unsigned char* pDstLine = rRawBitmap.mpBits;
    1503           0 :         for( int h = rRawBitmap.mnHeight; --h >= 0; )
    1504             :         {
    1505           0 :             memcpy( pDstLine, pSrcLine, rBitmapFT.pitch );
    1506           0 :             pDstLine += rRawBitmap.mnScanlineSize;
    1507           0 :             pSrcLine += rBitmapFT.pitch;
    1508             :         }
    1509             : 
    1510           0 :         unsigned char* p = rRawBitmap.mpBits;
    1511           0 :         for( sal_uLong y=0; y < rRawBitmap.mnHeight; y++ )
    1512             :         {
    1513           0 :             unsigned char nLastByte = 0;
    1514           0 :             for( sal_uLong x=0; x < rRawBitmap.mnScanlineSize; x++ )
    1515             :             {
    1516           0 :             unsigned char nTmp = p[x] << 7;
    1517           0 :             p[x] |= (p[x] >> 1) | nLastByte;
    1518           0 :             nLastByte = nTmp;
    1519             :             }
    1520           0 :             p += rRawBitmap.mnScanlineSize;
    1521             :         }
    1522             :     }
    1523             : 
    1524           1 :     FT_Done_Glyph( pGlyphFT );
    1525             : 
    1526             :     // special case for 0/90/180/270 degree orientation
    1527           1 :     switch( nAngle )
    1528             :     {
    1529             :         case  -900:
    1530             :         case  +900:
    1531             :         case +1800:
    1532             :         case +2700:
    1533           0 :             rRawBitmap.Rotate( nAngle );
    1534           0 :             break;
    1535             :     }
    1536             : 
    1537           1 :     return true;
    1538             : }
    1539             : 
    1540             : // -----------------------------------------------------------------------
    1541             : 
    1542       17079 : bool ServerFont::GetGlyphBitmap8( int nGlyphIndex, RawBitmap& rRawBitmap ) const
    1543             : {
    1544       17079 :     if( maSizeFT )
    1545       17079 :         pFTActivateSize( maSizeFT );
    1546             : 
    1547             :     int nGlyphFlags;
    1548       17079 :     SplitGlyphFlags( *this, nGlyphIndex, nGlyphFlags );
    1549             : 
    1550       17079 :     FT_Int nLoadFlags = mnLoadFlags;
    1551             : 
    1552       17079 :     if( mbArtItalic )
    1553           0 :         nLoadFlags |= FT_LOAD_NO_BITMAP;
    1554             : 
    1555             : #if (FTVERSION <= 2004) && !defined(TT_CONFIG_OPTION_BYTECODE_INTERPRETER)
    1556             :     // autohinting in FT<=2.0.4 makes antialiased glyphs look worse
    1557             :     nLoadFlags |= FT_LOAD_NO_HINTING;
    1558             : #else
    1559       17079 :     if( (nGlyphFlags & GF_UNHINTED) || (mnPrioAutoHint < mnPrioAntiAlias) )
    1560           0 :         nLoadFlags |= FT_LOAD_NO_HINTING;
    1561             : #endif
    1562             : 
    1563       17079 :     if( mnPrioEmbedded <= mnPrioAntiAlias )
    1564           0 :         nLoadFlags |= FT_LOAD_NO_BITMAP;
    1565             : 
    1566       17079 :     FT_Error rc = -1;
    1567             : #if (FTVERSION <= 2008)
    1568             :     // #88364# freetype<=2005 prefers autohinting to embedded bitmaps
    1569             :     // => first we have to try without hinting
    1570             :     if( (nLoadFlags & (FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP)) == 0 )
    1571             :     {
    1572             :         rc = FT_Load_Glyph( maFaceFT, nGlyphIndex, nLoadFlags|FT_LOAD_NO_HINTING );
    1573             :         if( (rc==FT_Err_Ok) && (maFaceFT->glyph->format != FT_GLYPH_FORMAT_BITMAP) )
    1574             :             rc = -1; // mark as "loading embedded bitmap" was unsuccessful
    1575             :         nLoadFlags |= FT_LOAD_NO_BITMAP;
    1576             :     }
    1577             : 
    1578             :     if( rc != FT_Err_Ok )
    1579             : #endif
    1580       17079 :         rc = FT_Load_Glyph( maFaceFT, nGlyphIndex, nLoadFlags );
    1581             : 
    1582       17079 :     if( rc != FT_Err_Ok )
    1583         224 :         return false;
    1584             : 
    1585       16855 :     if( mbArtBold && pFTEmbolden )
    1586           0 :         (*pFTEmbolden)( maFaceFT->glyph );
    1587             : 
    1588             :     FT_Glyph pGlyphFT;
    1589       16855 :     rc = FT_Get_Glyph( maFaceFT->glyph, &pGlyphFT );
    1590       16855 :     if( rc != FT_Err_Ok )
    1591           0 :         return false;
    1592             : 
    1593       16855 :     int nAngle = ApplyGlyphTransform( nGlyphFlags, pGlyphFT, true );
    1594             : 
    1595       16855 :     if( mbArtItalic )
    1596             :     {
    1597             :         FT_Matrix aMatrix;
    1598           0 :         aMatrix.xx = aMatrix.yy = 0x10000L;
    1599           0 :         if( nFTVERSION >= 2102 )    // Freetype 2.1.2 API swapped xy with yx
    1600           0 :             aMatrix.xy = 0x6000L, aMatrix.yx = 0;
    1601             :         else
    1602           0 :             aMatrix.yx = 0x6000L, aMatrix.xy = 0;
    1603           0 :         FT_Glyph_Transform( pGlyphFT, &aMatrix, NULL );
    1604             :     }
    1605             : 
    1606       16855 :     if( pGlyphFT->format == FT_GLYPH_FORMAT_OUTLINE )
    1607       16845 :         ((FT_OutlineGlyph)pGlyphFT)->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
    1608             : 
    1609       16855 :     bool bEmbedded = (pGlyphFT->format == FT_GLYPH_FORMAT_BITMAP);
    1610       16855 :     if( !bEmbedded )
    1611             :     {
    1612       16845 :         rc = FT_Glyph_To_Bitmap( &pGlyphFT, FT_RENDER_MODE_NORMAL, NULL, sal_True );
    1613       16845 :         if( rc != FT_Err_Ok )
    1614             :         {
    1615           0 :             FT_Done_Glyph( pGlyphFT );
    1616           0 :             return false;
    1617             :         }
    1618             :     }
    1619             : 
    1620       16855 :     const FT_BitmapGlyph pBmpGlyphFT = reinterpret_cast<const FT_BitmapGlyph>(pGlyphFT);
    1621       16855 :     rRawBitmap.mnXOffset        = +pBmpGlyphFT->left;
    1622       16855 :     rRawBitmap.mnYOffset        = -pBmpGlyphFT->top;
    1623             : 
    1624       16855 :     const FT_Bitmap& rBitmapFT  = pBmpGlyphFT->bitmap;
    1625       16855 :     rRawBitmap.mnHeight         = rBitmapFT.rows;
    1626       16855 :     rRawBitmap.mnWidth          = rBitmapFT.width;
    1627       16855 :     rRawBitmap.mnBitCount       = 8;
    1628       16855 :     rRawBitmap.mnScanlineSize   = bEmbedded ? rBitmapFT.width : rBitmapFT.pitch;
    1629       16855 :     if( mbArtBold && !pFTEmbolden )
    1630             :     {
    1631           0 :         ++rRawBitmap.mnWidth;
    1632           0 :             ++rRawBitmap.mnScanlineSize;
    1633             :     }
    1634       16855 :     rRawBitmap.mnScanlineSize = (rRawBitmap.mnScanlineSize + 3) & -4;
    1635             : 
    1636       16855 :     const sal_uLong nNeededSize = rRawBitmap.mnScanlineSize * rRawBitmap.mnHeight;
    1637       16855 :     if( rRawBitmap.mnAllocated < nNeededSize )
    1638             :     {
    1639       15790 :         delete[] rRawBitmap.mpBits;
    1640       15790 :         rRawBitmap.mnAllocated = 2*nNeededSize;
    1641       15790 :         rRawBitmap.mpBits = new unsigned char[ rRawBitmap.mnAllocated ];
    1642             :     }
    1643             : 
    1644       16855 :     const unsigned char* pSrc = rBitmapFT.buffer;
    1645       16855 :     unsigned char* pDest = rRawBitmap.mpBits;
    1646       16855 :     if( !bEmbedded )
    1647             :     {
    1648      162932 :         for( int y = rRawBitmap.mnHeight, x; --y >= 0 ; )
    1649             :         {
    1650     1139101 :             for( x = 0; x < rBitmapFT.width; ++x )
    1651     1009859 :                 *(pDest++) = *(pSrc++);
    1652      394051 :             for(; x < int(rRawBitmap.mnScanlineSize); ++x )
    1653      264809 :                 *(pDest++) = 0;
    1654             :         }
    1655             :     }
    1656             :     else
    1657             :     {
    1658         144 :         for( int y = rRawBitmap.mnHeight, x; --y >= 0 ; )
    1659             :         {
    1660         124 :             unsigned char nSrc = 0;
    1661        1664 :             for( x = 0; x < rBitmapFT.width; ++x, nSrc+=nSrc )
    1662             :             {
    1663        1540 :                 if( (x & 7) == 0 )
    1664         248 :                     nSrc = *(pSrc++);
    1665        1540 :                 *(pDest++) = (0x7F - nSrc) >> 8;
    1666             :             }
    1667         280 :             for(; x < int(rRawBitmap.mnScanlineSize); ++x )
    1668         156 :                 *(pDest++) = 0;
    1669             :         }
    1670             :     }
    1671             : 
    1672       16855 :     if( mbArtBold && !pFTEmbolden )
    1673             :     {
    1674             :         // overlay with glyph image shifted by one left pixel
    1675           0 :         unsigned char* p = rRawBitmap.mpBits;
    1676           0 :         for( sal_uLong y=0; y < rRawBitmap.mnHeight; y++ )
    1677             :         {
    1678           0 :             unsigned char nLastByte = 0;
    1679           0 :             for( sal_uLong x=0; x < rRawBitmap.mnWidth; x++ )
    1680             :             {
    1681           0 :                 unsigned char nTmp = p[x];
    1682           0 :                 p[x] |= p[x] | nLastByte;
    1683           0 :                 nLastByte = nTmp;
    1684             :             }
    1685           0 :             p += rRawBitmap.mnScanlineSize;
    1686             :         }
    1687             :     }
    1688             : 
    1689       16855 :     if( !bEmbedded && mbUseGamma )
    1690             :     {
    1691           0 :         unsigned char* p = rRawBitmap.mpBits;
    1692           0 :         for( sal_uLong y=0; y < rRawBitmap.mnHeight; y++ )
    1693             :         {
    1694           0 :             for( sal_uLong x=0; x < rRawBitmap.mnWidth; x++ )
    1695             :             {
    1696           0 :                 p[x] = aGammaTable[ p[x] ];
    1697             :             }
    1698           0 :             p += rRawBitmap.mnScanlineSize;
    1699             :         }
    1700             :     }
    1701             : 
    1702       16855 :     FT_Done_Glyph( pGlyphFT );
    1703             : 
    1704             :     // special case for 0/90/180/270 degree orientation
    1705       16855 :     switch( nAngle )
    1706             :     {
    1707             :         case  -900:
    1708             :         case  +900:
    1709             :         case +1800:
    1710             :         case +2700:
    1711        2140 :             rRawBitmap.Rotate( nAngle );
    1712        2140 :             break;
    1713             :     }
    1714             : 
    1715       16855 :     return true;
    1716             : }
    1717             : 
    1718             : // -----------------------------------------------------------------------
    1719             : // determine unicode ranges in font
    1720             : // -----------------------------------------------------------------------
    1721             : 
    1722        2841 : const ImplFontCharMap* ServerFont::GetImplFontCharMap( void ) const
    1723             : {
    1724        2841 :     const ImplFontCharMap* pIFCMap = mpFontInfo->GetImplFontCharMap();
    1725        2841 :     return pIFCMap;
    1726             : }
    1727             : 
    1728        2841 : const ImplFontCharMap* FtFontInfo::GetImplFontCharMap( void )
    1729             : {
    1730             :     // check if the charmap is already cached
    1731        2841 :     if( mpFontCharMap )
    1732        2819 :         return mpFontCharMap;
    1733             : 
    1734             :     // get the charmap and cache it
    1735          22 :     CmapResult aCmapResult;
    1736          22 :     bool bOK = GetFontCodeRanges( aCmapResult );
    1737          22 :     if( bOK )
    1738          22 :         mpFontCharMap = new ImplFontCharMap( aCmapResult );
    1739             :     else
    1740           0 :                mpFontCharMap = ImplFontCharMap::GetDefaultMap();
    1741          22 :     mpFontCharMap->AddReference();
    1742          22 :     return mpFontCharMap;
    1743             : }
    1744             : 
    1745             : // TODO: merge into method GetFontCharMap()
    1746          22 : bool FtFontInfo::GetFontCodeRanges( CmapResult& rResult ) const
    1747             : {
    1748          22 :     rResult.mbSymbolic = IsSymbolFont();
    1749             : 
    1750             :     // TODO: is the full CmapResult needed on platforms calling this?
    1751          22 :     if( FT_IS_SFNT( maFaceFT ) )
    1752             :     {
    1753          21 :         sal_uLong nLength = 0;
    1754          21 :         const unsigned char* pCmap = GetTable( "cmap", &nLength );
    1755          21 :         if( pCmap && (nLength > 0) )
    1756          21 :             if( ParseCMAP( pCmap, nLength, rResult ) )
    1757          21 :                 return true;
    1758             :     }
    1759             : 
    1760             :     typedef std::vector<sal_uInt32> U32Vector;
    1761           1 :     U32Vector aCodes;
    1762             : 
    1763             :     // FT's coverage is available since FT>=2.1.0 (OOo-baseline>=2.1.4 => ok)
    1764           1 :     aCodes.reserve( 0x1000 );
    1765             :     FT_UInt nGlyphIndex;
    1766           4 :     for( sal_uInt32 cCode = FT_Get_First_Char( maFaceFT, &nGlyphIndex );; )
    1767             :     {
    1768           4 :         if( !nGlyphIndex )
    1769           1 :             break;
    1770           3 :         aCodes.push_back( cCode );  // first code inside range
    1771           3 :         sal_uInt32 cNext = cCode;
    1772         188 :         do cNext = FT_Get_Next_Char( maFaceFT, cCode, &nGlyphIndex ); while( cNext == ++cCode );
    1773           3 :         aCodes.push_back( cCode );  // first code outside range
    1774           3 :         cCode = cNext;
    1775             :     }
    1776             : 
    1777           1 :     const int nCount = aCodes.size();
    1778           1 :     if( !nCount) {
    1779           0 :         if( !rResult.mbSymbolic )
    1780           0 :             return false;
    1781             : 
    1782             :         // we usually get here for Type1 symbol fonts
    1783           0 :         aCodes.push_back( 0xF020 );
    1784           0 :         aCodes.push_back( 0xF100 );
    1785             :     }
    1786             : 
    1787           1 :     sal_uInt32* pCodes = new sal_uInt32[ nCount ];
    1788           7 :     for( int i = 0; i < nCount; ++i )
    1789           6 :         pCodes[i] = aCodes[i];
    1790           1 :     rResult.mpRangeCodes = pCodes;
    1791           1 :     rResult.mnRangeCount = nCount / 2;
    1792           1 :     return true;
    1793             : }
    1794             : 
    1795           0 : bool ServerFont::GetFontCapabilities(vcl::FontCapabilities &rFontCapabilities) const
    1796             : {
    1797           0 :     bool bRet = false;
    1798             : 
    1799           0 :     sal_uLong nLength = 0;
    1800             :     // load GSUB table
    1801           0 :     const FT_Byte* pGSUB = mpFontInfo->GetTable("GSUB", &nLength);
    1802           0 :     if (pGSUB)
    1803           0 :         vcl::getTTScripts(rFontCapabilities.maGSUBScriptTags, pGSUB, nLength);
    1804             : 
    1805             :     // load OS/2 table
    1806           0 :     const FT_Byte* pOS2 = mpFontInfo->GetTable("OS/2", &nLength);
    1807           0 :     if (pOS2)
    1808             :     {
    1809             :         bRet = vcl::getTTCoverage(
    1810             :             rFontCapabilities.maUnicodeRange,
    1811             :             rFontCapabilities.maCodePageRange,
    1812           0 :             pOS2, nLength);
    1813             :     }
    1814             : 
    1815           0 :     return bRet;
    1816             : }
    1817             : 
    1818             : // -----------------------------------------------------------------------
    1819             : 
    1820           0 : sal_uLong ServerFont::GetKernPairs( ImplKernPairData** ppKernPairs ) const
    1821             : {
    1822             :     // if no kerning info is available in the font file
    1823           0 :     *ppKernPairs = NULL;
    1824           0 :     if( !FT_HAS_KERNING( maFaceFT ) || !FT_IS_SFNT( maFaceFT ) )
    1825             :     {
    1826             :         // then we have may have extra kerning info from e.g. psprint
    1827           0 :         int nCount = mpFontInfo->GetExtraKernPairs( ppKernPairs );
    1828             :         // scale the kern values to match the font size
    1829           0 :         const FontSelectPattern& rFSD = GetFontSelData();
    1830           0 :         int nFontWidth = rFSD.mnWidth ? rFSD.mnWidth : rFSD.mnHeight;
    1831           0 :         ImplKernPairData* pKernPair = *ppKernPairs;
    1832           0 :         for( int i = nCount; --i >= 0; ++pKernPair )
    1833             :         {
    1834           0 :             long& rVal = pKernPair->mnKern;
    1835           0 :             rVal = ((rVal * nFontWidth) + 500) / 1000;
    1836             :         }
    1837           0 :         return nCount;
    1838             :     }
    1839             : 
    1840             :     // when font faces of different sizes share the same maFaceFT
    1841             :     // then we have to make sure that it uses the correct maSizeFT
    1842           0 :     if( maSizeFT )
    1843           0 :         pFTActivateSize( maSizeFT );
    1844             : 
    1845             :     // first figure out which glyph pairs are involved in kerning
    1846           0 :     sal_uLong nKernLength = 0;
    1847           0 :     const FT_Byte* const pKern = mpFontInfo->GetTable( "kern", &nKernLength );
    1848           0 :     if( !pKern )
    1849           0 :         return 0;
    1850             : 
    1851             :     // combine TTF/OTF tables from the font file to build a vector of
    1852             :     // unicode kerning pairs using Freetype's glyph kerning calculation
    1853             :     // for the kerning value
    1854             : 
    1855             :     // TODO: is it worth to share the glyph->unicode mapping between
    1856             :     // different instances of the same font face?
    1857             : 
    1858             :     typedef std::vector<ImplKernPairData> KernVector;
    1859           0 :     KernVector aKernGlyphVector;
    1860             :     ImplKernPairData aKernPair;
    1861           0 :     aKernPair.mnKern = 0; // To prevent "is used uninitialized" warning...
    1862             : 
    1863           0 :     const FT_Byte* pBuffer = pKern;
    1864           0 :     sal_uLong nVersion = GetUShort( pBuffer+0 );
    1865           0 :     sal_uInt16 nTableCnt = GetUShort( pBuffer+2 );
    1866             : 
    1867             :     // Microsoft/Old TrueType style kern table
    1868           0 :     if ( nVersion == 0 )
    1869             :     {
    1870           0 :         pBuffer += 4;
    1871             : 
    1872           0 :         for( sal_uInt16 nTableIdx = 0; nTableIdx < nTableCnt; ++nTableIdx )
    1873             :         {
    1874             :             // sal_uInt16 nSubVersion  = GetUShort( pBuffer+0 );
    1875             :             // sal_uInt16 nSubLength   = GetUShort( pBuffer+2 );
    1876           0 :             sal_uInt16 nSubCoverage = GetUShort( pBuffer+4 );
    1877           0 :             pBuffer += 6;
    1878           0 :             if( (nSubCoverage&0x03) != 0x01 )   // no interest in minimum info here
    1879           0 :                 continue;
    1880           0 :             switch( nSubCoverage >> 8 )
    1881             :             {
    1882             :                 case 0: // version 0, kerning format 0
    1883             :                 {
    1884           0 :                     sal_uInt16 nPairs = GetUShort( pBuffer );
    1885           0 :                     pBuffer += 8;   // skip search hints
    1886           0 :                     aKernGlyphVector.reserve( aKernGlyphVector.size() + nPairs );
    1887           0 :                     for( int i = 0; i < nPairs; ++i )
    1888             :                     {
    1889           0 :                         aKernPair.mnChar1 = GetUShort( pBuffer+0 );
    1890           0 :                         aKernPair.mnChar2 = GetUShort( pBuffer+2 );
    1891             :                         //long nUnscaledKern= GetSShort( pBuffer );
    1892           0 :                         pBuffer += 6;
    1893           0 :                         aKernGlyphVector.push_back( aKernPair );
    1894             :                     }
    1895             :                 }
    1896           0 :                 break;
    1897             : 
    1898             :                 case 2: // version 0, kerning format 2
    1899             :                 {
    1900           0 :                     const FT_Byte* pSubTable = pBuffer;
    1901             :                     //sal_uInt16 nRowWidth  = GetUShort( pBuffer+0 );
    1902           0 :                     sal_uInt16 nOfsLeft     = GetUShort( pBuffer+2 );
    1903           0 :                     sal_uInt16 nOfsRight    = GetUShort( pBuffer+4 );
    1904           0 :                     sal_uInt16 nOfsArray    = GetUShort( pBuffer+6 );
    1905           0 :                     pBuffer += 8;
    1906             : 
    1907           0 :                     const FT_Byte* pTmp = pSubTable + nOfsLeft;
    1908           0 :                     sal_uInt16 nFirstLeft   = GetUShort( pTmp+0 );
    1909           0 :                     sal_uInt16 nLastLeft    = GetUShort( pTmp+2 ) + nFirstLeft - 1;
    1910             : 
    1911           0 :                     pTmp = pSubTable + nOfsRight;
    1912           0 :                     sal_uInt16 nFirstRight  = GetUShort( pTmp+0 );
    1913           0 :                     sal_uInt16 nLastRight   = GetUShort( pTmp+2 ) + nFirstRight - 1;
    1914             : 
    1915           0 :                     sal_uLong nPairs = (sal_uLong)(nLastLeft - nFirstLeft + 1) * (nLastRight - nFirstRight + 1);
    1916           0 :                     aKernGlyphVector.reserve( aKernGlyphVector.size() + nPairs );
    1917             : 
    1918           0 :                     pTmp = pSubTable + nOfsArray;
    1919           0 :                     for( int nLeft = nFirstLeft; nLeft < nLastLeft; ++nLeft )
    1920             :                     {
    1921           0 :                         aKernPair.mnChar1 = nLeft;
    1922           0 :                         for( int nRight = 0; nRight < nLastRight; ++nRight )
    1923             :                         {
    1924           0 :                             if( GetUShort( pTmp ) != 0 )
    1925             :                             {
    1926           0 :                                 aKernPair.mnChar2 = nRight;
    1927           0 :                                 aKernGlyphVector.push_back( aKernPair );
    1928             :                             }
    1929           0 :                             pTmp += 2;
    1930             :                         }
    1931             :                     }
    1932             :                 }
    1933           0 :                 break;
    1934             :             }
    1935             :         }
    1936             :     }
    1937             : 
    1938             :     // Apple New style kern table
    1939           0 :     pBuffer = pKern;
    1940           0 :     nVersion = NEXT_U32( pBuffer );
    1941           0 :     nTableCnt = NEXT_U32( pBuffer );
    1942           0 :     if ( nVersion == 0x00010000 )
    1943             :     {
    1944           0 :         for( sal_uInt16 nTableIdx = 0; nTableIdx < nTableCnt; ++nTableIdx )
    1945             :         {
    1946           0 :             /*sal_uLong  nLength  =*/ NEXT_U32( pBuffer );
    1947           0 :             sal_uInt16 nCoverage   = NEXT_U16( pBuffer );
    1948           0 :             /*sal_uInt16 nTupleIndex =*/ NEXT_U16( pBuffer );
    1949             : 
    1950             :             // Kerning sub-table format, 0 through 3
    1951           0 :             sal_uInt8 nSubTableFormat  = nCoverage & 0x00FF;
    1952             : 
    1953           0 :             switch( nSubTableFormat )
    1954             :             {
    1955             :                 case 0: // version 0, kerning format 0
    1956             :                 {
    1957           0 :                     sal_uInt16 nPairs = NEXT_U16( pBuffer );
    1958           0 :                     pBuffer += 6;   // skip search hints
    1959           0 :                     aKernGlyphVector.reserve( aKernGlyphVector.size() + nPairs );
    1960           0 :                     for( int i = 0; i < nPairs; ++i )
    1961             :                     {
    1962           0 :                         aKernPair.mnChar1 = NEXT_U16( pBuffer );
    1963           0 :                         aKernPair.mnChar2 = NEXT_U16( pBuffer );
    1964           0 :                         /*long nUnscaledKern=*/ NEXT_S16( pBuffer );
    1965           0 :                         aKernGlyphVector.push_back( aKernPair );
    1966             :                     }
    1967             :                 }
    1968           0 :                 break;
    1969             : 
    1970             :                 case 2: // version 0, kerning format 2
    1971             :                 {
    1972           0 :                     const FT_Byte* pSubTable = pBuffer;
    1973           0 :                     /*sal_uInt16 nRowWidth  =*/ NEXT_U16( pBuffer );
    1974           0 :                     sal_uInt16 nOfsLeft     = NEXT_U16( pBuffer );
    1975           0 :                     sal_uInt16 nOfsRight    = NEXT_U16( pBuffer );
    1976           0 :                     sal_uInt16 nOfsArray    = NEXT_U16( pBuffer );
    1977             : 
    1978           0 :                     const FT_Byte* pTmp = pSubTable + nOfsLeft;
    1979           0 :                     sal_uInt16 nFirstLeft   = NEXT_U16( pTmp );
    1980           0 :                     sal_uInt16 nLastLeft    = NEXT_U16( pTmp ) + nFirstLeft - 1;
    1981             : 
    1982           0 :                     pTmp = pSubTable + nOfsRight;
    1983           0 :                     sal_uInt16 nFirstRight  = NEXT_U16( pTmp );
    1984           0 :                     sal_uInt16 nLastRight   = NEXT_U16( pTmp ) + nFirstRight - 1;
    1985             : 
    1986           0 :                     sal_uLong nPairs = (sal_uLong)(nLastLeft - nFirstLeft + 1) * (nLastRight - nFirstRight + 1);
    1987           0 :                     aKernGlyphVector.reserve( aKernGlyphVector.size() + nPairs );
    1988             : 
    1989           0 :                     pTmp = pSubTable + nOfsArray;
    1990           0 :                     for( int nLeft = nFirstLeft; nLeft < nLastLeft; ++nLeft )
    1991             :                     {
    1992           0 :                         aKernPair.mnChar1 = nLeft;
    1993           0 :                         for( int nRight = 0; nRight < nLastRight; ++nRight )
    1994             :                         {
    1995           0 :                             if( NEXT_S16( pTmp ) != 0 )
    1996             :                             {
    1997           0 :                                 aKernPair.mnChar2 = nRight;
    1998           0 :                                 aKernGlyphVector.push_back( aKernPair );
    1999             :                             }
    2000             :                         }
    2001             :                     }
    2002             :                 }
    2003           0 :                 break;
    2004             : 
    2005             :                 default:
    2006           0 :                     fprintf( stderr, "gcach_ftyp.cxx:  Found unsupported Apple-style kern subtable type %d.\n", nSubTableFormat );
    2007           0 :                     break;
    2008             :             }
    2009             :         }
    2010             :     }
    2011             : 
    2012             :     // now create VCL's ImplKernPairData[] format for all glyph pairs
    2013           0 :     sal_uLong nKernCount = aKernGlyphVector.size();
    2014           0 :     if( nKernCount )
    2015             :     {
    2016             :         // prepare glyphindex to character mapping
    2017             :         // TODO: this is needed to support VCL's existing kerning infrastructure,
    2018             :         // eliminate it up by redesigning kerning infrastructure to work with glyph indizes
    2019             :         typedef boost::unordered_multimap<sal_uInt16,sal_Unicode> Cmap;
    2020           0 :         Cmap aCmap;
    2021           0 :         for( sal_Unicode aChar = 0x0020; aChar < 0xFFFE; ++aChar )
    2022             :         {
    2023           0 :             sal_uInt16 nGlyphIndex = GetGlyphIndex( aChar );
    2024           0 :             if( nGlyphIndex )
    2025           0 :                 aCmap.insert( Cmap::value_type( nGlyphIndex, aChar ) );
    2026             :         }
    2027             : 
    2028             :         // translate both glyph indizes in kerning pairs to characters
    2029             :         // problem is that these are 1:n mappings...
    2030           0 :         KernVector aKernCharVector;
    2031           0 :         aKernCharVector.reserve( nKernCount );
    2032           0 :         KernVector::iterator it;
    2033           0 :         for( it = aKernGlyphVector.begin(); it != aKernGlyphVector.end(); ++it )
    2034             :         {
    2035             :             FT_Vector aKernVal;
    2036           0 :             FT_Error rcFT = FT_Get_Kerning( maFaceFT, it->mnChar1, it->mnChar2,
    2037           0 :                 FT_KERNING_DEFAULT, &aKernVal );
    2038           0 :             aKernPair.mnKern = aKernVal.x >> 6;
    2039           0 :             if( (aKernPair.mnKern == 0) || (rcFT != FT_Err_Ok) )
    2040           0 :                 continue;
    2041             : 
    2042             :             typedef std::pair<Cmap::iterator,Cmap::iterator> CPair;
    2043           0 :             const CPair p1 = aCmap.equal_range( it->mnChar1 );
    2044           0 :             const CPair p2 = aCmap.equal_range( it->mnChar2 );
    2045           0 :             for( Cmap::const_iterator i1 = p1.first; i1 != p1.second; ++i1 )
    2046             :             {
    2047           0 :                 aKernPair.mnChar1 = (*i1).second;
    2048           0 :                 for( Cmap::const_iterator i2 = p2.first; i2 != p2.second; ++i2 )
    2049             :                 {
    2050           0 :                     aKernPair.mnChar2 = (*i2).second;
    2051           0 :                     aKernCharVector.push_back( aKernPair );
    2052             :                 }
    2053             :             }
    2054             :         }
    2055             : 
    2056             :         // now move the resulting vector into VCL's ImplKernPairData[] format
    2057           0 :         nKernCount = aKernCharVector.size();
    2058           0 :         ImplKernPairData* pTo = new ImplKernPairData[ nKernCount ];
    2059           0 :         *ppKernPairs = pTo;
    2060           0 :         for( it = aKernCharVector.begin(); it != aKernCharVector.end(); ++it, ++pTo )
    2061             :         {
    2062           0 :             pTo->mnChar1 = it->mnChar1;
    2063           0 :             pTo->mnChar2 = it->mnChar2;
    2064           0 :             pTo->mnKern = it->mnKern;
    2065           0 :         }
    2066             :     }
    2067             : 
    2068           0 :     return nKernCount;
    2069             : }
    2070             : 
    2071             : // -----------------------------------------------------------------------
    2072             : // outline stuff
    2073             : // -----------------------------------------------------------------------
    2074             : 
    2075             : class PolyArgs
    2076             : {
    2077             : public:
    2078             :                 PolyArgs( PolyPolygon& rPolyPoly, sal_uInt16 nMaxPoints );
    2079             :                 ~PolyArgs();
    2080             : 
    2081             :     void        AddPoint( long nX, long nY, PolyFlags);
    2082             :     void        ClosePolygon();
    2083             : 
    2084           0 :     long        GetPosX() const { return maPosition.x;}
    2085           0 :     long        GetPosY() const { return maPosition.y;}
    2086             : 
    2087             : private:
    2088             :     PolyPolygon& mrPolyPoly;
    2089             : 
    2090             :     Point*      mpPointAry;
    2091             :     sal_uInt8*       mpFlagAry;
    2092             : 
    2093             :     FT_Vector   maPosition;
    2094             :     sal_uInt16      mnMaxPoints;
    2095             :     sal_uInt16      mnPoints;
    2096             :     sal_uInt16      mnPoly;
    2097             :     bool        bHasOffline;
    2098             : };
    2099             : 
    2100             : // -----------------------------------------------------------------------
    2101             : 
    2102          73 : PolyArgs::PolyArgs( PolyPolygon& rPolyPoly, sal_uInt16 nMaxPoints )
    2103             : :   mrPolyPoly(rPolyPoly),
    2104             :     mnMaxPoints(nMaxPoints),
    2105             :     mnPoints(0),
    2106             :     mnPoly(0),
    2107          73 :     bHasOffline(false)
    2108             : {
    2109          73 :     mpPointAry  = new Point[ mnMaxPoints ];
    2110          73 :     mpFlagAry   = new sal_uInt8 [ mnMaxPoints ];
    2111          73 : }
    2112             : 
    2113             : // -----------------------------------------------------------------------
    2114             : 
    2115             : 
    2116          73 : PolyArgs::~PolyArgs()
    2117             : {
    2118          73 :     delete[] mpFlagAry;
    2119          73 :     delete[] mpPointAry;
    2120          73 : }
    2121             : 
    2122             : // -----------------------------------------------------------------------
    2123             : 
    2124        6821 : void PolyArgs::AddPoint( long nX, long nY, PolyFlags aFlag )
    2125             : {
    2126             :     DBG_ASSERT( (mnPoints < mnMaxPoints), "FTGlyphOutline: AddPoint overflow!" );
    2127        6821 :     if( mnPoints >= mnMaxPoints )
    2128        6821 :         return;
    2129             : 
    2130        6821 :     maPosition.x = nX;
    2131        6821 :     maPosition.y = nY;
    2132        6821 :     mpPointAry[ mnPoints ] = Point( nX, nY );
    2133        6821 :     mpFlagAry[ mnPoints++ ]= aFlag;
    2134        6821 :     bHasOffline |= (aFlag != POLY_NORMAL);
    2135             : }
    2136             : 
    2137             : // -----------------------------------------------------------------------
    2138             : 
    2139         338 : void PolyArgs::ClosePolygon()
    2140             : {
    2141         338 :     if( !mnPoly++ )
    2142         338 :         return;
    2143             : 
    2144             :     // freetype seems to always close the polygon with an ON_CURVE point
    2145             :     // PolyPoly wants to close the polygon itself => remove last point
    2146             :     DBG_ASSERT( (mnPoints >= 2), "FTGlyphOutline: PolyFinishNum failed!" );
    2147         265 :     --mnPoints;
    2148             :     DBG_ASSERT( (mpPointAry[0]==mpPointAry[mnPoints]), "FTGlyphOutline: PolyFinishEq failed!" );
    2149             :     DBG_ASSERT( (mpFlagAry[0]==POLY_NORMAL), "FTGlyphOutline: PolyFinishFE failed!" );
    2150             :     DBG_ASSERT( (mpFlagAry[mnPoints]==POLY_NORMAL), "FTGlyphOutline: PolyFinishFS failed!" );
    2151             : 
    2152         265 :     Polygon aPoly( mnPoints, mpPointAry, (bHasOffline ? mpFlagAry : NULL) );
    2153             : 
    2154             :     // #i35928#
    2155             :     // This may be a invalid polygons, e.g. the last point is a control point.
    2156             :     // So close the polygon (and add the first point again) if the last point
    2157             :     // is a control point or different from first.
    2158             :     // #i48298#
    2159             :     // Now really duplicating the first point, to close or correct the
    2160             :     // polygon. Also no longer duplicating the flags, but enforcing
    2161             :     // POLY_NORMAL for the newly added last point.
    2162         265 :     const sal_uInt16 nPolySize(aPoly.GetSize());
    2163         265 :     if(nPolySize)
    2164             :     {
    2165         454 :         if((aPoly.HasFlags() && POLY_CONTROL == aPoly.GetFlags(nPolySize - 1))
    2166         189 :             || (aPoly.GetPoint(nPolySize - 1) != aPoly.GetPoint(0)))
    2167             :         {
    2168         265 :             aPoly.SetSize(nPolySize + 1);
    2169         265 :             aPoly.SetPoint(aPoly.GetPoint(0), nPolySize);
    2170             : 
    2171         265 :             if(aPoly.HasFlags())
    2172             :             {
    2173         178 :                 aPoly.SetFlags(nPolySize, POLY_NORMAL);
    2174             :             }
    2175             :         }
    2176             :     }
    2177             : 
    2178         265 :     mrPolyPoly.Insert( aPoly );
    2179         265 :     mnPoints = 0;
    2180         265 :     bHasOffline = false;
    2181             : }
    2182             : 
    2183             : // -----------------------------------------------------------------------
    2184             : 
    2185             : extern "C" {
    2186             : 
    2187             : // TODO: wait till all compilers accept that calling conventions
    2188             : // for functions are the same independent of implementation constness,
    2189             : // then uncomment the const-tokens in the function interfaces below
    2190         265 : static int FT_move_to( FT_Vector_CPtr p0, void* vpPolyArgs )
    2191             : {
    2192         265 :     PolyArgs& rA = *reinterpret_cast<PolyArgs*>(vpPolyArgs);
    2193             : 
    2194             :     // move_to implies a new polygon => finish old polygon first
    2195         265 :     rA.ClosePolygon();
    2196             : 
    2197         265 :     rA.AddPoint( p0->x, p0->y, POLY_NORMAL );
    2198         265 :     return 0;
    2199             : }
    2200             : 
    2201        1345 : static int FT_line_to( FT_Vector_CPtr p1, void* vpPolyArgs )
    2202             : {
    2203        1345 :     PolyArgs& rA = *reinterpret_cast<PolyArgs*>(vpPolyArgs);
    2204        1345 :     rA.AddPoint( p1->x, p1->y, POLY_NORMAL );
    2205        1345 :     return 0;
    2206             : }
    2207             : 
    2208           0 : static int FT_conic_to( FT_Vector_CPtr p1, FT_Vector_CPtr p2, void* vpPolyArgs )
    2209             : {
    2210           0 :     PolyArgs& rA = *reinterpret_cast<PolyArgs*>(vpPolyArgs);
    2211             : 
    2212             :     // VCL's Polygon only knows cubic beziers
    2213           0 :     const long nX1 = (2 * rA.GetPosX() + 4 * p1->x + 3) / 6;
    2214           0 :     const long nY1 = (2 * rA.GetPosY() + 4 * p1->y + 3) / 6;
    2215           0 :     rA.AddPoint( nX1, nY1, POLY_CONTROL );
    2216             : 
    2217           0 :     const long nX2 = (2 * p2->x + 4 * p1->x + 3) / 6;
    2218           0 :     const long nY2 = (2 * p2->y + 4 * p1->y + 3) / 6;
    2219           0 :     rA.AddPoint( nX2, nY2, POLY_CONTROL );
    2220             : 
    2221           0 :     rA.AddPoint( p2->x, p2->y, POLY_NORMAL );
    2222           0 :     return 0;
    2223             : }
    2224             : 
    2225        1737 : static int FT_cubic_to( FT_Vector_CPtr p1, FT_Vector_CPtr p2, FT_Vector_CPtr p3, void* vpPolyArgs )
    2226             : {
    2227        1737 :     PolyArgs& rA = *reinterpret_cast<PolyArgs*>(vpPolyArgs);
    2228        1737 :     rA.AddPoint( p1->x, p1->y, POLY_CONTROL );
    2229        1737 :     rA.AddPoint( p2->x, p2->y, POLY_CONTROL );
    2230        1737 :     rA.AddPoint( p3->x, p3->y, POLY_NORMAL );
    2231        1737 :     return 0;
    2232             : }
    2233             : 
    2234             : } // extern "C"
    2235             : 
    2236             : // -----------------------------------------------------------------------
    2237             : 
    2238         119 : bool ServerFont::GetGlyphOutline( int nGlyphIndex,
    2239             :     ::basegfx::B2DPolyPolygon& rB2DPolyPoly ) const
    2240             : {
    2241         119 :     if( maSizeFT )
    2242         119 :         pFTActivateSize( maSizeFT );
    2243             : 
    2244         119 :     rB2DPolyPoly.clear();
    2245             : 
    2246             :     int nGlyphFlags;
    2247         119 :     SplitGlyphFlags( *this, nGlyphIndex, nGlyphFlags );
    2248             : 
    2249         119 :     FT_Int nLoadFlags = FT_LOAD_DEFAULT | FT_LOAD_IGNORE_TRANSFORM;
    2250             : 
    2251             : #ifdef FT_LOAD_TARGET_LIGHT
    2252             :     // enable "light hinting" if available
    2253         119 :     if( nFTVERSION >= 2103 )
    2254         119 :         nLoadFlags |= FT_LOAD_TARGET_LIGHT;
    2255             : #endif
    2256             : 
    2257         119 :     FT_Error rc = FT_Load_Glyph( maFaceFT, nGlyphIndex, nLoadFlags );
    2258         119 :     if( rc != FT_Err_Ok )
    2259          46 :         return false;
    2260             : 
    2261          73 :     if( mbArtBold && pFTEmbolden )
    2262           0 :         (*pFTEmbolden)( maFaceFT->glyph );
    2263             : 
    2264             :     FT_Glyph pGlyphFT;
    2265          73 :     rc = FT_Get_Glyph( maFaceFT->glyph, &pGlyphFT );
    2266          73 :     if( rc != FT_Err_Ok )
    2267           0 :         return false;
    2268             : 
    2269          73 :     if( pGlyphFT->format != FT_GLYPH_FORMAT_OUTLINE )
    2270           0 :         return false;
    2271             : 
    2272          73 :     if( mbArtItalic )
    2273             :     {
    2274             :         FT_Matrix aMatrix;
    2275           0 :         aMatrix.xx = aMatrix.yy = 0x10000L;
    2276           0 :         if( nFTVERSION >= 2102 )    // Freetype 2.1.2 API swapped xy with yx
    2277           0 :             aMatrix.xy = 0x6000L, aMatrix.yx = 0;
    2278             :         else
    2279           0 :             aMatrix.yx = 0x6000L, aMatrix.xy = 0;
    2280           0 :         FT_Glyph_Transform( pGlyphFT, &aMatrix, NULL );
    2281             :     }
    2282             : 
    2283          73 :     FT_Outline& rOutline = reinterpret_cast<FT_OutlineGlyphRec*>(pGlyphFT)->outline;
    2284          73 :     if( !rOutline.n_points )    // blank glyphs are ok
    2285           0 :         return true;
    2286             : 
    2287          73 :     long nMaxPoints = 1 + rOutline.n_points * 3;
    2288          73 :     PolyPolygon aToolPolyPolygon;
    2289          73 :     PolyArgs aPolyArg( aToolPolyPolygon, nMaxPoints );
    2290             : 
    2291          73 :     /*int nAngle =*/ ApplyGlyphTransform( nGlyphFlags, pGlyphFT, false );
    2292             : 
    2293             :     FT_Outline_Funcs aFuncs;
    2294          73 :     aFuncs.move_to  = &FT_move_to;
    2295          73 :     aFuncs.line_to  = &FT_line_to;
    2296          73 :     aFuncs.conic_to = &FT_conic_to;
    2297          73 :     aFuncs.cubic_to = &FT_cubic_to;
    2298          73 :     aFuncs.shift    = 0;
    2299          73 :     aFuncs.delta    = 0;
    2300          73 :     rc = FT_Outline_Decompose( &rOutline, &aFuncs, (void*)&aPolyArg );
    2301          73 :     aPolyArg.ClosePolygon();    // close last polygon
    2302          73 :     FT_Done_Glyph( pGlyphFT );
    2303             : 
    2304             :     // convert to basegfx polypolygon
    2305             :     // TODO: get rid of the intermediate tools polypolygon
    2306          73 :     rB2DPolyPoly = aToolPolyPolygon.getB2DPolyPolygon();
    2307          73 :     rB2DPolyPoly.transform(basegfx::tools::createScaleB2DHomMatrix( +1.0/(1<<6), -1.0/(1<<6) ));
    2308             : 
    2309          73 :     return true;
    2310             : }
    2311             : 
    2312             : // -----------------------------------------------------------------------
    2313             : 
    2314         974 : bool ServerFont::ApplyGSUB( const FontSelectPattern& rFSD )
    2315             : {
    2316             : #define MKTAG(s) ((((((s[0]<<8)+s[1])<<8)+s[2])<<8)+s[3])
    2317             : 
    2318             :     typedef std::vector<sal_uLong> ReqFeatureTagList;
    2319         974 :     ReqFeatureTagList aReqFeatureTagList;
    2320         974 :     if( rFSD.mbVertical )
    2321           0 :         aReqFeatureTagList.push_back( MKTAG("vert") );
    2322         974 :     sal_uLong nRequestedScript = 0;     //MKTAG("hani");//### TODO: where to get script?
    2323         974 :     sal_uLong nRequestedLangsys = 0;    //MKTAG("ZHT"); //### TODO: where to get langsys?
    2324             :     // TODO: request more features depending on script and language system
    2325             : 
    2326         974 :     if( aReqFeatureTagList.empty()) // nothing to do
    2327         974 :         return true;
    2328             : 
    2329             :     // load GSUB table into memory
    2330           0 :     sal_uLong nLength = 0;
    2331           0 :     const FT_Byte* const pGsubBase = mpFontInfo->GetTable( "GSUB", &nLength );
    2332           0 :     if( !pGsubBase )
    2333           0 :         return false;
    2334             : 
    2335             :     // parse GSUB header
    2336           0 :     const FT_Byte* pGsubHeader = pGsubBase;
    2337           0 :     const sal_uInt16 nOfsScriptList     = GetUShort( pGsubHeader+4 );
    2338           0 :     const sal_uInt16 nOfsFeatureTable   = GetUShort( pGsubHeader+6 );
    2339           0 :     const sal_uInt16 nOfsLookupList     = GetUShort( pGsubHeader+8 );
    2340           0 :     pGsubHeader += 10;
    2341             : 
    2342             :     typedef std::vector<sal_uInt16> UshortList;
    2343           0 :     UshortList aFeatureIndexList;
    2344             : 
    2345             :     // parse Script Table
    2346           0 :     const FT_Byte* pScriptHeader = pGsubBase + nOfsScriptList;
    2347           0 :     const sal_uInt16 nCntScript = GetUShort( pScriptHeader+0 );
    2348           0 :     pScriptHeader += 2;
    2349           0 :     for( sal_uInt16 nScriptIndex = 0; nScriptIndex < nCntScript; ++nScriptIndex )
    2350             :     {
    2351           0 :         const sal_uLong nScriptTag      = GetUInt( pScriptHeader+0 ); // e.g. hani/arab/kana/hang
    2352           0 :         const sal_uInt16 nOfsScriptTable= GetUShort( pScriptHeader+4 );
    2353           0 :         pScriptHeader += 6; //###
    2354           0 :         if( (nScriptTag != nRequestedScript) && (nRequestedScript != 0) )
    2355           0 :             continue;
    2356             : 
    2357           0 :         const FT_Byte* pScriptTable     = pGsubBase + nOfsScriptList + nOfsScriptTable;
    2358           0 :         const sal_uInt16 nDefaultLangsysOfs = GetUShort( pScriptTable+0 );
    2359           0 :         const sal_uInt16 nCntLangSystem     = GetUShort( pScriptTable+2 );
    2360           0 :         pScriptTable += 4;
    2361           0 :         sal_uInt16 nLangsysOffset = 0;
    2362             : 
    2363           0 :         for( sal_uInt16 nLangsysIndex = 0; nLangsysIndex < nCntLangSystem; ++nLangsysIndex )
    2364             :         {
    2365           0 :             const sal_uLong nTag    = GetUInt( pScriptTable+0 );    // e.g. KOR/ZHS/ZHT/JAN
    2366           0 :             const sal_uInt16 nOffset= GetUShort( pScriptTable+4 );
    2367           0 :             pScriptTable += 6;
    2368           0 :             if( (nTag != nRequestedLangsys) && (nRequestedLangsys != 0) )
    2369           0 :                 continue;
    2370           0 :             nLangsysOffset = nOffset;
    2371           0 :             break;
    2372             :         }
    2373             : 
    2374           0 :         if( (nDefaultLangsysOfs != 0) && (nDefaultLangsysOfs != nLangsysOffset) )
    2375             :         {
    2376           0 :             const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nDefaultLangsysOfs;
    2377           0 :             const sal_uInt16 nReqFeatureIdx = GetUShort( pLangSys+2 );
    2378           0 :             const sal_uInt16 nCntFeature    = GetUShort( pLangSys+4 );
    2379           0 :             pLangSys += 6;
    2380           0 :             aFeatureIndexList.push_back( nReqFeatureIdx );
    2381           0 :             for( sal_uInt16 i = 0; i < nCntFeature; ++i )
    2382             :             {
    2383           0 :                 const sal_uInt16 nFeatureIndex = GetUShort( pLangSys );
    2384           0 :                 pLangSys += 2;
    2385           0 :                 aFeatureIndexList.push_back( nFeatureIndex );
    2386             :             }
    2387             :         }
    2388             : 
    2389           0 :         if( nLangsysOffset != 0 )
    2390             :         {
    2391           0 :             const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nLangsysOffset;
    2392           0 :             const sal_uInt16 nReqFeatureIdx = GetUShort( pLangSys+2 );
    2393           0 :             const sal_uInt16 nCntFeature    = GetUShort( pLangSys+4 );
    2394           0 :             pLangSys += 6;
    2395           0 :             aFeatureIndexList.push_back( nReqFeatureIdx );
    2396           0 :             for( sal_uInt16 i = 0; i < nCntFeature; ++i )
    2397             :             {
    2398           0 :                 const sal_uInt16 nFeatureIndex = GetUShort( pLangSys );
    2399           0 :                 pLangSys += 2;
    2400           0 :                 aFeatureIndexList.push_back( nFeatureIndex );
    2401             :             }
    2402             :         }
    2403             :     }
    2404             : 
    2405           0 :     if( aFeatureIndexList.empty() )
    2406           0 :         return true;
    2407             : 
    2408           0 :     UshortList aLookupIndexList;
    2409           0 :     UshortList aLookupOffsetList;
    2410             : 
    2411             :     // parse Feature Table
    2412           0 :     const FT_Byte* pFeatureHeader = pGsubBase + nOfsFeatureTable;
    2413           0 :     const sal_uInt16 nCntFeature = GetUShort( pFeatureHeader );
    2414           0 :     pFeatureHeader += 2;
    2415           0 :     for( sal_uInt16 nFeatureIndex = 0; nFeatureIndex < nCntFeature; ++nFeatureIndex )
    2416             :     {
    2417           0 :         const sal_uLong nTag    = GetUInt( pFeatureHeader+0 ); // e.g. locl/vert/trad/smpl/liga/fina/...
    2418           0 :         const sal_uInt16 nOffset= GetUShort( pFeatureHeader+4 );
    2419           0 :         pFeatureHeader += 6;
    2420             : 
    2421             :         // short circuit some feature lookups
    2422           0 :         if( aFeatureIndexList[0] != nFeatureIndex ) // required feature?
    2423             :         {
    2424           0 :             const int nRequested = std::count( aFeatureIndexList.begin(), aFeatureIndexList.end(), nFeatureIndex);
    2425           0 :             if( !nRequested )  // ignore features that are not requested
    2426           0 :                 continue;
    2427           0 :             const int nAvailable = std::count( aReqFeatureTagList.begin(), aReqFeatureTagList.end(), nTag);
    2428           0 :             if( !nAvailable )  // some fonts don't provide features they request!
    2429           0 :                 continue;
    2430             :         }
    2431             : 
    2432           0 :         const FT_Byte* pFeatureTable = pGsubBase + nOfsFeatureTable + nOffset;
    2433           0 :         const sal_uInt16 nCntLookups = GetUShort( pFeatureTable+0 );
    2434           0 :         pFeatureTable += 2;
    2435           0 :         for( sal_uInt16 i = 0; i < nCntLookups; ++i )
    2436             :         {
    2437           0 :             const sal_uInt16 nLookupIndex = GetUShort( pFeatureTable );
    2438           0 :             pFeatureTable += 2;
    2439           0 :             aLookupIndexList.push_back( nLookupIndex );
    2440             :         }
    2441           0 :         if( nCntLookups == 0 ) //### hack needed by Mincho/Gothic/Mingliu/Simsun/...
    2442           0 :             aLookupIndexList.push_back( 0 );
    2443             :     }
    2444             : 
    2445             :     // parse Lookup List
    2446           0 :     const FT_Byte* pLookupHeader = pGsubBase + nOfsLookupList;
    2447           0 :     const sal_uInt16 nCntLookupTable = GetUShort( pLookupHeader );
    2448           0 :     pLookupHeader += 2;
    2449           0 :     for( sal_uInt16 nLookupIdx = 0; nLookupIdx < nCntLookupTable; ++nLookupIdx )
    2450             :     {
    2451           0 :         const sal_uInt16 nOffset = GetUShort( pLookupHeader );
    2452           0 :         pLookupHeader += 2;
    2453           0 :         if( std::count( aLookupIndexList.begin(), aLookupIndexList.end(), nLookupIdx ) )
    2454           0 :             aLookupOffsetList.push_back( nOffset );
    2455             :     }
    2456             : 
    2457           0 :     UshortList::const_iterator lookup_it = aLookupOffsetList.begin();
    2458           0 :     for(; lookup_it != aLookupOffsetList.end(); ++lookup_it )
    2459             :     {
    2460           0 :         const sal_uInt16 nOfsLookupTable = *lookup_it;
    2461           0 :         const FT_Byte* pLookupTable = pGsubBase + nOfsLookupList + nOfsLookupTable;
    2462           0 :         const sal_uInt16 eLookupType        = GetUShort( pLookupTable+0 );
    2463           0 :         const sal_uInt16 nCntLookupSubtable = GetUShort( pLookupTable+4 );
    2464           0 :         pLookupTable += 6;
    2465             : 
    2466             :         // TODO: switch( eLookupType )
    2467           0 :         if( eLookupType != 1 )  // TODO: once we go beyond SingleSubst
    2468           0 :             continue;
    2469             : 
    2470           0 :         for( sal_uInt16 nSubTableIdx = 0; nSubTableIdx < nCntLookupSubtable; ++nSubTableIdx )
    2471             :         {
    2472           0 :             const sal_uInt16 nOfsSubLookupTable = GetUShort( pLookupTable );
    2473           0 :             pLookupTable += 2;
    2474           0 :             const FT_Byte* pSubLookup = pGsubBase + nOfsLookupList + nOfsLookupTable + nOfsSubLookupTable;
    2475             : 
    2476           0 :             const sal_uInt16 nFmtSubstitution   = GetUShort( pSubLookup+0 );
    2477           0 :             const sal_uInt16 nOfsCoverage       = GetUShort( pSubLookup+2 );
    2478           0 :             pSubLookup += 4;
    2479             : 
    2480             :             typedef std::pair<sal_uInt16,sal_uInt16> GlyphSubst;
    2481             :             typedef std::vector<GlyphSubst> SubstVector;
    2482           0 :             SubstVector aSubstVector;
    2483             : 
    2484           0 :             const FT_Byte* pCoverage    = pGsubBase + nOfsLookupList + nOfsLookupTable + nOfsSubLookupTable + nOfsCoverage;
    2485           0 :             const sal_uInt16 nFmtCoverage   = GetUShort( pCoverage+0 );
    2486           0 :             pCoverage += 2;
    2487           0 :             switch( nFmtCoverage )
    2488             :             {
    2489             :                 case 1:         // Coverage Format 1
    2490             :                     {
    2491           0 :                         const sal_uInt16 nCntGlyph = GetUShort( pCoverage );
    2492           0 :                         pCoverage += 2;
    2493           0 :                         aSubstVector.reserve( nCntGlyph );
    2494           0 :                         for( sal_uInt16 i = 0; i < nCntGlyph; ++i )
    2495             :                         {
    2496           0 :                             const sal_uInt16 nGlyphId = GetUShort( pCoverage );
    2497           0 :                             pCoverage += 2;
    2498           0 :                             aSubstVector.push_back( GlyphSubst( nGlyphId, 0 ) );
    2499             :                         }
    2500             :                     }
    2501           0 :                     break;
    2502             : 
    2503             :                 case 2:         // Coverage Format 2
    2504             :                     {
    2505           0 :                         const sal_uInt16 nCntRange = GetUShort( pCoverage );
    2506           0 :                         pCoverage += 2;
    2507           0 :                         for( int i = nCntRange; --i >= 0; )
    2508             :                         {
    2509           0 :                             const sal_uInt32 nGlyph0 = GetUShort( pCoverage+0 );
    2510           0 :                             const sal_uInt32 nGlyph1 = GetUShort( pCoverage+2 );
    2511           0 :                             const sal_uInt16 nCovIdx = GetUShort( pCoverage+4 );
    2512           0 :                             pCoverage += 6;
    2513           0 :                             for( sal_uInt32 j = nGlyph0; j <= nGlyph1; ++j )
    2514           0 :                                 aSubstVector.push_back( GlyphSubst( static_cast<sal_uInt16>(j + nCovIdx), 0 ) );
    2515             :                         }
    2516             :                     }
    2517           0 :                     break;
    2518             :             }
    2519             : 
    2520           0 :             SubstVector::iterator it( aSubstVector.begin() );
    2521             : 
    2522           0 :             switch( nFmtSubstitution )
    2523             :             {
    2524             :                 case 1:     // Single Substitution Format 1
    2525             :                     {
    2526           0 :                         const sal_uInt16 nDeltaGlyphId = GetUShort( pSubLookup );
    2527           0 :                         pSubLookup += 2;
    2528           0 :                         for(; it != aSubstVector.end(); ++it )
    2529           0 :                             (*it).second = (*it).first + nDeltaGlyphId;
    2530             :                     }
    2531           0 :                     break;
    2532             : 
    2533             :                 case 2:     // Single Substitution Format 2
    2534             :                     {
    2535           0 :                         const sal_uInt16 nCntGlyph = GetUShort( pSubLookup );
    2536           0 :                         pSubLookup += 2;
    2537           0 :                         for( int i = nCntGlyph; (it != aSubstVector.end()) && (--i>=0); ++it )
    2538             :                         {
    2539           0 :                             const sal_uInt16 nGlyphId = GetUShort( pSubLookup );
    2540           0 :                             pSubLookup += 2;
    2541           0 :                             (*it).second = nGlyphId;
    2542             :                         }
    2543             :                     }
    2544           0 :                     break;
    2545             :             }
    2546             : 
    2547             :             DBG_ASSERT( (it == aSubstVector.end()), "lookup<->coverage table mismatch" );
    2548             :             // now apply the glyph substitutions that have been collected in this subtable
    2549           0 :             for( it = aSubstVector.begin(); it != aSubstVector.end(); ++it )
    2550           0 :                 maGlyphSubstitution[ (*it).first ] =  (*it).second;
    2551           0 :         }
    2552             :     }
    2553             : 
    2554           0 :     return true;
    2555             : }
    2556             : 
    2557        3199 : const unsigned char* ServerFont::GetTable(const char* pName, sal_uLong* pLength)
    2558             : {
    2559        3199 :     return mpFontInfo->GetTable( pName, pLength );
    2560             : }
    2561             : 
    2562             : #ifdef ENABLE_GRAPHITE
    2563           0 : GraphiteFaceWrapper* ServerFont::GetGraphiteFace() const
    2564             : {
    2565           0 :     return mpFontInfo->GetGraphiteFace();
    2566             : }
    2567             : #endif
    2568             : 
    2569             : // =======================================================================
    2570             : 
    2571             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10