1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/graphite2/src/inc/TtfUtil.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 1.4 +/* GRAPHITE2 LICENSING 1.5 + 1.6 + Copyright 2010, SIL International 1.7 + All rights reserved. 1.8 + 1.9 + This library is free software; you can redistribute it and/or modify 1.10 + it under the terms of the GNU Lesser General Public License as published 1.11 + by the Free Software Foundation; either version 2.1 of License, or 1.12 + (at your option) any later version. 1.13 + 1.14 + This program is distributed in the hope that it will be useful, 1.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.17 + Lesser General Public License for more details. 1.18 + 1.19 + You should also have received a copy of the GNU Lesser General Public 1.20 + License along with this library in the file named "LICENSE". 1.21 + If not, write to the Free Software Foundation, 51 Franklin Street, 1.22 + Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 1.23 + internet at http://www.fsf.org/licenses/lgpl.html. 1.24 + 1.25 +Alternatively, the contents of this file may be used under the terms of the 1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public 1.27 +License, as published by the Free Software Foundation, either version 2 1.28 +of the License or (at your option) any later version. 1.29 +*/ 1.30 +#pragma once 1.31 +/*--------------------------------------------------------------------*//*:Ignore this sentence. 1.32 + 1.33 +File: TtfUtil.h 1.34 +Responsibility: Alan Ward 1.35 +Last reviewed: Not yet. 1.36 + 1.37 +Description: 1.38 + Utility class for handling TrueType font files. 1.39 +----------------------------------------------------------------------------------------------*/ 1.40 + 1.41 + 1.42 +#include <cstddef> 1.43 + 1.44 +namespace graphite2 1.45 +{ 1.46 +namespace TtfUtil 1.47 +{ 1.48 + 1.49 +typedef long fontTableId32; 1.50 +typedef unsigned short gid16; 1.51 + 1.52 +#define TTF_TAG(a,b,c,d) ((a << 24UL) + (b << 16UL) + (c << 8UL) + (d)) 1.53 + 1.54 +// Enumeration used to specify a table in a TTF file 1.55 +class Tag 1.56 +{ 1.57 + unsigned long _v; 1.58 +public: 1.59 + Tag(const char n[5]) throw() : _v(TTF_TAG(n[0],n[1],n[2],n[3])) {} 1.60 + Tag(const unsigned long tag) throw() : _v(tag) {} 1.61 + 1.62 + operator unsigned long () const throw () { return _v; } 1.63 + 1.64 + enum 1.65 + { 1.66 + Feat = TTF_TAG('F','e','a','t'), 1.67 + Glat = TTF_TAG('G','l','a','t'), 1.68 + Gloc = TTF_TAG('G','l','o','c'), 1.69 + Sile = TTF_TAG('S','i','l','e'), 1.70 + Silf = TTF_TAG('S','i','l','f'), 1.71 + Sill = TTF_TAG('S','i','l','l'), 1.72 + cmap = TTF_TAG('c','m','a','p'), 1.73 + cvt = TTF_TAG('c','v','t',' '), 1.74 + cryp = TTF_TAG('c','r','y','p'), 1.75 + head = TTF_TAG('h','e','a','d'), 1.76 + fpgm = TTF_TAG('f','p','g','m'), 1.77 + gdir = TTF_TAG('g','d','i','r'), 1.78 + glyf = TTF_TAG('g','l','y','f'), 1.79 + hdmx = TTF_TAG('h','d','m','x'), 1.80 + hhea = TTF_TAG('h','h','e','a'), 1.81 + hmtx = TTF_TAG('h','m','t','x'), 1.82 + loca = TTF_TAG('l','o','c','a'), 1.83 + kern = TTF_TAG('k','e','r','n'), 1.84 + LTSH = TTF_TAG('L','T','S','H'), 1.85 + maxp = TTF_TAG('m','a','x','p'), 1.86 + name = TTF_TAG('n','a','m','e'), 1.87 + OS_2 = TTF_TAG('O','S','/','2'), 1.88 + post = TTF_TAG('p','o','s','t'), 1.89 + prep = TTF_TAG('p','r','e','p') 1.90 + }; 1.91 +}; 1.92 + 1.93 +/*---------------------------------------------------------------------------------------------- 1.94 + Class providing utility methods to parse a TrueType font file (TTF). 1.95 + Callling application handles all file input and memory allocation. 1.96 + Assumes minimal knowledge of TTF file format. 1.97 +----------------------------------------------------------------------------------------------*/ 1.98 + ////////////////////////////////// tools to find & check TTF tables 1.99 + bool GetHeaderInfo(size_t & lOffset, size_t & lSize); 1.100 + bool CheckHeader(const void * pHdr); 1.101 + bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize); 1.102 + bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir, 1.103 + size_t & lOffset, size_t & lSize); 1.104 + bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize); 1.105 + 1.106 + ////////////////////////////////// simple font wide info 1.107 + size_t GlyphCount(const void * pMaxp); 1.108 +#ifdef ALL_TTFUTILS 1.109 + size_t MaxCompositeComponentCount(const void * pMaxp); 1.110 + size_t MaxCompositeLevelCount(const void * pMaxp); 1.111 + size_t LocaGlyphCount(size_t lLocaSize, const void * pHead); // throw (std::domain_error); 1.112 +#endif 1.113 + int DesignUnits(const void * pHead); 1.114 +#ifdef ALL_TTFUTILS 1.115 + int HeadTableCheckSum(const void * pHead); 1.116 + void HeadTableCreateTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD); 1.117 + void HeadTableModifyTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD); 1.118 + bool IsItalic(const void * pHead); 1.119 + int FontAscent(const void * pOs2); 1.120 + int FontDescent(const void * pOs2); 1.121 + bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic); 1.122 + bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize); 1.123 + bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize); 1.124 + bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize); 1.125 + bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize); 1.126 + int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp, 1.127 + const char * pPostName); 1.128 +#endif 1.129 + 1.130 + ////////////////////////////////// utility methods helpful for name table 1.131 + bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId, 1.132 + int nLangId, int nNameId, size_t & lOffset, size_t & lSize); 1.133 + //size_t NameTableLength(const byte * pTable); 1.134 +#ifdef ALL_TTFUTILS 1.135 + int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId, 1.136 + int *nameIdList, int cNameIds, short *langIdList); 1.137 + void SwapWString(void * pWStr, size_t nSize = 0); // throw (std::invalid_argument); 1.138 +#endif 1.139 + 1.140 + ////////////////////////////////// cmap lookup tools 1.141 + const void * FindCmapSubtable(const void * pCmap, int nPlatformId = 3, 1.142 + int nEncodingId = 1, size_t length = 0); 1.143 + bool CheckCmapSubtable4(const void * pCmap31); 1.144 + gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey = 0); 1.145 + unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId, 1.146 + int * pRangeKey = 0); 1.147 + bool CheckCmapSubtable12(const void *pCmap310); 1.148 + gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey = 0); 1.149 + unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId, 1.150 + int * pRangeKey = 0); 1.151 + 1.152 + ///////////////////////////////// horizontal metric data for a glyph 1.153 + bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize, 1.154 + const void * pHhea, int & nLsb, unsigned int & nAdvWid); 1.155 + 1.156 + ////////////////////////////////// primitives for loca and glyf lookup 1.157 + size_t LocaLookup(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, 1.158 + const void * pHead); // throw (std::out_of_range); 1.159 + void * GlyfLookup(const void * pGlyf, size_t lGlyfOffset, size_t lTableLen); 1.160 + 1.161 + ////////////////////////////////// primitves for simple glyph data 1.162 + bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin, 1.163 + int & xMax, int & yMax); 1.164 + 1.165 +#ifdef ALL_TTFUTILS 1.166 + int GlyfContourCount(const void * pSimpleGlyf); 1.167 + bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint, 1.168 + int cnPointsTotal, size_t & cnPoints); 1.169 + bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY, 1.170 + char * prgbFlag, int cnPointsTotal, int & cnPoints); 1.171 + 1.172 + // primitive to find the glyph ids in a composite glyph 1.173 + bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId, 1.174 + size_t cnCompIdTotal, size_t & cnCompId); 1.175 + // primitive to find the placement data for a component in a composite glyph 1.176 + bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId, 1.177 + bool fOffset, int & a, int & b); 1.178 + // primitive to find the transform data for a component in a composite glyph 1.179 + bool GetComponentTransform(const void * pSimpleGlyf, int nCompId, 1.180 + float & flt11, float & flt12, float & flt21, float & flt22, bool & fTransOffset); 1.181 +#endif 1.182 + 1.183 + ////////////////////////////////// operate on composite or simple glyph (auto glyf lookup) 1.184 + void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 1.185 + size_t lGlyfSize, size_t lLocaSize, const void * pHead); // primitive used by below methods 1.186 + 1.187 +#ifdef ALL_TTFUTILS 1.188 + // below are primary user methods for handling glyf data 1.189 + bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead); 1.190 + bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 1.191 + size_t lGlyfSize, size_t lLocaSize, const void * pHead); 1.192 + 1.193 + bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, size_t lGlyfSize, size_t lLocaSize, 1.194 + const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax); 1.195 + bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 1.196 + size_t lGlyfSize, size_t lLocaSize, const void *pHead, size_t & cnContours); 1.197 + bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 1.198 + size_t lGlyfSize, size_t lLocaSize, const void * pHead, int * prgnContourEndPoint, size_t cnPoints); 1.199 + bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, 1.200 + size_t lGlyfSize, size_t lLocaSize, const void * pHead, const int * prgnContourEndPoint, size_t cnEndPoints, 1.201 + int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints); 1.202 + 1.203 + // utitily method used by high-level GlyfPoints 1.204 + bool SimplifyFlags(char * prgbFlags, int cnPoints); 1.205 + bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints); 1.206 +#endif 1.207 + 1.208 +} // end of namespace TtfUtil 1.209 +} // end of namespace graphite2