gfx/graphite2/src/inc/TtfUtil.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* GRAPHITE2 LICENSING
michael@0 2
michael@0 3 Copyright 2010, SIL International
michael@0 4 All rights reserved.
michael@0 5
michael@0 6 This library is free software; you can redistribute it and/or modify
michael@0 7 it under the terms of the GNU Lesser General Public License as published
michael@0 8 by the Free Software Foundation; either version 2.1 of License, or
michael@0 9 (at your option) any later version.
michael@0 10
michael@0 11 This program is distributed in the hope that it will be useful,
michael@0 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michael@0 14 Lesser General Public License for more details.
michael@0 15
michael@0 16 You should also have received a copy of the GNU Lesser General Public
michael@0 17 License along with this library in the file named "LICENSE".
michael@0 18 If not, write to the Free Software Foundation, 51 Franklin Street,
michael@0 19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
michael@0 20 internet at http://www.fsf.org/licenses/lgpl.html.
michael@0 21
michael@0 22 Alternatively, the contents of this file may be used under the terms of the
michael@0 23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
michael@0 24 License, as published by the Free Software Foundation, either version 2
michael@0 25 of the License or (at your option) any later version.
michael@0 26 */
michael@0 27 #pragma once
michael@0 28 /*--------------------------------------------------------------------*//*:Ignore this sentence.
michael@0 29
michael@0 30 File: TtfUtil.h
michael@0 31 Responsibility: Alan Ward
michael@0 32 Last reviewed: Not yet.
michael@0 33
michael@0 34 Description:
michael@0 35 Utility class for handling TrueType font files.
michael@0 36 ----------------------------------------------------------------------------------------------*/
michael@0 37
michael@0 38
michael@0 39 #include <cstddef>
michael@0 40
michael@0 41 namespace graphite2
michael@0 42 {
michael@0 43 namespace TtfUtil
michael@0 44 {
michael@0 45
michael@0 46 typedef long fontTableId32;
michael@0 47 typedef unsigned short gid16;
michael@0 48
michael@0 49 #define TTF_TAG(a,b,c,d) ((a << 24UL) + (b << 16UL) + (c << 8UL) + (d))
michael@0 50
michael@0 51 // Enumeration used to specify a table in a TTF file
michael@0 52 class Tag
michael@0 53 {
michael@0 54 unsigned long _v;
michael@0 55 public:
michael@0 56 Tag(const char n[5]) throw() : _v(TTF_TAG(n[0],n[1],n[2],n[3])) {}
michael@0 57 Tag(const unsigned long tag) throw() : _v(tag) {}
michael@0 58
michael@0 59 operator unsigned long () const throw () { return _v; }
michael@0 60
michael@0 61 enum
michael@0 62 {
michael@0 63 Feat = TTF_TAG('F','e','a','t'),
michael@0 64 Glat = TTF_TAG('G','l','a','t'),
michael@0 65 Gloc = TTF_TAG('G','l','o','c'),
michael@0 66 Sile = TTF_TAG('S','i','l','e'),
michael@0 67 Silf = TTF_TAG('S','i','l','f'),
michael@0 68 Sill = TTF_TAG('S','i','l','l'),
michael@0 69 cmap = TTF_TAG('c','m','a','p'),
michael@0 70 cvt = TTF_TAG('c','v','t',' '),
michael@0 71 cryp = TTF_TAG('c','r','y','p'),
michael@0 72 head = TTF_TAG('h','e','a','d'),
michael@0 73 fpgm = TTF_TAG('f','p','g','m'),
michael@0 74 gdir = TTF_TAG('g','d','i','r'),
michael@0 75 glyf = TTF_TAG('g','l','y','f'),
michael@0 76 hdmx = TTF_TAG('h','d','m','x'),
michael@0 77 hhea = TTF_TAG('h','h','e','a'),
michael@0 78 hmtx = TTF_TAG('h','m','t','x'),
michael@0 79 loca = TTF_TAG('l','o','c','a'),
michael@0 80 kern = TTF_TAG('k','e','r','n'),
michael@0 81 LTSH = TTF_TAG('L','T','S','H'),
michael@0 82 maxp = TTF_TAG('m','a','x','p'),
michael@0 83 name = TTF_TAG('n','a','m','e'),
michael@0 84 OS_2 = TTF_TAG('O','S','/','2'),
michael@0 85 post = TTF_TAG('p','o','s','t'),
michael@0 86 prep = TTF_TAG('p','r','e','p')
michael@0 87 };
michael@0 88 };
michael@0 89
michael@0 90 /*----------------------------------------------------------------------------------------------
michael@0 91 Class providing utility methods to parse a TrueType font file (TTF).
michael@0 92 Callling application handles all file input and memory allocation.
michael@0 93 Assumes minimal knowledge of TTF file format.
michael@0 94 ----------------------------------------------------------------------------------------------*/
michael@0 95 ////////////////////////////////// tools to find & check TTF tables
michael@0 96 bool GetHeaderInfo(size_t & lOffset, size_t & lSize);
michael@0 97 bool CheckHeader(const void * pHdr);
michael@0 98 bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize);
michael@0 99 bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir,
michael@0 100 size_t & lOffset, size_t & lSize);
michael@0 101 bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize);
michael@0 102
michael@0 103 ////////////////////////////////// simple font wide info
michael@0 104 size_t GlyphCount(const void * pMaxp);
michael@0 105 #ifdef ALL_TTFUTILS
michael@0 106 size_t MaxCompositeComponentCount(const void * pMaxp);
michael@0 107 size_t MaxCompositeLevelCount(const void * pMaxp);
michael@0 108 size_t LocaGlyphCount(size_t lLocaSize, const void * pHead); // throw (std::domain_error);
michael@0 109 #endif
michael@0 110 int DesignUnits(const void * pHead);
michael@0 111 #ifdef ALL_TTFUTILS
michael@0 112 int HeadTableCheckSum(const void * pHead);
michael@0 113 void HeadTableCreateTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD);
michael@0 114 void HeadTableModifyTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD);
michael@0 115 bool IsItalic(const void * pHead);
michael@0 116 int FontAscent(const void * pOs2);
michael@0 117 int FontDescent(const void * pOs2);
michael@0 118 bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic);
michael@0 119 bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize);
michael@0 120 bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize);
michael@0 121 bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize);
michael@0 122 bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize);
michael@0 123 int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp,
michael@0 124 const char * pPostName);
michael@0 125 #endif
michael@0 126
michael@0 127 ////////////////////////////////// utility methods helpful for name table
michael@0 128 bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId,
michael@0 129 int nLangId, int nNameId, size_t & lOffset, size_t & lSize);
michael@0 130 //size_t NameTableLength(const byte * pTable);
michael@0 131 #ifdef ALL_TTFUTILS
michael@0 132 int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId,
michael@0 133 int *nameIdList, int cNameIds, short *langIdList);
michael@0 134 void SwapWString(void * pWStr, size_t nSize = 0); // throw (std::invalid_argument);
michael@0 135 #endif
michael@0 136
michael@0 137 ////////////////////////////////// cmap lookup tools
michael@0 138 const void * FindCmapSubtable(const void * pCmap, int nPlatformId = 3,
michael@0 139 int nEncodingId = 1, size_t length = 0);
michael@0 140 bool CheckCmapSubtable4(const void * pCmap31);
michael@0 141 gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey = 0);
michael@0 142 unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId,
michael@0 143 int * pRangeKey = 0);
michael@0 144 bool CheckCmapSubtable12(const void *pCmap310);
michael@0 145 gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey = 0);
michael@0 146 unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId,
michael@0 147 int * pRangeKey = 0);
michael@0 148
michael@0 149 ///////////////////////////////// horizontal metric data for a glyph
michael@0 150 bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize,
michael@0 151 const void * pHhea, int & nLsb, unsigned int & nAdvWid);
michael@0 152
michael@0 153 ////////////////////////////////// primitives for loca and glyf lookup
michael@0 154 size_t LocaLookup(gid16 nGlyphId, const void * pLoca, size_t lLocaSize,
michael@0 155 const void * pHead); // throw (std::out_of_range);
michael@0 156 void * GlyfLookup(const void * pGlyf, size_t lGlyfOffset, size_t lTableLen);
michael@0 157
michael@0 158 ////////////////////////////////// primitves for simple glyph data
michael@0 159 bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin,
michael@0 160 int & xMax, int & yMax);
michael@0 161
michael@0 162 #ifdef ALL_TTFUTILS
michael@0 163 int GlyfContourCount(const void * pSimpleGlyf);
michael@0 164 bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint,
michael@0 165 int cnPointsTotal, size_t & cnPoints);
michael@0 166 bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY,
michael@0 167 char * prgbFlag, int cnPointsTotal, int & cnPoints);
michael@0 168
michael@0 169 // primitive to find the glyph ids in a composite glyph
michael@0 170 bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId,
michael@0 171 size_t cnCompIdTotal, size_t & cnCompId);
michael@0 172 // primitive to find the placement data for a component in a composite glyph
michael@0 173 bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId,
michael@0 174 bool fOffset, int & a, int & b);
michael@0 175 // primitive to find the transform data for a component in a composite glyph
michael@0 176 bool GetComponentTransform(const void * pSimpleGlyf, int nCompId,
michael@0 177 float & flt11, float & flt12, float & flt21, float & flt22, bool & fTransOffset);
michael@0 178 #endif
michael@0 179
michael@0 180 ////////////////////////////////// operate on composite or simple glyph (auto glyf lookup)
michael@0 181 void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
michael@0 182 size_t lGlyfSize, size_t lLocaSize, const void * pHead); // primitive used by below methods
michael@0 183
michael@0 184 #ifdef ALL_TTFUTILS
michael@0 185 // below are primary user methods for handling glyf data
michael@0 186 bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead);
michael@0 187 bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
michael@0 188 size_t lGlyfSize, size_t lLocaSize, const void * pHead);
michael@0 189
michael@0 190 bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, size_t lGlyfSize, size_t lLocaSize,
michael@0 191 const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax);
michael@0 192 bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
michael@0 193 size_t lGlyfSize, size_t lLocaSize, const void *pHead, size_t & cnContours);
michael@0 194 bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
michael@0 195 size_t lGlyfSize, size_t lLocaSize, const void * pHead, int * prgnContourEndPoint, size_t cnPoints);
michael@0 196 bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca,
michael@0 197 size_t lGlyfSize, size_t lLocaSize, const void * pHead, const int * prgnContourEndPoint, size_t cnEndPoints,
michael@0 198 int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints);
michael@0 199
michael@0 200 // utitily method used by high-level GlyfPoints
michael@0 201 bool SimplifyFlags(char * prgbFlags, int cnPoints);
michael@0 202 bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints);
michael@0 203 #endif
michael@0 204
michael@0 205 } // end of namespace TtfUtil
michael@0 206 } // end of namespace graphite2

mercurial