LCOV - code coverage report
Current view: top level - vcl/source/fontsubset - gsub.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 194 0.0 %
Date: 2014-04-14 Functions: 0 6 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "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           0 : inline sal_uInt32 NEXT_Long( const unsigned char* &p )
      39             : {
      40           0 :     sal_uInt32 nVal = (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
      41           0 :     p += 4;
      42           0 :     return nVal;
      43             : }
      44             : 
      45           0 : inline sal_uInt16 NEXT_UShort( const unsigned char* &p )
      46             : {
      47           0 :     sal_uInt16 nVal = (p[0]<<8) + p[1];
      48           0 :     p += 2;
      49           0 :     return nVal;
      50             : }
      51             : 
      52             : #define MKTAG(s) ((((((s[0]<<8)+s[1])<<8)+s[2])<<8)+s[3])
      53             : 
      54           0 : bool ReadGSUB( struct _TrueTypeFont* pTTFile,
      55             :     int nRequestedScript, int nRequestedLangsys )
      56             : {
      57           0 :     const FT_Byte* pGsubBase = (FT_Byte*)pTTFile->tables[ O_gsub ];
      58           0 :     if( !pGsubBase )
      59           0 :         return false;
      60             : 
      61             :     // #129682# check offsets inside GSUB table
      62           0 :     const FT_Byte* pGsubLimit = pGsubBase + pTTFile->tlens[ O_gsub ];
      63             : 
      64             :     // parse GSUB header
      65           0 :     const FT_Byte* pGsubHeader = pGsubBase;
      66           0 :     const sal_uLong nVersion            = NEXT_Long( pGsubHeader );
      67           0 :     const sal_uInt16 nOfsScriptList     = NEXT_UShort( pGsubHeader );
      68           0 :     const sal_uInt16 nOfsFeatureTable   = NEXT_UShort( pGsubHeader );
      69           0 :     const sal_uInt16 nOfsLookupList     = NEXT_UShort( pGsubHeader );
      70             : 
      71             :     // sanity check the GSUB header
      72           0 :     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           0 :     ReqFeatureTagList aReqFeatureTagList;
      78             : 
      79           0 :     aReqFeatureTagList.push_back( MKTAG("vert") );
      80             : 
      81             :     typedef std::vector<sal_uInt16> UshortList;
      82           0 :     UshortList aFeatureIndexList;
      83             : 
      84             :     // parse Script Table
      85           0 :     const FT_Byte* pScriptHeader = pGsubBase + nOfsScriptList;
      86           0 :     const sal_uInt16 nCntScript = NEXT_UShort( pScriptHeader );
      87           0 :     if( pGsubLimit < pScriptHeader + 6 * nCntScript )
      88           0 :         return false;
      89           0 :     for( sal_uInt16 nScriptIndex = 0; nScriptIndex < nCntScript; ++nScriptIndex )
      90             :     {
      91           0 :         const sal_uLong nTag            = NEXT_Long( pScriptHeader ); // e.g. hani/arab/kana/hang
      92           0 :         const sal_uInt16 nOfsScriptTable= NEXT_UShort( pScriptHeader );
      93           0 :         if( (nTag != (sal_uInt16)nRequestedScript) && (nRequestedScript != 0) )
      94           0 :             continue;
      95             : 
      96           0 :         const FT_Byte* pScriptTable     = pGsubBase + nOfsScriptList + nOfsScriptTable;
      97           0 :         if( pGsubLimit < pScriptTable + 4 )
      98           0 :             return false;
      99           0 :         const sal_uInt16 nDefaultLangsysOfs = NEXT_UShort( pScriptTable );
     100           0 :         const sal_uInt16 nCntLangSystem     = NEXT_UShort( pScriptTable );
     101           0 :         sal_uInt16 nLangsysOffset = 0;
     102           0 :         if( pGsubLimit < pScriptTable + 6 * nCntLangSystem )
     103           0 :             return false;
     104           0 :         for( sal_uInt16 nLangsysIndex = 0; nLangsysIndex < nCntLangSystem; ++nLangsysIndex )
     105             :         {
     106           0 :             const sal_uLong nInnerTag = NEXT_Long( pScriptTable );    // e.g. KOR/ZHS/ZHT/JAN
     107           0 :             const sal_uInt16 nOffset= NEXT_UShort( pScriptTable );
     108           0 :             if( (nInnerTag != (sal_uInt16)nRequestedLangsys) && (nRequestedLangsys != 0) )
     109           0 :                 continue;
     110           0 :             nLangsysOffset = nOffset;
     111           0 :             break;
     112             :         }
     113             : 
     114           0 :         if( (nDefaultLangsysOfs != 0) && (nDefaultLangsysOfs != nLangsysOffset) )
     115             :         {
     116           0 :             const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nDefaultLangsysOfs;
     117           0 :             if( pGsubLimit < pLangSys + 6 )
     118           0 :                 return false;
     119           0 :             /*const sal_uInt16 nLookupOrder   =*/ NEXT_UShort( pLangSys );
     120           0 :             const sal_uInt16 nReqFeatureIdx = NEXT_UShort( pLangSys );
     121           0 :             const sal_uInt16 nCntFeature    = NEXT_UShort( pLangSys );
     122           0 :             if( pGsubLimit < pLangSys + 2 * nCntFeature )
     123           0 :                 return false;
     124           0 :             aFeatureIndexList.push_back( nReqFeatureIdx );
     125           0 :             for( sal_uInt16 i = 0; i < nCntFeature; ++i )
     126             :             {
     127           0 :                 const sal_uInt16 nFeatureIndex = NEXT_UShort( pLangSys );
     128           0 :                 aFeatureIndexList.push_back( nFeatureIndex );
     129             :             }
     130             :         }
     131             : 
     132           0 :         if( nLangsysOffset != 0 )
     133             :         {
     134           0 :             const FT_Byte* pLangSys = pGsubBase + nOfsScriptList + nOfsScriptTable + nLangsysOffset;
     135           0 :             if( pGsubLimit < pLangSys + 6 )
     136           0 :                 return false;
     137           0 :             /*const sal_uInt16 nLookupOrder   =*/ NEXT_UShort( pLangSys );
     138           0 :             const sal_uInt16 nReqFeatureIdx = NEXT_UShort( pLangSys );
     139           0 :             const sal_uInt16 nCntFeature    = NEXT_UShort( pLangSys );
     140           0 :             if( pGsubLimit < pLangSys + 2 * nCntFeature )
     141           0 :                 return false;
     142           0 :             aFeatureIndexList.push_back( nReqFeatureIdx );
     143           0 :             for( sal_uInt16 i = 0; i < nCntFeature; ++i )
     144             :             {
     145           0 :                 const sal_uInt16 nFeatureIndex = NEXT_UShort( pLangSys );
     146           0 :                 aFeatureIndexList.push_back( nFeatureIndex );
     147             :             }
     148             :         }
     149             :     }
     150             : 
     151           0 :     if( aFeatureIndexList.empty() )
     152           0 :         return true;
     153             : 
     154           0 :     UshortList aLookupIndexList;
     155           0 :     UshortList aLookupOffsetList;
     156             : 
     157             :     // parse Feature Table
     158           0 :     const FT_Byte* pFeatureHeader = pGsubBase + nOfsFeatureTable;
     159           0 :     if( pGsubLimit < pFeatureHeader + 2 )
     160           0 :           return false;
     161           0 :     const sal_uInt16 nCntFeature = NEXT_UShort( pFeatureHeader );
     162           0 :     if( pGsubLimit < pFeatureHeader + 6 * nCntFeature )
     163           0 :           return false;
     164           0 :     for( sal_uInt16 nFeatureIndex = 0; nFeatureIndex < nCntFeature; ++nFeatureIndex )
     165             :     {
     166           0 :         const sal_uLong nTag    = NEXT_Long( pFeatureHeader ); // e.g. locl/vert/trad/smpl/liga/fina/...
     167           0 :         const sal_uInt16 nOffset= NEXT_UShort( pFeatureHeader );
     168             : 
     169             :         // ignore unneeded feature lookups
     170           0 :         if( aFeatureIndexList[0] != nFeatureIndex ) // do not ignore the required feature
     171             :         {
     172           0 :             const int nRequested = std::count( aFeatureIndexList.begin(), aFeatureIndexList.end(), nFeatureIndex);
     173           0 :             if( !nRequested )   // ignore features that are not requested
     174           0 :                 continue;
     175           0 :             const int nAvailable = std::count( aReqFeatureTagList.begin(), aReqFeatureTagList.end(), nTag);
     176           0 :             if( !nAvailable )   // some fonts don't provide features they request!
     177           0 :                 continue;
     178             :         }
     179             : 
     180           0 :         const FT_Byte* pFeatureTable = pGsubBase + nOfsFeatureTable + nOffset;
     181           0 :         if( pGsubLimit < pFeatureTable + 2 )
     182           0 :             return false;
     183           0 :         const sal_uInt16 nCntLookups = NEXT_UShort( pFeatureTable );
     184           0 :         if( pGsubLimit < pFeatureTable + 2 * nCntLookups )
     185           0 :             return false;
     186           0 :         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           0 :         if( nCntLookups == 0 ) //### hack needed by Mincho/Gothic/Mingliu/Simsun/...
     192           0 :             aLookupIndexList.push_back( 0 );
     193             :     }
     194             : 
     195             :     // parse Lookup List
     196           0 :     const FT_Byte* pLookupHeader = pGsubBase + nOfsLookupList;
     197           0 :     if( pGsubLimit < pLookupHeader + 2 )
     198           0 :         return false;
     199           0 :     const sal_uInt16 nCntLookupTable = NEXT_UShort( pLookupHeader );
     200           0 :     if( pGsubLimit < pLookupHeader + 2 * nCntLookupTable )
     201           0 :         return false;
     202           0 :     for( sal_uInt16 nLookupIdx = 0; nLookupIdx < nCntLookupTable; ++nLookupIdx )
     203             :     {
     204           0 :         const sal_uInt16 nOffset = NEXT_UShort( pLookupHeader );
     205           0 :         if( std::count( aLookupIndexList.begin(), aLookupIndexList.end(), nLookupIdx ) )
     206           0 :             aLookupOffsetList.push_back( nOffset );
     207             :     }
     208             : 
     209           0 :     UshortList::const_iterator it = aLookupOffsetList.begin();
     210           0 :     for(; it != aLookupOffsetList.end(); ++it )
     211             :     {
     212           0 :         const sal_uInt16 nOfsLookupTable = *it;
     213           0 :         const FT_Byte* pLookupTable = pGsubBase + nOfsLookupList + nOfsLookupTable;
     214           0 :         if( pGsubLimit < pLookupTable + 6 )
     215           0 :             return false;
     216           0 :         const sal_uInt16 eLookupType        = NEXT_UShort( pLookupTable );
     217           0 :         /*const sal_uInt16 eLookupFlag        =*/ NEXT_UShort( pLookupTable );
     218           0 :         const sal_uInt16 nCntLookupSubtable = NEXT_UShort( pLookupTable );
     219             : 
     220             :         // TODO: switch( eLookupType )
     221           0 :         if( eLookupType != 1 )  // TODO: once we go beyond SingleSubst
     222           0 :             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 = (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           0 :     return true;
     317             : }
     318             : 
     319           0 : void ReleaseGSUB(struct _TrueTypeFont* pTTFile)
     320             : {
     321           0 :     GlyphSubstitution* pGlyphSubstitution = (GlyphSubstitution*)pTTFile->pGSubstitution;
     322           0 :     if( pGlyphSubstitution )
     323           0 :         delete pGlyphSubstitution;
     324           0 : }
     325             : 
     326           0 : int UseGSUB( struct _TrueTypeFont* pTTFile, int nGlyph )
     327             : {
     328           0 :     GlyphSubstitution* pGlyphSubstitution = (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           0 : int HasVerticalGSUB( struct _TrueTypeFont* pTTFile )
     340             : {
     341           0 :     GlyphSubstitution* pGlyphSubstitution = (GlyphSubstitution*)pTTFile->pGSubstitution;
     342           0 :     return pGlyphSubstitution ? +1 : 0;
     343             : }
     344             : 
     345             : }
     346             : 
     347             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10