Add BinSearchArrayOf<>

This commit is contained in:
Behdad Esfahbod 2017-10-31 20:05:37 -06:00
parent aca378f51e
commit b0e33da02d
3 changed files with 44 additions and 16 deletions

View File

@ -73,10 +73,9 @@ typedef struct OffsetTable
friend struct OpenTypeFontFile; friend struct OpenTypeFontFile;
inline unsigned int get_table_count (void) const inline unsigned int get_table_count (void) const
{ return numTables; } { return tables.len; }
inline const TableRecord& get_table (unsigned int i) const inline const TableRecord& get_table (unsigned int i) const
{ {
if (unlikely (i >= numTables)) return Null(TableRecord);
return tables[i]; return tables[i];
} }
inline unsigned int get_table_tags (unsigned int start_offset, inline unsigned int get_table_tags (unsigned int start_offset,
@ -85,26 +84,27 @@ typedef struct OffsetTable
{ {
if (table_count) if (table_count)
{ {
if (start_offset >= numTables) if (start_offset >= tables.len)
*table_count = 0; *table_count = 0;
else else
*table_count = MIN (*table_count, numTables - start_offset); *table_count = MIN (*table_count, tables.len - start_offset);
const TableRecord *sub_tables = tables + start_offset; const TableRecord *sub_tables = tables.array + start_offset;
unsigned int count = *table_count; unsigned int count = *table_count;
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
table_tags[i] = sub_tables[i].tag; table_tags[i] = sub_tables[i].tag;
} }
return numTables; return tables.len;
} }
inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const inline bool find_table_index (hb_tag_t tag, unsigned int *table_index) const
{ {
Tag t; Tag t;
t.set (tag); t.set (tag);
unsigned int count = numTables; unsigned int count = tables.len;
/* TODO bsearch() */
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
{ {
if (t == tables[i].tag) if (t == tables.array[i].tag)
{ {
if (table_index) *table_index = i; if (table_index) *table_index = i;
return true; return true;
@ -124,16 +124,13 @@ typedef struct OffsetTable
inline bool sanitize (hb_sanitize_context_t *c) const inline bool sanitize (hb_sanitize_context_t *c) const
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (c->check_struct (this) && c->check_array (tables, TableRecord::static_size, numTables)); return_trace (c->check_struct (this) && tables.sanitize (c));
} }
protected: protected:
Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */ Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
USHORT numTables; /* Number of tables. */ BinSearchArrayOf<TableRecord>
USHORT searchRangeZ; /* (Maximum power of 2 <= numTables) x 16 */ tables;
USHORT entrySelectorZ; /* Log2(maximum power of 2 <= numTables). */
USHORT rangeShiftZ; /* NumTables x 16-searchRange. */
TableRecord tables[VAR]; /* TableRecord entries. numTables items */
public: public:
DEFINE_SIZE_ARRAY (12, tables); DEFINE_SIZE_ARRAY (12, tables);
} OpenTypeFontFace; } OpenTypeFontFace;

View File

@ -1045,7 +1045,9 @@ struct HeadlessArrayOf
}; };
/* An array with sorted elements. Supports binary searching. */ /*
* An array with sorted elements. Supports binary searching.
*/
template <typename Type, typename LenType=USHORT> template <typename Type, typename LenType=USHORT>
struct SortedArrayOf : ArrayOf<Type, LenType> struct SortedArrayOf : ArrayOf<Type, LenType>
{ {
@ -1070,6 +1072,35 @@ struct SortedArrayOf : ArrayOf<Type, LenType>
} }
}; };
/*
* Binary-search arrays
*/
struct BinSearchHeader
{
inline operator uint32_t (void) const { return len; }
inline bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (c->check_struct (this));
}
protected:
USHORT len;
USHORT searchRangeZ;
USHORT entrySelectorZ;
USHORT rangeShiftZ;
public:
DEFINE_SIZE_STATIC (8);
};
template <typename Type>
struct BinSearchArrayOf : SortedArrayOf<Type, BinSearchHeader>
{
};
/* Lazy struct and blob loaders. */ /* Lazy struct and blob loaders. */

View File

@ -37,8 +37,8 @@
#include "hb-ot-hhea-table.hh" #include "hb-ot-hhea-table.hh"
#include "hb-ot-hmtx-table.hh" #include "hb-ot-hmtx-table.hh"
#include "hb-ot-os2-table.hh" #include "hb-ot-os2-table.hh"
#include "hb-ot-var-hvar-table.hh"
#include "hb-ot-post-table.hh" #include "hb-ot-post-table.hh"
#include "hb-ot-var-hvar-table.hh"
struct hb_ot_face_metrics_accelerator_t struct hb_ot_face_metrics_accelerator_t