hb-open-file-private.hh 10.4 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009  Red Hat, Inc.
3
 * Copyright © 2012  Google, Inc.
4
 *
B
Behdad Esfahbod 已提交
5
 *  This is part of HarfBuzz, a text shaping library.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
26
 * Google Author(s): Behdad Esfahbod
27 28
 */

29 30
#ifndef HB_OPEN_FILE_PRIVATE_HH
#define HB_OPEN_FILE_PRIVATE_HH
31

32
#include "hb-open-type-private.hh"
33
#include "hb-ot-head-table.hh"
34

B
Behdad Esfahbod 已提交
35

36 37
namespace OT {

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

/*
 *
 * The OpenType Font File
 *
 */


/*
 * Organization of an OpenType Font
 */

struct OpenTypeFontFile;
struct OffsetTable;
struct TTCHeader;

B
Behdad Esfahbod 已提交
54

55
typedef struct TableRecord
56
{
57
  int cmp (Tag t) const
58
  { return -t.cmp (tag); }
59

B
Behdad Esfahbod 已提交
60 61 62 63 64 65 66
  static int cmp (const void *pa, const void *pb)
  {
    const TableRecord *a = (const TableRecord *) pa;
    const TableRecord *b = (const TableRecord *) pb;
    return b->cmp (a->tag);
  }

B
Behdad Esfahbod 已提交
67 68
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
69
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
70
    return_trace (c->check_struct (this));
B
Behdad Esfahbod 已提交
71 72
  }

73 74
  Tag		tag;		/* 4-byte identifier. */
  CheckSum	checkSum;	/* CheckSum for this table. */
B
Behdad Esfahbod 已提交
75
  Offset32	offset;		/* Offset from beginning of TrueType font
76
				 * file. */
B
Behdad Esfahbod 已提交
77
  HBUINT32	length;		/* Length of this table. */
B
Behdad Esfahbod 已提交
78 79
  public:
  DEFINE_SIZE_STATIC (16);
80 81 82 83 84 85
} OpenTypeTable;

