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 : #pragma once
28 : /*--------------------------------------------------------------------*//*:Ignore this sentence.
29 :
30 : File: TtfUtil.h
31 : Responsibility: Alan Ward
32 : Last reviewed: Not yet.
33 :
34 : Description:
35 : Utility class for handling TrueType font files.
36 : ----------------------------------------------------------------------------------------------*/
37 :
38 :
39 : //#include <cstddef>
40 : //#include <stdexcept>
41 :
42 : namespace graphite2
43 : {
44 : namespace TtfUtil
45 : {
46 :
47 : typedef long fontTableId32;
48 : typedef unsigned short gid16;
49 :
50 : #define TTF_TAG(a,b,c,d) ((a << 24UL) + (b << 16UL) + (c << 8UL) + (d))
51 :
52 : // Enumeration used to specify a table in a TTF file
53 : class Tag
54 : {
55 : unsigned long _v;
56 : public:
57 : Tag(const char n[5]) throw() : _v(TTF_TAG(n[0],n[1],n[2],n[3])) {}
58 0 : Tag(const unsigned long tag) throw() : _v(tag) {}
59 :
60 0 : operator unsigned long () const throw () { return _v; }
61 :
62 : enum
63 : {
64 : Feat = TTF_TAG('F','e','a','t'),
65 : Glat = TTF_TAG('G','l','a','t'),
66 : Gloc = TTF_TAG('G','l','o','c'),
67 : Sile = TTF_TAG('S','i','l','e'),
68 : Silf = TTF_TAG('S','i','l','f'),
69 : Sill = TTF_TAG('S','i','l','l'),
70 : cmap = TTF_TAG('c','m','a','p'),
71 : cvt = TTF_TAG('c','v','t',' '),
72 : cryp = TTF_TAG('c','r','y','p'),
73 : head = TTF_TAG('h','e','a','d'),
74 : fpgm = TTF_TAG('f','p','g','m'),
75 : gdir = TTF_TAG('g','d','i','r'),
76 : glyf = TTF_TAG('g','l','y','f'),
77 : hdmx = TTF_TAG('h','d','m','x'),
78 : hhea = TTF_TAG('h','h','e','a'),
79 : hmtx = TTF_TAG('h','m','t','x'),
80 : loca = TTF_TAG('l','o','c','a'),
81 : kern = TTF_TAG('k','e','r','n'),
82 : LTSH = TTF_TAG('L','T','S','H'),
83 : maxp = TTF_TAG('m','a','x','p'),
84 : name = TTF_TAG('n','a','m','e'),
85 : OS_2 = TTF_TAG('O','S','/','2'),
86 : post = TTF_TAG('p','o','s','t'),
87 : prep = TTF_TAG('p','r','e','p')
88 : };
89 : };
90 :
91 : /*----------------------------------------------------------------------------------------------
92 : Class providing utility methods to parse a TrueType font file (TTF).
93 : Callling application handles all file input and memory allocation.
94 : Assumes minimal knowledge of TTF file format.
95 : ----------------------------------------------------------------------------------------------*/
96 : ////////////////////////////////// tools to find & check TTF tables
97 : bool GetHeaderInfo(size_t & lOffset, size_t & lSize);
98 : bool CheckHeader(const void * pHdr);
99 : bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize);
100 : bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir,
101 : size_t & lOffset, size_t & lSize);
102 : bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize);
103 :
104 : ////////////////////////////////// simple font wide info
105 : size_t GlyphCount(const void * pMaxp);
106 : #ifdef ALL_TTFUTILS
107 : size_t MaxCompositeComponentCount(const void * pMaxp);
108 : size_t MaxCompositeLevelCount(const void * pMaxp);
109 : size_t LocaGlyphCount(size_t lLocaSize, const void * pHead); // throw (std::domain_error);
110 : #endif
111 : int DesignUnits(const void * pHead);
112 : #ifdef ALL_TTFUTILS
113 : int HeadTableCheckSum(const void * pHead);
114 : void HeadTableCreateTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD);
115 : void HeadTableModifyTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD);
116 : bool IsItalic(const void * pHead);
117 : int FontAscent(const void * pOs2);
118 : int FontDescent(const void * pOs2);
119 : bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic);
120 : bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize);
121 : bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize);
122 : bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize);
123 : bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize);
124 : int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp,
125 : const char * pPostName);
126 : #endif
127 :
128 : ////////////////////////////////// utility methods helpful for name table
129 : bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId,
130 : int nLangId, int nNameId, size_t & lOffset, size_t & lSize);
131 : //size_t NameTableLength(const byte * pTable);
132 : #ifdef ALL_TTFUTILS
133 : int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId,
134 : int *nameIdList, int cNameIds, short *langIdList);
135 : void SwapWString(void * pWStr, size_t nSize = 0); // throw (std::invalid_argument);
136 : #endif
137 :
138 : ////////////////////////////////// cmap lookup tools
139 : const void * FindCmapSubtable(const void * pCmap, int nPlatformId = 3,
140 : int nEncodingId = 1, size_t length = 0);
141 : bool CheckCmap31Subtable(const void * pCmap31);
142 : gid16 Cmap31Lookup(const void * pCmap31, int nUnicodeId, int rangeKey = 0);
143 : unsigned int Cmap31NextCodepoint(const void *pCmap31, unsigned int nUnicodeId,
144 : int * pRangeKey = 0);
145 : bool CheckCmap310Subtable(const void *pCmap310);
146 : gid16 Cmap310Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey = 0);
147 : unsigned int Cmap310NextCodepoint(const void *pCmap310, unsigned int nUnicodeId,
148 : int * pRangeKey = 0);
149 :
150 : ///////////////////////////////// horizontal metric data for a glyph
151 : bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize,
152 : const void * pHhea, int & nLsb, unsigned int & nAdvWid);
153 :
154 : ////////////////////////////////// primitives for loca and glyf lookup
155 : size_t LocaLookup(gid16 nGlyphId, const void * pLoca, size_t lLocaSize,
156 : const void * pHead); // throw (std::out_of_range);
157 : void * GlyfLookup(const void * pGlyf, size_t lGlyfOffset, size_t lTableLen);
158 :
159 : ////////////////////////////////// primitves for simple glyph data
160 : bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin,
161 : int & xMax, int & yMax);
162 :
163 : #ifdef ALL_TTFUTILS
164 : int GlyfContourCount(const void * pSimpleGlyf);
165 : bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint,
166 : int cnPointsTotal, size_t & cnPoints);
167 : bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY,
168 : char * prgbFlag, int cnPointsTotal, int & cnPoints);
169 :
170 : // primitive to find the glyph ids in a composite glyph
171 : bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId,
172 : size_t cnCompIdTotal, size_t & cnCompId);
173 : // primitive to find the placement data for a component in a composite glyph
174 : bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId,
175 : bool fOffset, int & a, int & b);
176 : // primitive to find the transform data for a component in a composite glyph
177 : bool GetComponentTransform(const void * pSimpleGlyf, int nCompId,
178 : float & flt11, float & flt12, float & flt21, float & flt22, bool & fTransOffset);
179 : #endif
180 :
181 : ////////////////////////////////// operate on composite or simple glyph (auto glyf lookup)
182 : void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
183 : size_t lGlyfSize, size_t lLocaSize, const void * pHead); // primitive used by below methods
184 :
185 : #ifdef ALL_TTFUTILS
186 : // below are primary user methods for handling glyf data
187 : bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead);
188 : bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
189 : size_t lGlyfSize, size_t lLocaSize, const void * pHead);
190 :
191 : bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, size_t lGlyfSize, size_t lLocaSize,
192 : const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax);
193 : bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
194 : size_t lGlyfSize, size_t lLocaSize, const void *pHead, size_t & cnContours);
195 : bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
196 : size_t lGlyfSize, size_t lLocaSize, const void * pHead, int * prgnContourEndPoint, size_t cnPoints);
197 : bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
198 : size_t lGlyfSize, size_t lLocaSize, const void * pHead, const int * prgnContourEndPoint, size_t cnEndPoints,
199 : int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints);
200 :
201 : // utitily method used by high-level GlyfPoints
202 : bool SimplifyFlags(char * prgbFlags, int cnPoints);
203 : bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints);
204 : #endif
205 :
206 : } // end of namespace TtfUtil
207 : } // end of namespace graphite2
|