michael@0: /* michael@0: * Copyright © 2007,2008,2009 Red Hat, Inc. michael@0: * Copyright © 2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_OPEN_FILE_PRIVATE_HH michael@0: #define HB_OPEN_FILE_PRIVATE_HH michael@0: michael@0: #include "hb-open-type-private.hh" michael@0: michael@0: michael@0: namespace OT { michael@0: michael@0: michael@0: /* michael@0: * michael@0: * The OpenType Font File michael@0: * michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Organization of an OpenType Font michael@0: */ michael@0: michael@0: struct OpenTypeFontFile; michael@0: struct OffsetTable; michael@0: struct TTCHeader; michael@0: michael@0: michael@0: typedef struct TableRecord michael@0: { michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (c->check_struct (this)); michael@0: } michael@0: michael@0: Tag tag; /* 4-byte identifier. */ michael@0: CheckSum checkSum; /* CheckSum for this table. */ michael@0: ULONG offset; /* Offset from beginning of TrueType font michael@0: * file. */ michael@0: ULONG length; /* Length of this table. */ michael@0: public: michael@0: DEFINE_SIZE_STATIC (16); michael@0: } OpenTypeTable; michael@0: michael@0: typedef struct OffsetTable michael@0: { michael@0: friend struct OpenTypeFontFile; michael@0: michael@0: inline unsigned int get_table_count (void) const michael@0: { return numTables; } michael@0: inline const TableRecord& get_table (unsigned int i) const michael@0: { michael@0: if (unlikely (i >= numTables)) return Null(TableRecord); michael@0: return tables[i]; michael@0: } michael@0: inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const michael@0: { michael@0: Tag t; michael@0: t.set (tag); michael@0: unsigned int count = numTables; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: { michael@0: if (t == tables[i].tag) michael@0: { michael@0: if (table_index) *table_index = i; michael@0: return true; michael@0: } michael@0: } michael@0: if (table_index) *table_index = Index::NOT_FOUND_INDEX; michael@0: return false; michael@0: } michael@0: inline const TableRecord& get_table_by_tag (hb_tag_t tag) const michael@0: { michael@0: unsigned int table_index; michael@0: find_table_index (tag, &table_index); michael@0: return get_table (table_index); michael@0: } michael@0: michael@0: public: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (c->check_struct (this) && c->check_array (tables, TableRecord::static_size, numTables)); michael@0: } michael@0: michael@0: protected: michael@0: Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */ michael@0: USHORT numTables; /* Number of tables. */ michael@0: USHORT searchRange; /* (Maximum power of 2 <= numTables) x 16 */ michael@0: USHORT entrySelector; /* Log2(maximum power of 2 <= numTables). */ michael@0: USHORT rangeShift; /* NumTables x 16-searchRange. */ michael@0: TableRecord tables[VAR]; /* TableRecord entries. numTables items */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (12, tables); michael@0: } OpenTypeFontFace; michael@0: michael@0: michael@0: /* michael@0: * TrueType Collections michael@0: */ michael@0: michael@0: struct TTCHeaderVersion1 michael@0: { michael@0: friend struct TTCHeader; michael@0: michael@0: inline unsigned int get_face_count (void) const { return table.len; } michael@0: inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (table.sanitize (c, this)); michael@0: } michael@0: michael@0: protected: michael@0: Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ michael@0: FixedVersion version; /* Version of the TTC Header (1.0), michael@0: * 0x00010000 */ michael@0: LongOffsetLongArrayOf michael@0: table; /* Array of offsets to the OffsetTable for each font michael@0: * from the beginning of the file */ michael@0: public: michael@0: DEFINE_SIZE_ARRAY (12, table); michael@0: }; michael@0: michael@0: struct TTCHeader michael@0: { michael@0: friend struct OpenTypeFontFile; michael@0: michael@0: private: michael@0: michael@0: inline unsigned int get_face_count (void) const michael@0: { michael@0: switch (u.header.version.major) { michael@0: case 2: /* version 2 is compatible with version 1 */ michael@0: case 1: return u.version1.get_face_count (); michael@0: default:return 0; michael@0: } michael@0: } michael@0: inline const OpenTypeFontFace& get_face (unsigned int i) const michael@0: { michael@0: switch (u.header.version.major) { michael@0: case 2: /* version 2 is compatible with version 1 */ michael@0: case 1: return u.version1.get_face (i); michael@0: default:return Null(OpenTypeFontFace); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!u.header.version.sanitize (c))) return TRACE_RETURN (false); michael@0: switch (u.header.version.major) { michael@0: case 2: /* version 2 is compatible with version 1 */ michael@0: case 1: return TRACE_RETURN (u.version1.sanitize (c)); michael@0: default:return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: struct { michael@0: Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ michael@0: FixedVersion version; /* Version of the TTC Header (1.0 or 2.0), michael@0: * 0x00010000 or 0x00020000 */ michael@0: } header; michael@0: TTCHeaderVersion1 version1; michael@0: } u; michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * OpenType Font File michael@0: */ michael@0: michael@0: struct OpenTypeFontFile michael@0: { michael@0: static const hb_tag_t CFFTag = HB_TAG ('O','T','T','O'); /* OpenType with Postscript outlines */ michael@0: static const hb_tag_t TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ); /* OpenType with TrueType outlines */ michael@0: static const hb_tag_t TTCTag = HB_TAG ('t','t','c','f'); /* TrueType Collection */ michael@0: static const hb_tag_t TrueTag = HB_TAG ('t','r','u','e'); /* Obsolete Apple TrueType */ michael@0: static const hb_tag_t Typ1Tag = HB_TAG ('t','y','p','1'); /* Obsolete Apple Type1 font in SFNT container */ michael@0: michael@0: inline hb_tag_t get_tag (void) const { return u.tag; } michael@0: michael@0: inline unsigned int get_face_count (void) const michael@0: { michael@0: switch (u.tag) { michael@0: case CFFTag: /* All the non-collection tags */ michael@0: case TrueTag: michael@0: case Typ1Tag: michael@0: case TrueTypeTag: return 1; michael@0: case TTCTag: return u.ttcHeader.get_face_count (); michael@0: default: return 0; michael@0: } michael@0: } michael@0: inline const OpenTypeFontFace& get_face (unsigned int i) const michael@0: { michael@0: switch (u.tag) { michael@0: /* Note: for non-collection SFNT data we ignore index. This is because michael@0: * Apple dfont container is a container of SFNT's. So each SFNT is a michael@0: * non-TTC, but the index is more than zero. */ michael@0: case CFFTag: /* All the non-collection tags */ michael@0: case TrueTag: michael@0: case Typ1Tag: michael@0: case TrueTypeTag: return u.fontFace; michael@0: case TTCTag: return u.ttcHeader.get_face (i); michael@0: default: return Null(OpenTypeFontFace); michael@0: } michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!u.tag.sanitize (c))) return TRACE_RETURN (false); michael@0: switch (u.tag) { michael@0: case CFFTag: /* All the non-collection tags */ michael@0: case TrueTag: michael@0: case Typ1Tag: michael@0: case TrueTypeTag: return TRACE_RETURN (u.fontFace.sanitize (c)); michael@0: case TTCTag: return TRACE_RETURN (u.ttcHeader.sanitize (c)); michael@0: default: return TRACE_RETURN (true); michael@0: } michael@0: } michael@0: michael@0: protected: michael@0: union { michael@0: Tag tag; /* 4-byte identifier. */ michael@0: OpenTypeFontFace fontFace; michael@0: TTCHeader ttcHeader; michael@0: } u; michael@0: public: michael@0: DEFINE_SIZE_UNION (4, tag); michael@0: }; michael@0: michael@0: michael@0: } /* namespace OT */ michael@0: michael@0: michael@0: #endif /* HB_OPEN_FILE_PRIVATE_HH */