typedef struct OffsetTable
{
  friend struct OpenTypeFontFile;

86
  inline unsigned int get_table_count (void) const
B
Behdad Esfahbod 已提交
87
  { return tables.len; }
88
  inline const TableRecord& get_table (unsigned int i) const
89
  {
90
    return tables[i];
91
  }
B
Behdad Esfahbod 已提交
92
  inline unsigned int get_table_tags (unsigned int  start_offset,
B
Behdad Esfahbod 已提交
93 94 95 96 97
				      unsigned int *table_count, /* IN/OUT */
				      hb_tag_t     *table_tags /* OUT */) const
  {
    if (table_count)
    {
B
Behdad Esfahbod 已提交
98
      if (start_offset >= tables.len)
B
Behdad Esfahbod 已提交
99 100
        *table_count = 0;
      else
101
        *table_count = MIN<unsigned int> (*table_count, tables.len - start_offset);
B
Behdad Esfahbod 已提交
102

B
Behdad Esfahbod 已提交
103
      const TableRecord *sub_tables = tables.array + start_offset;
B
Behdad Esfahbod 已提交
104 105 106 107
      unsigned int count = *table_count;
      for (unsigned int i = 0; i < count; i++)
	table_tags[i] = sub_tables[i].tag;
    }
B
Behdad Esfahbod 已提交
108
    return tables.len;
B
Behdad Esfahbod 已提交
109
  }
110 111
  inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
  {
B
Behdad Esfahbod 已提交
112 113
    Tag t;
    t.set (tag);
114 115 116 117 118 119
    /* Linear-search for small tables to work around fonts with unsorted
     * table list. */
    int i = tables.len < 64 ? tables.lsearch (t) : tables.bsearch (t);
    if (table_index)
      *table_index = i == -1 ? Index::NOT_FOUND_INDEX : (unsigned int) i;
    return i != -1;
120
  }
121
  inline const TableRecord& get_table_by_tag (hb_tag_t tag) const
122 123 124 125 126
  {
    unsigned int table_index;
    find_table_index (tag, &table_index);
    return get_table (table_index);
  }
B
Behdad Esfahbod 已提交
127 128

  public:
B
Behdad Esfahbod 已提交
129 130 131 132 133 134 135 136

  inline bool serialize (hb_serialize_context_t *c,
			 hb_tag_t sfnt_tag,
			 Supplier<hb_tag_t> &tags,
			 Supplier<hb_blob_t *> &blobs,
			 unsigned int table_count)
  {
    TRACE_SERIALIZE (this);
137
    /* Alloc 12 for the OTHeader. */
B
Behdad Esfahbod 已提交
138
    if (unlikely (!c->extend_min (*this))) return_trace (false);
139
    /* Write sfntVersion (bytes 0..3). */
B
Behdad Esfahbod 已提交
140
    sfnt_version.set (sfnt_tag);
141
    /* Take space for numTables, searchRange, entrySelector, RangeShift
142
     * and the TableRecords themselves.  */
B
Behdad Esfahbod 已提交
143
    if (unlikely (!tables.serialize (c, table_count))) return_trace (false);
B
Behdad Esfahbod 已提交
144

145 146 147
    const char *dir_end = (const char *) c->head;
    HBUINT32 *checksum_adjustment = nullptr;

148
    /* Write OffsetTables, alloc for and write actual table blobs. */
B
Behdad Esfahbod 已提交
149 150 151 152
    for (unsigned int i = 0; i < table_count; i++)
    {
      TableRecord &rec = tables.array[i];
      hb_blob_t *blob = blobs[i];
B
Behdad Esfahbod 已提交
153
      rec.tag.set (tags[i]);
B
Behdad Esfahbod 已提交
154 155
      rec.length.set (hb_blob_get_length (blob));
      rec.offset.serialize (c, this);
156 157 158 159 160 161 162

      /* Allocate room for the table and copy it. */
      char *start = (char *) c->allocate_size<void> (rec.length);
      if (unlikely (!start)) {return false;}

      memcpy (start, hb_blob_get_data (blob, nullptr), rec.length);

163
      /* 4-byte allignment. */
B
Behdad Esfahbod 已提交
164
      if (rec.length % 4)
165
	c->allocate_size<void> (4 - rec.length % 4);
166
      const char *end = (const char *) c->head;
167 168 169 170 171 172 173 174

      if (tags[i] == HB_OT_TAG_head && end - start >= head::static_size)
      {
	head *h = (head *) start;
	checksum_adjustment = &h->checkSumAdjustment;
	checksum_adjustment->set (0);
      }

175
      rec.checkSum.set_for_data (start, end - start);
B
Behdad Esfahbod 已提交
176
    }
177 178
    tags += table_count;
    blobs += table_count;
B
Behdad Esfahbod 已提交
179

B
Behdad Esfahbod 已提交
180
    tables.qsort ();
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    if (checksum_adjustment)
    {
      CheckSum checksum;

      /* The following line is a slower version of the following block. */
      //checksum.set_for_data (this, (const char *) c->head - (const char *) this);
      checksum.set_for_data (this, dir_end - (const char *) this);
      for (unsigned int i = 0; i < table_count; i++)
      {
	TableRecord &rec = tables.array[i];
	checksum.set (checksum + rec.checkSum);
      }

      checksum_adjustment->set (0xB1B0AFBAu - checksum);
    }

B
Behdad Esfahbod 已提交
198 199 200
    return_trace (true);
  }

B
Behdad Esfahbod 已提交
201 202
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
203
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
204
    return_trace (c->check_struct (this) && tables.sanitize (c));
B
Behdad Esfahbod 已提交
205 206
  }

207
  protected:
