LCOV - code coverage report
Current view: top level - gfx/graphite2/src - TtfUtil.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 335 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 20 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*  GRAPHITE2 LICENSING
       2             : 
       3             :     Copyright 2010, SIL International
       4             :     All rights reserved.
       5             : 
       6             :     This library is free software; you can redistribute it and/or modify
       7             :     it under the terms of the GNU Lesser General Public License as published
       8             :     by the Free Software Foundation; either version 2.1 of License, or
       9             :     (at your option) any later version.
      10             : 
      11             :     This program is distributed in the hope that it will be useful,
      12             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :     Lesser General Public License for more details.
      15             : 
      16             :     You should also have received a copy of the GNU Lesser General Public
      17             :     License along with this library in the file named "LICENSE".
      18             :     If not, write to the Free Software Foundation, 51 Franklin Street, 
      19             :     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
      20             :     internet at http://www.fsf.org/licenses/lgpl.html.
      21             : 
      22             : Alternatively, the contents of this file may be used under the terms of the
      23             : Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
      24             : License, as published by the Free Software Foundation, either version 2
      25             : of the License or (at your option) any later version.
      26             : */
      27             : /*--------------------------------------------------------------------*//*:Ignore this sentence.
      28             : 
      29             : File: TtfUtil.cpp
      30             : Responsibility: Alan Ward
      31             : Last reviewed: Not yet.
      32             : 
      33             : Description
      34             :     Implements the methods for TtfUtil class. This file should remain portable to any C++ 
      35             :     environment by only using standard C++ and the TTF structurs defined in Tt.h.
      36             : -------------------------------------------------------------------------------*//*:End Ignore*/
      37             : 
      38             : 
      39             : /***********************************************************************************************
      40             :     Include files
      41             : ***********************************************************************************************/
      42             : // Language headers
      43             : //#include <algorithm>
      44             : #include <cassert>
      45             : #include <cstddef>
      46             : #include <cstring>
      47             : #include <climits>
      48             : #include <cwchar>
      49             : //#include <stdexcept>
      50             : // Platform headers
      51             : // Module headers
      52             : #include "inc/TtfUtil.h"
      53             : #include "inc/TtfTypes.h"
      54             : #include "inc/Endian.h"
      55             : 
      56             : /***********************************************************************************************
      57             :     Forward declarations
      58             : ***********************************************************************************************/
      59             : 
      60             : /***********************************************************************************************
      61             :     Local Constants and static variables
      62             : ***********************************************************************************************/
      63             : namespace 
      64             : {
      65             : #ifdef ALL_TTFUTILS
      66             :     // max number of components allowed in composite glyphs
      67             :     const int kMaxGlyphComponents = 8;
      68             : #endif
      69             : 
      70             :     template <int R, typename T>
      71             :     inline float fixed_to_float(const T f) {
      72             :         return float(f)/float(2^R);
      73             :     }
      74             : 
      75             : /*----------------------------------------------------------------------------------------------
      76             :     Table of standard Postscript glyph names. From Martin Hosken. Disagress with ttfdump.exe
      77             : ---------------------------------------------------------------------------------------------*/
      78             : #ifdef ALL_TTFUTILS
      79             :     const int kcPostNames = 258;
      80             : 
      81             :     const char * rgPostName[kcPostNames] = {
      82             :         ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", 
      83             :         "dollar", "percent", "ampersand", "quotesingle", "parenleft", 
      84             :         "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash", 
      85             :         "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", 
      86             :         "nine", "colon", "semicolon", "less", "equal", "greater", "question", 
      87             :         "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 
      88             :         "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", 
      89             :         "bracketleft", "backslash", "bracketright", "asciicircum", 
      90             :         "underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", 
      91             :         "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", 
      92             :         "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", 
      93             :         "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", 
      94             :         "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", 
      95             :         "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", 
      96             :         "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", 
      97             :         "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", 
      98             :         "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", 
      99             :         "section", "bullet", "paragraph", "germandbls", "registered", 
     100             :         "copyright", "trademark", "acute", "dieresis", "notequal", "AE", 
     101             :         "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", 
     102             :         "mu", "partialdiff", "summation", "product", "pi", "integral", 
     103             :         "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", 
     104             :         "exclamdown", "logicalnot", "radical", "florin", "approxequal", 
     105             :         "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", 
     106             :         "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", 
     107             :         "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", 
     108             :         "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", 
     109             :         "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", 
     110             :         "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", 
     111             :         "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", 
     112             :         "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", 
     113             :         "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", 
     114             :         "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", 
     115             :         "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", 
     116             :         "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", 
     117             :         "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", 
     118             :         "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", 
     119             :         "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", 
     120             :         "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", 
     121             :         "dcroat" };
     122             : #endif
     123             : 
     124             : } // end of namespace
     125             : 
     126             : /***********************************************************************************************
     127             :     Methods
     128             : ***********************************************************************************************/
     129             : 
     130             : /* Note on error processing: The code guards against bad glyph ids being used to look up data
     131             : in open ended tables (loca, hmtx). If the glyph id comes from a cmap this shouldn't happen 
     132             : but it seems prudent to check for user errors here. The code does assume that data obtained 
     133             : from the TTF file is valid otherwise (though the CheckTable method seeks to check for 
     134             : obvious problems that might accompany a change in table versions). For example an invalid 
     135             : offset in the loca table which could exceed the size of the glyf table is NOT trapped.
     136             : Likewise if numberOf_LongHorMetrics in the hhea table is wrong, this will NOT be trapped, 
     137             : which could cause a lookup in the hmtx table to exceed the table length. Of course, TTF tables
     138             : that are completely corrupt will cause unpredictable results. */
     139             : 
     140             : /* Note on composite glyphs: Glyphs that have components that are themselves composites
     141             : are not supported. IsDeepComposite can be used to test for this. False is returned from many 
     142             : of the methods in this cases. It is unclear how to build composite glyphs in some cases, 
     143             : so this code represents my best guess until test cases can be found. See notes on the high-
     144             : level GlyfPoints method. */
     145             : namespace graphite2
     146             : {
     147             : namespace TtfUtil
     148             : {
     149             : 
     150             : 
     151             : /*----------------------------------------------------------------------------------------------
     152             :     Get offset and size of the offset table needed to find table directory.
     153             :     Return true if success, false otherwise.
     154             :     lSize excludes any table directory entries.
     155             : ----------------------------------------------------------------------------------------------*/
     156           0 : bool GetHeaderInfo(size_t & lOffset, size_t & lSize)
     157             : {
     158           0 :     lOffset = 0;
     159           0 :     lSize   = offsetof(Sfnt::OffsetSubTable, table_directory);
     160           0 :     assert(sizeof(uint32) + 4*sizeof (uint16) == lSize);
     161           0 :     return true;
     162             : }
     163             : 
     164             : /*----------------------------------------------------------------------------------------------
     165             :     Check the offset table for expected data.
     166             :     Return true if success, false otherwise.
     167             : ----------------------------------------------------------------------------------------------*/
     168           0 : bool CheckHeader(const void * pHdr)
     169             : {
     170             :     const Sfnt::OffsetSubTable * pOffsetTable  
     171           0 :         = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr);
     172             : 
     173           0 :     return pHdr && be::swap(pOffsetTable->scaler_type) == Sfnt::OffsetSubTable::TrueTypeWin;
     174             : }
     175             : 
     176             : /*----------------------------------------------------------------------------------------------
     177             :     Get offset and size of the table directory.
     178             :     Return true if successful, false otherwise.
     179             : ----------------------------------------------------------------------------------------------*/
     180           0 : bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize)
     181             : {
     182             :     const Sfnt::OffsetSubTable * pOffsetTable
     183           0 :         = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr);
     184             : 
     185           0 :     lOffset = offsetof(Sfnt::OffsetSubTable, table_directory);
     186           0 :     lSize   = be::swap(pOffsetTable->num_tables)
     187           0 :         * sizeof(Sfnt::OffsetSubTable::Entry);
     188             :     
     189           0 :     return true;
     190             : }
     191             : 
     192             : 
     193             : /*----------------------------------------------------------------------------------------------
     194             :     Get offset and size of the specified table.
     195             :     Return true if successful, false otherwise. On false, offset and size will be 0.
     196             : ----------------------------------------------------------------------------------------------*/
     197           0 : bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir,
     198             :                            size_t & lOffset, size_t & lSize)
     199             : {
     200             :     const Sfnt::OffsetSubTable * pOffsetTable 
     201           0 :         = reinterpret_cast<const Sfnt::OffsetSubTable *>(pHdr);
     202           0 :     const size_t num_tables = be::swap(pOffsetTable->num_tables);
     203             :     const Sfnt::OffsetSubTable::Entry 
     204             :         * entry_itr = reinterpret_cast<const Sfnt::OffsetSubTable::Entry *>(
     205           0 :             pTableDir),
     206           0 :         * const  dir_end = entry_itr + num_tables;
     207             : 
     208           0 :     if (num_tables > 40)
     209           0 :         return false;
     210             : 
     211           0 :     for (;entry_itr != dir_end; ++entry_itr) // 40 - safe guard
     212             :     {
     213           0 :         if (be::swap(entry_itr->tag) == TableTag)
     214             :         {
     215           0 :             lOffset = be::swap(entry_itr->offset);
     216           0 :             lSize   = be::swap(entry_itr->length);
     217           0 :             return true;
     218             :         }
     219             :     }
     220             : 
     221           0 :     return false;
     222             : }
     223             : 
     224             : /*----------------------------------------------------------------------------------------------
     225             :     Check the specified table. Tests depend on the table type.
     226             :     Return true if successful, false otherwise.
     227             : ----------------------------------------------------------------------------------------------*/
     228           0 : bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize)
     229             : {
     230             :     using namespace Sfnt;
     231             :     
     232           0 :     if (pTable == 0 || lTableSize < 4) return false;
     233             : 
     234           0 :     switch(TableId)
     235             :     {
     236             :     case Tag::cmap: // cmap
     237             :     {
     238             :         const Sfnt::CharacterCodeMap * const pCmap 
     239           0 :             = reinterpret_cast<const Sfnt::CharacterCodeMap *>(pTable);
     240           0 :         if (lTableSize < sizeof(Sfnt::CharacterCodeMap))
     241           0 :             return false;
     242           0 :         return be::swap(pCmap->version) == 0;
     243             :     }
     244             : 
     245             :     case Tag::head: // head
     246             :     {
     247             :         const Sfnt::FontHeader * const pHead 
     248           0 :             = reinterpret_cast<const Sfnt::FontHeader *>(pTable);
     249           0 :         if (lTableSize < sizeof(Sfnt::FontHeader))
     250           0 :             return false;
     251           0 :         bool r = be::swap(pHead->version) == OneFix
     252           0 :             && be::swap(pHead->magic_number) == FontHeader::MagicNumber
     253           0 :             && be::swap(pHead->glyph_data_format)
     254             :                     == FontHeader::GlypDataFormat 
     255           0 :             && (be::swap(pHead->index_to_loc_format)
     256             :                     == FontHeader::ShortIndexLocFormat 
     257           0 :                 || be::swap(pHead->index_to_loc_format)
     258             :                     == FontHeader::LongIndexLocFormat) 
     259           0 :             && sizeof(FontHeader) <= lTableSize;
     260           0 :         return r;
     261             :     }
     262             : 
     263             :     case Tag::post: // post
     264             :     {
     265             :         const Sfnt::PostScriptGlyphName * const pPost 
     266           0 :             = reinterpret_cast<const Sfnt::PostScriptGlyphName *>(pTable);
     267           0 :         if (lTableSize < sizeof(Sfnt::PostScriptGlyphName))
     268           0 :             return false;
     269           0 :         const fixed format = be::swap(pPost->format);
     270             :         bool r = format == PostScriptGlyphName::Format1 
     271           0 :             || format == PostScriptGlyphName::Format2 
     272           0 :             || format == PostScriptGlyphName::Format3 
     273           0 :             || format == PostScriptGlyphName::Format25;
     274           0 :         return r;
     275             :     }
     276             : 
     277             :     case Tag::hhea: // hhea
     278             :     {
     279             :         const Sfnt::HorizontalHeader * pHhea = 
     280           0 :             reinterpret_cast<const Sfnt::HorizontalHeader *>(pTable);
     281           0 :         if (lTableSize < sizeof(Sfnt::HorizontalHeader))
     282           0 :             return false;
     283           0 :         bool r = be::swap(pHhea->version) == OneFix
     284           0 :             && be::swap(pHhea->metric_data_format) == 0
     285           0 :             && sizeof (Sfnt::HorizontalHeader) <= lTableSize;
     286           0 :         return r;
     287             :     }
     288             : 
     289             :     case Tag::maxp: // maxp
     290             :     {
     291             :         const Sfnt::MaximumProfile * pMaxp = 
     292           0 :             reinterpret_cast<const Sfnt::MaximumProfile *>(pTable);
     293           0 :         if (lTableSize < sizeof(Sfnt::MaximumProfile))
     294           0 :             return false;
     295           0 :         bool r = be::swap(pMaxp->version) == OneFix
     296           0 :             && sizeof(Sfnt::MaximumProfile) <= lTableSize;
     297           0 :         return r;
     298             :     }
     299             : 
     300             :     case Tag::OS_2: // OS/2
     301             :     {
     302             :         const Sfnt::Compatibility * pOs2 
     303           0 :             = reinterpret_cast<const Sfnt::Compatibility *>(pTable);
     304           0 :         if (be::swap(pOs2->version) == 0)
     305             :         { // OS/2 table version 1 size
     306             : //          if (sizeof(Sfnt::Compatibility) 
     307             : //                  - sizeof(uint32)*2 - sizeof(int16)*2 
     308             : //                  - sizeof(uint16)*3 <= lTableSize)
     309           0 :             if (sizeof(Sfnt::Compatibility0) <= lTableSize)
     310           0 :                 return true;
     311             :         }
     312           0 :         else if (be::swap(pOs2->version) == 1)
     313             :         { // OS/2 table version 2 size
     314             : //          if (sizeof(Sfnt::Compatibility) 
     315             : //                  - sizeof(int16) *2 
     316             : //                  - sizeof(uint16)*3 <= lTableSize)
     317           0 :             if (sizeof(Sfnt::Compatibility1) <= lTableSize)
     318           0 :                 return true;
     319             :         }
     320           0 :         else if (be::swap(pOs2->version) == 2)
     321             :         { // OS/2 table version 3 size
     322           0 :             if (sizeof(Sfnt::Compatibility2) <= lTableSize)
     323           0 :                 return true;
     324             :         }
     325           0 :         else if (be::swap(pOs2->version) == 3 || be::swap(pOs2->version) == 4)
     326             :         { // OS/2 table version 4 size - version 4 changed the meaning of some fields which we don't use
     327           0 :             if (sizeof(Sfnt::Compatibility3) <= lTableSize)
     328           0 :                 return true;
     329             :         }
     330             :         else
     331           0 :             return false;
     332           0 :         break;
     333             :     }
     334             : 
     335             :     case Tag::name:
     336             :     {
     337             :         const Sfnt::FontNames * pName 
     338           0 :             = reinterpret_cast<const Sfnt::FontNames *>(pTable);
     339           0 :         if (lTableSize < sizeof(Sfnt::FontNames))
     340           0 :             return false;
     341           0 :         return be::swap(pName->format) == 0;
     342             :     }
     343             : 
     344             :     default:
     345           0 :         break;
     346             :     }
     347             : 
     348           0 :     return true;
     349             : }
     350             : 
     351             : /*----------------------------------------------------------------------------------------------
     352             :     Return the number of glyphs in the font. Should never be less than zero.
     353             : 
     354             :     Note: this method is not currently used by the Graphite engine.
     355             : ----------------------------------------------------------------------------------------------*/
     356           0 : size_t GlyphCount(const void * pMaxp)
     357             : {
     358             :     const Sfnt::MaximumProfile * pTable = 
     359           0 :             reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp);
     360           0 :     return be::swap(pTable->num_glyphs);
     361             : }
     362             : 
     363             : #ifdef ALL_TTFUTILS
     364             : /*----------------------------------------------------------------------------------------------
     365             :     Return the maximum number of components for any composite glyph in the font.
     366             : 
     367             :     Note: this method is not currently used by the Graphite engine.
     368             : ----------------------------------------------------------------------------------------------*/
     369             : size_t  MaxCompositeComponentCount(const void * pMaxp)
     370             : {
     371             :     const Sfnt::MaximumProfile * pTable = 
     372             :             reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp);
     373             :     return be::swap(pTable->max_component_elements);
     374             : }
     375             : 
     376             : /*----------------------------------------------------------------------------------------------
     377             :     Composite glyphs can be composed of glyphs that are themselves composites.
     378             :     This method returns the maximum number of levels like this for any glyph in the font.
     379             :     A non-composite glyph has a level of 1.
     380             : 
     381             :     Note: this method is not currently used by the Graphite engine.
     382             : ----------------------------------------------------------------------------------------------*/
     383             : size_t  MaxCompositeLevelCount(const void * pMaxp)
     384             : {
     385             :     const Sfnt::MaximumProfile * pTable = 
     386             :             reinterpret_cast<const Sfnt::MaximumProfile *>(pMaxp);
     387             :     return be::swap(pTable->max_component_depth);
     388             : }
     389             : 
     390             : /*----------------------------------------------------------------------------------------------
     391             :     Return the number of glyphs in the font according to a differt source.
     392             :     Should never be less than zero. Return -1 on failure.
     393             : 
     394             :     Note: this method is not currently used by the Graphite engine.
     395             : ----------------------------------------------------------------------------------------------*/
     396             : size_t LocaGlyphCount(size_t lLocaSize, const void * pHead) //throw(std::domain_error)
     397             : {
     398             : 
     399             :     const Sfnt::FontHeader * pTable 
     400             :         = reinterpret_cast<const Sfnt::FontHeader *>(pHead);
     401             : 
     402             :     if (be::swap(pTable->index_to_loc_format)
     403             :         == Sfnt::FontHeader::ShortIndexLocFormat)
     404             :     // loca entries are two bytes and have been divided by two
     405             :         return (lLocaSize >> 1) - 1;
     406             :     
     407             :     if (be::swap(pTable->index_to_loc_format)
     408             :         == Sfnt::FontHeader::LongIndexLocFormat)
     409             :      // loca entries are four bytes
     410             :         return (lLocaSize >> 2) - 1;
     411             : 
     412             :     return -1;
     413             :     //throw std::domain_error("head table in inconsistent state. The font may be corrupted");
     414             : }
     415             : #endif
     416             : 
     417             : /*----------------------------------------------------------------------------------------------
     418             :     Return the design units the font is designed with
     419             : ----------------------------------------------------------------------------------------------*/
     420           0 : int DesignUnits(const void * pHead)
     421             : {
     422             :     const Sfnt::FontHeader * pTable = 
     423           0 :             reinterpret_cast<const Sfnt::FontHeader *>(pHead);
     424             :     
     425           0 :     return be::swap(pTable->units_per_em);
     426             : }
     427             : 
     428             : #ifdef ALL_TTFUTILS
     429             : /*----------------------------------------------------------------------------------------------
     430             :     Return the checksum from the head table, which serves as a unique identifer for the font.
     431             : ----------------------------------------------------------------------------------------------*/
     432             : int HeadTableCheckSum(const void * pHead)
     433             : {
     434             :     const Sfnt::FontHeader * pTable = 
     435             :             reinterpret_cast<const Sfnt::FontHeader *>(pHead);
     436             :     
     437             :     return be::swap(pTable->check_sum_adjustment);
     438             : }
     439             : 
     440             : /*----------------------------------------------------------------------------------------------
     441             :     Return the create time from the head table. This consists of a 64-bit integer, which
     442             :     we return here as two 32-bit integers.
     443             : 
     444             :     Note: this method is not currently used by the Graphite engine.
     445             : ----------------------------------------------------------------------------------------------*/
     446             : void HeadTableCreateTime(const void * pHead,
     447             :     unsigned int * pnDateBC, unsigned int * pnDateAD)
     448             : {
     449             :     const Sfnt::FontHeader * pTable = 
     450             :             reinterpret_cast<const Sfnt::FontHeader *>(pHead);
     451             :     
     452             :     *pnDateBC = be::swap(pTable->created[0]);
     453             :     *pnDateAD = be::swap(pTable->created[1]);
     454             : }
     455             : 
     456             : /*----------------------------------------------------------------------------------------------
     457             :     Return the modify time from the head table.This consists of a 64-bit integer, which
     458             :     we return here as two 32-bit integers.
     459             : 
     460             :     Note: this method is not currently used by the Graphite engine.
     461             : ----------------------------------------------------------------------------------------------*/
     462             : void HeadTableModifyTime(const void * pHead,
     463             :     unsigned int * pnDateBC, unsigned int *pnDateAD)
     464             : {
     465             :     const Sfnt::FontHeader * pTable = 
     466             :             reinterpret_cast<const Sfnt::FontHeader *>(pHead);
     467             :     
     468             :     *pnDateBC = be::swap(pTable->modified[0]);
     469             :     *pnDateAD = be::swap(pTable->modified[1]);
     470             : }
     471             : 
     472             : /*----------------------------------------------------------------------------------------------
     473             :     Return true if the font is italic.
     474             : ----------------------------------------------------------------------------------------------*/
     475             : bool IsItalic(const void * pHead)
     476             : {
     477             :     const Sfnt::FontHeader * pTable = 
     478             :             reinterpret_cast<const Sfnt::FontHeader *>(pHead);
     479             : 
     480             :     return ((be::swap(pTable->mac_style) & 0x00000002) != 0);
     481             : }
     482             : 
     483             : /*----------------------------------------------------------------------------------------------
     484             :     Return the ascent for the font
     485             : ----------------------------------------------------------------------------------------------*/
     486             : int FontAscent(const void * pOs2)
     487             : {
     488             :     const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2);
     489             : 
     490             :     return be::swap(pTable->win_ascent);
     491             : }
     492             : 
     493             : /*----------------------------------------------------------------------------------------------
     494             :     Return the descent for the font
     495             : ----------------------------------------------------------------------------------------------*/
     496             : int FontDescent(const void * pOs2)
     497             : {
     498             :     const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2);
     499             : 
     500             :     return be::swap(pTable->win_descent);
     501             : }
     502             : 
     503             : /*----------------------------------------------------------------------------------------------
     504             :     Get the bold and italic style bits.
     505             :     Return true if successful. false otherwise.
     506             :     In addition to checking the OS/2 table, one could also check
     507             :         the head table's macStyle field (overridden by the OS/2 table on Win)
     508             :         the sub-family name in the name table (though this can contain oblique, dark, etc too)
     509             : ----------------------------------------------------------------------------------------------*/
     510             : bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic)
     511             : {
     512             :     const Sfnt::Compatibility * pTable = reinterpret_cast<const Sfnt::Compatibility *>(pOs2);
     513             : 
     514             :     fBold = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Bold) != 0;
     515             :     fItalic = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Italic) != 0;
     516             :     
     517             :     return true;
     518             : }
     519             : #endif
     520             : 
     521             : /*----------------------------------------------------------------------------------------------
     522             :     Method for searching name table.
     523             : ----------------------------------------------------------------------------------------------*/
     524           0 : bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId,
     525             :         int nLangId, int nNameId, size_t & lOffset, size_t & lSize)
     526             : {
     527           0 :     lOffset = 0;
     528           0 :     lSize = 0;
     529             : 
     530           0 :     const Sfnt::FontNames * pTable = reinterpret_cast<const Sfnt::FontNames *>(pName);
     531           0 :     uint16 cRecord = be::swap(pTable->count);
     532           0 :     uint16 nRecordOffset = be::swap(pTable->string_offset);
     533           0 :     const Sfnt::NameRecord * pRecord = reinterpret_cast<const Sfnt::NameRecord *>(pTable + 1);
     534             : 
     535           0 :     for (int i = 0; i < cRecord; ++i)
     536             :     {
     537           0 :         if (be::swap(pRecord->platform_id) == nPlatformId &&
     538           0 :             be::swap(pRecord->platform_specific_id) == nEncodingId &&
     539           0 :             be::swap(pRecord->language_id) == nLangId &&
     540           0 :             be::swap(pRecord->name_id) == nNameId)
     541             :         {
     542           0 :             lOffset = be::swap(pRecord->offset) + nRecordOffset;
     543           0 :             lSize = be::swap(pRecord->length);
     544           0 :             return true;
     545             :         }
     546           0 :         pRecord++;
     547             :     }
     548             : 
     549           0 :     return false;
     550             : }
     551             : 
     552             : #ifdef ALL_TTFUTILS
     553             : /*----------------------------------------------------------------------------------------------
     554             :     Return all the lang-IDs that have data for the given name-IDs. Assume that there is room
     555             :     in the return array (langIdList) for 128 items. The purpose of this method is to return
     556             :     a list of all possible lang-IDs.
     557             : ----------------------------------------------------------------------------------------------*/
     558             : int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId,
     559             :         int * nameIdList, int cNameIds, short * langIdList)
     560             : {
     561             :     const Sfnt::FontNames * pTable = reinterpret_cast<const Sfnt::FontNames *>(pName);
     562             :         int cLangIds = 0;
     563             :     uint16 cRecord = be::swap(pTable->count);
     564             :         if (cRecord > 127) return cLangIds;
     565             :     //uint16 nRecordOffset = swapw(pTable->stringOffset);
     566             :     const Sfnt::NameRecord * pRecord = reinterpret_cast<const Sfnt::NameRecord *>(pTable + 1);
     567             : 
     568             :     for (int i = 0; i < cRecord; ++i)
     569             :     {
     570             :         if (be::swap(pRecord->platform_id) == nPlatformId &&
     571             :             be::swap(pRecord->platform_specific_id) == nEncodingId)
     572             :         {
     573             :             bool fNameFound = false;
     574             :             int nLangId = be::swap(pRecord->language_id);
     575             :             int nNameId = be::swap(pRecord->name_id);
     576             :             for (int j = 0; j < cNameIds; j++)
     577             :             {
     578             :                 if (nNameId == nameIdList[j])
     579             :                 {
     580             :                     fNameFound = true;
     581             :                     break;
     582             :                 }
     583             :             }
     584             :             if (fNameFound)
     585             :             {
     586             :                 // Add it if it's not there.
     587             :                 int ilang;
     588             :                 for (ilang = 0; ilang < cLangIds; ilang++)
     589             :                     if (langIdList[ilang] == nLangId)
     590             :                         break;
     591             :                 if (ilang >= cLangIds)
     592             :                 {
     593             :                     langIdList[cLangIds] = short(nLangId);
     594             :                     cLangIds++;
     595             :                 }
     596             :                 if (cLangIds == 128)
     597             :                     return cLangIds;
     598             :             }
     599             :         }
     600             :         pRecord++;
     601             :     }
     602             : 
     603             :     return cLangIds;
     604             : }
     605             : 
     606             : /*----------------------------------------------------------------------------------------------
     607             :     Get the offset and size of the font family name in English for the MS Platform with Unicode
     608             :     writing system. The offset is within the pName data. The string is double byte with MSB
     609             :     first.
     610             : ----------------------------------------------------------------------------------------------*/
     611             : bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize)
     612             : {
     613             :     return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, 
     614             :         Sfnt::NameRecord::Family, lOffset, lSize);
     615             : }
     616             : 
     617             : /*----------------------------------------------------------------------------------------------
     618             :     Get the offset and size of the full font name in English for the MS Platform with Unicode
     619             :     writing system. The offset is within the pName data. The string is double byte with MSB
     620             :     first.
     621             : 
     622             :     Note: this method is not currently used by the Graphite engine.
     623             : ----------------------------------------------------------------------------------------------*/
     624             : bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize)
     625             : {
     626             :     return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, 
     627             :         Sfnt::NameRecord::Fullname, lOffset, lSize);
     628             : }
     629             : 
     630             : /*----------------------------------------------------------------------------------------------
     631             :     Get the offset and size of the font family name in English for the MS Platform with Symbol
     632             :     writing system. The offset is within the pName data. The string is double byte with MSB
     633             :     first.
     634             : ----------------------------------------------------------------------------------------------*/
     635             : bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize)
     636             : {
     637             :     return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, 
     638             :         Sfnt::NameRecord::Family, lOffset, lSize);
     639             : }
     640             : 
     641             : /*----------------------------------------------------------------------------------------------
     642             :     Get the offset and size of the full font name in English for the MS Platform with Symbol
     643             :     writing system. The offset is within the pName data. The string is double byte with MSB
     644             :     first.
     645             : 
     646             :     Note: this method is not currently used by the Graphite engine.
     647             : ----------------------------------------------------------------------------------------------*/
     648             : bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize)
     649             : {
     650             :     return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, 
     651             :         Sfnt::NameRecord::Fullname, lOffset, lSize);
     652             : }
     653             : 
     654             : /*----------------------------------------------------------------------------------------------
     655             :     Return the Glyph ID for a given Postscript name. This method finds the first glyph which
     656             :     matches the requested Postscript name. Ideally every glyph should have a unique Postscript 
     657             :     name (except for special names such as .notdef), but this is not always true.
     658             :     On failure return value less than zero.
     659             :        -1 - table search failed
     660             :        -2 - format 3 table (no Postscript glyph info)
     661             :        -3 - other failures
     662             : 
     663             :     Note: this method is not currently used by the Graphite engine.
     664             : ----------------------------------------------------------------------------------------------*/
     665             : int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp, 
     666             :                         const char * pPostName)
     667             : {
     668             :     using namespace Sfnt;
     669             :     
     670             :     const Sfnt::PostScriptGlyphName * pTable 
     671             :         = reinterpret_cast<const Sfnt::PostScriptGlyphName *>(pPost);
     672             :     fixed format = be::swap(pTable->format);
     673             : 
     674             :     if (format == PostScriptGlyphName::Format3)
     675             :     { // format 3 - no Postscript glyph info in font
     676             :         return -2;
     677             :     }
     678             : 
     679             :     // search for given Postscript name among the standard names
     680             :     int iPostName = -1; // index in standard names
     681             :     for (int i = 0; i < kcPostNames; i++)
     682             :     {
     683             :         if (!strcmp(pPostName, rgPostName[i]))
     684             :         {
     685             :             iPostName = i;
     686             :             break;
     687             :         }
     688             :     }
     689             : 
     690             :     if (format == PostScriptGlyphName::Format1)
     691             :     { // format 1 - use standard Postscript names
     692             :         return iPostName;
     693             :     }
     694             :     
     695             :     if (format == PostScriptGlyphName::Format25)
     696             :     { 
     697             :         if (iPostName == -1)
     698             :             return -1;
     699             :         
     700             :         const PostScriptGlyphName25 * pTable25 
     701             :             = static_cast<const PostScriptGlyphName25 *>(pTable);
     702             :         int cnGlyphs = GlyphCount(pMaxp);
     703             :         for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs && nGlyphId < kcPostNames; 
     704             :                 nGlyphId++)
     705             :         { // glyph_name_index25 contains bytes so no byte swapping needed
     706             :           // search for first glyph id that uses the standard name 
     707             :             if (nGlyphId + pTable25->offset[nGlyphId] == iPostName)
     708             :                 return nGlyphId;
     709             :         }
     710             :     }
     711             : 
     712             :     if (format == PostScriptGlyphName::Format2)
     713             :     { // format 2
     714             :         const PostScriptGlyphName2 * pTable2 
     715             :             = static_cast<const PostScriptGlyphName2 *>(pTable);
     716             :         
     717             :         int cnGlyphs = be::swap(pTable2->number_of_glyphs);
     718             : 
     719             :         if (iPostName != -1)
     720             :         { // did match a standard name, look for first glyph id mapped to that name
     721             :             for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++)
     722             :             {
     723             :                 if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iPostName)
     724             :                     return nGlyphId;
     725             :             }
     726             :         }
     727             : 
     728             :         { // did not match a standard name, search font specific names
     729             :             size_t nStrSizeGoal = strlen(pPostName);
     730             :             const char * pFirstGlyphName = reinterpret_cast<const char *>(
     731             :                 &pTable2->glyph_name_index[0] + cnGlyphs);
     732             :             const char * pGlyphName = pFirstGlyphName;
     733             :             int iInNames = 0; // index in font specific names
     734             :             bool fFound = false;
     735             :             const char * const endOfTable 
     736             :                 = reinterpret_cast<const char *>(pTable2) + lPostSize;
     737             :             while (pGlyphName < endOfTable && !fFound) 
     738             :             { // search Pascal strings for first matching name
     739             :                 size_t nStringSize = size_t(*pGlyphName);
     740             :                 if (nStrSizeGoal != nStringSize ||
     741             :                     strncmp(pGlyphName + 1, pPostName, nStringSize))
     742             :                 { // did not match
     743             :                     ++iInNames;
     744             :                     pGlyphName += nStringSize + 1;
     745             :                 }
     746             :                 else
     747             :                 { // did match
     748             :                     fFound = true;
     749             :                 }
     750             :             }
     751             :             if (!fFound)
     752             :                 return -1; // no font specific name matches request
     753             : 
     754             :             iInNames += kcPostNames;
     755             :             for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++)
     756             :             { // search for first glyph id that maps to the found string index
     757             :                 if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iInNames)
     758             :                     return nGlyphId;
     759             :             }
     760             :             return -1; // no glyph mapped to this index (very strange)
     761             :         }
     762             :     }
     763             : 
     764             :     return -3;
     765             : }
     766             : 
     767             : /*----------------------------------------------------------------------------------------------
     768             :     Convert a Unicode character string from big endian (MSB first, Motorola) format to little 
     769             :     endian (LSB first, Intel) format. 
     770             :     nSize is the number of Unicode characters in the string. It should not include any 
     771             :     terminating null. If nSize is 0, it is assumed the string is null terminated. nSize 
     772             :     defaults to 0.
     773             :     Return true if successful, false otherwise. 
     774             : ----------------------------------------------------------------------------------------------*/
     775             : void SwapWString(void * pWStr, size_t nSize /* = 0 */) //throw (std::invalid_argument)
     776             : {
     777             :     if (pWStr == 0)
     778             :     {
     779             : //      throw std::invalid_argument("null pointer given");
     780             :         return;
     781             :     }
     782             : 
     783             :     uint16 * pStr = reinterpret_cast<uint16 *>(pWStr);
     784             :     uint16 * const pStrEnd = pStr + (nSize == 0 ? wcslen((const wchar_t*)pStr) : nSize);
     785             : 
     786             :         for (; pStr != pStrEnd; ++pStr)
     787             :           *pStr = be::swap(*pStr);
     788             : //  std::transform(pStr, pStrEnd, pStr, read<uint16>);
     789             : 
     790             : //      for (int i = 0; i < nSize; i++)
     791             : //      { // swap the wide characters in the string
     792             : //          pStr[i] = utf16(be::swap(uint16(pStr[i])));
     793             : //      }
     794             : }
     795             : #endif
     796             : 
     797             : /*----------------------------------------------------------------------------------------------
     798             :     Get the left-side bearing and and advance width based on the given tables and Glyph ID
     799             :     Return true if successful, false otherwise. On false, one or both value could be INT_MIN
     800             : ----------------------------------------------------------------------------------------------*/
     801           0 : bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize, const void * pHhea, 
     802             :                          int & nLsb, unsigned int & nAdvWid)
     803             : {
     804             :     const Sfnt::HorizontalMetric * phmtx = 
     805           0 :         reinterpret_cast<const Sfnt::HorizontalMetric *>(pHmtx);
     806             : 
     807             :     const Sfnt::HorizontalHeader * phhea = 
     808           0 :         reinterpret_cast<const Sfnt::HorizontalHeader *>(pHhea);
     809             : 
     810           0 :     size_t cLongHorMetrics = be::swap(phhea->num_long_hor_metrics);
     811           0 :     if (nGlyphId < cLongHorMetrics) 
     812             :     {   // glyph id is acceptable
     813           0 :         if ((nGlyphId + 1) * sizeof(Sfnt::HorizontalMetric) > lHmtxSize) return false;
     814           0 :         nAdvWid = be::swap(phmtx[nGlyphId].advance_width);
     815           0 :         nLsb = be::swap(phmtx[nGlyphId].left_side_bearing);
     816             :     }
     817             :     else
     818             :     {
     819             :         // guard against bad glyph id
     820           0 :         size_t lLsbOffset = sizeof(Sfnt::HorizontalMetric) * cLongHorMetrics +
     821           0 :             sizeof(int16) * (nGlyphId - cLongHorMetrics); // offset in bytes
     822             :         // We test like this as LsbOffset is an offset not a length.
     823           0 :         if (lLsbOffset >= lHmtxSize - sizeof(int16) || cLongHorMetrics == 0)
     824             :         {
     825           0 :             nLsb = 0;
     826           0 :             return false;
     827             :         }
     828           0 :         nAdvWid = be::swap(phmtx[cLongHorMetrics - 1].advance_width);
     829           0 :         nLsb = be::peek<int16>(reinterpret_cast<const byte *>(phmtx) + lLsbOffset);
     830             :     }
     831             : 
     832           0 :     return true;
     833             : }
     834             : 
     835             : /*----------------------------------------------------------------------------------------------
     836             :     Return a pointer to the requested cmap subtable. By default find the Microsoft Unicode
     837             :     subtable. Pass nEncoding as -1 to find first table that matches only nPlatformId.
     838             :     Return NULL if the subtable cannot be found.
     839             : ----------------------------------------------------------------------------------------------*/
     840           0 : const void * FindCmapSubtable(const void * pCmap, int nPlatformId, /* =3 */ int nEncodingId, /* = 1 */ size_t length)
     841             : {
     842           0 :     const Sfnt::CharacterCodeMap * pTable = reinterpret_cast<const Sfnt::CharacterCodeMap *>(pCmap);
     843           0 :     uint16 csuPlatforms = be::swap(pTable->num_subtables);
     844           0 :     if (length && (sizeof(Sfnt::CharacterCodeMap) + 8 * (csuPlatforms - 1) > length))
     845           0 :         return NULL;
     846           0 :     for (int i = 0; i < csuPlatforms; i++)
     847             :     {
     848           0 :         if (be::swap(pTable->encoding[i].platform_id) == nPlatformId &&
     849           0 :                 (nEncodingId == -1 || be::swap(pTable->encoding[i].platform_specific_id) == nEncodingId))
     850             :         {
     851           0 :             uint32 offset = be::swap(pTable->encoding[i].offset);
     852           0 :             const uint8 * pRtn = reinterpret_cast<const uint8 *>(pCmap) + offset;
     853           0 :             if (length)
     854             :             {
     855           0 :                 if (offset > length - 2) return NULL;
     856           0 :                 uint16 format = be::read<uint16>(pRtn);
     857           0 :                 if (format == 4)
     858             :                 {
     859           0 :                     if (offset > length - 4) return NULL;
     860           0 :                     uint16 subTableLength = be::peek<uint16>(pRtn);
     861           0 :                     if (i + 1 == csuPlatforms)
     862             :                     {
     863           0 :                         if (subTableLength > length - offset)
     864           0 :                             return NULL;
     865             :                     }
     866           0 :                     else if (subTableLength > be::swap(pTable->encoding[i+1].offset))
     867           0 :                         return NULL;
     868             :                 }
     869           0 :                 if (format == 12)
     870             :                 {
     871           0 :                     if (offset > length - 6) return NULL;
     872           0 :                     uint32 subTableLength = be::peek<uint32>(pRtn);
     873           0 :                     if (i + 1 == csuPlatforms)
     874             :                     {
     875           0 :                         if (subTableLength > length - offset)
     876           0 :                             return NULL;
     877             :                     }
     878           0 :                     else if (subTableLength > be::swap(pTable->encoding[i+1].offset))
     879           0 :                         return NULL;
     880             :                 }
     881             :             }
     882           0 :             return reinterpret_cast<const uint8 *>(pCmap) + offset;
     883             :         }
     884             :     }
     885             : 
     886           0 :     return 0;
     887             : }
     888             : 
     889             : /*----------------------------------------------------------------------------------------------
     890             :     Check the Microsoft Unicode subtable for expected values
     891             : ----------------------------------------------------------------------------------------------*/
     892           0 : bool CheckCmapSubtable4(const void * pCmapSubtable4, const void * pCmapEnd /*, unsigned int maxgid*/)
     893             : {
     894           0 :     size_t table_len = (const byte *)pCmapEnd - (const byte *)pCmapSubtable4;
     895           0 :     if (!pCmapSubtable4) return false;
     896           0 :     const Sfnt::CmapSubTable * pTable = reinterpret_cast<const Sfnt::CmapSubTable *>(pCmapSubtable4);
     897             :     // Bob H say some freeware TT fonts have version 1 (eg, CALIGULA.TTF) 
     898             :     // so don't check subtable version. 21 Mar 2002 spec changes version to language.
     899           0 :     if (table_len < sizeof(*pTable) || be::swap(pTable->format) != 4) return false;
     900           0 :     const Sfnt::CmapSubTableFormat4 * pTable4 = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmapSubtable4);
     901           0 :     if (table_len < sizeof(*pTable4))
     902           0 :         return false;
     903           0 :     uint16 length = be::swap(pTable4->length);
     904           0 :     if (length > table_len)
     905           0 :         return false;
     906           0 :     if (length < sizeof(Sfnt::CmapSubTableFormat4))
     907           0 :         return false;
     908           0 :     uint16 nRanges = be::swap(pTable4->seg_count_x2) >> 1;
     909           0 :     if (!nRanges || length < sizeof(Sfnt::CmapSubTableFormat4) + 4 * nRanges * sizeof(uint16))
     910           0 :         return false;
     911             :     // check last range is properly terminated
     912           0 :     uint16 chEnd = be::peek<uint16>(pTable4->end_code + nRanges - 1);
     913           0 :     if (chEnd != 0xFFFF)
     914           0 :         return false;
     915             : #if 0
     916             :     int lastend = -1;
     917             :     for (int i = 0; i < nRanges; ++i)
     918             :     {
     919             :         uint16 end = be::peek<uint16>(pTable4->end_code + i);
     920             :         uint16 start = be::peek<uint16>(pTable4->end_code + nRanges + 1 + i);
     921             :         int16 delta = be::peek<int16>(pTable4->end_code + 2*nRanges + 1 + i);
     922             :         uint16 offset = be::peek<uint16>(pTable4->end_code + 3*nRanges + 1 + i);
     923             :         if (lastend >= end || lastend >= start)
     924             :             return false;
     925             :         if (offset)
     926             :         {
     927             :             const uint16 *gstart = pTable4->end_code + 3*nRanges + 1 + i + (offset >> 1);
     928             :             const uint16 *gend = gstart + end - start;
     929             :             if ((char *)gend >= (char *)pCmapSubtable4 + length)
     930             :                 return false;
     931             :             while (gstart <= gend)
     932             :             {
     933             :                 uint16 g = be::peek<uint16>(gstart++);
     934             :                 if (g && ((g + delta) & 0xFFFF) > maxgid)
     935             :                     return false;
     936             :             }
     937             :         }
     938             :         else if (((delta + end) & 0xFFFF) > maxgid)
     939             :             return false;
     940             :         lastend = end;
     941             :     }
     942             : #endif
     943           0 :     return true;
     944             : }
     945             : 
     946             : /*----------------------------------------------------------------------------------------------
     947             :     Return the Glyph ID for the given Unicode ID in the Microsoft Unicode subtable.
     948             :     (Actually this code only depends on subtable being format 4.)
     949             :     Return 0 if the Unicode ID is not in the subtable.
     950             : ----------------------------------------------------------------------------------------------*/
     951           0 : gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey)
     952             : {
     953           0 :     const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmapSubtabel4);
     954             : 
     955           0 :     uint16 nSeg = be::swap(pTable->seg_count_x2) >> 1;
     956             :   
     957             :     uint16 n;
     958             :     const uint16 * pLeft, * pMid;
     959             :     uint16 cMid, chStart, chEnd;
     960             : 
     961           0 :     if (rangeKey)
     962             :     {
     963           0 :         pMid = &(pTable->end_code[rangeKey]);
     964           0 :         chEnd = be::peek<uint16>(pMid);
     965             :     }
     966             :     else
     967             :     {
     968             :         // Binary search of the endCode[] array
     969           0 :         pLeft = &(pTable->end_code[0]);
     970           0 :         n = nSeg;
     971           0 :         while (n > 0)
     972             :         {
     973           0 :             cMid = n >> 1;           // Pick an element in the middle
     974           0 :             pMid = pLeft + cMid;
     975           0 :             chEnd = be::peek<uint16>(pMid);
     976           0 :             if (nUnicodeId <= chEnd)
     977             :             {
     978           0 :                 if (cMid == 0 || nUnicodeId > be::peek<uint16>(pMid -1))
     979           0 :                         break;          // Must be this seg or none!
     980           0 :                 n = cMid;            // Continue on left side, omitting mid point
     981             :             }
     982             :             else
     983             :             {
     984           0 :                 pLeft = pMid + 1;    // Continue on right side, omitting mid point
     985           0 :                 n -= (cMid + 1);
     986             :             }
     987             :         }
     988             : 
     989           0 :         if (!n)
     990           0 :         return 0;
     991             :     }
     992             : 
     993             :     // Ok, we're down to one segment and pMid points to the endCode element
     994             :     // Either this is it or none is.
     995             : 
     996           0 :     chStart = be::peek<uint16>(pMid += nSeg + 1);
     997           0 :     if (chEnd >= nUnicodeId && nUnicodeId >= chStart)
     998             :     {
     999             :         // Found correct segment. Find Glyph Id
    1000           0 :         int16 idDelta = be::peek<uint16>(pMid += nSeg);
    1001           0 :         uint16 idRangeOffset = be::peek<uint16>(pMid += nSeg);
    1002             : 
    1003           0 :         if (idRangeOffset == 0)
    1004           0 :             return (uint16)(idDelta + nUnicodeId); // must use modulus 2^16
    1005             : 
    1006             :         // Look up value in glyphIdArray
    1007           0 :         const ptrdiff_t offset = (nUnicodeId - chStart) + (idRangeOffset >> 1) +
    1008           0 :                 (pMid - reinterpret_cast<const uint16 *>(pTable));
    1009           0 :         if (offset * 2 + 1 >= be::swap<uint16>(pTable->length))
    1010           0 :             return 0;
    1011           0 :         gid16 nGlyphId = be::peek<uint16>(reinterpret_cast<const uint16 *>(pTable)+offset);
    1012             :         // If this value is 0, return 0. Else add the idDelta
    1013           0 :         return nGlyphId ? nGlyphId + idDelta : 0;
    1014             :     }
    1015             : 
    1016           0 :     return 0;
    1017             : }
    1018             : 
    1019             : /*----------------------------------------------------------------------------------------------
    1020             :     Return the next Unicode value in the cmap. Pass 0 to obtain the first item.
    1021             :     Returns 0xFFFF as the last item.
    1022             :     pRangeKey is an optional key that is used to optimize the search; its value is the range
    1023             :     in which the character is found.
    1024             : ----------------------------------------------------------------------------------------------*/
    1025           0 : unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId, int * pRangeKey)
    1026             : {
    1027           0 :     const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat4 *>(pCmap31);
    1028             : 
    1029           0 :     uint16 nRange = be::swap(pTable->seg_count_x2) >> 1;
    1030             : 
    1031           0 :     uint32 nUnicodePrev = (uint32)nUnicodeId;
    1032             : 
    1033           0 :     const uint16 * pStartCode = &(pTable->end_code[0])
    1034             :         + nRange // length of end code array
    1035           0 :         + 1;   // reserved word
    1036             : 
    1037           0 :     if (nUnicodePrev == 0)
    1038             :     {
    1039             :         // return the first codepoint.
    1040           0 :         if (pRangeKey)
    1041           0 :             *pRangeKey = 0;
    1042           0 :         return be::peek<uint16>(pStartCode);
    1043             :     }
    1044           0 :     else if (nUnicodePrev >= 0xFFFF)
    1045             :     {
    1046           0 :         if (pRangeKey)
    1047           0 :             *pRangeKey = nRange - 1;
    1048           0 :         return 0xFFFF;
    1049             :     }
    1050             : 
    1051           0 :     int iRange = (pRangeKey) ? *pRangeKey : 0;
    1052             :     // Just in case we have a bad key:
    1053           0 :     while (iRange > 0 && be::peek<uint16>(pStartCode + iRange) > nUnicodePrev)
    1054           0 :         iRange--;
    1055           0 :     while (iRange < nRange - 1 && be::peek<uint16>(pTable->end_code + iRange) < nUnicodePrev)
    1056           0 :         iRange++;
    1057             : 
    1058             :     // Now iRange is the range containing nUnicodePrev.
    1059           0 :     unsigned int nStartCode = be::peek<uint16>(pStartCode + iRange);
    1060           0 :     unsigned int nEndCode = be::peek<uint16>(pTable->end_code + iRange);
    1061             : 
    1062           0 :     if (nStartCode > nUnicodePrev)
    1063             :         // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable
    1064             :         // answer this time around.
    1065           0 :         nUnicodePrev = nStartCode - 1;
    1066             : 
    1067           0 :     if (nEndCode > nUnicodePrev)
    1068             :     {
    1069             :         // Next is in the same range; it is the next successive codepoint.
    1070           0 :         if (pRangeKey)
    1071           0 :             *pRangeKey = iRange;
    1072           0 :         return nUnicodePrev + 1;
    1073             :     }
    1074             : 
    1075             :     // Otherwise the next codepoint is the first one in the next range.
    1076             :     // There is guaranteed to be a next range because there must be one that
    1077             :     // ends with 0xFFFF.
    1078           0 :     if (pRangeKey)
    1079           0 :         *pRangeKey = iRange + 1;
    1080           0 :     return (iRange + 1 >= nRange) ? 0xFFFF : be::peek<uint16>(pStartCode + iRange + 1);
    1081             : }
    1082             : 
    1083             : /*----------------------------------------------------------------------------------------------
    1084             :     Check the Microsoft UCS-4 subtable for expected values.
    1085             : ----------------------------------------------------------------------------------------------*/
    1086           0 : bool CheckCmapSubtable12(const void *pCmapSubtable12, const void *pCmapEnd /*, unsigned int maxgid*/)
    1087             : {
    1088           0 :     size_t table_len = (const byte *)pCmapEnd - (const byte *)pCmapSubtable12;
    1089           0 :     if (!pCmapSubtable12)  return false;
    1090           0 :     const Sfnt::CmapSubTable * pTable = reinterpret_cast<const Sfnt::CmapSubTable *>(pCmapSubtable12);
    1091           0 :     if (table_len < sizeof(*pTable) || be::swap(pTable->format) != 12)
    1092           0 :         return false;
    1093           0 :     const Sfnt::CmapSubTableFormat12 * pTable12 = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmapSubtable12);
    1094           0 :     if (table_len < sizeof(*pTable12))
    1095           0 :         return false;
    1096           0 :     uint32 length = be::swap(pTable12->length);
    1097           0 :     if (length > table_len)
    1098           0 :         return false;
    1099           0 :     if (length < sizeof(Sfnt::CmapSubTableFormat12))
    1100           0 :         return false;
    1101           0 :     uint32 num_groups = be::swap(pTable12->num_groups);
    1102           0 :     if (num_groups > 0x10000000 || length != (sizeof(Sfnt::CmapSubTableFormat12) + (num_groups - 1) * sizeof(uint32) * 3))
    1103           0 :         return false;
    1104             : #if 0
    1105             :     for (unsigned int i = 0; i < num_groups; ++i)
    1106             :     {
    1107             :         if (be::swap(pTable12->group[i].end_char_code)  - be::swap(pTable12->group[i].start_char_code) + be::swap(pTable12->group[i].start_glyph_id) > maxgid)
    1108             :             return false;
    1109             :         if (i > 0 && be::swap(pTable12->group[i].start_char_code) <= be::swap(pTable12->group[i-1].end_char_code))
    1110             :             return false;
    1111             :     }
    1112             : #endif
    1113           0 :     return true;
    1114             : }
    1115             : 
    1116             : /*----------------------------------------------------------------------------------------------
    1117             :     Return the Glyph ID for the given Unicode ID in the Microsoft UCS-4 subtable.
    1118             :     (Actually this code only depends on subtable being format 12.)
    1119             :     Return 0 if the Unicode ID is not in the subtable.
    1120             : ----------------------------------------------------------------------------------------------*/
    1121           0 : gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey)
    1122             : {
    1123           0 :     const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmap310);
    1124             : 
    1125             :     //uint32 uLength = be::swap(pTable->length); //could use to test for premature end of table
    1126           0 :     uint32 ucGroups = be::swap(pTable->num_groups);
    1127             : 
    1128           0 :     for (unsigned int i = rangeKey; i < ucGroups; i++)
    1129             :     {
    1130           0 :         uint32 uStartCode = be::swap(pTable->group[i].start_char_code);
    1131           0 :         uint32 uEndCode = be::swap(pTable->group[i].end_char_code);
    1132           0 :         if (uUnicodeId >= uStartCode && uUnicodeId <= uEndCode)
    1133             :         {
    1134           0 :             uint32 uDiff = uUnicodeId - uStartCode;
    1135           0 :             uint32 uStartGid = be::swap(pTable->group[i].start_glyph_id);
    1136           0 :             return static_cast<gid16>(uStartGid + uDiff);
    1137             :         }
    1138             :     }
    1139             : 
    1140           0 :     return 0;
    1141             : }
    1142             : 
    1143             : /*----------------------------------------------------------------------------------------------
    1144             :     Return the next Unicode value in the cmap. Pass 0 to obtain the first item.
    1145             :     Returns 0x10FFFF as the last item.
    1146             :     pRangeKey is an optional key that is used to optimize the search; its value is the range
    1147             :     in which the character is found.
    1148             : ----------------------------------------------------------------------------------------------*/
    1149           0 : unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId, int * pRangeKey)
    1150             : {
    1151           0 :     const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast<const Sfnt::CmapSubTableFormat12 *>(pCmap310);
    1152             : 
    1153           0 :     int nRange = be::swap(pTable->num_groups);
    1154             : 
    1155           0 :     uint32 nUnicodePrev = (uint32)nUnicodeId;
    1156             : 
    1157           0 :     if (nUnicodePrev == 0)
    1158             :     {
    1159             :         // return the first codepoint.
    1160           0 :         if (pRangeKey)
    1161           0 :             *pRangeKey = 0;
    1162           0 :         return be::swap(pTable->group[0].start_char_code);
    1163             :     }
    1164           0 :     else if (nUnicodePrev >= 0x10FFFF)
    1165             :     {
    1166           0 :         if (pRangeKey)
    1167           0 :             *pRangeKey = nRange;
    1168           0 :         return 0x10FFFF;
    1169             :     }
    1170             : 
    1171           0 :     int iRange = (pRangeKey) ? *pRangeKey : 0;
    1172             :     // Just in case we have a bad key:
    1173           0 :     while (iRange > 0 && be::swap(pTable->group[iRange].start_char_code) > nUnicodePrev)
    1174           0 :         iRange--;
    1175           0 :     while (iRange < nRange - 1 && be::swap(pTable->group[iRange].end_char_code) < nUnicodePrev)
    1176           0 :         iRange++;
    1177             : 
    1178             :     // Now iRange is the range containing nUnicodePrev.
    1179             : 
    1180           0 :     unsigned int nStartCode = be::swap(pTable->group[iRange].start_char_code);
    1181           0 :     unsigned int nEndCode = be::swap(pTable->group[iRange].end_char_code);
    1182             : 
    1183           0 :     if (nStartCode > nUnicodePrev)
    1184             :         // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable
    1185             :         // answer this time around.
    1186           0 :         nUnicodePrev = nStartCode - 1;
    1187             : 
    1188           0 :     if (nEndCode > nUnicodePrev)
    1189             :     {
    1190             :         // Next is in the same range; it is the next successive codepoint.
    1191           0 :         if (pRangeKey)
    1192           0 :             *pRangeKey = iRange;
    1193           0 :         return nUnicodePrev + 1;
    1194             :     }
    1195             : 
    1196             :     // Otherwise the next codepoint is the first one in the next range, or 10FFFF if we're done.
    1197           0 :     if (pRangeKey)
    1198           0 :         *pRangeKey = iRange + 1;
    1199           0 :     return (iRange + 1 >= nRange) ? 0x10FFFF : be::swap(pTable->group[iRange + 1].start_char_code);
    1200             : }
    1201             : 
    1202             : /*----------------------------------------------------------------------------------------------
    1203             :     Return the offset stored in the loca table for the given Glyph ID.
    1204             :     (This offset is into the glyf table.)
    1205             :     Return -1 if the lookup failed.
    1206             :     Technically this method should return an unsigned long but it is unlikely the offset will
    1207             :         exceed 2^31.
    1208             : ----------------------------------------------------------------------------------------------*/
    1209           0 : size_t LocaLookup(gid16 nGlyphId, 
    1210             :         const void * pLoca, size_t lLocaSize, 
    1211             :         const void * pHead) // throw (std::out_of_range)
    1212             : {
    1213           0 :     const Sfnt::FontHeader * pTable = reinterpret_cast<const Sfnt::FontHeader *>(pHead);
    1214           0 :     size_t res = -2;
    1215             : 
    1216             :     // CheckTable verifies the index_to_loc_format is valid
    1217           0 :     if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat)
    1218             :     { // loca entries are two bytes and have been divided by two
    1219           0 :         if (lLocaSize > 1 && nGlyphId + 1u < lLocaSize >> 1) // allow sentinel value to be accessed
    1220             :         {
    1221           0 :             const uint16 * pShortTable = reinterpret_cast<const uint16 *>(pLoca);
    1222           0 :             res = be::peek<uint16>(pShortTable + nGlyphId) << 1;
    1223           0 :             if (res == static_cast<size_t>(be::peek<uint16>(pShortTable + nGlyphId + 1) << 1))
    1224           0 :                 return -1;
    1225             :         }
    1226             :     }
    1227           0 :     else if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat)
    1228             :     { // loca entries are four bytes
    1229           0 :         if (lLocaSize > 3 && nGlyphId + 1u < lLocaSize >> 2)
    1230             :         {
    1231           0 :             const uint32 * pLongTable = reinterpret_cast<const uint32 *>(pLoca);
    1232           0 :             res = be::peek<uint32>(pLongTable + nGlyphId);
    1233           0 :             if (res == static_cast<size_t>(be::peek<uint32>(pLongTable + nGlyphId + 1)))
    1234           0 :                 return -1;
    1235             :         }
    1236             :     }
    1237             : 
    1238             :     // only get here if glyph id was bad
    1239           0 :     return res;
    1240             :     //throw std::out_of_range("glyph id out of range for font");
    1241             : }
    1242             : 
    1243             : /*----------------------------------------------------------------------------------------------
    1244             :     Return a pointer into the glyf table based on the given offset (from LocaLookup).
    1245             :     Return NULL on error.
    1246             : ----------------------------------------------------------------------------------------------*/
    1247           0 : void * GlyfLookup(const void * pGlyf, size_t nGlyfOffset, size_t nTableLen)
    1248             : {
    1249           0 :     const uint8 * pByte = reinterpret_cast<const uint8 *>(pGlyf);
    1250           0 :         if (nGlyfOffset + pByte < pByte || nGlyfOffset + sizeof(Sfnt::Glyph) >= nTableLen)
    1251           0 :             return NULL;
    1252           0 :     return const_cast<uint8 *>(pByte + nGlyfOffset);
    1253             : }
    1254             : 
    1255             : /*----------------------------------------------------------------------------------------------
    1256             :     Get the bounding box coordinates for a simple glyf entry (non-composite).
    1257             :     Return true if successful, false otherwise.
    1258             : ----------------------------------------------------------------------------------------------*/
    1259           0 : bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin, 
    1260             :                       int & xMax, int & yMax)
    1261             : {
    1262           0 :     const Sfnt::Glyph * pGlyph = reinterpret_cast<const Sfnt::Glyph *>(pSimpleGlyf);
    1263             : 
    1264           0 :     xMin = be::swap(pGlyph->x_min);
    1265           0 :     yMin = be::swap(pGlyph->y_min);
    1266           0 :     xMax = be::swap(pGlyph->x_max);
    1267           0 :     yMax = be::swap(pGlyph->y_max);
    1268             : 
    1269           0 :     return true;
    1270             : }
    1271             : 
    1272             : #ifdef ALL_TTFUTILS
    1273             : /*----------------------------------------------------------------------------------------------
    1274             :     Return the number of contours for a simple glyf entry (non-composite)
    1275             :     Returning -1 means this is a composite glyph
    1276             : ----------------------------------------------------------------------------------------------*/
    1277             : int GlyfContourCount(const void * pSimpleGlyf)
    1278             : {
    1279             :     const Sfnt::Glyph * pGlyph = reinterpret_cast<const Sfnt::Glyph *>(pSimpleGlyf);
    1280             :     return be::swap(pGlyph->number_of_contours); // -1 means composite glyph
    1281             : }
    1282             : 
    1283             : /*----------------------------------------------------------------------------------------------
    1284             :     Get the point numbers for the end points of the glyph contours for a simple
    1285             :     glyf entry (non-composite). 
    1286             :     cnPointsTotal - count of contours from GlyfContourCount(); (same as number of end points)
    1287             :     prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers
    1288             :     cnPoints - count of points placed in above range
    1289             :     Return true if successful, false otherwise.
    1290             :         False could indicate a multi-level composite glyphs.
    1291             : ----------------------------------------------------------------------------------------------*/
    1292             : bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint, 
    1293             :                                    int cnPointsTotal, int & cnPoints)
    1294             : {
    1295             :     const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf);
    1296             : 
    1297             :     int cContours = be::swap(pGlyph->number_of_contours);
    1298             :     if (cContours < 0)
    1299             :         return false; // this method isn't supposed handle composite glyphs
    1300             : 
    1301             :     for (int i = 0; i < cContours && i < cnPointsTotal; i++)
    1302             :     {
    1303             :         prgnContourEndPoint[i] = be::swap(pGlyph->end_pts_of_contours[i]);
    1304             :     }
    1305             : 
    1306             :     cnPoints = cContours;
    1307             :     return true;
    1308             : }
    1309             : 
    1310             : /*----------------------------------------------------------------------------------------------
    1311             :     Get the points for a simple glyf entry (non-composite)
    1312             :     cnPointsTotal - count of points from largest end point obtained from GlyfContourEndPoints
    1313             :     prgnX & prgnY - should point to buffers large enough to hold cnPointsTotal integers
    1314             :         The ranges are parallel so that coordinates for point(n) are found at offset n in both 
    1315             :         ranges. This is raw point data with relative coordinates.
    1316             :     prgbFlag - should point to a buffer a large enough to hold cnPointsTotal bytes
    1317             :         This range is parallel to the prgnX & prgnY
    1318             :     cnPoints - count of points placed in above ranges
    1319             :     Return true if successful, false otherwise. 
    1320             :         False could indicate a composite glyph
    1321             : ----------------------------------------------------------------------------------------------*/
    1322             : bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY, 
    1323             :         char * prgbFlag, int cnPointsTotal, int & cnPoints)
    1324             : {
    1325             :     using namespace Sfnt;
    1326             :     
    1327             :     const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf);
    1328             :     int cContours = be::swap(pGlyph->number_of_contours);
    1329             :     // return false for composite glyph
    1330             :     if (cContours <= 0)
    1331             :         return false;
    1332             :     int cPts = be::swap(pGlyph->end_pts_of_contours[cContours - 1]) + 1;
    1333             :     if (cPts > cnPointsTotal)
    1334             :         return false;
    1335             : 
    1336             :     // skip over bounding box data & point to byte count of instructions (hints)
    1337             :     const uint8 * pbGlyph = reinterpret_cast<const uint8 *>
    1338             :         (&pGlyph->end_pts_of_contours[cContours]);
    1339             :     
    1340             :     // skip over hints & point to first flag
    1341             :     int cbHints = be::swap(*(uint16 *)pbGlyph);
    1342             :     pbGlyph += sizeof(uint16);
    1343             :     pbGlyph += cbHints;
    1344             : 
    1345             :     // load flags & point to first x coordinate
    1346             :     int iFlag = 0;
    1347             :     while (iFlag < cPts)
    1348             :     {
    1349             :         if (!(*pbGlyph & SimpleGlyph::Repeat))
    1350             :         { // flag isn't repeated
    1351             :             prgbFlag[iFlag] = (char)*pbGlyph;
    1352             :             pbGlyph++;
    1353             :             iFlag++;
    1354             :         }
    1355             :         else
    1356             :         { // flag is repeated; count specified by next byte
    1357             :             char chFlag = (char)*pbGlyph;
    1358             :             pbGlyph++;
    1359             :             int cFlags = (int)*pbGlyph;
    1360             :             pbGlyph++;
    1361             :             prgbFlag[iFlag] = chFlag;
    1362             :             iFlag++;
    1363             :             for (int i = 0; i < cFlags; i++)
    1364             :             {
    1365             :                 prgbFlag[iFlag + i] = chFlag;
    1366             :             }
    1367             :             iFlag += cFlags;
    1368             :         }
    1369             :     }
    1370             :     if (iFlag != cPts)
    1371             :         return false;
    1372             : 
    1373             :     // load x coordinates
    1374             :     iFlag = 0;
    1375             :     while (iFlag < cPts)
    1376             :     {
    1377             :         if (prgbFlag[iFlag] & SimpleGlyph::XShort)
    1378             :         {
    1379             :             prgnX[iFlag] = *pbGlyph;
    1380             :             if (!(prgbFlag[iFlag] & SimpleGlyph::XIsPos))
    1381             :             {
    1382             :                 prgnX[iFlag] = -prgnX[iFlag];
    1383             :             }
    1384             :             pbGlyph++;
    1385             :         }
    1386             :         else
    1387             :         {
    1388             :             if (prgbFlag[iFlag] & SimpleGlyph::XIsSame)
    1389             :             {
    1390             :                 prgnX[iFlag] = 0;
    1391             :                 // do NOT increment pbGlyph
    1392             :             }
    1393             :             else
    1394             :             {
    1395             :                 prgnX[iFlag] = be::swap(*(int16 *)pbGlyph);
    1396             :                 pbGlyph += sizeof(int16);
    1397             :             }
    1398             :         }
    1399             :         iFlag++;
    1400             :     }
    1401             :         
    1402             :     // load y coordinates
    1403             :     iFlag = 0;
    1404             :     while (iFlag < cPts)
    1405             :     {
    1406             :         if (prgbFlag[iFlag] & SimpleGlyph::YShort)
    1407             :         {
    1408             :             prgnY[iFlag] = *pbGlyph;
    1409             :             if (!(prgbFlag[iFlag] & SimpleGlyph::YIsPos))
    1410             :             {
    1411             :                 prgnY[iFlag] = -prgnY[iFlag];
    1412             :             }
    1413             :             pbGlyph++;
    1414             :         }
    1415             :         else
    1416             :         {
    1417             :             if (prgbFlag[iFlag] & SimpleGlyph::YIsSame)
    1418             :             {
    1419             :                 prgnY[iFlag] = 0;
    1420             :                 // do NOT increment pbGlyph
    1421             :             }
    1422             :             else
    1423             :             {
    1424             :                 prgnY[iFlag] = be::swap(*(int16 *)pbGlyph);
    1425             :                 pbGlyph += sizeof(int16);
    1426             :             }
    1427             :         }
    1428             :         iFlag++;
    1429             :     }
    1430             :         
    1431             :     cnPoints = cPts;
    1432             :     return true;
    1433             : }
    1434             : 
    1435             : /*----------------------------------------------------------------------------------------------
    1436             :     Fill prgnCompId with the component Glyph IDs from pSimpleGlyf.
    1437             :     Client must allocate space before calling.
    1438             :     pSimpleGlyf - assumed to point to a composite glyph
    1439             :     cCompIdTotal - the number of elements in prgnCompId 
    1440             :     cCompId  - the total number of Glyph IDs stored in prgnCompId
    1441             :     Return true if successful, false otherwise
    1442             :         False could indicate a non-composite glyph or the input array was not big enough
    1443             : ----------------------------------------------------------------------------------------------*/
    1444             : bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId, 
    1445             :         size_t cnCompIdTotal, size_t & cnCompId)
    1446             : {
    1447             :     using namespace Sfnt;
    1448             :     
    1449             :     if (GlyfContourCount(pSimpleGlyf) >= 0)
    1450             :         return false;
    1451             : 
    1452             :     const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf);
    1453             :     // for a composite glyph, the special data begins here
    1454             :     const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]);
    1455             : 
    1456             :     uint16 GlyphFlags;
    1457             :     size_t iCurrentComp = 0;
    1458             :     do 
    1459             :     {
    1460             :         GlyphFlags = be::swap(*((uint16 *)pbGlyph));
    1461             :         pbGlyph += sizeof(uint16);
    1462             :         prgnCompId[iCurrentComp++] = be::swap(*((uint16 *)pbGlyph));
    1463             :         pbGlyph += sizeof(uint16);
    1464             :         if (iCurrentComp >= cnCompIdTotal) 
    1465             :             return false;
    1466             :         int nOffset = 0;
    1467             :         nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2;
    1468             :         nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0;
    1469             :         nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale  ? 4 : 0;
    1470             :         nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo  ? 8 :  0;
    1471             :         pbGlyph += nOffset;
    1472             :     } while (GlyphFlags & CompoundGlyph::MoreComponents);
    1473             : 
    1474             :     cnCompId = iCurrentComp;
    1475             : 
    1476             :     return true;
    1477             : }
    1478             : 
    1479             : /*----------------------------------------------------------------------------------------------
    1480             :     Return info on how a component glyph is to be placed
    1481             :     pSimpleGlyph - assumed to point to a composite glyph
    1482             :     nCompId - glyph id for component of interest
    1483             :     bOffset - if true, a & b are the x & y offsets for this component
    1484             :               if false, b is the point on this component that is attaching to point a on the
    1485             :                 preceding glyph
    1486             :     Return true if successful, false otherwise
    1487             :         False could indicate a non-composite glyph or that component wasn't found
    1488             : ----------------------------------------------------------------------------------------------*/
    1489             : bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId,
    1490             :                                     bool fOffset, int & a, int & b)
    1491             : {
    1492             :     using namespace Sfnt;
    1493             :     
    1494             :     if (GlyfContourCount(pSimpleGlyf) >= 0)
    1495             :         return false;
    1496             : 
    1497             :     const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf);
    1498             :     // for a composite glyph, the special data begins here
    1499             :     const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]);
    1500             : 
    1501             :     uint16 GlyphFlags;
    1502             :     do 
    1503             :     {
    1504             :         GlyphFlags = be::swap(*((uint16 *)pbGlyph));
    1505             :         pbGlyph += sizeof(uint16);
    1506             :         if (be::swap(*((uint16 *)pbGlyph)) == nCompId)
    1507             :         {
    1508             :             pbGlyph += sizeof(uint16); // skip over glyph id of component
    1509             :             fOffset = (GlyphFlags & CompoundGlyph::ArgsAreXYValues) == CompoundGlyph::ArgsAreXYValues;
    1510             : 
    1511             :             if (GlyphFlags & CompoundGlyph::Arg1Arg2Words )
    1512             :             {
    1513             :                 a = be::swap(*(int16 *)pbGlyph);
    1514             :                 pbGlyph += sizeof(int16);
    1515             :                 b = be::swap(*(int16 *)pbGlyph);
    1516             :                 pbGlyph += sizeof(int16);
    1517             :             }
    1518             :             else
    1519             :             { // args are signed bytes
    1520             :                 a = *pbGlyph++;
    1521             :                 b = *pbGlyph++;
    1522             :             }
    1523             :             return true;
    1524             :         }
    1525             :         pbGlyph += sizeof(uint16); // skip over glyph id of component
    1526             :         int nOffset = 0;
    1527             :         nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words  ? 4 : 2;
    1528             :         nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0;
    1529             :         nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale  ? 4 : 0;
    1530             :         nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo  ? 8 :  0;
    1531             :         pbGlyph += nOffset;
    1532             :     } while (GlyphFlags & CompoundGlyph::MoreComponents);
    1533             : 
    1534             :     // didn't find requested component
    1535             :     fOffset = true;
    1536             :     a = 0;
    1537             :     b = 0;
    1538             :     return false;
    1539             : }
    1540             : 
    1541             : /*----------------------------------------------------------------------------------------------
    1542             :     Return info on how a component glyph is to be transformed
    1543             :     pSimpleGlyph - assumed to point to a composite glyph
    1544             :     nCompId - glyph id for component of interest
    1545             :     flt11, flt11, flt11, flt11 - a 2x2 matrix giving the transform
    1546             :     bTransOffset - whether to transform the offset from above method 
    1547             :         The spec is unclear about the meaning of this flag
    1548             :         Currently - initialize to true for MS rasterizer and false for Mac rasterizer, then
    1549             :             on return it will indicate whether transform should apply to offset (MSDN CD 10/99)
    1550             :     Return true if successful, false otherwise
    1551             :         False could indicate a non-composite glyph or that component wasn't found
    1552             : ----------------------------------------------------------------------------------------------*/
    1553             : bool GetComponentTransform(const void * pSimpleGlyf, int nCompId, 
    1554             :                                     float & flt11, float & flt12, float & flt21, float & flt22, 
    1555             :                                     bool & fTransOffset)
    1556             : {
    1557             :     using namespace Sfnt;
    1558             :     
    1559             :     if (GlyfContourCount(pSimpleGlyf) >= 0)
    1560             :         return false;
    1561             : 
    1562             :     const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast<const Sfnt::SimpleGlyph *>(pSimpleGlyf);
    1563             :     // for a composite glyph, the special data begins here
    1564             :     const uint8 * pbGlyph = reinterpret_cast<const uint8 *>(&pGlyph->end_pts_of_contours[0]);
    1565             : 
    1566             :     uint16 GlyphFlags;
    1567             :     do 
    1568             :     {
    1569             :         GlyphFlags = be::swap(*((uint16 *)pbGlyph));
    1570             :         pbGlyph += sizeof(uint16);
    1571             :         if (be::swap(*((uint16 *)pbGlyph)) == nCompId)
    1572             :         {
    1573             :             pbGlyph += sizeof(uint16); // skip over glyph id of component
    1574             :             pbGlyph += GlyphFlags & CompoundGlyph::Arg1Arg2Words  ? 4 : 2; // skip over placement data
    1575             : 
    1576             :             if (fTransOffset) // MS rasterizer
    1577             :                 fTransOffset = !(GlyphFlags & CompoundGlyph::UnscaledOffset); 
    1578             :             else // Apple rasterizer
    1579             :                 fTransOffset = (GlyphFlags & CompoundGlyph::ScaledOffset) != 0;
    1580             : 
    1581             :             if (GlyphFlags & CompoundGlyph::HaveScale)
    1582             :             {
    1583             :                 flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph));
    1584             :                 pbGlyph += sizeof(uint16);
    1585             :                 flt12 = 0;
    1586             :                 flt21 = 0;
    1587             :                 flt22 = flt11;
    1588             :             }
    1589             :             else if (GlyphFlags & CompoundGlyph::HaveXAndYScale)
    1590             :             {
    1591             :                 flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph));
    1592             :                 pbGlyph += sizeof(uint16);
    1593             :                 flt12 = 0;
    1594             :                 flt21 = 0;
    1595             :                 flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph));
    1596             :                 pbGlyph += sizeof(uint16);
    1597             :             }
    1598             :             else if (GlyphFlags & CompoundGlyph::HaveTwoByTwo)
    1599             :             {
    1600             :                 flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph));
    1601             :                 pbGlyph += sizeof(uint16);
    1602             :                 flt12 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph));
    1603             :                 pbGlyph += sizeof(uint16);
    1604             :                 flt21 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph));
    1605             :                 pbGlyph += sizeof(uint16);
    1606             :                 flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph));
    1607             :                 pbGlyph += sizeof(uint16);
    1608             :             }
    1609             :             else
    1610             :             { // identity transform
    1611             :                 flt11 = 1.0;
    1612             :                 flt12 = 0.0;
    1613             :                 flt21 = 0.0;
    1614             :                 flt22 = 1.0;
    1615             :             }
    1616             :             return true;
    1617             :         }
    1618             :         pbGlyph += sizeof(uint16); // skip over glyph id of component
    1619             :         int nOffset = 0;
    1620             :         nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words  ? 4 : 2;
    1621             :         nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0;
    1622             :         nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale  ? 4 : 0;
    1623             :         nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo  ? 8 :  0;
    1624             :         pbGlyph += nOffset;
    1625             :     } while (GlyphFlags & CompoundGlyph::MoreComponents);
    1626             : 
    1627             :     // didn't find requested component
    1628             :     fTransOffset = false;
    1629             :     flt11 = 1;
    1630             :     flt12 = 0;
    1631             :     flt21 = 0;
    1632             :     flt22 = 1;
    1633             :     return false;
    1634             : }
    1635             : #endif
    1636             : 
    1637             : /*----------------------------------------------------------------------------------------------
    1638             :     Return a pointer into the glyf table based on the given tables and Glyph ID
    1639             :     Since this method doesn't check for spaces, it is good to call IsSpace before using it.
    1640             :     Return NULL on error.
    1641             : ----------------------------------------------------------------------------------------------*/
    1642           0 : void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 
    1643             :                            size_t lGlyfSize, size_t lLocaSize, const void * pHead)
    1644             : {
    1645             :     // test for valid glyph id
    1646             :     // CheckTable verifies the index_to_loc_format is valid
    1647             :     
    1648             :     const Sfnt::FontHeader * pTable 
    1649           0 :         = reinterpret_cast<const Sfnt::FontHeader *>(pHead);
    1650             : 
    1651           0 :     if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat)
    1652             :     { // loca entries are two bytes (and have been divided by two)
    1653           0 :         if (nGlyphId >= (lLocaSize >> 1) - 1) // don't allow nGlyphId to access sentinel
    1654             :         {
    1655             : //          throw std::out_of_range("glyph id out of range for font");
    1656           0 :             return NULL;
    1657             :         }
    1658             :     }
    1659           0 :     if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat)
    1660             :     { // loca entries are four bytes
    1661           0 :         if (nGlyphId >= (lLocaSize >> 2) - 1)
    1662             :         {
    1663             : //          throw std::out_of_range("glyph id out of range for font");
    1664           0 :             return NULL;
    1665             :         }
    1666             :     }
    1667             : 
    1668           0 :     long lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead);
    1669           0 :     void * pSimpleGlyf = GlyfLookup(pGlyf, lGlyfOffset, lGlyfSize); // invalid loca offset returns null
    1670           0 :     return pSimpleGlyf;
    1671             : }
    1672             : 
    1673             : #ifdef ALL_TTFUTILS
    1674             : /*----------------------------------------------------------------------------------------------
    1675             :     Determine if a particular Glyph ID has any data in the glyf table. If it is white space,
    1676             :     there will be no glyf data, though there will be metric data in hmtx, etc.
    1677             : ----------------------------------------------------------------------------------------------*/
    1678             : bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead)
    1679             : {
    1680             :     size_t lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead);
    1681             :     
    1682             :     // the +1 should always work because there is a sentinel value at the end of the loca table
    1683             :     size_t lNextGlyfOffset = LocaLookup(nGlyphId + 1, pLoca, lLocaSize, pHead);
    1684             : 
    1685             :     return (lNextGlyfOffset - lGlyfOffset) == 0;
    1686             : }
    1687             : 
    1688             : /*----------------------------------------------------------------------------------------------
    1689             :     Determine if a particular Glyph ID is a multi-level composite.
    1690             : ----------------------------------------------------------------------------------------------*/
    1691             : bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 
    1692             :                     size_t lGlyfSize, long lLocaSize, const void * pHead)
    1693             : {
    1694             :     if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;}
    1695             : 
    1696             :     void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1697             :     if (pSimpleGlyf == NULL)
    1698             :         return false; // no way to really indicate an error occured here
    1699             : 
    1700             :     if (GlyfContourCount(pSimpleGlyf) >= 0)
    1701             :         return false;
    1702             : 
    1703             :     int rgnCompId[kMaxGlyphComponents]; // assumes only a limited number of glyph components
    1704             :     size_t cCompIdTotal = kMaxGlyphComponents;
    1705             :     size_t cCompId = 0;
    1706             : 
    1707             :     if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId))
    1708             :         return false;
    1709             : 
    1710             :     for (size_t i = 0; i < cCompId; i++)
    1711             :     {
    1712             :         pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), 
    1713             :                             pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1714             :         if (pSimpleGlyf == NULL) {return false;}
    1715             : 
    1716             :         if (GlyfContourCount(pSimpleGlyf) < 0)
    1717             :             return true;
    1718             :     }
    1719             : 
    1720             :     return false;
    1721             : }
    1722             : 
    1723             : /*----------------------------------------------------------------------------------------------
    1724             :     Get the bounding box coordinates based on the given tables and Glyph ID
    1725             :     Handles both simple and composite glyphs.
    1726             :     Return true if successful, false otherwise. On false, all point values will be INT_MIN
    1727             :         False may indicate a white space glyph
    1728             : ----------------------------------------------------------------------------------------------*/
    1729             : bool GlyfBox(gid16  nGlyphId, const void * pGlyf, const void * pLoca, 
    1730             :         size_t lGlyfSize, size_t lLocaSize, const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax)
    1731             : {
    1732             :     xMin = yMin = xMax = yMax = INT_MIN;
    1733             : 
    1734             :     if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;}
    1735             : 
    1736             :     void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1737             :     if (pSimpleGlyf == NULL) {return false;}
    1738             : 
    1739             :     return GlyfBox(pSimpleGlyf, xMin, yMin, xMax, yMax);
    1740             : }
    1741             : 
    1742             : /*----------------------------------------------------------------------------------------------
    1743             :     Get the number of contours based on the given tables and Glyph ID
    1744             :     Handles both simple and composite glyphs.
    1745             :     Return true if successful, false otherwise. On false, cnContours will be INT_MIN
    1746             :         False may indicate a white space glyph or a multi-level composite glyph.
    1747             : ----------------------------------------------------------------------------------------------*/
    1748             : bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 
    1749             :     size_t lGlyfSize, size_t lLocaSize, const void * pHead, size_t & cnContours)
    1750             : {
    1751             :     cnContours = static_cast<size_t>(INT_MIN);
    1752             : 
    1753             :     if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;}
    1754             : 
    1755             :     void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1756             :     if (pSimpleGlyf == NULL) {return false;}
    1757             : 
    1758             :     int cRtnContours = GlyfContourCount(pSimpleGlyf);
    1759             :     if (cRtnContours >= 0)
    1760             :     {
    1761             :         cnContours = size_t(cRtnContours);
    1762             :         return true;
    1763             :     }
    1764             :         
    1765             :     //handle composite glyphs
    1766             : 
    1767             :     int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components
    1768             :     size_t cCompIdTotal = kMaxGlyphComponents;
    1769             :     size_t cCompId = 0;
    1770             : 
    1771             :     if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId))
    1772             :         return false;
    1773             : 
    1774             :     cRtnContours = 0;
    1775             :     int cTmp = 0;
    1776             :     for (size_t i = 0; i < cCompId; i++)
    1777             :     {
    1778             :         if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;}
    1779             :         pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), 
    1780             :                                  pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1781             :         if (pSimpleGlyf == 0) {return false;}
    1782             :         // return false on multi-level composite
    1783             :         if ((cTmp = GlyfContourCount(pSimpleGlyf)) < 0) 
    1784             :             return false;
    1785             :         cRtnContours += cTmp;
    1786             :     }
    1787             : 
    1788             :     cnContours = size_t(cRtnContours);
    1789             :     return true;
    1790             : }
    1791             : 
    1792             : /*----------------------------------------------------------------------------------------------
    1793             :     Get the point numbers for the end points of the glyph contours based on the given tables 
    1794             :     and Glyph ID
    1795             :     Handles both simple and composite glyphs.
    1796             :     cnPoints - count of contours from GlyfContourCount (same as number of end points)
    1797             :     prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers
    1798             :     Return true if successful, false otherwise. On false, all end points are INT_MIN
    1799             :         False may indicate a white space glyph or a multi-level composite glyph.
    1800             : ----------------------------------------------------------------------------------------------*/
    1801             : bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 
    1802             :     size_t lGlyfSize, size_t lLocaSize, const void * pHead,
    1803             :     int * prgnContourEndPoint, size_t cnPoints)
    1804             : {
    1805             :         memset(prgnContourEndPoint, 0xFF, cnPoints * sizeof(int));
    1806             :     // std::fill_n(prgnContourEndPoint, cnPoints, INT_MIN);
    1807             : 
    1808             :     if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;}
    1809             : 
    1810             :     void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1811             :     if (pSimpleGlyf == NULL) {return false;}
    1812             : 
    1813             :     int cContours = GlyfContourCount(pSimpleGlyf);
    1814             :     int cActualPts = 0;
    1815             :     if (cContours > 0)
    1816             :         return GlyfContourEndPoints(pSimpleGlyf, prgnContourEndPoint, cnPoints, cActualPts);
    1817             :     
    1818             :     // handle composite glyphs
    1819             :     
    1820             :     int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components
    1821             :     size_t cCompIdTotal = kMaxGlyphComponents;
    1822             :     size_t cCompId = 0;
    1823             : 
    1824             :     if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId))
    1825             :         return false;
    1826             : 
    1827             :     int * prgnCurrentEndPoint = prgnContourEndPoint;
    1828             :     int cCurrentPoints = cnPoints;
    1829             :     int nPrevPt = 0;
    1830             :     for (size_t i = 0; i < cCompId; i++)
    1831             :     {
    1832             :         if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;}
    1833             :         pSimpleGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1834             :         if (pSimpleGlyf == NULL) {return false;}
    1835             :         // returns false on multi-level composite
    1836             :         if (!GlyfContourEndPoints(pSimpleGlyf, prgnCurrentEndPoint, cCurrentPoints, cActualPts))
    1837             :             return false;
    1838             :         // points in composite are numbered sequentially as components are added
    1839             :         //  must adjust end point numbers for new point numbers
    1840             :         for (int j = 0; j < cActualPts; j++)
    1841             :             prgnCurrentEndPoint[j] += nPrevPt;
    1842             :         nPrevPt = prgnCurrentEndPoint[cActualPts - 1] + 1;
    1843             : 
    1844             :         prgnCurrentEndPoint += cActualPts;
    1845             :         cCurrentPoints -= cActualPts;
    1846             :     }
    1847             : 
    1848             :     return true;
    1849             : }
    1850             : 
    1851             : /*----------------------------------------------------------------------------------------------
    1852             :     Get the points for a glyph based on the given tables and Glyph ID
    1853             :     Handles both simple and composite glyphs.
    1854             :     cnPoints - count of points from largest end point obtained from GlyfContourEndPoints
    1855             :     prgnX & prgnY - should point to buffers large enough to hold cnPoints integers
    1856             :         The ranges are parallel so that coordinates for point(n) are found at offset n in 
    1857             :         both ranges. These points are in absolute coordinates.
    1858             :     prgfOnCurve - should point to a buffer a large enough to hold cnPoints bytes (bool)
    1859             :         This range is parallel to the prgnX & prgnY
    1860             :     Return true if successful, false otherwise. On false, all points may be INT_MIN
    1861             :         False may indicate a white space glyph, a multi-level composite, or a corrupt font
    1862             :         It's not clear from the TTF spec when the transforms should be applied. Should the 
    1863             :         transform be done before or after attachment point calcs? (current code - before) 
    1864             :         Should the transform be applied to other offsets? (currently - no; however commented 
    1865             :         out code is in place so that if CompoundGlyph::UnscaledOffset on the MS rasterizer is 
    1866             :         clear (typical) then yes, and if CompoundGlyph::ScaledOffset on the Apple rasterizer is 
    1867             :         clear (typical?) then no). See GetComponentTransform.
    1868             :         It's also unclear where point numbering with attachment poinst starts 
    1869             :         (currently - first point number is relative to whole glyph, second point number is 
    1870             :         relative to current glyph). 
    1871             : ----------------------------------------------------------------------------------------------*/
    1872             : bool GlyfPoints(gid16 nGlyphId, const void * pGlyf,
    1873             :         const void * pLoca, size_t lGlyfSize, size_t lLocaSize, const void * pHead,
    1874             :         const int * /*prgnContourEndPoint*/, size_t /*cnEndPoints*/,
    1875             :         int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints)
    1876             : {
    1877             :         memset(prgnX, 0x7F, cnPoints * sizeof(int));
    1878             :         memset(prgnY, 0x7F, cnPoints * sizeof(int));
    1879             : 
    1880             :     if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) 
    1881             :         return false;
    1882             : 
    1883             :     void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1884             :     if (pSimpleGlyf == NULL)
    1885             :         return false;
    1886             : 
    1887             :     int cContours = GlyfContourCount(pSimpleGlyf);
    1888             :     int cActualPts;
    1889             :     if (cContours > 0)
    1890             :     {
    1891             :         if (!GlyfPoints(pSimpleGlyf, prgnX, prgnY, (char *)prgfOnCurve, cnPoints, cActualPts))
    1892             :             return false;
    1893             :         CalcAbsolutePoints(prgnX, prgnY, cnPoints);
    1894             :         SimplifyFlags((char *)prgfOnCurve, cnPoints);
    1895             :         return true;
    1896             :     }
    1897             : 
    1898             :     // handle composite glyphs  
    1899             :     int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components
    1900             :     size_t cCompIdTotal = kMaxGlyphComponents;
    1901             :     size_t cCompId = 0;
    1902             : 
    1903             :     // this will fail if there are more components than there is room for
    1904             :     if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId))
    1905             :         return false;
    1906             : 
    1907             :     int * prgnCurrentX = prgnX;
    1908             :     int * prgnCurrentY = prgnY;
    1909             :     char * prgbCurrentFlag = (char *)prgfOnCurve; // converting bool to char should be safe
    1910             :     int cCurrentPoints = cnPoints;
    1911             :     bool fOffset = true, fTransOff = true;
    1912             :     int a, b;
    1913             :     float flt11, flt12, flt21, flt22;
    1914             :     // int * prgnPrevX = prgnX; // in case first att pt number relative to preceding glyph
    1915             :     // int * prgnPrevY = prgnY;
    1916             :     for (size_t i = 0; i < cCompId; i++)
    1917             :     {
    1918             :         if (IsSpace(static_cast<gid16>(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;}
    1919             :         void * pCompGlyf = GlyfLookup(static_cast<gid16>(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead);
    1920             :         if (pCompGlyf == NULL) {return false;}
    1921             :         // returns false on multi-level composite
    1922             :         if (!GlyfPoints(pCompGlyf, prgnCurrentX, prgnCurrentY, prgbCurrentFlag, 
    1923             :             cCurrentPoints, cActualPts))
    1924             :             return false; 
    1925             :         if (!GetComponentPlacement(pSimpleGlyf, rgnCompId[i], fOffset, a, b))
    1926             :             return false;
    1927             :         if (!GetComponentTransform(pSimpleGlyf, rgnCompId[i], 
    1928             :             flt11, flt12, flt21, flt22, fTransOff))
    1929             :             return false;
    1930             :         bool fIdTrans = flt11 == 1.0 && flt12 == 0.0 && flt21 == 0.0 && flt22 == 1.0;
    1931             : 
    1932             :         // convert points to absolute coordinates
    1933             :         // do before transform and attachment point placement are applied
    1934             :         CalcAbsolutePoints(prgnCurrentX, prgnCurrentY, cActualPts);
    1935             : 
    1936             :         // apply transform - see main method note above
    1937             :         // do before attachment point calcs
    1938             :         if (!fIdTrans)
    1939             :             for (int j = 0; j < cActualPts; j++)
    1940             :             {
    1941             :                 int x = prgnCurrentX[j]; // store before transform applied
    1942             :                 int y = prgnCurrentY[j];
    1943             :                 prgnCurrentX[j] = (int)(x * flt11 + y * flt12);
    1944             :                 prgnCurrentY[j] = (int)(x * flt21 + y * flt22);
    1945             :             }
    1946             :             
    1947             :         // apply placement - see main method note above
    1948             :         int nXOff, nYOff;
    1949             :         if (fOffset) // explicit x & y offsets
    1950             :         { 
    1951             :             /* ignore fTransOff for now
    1952             :             if (fTransOff && !fIdTrans) 
    1953             :             {   // transform x & y offsets
    1954             :                 nXOff = (int)(a * flt11 + b * flt12);
    1955             :                 nYOff = (int)(a * flt21 + b * flt22);
    1956             :             }
    1957             :             else */ 
    1958             :             { // don't transform offset
    1959             :                 nXOff = a;
    1960             :                 nYOff = b;
    1961             :             }
    1962             :         }
    1963             :         else  // attachment points
    1964             :         {   // in case first point is relative to preceding glyph and second relative to current
    1965             :             // nXOff = prgnPrevX[a] - prgnCurrentX[b];
    1966             :             // nYOff = prgnPrevY[a] - prgnCurrentY[b];
    1967             :             // first point number relative to whole composite, second relative to current glyph
    1968             :             nXOff = prgnX[a] - prgnCurrentX[b];
    1969             :             nYOff = prgnY[a] - prgnCurrentY[b];
    1970             :         }
    1971             :         for (int j = 0; j < cActualPts; j++)
    1972             :         {
    1973             :             prgnCurrentX[j] += nXOff;
    1974             :             prgnCurrentY[j] += nYOff;
    1975             :         }
    1976             : 
    1977             :         // prgnPrevX = prgnCurrentX;
    1978             :         // prgnPrevY = prgnCurrentY;
    1979             :         prgnCurrentX += cActualPts;
    1980             :         prgnCurrentY += cActualPts;
    1981             :         prgbCurrentFlag += cActualPts;
    1982             :         cCurrentPoints -= cActualPts;
    1983             :     }
    1984             : 
    1985             :     SimplifyFlags((char *)prgfOnCurve, cnPoints);
    1986             : 
    1987             :     return true;
    1988             : }
    1989             : 
    1990             : /*----------------------------------------------------------------------------------------------
    1991             :     Simplify the meaning of flags to just indicate whether point is on-curve or off-curve.
    1992             : ---------------------------------------------------------------------------------------------*/
    1993             : bool SimplifyFlags(char * prgbFlags, int cnPoints)
    1994             : {
    1995             :     for (int i = 0; i < cnPoints; i++)
    1996             :         prgbFlags[i] = static_cast<char>(prgbFlags[i] & Sfnt::SimpleGlyph::OnCurve);
    1997             :     return true;
    1998             : }
    1999             : 
    2000             : /*----------------------------------------------------------------------------------------------
    2001             :     Convert relative point coordinates to absolute coordinates
    2002             :     Points are stored in the font such that they are offsets from one another except for the 
    2003             :         first point of a glyph.
    2004             : ---------------------------------------------------------------------------------------------*/
    2005             : bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints)
    2006             : {
    2007             :     int nX = prgnX[0];
    2008             :     int nY = prgnY[0];
    2009             :     for (int i = 1; i < cnPoints; i++)
    2010             :     {
    2011             :         prgnX[i] += nX;
    2012             :         nX = prgnX[i];
    2013             :         prgnY[i] += nY;
    2014             :         nY = prgnY[i];
    2015             :     }
    2016             : 
    2017             :     return true;
    2018             : }
    2019             : #endif
    2020             : 
    2021             : /*----------------------------------------------------------------------------------------------
    2022             :     Return the length of the 'name' table in bytes.
    2023             :     Currently used.
    2024             : ---------------------------------------------------------------------------------------------*/
    2025             : #if 0
    2026             : size_t NameTableLength(const byte * pTable)
    2027             : {
    2028             :     byte * pb = (const_cast<byte *>(pTable)) + 2; // skip format
    2029             :     size_t cRecords = *pb++ << 8; cRecords += *pb++;
    2030             :     int dbStringOffset0 = (*pb++) << 8; dbStringOffset0 += *pb++;
    2031             :     int dbMaxStringOffset = 0;
    2032             :     for (size_t irec = 0; irec < cRecords; irec++)
    2033             :     {
    2034             :         int nPlatform = (*pb++) << 8; nPlatform += *pb++;
    2035             :         int nEncoding = (*pb++) << 8; nEncoding += *pb++;
    2036             :         int nLanguage = (*pb++) << 8; nLanguage += *pb++;
    2037             :         int nName = (*pb++) << 8; nName += *pb++;
    2038             :         int cbStringLen = (*pb++) << 8; cbStringLen += *pb++;
    2039             :         int dbStringOffset = (*pb++) << 8; dbStringOffset += *pb++;
    2040             :         if (dbMaxStringOffset < dbStringOffset + cbStringLen)
    2041             :             dbMaxStringOffset = dbStringOffset + cbStringLen;
    2042             :     }
    2043             :     return dbStringOffset0 + dbMaxStringOffset;
    2044             : }
    2045             : #endif
    2046             : 
    2047             : } // end of namespace TtfUtil
    2048             : } // end of namespace graphite

Generated by: LCOV version 1.13