LCOV - code coverage report
Current view: top level - vcl/source/fontsubset - gsub.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 113 194 58.2 %
Date: 2015-06-13 12:38:46 Functions: 5 6 83.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             : #include "sft.hxx"
      21             : 
      22             : #include "gsub.h"
      23             : 
      24             : #include <osl/diagnose.h>
      25             : 
      26             : #include <vector>
      27             : #include <map>
      28             : #include <algorithm>
      29             : 
      30             : namespace vcl
      31             : {
      32             : 
      33             : typedef sal_uIntPtr sal_uLong;
      34             : typedef sal_uInt8 FT_Byte;
      35             : 
      36             : typedef std::map<sal_uInt16,sal_uInt16> GlyphSubstitution;
      37             : 
      38      354506 : inline sal_uInt32 NEXT_Long( const unsigned char* &p )
      39             : {
      40      354506 :     sal_uInt32 nVal = (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
      41      354506 :     p += 4;
      42      354506 :     return nVal;
      43             : }
      44             : 
      45     1198363 : inline sal_uInt16 NEXT_UShort( const unsigned char* &p )
      46             : {
      47     1198363 :     sal_uInt16 nVal = (p[0]<<8) + p[1];
      48     1198363 :     p += 2;
      49     1198363 :     return nVal;
      50             : }
      51             : 
      52             : #define MKTAG(s) ((((((s[0]<<8)+s[1])<<8)+s[2])<<8)+s[3])
      53             : 
      54       10257 : bool ReadGSUB( struct _TrueTypeFont* pTTFile,
      55             :     int nRequestedScript, int nRequestedLangsys )
      56             : {
      57       10257 :     const FT_Byte* pGsubBase = pTTFile->tables[ O_gsub ];
      58       10257 :     if( !pGsubBase )
      59        1160 :         return false;
      60             : 
      61             :     // #129682# check offsets inside GSUB table
      62        9097 :     const FT_Byte* pGsubLimit = pGsubBase + pTTFile->tlens[ O_gsub ];
      63             : 
      64             :     // parse GSUB header
      65        9097 :     const FT_Byte* pGsubHeader = pGsubBase;
      66        9097 :     const sal_uLong nVersion            = NEXT_Long( pGsubHeader );
      67        9097 :     const sal_uInt16 nOfsScriptList     = NEXT_UShort( pGsubHeader );
      68        9097 :     const sal_uInt16 nOfsFeatureTable   = NEXT_UShort( pGsubHeader );
      69        9097 :     const sal_uInt16 nOfsLookupList     = NEXT_UShort( pGsubHeader );
      70             : 
      71             :     // sanity check the GSUB header
      72        9097 :     if( nVersion != 0x00010000 )
      73           0 :         if( nVersion != 0x00001000 )    // workaround for SunBatang etc.
      74           0 :             return false;               // unknown format or broken
      75             : 
      76             :     typedef std::vector<sal_uLong> ReqFeatureTagList;
      77        9097 :     ReqFeatureTagList aReqFeatureTagList;
      78             : 
      79        9097 :     aReqFeatureTagList.push_back( MKTAG("vert") );
      80             : 
      81             :     typedef std::vector<sal_uInt16> UshortList;
      82       18194 :     UshortList aFeatureIndexList;
      83             : 
      84             :     // parse Script Table
      85        9097 :     const FT_Byte* pScriptHeader = pGsubBase + nOfsScriptList;
      86        9097 :     const sal_uInt16 nCntScript = NEXT_UShort( pScriptHeader );
      87        9097 :     if( pGsubLimit < pScriptHeader + 6 * nCntScript )
      88           0 :         return false;
      89       47110 :     for( sal_uInt16 nScriptIndex = 0; nScriptIndex < nCntScript; ++nScriptIndex )
      90             :     {
      91       38013 :         const sal_uLong nTag            = NEXT_Long( pScriptHeader ); // e.g. hani/arab/kana/hang
      92       38013 :         const sal_uInt16 nOfsScriptTable= NEXT_UShort( pScriptHeader );
      93       38013 :         if( (nTag != (sal_uInt16)nRequestedScript) && (nRequestedScript != 0) )
      94           0 :             continue;
      95             : 
      96       38013 :         const FT_Byte* pScriptTable     = pGsubBase + nOfsScriptList + nOfsScriptTable;
      97       38013 :         if( pGsubLimit < pScriptTable + 4 )
      98           0 :             return false;
      99       38013 :         const sal_uInt16 nDefaultLangsysOfs = NEXT_UShort( pScriptTable );
     100       38013 :         const sal_uInt16 nCntLangSystem     = NEXT_UShort( pScriptTable );
     101       38013 :         sal_uInt16 nLangsysOffset = 0;
     102       38013 :         if( pGsubLimit < pScriptTable + 6 * nCntLangSystem )
     103           0 :             return false;
     104       38013 :         for( sal_uInt16 nLangsysIndex = 0; nLangsysIndex < nCntLangSystem; ++nLangsysIndex )
     105             :         {
     106       10512 :             const sal_uLong nInnerTag = NEXT_Long( pScriptTable );    // e.g. KOR/ZHS/ZHT/JAN
     107       10512 :             const sal_uInt16 nOffset= NEXT_UShort( pScriptTable );
     108       10512 :             if( (nInnerTag != (sal_uInt16)nRequestedLangsys) && (nRequestedLangsys != 0) )
     109           0 :                 continue;
     110       10512 :             nLangsysOffset = nOffset;
     111       10512 :             break;
     112             :         }
     113             : 
     114       38013 :         if( (nDefaultLangsysOfs != 0) && (nDefaultLangsysOfs != nLangsysOffset) )
     115             :         {
     116       35836 :             const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nDefaultLangsysOfs;
     117       35836 :             if( pGsubLimit < pLangSys + 6 )
     118           0 :                 return false;
     119       35836 :             /*const sal_uInt16 nLookupOrder   =*/ NEXT_UShort( pLangSys );
     120       35836 :             const sal_uInt16 nReqFeatureIdx = NEXT_UShort( pLangSys );
     121       35836 :             const sal_uInt16 nCntFeature    = NEXT_UShort( pLangSys );
     122       35836 :             if( pGsubLimit < pLangSys + 2 * nCntFeature )
     123           0 :                 return false;
     124       35836 :             aFeatureIndexList.push_back( nReqFeatureIdx );
     125      352704 :             for( sal_uInt16 i = 0; i < nCntFeature; ++i )
     126             :             {
     127      316868 :                 const sal_uInt16 nFeatureIndex = NEXT_UShort( pLangSys );
     128      316868 :                 aFeatureIndexList.push_back( nFeatureIndex );
     129             :             }
     130             :         }
     131             : 
     132       38013 :         if( nLangsysOffset != 0 )
     133             :         {
     134       10512 :             const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nLangsysOffset;
     135       10512 :             if( pGsubLimit < pLangSys + 6 )
     136           0 :                 return false;
     137       10512 :             /*const sal_uInt16 nLookupOrder   =*/ NEXT_UShort( pLangSys );
     138       10512 :             const sal_uInt16 nReqFeatureIdx = NEXT_UShort( pLangSys );
     139       10512 :             const sal_uInt16 nCntFeature    = NEXT_UShort( pLangSys );
     140       10512 :             if( pGsubLimit < pLangSys + 2 * nCntFeature )
     141           0 :                 return false;
     142       10512 :             aFeatureIndexList.push_back( nReqFeatureIdx );
     143      109716 :             for( sal_uInt16 i = 0; i < nCntFeature; ++i )
     144             :             {
     145       99204 :                 const sal_uInt16 nFeatureIndex = NEXT_UShort( pLangSys );
     146       99204 :                 aFeatureIndexList.push_back( nFeatureIndex );
     147             :             }
     148             :         }
     149             :     }
     150             : 
     151        9097 :     if( aFeatureIndexList.empty() )
     152           0 :         return true;
     153             : 
     154       18194 :     UshortList aLookupIndexList;
     155       18194 :     UshortList aLookupOffsetList;
     156             : 
     157             :     // parse Feature Table
     158        9097 :     const FT_Byte* pFeatureHeader = pGsubBase + nOfsFeatureTable;
     159        9097 :     if( pGsubLimit < pFeatureHeader + 2 )
     160           0 :           return false;
     161        9097 :     const sal_uInt16 nCntFeature = NEXT_UShort( pFeatureHeader );
     162        9097 :     if( pGsubLimit < pFeatureHeader + 6 * nCntFeature )
     163           0 :           return false;
     164      305981 :     for( sal_uInt16 nFeatureIndex = 0; nFeatureIndex < nCntFeature; ++nFeatureIndex )
     165             :     {
     166      296884 :         const sal_uLong nTag    = NEXT_Long( pFeatureHeader ); // e.g. locl/vert/trad/smpl/liga/fina/...
     167      296884 :         const sal_uInt16 nOffset= NEXT_UShort( pFeatureHeader );
     168             : 
     169             :         // ignore unneeded feature lookups
     170      296884 :         if( aFeatureIndexList[0] != nFeatureIndex ) // do not ignore the required feature
     171             :         {
     172      296642 :             const int nRequested = std::count( aFeatureIndexList.begin(), aFeatureIndexList.end(), nFeatureIndex);
     173      296642 :             if( !nRequested )   // ignore features that are not requested
     174      382544 :                 continue;
     175      210740 :             const int nAvailable = std::count( aReqFeatureTagList.begin(), aReqFeatureTagList.end(), nTag);
     176      210740 :             if( !nAvailable )   // some fonts don't provide features they request!
     177      210740 :                 continue;
     178             :         }
     179             : 
     180         242 :         const FT_Byte* pFeatureTable = pGsubBase + nOfsFeatureTable + nOffset;
     181         242 :         if( pGsubLimit < pFeatureTable + 2 )
     182           0 :             return false;
     183         242 :         const sal_uInt16 nCntLookups = NEXT_UShort( pFeatureTable );
     184         242 :         if( pGsubLimit < pFeatureTable + 2 * nCntLookups )
     185           0 :             return false;
     186         242 :         for( sal_uInt16 i = 0; i < nCntLookups; ++i )
     187             :         {
     188           0 :             const sal_uInt16 nLookupIndex = NEXT_UShort( pFeatureTable );
     189           0 :             aLookupIndexList.push_back( nLookupIndex );
     190             :         }
     191         242 :         if( nCntLookups == 0 ) //### hack needed by Mincho/Gothic/Mingliu/Simsun/...
     192         242 :             aLookupIndexList.push_back( 0 );
     193             :     }
     194             : 
     195             :     // parse Lookup List
     196        9097 :     const FT_Byte* pLookupHeader = pGsubBase + nOfsLookupList;
     197        9097 :     if( pGsubLimit < pLookupHeader + 2 )
     198           0 :         return false;
     199        9097 :     const sal_uInt16 nCntLookupTable = NEXT_UShort( pLookupHeader );
     200        9097 :     if( pGsubLimit < pLookupHeader + 2 * nCntLookupTable )
     201           0 :         return false;
     202      175359 :     for( sal_uInt16 nLookupIdx = 0; nLookupIdx < nCntLookupTable; ++nLookupIdx )
     203             :     {
     204      166262 :         const sal_uInt16 nOffset = NEXT_UShort( pLookupHeader );
     205      166262 :         if( std::count( aLookupIndexList.begin(), aLookupIndexList.end(), nLookupIdx ) )
     206         242 :             aLookupOffsetList.push_back( nOffset );
     207             :     }
     208             : 
     209        9097 :     UshortList::const_iterator it = aLookupOffsetList.begin();
     210        9339 :     for(; it != aLookupOffsetList.end(); ++it )
     211             :     {
     212         242 :         const sal_uInt16 nOfsLookupTable = *it;
     213         242 :         const FT_Byte* pLookupTable = pGsubBase + nOfsLookupList + nOfsLookupTable;
     214         242 :         if( pGsubLimit < pLookupTable + 6 )
     215           0 :             return false;
     216         242 :         const sal_uInt16 eLookupType        = NEXT_UShort( pLookupTable );
     217         242 :         /*const sal_uInt16 eLookupFlag        =*/ NEXT_UShort( pLookupTable );
     218         242 :         const sal_uInt16 nCntLookupSubtable = NEXT_UShort( pLookupTable );
     219             : 
     220             :         // TODO: switch( eLookupType )
     221         242 :         if( eLookupType != 1 )  // TODO: once we go beyond SingleSubst
     222         242 :             continue;
     223             : 
     224           0 :         if( pGsubLimit < pLookupTable + 2 * nCntLookupSubtable )
     225           0 :             return false;
     226           0 :         for( sal_uInt16 nSubTableIdx = 0; nSubTableIdx < nCntLookupSubtable; ++nSubTableIdx )
     227             :         {
     228           0 :             const sal_uInt16 nOfsSubLookupTable = NEXT_UShort( pLookupTable );
     229           0 :             const FT_Byte* pSubLookup = pGsubBase + nOfsLookupList + nOfsLookupTable + nOfsSubLookupTable;
     230           0 :             if( pGsubLimit < pSubLookup + 6 )
     231           0 :                 return false;
     232           0 :             const sal_uInt16 nFmtSubstitution   = NEXT_UShort( pSubLookup );
     233           0 :             const sal_uInt16 nOfsCoverage       = NEXT_UShort( pSubLookup );
     234             : 
     235             :             typedef std::pair<sal_uInt16,sal_uInt16> GlyphSubst;
     236             :             typedef std::vector<GlyphSubst> SubstVector;
     237           0 :             SubstVector aSubstVector;
     238             : 
     239             :             const FT_Byte* pCoverage    = pGsubBase
     240           0 :                 + nOfsLookupList + nOfsLookupTable + nOfsSubLookupTable + nOfsCoverage;
     241           0 :             if( pGsubLimit < pCoverage + 4 )
     242           0 :                 return false;
     243           0 :             const sal_uInt16 nFmtCoverage   = NEXT_UShort( pCoverage );
     244           0 :             switch( nFmtCoverage )
     245             :             {
     246             :                 case 1:         // Coverage Format 1
     247             :                 {
     248           0 :                     const sal_uInt16 nCntGlyph = NEXT_UShort( pCoverage );
     249           0 :                     if( pGsubLimit < pCoverage + 2 * nCntGlyph )
     250             :                         // TODO? nCntGlyph = (pGsubLimit - pCoverage) / 2;
     251           0 :                         return false;
     252           0 :                     aSubstVector.reserve( nCntGlyph );
     253           0 :                     for( sal_uInt16 i = 0; i < nCntGlyph; ++i )
     254             :                     {
     255           0 :                         const sal_uInt16 nGlyphId = NEXT_UShort( pCoverage );
     256           0 :                         aSubstVector.push_back( GlyphSubst( nGlyphId, 0 ) );
     257             :                     }
     258             :                 }
     259           0 :                 break;
     260             : 
     261             :                 case 2:         // Coverage Format 2
     262             :                 {
     263           0 :                     const sal_uInt16 nCntRange = NEXT_UShort( pCoverage );
     264           0 :                     if( pGsubLimit < pCoverage + 6 * nCntRange )
     265             :                         // TODO? nCntGlyph = (pGsubLimit - pCoverage) / 6;
     266           0 :                         return false;
     267           0 :                     for( int i = nCntRange; --i >= 0; )
     268             :                     {
     269           0 :                         const sal_uInt32 nGlyph0 = NEXT_UShort( pCoverage );
     270           0 :                         const sal_uInt32 nGlyph1 = NEXT_UShort( pCoverage );
     271           0 :                         const sal_uInt16 nCovIdx = NEXT_UShort( pCoverage );
     272           0 :                         for( sal_uInt32 j = nGlyph0; j <= nGlyph1; ++j )
     273           0 :                             aSubstVector.push_back( GlyphSubst( static_cast<sal_uInt16>(j + nCovIdx), 0 ) );
     274             :                     }
     275             :                 }
     276           0 :                 break;
     277             :             }
     278             : 
     279           0 :             SubstVector::iterator subst_it( aSubstVector.begin() );
     280             : 
     281           0 :             switch( nFmtSubstitution )
     282             :             {
     283             :                 case 1:     // Single Substitution Format 1
     284             :                 {
     285           0 :                     const sal_uInt16 nDeltaGlyphId = NEXT_UShort( pSubLookup );
     286             : 
     287           0 :                     for(; subst_it != aSubstVector.end(); ++subst_it )
     288           0 :                         (*subst_it).second = (*subst_it).first + nDeltaGlyphId;
     289             :                 }
     290           0 :                 break;
     291             : 
     292             :                 case 2:     // Single Substitution Format 2
     293             :                 {
     294           0 :                     const sal_uInt16 nCntGlyph = NEXT_UShort( pSubLookup );
     295           0 :                     for( int i = nCntGlyph; (subst_it != aSubstVector.end()) && (--i>=0); ++subst_it )
     296             :                     {
     297           0 :                         if( pGsubLimit < pSubLookup + 2 )
     298           0 :                             return false;
     299           0 :                         const sal_uInt16 nGlyphId = NEXT_UShort( pSubLookup );
     300           0 :                         (*subst_it).second = nGlyphId;
     301             :                     }
     302             :                 }
     303           0 :                 break;
     304             :             }
     305             : 
     306             :             // now apply the glyph substitutions that have been collected in this subtable
     307           0 :             if( !aSubstVector.empty() )
     308             :             {
     309           0 :                 GlyphSubstitution* pGSubstitution = new GlyphSubstitution;
     310           0 :                 pTTFile->pGSubstitution = static_cast<void*>(pGSubstitution);
     311           0 :                 for( subst_it = aSubstVector.begin(); subst_it != aSubstVector.end(); ++subst_it )
     312           0 :                     (*pGSubstitution)[ (*subst_it).first ] =  (*subst_it).second;
     313             :             }
     314           0 :         }
     315             :     }
     316       18194 :     return true;
     317             : }
     318             : 
     319       10257 : void ReleaseGSUB(struct _TrueTypeFont* pTTFile)
     320             : {
     321       10257 :     GlyphSubstitution* pGlyphSubstitution = static_cast<GlyphSubstitution*>(pTTFile->pGSubstitution);
     322       10257 :     if( pGlyphSubstitution )
     323           0 :         delete pGlyphSubstitution;
     324       10257 : }
     325             : 
     326           0 : int UseGSUB( struct _TrueTypeFont* pTTFile, int nGlyph )
     327             : {
     328           0 :     GlyphSubstitution* pGlyphSubstitution = static_cast<GlyphSubstitution*>(pTTFile->pGSubstitution);
     329           0 :     if( pGlyphSubstitution != 0 )
     330             :     {
     331           0 :         GlyphSubstitution::const_iterator it( pGlyphSubstitution->find( sal::static_int_cast<sal_uInt16>(nGlyph) ) );
     332           0 :         if( it != pGlyphSubstitution->end() )
     333           0 :             nGlyph = (*it).second;
     334             :     }
     335             : 
     336           0 :     return nGlyph;
     337             : }
     338             : 
     339       10244 : int HasVerticalGSUB( struct _TrueTypeFont* pTTFile )
     340             : {
     341       10244 :     GlyphSubstitution* pGlyphSubstitution = static_cast<GlyphSubstitution*>(pTTFile->pGSubstitution);
     342       10244 :     return pGlyphSubstitution ? +1 : 0;
     343             : }
     344             : 
     345             : }
     346             : 
     347             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11