208
  Tag		sfnt_version;	/* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
B
Behdad Esfahbod 已提交
209 210
  BinSearchArrayOf<TableRecord>
		tables;
B
Behdad Esfahbod 已提交
211
  public:
212
  DEFINE_SIZE_ARRAY (12, tables);
213
} OpenTypeFontFace;
B
Behdad Esfahbod 已提交
214

215 216 217 218 219

/*
 * TrueType Collections
 */

220
struct TTCHeaderVersion1
221
{
222
  friend struct TTCHeader;
223

B
Behdad Esfahbod 已提交
224
  inline unsigned int get_face_count (void) const { return table.len; }
B
Behdad Esfahbod 已提交
225
  inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; }
B
Behdad Esfahbod 已提交
226

B
Behdad Esfahbod 已提交
227 228
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
229
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
230
    return_trace (table.sanitize (c, this));
B
Behdad Esfahbod 已提交
231 232
  }

233
  protected:
234
  Tag		ttcTag;		/* TrueType Collection ID string: 'ttcf' */
B
Behdad Esfahbod 已提交
235
  FixedVersion<>version;	/* Version of the TTC Header (1.0),
236
				 * 0x00010000u */
B
Behdad Esfahbod 已提交
237
  ArrayOf<LOffsetTo<OffsetTable>, HBUINT32>
238 239
		table;		/* Array of offsets to the OffsetTable for each font
				 * from the beginning of the file */
240
  public:
241
  DEFINE_SIZE_ARRAY (12, table);
242
};
243 244 245 246 247 248 249 250 251

struct TTCHeader
{
  friend struct OpenTypeFontFile;

  private:

  inline unsigned int get_face_count (void) const
  {
B
Behdad Esfahbod 已提交
252
    switch (u.header.version.major) {
253
    case 2: /* version 2 is compatible with version 1 */
B
Behdad Esfahbod 已提交
254
    case 1: return u.version1.get_face_count ();
255 256 257 258 259
    default:return 0;
    }
  }
  inline const OpenTypeFontFace& get_face (unsigned int i) const
  {
B
Behdad Esfahbod 已提交
260
    switch (u.header.version.major) {
261
    case 2: /* version 2 is compatible with version 1 */
B
Behdad Esfahbod 已提交
262
    case 1: return u.version1.get_face (i);
B
Behdad Esfahbod 已提交
263
    default:return Null(OpenTypeFontFace);
264 265 266
    }
  }

B
Behdad Esfahbod 已提交
267 268
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
269
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
270
    if (unlikely (!u.header.version.sanitize (c))) return_trace (false);
B
Behdad Esfahbod 已提交
271
    switch (u.header.version.major) {
272
    case 2: /* version 2 is compatible with version 1 */
B
Behdad Esfahbod 已提交
273 274
    case 1: return_trace (u.version1.sanitize (c));
    default:return_trace (true);
275 276 277
    }
  }

278
  protected:
279 280 281
  union {
  struct {
  Tag		ttcTag;		/* TrueType Collection ID string: 'ttcf' */
B
Behdad Esfahbod 已提交
282
  FixedVersion<>version;	/* Version of the TTC Header (1.0 or 2.0),
283
				 * 0x00010000u or 0x00020000u */
284
  }			header;
B
Behdad Esfahbod 已提交
285
  TTCHeaderVersion1	version1;
286 287
  } u;
};
288 289 290 291 292 293 294 295


/*
 * OpenType Font File
 */

