Path: blob/master/thirdparty/graphite/src/inc/TtfUtil.h
9906 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2010, SIL International, All rights reserved.2/*3Responsibility: Alan Ward4Last reviewed: Not yet.56Description:7Utility class for handling TrueType font files.8*/9#pragma once101112#include <cstddef>1314namespace graphite215{16namespace TtfUtil17{1819#define OVERFLOW_OFFSET_CHECK(p, o) (o + reinterpret_cast<size_t>(p) < reinterpret_cast<size_t>(p))2021typedef long fontTableId32;22typedef unsigned short gid16;2324#define TTF_TAG(a,b,c,d) ((a << 24UL) + (b << 16UL) + (c << 8UL) + (d))2526// Enumeration used to specify a table in a TTF file27class Tag28{29unsigned int _v;30public:31Tag(const char n[5]) throw() : _v(TTF_TAG(n[0],n[1],n[2],n[3])) {}32Tag(const unsigned int tag) throw() : _v(tag) {}3334operator unsigned int () const throw () { return _v; }3536enum37{38Feat = TTF_TAG('F','e','a','t'),39Glat = TTF_TAG('G','l','a','t'),40Gloc = TTF_TAG('G','l','o','c'),41Sile = TTF_TAG('S','i','l','e'),42Silf = TTF_TAG('S','i','l','f'),43Sill = TTF_TAG('S','i','l','l'),44cmap = TTF_TAG('c','m','a','p'),45cvt = TTF_TAG('c','v','t',' '),46cryp = TTF_TAG('c','r','y','p'),47head = TTF_TAG('h','e','a','d'),48fpgm = TTF_TAG('f','p','g','m'),49gdir = TTF_TAG('g','d','i','r'),50glyf = TTF_TAG('g','l','y','f'),51hdmx = TTF_TAG('h','d','m','x'),52hhea = TTF_TAG('h','h','e','a'),53hmtx = TTF_TAG('h','m','t','x'),54loca = TTF_TAG('l','o','c','a'),55kern = TTF_TAG('k','e','r','n'),56LTSH = TTF_TAG('L','T','S','H'),57maxp = TTF_TAG('m','a','x','p'),58name = TTF_TAG('n','a','m','e'),59OS_2 = TTF_TAG('O','S','/','2'),60post = TTF_TAG('p','o','s','t'),61prep = TTF_TAG('p','r','e','p')62};63};6465/*----------------------------------------------------------------------------------------------66Class providing utility methods to parse a TrueType font file (TTF).67Callling application handles all file input and memory allocation.68Assumes minimal knowledge of TTF file format.69----------------------------------------------------------------------------------------------*/70////////////////////////////////// tools to find & check TTF tables71bool GetHeaderInfo(size_t & lOffset, size_t & lSize);72bool CheckHeader(const void * pHdr);73bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize);74bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir,75size_t & lOffset, size_t & lSize);76bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize);7778////////////////////////////////// simple font wide info79size_t GlyphCount(const void * pMaxp);80#ifdef ALL_TTFUTILS81size_t MaxCompositeComponentCount(const void * pMaxp);82size_t MaxCompositeLevelCount(const void * pMaxp);83size_t LocaGlyphCount(size_t lLocaSize, const void * pHead); // throw (std::domain_error);84#endif85int DesignUnits(const void * pHead);86#ifdef ALL_TTFUTILS87int HeadTableCheckSum(const void * pHead);88void HeadTableCreateTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD);89void HeadTableModifyTime(const void * pHead, unsigned int * pnDateBC, unsigned int * pnDateAD);90bool IsItalic(const void * pHead);91int FontAscent(const void * pOs2);92int FontDescent(const void * pOs2);93bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic);94bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize);95bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize);96bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize);97bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize);98int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp,99const char * pPostName);100#endif101102////////////////////////////////// utility methods helpful for name table103bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId,104int nLangId, int nNameId, size_t & lOffset, size_t & lSize);105//size_t NameTableLength(const byte * pTable);106#ifdef ALL_TTFUTILS107int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId,108int *nameIdList, int cNameIds, short *langIdList);109void SwapWString(void * pWStr, size_t nSize = 0); // throw (std::invalid_argument);110#endif111112////////////////////////////////// cmap lookup tools113const void * FindCmapSubtable(const void * pCmap, int nPlatformId = 3,114int nEncodingId = 1, size_t length = 0);115bool CheckCmapSubtable4(const void * pCmap31, const void * pCmapEnd /*, unsigned int maxgid*/);116gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey = 0);117unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId,118int * pRangeKey = 0);119bool CheckCmapSubtable12(const void *pCmap310, const void * pCmapEnd /*, unsigned int maxgid*/);120gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey = 0);121unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId,122int * pRangeKey = 0);123124///////////////////////////////// horizontal metric data for a glyph125bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize,126const void * pHhea, int & nLsb, unsigned int & nAdvWid);127128////////////////////////////////// primitives for loca and glyf lookup129size_t LocaLookup(gid16 nGlyphId, const void * pLoca, size_t lLocaSize,130const void * pHead); // throw (std::out_of_range);131void * GlyfLookup(const void * pGlyf, size_t lGlyfOffset, size_t lTableLen);132133////////////////////////////////// primitves for simple glyph data134bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin,135int & xMax, int & yMax);136137#ifdef ALL_TTFUTILS138int GlyfContourCount(const void * pSimpleGlyf);139bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint,140int cnPointsTotal, size_t & cnPoints);141bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY,142char * prgbFlag, int cnPointsTotal, int & cnPoints);143144// primitive to find the glyph ids in a composite glyph145bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId,146size_t cnCompIdTotal, size_t & cnCompId);147// primitive to find the placement data for a component in a composite glyph148bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId,149bool fOffset, int & a, int & b);150// primitive to find the transform data for a component in a composite glyph151bool GetComponentTransform(const void * pSimpleGlyf, int nCompId,152float & flt11, float & flt12, float & flt21, float & flt22, bool & fTransOffset);153#endif154155////////////////////////////////// operate on composite or simple glyph (auto glyf lookup)156void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca,157size_t lGlyfSize, size_t lLocaSize, const void * pHead); // primitive used by below methods158159#ifdef ALL_TTFUTILS160// below are primary user methods for handling glyf data161bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead);162bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca,163size_t lGlyfSize, size_t lLocaSize, const void * pHead);164165bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, size_t lGlyfSize, size_t lLocaSize,166const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax);167bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca,168size_t lGlyfSize, size_t lLocaSize, const void *pHead, size_t & cnContours);169bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca,170size_t lGlyfSize, size_t lLocaSize, const void * pHead, int * prgnContourEndPoint, size_t cnPoints);171bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca,172size_t lGlyfSize, size_t lLocaSize, const void * pHead, const int * prgnContourEndPoint, size_t cnEndPoints,173int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints);174175// utitily method used by high-level GlyfPoints176bool SimplifyFlags(char * prgbFlags, int cnPoints);177bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints);178#endif179180} // end of namespace TtfUtil181} // end of namespace graphite2182183184