mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-28 17:19:17 +01:00
[CBDT] Cap strikes considered instead of indexing them (#6606)
#6500 made the CBDT accelerator build a per-face (ppem, glyph range) -> record index at load time, with a strike set/map, a sorted vector, a walk_ppems fallback and an HB_CBDT_MAX_INDEX_ENTRIES cap, to avoid walking a huge strike array on every glyph query. Real fonts have a handful of strikes, so that machinery costs every CBDT face memory and load time for the benefit of pathological fonts only. Remove it and restore the simple CBLC::choose_strike() and BitmapSizeTable::find_table() lookup path, but only consider the first HB_CBLC_MAX_STRIKES (256) strikes when choosing one; later ones are ignored. ppem is 8-bit, so this never drops a distinct size a real font could need. The sbix strike index, the CBDT subsetter's record map and its image-copy budget from #6500 are kept unchanged. Adds test_hb_ot_color_png_cbdt_max_strikes, which places the only usable strike at index 255 / 256 and checks it is found / ignored; it fails with the cap removed. Full meson test passes. Follow-up to #6500 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
85c9526477
commit
6df7f66f94
3 files changed
+93
-130
No files matched your search
+34
-123
@@ -704,10 +704,6 @@ struct IndexSubtableArray
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* The caller must bound i by the strike's sanitized numberOfIndexSubtables. */
|
||||
const IndexSubtableRecord &get_record (unsigned int i) const
|
||||
{ return indexSubtablesZ[i]; }
|
||||
|
||||
protected:
|
||||
UnsizedArrayOf<IndexSubtableRecord> indexSubtablesZ;
|
||||
};
|
||||
@@ -872,6 +868,36 @@ struct CBLC
|
||||
// Implemented in cc file as it depends on definition of CBDT.
|
||||
HB_INTERNAL bool subset (hb_subset_context_t *c) const;
|
||||
|
||||
protected:
|
||||
const BitmapSizeTable &choose_strike (hb_font_t *font) const
|
||||
{
|
||||
/* Strikes beyond HB_CBLC_MAX_STRIKES are ignored, such that a font
|
||||
* claiming a huge number of them cannot make every per-glyph query
|
||||
* walk them all. */
|
||||
unsigned count = hb_min (sizeTables.len, (unsigned) HB_CBLC_MAX_STRIKES);
|
||||
if (unlikely (!count))
|
||||
return Null (BitmapSizeTable);
|
||||
|
||||
unsigned int requested_ppem = hb_max (font->x_ppem, font->y_ppem);
|
||||
if (!requested_ppem)
|
||||
requested_ppem = 1<<30; /* Choose largest strike. */
|
||||
unsigned int best_i = 0;
|
||||
unsigned int best_ppem = hb_max (sizeTables[0].ppemX, sizeTables[0].ppemY);
|
||||
|
||||
for (unsigned int i = 1; i < count; i++)
|
||||
{
|
||||
unsigned int ppem = hb_max (sizeTables[i].ppemX, sizeTables[i].ppemY);
|
||||
if ((requested_ppem <= ppem && ppem < best_ppem) ||
|
||||
(requested_ppem > best_ppem && ppem > best_ppem))
|
||||
{
|
||||
best_i = i;
|
||||
best_ppem = ppem;
|
||||
}
|
||||
}
|
||||
|
||||
return sizeTables[best_i];
|
||||
}
|
||||
|
||||
protected:
|
||||
FixedVersion<> version;
|
||||
Array32Of<BitmapSizeTable> sizeTables;
|
||||
@@ -891,66 +917,6 @@ struct CBDT
|
||||
this->cbdt = hb_sanitize_context_t ().reference_table<CBDT> (face);
|
||||
|
||||
upem = hb_face_get_upem (face);
|
||||
|
||||
/* Index the strikes by ppem and their records by glyph once, such that
|
||||
* choose_strike() and find_table() don't walk the table's arrays on
|
||||
* every query. Both counts are 32-bit and only bounded by the table
|
||||
* length. Of the strikes with equal ppem the first one wins, and a
|
||||
* glyph belongs to the first record that covers it.
|
||||
*
|
||||
* The record index is capped at HB_CBDT_MAX_INDEX_ENTRIES: a strike
|
||||
* whose records would overflow it (or hit an allocation failure)
|
||||
* keeps the linear walk through walk_ppems, so the index stays
|
||||
* bounded no matter how many records the sanitizer accepted. */
|
||||
unsigned count = cblc->sizeTables.len;
|
||||
bool index_full = false;
|
||||
for (unsigned int i = 0; i < count; i++)
|
||||
{
|
||||
const BitmapSizeTable &strike = cblc->sizeTables[i];
|
||||
unsigned int ppem = hb_max (strike.ppemX, strike.ppemY);
|
||||
if (strike_ppems.has (ppem)) continue;
|
||||
strike_ppems.add (ppem);
|
||||
strike_for_ppem.set (ppem, i);
|
||||
|
||||
/* A strike with a zero ppem is never used by a query (get_extents()
|
||||
* and reference_png() reject it), so leave it out of the index; a
|
||||
* lookup for it finds nothing and returns no bitmap, as before. */
|
||||
if (!strike.ppemX || !strike.ppemY) continue;
|
||||
if (index_full) { walk_ppems.add (ppem); continue; }
|
||||
|
||||
const IndexSubtableArray &records = cblc+strike.indexSubtableArrayOffset;
|
||||
unsigned int num_records = strike.numberOfIndexSubtables;
|
||||
unsigned int start = glyph_ranges.length;
|
||||
hb_set_t glyphs; /* Glyphs not covered by an earlier record. */
|
||||
glyphs.add_range (0, 0xFFFFu);
|
||||
bool overflow = false;
|
||||
for (unsigned int j = 0; j < num_records && !glyphs.is_empty (); j++)
|
||||
{
|
||||
const IndexSubtableRecord &record = records.get_record (j);
|
||||
hb_codepoint_t first = record.firstGlyphIndex;
|
||||
hb_codepoint_t last = record.lastGlyphIndex;
|
||||
if (!glyphs.intersects (first, last)) continue;
|
||||
|
||||
for (hb_codepoint_t a = first - 1, b = first - 1;
|
||||
glyphs.next_range (&a, &b) && a <= last;)
|
||||
glyph_ranges.push (glyph_range_t {ppem << 16 | a,
|
||||
ppem << 16 | hb_min (b, last), j});
|
||||
glyphs.del_range (first, last);
|
||||
|
||||
if (glyph_ranges.length > HB_CBDT_MAX_INDEX_ENTRIES)
|
||||
{ overflow = true; break; }
|
||||
}
|
||||
/* glyphs.in_error() matters: a failed add_range() leaves it empty,
|
||||
* which would otherwise look like a fully-indexed strike with no
|
||||
* records and silently drop its bitmaps instead of walking. */
|
||||
if (unlikely (overflow || glyph_ranges.in_error () || glyphs.in_error ()))
|
||||
{
|
||||
glyph_ranges.shrink (start);
|
||||
walk_ppems.add (ppem);
|
||||
index_full = true;
|
||||
}
|
||||
}
|
||||
glyph_ranges.qsort ();
|
||||
}
|
||||
~accelerator_t ()
|
||||
{
|
||||
@@ -962,8 +928,8 @@ struct CBDT
|
||||
get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents, bool scale = true) const
|
||||
{
|
||||
const void *base;
|
||||
const BitmapSizeTable &strike = choose_strike (font);
|
||||
const IndexSubtableRecord *subtable_record = find_table (strike, glyph, &base);
|
||||
const BitmapSizeTable &strike = this->cblc->choose_strike (font);
|
||||
const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
|
||||
if (!subtable_record || !strike.ppemX || !strike.ppemY)
|
||||
return false;
|
||||
|
||||
@@ -1015,8 +981,8 @@ struct CBDT
|
||||
reference_png (hb_font_t *font, hb_codepoint_t glyph) const
|
||||
{
|
||||
const void *base;
|
||||
const BitmapSizeTable &strike = choose_strike (font);
|
||||
const IndexSubtableRecord *subtable_record = find_table (strike, glyph, &base);
|
||||
const BitmapSizeTable &strike = this->cblc->choose_strike (font);
|
||||
const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
|
||||
if (!subtable_record || !strike.ppemX || !strike.ppemY)
|
||||
return hb_blob_get_empty ();
|
||||
|
||||
@@ -1091,66 +1057,11 @@ struct CBDT
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
const BitmapSizeTable &choose_strike (hb_font_t *font) const
|
||||
{
|
||||
if (unlikely (strike_ppems.is_empty ()))
|
||||
return Null (BitmapSizeTable);
|
||||
|
||||
unsigned int requested_ppem = hb_max (font->x_ppem, font->y_ppem);
|
||||
if (!requested_ppem)
|
||||
requested_ppem = 1<<30; /* Choose largest strike. */
|
||||
|
||||
/* Smallest strike that is large enough, or otherwise the largest. */
|
||||
hb_codepoint_t ppem = requested_ppem - 1;
|
||||
if (!strike_ppems.next (&ppem))
|
||||
ppem = strike_ppems.get_max ();
|
||||
|
||||
return cblc->sizeTables[strike_for_ppem.get (ppem)];
|
||||
}
|
||||
|
||||
/* strike must come from choose_strike(), as the index is keyed by ppem. */
|
||||
const IndexSubtableRecord *
|
||||
find_table (const BitmapSizeTable &strike, hb_codepoint_t glyph, const void **out_base) const
|
||||
{
|
||||
unsigned int ppem = hb_max (strike.ppemX, strike.ppemY);
|
||||
if (unlikely (walk_ppems.has (ppem)))
|
||||
/* This strike's records overflowed the index; walk them. */
|
||||
return strike.find_table (glyph, cblc, out_base);
|
||||
|
||||
const IndexSubtableArray &records = cblc+strike.indexSubtableArrayOffset;
|
||||
*out_base = &records;
|
||||
if (unlikely (glyph > 0xFFFFu)) return nullptr;
|
||||
const glyph_range_t *range = glyph_ranges.bsearch (ppem << 16 | glyph);
|
||||
return range ? &records.get_record (range->record) : nullptr;
|
||||
}
|
||||
|
||||
struct glyph_range_t
|
||||
{
|
||||
hb_codepoint_t first, last; /* ppem << 16 | glyph, inclusive. */
|
||||
unsigned int record; /* Index into the strike's records. */
|
||||
|
||||
int cmp (hb_codepoint_t key) const
|
||||
{ return key < first ? -1 : key <= last ? 0 : +1; }
|
||||
|
||||
HB_INTERNAL static int cmp (const void *pa, const void *pb)
|
||||
{
|
||||
const glyph_range_t *a = (const glyph_range_t *) pa;
|
||||
const glyph_range_t *b = (const glyph_range_t *) pb;
|
||||
return a->first < b->first ? -1 : a->first > b->first ? +1 : 0;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
hb_blob_ptr_t<CBLC> cblc;
|
||||
hb_blob_ptr_t<CBDT> cbdt;
|
||||
|
||||
unsigned int upem;
|
||||
hb_set_t strike_ppems;
|
||||
hb_map_t strike_for_ppem;
|
||||
hb_set_t walk_ppems;
|
||||
hb_sorted_vector_t<glyph_range_t> glyph_ranges;
|
||||
};
|
||||
|
||||
bool sanitize (hb_sanitize_context_t *c) const
|
||||
|
||||
+5
-7
@@ -143,13 +143,11 @@
|
||||
#define HB_SUBSET_CBDT_MAX_COPY_FACTOR 64
|
||||
#endif
|
||||
|
||||
/* Entries the CBDT accelerator builds to answer find_table() by glyph
|
||||
* without walking a strike's IndexSubtableRecord array per query. The
|
||||
* record count is a 32-bit field bounded only by the table length; a strike
|
||||
* whose records would push the shared index past this keeps the linear
|
||||
* walk, so the index stays bounded whatever the font claims. */
|
||||
#ifndef HB_CBDT_MAX_INDEX_ENTRIES
|
||||
#define HB_CBDT_MAX_INDEX_ENTRIES 65536
|
||||
/* Strikes of a CBLC table considered when choosing one for a glyph query;
|
||||
* later ones are ignored. numSizes is a 32-bit field and every query walks
|
||||
* the strikes, while ppem is 8-bit and real fonts have a handful. */
|
||||
#ifndef HB_CBLC_MAX_STRIKES
|
||||
#define HB_CBLC_MAX_STRIKES 256
|
||||
#endif
|
||||
|
||||
/* Total bytes of sub-blobs (gradient color stops, clip outlines) in one
|
||||
|
||||
@@ -532,6 +532,59 @@ test_hb_ot_color_png_cbdt_records (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static unsigned int
|
||||
cbdt_png_length_with_strikes (unsigned int num_strikes)
|
||||
{
|
||||
/* num_strikes strikes, of which only the last has a ppem and a one-byte
|
||||
* image for glyph 0. The others have ppem 0 and are never chosen over
|
||||
* it, unless it is beyond the strikes considered. */
|
||||
unsigned int array_offset = 8 + 48 * num_strikes;
|
||||
unsigned int cblc_length = array_offset + 8 + 12;
|
||||
uint8_t *cblc = calloc (cblc_length, 1);
|
||||
uint8_t *p;
|
||||
static const uint8_t cbdt[4 + 8] = {0, 3, 0, 0, 0, 0, 0, 1};
|
||||
|
||||
cblc[1] = 3; /* version */
|
||||
cblc[4] = num_strikes >> 24; cblc[5] = num_strikes >> 16;
|
||||
cblc[6] = num_strikes >> 8; cblc[7] = num_strikes;
|
||||
p = cblc + array_offset - 48; /* Last BitmapSize */
|
||||
p[0] = array_offset >> 24; p[1] = array_offset >> 16;
|
||||
p[2] = array_offset >> 8; p[3] = array_offset; /* indexSubtableArrayOffset */
|
||||
p[11] = 1; /* numberOfIndexSubtables */
|
||||
p[44] = p[45] = p[46] = 32; p[47] = 1; /* ppemX, ppemY, bitDepth, flags */
|
||||
p = cblc + array_offset;
|
||||
p[7] = 8; /* IndexSubtableRecord: glyphs 0..0, offsetToSubtable */
|
||||
p += 8;
|
||||
p[1] = 3; p[3] = 19; p[7] = 4; p[11] = 8; /* IndexSubtableFormat3 */
|
||||
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
hb_blob_t *blob = hb_blob_create ((const char *) cblc, cblc_length,
|
||||
HB_MEMORY_MODE_WRITABLE, cblc, free);
|
||||
hb_face_builder_add_table (face, HB_TAG ('C','B','L','C'), blob);
|
||||
hb_blob_destroy (blob);
|
||||
blob = hb_blob_create ((const char *) cbdt, sizeof (cbdt),
|
||||
HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
hb_face_builder_add_table (face, HB_TAG ('C','B','D','T'), blob);
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
blob = hb_ot_color_glyph_reference_png (font, 0);
|
||||
unsigned int length = hb_blob_get_length (blob);
|
||||
hb_blob_destroy (blob);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
return length;
|
||||
}
|
||||
|
||||
static void
|
||||
test_hb_ot_color_png_cbdt_max_strikes (void)
|
||||
{
|
||||
/* Strikes beyond HB_CBLC_MAX_STRIKES (256) are ignored. */
|
||||
g_assert_cmpuint (cbdt_png_length_with_strikes (1), ==, 1);
|
||||
g_assert_cmpuint (cbdt_png_length_with_strikes (256), ==, 1);
|
||||
g_assert_cmpuint (cbdt_png_length_with_strikes (257), ==, 0);
|
||||
}
|
||||
|
||||
#if !defined(HB_NO_VAR) && !defined(HB_NO_PAINT)
|
||||
static void
|
||||
test_hb_ot_color_clip_extents_overflow (void)
|
||||
@@ -670,6 +723,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_hb_ot_color_has_data);
|
||||
hb_test_add (test_hb_ot_color_png);
|
||||
hb_test_add (test_hb_ot_color_png_cbdt_records);
|
||||
hb_test_add (test_hb_ot_color_png_cbdt_max_strikes);
|
||||
#if !defined(HB_NO_VAR) && !defined(HB_NO_PAINT)
|
||||
hb_test_add (test_hb_ot_color_clip_extents_overflow);
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user