struct OpenTypeFontFile
{
296 297
  static const hb_tag_t tableTag	= HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */

B
Behdad Esfahbod 已提交
298 299 300
  static const hb_tag_t CFFTag		= HB_TAG ('O','T','T','O'); /* OpenType with Postscript outlines */
  static const hb_tag_t TrueTypeTag	= HB_TAG ( 0 , 1 , 0 , 0 ); /* OpenType with TrueType outlines */
  static const hb_tag_t TTCTag		= HB_TAG ('t','t','c','f'); /* TrueType Collection */
B
Behdad Esfahbod 已提交
301 302
  static const hb_tag_t TrueTag		= HB_TAG ('t','r','u','e'); /* Obsolete Apple TrueType */
  static const hb_tag_t Typ1Tag		= HB_TAG ('t','y','p','1'); /* Obsolete Apple Type1 font in SFNT container */
303

B
Behdad Esfahbod 已提交
304 305
  inline hb_tag_t get_tag (void) const { return u.tag; }

B
Behdad Esfahbod 已提交
306
  inline unsigned int get_face_count (void) const
307
  {
B
Behdad Esfahbod 已提交
308 309
    switch (u.tag) {
    case CFFTag:	/* All the non-collection tags */
310 311
    case TrueTag:
    case Typ1Tag:
B
Behdad Esfahbod 已提交
312
    case TrueTypeTag:	return 1;
B
Behdad Esfahbod 已提交
313
    case TTCTag:	return u.ttcHeader.get_face_count ();
B
Behdad Esfahbod 已提交
314
    default:		return 0;
315 316
    }
  }
B
Behdad Esfahbod 已提交
317
  inline const OpenTypeFontFace& get_face (unsigned int i) const
318
  {
B
Behdad Esfahbod 已提交
319
    switch (u.tag) {
320 321 322
    /* Note: for non-collection SFNT data we ignore index.  This is because
     * Apple dfont container is a container of SFNT's.  So each SFNT is a
     * non-TTC, but the index is more than zero. */
B
Behdad Esfahbod 已提交
323
    case CFFTag:	/* All the non-collection tags */
324 325
    case TrueTag:
    case Typ1Tag:
B
Behdad Esfahbod 已提交
326 327
    case TrueTypeTag:	return u.fontFace;
    case TTCTag:	return u.ttcHeader.get_face (i);
B
Behdad Esfahbod 已提交
328
    default:		return Null(OpenTypeFontFace);
329 330 331
    }
  }

B
Behdad Esfahbod 已提交
332 333 334 335 336 337 338 339 340 341 342 343
  inline bool serialize_single (hb_serialize_context_t *c,
				hb_tag_t sfnt_tag,
			        Supplier<hb_tag_t> &tags,
			        Supplier<hb_blob_t *> &blobs,
			        unsigned int table_count)
  {
    TRACE_SERIALIZE (this);
    assert (sfnt_tag != TTCTag);
    if (unlikely (!c->extend_min (*this))) return_trace (false);
    return_trace (u.fontFace.serialize (c, sfnt_tag, tags, blobs, table_count));
  }

B
Behdad Esfahbod 已提交
344 345
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
B
Behdad Esfahbod 已提交
346
    TRACE_SANITIZE (this);
B
Behdad Esfahbod 已提交
347
    if (unlikely (!u.tag.sanitize (c))) return_trace (false);
B
Behdad Esfahbod 已提交
348 349
    switch (u.tag) {
    case CFFTag:	/* All the non-collection tags */
350 351
    case TrueTag:
    case Typ1Tag:
B
Behdad Esfahbod 已提交
352 353 354
    case TrueTypeTag:	return_trace (u.fontFace.sanitize (c));
    case TTCTag:	return_trace (u.ttcHeader.sanitize (c));
    default:		return_trace (true);
B
Behdad Esfahbod 已提交
355 356 357
    }
  }

358
  protected:
B
Behdad Esfahbod 已提交
359 360
  union {
  Tag			tag;		/* 4-byte identifier. */
B
Behdad Esfahbod 已提交
361 362
  OpenTypeFontFace	fontFace;
  TTCHeader		ttcHeader;
B
Behdad Esfahbod 已提交
363
  } u;
B
Behdad Esfahbod 已提交
364 365
  public:
  DEFINE_SIZE_UNION (4, tag);
366 367 368
};


B
Behdad Esfahbod 已提交
369
} /* namespace OT */
370

B
Behdad Esfahbod 已提交
371

372
#endif /* HB_OPEN_FILE_PRIVATE_HH */