michael@0: /* GRAPHITE2 LICENSING michael@0: michael@0: Copyright 2010, SIL International michael@0: All rights reserved. michael@0: michael@0: This library is free software; you can redistribute it and/or modify michael@0: it under the terms of the GNU Lesser General Public License as published michael@0: by the Free Software Foundation; either version 2.1 of License, or michael@0: (at your option) any later version. michael@0: michael@0: This program is distributed in the hope that it will be useful, michael@0: but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: Lesser General Public License for more details. michael@0: michael@0: You should also have received a copy of the GNU Lesser General Public michael@0: License along with this library in the file named "LICENSE". michael@0: If not, write to the Free Software Foundation, 51 Franklin Street, michael@0: Suite 500, Boston, MA 02110-1335, USA or visit their web page on the michael@0: internet at http://www.fsf.org/licenses/lgpl.html. michael@0: michael@0: Alternatively, the contents of this file may be used under the terms of the michael@0: Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public michael@0: License, as published by the Free Software Foundation, either version 2 michael@0: of the License or (at your option) any later version. michael@0: */ michael@0: /*--------------------------------------------------------------------*//*:Ignore this sentence. michael@0: michael@0: File: TtfUtil.cpp michael@0: Responsibility: Alan Ward michael@0: Last reviewed: Not yet. michael@0: michael@0: Description michael@0: Implements the methods for TtfUtil class. This file should remain portable to any C++ michael@0: environment by only using standard C++ and the TTF structurs defined in Tt.h. michael@0: -------------------------------------------------------------------------------*//*:End Ignore*/ michael@0: michael@0: michael@0: /*********************************************************************************************** michael@0: Include files michael@0: ***********************************************************************************************/ michael@0: // Language headers michael@0: //#include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: //#include michael@0: // Platform headers michael@0: // Module headers michael@0: #include "inc/TtfUtil.h" michael@0: #include "inc/TtfTypes.h" michael@0: #include "inc/Endian.h" michael@0: michael@0: /*********************************************************************************************** michael@0: Forward declarations michael@0: ***********************************************************************************************/ michael@0: michael@0: /*********************************************************************************************** michael@0: Local Constants and static variables michael@0: ***********************************************************************************************/ michael@0: namespace michael@0: { michael@0: // max number of components allowed in composite glyphs michael@0: const int kMaxGlyphComponents = 8; michael@0: michael@0: template michael@0: inline float fixed_to_float(const T f) { michael@0: return float(f)/float(2^R); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Table of standard Postscript glyph names. From Martin Hosken. Disagress with ttfdump.exe michael@0: ---------------------------------------------------------------------------------------------*/ michael@0: #ifdef ALL_TTFUTILS michael@0: const int kcPostNames = 258; michael@0: michael@0: const char * rgPostName[kcPostNames] = { michael@0: ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", michael@0: "dollar", "percent", "ampersand", "quotesingle", "parenleft", michael@0: "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash", michael@0: "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", michael@0: "nine", "colon", "semicolon", "less", "equal", "greater", "question", michael@0: "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", michael@0: "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", michael@0: "bracketleft", "backslash", "bracketright", "asciicircum", michael@0: "underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", michael@0: "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", michael@0: "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", michael@0: "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", michael@0: "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", michael@0: "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", michael@0: "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", michael@0: "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", michael@0: "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", michael@0: "section", "bullet", "paragraph", "germandbls", "registered", michael@0: "copyright", "trademark", "acute", "dieresis", "notequal", "AE", michael@0: "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", michael@0: "mu", "partialdiff", "summation", "product", "pi", "integral", michael@0: "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", michael@0: "exclamdown", "logicalnot", "radical", "florin", "approxequal", michael@0: "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", michael@0: "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", michael@0: "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", michael@0: "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", michael@0: "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", michael@0: "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", michael@0: "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", michael@0: "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", michael@0: "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", michael@0: "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", michael@0: "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", michael@0: "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", michael@0: "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", michael@0: "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", michael@0: "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", michael@0: "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", michael@0: "dcroat" }; michael@0: #endif michael@0: michael@0: } // end of namespace michael@0: michael@0: /*********************************************************************************************** michael@0: Methods michael@0: ***********************************************************************************************/ michael@0: michael@0: /* Note on error processing: The code guards against bad glyph ids being used to look up data michael@0: in open ended tables (loca, hmtx). If the glyph id comes from a cmap this shouldn't happen michael@0: but it seems prudent to check for user errors here. The code does assume that data obtained michael@0: from the TTF file is valid otherwise (though the CheckTable method seeks to check for michael@0: obvious problems that might accompany a change in table versions). For example an invalid michael@0: offset in the loca table which could exceed the size of the glyf table is NOT trapped. michael@0: Likewise if numberOf_LongHorMetrics in the hhea table is wrong, this will NOT be trapped, michael@0: which could cause a lookup in the hmtx table to exceed the table length. Of course, TTF tables michael@0: that are completely corrupt will cause unpredictable results. */ michael@0: michael@0: /* Note on composite glyphs: Glyphs that have components that are themselves composites michael@0: are not supported. IsDeepComposite can be used to test for this. False is returned from many michael@0: of the methods in this cases. It is unclear how to build composite glyphs in some cases, michael@0: so this code represents my best guess until test cases can be found. See notes on the high- michael@0: level GlyfPoints method. */ michael@0: namespace graphite2 michael@0: { michael@0: namespace TtfUtil michael@0: { michael@0: michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get offset and size of the offset table needed to find table directory. michael@0: Return true if success, false otherwise. michael@0: lSize excludes any table directory entries. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GetHeaderInfo(size_t & lOffset, size_t & lSize) michael@0: { michael@0: lOffset = 0; michael@0: lSize = offsetof(Sfnt::OffsetSubTable, table_directory); michael@0: assert(sizeof(uint32) + 4*sizeof (uint16) == lSize); michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Check the offset table for expected data. michael@0: Return true if success, false otherwise. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool CheckHeader(const void * pHdr) michael@0: { michael@0: const Sfnt::OffsetSubTable * pOffsetTable michael@0: = reinterpret_cast(pHdr); michael@0: michael@0: return pHdr && be::swap(pOffsetTable->scaler_type) == Sfnt::OffsetSubTable::TrueTypeWin; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get offset and size of the table directory. michael@0: Return true if successful, false otherwise. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GetTableDirInfo(const void * pHdr, size_t & lOffset, size_t & lSize) michael@0: { michael@0: const Sfnt::OffsetSubTable * pOffsetTable michael@0: = reinterpret_cast(pHdr); michael@0: michael@0: lOffset = offsetof(Sfnt::OffsetSubTable, table_directory); michael@0: lSize = be::swap(pOffsetTable->num_tables) michael@0: * sizeof(Sfnt::OffsetSubTable::Entry); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get offset and size of the specified table. michael@0: Return true if successful, false otherwise. On false, offset and size will be 0. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GetTableInfo(const Tag TableTag, const void * pHdr, const void * pTableDir, michael@0: size_t & lOffset, size_t & lSize) michael@0: { michael@0: const Sfnt::OffsetSubTable * pOffsetTable michael@0: = reinterpret_cast(pHdr); michael@0: const size_t num_tables = be::swap(pOffsetTable->num_tables); michael@0: const Sfnt::OffsetSubTable::Entry michael@0: * entry_itr = reinterpret_cast( michael@0: pTableDir), michael@0: * const dir_end = entry_itr + num_tables; michael@0: michael@0: if (num_tables > 40) michael@0: return false; michael@0: michael@0: for (;entry_itr != dir_end; ++entry_itr) // 40 - safe guard michael@0: { michael@0: if (be::swap(entry_itr->tag) == TableTag) michael@0: { michael@0: lOffset = be::swap(entry_itr->offset); michael@0: lSize = be::swap(entry_itr->length); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Check the specified table. Tests depend on the table type. michael@0: Return true if successful, false otherwise. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool CheckTable(const Tag TableId, const void * pTable, size_t lTableSize) michael@0: { michael@0: using namespace Sfnt; michael@0: michael@0: if (pTable == 0) return false; michael@0: michael@0: switch(TableId) michael@0: { michael@0: case Tag::cmap: // cmap michael@0: { michael@0: const Sfnt::CharacterCodeMap * const pCmap michael@0: = reinterpret_cast(pTable); michael@0: return be::swap(pCmap->version) == 0; michael@0: } michael@0: michael@0: case Tag::head: // head michael@0: { michael@0: const Sfnt::FontHeader * const pHead michael@0: = reinterpret_cast(pTable); michael@0: bool r = be::swap(pHead->version) == OneFix michael@0: && be::swap(pHead->magic_number) == FontHeader::MagicNumber michael@0: && be::swap(pHead->glyph_data_format) michael@0: == FontHeader::GlypDataFormat michael@0: && (be::swap(pHead->index_to_loc_format) michael@0: == FontHeader::ShortIndexLocFormat michael@0: || be::swap(pHead->index_to_loc_format) michael@0: == FontHeader::LongIndexLocFormat) michael@0: && sizeof(FontHeader) <= lTableSize; michael@0: return r; michael@0: } michael@0: michael@0: case Tag::post: // post michael@0: { michael@0: const Sfnt::PostScriptGlyphName * const pPost michael@0: = reinterpret_cast(pTable); michael@0: const fixed format = be::swap(pPost->format); michael@0: bool r = format == PostScriptGlyphName::Format1 michael@0: || format == PostScriptGlyphName::Format2 michael@0: || format == PostScriptGlyphName::Format3 michael@0: || format == PostScriptGlyphName::Format25; michael@0: return r; michael@0: } michael@0: michael@0: case Tag::hhea: // hhea michael@0: { michael@0: const Sfnt::HorizontalHeader * pHhea = michael@0: reinterpret_cast(pTable); michael@0: bool r = be::swap(pHhea->version) == OneFix michael@0: && be::swap(pHhea->metric_data_format) == 0 michael@0: && sizeof (Sfnt::HorizontalHeader) <= lTableSize; michael@0: return r; michael@0: } michael@0: michael@0: case Tag::maxp: // maxp michael@0: { michael@0: const Sfnt::MaximumProfile * pMaxp = michael@0: reinterpret_cast(pTable); michael@0: bool r = be::swap(pMaxp->version) == OneFix michael@0: && sizeof(Sfnt::MaximumProfile) <= lTableSize; michael@0: return r; michael@0: } michael@0: michael@0: case Tag::OS_2: // OS/2 michael@0: { michael@0: const Sfnt::Compatibility * pOs2 michael@0: = reinterpret_cast(pTable); michael@0: if (be::swap(pOs2->version) == 0) michael@0: { // OS/2 table version 1 size michael@0: // if (sizeof(Sfnt::Compatibility) michael@0: // - sizeof(uint32)*2 - sizeof(int16)*2 michael@0: // - sizeof(uint16)*3 <= lTableSize) michael@0: if (sizeof(Sfnt::Compatibility0) <= lTableSize) michael@0: return true; michael@0: } michael@0: else if (be::swap(pOs2->version) == 1) michael@0: { // OS/2 table version 2 size michael@0: // if (sizeof(Sfnt::Compatibility) michael@0: // - sizeof(int16) *2 michael@0: // - sizeof(uint16)*3 <= lTableSize) michael@0: if (sizeof(Sfnt::Compatibility1) <= lTableSize) michael@0: return true; michael@0: } michael@0: else if (be::swap(pOs2->version) == 2) michael@0: { // OS/2 table version 3 size michael@0: if (sizeof(Sfnt::Compatibility2) <= lTableSize) michael@0: return true; michael@0: } michael@0: else if (be::swap(pOs2->version) == 3 || be::swap(pOs2->version) == 4) michael@0: { // OS/2 table version 4 size - version 4 changed the meaning of some fields which we don't use michael@0: if (sizeof(Sfnt::Compatibility3) <= lTableSize) michael@0: return true; michael@0: } michael@0: else michael@0: return false; michael@0: break; michael@0: } michael@0: michael@0: case Tag::name: michael@0: { michael@0: const Sfnt::FontNames * pName michael@0: = reinterpret_cast(pTable); michael@0: return be::swap(pName->format) == 0; michael@0: } michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the number of glyphs in the font. Should never be less than zero. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: size_t GlyphCount(const void * pMaxp) michael@0: { michael@0: const Sfnt::MaximumProfile * pTable = michael@0: reinterpret_cast(pMaxp); michael@0: return be::swap(pTable->num_glyphs); michael@0: } michael@0: michael@0: #ifdef ALL_TTFUTILS michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the maximum number of components for any composite glyph in the font. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: size_t MaxCompositeComponentCount(const void * pMaxp) michael@0: { michael@0: const Sfnt::MaximumProfile * pTable = michael@0: reinterpret_cast(pMaxp); michael@0: return be::swap(pTable->max_component_elements); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Composite glyphs can be composed of glyphs that are themselves composites. michael@0: This method returns the maximum number of levels like this for any glyph in the font. michael@0: A non-composite glyph has a level of 1. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: size_t MaxCompositeLevelCount(const void * pMaxp) michael@0: { michael@0: const Sfnt::MaximumProfile * pTable = michael@0: reinterpret_cast(pMaxp); michael@0: return be::swap(pTable->max_component_depth); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the number of glyphs in the font according to a differt source. michael@0: Should never be less than zero. Return -1 on failure. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: size_t LocaGlyphCount(size_t lLocaSize, const void * pHead) //throw(std::domain_error) michael@0: { michael@0: michael@0: const Sfnt::FontHeader * pTable michael@0: = reinterpret_cast(pHead); michael@0: michael@0: if (be::swap(pTable->index_to_loc_format) michael@0: == Sfnt::FontHeader::ShortIndexLocFormat) michael@0: // loca entries are two bytes and have been divided by two michael@0: return (lLocaSize >> 1) - 1; michael@0: michael@0: if (be::swap(pTable->index_to_loc_format) michael@0: == Sfnt::FontHeader::LongIndexLocFormat) michael@0: // loca entries are four bytes michael@0: return (lLocaSize >> 2) - 1; michael@0: michael@0: return -1; michael@0: //throw std::domain_error("head table in inconsistent state. The font may be corrupted"); michael@0: } michael@0: #endif michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the design units the font is designed with michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: int DesignUnits(const void * pHead) michael@0: { michael@0: const Sfnt::FontHeader * pTable = michael@0: reinterpret_cast(pHead); michael@0: michael@0: return be::swap(pTable->units_per_em); michael@0: } michael@0: michael@0: #ifdef ALL_TTFUTILS michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the checksum from the head table, which serves as a unique identifer for the font. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: int HeadTableCheckSum(const void * pHead) michael@0: { michael@0: const Sfnt::FontHeader * pTable = michael@0: reinterpret_cast(pHead); michael@0: michael@0: return be::swap(pTable->check_sum_adjustment); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the create time from the head table. This consists of a 64-bit integer, which michael@0: we return here as two 32-bit integers. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: void HeadTableCreateTime(const void * pHead, michael@0: unsigned int * pnDateBC, unsigned int * pnDateAD) michael@0: { michael@0: const Sfnt::FontHeader * pTable = michael@0: reinterpret_cast(pHead); michael@0: michael@0: *pnDateBC = be::swap(pTable->created[0]); michael@0: *pnDateAD = be::swap(pTable->created[1]); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the modify time from the head table.This consists of a 64-bit integer, which michael@0: we return here as two 32-bit integers. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: void HeadTableModifyTime(const void * pHead, michael@0: unsigned int * pnDateBC, unsigned int *pnDateAD) michael@0: { michael@0: const Sfnt::FontHeader * pTable = michael@0: reinterpret_cast(pHead); michael@0: michael@0: *pnDateBC = be::swap(pTable->modified[0]); michael@0: *pnDateAD = be::swap(pTable->modified[1]); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return true if the font is italic. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool IsItalic(const void * pHead) michael@0: { michael@0: const Sfnt::FontHeader * pTable = michael@0: reinterpret_cast(pHead); michael@0: michael@0: return ((be::swap(pTable->mac_style) & 0x00000002) != 0); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the ascent for the font michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: int FontAscent(const void * pOs2) michael@0: { michael@0: const Sfnt::Compatibility * pTable = reinterpret_cast(pOs2); michael@0: michael@0: return be::swap(pTable->win_ascent); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the descent for the font michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: int FontDescent(const void * pOs2) michael@0: { michael@0: const Sfnt::Compatibility * pTable = reinterpret_cast(pOs2); michael@0: michael@0: return be::swap(pTable->win_descent); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the bold and italic style bits. michael@0: Return true if successful. false otherwise. michael@0: In addition to checking the OS/2 table, one could also check michael@0: the head table's macStyle field (overridden by the OS/2 table on Win) michael@0: the sub-family name in the name table (though this can contain oblique, dark, etc too) michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool FontOs2Style(const void *pOs2, bool & fBold, bool & fItalic) michael@0: { michael@0: const Sfnt::Compatibility * pTable = reinterpret_cast(pOs2); michael@0: michael@0: fBold = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Bold) != 0; michael@0: fItalic = (be::swap(pTable->fs_selection) & Sfnt::Compatibility::Italic) != 0; michael@0: michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Method for searching name table. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GetNameInfo(const void * pName, int nPlatformId, int nEncodingId, michael@0: int nLangId, int nNameId, size_t & lOffset, size_t & lSize) michael@0: { michael@0: lOffset = 0; michael@0: lSize = 0; michael@0: michael@0: const Sfnt::FontNames * pTable = reinterpret_cast(pName); michael@0: uint16 cRecord = be::swap(pTable->count); michael@0: uint16 nRecordOffset = be::swap(pTable->string_offset); michael@0: const Sfnt::NameRecord * pRecord = reinterpret_cast(pTable + 1); michael@0: michael@0: for (int i = 0; i < cRecord; ++i) michael@0: { michael@0: if (be::swap(pRecord->platform_id) == nPlatformId && michael@0: be::swap(pRecord->platform_specific_id) == nEncodingId && michael@0: be::swap(pRecord->language_id) == nLangId && michael@0: be::swap(pRecord->name_id) == nNameId) michael@0: { michael@0: lOffset = be::swap(pRecord->offset) + nRecordOffset; michael@0: lSize = be::swap(pRecord->length); michael@0: return true; michael@0: } michael@0: pRecord++; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: #ifdef ALL_TTFUTILS michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return all the lang-IDs that have data for the given name-IDs. Assume that there is room michael@0: in the return array (langIdList) for 128 items. The purpose of this method is to return michael@0: a list of all possible lang-IDs. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: int GetLangsForNames(const void * pName, int nPlatformId, int nEncodingId, michael@0: int * nameIdList, int cNameIds, short * langIdList) michael@0: { michael@0: const Sfnt::FontNames * pTable = reinterpret_cast(pName); michael@0: int cLangIds = 0; michael@0: uint16 cRecord = be::swap(pTable->count); michael@0: if (cRecord > 127) return cLangIds; michael@0: //uint16 nRecordOffset = swapw(pTable->stringOffset); michael@0: const Sfnt::NameRecord * pRecord = reinterpret_cast(pTable + 1); michael@0: michael@0: for (int i = 0; i < cRecord; ++i) michael@0: { michael@0: if (be::swap(pRecord->platform_id) == nPlatformId && michael@0: be::swap(pRecord->platform_specific_id) == nEncodingId) michael@0: { michael@0: bool fNameFound = false; michael@0: int nLangId = be::swap(pRecord->language_id); michael@0: int nNameId = be::swap(pRecord->name_id); michael@0: for (int j = 0; j < cNameIds; j++) michael@0: { michael@0: if (nNameId == nameIdList[j]) michael@0: { michael@0: fNameFound = true; michael@0: break; michael@0: } michael@0: } michael@0: if (fNameFound) michael@0: { michael@0: // Add it if it's not there. michael@0: int ilang; michael@0: for (ilang = 0; ilang < cLangIds; ilang++) michael@0: if (langIdList[ilang] == nLangId) michael@0: break; michael@0: if (ilang >= cLangIds) michael@0: { michael@0: langIdList[cLangIds] = short(nLangId); michael@0: cLangIds++; michael@0: } michael@0: if (cLangIds == 128) michael@0: return cLangIds; michael@0: } michael@0: } michael@0: pRecord++; michael@0: } michael@0: michael@0: return cLangIds; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the offset and size of the font family name in English for the MS Platform with Unicode michael@0: writing system. The offset is within the pName data. The string is double byte with MSB michael@0: first. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool Get31EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize) michael@0: { michael@0: return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, michael@0: Sfnt::NameRecord::Family, lOffset, lSize); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the offset and size of the full font name in English for the MS Platform with Unicode michael@0: writing system. The offset is within the pName data. The string is double byte with MSB michael@0: first. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool Get31EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize) michael@0: { michael@0: return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 1, 1033, michael@0: Sfnt::NameRecord::Fullname, lOffset, lSize); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the offset and size of the font family name in English for the MS Platform with Symbol michael@0: writing system. The offset is within the pName data. The string is double byte with MSB michael@0: first. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool Get30EngFamilyInfo(const void * pName, size_t & lOffset, size_t & lSize) michael@0: { michael@0: return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, michael@0: Sfnt::NameRecord::Family, lOffset, lSize); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the offset and size of the full font name in English for the MS Platform with Symbol michael@0: writing system. The offset is within the pName data. The string is double byte with MSB michael@0: first. michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool Get30EngFullFontInfo(const void * pName, size_t & lOffset, size_t & lSize) michael@0: { michael@0: return GetNameInfo(pName, Sfnt::NameRecord::Microsoft, 0, 1033, michael@0: Sfnt::NameRecord::Fullname, lOffset, lSize); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the Glyph ID for a given Postscript name. This method finds the first glyph which michael@0: matches the requested Postscript name. Ideally every glyph should have a unique Postscript michael@0: name (except for special names such as .notdef), but this is not always true. michael@0: On failure return value less than zero. michael@0: -1 - table search failed michael@0: -2 - format 3 table (no Postscript glyph info) michael@0: -3 - other failures michael@0: michael@0: Note: this method is not currently used by the Graphite engine. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: int PostLookup(const void * pPost, size_t lPostSize, const void * pMaxp, michael@0: const char * pPostName) michael@0: { michael@0: using namespace Sfnt; michael@0: michael@0: const Sfnt::PostScriptGlyphName * pTable michael@0: = reinterpret_cast(pPost); michael@0: fixed format = be::swap(pTable->format); michael@0: michael@0: if (format == PostScriptGlyphName::Format3) michael@0: { // format 3 - no Postscript glyph info in font michael@0: return -2; michael@0: } michael@0: michael@0: // search for given Postscript name among the standard names michael@0: int iPostName = -1; // index in standard names michael@0: for (int i = 0; i < kcPostNames; i++) michael@0: { michael@0: if (!strcmp(pPostName, rgPostName[i])) michael@0: { michael@0: iPostName = i; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (format == PostScriptGlyphName::Format1) michael@0: { // format 1 - use standard Postscript names michael@0: return iPostName; michael@0: } michael@0: michael@0: if (format == PostScriptGlyphName::Format25) michael@0: { michael@0: if (iPostName == -1) michael@0: return -1; michael@0: michael@0: const PostScriptGlyphName25 * pTable25 michael@0: = static_cast(pTable); michael@0: int cnGlyphs = GlyphCount(pMaxp); michael@0: for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs && nGlyphId < kcPostNames; michael@0: nGlyphId++) michael@0: { // glyph_name_index25 contains bytes so no byte swapping needed michael@0: // search for first glyph id that uses the standard name michael@0: if (nGlyphId + pTable25->offset[nGlyphId] == iPostName) michael@0: return nGlyphId; michael@0: } michael@0: } michael@0: michael@0: if (format == PostScriptGlyphName::Format2) michael@0: { // format 2 michael@0: const PostScriptGlyphName2 * pTable2 michael@0: = static_cast(pTable); michael@0: michael@0: int cnGlyphs = be::swap(pTable2->number_of_glyphs); michael@0: michael@0: if (iPostName != -1) michael@0: { // did match a standard name, look for first glyph id mapped to that name michael@0: for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++) michael@0: { michael@0: if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iPostName) michael@0: return nGlyphId; michael@0: } michael@0: } michael@0: michael@0: { // did not match a standard name, search font specific names michael@0: size_t nStrSizeGoal = strlen(pPostName); michael@0: const char * pFirstGlyphName = reinterpret_cast( michael@0: &pTable2->glyph_name_index[0] + cnGlyphs); michael@0: const char * pGlyphName = pFirstGlyphName; michael@0: int iInNames = 0; // index in font specific names michael@0: bool fFound = false; michael@0: const char * const endOfTable michael@0: = reinterpret_cast(pTable2) + lPostSize; michael@0: while (pGlyphName < endOfTable && !fFound) michael@0: { // search Pascal strings for first matching name michael@0: size_t nStringSize = size_t(*pGlyphName); michael@0: if (nStrSizeGoal != nStringSize || michael@0: strncmp(pGlyphName + 1, pPostName, nStringSize)) michael@0: { // did not match michael@0: ++iInNames; michael@0: pGlyphName += nStringSize + 1; michael@0: } michael@0: else michael@0: { // did match michael@0: fFound = true; michael@0: } michael@0: } michael@0: if (!fFound) michael@0: return -1; // no font specific name matches request michael@0: michael@0: iInNames += kcPostNames; michael@0: for (gid16 nGlyphId = 0; nGlyphId < cnGlyphs; nGlyphId++) michael@0: { // search for first glyph id that maps to the found string index michael@0: if (be::swap(pTable2->glyph_name_index[nGlyphId]) == iInNames) michael@0: return nGlyphId; michael@0: } michael@0: return -1; // no glyph mapped to this index (very strange) michael@0: } michael@0: } michael@0: michael@0: return -3; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Convert a Unicode character string from big endian (MSB first, Motorola) format to little michael@0: endian (LSB first, Intel) format. michael@0: nSize is the number of Unicode characters in the string. It should not include any michael@0: terminating null. If nSize is 0, it is assumed the string is null terminated. nSize michael@0: defaults to 0. michael@0: Return true if successful, false otherwise. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: void SwapWString(void * pWStr, size_t nSize /* = 0 */) //throw (std::invalid_argument) michael@0: { michael@0: if (pWStr == 0) michael@0: { michael@0: // throw std::invalid_argument("null pointer given"); michael@0: return; michael@0: } michael@0: michael@0: uint16 * pStr = reinterpret_cast(pWStr); michael@0: uint16 * const pStrEnd = pStr + (nSize == 0 ? wcslen((const wchar_t*)pStr) : nSize); michael@0: michael@0: for (; pStr != pStrEnd; ++pStr) michael@0: *pStr = be::swap(*pStr); michael@0: // std::transform(pStr, pStrEnd, pStr, read); michael@0: michael@0: // for (int i = 0; i < nSize; i++) michael@0: // { // swap the wide characters in the string michael@0: // pStr[i] = utf16(be::swap(uint16(pStr[i]))); michael@0: // } michael@0: } michael@0: #endif michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the left-side bearing and and advance width based on the given tables and Glyph ID michael@0: Return true if successful, false otherwise. On false, one or both value could be INT_MIN michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool HorMetrics(gid16 nGlyphId, const void * pHmtx, size_t lHmtxSize, const void * pHhea, michael@0: int & nLsb, unsigned int & nAdvWid) michael@0: { michael@0: const Sfnt::HorizontalMetric * phmtx = michael@0: reinterpret_cast(pHmtx); michael@0: michael@0: const Sfnt::HorizontalHeader * phhea = michael@0: reinterpret_cast(pHhea); michael@0: michael@0: size_t cLongHorMetrics = be::swap(phhea->num_long_hor_metrics); michael@0: if (nGlyphId < cLongHorMetrics) michael@0: { // glyph id is acceptable michael@0: if (nGlyphId * sizeof(Sfnt::HorizontalMetric) >= lHmtxSize) return false; michael@0: nAdvWid = be::swap(phmtx[nGlyphId].advance_width); michael@0: nLsb = be::swap(phmtx[nGlyphId].left_side_bearing); michael@0: } michael@0: else michael@0: { michael@0: // guard against bad glyph id michael@0: size_t lLsbOffset = sizeof(Sfnt::HorizontalMetric) * cLongHorMetrics + michael@0: sizeof(int16) * (nGlyphId - cLongHorMetrics); // offset in bytes michael@0: // We test like this as LsbOffset is an offset not a length. michael@0: if (lLsbOffset > lHmtxSize - sizeof(int16)) michael@0: { michael@0: nLsb = 0; michael@0: return false; michael@0: } michael@0: nAdvWid = be::swap(phmtx[cLongHorMetrics - 1].advance_width); michael@0: nLsb = be::peek(reinterpret_cast(phmtx) + lLsbOffset); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return a pointer to the requested cmap subtable. By default find the Microsoft Unicode michael@0: subtable. Pass nEncoding as -1 to find first table that matches only nPlatformId. michael@0: Return NULL if the subtable cannot be found. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: const void * FindCmapSubtable(const void * pCmap, int nPlatformId, /* =3 */ int nEncodingId, /* = 1 */ size_t length) michael@0: { michael@0: const Sfnt::CharacterCodeMap * pTable = reinterpret_cast(pCmap); michael@0: uint16 csuPlatforms = be::swap(pTable->num_subtables); michael@0: if (length && (sizeof(Sfnt::CharacterCodeMap) + 8 * (csuPlatforms - 1) > length)) michael@0: return NULL; michael@0: for (int i = 0; i < csuPlatforms; i++) michael@0: { michael@0: if (be::swap(pTable->encoding[i].platform_id) == nPlatformId && michael@0: (nEncodingId == -1 || be::swap(pTable->encoding[i].platform_specific_id) == nEncodingId)) michael@0: { michael@0: uint32 offset = be::swap(pTable->encoding[i].offset); michael@0: const uint8 * pRtn = reinterpret_cast(pCmap) + offset; michael@0: if (length) michael@0: { michael@0: if (offset > length) return NULL; michael@0: uint16 format = be::read(pRtn); michael@0: if (format == 4) michael@0: { michael@0: uint16 subTableLength = be::peek(pRtn); michael@0: if (i + 1 == csuPlatforms) michael@0: { michael@0: if (subTableLength > length - offset) michael@0: return NULL; michael@0: } michael@0: else if (subTableLength > be::swap(pTable->encoding[i+1].offset)) michael@0: return NULL; michael@0: } michael@0: if (format == 12) michael@0: { michael@0: uint32 subTableLength = be::peek(pRtn); michael@0: if (i + 1 == csuPlatforms) michael@0: { michael@0: if (subTableLength > length - offset) michael@0: return NULL; michael@0: } michael@0: else if (subTableLength > be::swap(pTable->encoding[i+1].offset)) michael@0: return NULL; michael@0: } michael@0: } michael@0: return reinterpret_cast(pCmap) + offset; michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Check the Microsoft Unicode subtable for expected values michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool CheckCmapSubtable4(const void * pCmapSubtable4) michael@0: { michael@0: if (!pCmapSubtable4) return false; michael@0: const Sfnt::CmapSubTable * pTable = reinterpret_cast(pCmapSubtable4); michael@0: // Bob H says ome freeware TT fonts have version 1 (eg, CALIGULA.TTF) michael@0: // so don't check subtable version. 21 Mar 2002 spec changes version to language. michael@0: if (be::swap(pTable->format) != 4) return false; michael@0: const Sfnt::CmapSubTableFormat4 * pTable4 = reinterpret_cast(pCmapSubtable4); michael@0: uint16 length = be::swap(pTable4->length); michael@0: if (length < sizeof(Sfnt::CmapSubTableFormat4)) michael@0: return false; michael@0: uint16 nRanges = be::swap(pTable4->seg_count_x2) >> 1; michael@0: if (length < sizeof(Sfnt::CmapSubTableFormat4) + 4 * nRanges * sizeof(uint16)) michael@0: return false; michael@0: // check last range is properly terminated michael@0: uint16 chEnd = be::peek(pTable4->end_code + nRanges - 1); michael@0: return (chEnd == 0xFFFF); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the Glyph ID for the given Unicode ID in the Microsoft Unicode subtable. michael@0: (Actually this code only depends on subtable being format 4.) michael@0: Return 0 if the Unicode ID is not in the subtable. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: gid16 CmapSubtable4Lookup(const void * pCmapSubtabel4, unsigned int nUnicodeId, int rangeKey) michael@0: { michael@0: const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast(pCmapSubtabel4); michael@0: michael@0: uint16 nSeg = be::swap(pTable->seg_count_x2) >> 1; michael@0: michael@0: uint16 n; michael@0: const uint16 * pLeft, * pMid; michael@0: uint16 cMid, chStart, chEnd; michael@0: michael@0: if (rangeKey) michael@0: { michael@0: pMid = &(pTable->end_code[rangeKey]); michael@0: chEnd = be::peek(pMid); michael@0: } michael@0: else michael@0: { michael@0: // Binary search of the endCode[] array michael@0: pLeft = &(pTable->end_code[0]); michael@0: n = nSeg; michael@0: while (n > 0) michael@0: { michael@0: cMid = n >> 1; // Pick an element in the middle michael@0: pMid = pLeft + cMid; michael@0: chEnd = be::peek(pMid); michael@0: if (nUnicodeId <= chEnd) michael@0: { michael@0: if (cMid == 0 || nUnicodeId > be::peek(pMid -1)) michael@0: break; // Must be this seg or none! michael@0: n = cMid; // Continue on left side, omitting mid point michael@0: } michael@0: else michael@0: { michael@0: pLeft = pMid + 1; // Continue on right side, omitting mid point michael@0: n -= (cMid + 1); michael@0: } michael@0: } michael@0: michael@0: if (!n) michael@0: return 0; michael@0: } michael@0: michael@0: // Ok, we're down to one segment and pMid points to the endCode element michael@0: // Either this is it or none is. michael@0: michael@0: chStart = be::peek(pMid += nSeg + 1); michael@0: if (chEnd >= nUnicodeId && nUnicodeId >= chStart) michael@0: { michael@0: // Found correct segment. Find Glyph Id michael@0: int16 idDelta = be::peek(pMid += nSeg); michael@0: uint16 idRangeOffset = be::peek(pMid += nSeg); michael@0: michael@0: if (idRangeOffset == 0) michael@0: return (uint16)(idDelta + nUnicodeId); // must use modulus 2^16 michael@0: michael@0: // Look up value in glyphIdArray michael@0: const ptrdiff_t offset = (nUnicodeId - chStart) + (idRangeOffset >> 1) + michael@0: (pMid - reinterpret_cast(pTable)); michael@0: if (offset * 2 >= be::swap(pTable->length)) michael@0: return 0; michael@0: gid16 nGlyphId = be::peek(reinterpret_cast(pTable)+offset); michael@0: // If this value is 0, return 0. Else add the idDelta michael@0: return nGlyphId ? nGlyphId + idDelta : 0; michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the next Unicode value in the cmap. Pass 0 to obtain the first item. michael@0: Returns 0xFFFF as the last item. michael@0: pRangeKey is an optional key that is used to optimize the search; its value is the range michael@0: in which the character is found. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: unsigned int CmapSubtable4NextCodepoint(const void *pCmap31, unsigned int nUnicodeId, int * pRangeKey) michael@0: { michael@0: const Sfnt::CmapSubTableFormat4 * pTable = reinterpret_cast(pCmap31); michael@0: michael@0: uint16 nRange = be::swap(pTable->seg_count_x2) >> 1; michael@0: michael@0: uint32 nUnicodePrev = (uint32)nUnicodeId; michael@0: michael@0: const uint16 * pStartCode = &(pTable->end_code[0]) michael@0: + nRange // length of end code array michael@0: + 1; // reserved word michael@0: michael@0: if (nUnicodePrev == 0) michael@0: { michael@0: // return the first codepoint. michael@0: if (pRangeKey) michael@0: *pRangeKey = 0; michael@0: return be::peek(pStartCode); michael@0: } michael@0: else if (nUnicodePrev >= 0xFFFF) michael@0: { michael@0: if (pRangeKey) michael@0: *pRangeKey = nRange - 1; michael@0: return 0xFFFF; michael@0: } michael@0: michael@0: int iRange = (pRangeKey) ? *pRangeKey : 0; michael@0: // Just in case we have a bad key: michael@0: while (iRange > 0 && be::peek(pStartCode + iRange) > nUnicodePrev) michael@0: iRange--; michael@0: while (be::peek(pTable->end_code + iRange) < nUnicodePrev) michael@0: iRange++; michael@0: michael@0: // Now iRange is the range containing nUnicodePrev. michael@0: unsigned int nStartCode = be::peek(pStartCode + iRange); michael@0: unsigned int nEndCode = be::peek(pTable->end_code + iRange); michael@0: michael@0: if (nStartCode > nUnicodePrev) michael@0: // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable michael@0: // answer this time around. michael@0: nUnicodePrev = nStartCode - 1; michael@0: michael@0: if (nEndCode > nUnicodePrev) michael@0: { michael@0: // Next is in the same range; it is the next successive codepoint. michael@0: if (pRangeKey) michael@0: *pRangeKey = iRange; michael@0: return nUnicodePrev + 1; michael@0: } michael@0: michael@0: // Otherwise the next codepoint is the first one in the next range. michael@0: // There is guaranteed to be a next range because there must be one that michael@0: // ends with 0xFFFF. michael@0: if (pRangeKey) michael@0: *pRangeKey = iRange + 1; michael@0: return be::peek(pStartCode + iRange + 1); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Check the Microsoft UCS-4 subtable for expected values. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool CheckCmapSubtable12(const void *pCmapSubtable12) michael@0: { michael@0: if (!pCmapSubtable12) return false; michael@0: const Sfnt::CmapSubTable * pTable = reinterpret_cast(pCmapSubtable12); michael@0: if (be::swap(pTable->format) != 12) michael@0: return false; michael@0: const Sfnt::CmapSubTableFormat12 * pTable12 = reinterpret_cast(pCmapSubtable12); michael@0: uint32 length = be::swap(pTable12->length); michael@0: if (length < sizeof(Sfnt::CmapSubTableFormat12)) michael@0: return false; michael@0: michael@0: return (length == (sizeof(Sfnt::CmapSubTableFormat12) + (be::swap(pTable12->num_groups) - 1) michael@0: * sizeof(uint32) * 3)); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the Glyph ID for the given Unicode ID in the Microsoft UCS-4 subtable. michael@0: (Actually this code only depends on subtable being format 12.) michael@0: Return 0 if the Unicode ID is not in the subtable. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: gid16 CmapSubtable12Lookup(const void * pCmap310, unsigned int uUnicodeId, int rangeKey) michael@0: { michael@0: const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast(pCmap310); michael@0: michael@0: //uint32 uLength = be::swap(pTable->length); //could use to test for premature end of table michael@0: uint32 ucGroups = be::swap(pTable->num_groups); michael@0: michael@0: for (unsigned int i = rangeKey; i < ucGroups; i++) michael@0: { michael@0: uint32 uStartCode = be::swap(pTable->group[i].start_char_code); michael@0: uint32 uEndCode = be::swap(pTable->group[i].end_char_code); michael@0: if (uUnicodeId >= uStartCode && uUnicodeId <= uEndCode) michael@0: { michael@0: uint32 uDiff = uUnicodeId - uStartCode; michael@0: uint32 uStartGid = be::swap(pTable->group[i].start_glyph_id); michael@0: return static_cast(uStartGid + uDiff); michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the next Unicode value in the cmap. Pass 0 to obtain the first item. michael@0: Returns 0x10FFFF as the last item. michael@0: pRangeKey is an optional key that is used to optimize the search; its value is the range michael@0: in which the character is found. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: unsigned int CmapSubtable12NextCodepoint(const void *pCmap310, unsigned int nUnicodeId, int * pRangeKey) michael@0: { michael@0: const Sfnt::CmapSubTableFormat12 * pTable = reinterpret_cast(pCmap310); michael@0: michael@0: int nRange = be::swap(pTable->num_groups); michael@0: michael@0: uint32 nUnicodePrev = (uint32)nUnicodeId; michael@0: michael@0: if (nUnicodePrev == 0) michael@0: { michael@0: // return the first codepoint. michael@0: if (pRangeKey) michael@0: *pRangeKey = 0; michael@0: return be::swap(pTable->group[0].start_char_code); michael@0: } michael@0: else if (nUnicodePrev >= 0x10FFFF) michael@0: { michael@0: if (pRangeKey) michael@0: *pRangeKey = nRange; michael@0: return 0x10FFFF; michael@0: } michael@0: michael@0: int iRange = (pRangeKey) ? *pRangeKey : 0; michael@0: // Just in case we have a bad key: michael@0: while (iRange > 0 && be::swap(pTable->group[iRange].start_char_code) > nUnicodePrev) michael@0: iRange--; michael@0: while (be::swap(pTable->group[iRange].end_char_code) < nUnicodePrev) michael@0: iRange++; michael@0: michael@0: // Now iRange is the range containing nUnicodePrev. michael@0: michael@0: unsigned int nStartCode = be::swap(pTable->group[iRange].start_char_code); michael@0: unsigned int nEndCode = be::swap(pTable->group[iRange].end_char_code); michael@0: michael@0: if (nStartCode > nUnicodePrev) michael@0: // Oops, nUnicodePrev is not in the cmap! Adjust so we get a reasonable michael@0: // answer this time around. michael@0: nUnicodePrev = nStartCode - 1; michael@0: michael@0: if (nEndCode > nUnicodePrev) michael@0: { michael@0: // Next is in the same range; it is the next successive codepoint. michael@0: if (pRangeKey) michael@0: *pRangeKey = iRange; michael@0: return nUnicodePrev + 1; michael@0: } michael@0: michael@0: // Otherwise the next codepoint is the first one in the next range, or 10FFFF if we're done. michael@0: if (pRangeKey) michael@0: *pRangeKey = iRange + 1; michael@0: return (iRange + 1 >= nRange) ? 0x10FFFF : be::swap(pTable->group[iRange + 1].start_char_code); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the offset stored in the loca table for the given Glyph ID. michael@0: (This offset is into the glyf table.) michael@0: Return -1 if the lookup failed. michael@0: Technically this method should return an unsigned long but it is unlikely the offset will michael@0: exceed 2^31. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: size_t LocaLookup(gid16 nGlyphId, michael@0: const void * pLoca, size_t lLocaSize, michael@0: const void * pHead) // throw (std::out_of_range) michael@0: { michael@0: const Sfnt::FontHeader * pTable = reinterpret_cast(pHead); michael@0: michael@0: // CheckTable verifies the index_to_loc_format is valid michael@0: if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat) michael@0: { // loca entries are two bytes and have been divided by two michael@0: if (nGlyphId < (lLocaSize >> 1) - 1) // allow sentinel value to be accessed michael@0: { michael@0: const uint16 * pShortTable = reinterpret_cast(pLoca); michael@0: return (be::peek(pShortTable + nGlyphId) << 1); michael@0: } michael@0: } michael@0: michael@0: if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat) michael@0: { // loca entries are four bytes michael@0: if (nGlyphId < (lLocaSize >> 2) - 1) michael@0: { michael@0: const uint32 * pLongTable = reinterpret_cast(pLoca); michael@0: return be::peek(pLongTable + nGlyphId); michael@0: } michael@0: } michael@0: michael@0: // only get here if glyph id was bad michael@0: return -1; michael@0: //throw std::out_of_range("glyph id out of range for font"); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return a pointer into the glyf table based on the given offset (from LocaLookup). michael@0: Return NULL on error. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: void * GlyfLookup(const void * pGlyf, size_t nGlyfOffset, size_t nTableLen) michael@0: { michael@0: const uint8 * pByte = reinterpret_cast(pGlyf); michael@0: if (nGlyfOffset == size_t(-1) || nGlyfOffset >= nTableLen) michael@0: return NULL; michael@0: return const_cast(pByte + nGlyfOffset); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the bounding box coordinates for a simple glyf entry (non-composite). michael@0: Return true if successful, false otherwise. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GlyfBox(const void * pSimpleGlyf, int & xMin, int & yMin, michael@0: int & xMax, int & yMax) michael@0: { michael@0: const Sfnt::Glyph * pGlyph = reinterpret_cast(pSimpleGlyf); michael@0: michael@0: xMin = be::swap(pGlyph->x_min); michael@0: yMin = be::swap(pGlyph->y_min); michael@0: xMax = be::swap(pGlyph->x_max); michael@0: yMax = be::swap(pGlyph->y_max); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: #ifdef ALL_TTFUTILS michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the number of contours for a simple glyf entry (non-composite) michael@0: Returning -1 means this is a composite glyph michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: int GlyfContourCount(const void * pSimpleGlyf) michael@0: { michael@0: const Sfnt::Glyph * pGlyph = reinterpret_cast(pSimpleGlyf); michael@0: return be::swap(pGlyph->number_of_contours); // -1 means composite glyph michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the point numbers for the end points of the glyph contours for a simple michael@0: glyf entry (non-composite). michael@0: cnPointsTotal - count of contours from GlyfContourCount(); (same as number of end points) michael@0: prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers michael@0: cnPoints - count of points placed in above range michael@0: Return true if successful, false otherwise. michael@0: False could indicate a multi-level composite glyphs. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GlyfContourEndPoints(const void * pSimpleGlyf, int * prgnContourEndPoint, michael@0: int cnPointsTotal, int & cnPoints) michael@0: { michael@0: const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast(pSimpleGlyf); michael@0: michael@0: int cContours = be::swap(pGlyph->number_of_contours); michael@0: if (cContours < 0) michael@0: return false; // this method isn't supposed handle composite glyphs michael@0: michael@0: for (int i = 0; i < cContours && i < cnPointsTotal; i++) michael@0: { michael@0: prgnContourEndPoint[i] = be::swap(pGlyph->end_pts_of_contours[i]); michael@0: } michael@0: michael@0: cnPoints = cContours; michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the points for a simple glyf entry (non-composite) michael@0: cnPointsTotal - count of points from largest end point obtained from GlyfContourEndPoints michael@0: prgnX & prgnY - should point to buffers large enough to hold cnPointsTotal integers michael@0: The ranges are parallel so that coordinates for point(n) are found at offset n in both michael@0: ranges. This is raw point data with relative coordinates. michael@0: prgbFlag - should point to a buffer a large enough to hold cnPointsTotal bytes michael@0: This range is parallel to the prgnX & prgnY michael@0: cnPoints - count of points placed in above ranges michael@0: Return true if successful, false otherwise. michael@0: False could indicate a composite glyph michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GlyfPoints(const void * pSimpleGlyf, int * prgnX, int * prgnY, michael@0: char * prgbFlag, int cnPointsTotal, int & cnPoints) michael@0: { michael@0: using namespace Sfnt; michael@0: michael@0: const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast(pSimpleGlyf); michael@0: int cContours = be::swap(pGlyph->number_of_contours); michael@0: // return false for composite glyph michael@0: if (cContours <= 0) michael@0: return false; michael@0: int cPts = be::swap(pGlyph->end_pts_of_contours[cContours - 1]) + 1; michael@0: if (cPts > cnPointsTotal) michael@0: return false; michael@0: michael@0: // skip over bounding box data & point to byte count of instructions (hints) michael@0: const uint8 * pbGlyph = reinterpret_cast michael@0: (&pGlyph->end_pts_of_contours[cContours]); michael@0: michael@0: // skip over hints & point to first flag michael@0: int cbHints = be::swap(*(uint16 *)pbGlyph); michael@0: pbGlyph += sizeof(uint16); michael@0: pbGlyph += cbHints; michael@0: michael@0: // load flags & point to first x coordinate michael@0: int iFlag = 0; michael@0: while (iFlag < cPts) michael@0: { michael@0: if (!(*pbGlyph & SimpleGlyph::Repeat)) michael@0: { // flag isn't repeated michael@0: prgbFlag[iFlag] = (char)*pbGlyph; michael@0: pbGlyph++; michael@0: iFlag++; michael@0: } michael@0: else michael@0: { // flag is repeated; count specified by next byte michael@0: char chFlag = (char)*pbGlyph; michael@0: pbGlyph++; michael@0: int cFlags = (int)*pbGlyph; michael@0: pbGlyph++; michael@0: prgbFlag[iFlag] = chFlag; michael@0: iFlag++; michael@0: for (int i = 0; i < cFlags; i++) michael@0: { michael@0: prgbFlag[iFlag + i] = chFlag; michael@0: } michael@0: iFlag += cFlags; michael@0: } michael@0: } michael@0: if (iFlag != cPts) michael@0: return false; michael@0: michael@0: // load x coordinates michael@0: iFlag = 0; michael@0: while (iFlag < cPts) michael@0: { michael@0: if (prgbFlag[iFlag] & SimpleGlyph::XShort) michael@0: { michael@0: prgnX[iFlag] = *pbGlyph; michael@0: if (!(prgbFlag[iFlag] & SimpleGlyph::XIsPos)) michael@0: { michael@0: prgnX[iFlag] = -prgnX[iFlag]; michael@0: } michael@0: pbGlyph++; michael@0: } michael@0: else michael@0: { michael@0: if (prgbFlag[iFlag] & SimpleGlyph::XIsSame) michael@0: { michael@0: prgnX[iFlag] = 0; michael@0: // do NOT increment pbGlyph michael@0: } michael@0: else michael@0: { michael@0: prgnX[iFlag] = be::swap(*(int16 *)pbGlyph); michael@0: pbGlyph += sizeof(int16); michael@0: } michael@0: } michael@0: iFlag++; michael@0: } michael@0: michael@0: // load y coordinates michael@0: iFlag = 0; michael@0: while (iFlag < cPts) michael@0: { michael@0: if (prgbFlag[iFlag] & SimpleGlyph::YShort) michael@0: { michael@0: prgnY[iFlag] = *pbGlyph; michael@0: if (!(prgbFlag[iFlag] & SimpleGlyph::YIsPos)) michael@0: { michael@0: prgnY[iFlag] = -prgnY[iFlag]; michael@0: } michael@0: pbGlyph++; michael@0: } michael@0: else michael@0: { michael@0: if (prgbFlag[iFlag] & SimpleGlyph::YIsSame) michael@0: { michael@0: prgnY[iFlag] = 0; michael@0: // do NOT increment pbGlyph michael@0: } michael@0: else michael@0: { michael@0: prgnY[iFlag] = be::swap(*(int16 *)pbGlyph); michael@0: pbGlyph += sizeof(int16); michael@0: } michael@0: } michael@0: iFlag++; michael@0: } michael@0: michael@0: cnPoints = cPts; michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Fill prgnCompId with the component Glyph IDs from pSimpleGlyf. michael@0: Client must allocate space before calling. michael@0: pSimpleGlyf - assumed to point to a composite glyph michael@0: cCompIdTotal - the number of elements in prgnCompId michael@0: cCompId - the total number of Glyph IDs stored in prgnCompId michael@0: Return true if successful, false otherwise michael@0: False could indicate a non-composite glyph or the input array was not big enough michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GetComponentGlyphIds(const void * pSimpleGlyf, int * prgnCompId, michael@0: size_t cnCompIdTotal, size_t & cnCompId) michael@0: { michael@0: using namespace Sfnt; michael@0: michael@0: if (GlyfContourCount(pSimpleGlyf) >= 0) michael@0: return false; michael@0: michael@0: const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast(pSimpleGlyf); michael@0: // for a composite glyph, the special data begins here michael@0: const uint8 * pbGlyph = reinterpret_cast(&pGlyph->end_pts_of_contours[0]); michael@0: michael@0: uint16 GlyphFlags; michael@0: size_t iCurrentComp = 0; michael@0: do michael@0: { michael@0: GlyphFlags = be::swap(*((uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: prgnCompId[iCurrentComp++] = be::swap(*((uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: if (iCurrentComp >= cnCompIdTotal) michael@0: return false; michael@0: int nOffset = 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; michael@0: pbGlyph += nOffset; michael@0: } while (GlyphFlags & CompoundGlyph::MoreComponents); michael@0: michael@0: cnCompId = iCurrentComp; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return info on how a component glyph is to be placed michael@0: pSimpleGlyph - assumed to point to a composite glyph michael@0: nCompId - glyph id for component of interest michael@0: bOffset - if true, a & b are the x & y offsets for this component michael@0: if false, b is the point on this component that is attaching to point a on the michael@0: preceding glyph michael@0: Return true if successful, false otherwise michael@0: False could indicate a non-composite glyph or that component wasn't found michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GetComponentPlacement(const void * pSimpleGlyf, int nCompId, michael@0: bool fOffset, int & a, int & b) michael@0: { michael@0: using namespace Sfnt; michael@0: michael@0: if (GlyfContourCount(pSimpleGlyf) >= 0) michael@0: return false; michael@0: michael@0: const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast(pSimpleGlyf); michael@0: // for a composite glyph, the special data begins here michael@0: const uint8 * pbGlyph = reinterpret_cast(&pGlyph->end_pts_of_contours[0]); michael@0: michael@0: uint16 GlyphFlags; michael@0: do michael@0: { michael@0: GlyphFlags = be::swap(*((uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: if (be::swap(*((uint16 *)pbGlyph)) == nCompId) michael@0: { michael@0: pbGlyph += sizeof(uint16); // skip over glyph id of component michael@0: fOffset = (GlyphFlags & CompoundGlyph::ArgsAreXYValues) == CompoundGlyph::ArgsAreXYValues; michael@0: michael@0: if (GlyphFlags & CompoundGlyph::Arg1Arg2Words ) michael@0: { michael@0: a = be::swap(*(int16 *)pbGlyph); michael@0: pbGlyph += sizeof(int16); michael@0: b = be::swap(*(int16 *)pbGlyph); michael@0: pbGlyph += sizeof(int16); michael@0: } michael@0: else michael@0: { // args are signed bytes michael@0: a = *pbGlyph++; michael@0: b = *pbGlyph++; michael@0: } michael@0: return true; michael@0: } michael@0: pbGlyph += sizeof(uint16); // skip over glyph id of component michael@0: int nOffset = 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; michael@0: pbGlyph += nOffset; michael@0: } while (GlyphFlags & CompoundGlyph::MoreComponents); michael@0: michael@0: // didn't find requested component michael@0: fOffset = true; michael@0: a = 0; michael@0: b = 0; michael@0: return false; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return info on how a component glyph is to be transformed michael@0: pSimpleGlyph - assumed to point to a composite glyph michael@0: nCompId - glyph id for component of interest michael@0: flt11, flt11, flt11, flt11 - a 2x2 matrix giving the transform michael@0: bTransOffset - whether to transform the offset from above method michael@0: The spec is unclear about the meaning of this flag michael@0: Currently - initialize to true for MS rasterizer and false for Mac rasterizer, then michael@0: on return it will indicate whether transform should apply to offset (MSDN CD 10/99) michael@0: Return true if successful, false otherwise michael@0: False could indicate a non-composite glyph or that component wasn't found michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GetComponentTransform(const void * pSimpleGlyf, int nCompId, michael@0: float & flt11, float & flt12, float & flt21, float & flt22, michael@0: bool & fTransOffset) michael@0: { michael@0: using namespace Sfnt; michael@0: michael@0: if (GlyfContourCount(pSimpleGlyf) >= 0) michael@0: return false; michael@0: michael@0: const Sfnt::SimpleGlyph * pGlyph = reinterpret_cast(pSimpleGlyf); michael@0: // for a composite glyph, the special data begins here michael@0: const uint8 * pbGlyph = reinterpret_cast(&pGlyph->end_pts_of_contours[0]); michael@0: michael@0: uint16 GlyphFlags; michael@0: do michael@0: { michael@0: GlyphFlags = be::swap(*((uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: if (be::swap(*((uint16 *)pbGlyph)) == nCompId) michael@0: { michael@0: pbGlyph += sizeof(uint16); // skip over glyph id of component michael@0: pbGlyph += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; // skip over placement data michael@0: michael@0: if (fTransOffset) // MS rasterizer michael@0: fTransOffset = !(GlyphFlags & CompoundGlyph::UnscaledOffset); michael@0: else // Apple rasterizer michael@0: fTransOffset = (GlyphFlags & CompoundGlyph::ScaledOffset) != 0; michael@0: michael@0: if (GlyphFlags & CompoundGlyph::HaveScale) michael@0: { michael@0: flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: flt12 = 0; michael@0: flt21 = 0; michael@0: flt22 = flt11; michael@0: } michael@0: else if (GlyphFlags & CompoundGlyph::HaveXAndYScale) michael@0: { michael@0: flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: flt12 = 0; michael@0: flt21 = 0; michael@0: flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: } michael@0: else if (GlyphFlags & CompoundGlyph::HaveTwoByTwo) michael@0: { michael@0: flt11 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: flt12 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: flt21 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: flt22 = fixed_to_float<14>(be::swap(*(uint16 *)pbGlyph)); michael@0: pbGlyph += sizeof(uint16); michael@0: } michael@0: else michael@0: { // identity transform michael@0: flt11 = 1.0; michael@0: flt12 = 0.0; michael@0: flt21 = 0.0; michael@0: flt22 = 1.0; michael@0: } michael@0: return true; michael@0: } michael@0: pbGlyph += sizeof(uint16); // skip over glyph id of component michael@0: int nOffset = 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::Arg1Arg2Words ? 4 : 2; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveScale ? 2 : 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveXAndYScale ? 4 : 0; michael@0: nOffset += GlyphFlags & CompoundGlyph::HaveTwoByTwo ? 8 : 0; michael@0: pbGlyph += nOffset; michael@0: } while (GlyphFlags & CompoundGlyph::MoreComponents); michael@0: michael@0: // didn't find requested component michael@0: fTransOffset = false; michael@0: flt11 = 1; michael@0: flt12 = 0; michael@0: flt21 = 0; michael@0: flt22 = 1; michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return a pointer into the glyf table based on the given tables and Glyph ID michael@0: Since this method doesn't check for spaces, it is good to call IsSpace before using it. michael@0: Return NULL on error. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: void * GlyfLookup(gid16 nGlyphId, const void * pGlyf, const void * pLoca, michael@0: size_t lGlyfSize, size_t lLocaSize, const void * pHead) michael@0: { michael@0: // test for valid glyph id michael@0: // CheckTable verifies the index_to_loc_format is valid michael@0: michael@0: const Sfnt::FontHeader * pTable michael@0: = reinterpret_cast(pHead); michael@0: michael@0: if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::ShortIndexLocFormat) michael@0: { // loca entries are two bytes (and have been divided by two) michael@0: if (nGlyphId >= (lLocaSize >> 1) - 1) // don't allow nGlyphId to access sentinel michael@0: { michael@0: // throw std::out_of_range("glyph id out of range for font"); michael@0: return NULL; michael@0: } michael@0: } michael@0: if (be::swap(pTable->index_to_loc_format) == Sfnt::FontHeader::LongIndexLocFormat) michael@0: { // loca entries are four bytes michael@0: if (nGlyphId >= (lLocaSize >> 2) - 1) michael@0: { michael@0: // throw std::out_of_range("glyph id out of range for font"); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: long lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead); michael@0: void * pSimpleGlyf = GlyfLookup(pGlyf, lGlyfOffset, lGlyfSize); // invalid loca offset returns null michael@0: return pSimpleGlyf; michael@0: } michael@0: michael@0: #ifdef ALL_TTFUTILS michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Determine if a particular Glyph ID has any data in the glyf table. If it is white space, michael@0: there will be no glyf data, though there will be metric data in hmtx, etc. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool IsSpace(gid16 nGlyphId, const void * pLoca, size_t lLocaSize, const void * pHead) michael@0: { michael@0: size_t lGlyfOffset = LocaLookup(nGlyphId, pLoca, lLocaSize, pHead); michael@0: michael@0: // the +1 should always work because there is a sentinel value at the end of the loca table michael@0: size_t lNextGlyfOffset = LocaLookup(nGlyphId + 1, pLoca, lLocaSize, pHead); michael@0: michael@0: return (lNextGlyfOffset - lGlyfOffset) == 0; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Determine if a particular Glyph ID is a multi-level composite. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool IsDeepComposite(gid16 nGlyphId, const void * pGlyf, const void * pLoca, michael@0: size_t lGlyfSize, long lLocaSize, const void * pHead) michael@0: { michael@0: if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} michael@0: michael@0: void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == NULL) michael@0: return false; // no way to really indicate an error occured here michael@0: michael@0: if (GlyfContourCount(pSimpleGlyf) >= 0) michael@0: return false; michael@0: michael@0: int rgnCompId[kMaxGlyphComponents]; // assumes only a limited number of glyph components michael@0: size_t cCompIdTotal = kMaxGlyphComponents; michael@0: size_t cCompId = 0; michael@0: michael@0: if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) michael@0: return false; michael@0: michael@0: for (size_t i = 0; i < cCompId; i++) michael@0: { michael@0: pSimpleGlyf = GlyfLookup(static_cast(rgnCompId[i]), michael@0: pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == NULL) {return false;} michael@0: michael@0: if (GlyfContourCount(pSimpleGlyf) < 0) michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the bounding box coordinates based on the given tables and Glyph ID michael@0: Handles both simple and composite glyphs. michael@0: Return true if successful, false otherwise. On false, all point values will be INT_MIN michael@0: False may indicate a white space glyph michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GlyfBox(gid16 nGlyphId, const void * pGlyf, const void * pLoca, michael@0: size_t lGlyfSize, size_t lLocaSize, const void * pHead, int & xMin, int & yMin, int & xMax, int & yMax) michael@0: { michael@0: xMin = yMin = xMax = yMax = INT_MIN; michael@0: michael@0: if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} michael@0: michael@0: void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == NULL) {return false;} michael@0: michael@0: return GlyfBox(pSimpleGlyf, xMin, yMin, xMax, yMax); michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the number of contours based on the given tables and Glyph ID michael@0: Handles both simple and composite glyphs. michael@0: Return true if successful, false otherwise. On false, cnContours will be INT_MIN michael@0: False may indicate a white space glyph or a multi-level composite glyph. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GlyfContourCount(gid16 nGlyphId, const void * pGlyf, const void * pLoca, michael@0: size_t lGlyfSize, size_t lLocaSize, const void * pHead, size_t & cnContours) michael@0: { michael@0: cnContours = static_cast(INT_MIN); michael@0: michael@0: if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} michael@0: michael@0: void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == NULL) {return false;} michael@0: michael@0: int cRtnContours = GlyfContourCount(pSimpleGlyf); michael@0: if (cRtnContours >= 0) michael@0: { michael@0: cnContours = size_t(cRtnContours); michael@0: return true; michael@0: } michael@0: michael@0: //handle composite glyphs michael@0: michael@0: int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components michael@0: size_t cCompIdTotal = kMaxGlyphComponents; michael@0: size_t cCompId = 0; michael@0: michael@0: if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) michael@0: return false; michael@0: michael@0: cRtnContours = 0; michael@0: int cTmp = 0; michael@0: for (size_t i = 0; i < cCompId; i++) michael@0: { michael@0: if (IsSpace(static_cast(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} michael@0: pSimpleGlyf = GlyfLookup(static_cast(rgnCompId[i]), michael@0: pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == 0) {return false;} michael@0: // return false on multi-level composite michael@0: if ((cTmp = GlyfContourCount(pSimpleGlyf)) < 0) michael@0: return false; michael@0: cRtnContours += cTmp; michael@0: } michael@0: michael@0: cnContours = size_t(cRtnContours); michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the point numbers for the end points of the glyph contours based on the given tables michael@0: and Glyph ID michael@0: Handles both simple and composite glyphs. michael@0: cnPoints - count of contours from GlyfContourCount (same as number of end points) michael@0: prgnContourEndPoints - should point to a buffer large enough to hold cnPoints integers michael@0: Return true if successful, false otherwise. On false, all end points are INT_MIN michael@0: False may indicate a white space glyph or a multi-level composite glyph. michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GlyfContourEndPoints(gid16 nGlyphId, const void * pGlyf, const void * pLoca, michael@0: size_t lGlyfSize, size_t lLocaSize, const void * pHead, michael@0: int * prgnContourEndPoint, size_t cnPoints) michael@0: { michael@0: memset(prgnContourEndPoint, 0xFF, cnPoints * sizeof(int)); michael@0: // std::fill_n(prgnContourEndPoint, cnPoints, INT_MIN); michael@0: michael@0: if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) {return false;} michael@0: michael@0: void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == NULL) {return false;} michael@0: michael@0: int cContours = GlyfContourCount(pSimpleGlyf); michael@0: int cActualPts = 0; michael@0: if (cContours > 0) michael@0: return GlyfContourEndPoints(pSimpleGlyf, prgnContourEndPoint, cnPoints, cActualPts); michael@0: michael@0: // handle composite glyphs michael@0: michael@0: int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components michael@0: size_t cCompIdTotal = kMaxGlyphComponents; michael@0: size_t cCompId = 0; michael@0: michael@0: if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) michael@0: return false; michael@0: michael@0: int * prgnCurrentEndPoint = prgnContourEndPoint; michael@0: int cCurrentPoints = cnPoints; michael@0: int nPrevPt = 0; michael@0: for (size_t i = 0; i < cCompId; i++) michael@0: { michael@0: if (IsSpace(static_cast(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} michael@0: pSimpleGlyf = GlyfLookup(static_cast(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == NULL) {return false;} michael@0: // returns false on multi-level composite michael@0: if (!GlyfContourEndPoints(pSimpleGlyf, prgnCurrentEndPoint, cCurrentPoints, cActualPts)) michael@0: return false; michael@0: // points in composite are numbered sequentially as components are added michael@0: // must adjust end point numbers for new point numbers michael@0: for (int j = 0; j < cActualPts; j++) michael@0: prgnCurrentEndPoint[j] += nPrevPt; michael@0: nPrevPt = prgnCurrentEndPoint[cActualPts - 1] + 1; michael@0: michael@0: prgnCurrentEndPoint += cActualPts; michael@0: cCurrentPoints -= cActualPts; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Get the points for a glyph based on the given tables and Glyph ID michael@0: Handles both simple and composite glyphs. michael@0: cnPoints - count of points from largest end point obtained from GlyfContourEndPoints michael@0: prgnX & prgnY - should point to buffers large enough to hold cnPoints integers michael@0: The ranges are parallel so that coordinates for point(n) are found at offset n in michael@0: both ranges. These points are in absolute coordinates. michael@0: prgfOnCurve - should point to a buffer a large enough to hold cnPoints bytes (bool) michael@0: This range is parallel to the prgnX & prgnY michael@0: Return true if successful, false otherwise. On false, all points may be INT_MIN michael@0: False may indicate a white space glyph, a multi-level composite, or a corrupt font michael@0: // TODO: doesn't support composite glyphs whose components are themselves components michael@0: It's not clear from the TTF spec when the transforms should be applied. Should the michael@0: transform be done before or after attachment point calcs? (current code - before) michael@0: Should the transform be applied to other offsets? (currently - no; however commented michael@0: out code is in place so that if CompoundGlyph::UnscaledOffset on the MS rasterizer is michael@0: clear (typical) then yes, and if CompoundGlyph::ScaledOffset on the Apple rasterizer is michael@0: clear (typical?) then no). See GetComponentTransform. michael@0: It's also unclear where point numbering with attachment poinst starts michael@0: (currently - first point number is relative to whole glyph, second point number is michael@0: relative to current glyph). michael@0: ----------------------------------------------------------------------------------------------*/ michael@0: bool GlyfPoints(gid16 nGlyphId, const void * pGlyf, michael@0: const void * pLoca, size_t lGlyfSize, size_t lLocaSize, const void * pHead, michael@0: const int * /*prgnContourEndPoint*/, size_t /*cnEndPoints*/, michael@0: int * prgnX, int * prgnY, bool * prgfOnCurve, size_t cnPoints) michael@0: { michael@0: memset(prgnX, 0x7F, cnPoints * sizeof(int)); michael@0: memset(prgnY, 0x7F, cnPoints * sizeof(int)); michael@0: michael@0: if (IsSpace(nGlyphId, pLoca, lLocaSize, pHead)) michael@0: return false; michael@0: michael@0: void * pSimpleGlyf = GlyfLookup(nGlyphId, pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pSimpleGlyf == NULL) michael@0: return false; michael@0: michael@0: int cContours = GlyfContourCount(pSimpleGlyf); michael@0: int cActualPts; michael@0: if (cContours > 0) michael@0: { michael@0: if (!GlyfPoints(pSimpleGlyf, prgnX, prgnY, (char *)prgfOnCurve, cnPoints, cActualPts)) michael@0: return false; michael@0: CalcAbsolutePoints(prgnX, prgnY, cnPoints); michael@0: SimplifyFlags((char *)prgfOnCurve, cnPoints); michael@0: return true; michael@0: } michael@0: michael@0: // handle composite glyphs michael@0: int rgnCompId[kMaxGlyphComponents]; // assumes no glyph will be made of more than 8 components michael@0: size_t cCompIdTotal = kMaxGlyphComponents; michael@0: size_t cCompId = 0; michael@0: michael@0: // this will fail if there are more components than there is room for michael@0: if (!GetComponentGlyphIds(pSimpleGlyf, rgnCompId, cCompIdTotal, cCompId)) michael@0: return false; michael@0: michael@0: int * prgnCurrentX = prgnX; michael@0: int * prgnCurrentY = prgnY; michael@0: char * prgbCurrentFlag = (char *)prgfOnCurve; // converting bool to char should be safe michael@0: int cCurrentPoints = cnPoints; michael@0: bool fOffset = true, fTransOff = true; michael@0: int a, b; michael@0: float flt11, flt12, flt21, flt22; michael@0: // int * prgnPrevX = prgnX; // in case first att pt number relative to preceding glyph michael@0: // int * prgnPrevY = prgnY; michael@0: for (size_t i = 0; i < cCompId; i++) michael@0: { michael@0: if (IsSpace(static_cast(rgnCompId[i]), pLoca, lLocaSize, pHead)) {return false;} michael@0: void * pCompGlyf = GlyfLookup(static_cast(rgnCompId[i]), pGlyf, pLoca, lGlyfSize, lLocaSize, pHead); michael@0: if (pCompGlyf == NULL) {return false;} michael@0: // returns false on multi-level composite michael@0: if (!GlyfPoints(pCompGlyf, prgnCurrentX, prgnCurrentY, prgbCurrentFlag, michael@0: cCurrentPoints, cActualPts)) michael@0: return false; michael@0: if (!GetComponentPlacement(pSimpleGlyf, rgnCompId[i], fOffset, a, b)) michael@0: return false; michael@0: if (!GetComponentTransform(pSimpleGlyf, rgnCompId[i], michael@0: flt11, flt12, flt21, flt22, fTransOff)) michael@0: return false; michael@0: bool fIdTrans = flt11 == 1.0 && flt12 == 0.0 && flt21 == 0.0 && flt22 == 1.0; michael@0: michael@0: // convert points to absolute coordinates michael@0: // do before transform and attachment point placement are applied michael@0: CalcAbsolutePoints(prgnCurrentX, prgnCurrentY, cActualPts); michael@0: michael@0: // apply transform - see main method note above michael@0: // do before attachment point calcs michael@0: if (!fIdTrans) michael@0: for (int j = 0; j < cActualPts; j++) michael@0: { michael@0: int x = prgnCurrentX[j]; // store before transform applied michael@0: int y = prgnCurrentY[j]; michael@0: prgnCurrentX[j] = (int)(x * flt11 + y * flt12); michael@0: prgnCurrentY[j] = (int)(x * flt21 + y * flt22); michael@0: } michael@0: michael@0: // apply placement - see main method note above michael@0: int nXOff, nYOff; michael@0: if (fOffset) // explicit x & y offsets michael@0: { michael@0: /* ignore fTransOff for now michael@0: if (fTransOff && !fIdTrans) michael@0: { // transform x & y offsets michael@0: nXOff = (int)(a * flt11 + b * flt12); michael@0: nYOff = (int)(a * flt21 + b * flt22); michael@0: } michael@0: else */ michael@0: { // don't transform offset michael@0: nXOff = a; michael@0: nYOff = b; michael@0: } michael@0: } michael@0: else // attachment points michael@0: { // in case first point is relative to preceding glyph and second relative to current michael@0: // nXOff = prgnPrevX[a] - prgnCurrentX[b]; michael@0: // nYOff = prgnPrevY[a] - prgnCurrentY[b]; michael@0: // first point number relative to whole composite, second relative to current glyph michael@0: nXOff = prgnX[a] - prgnCurrentX[b]; michael@0: nYOff = prgnY[a] - prgnCurrentY[b]; michael@0: } michael@0: for (int j = 0; j < cActualPts; j++) michael@0: { michael@0: prgnCurrentX[j] += nXOff; michael@0: prgnCurrentY[j] += nYOff; michael@0: } michael@0: michael@0: // prgnPrevX = prgnCurrentX; michael@0: // prgnPrevY = prgnCurrentY; michael@0: prgnCurrentX += cActualPts; michael@0: prgnCurrentY += cActualPts; michael@0: prgbCurrentFlag += cActualPts; michael@0: cCurrentPoints -= cActualPts; michael@0: } michael@0: michael@0: SimplifyFlags((char *)prgfOnCurve, cnPoints); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Simplify the meaning of flags to just indicate whether point is on-curve or off-curve. michael@0: ---------------------------------------------------------------------------------------------*/ michael@0: bool SimplifyFlags(char * prgbFlags, int cnPoints) michael@0: { michael@0: for (int i = 0; i < cnPoints; i++) michael@0: prgbFlags[i] = static_cast(prgbFlags[i] & Sfnt::SimpleGlyph::OnCurve); michael@0: return true; michael@0: } michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Convert relative point coordinates to absolute coordinates michael@0: Points are stored in the font such that they are offsets from one another except for the michael@0: first point of a glyph. michael@0: ---------------------------------------------------------------------------------------------*/ michael@0: bool CalcAbsolutePoints(int * prgnX, int * prgnY, int cnPoints) michael@0: { michael@0: int nX = prgnX[0]; michael@0: int nY = prgnY[0]; michael@0: for (int i = 1; i < cnPoints; i++) michael@0: { michael@0: prgnX[i] += nX; michael@0: nX = prgnX[i]; michael@0: prgnY[i] += nY; michael@0: nY = prgnY[i]; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: /*---------------------------------------------------------------------------------------------- michael@0: Return the length of the 'name' table in bytes. michael@0: Currently used. michael@0: ---------------------------------------------------------------------------------------------*/ michael@0: #if 0 michael@0: size_t NameTableLength(const byte * pTable) michael@0: { michael@0: byte * pb = (const_cast(pTable)) + 2; // skip format michael@0: size_t cRecords = *pb++ << 8; cRecords += *pb++; michael@0: int dbStringOffset0 = (*pb++) << 8; dbStringOffset0 += *pb++; michael@0: int dbMaxStringOffset = 0; michael@0: for (size_t irec = 0; irec < cRecords; irec++) michael@0: { michael@0: int nPlatform = (*pb++) << 8; nPlatform += *pb++; michael@0: int nEncoding = (*pb++) << 8; nEncoding += *pb++; michael@0: int nLanguage = (*pb++) << 8; nLanguage += *pb++; michael@0: int nName = (*pb++) << 8; nName += *pb++; michael@0: int cbStringLen = (*pb++) << 8; cbStringLen += *pb++; michael@0: int dbStringOffset = (*pb++) << 8; dbStringOffset += *pb++; michael@0: if (dbMaxStringOffset < dbStringOffset + cbStringLen) michael@0: dbMaxStringOffset = dbStringOffset + cbStringLen; michael@0: } michael@0: return dbStringOffset0 + dbMaxStringOffset; michael@0: } michael@0: #endif michael@0: michael@0: } // end of namespace TtfUtil michael@0: } // end of namespace graphite