harfbuzz/src/hb-open-file-private.hh

375 lines
10 KiB
C++
Raw Normal View History

/*
2011-04-21 23:14:28 +02:00
* Copyright © 2007,2008,2009 Red Hat, Inc.
* Copyright © 2012 Google, Inc.
*
2010-04-22 06:11:43 +02:00
* This is part of HarfBuzz, a text shaping library.
*
* 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
* Google Author(s): Behdad Esfahbod
*/
#ifndef HB_OPEN_FILE_PRIVATE_HH
#define HB_OPEN_FILE_PRIVATE_HH
#include "hb-open-type-private.hh"
#include "hb-ot-head-table.hh"
2010-07-23 21:11:18 +02:00
namespace OT {
/*
*
* The OpenType Font File
*
*/
/*
* Organization of an OpenType Font
*/
struct OpenTypeFontFile;
struct OffsetTable;
struct TTCHeader;
2010-05-10 22:38:32 +02:00
typedef struct TableRecord
{
int cmp (Tag t) const
{ return -t.cmp (tag); }
2018-02-08 04:13:10 +01:00
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);
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
2012-11-23 21:32:14 +01:00
TRACE_SANITIZE (this);
return_trace (c->check_struct (this));
2009-08-04 21:07:24 +02:00
}
Tag tag; /* 4-byte identifier. */
CheckSum checkSum; /* CheckSum for this table. */
2018-02-08 04:13:10 +01:00
Offset32 offset; /* Offset from beginning of TrueType font
* file. */
2018-02-08 00:12:55 +01:00
HBUINT32 length; /* Length of this table. */
2010-05-10 22:38:32 +02:00
public:
DEFINE_SIZE_STATIC (16);
} OpenTypeTable;
typedef struct OffsetTable
{
friend struct OpenTypeFontFile;
inline unsigned int get_table_count (void) const
2017-11-01 03:05:37 +01:00
{ return tables.len; }
inline const TableRecord& get_table (unsigned int i) const
{
return tables[i];
}
2018-02-08 00:12:55 +01:00
inline unsigned int get_table_tags (unsigned int start_offset,
unsigned int *table_count, /* IN/OUT */
hb_tag_t *table_tags /* OUT */) const
{
if (table_count)
{
2017-11-01 03:05:37 +01:00
if (start_offset >= tables.len)
*table_count = 0;
else
*table_count = MIN<unsigned int> (*table_count, tables.len - start_offset);
2018-05-09 01:56:11 +02:00
const TableRecord *sub_tables = tables.arrayZ + start_offset;
unsigned int count = *table_count;
for (unsigned int i = 0; i < count; i++)
table_tags[i] = sub_tables[i].tag;
}
2017-11-01 03:05:37 +01:00
return tables.len;
}
inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
{
2010-04-21 06:14:12 +02:00
Tag t;
t.set (tag);
/* 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;
}
inline const TableRecord& get_table_by_tag (hb_tag_t tag) const
{
unsigned int table_index;
find_table_index (tag, &table_index);
return get_table (table_index);
}
2009-08-04 21:07:24 +02:00
public:
2018-02-08 04:13:10 +01:00
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);
/* Alloc 12 for the OTHeader. */
2018-02-08 04:13:10 +01:00
if (unlikely (!c->extend_min (*this))) return_trace (false);
/* Write sfntVersion (bytes 0..3). */
2018-02-08 04:13:10 +01:00
sfnt_version.set (sfnt_tag);
/* Take space for numTables, searchRange, entrySelector, RangeShift
* and the TableRecords themselves. */
2018-02-08 04:13:10 +01:00
if (unlikely (!tables.serialize (c, table_count))) return_trace (false);
2018-02-08 04:49:01 +01:00
const char *dir_end = (const char *) c->head;
HBUINT32 *checksum_adjustment = nullptr;
/* Write OffsetTables, alloc for and write actual table blobs. */
2018-02-08 04:13:10 +01:00
for (unsigned int i = 0; i < table_count; i++)
{
2018-05-09 01:56:11 +02:00
TableRecord &rec = tables.arrayZ[i];
2018-02-08 04:13:10 +01:00
hb_blob_t *blob = blobs[i];
2018-02-08 04:49:01 +01:00
rec.tag.set (tags[i]);
2018-02-08 04:13:10 +01:00
rec.length.set (hb_blob_get_length (blob));
rec.offset.serialize (c, this);
/* 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);
/* 4-byte allignment. */
2018-02-08 04:13:10 +01:00
if (rec.length % 4)
c->allocate_size<void> (4 - rec.length % 4);
const char *end = (const char *) c->head;
if (tags[i] == HB_OT_TAG_head && end - start >= head::static_size)
{
head *h = (head *) start;
checksum_adjustment = &h->checkSumAdjustment;
checksum_adjustment->set (0);
}
rec.checkSum.set_for_data (start, end - start);
2018-02-08 04:13:10 +01:00
}
tags += table_count;
blobs += table_count;
2018-02-08 04:49:01 +01:00
2018-02-08 04:13:10 +01:00
tables.qsort ();
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++)
{
2018-05-09 01:56:11 +02:00
TableRecord &rec = tables.arrayZ[i];
checksum.set (checksum + rec.checkSum);
}
checksum_adjustment->set (0xB1B0AFBAu - checksum);
}
2018-02-08 04:13:10 +01:00
return_trace (true);
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
2012-11-23 21:32:14 +01:00
TRACE_SANITIZE (this);
2017-11-01 03:05:37 +01:00
return_trace (c->check_struct (this) && tables.sanitize (c));
2009-08-04 21:07:24 +02:00
}
protected:
Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
2017-11-01 03:05:37 +01:00
BinSearchArrayOf<TableRecord>
tables;
2010-05-10 22:38:32 +02:00
public:
DEFINE_SIZE_ARRAY (12, tables);
} OpenTypeFontFace;
2010-05-10 22:38:32 +02:00
/*
* TrueType Collections
*/
struct TTCHeaderVersion1
{
friend struct TTCHeader;
inline unsigned int get_face_count (void) const { return table.len; }
2010-04-22 20:11:33 +02:00
inline const OpenTypeFontFace& get_face (unsigned int i) const { return this+table[i]; }
2009-08-04 21:07:24 +02:00
inline bool sanitize (hb_sanitize_context_t *c) const
{
2012-11-23 21:32:14 +01:00
TRACE_SANITIZE (this);
return_trace (table.sanitize (c, this));
2009-08-04 21:07:24 +02:00
}
protected:
Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
2016-02-22 03:44:45 +01:00
FixedVersion<>version; /* Version of the TTC Header (1.0),
* 0x00010000u */
LArrayOf<LOffsetTo<OffsetTable> >
table; /* Array of offsets to the OffsetTable for each font
* from the beginning of the file */
public:
DEFINE_SIZE_ARRAY (12, table);
};
struct TTCHeader
{
friend struct OpenTypeFontFile;
private:
inline unsigned int get_face_count (void) const
{
switch (u.header.version.major) {
case 2: /* version 2 is compatible with version 1 */
2010-05-11 01:45:41 +02:00
case 1: return u.version1.get_face_count ();
default:return 0;
}
}
inline const OpenTypeFontFace& get_face (unsigned int i) const
{
switch (u.header.version.major) {
case 2: /* version 2 is compatible with version 1 */
2010-05-11 01:45:41 +02:00
case 1: return u.version1.get_face (i);
2010-04-23 19:32:03 +02:00
default:return Null(OpenTypeFontFace);
}
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
2012-11-23 21:32:14 +01:00
TRACE_SANITIZE (this);
if (unlikely (!u.header.version.sanitize (c))) return_trace (false);
switch (u.header.version.major) {
case 2: /* version 2 is compatible with version 1 */
case 1: return_trace (u.version1.sanitize (c));
default:return_trace (true);
}
}
protected:
union {
struct {
Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
2016-02-22 03:44:45 +01:00
FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0),
* 0x00010000u or 0x00020000u */
} header;
2010-05-11 01:45:41 +02:00
TTCHeaderVersion1 version1;
} u;
};
/*
* OpenType Font File
*/
struct OpenTypeFontFile
{
static const hb_tag_t tableTag = HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */
enum {
CFFTag = HB_TAG ('O','T','T','O'), /* OpenType with Postscript outlines */
TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ), /* OpenType with TrueType outlines */
TTCTag = HB_TAG ('t','t','c','f'), /* TrueType Collection */
TrueTag = HB_TAG ('t','r','u','e'), /* Obsolete Apple TrueType */
Typ1Tag = HB_TAG ('t','y','p','1') /* Obsolete Apple Type1 font in SFNT container */
};
2010-04-23 19:32:03 +02:00
inline hb_tag_t get_tag (void) const { return u.tag; }
inline unsigned int get_face_count (void) const
{
2010-04-23 19:32:03 +02:00
switch (u.tag) {
case CFFTag: /* All the non-collection tags */
case TrueTag:
case Typ1Tag:
2010-04-23 19:32:03 +02:00
case TrueTypeTag: return 1;
2010-05-11 01:45:41 +02:00
case TTCTag: return u.ttcHeader.get_face_count ();
2010-04-23 19:32:03 +02:00
default: return 0;
}
}
inline const OpenTypeFontFace& get_face (unsigned int i) const
{
2010-04-23 19:32:03 +02:00
switch (u.tag) {
/* 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. */
2010-04-23 19:32:03 +02:00
case CFFTag: /* All the non-collection tags */
case TrueTag:
case Typ1Tag:
2010-05-11 01:45:41 +02:00
case TrueTypeTag: return u.fontFace;
case TTCTag: return u.ttcHeader.get_face (i);
2010-04-23 19:32:03 +02:00
default: return Null(OpenTypeFontFace);
}
}
2018-02-08 04:13:10 +01:00
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));
}
inline bool sanitize (hb_sanitize_context_t *c) const
{
2012-11-23 21:32:14 +01:00
TRACE_SANITIZE (this);
if (unlikely (!u.tag.sanitize (c))) return_trace (false);
2010-04-23 19:32:03 +02:00
switch (u.tag) {
case CFFTag: /* All the non-collection tags */
case TrueTag:
case Typ1Tag:
case TrueTypeTag: return_trace (u.fontFace.sanitize (c));
case TTCTag: return_trace (u.ttcHeader.sanitize (c));
default: return_trace (true);
2009-08-04 21:07:24 +02:00
}
}
protected:
2010-04-23 19:32:03 +02:00
union {
Tag tag; /* 4-byte identifier. */
2010-05-11 01:45:41 +02:00
OpenTypeFontFace fontFace;
TTCHeader ttcHeader;
2010-04-23 19:32:03 +02:00
} u;
2010-05-11 01:45:41 +02:00
public:
DEFINE_SIZE_UNION (4, tag);
};
2012-11-17 03:49:54 +01:00
} /* namespace OT */
2010-07-23 21:11:18 +02:00
#endif /* HB_OPEN_FILE_PRIVATE_HH */