mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-27 16:49:42 +01:00
[layout] Use 24-bit ClassDef format 3 values
ClassDef format 3 stores startGlyphID, glyphCount, and each class value as uint24. Use the medium integer type for the class array, select a serializable format based on both glyph and class ranges, and keep the subsetter size estimator in sync. Test parsing and querying a class value above 65535, and test that the serializer and size estimator select the matching format. Testing: meson test -C build Assisted-by: OpenAI Codex
This commit is contained in:
1 parent
3a5572ee33
commit
d2e218ac26
3 files changed
+81
-38
No files matched your search
+23
-21
@@ -194,6 +194,7 @@ struct class_def_size_estimator_t
|
||||
included_class_def_glyphs.clear();
|
||||
included_classes.clear();
|
||||
class_def_range_count = 0;
|
||||
class_max = 0;
|
||||
coverage_size_val = compute_coverage_size ();
|
||||
}
|
||||
|
||||
@@ -218,11 +219,15 @@ struct class_def_size_estimator_t
|
||||
}
|
||||
|
||||
if (klass)
|
||||
{
|
||||
class_def_range_count += num_ranges_per_class.get (klass);
|
||||
if (glyphs && klass > class_max)
|
||||
class_max = klass;
|
||||
}
|
||||
included_classes.add(klass);
|
||||
}
|
||||
|
||||
return class_def_uses_format1 () ? class_def_format1_size () : class_def_format2_size ();
|
||||
return class_def_size ();
|
||||
}
|
||||
|
||||
unsigned num_glyph_ranges (const hb_set_t &glyphs) const {
|
||||
@@ -274,30 +279,26 @@ struct class_def_size_estimator_t
|
||||
return OT::Layout::SmallTypes::size;
|
||||
}
|
||||
|
||||
unsigned class_def_format1_size () const
|
||||
unsigned class_def_size () const
|
||||
{
|
||||
unsigned glyph_size = glyph_id_size (included_class_def_glyphs);
|
||||
unsigned size = OT::HBUINT16::static_size + glyph_size + glyph_size;
|
||||
if (!included_class_def_glyphs.is_empty ())
|
||||
size += OT::HBUINT16::static_size * (included_class_def_glyphs.get_max () -
|
||||
included_class_def_glyphs.get_min () + 1);
|
||||
return size;
|
||||
}
|
||||
|
||||
unsigned class_def_format2_size () const
|
||||
{
|
||||
unsigned glyph_size = glyph_id_size (included_class_def_glyphs);
|
||||
unsigned range_size = 2 * glyph_size + OT::HBUINT16::static_size;
|
||||
unsigned size = OT::HBUINT16::static_size + glyph_size;
|
||||
if (included_class_def_glyphs.is_empty ())
|
||||
return OT::HBUINT16::static_size + OT::Layout::SmallTypes::size;
|
||||
return size + range_size * class_def_range_count;
|
||||
}
|
||||
|
||||
bool class_def_uses_format1 () const
|
||||
{
|
||||
if (included_class_def_glyphs.is_empty ()) return false;
|
||||
return class_def_format1_size () <= class_def_format2_size ();
|
||||
unsigned best_size = UINT_MAX;
|
||||
hb_codepoint_t glyph_max = included_class_def_glyphs.get_max ();
|
||||
unsigned glyph_span = glyph_max - included_class_def_glyphs.get_min () + 1;
|
||||
|
||||
if (glyph_max <= 0xFFFFu && glyph_span <= 0xFFFFu && class_max <= 0xFFFFu)
|
||||
best_size = hb_min (best_size, 6 + 2 * glyph_span);
|
||||
if (glyph_max <= 0xFFFFu && class_def_range_count <= 0xFFFFu && class_max <= 0xFFFFu)
|
||||
best_size = hb_min (best_size, 4 + 6 * class_def_range_count);
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
if (glyph_max <= 0xFFFFFFu && glyph_span <= 0xFFFFFFu && class_max <= 0xFFFFFFu)
|
||||
best_size = hb_min (best_size, 8 + 3 * glyph_span);
|
||||
if (glyph_max <= 0xFFFFFFu && class_def_range_count <= 0xFFFFFFu && class_max <= 0xFFFFu)
|
||||
best_size = hb_min (best_size, 5 + 8 * class_def_range_count);
|
||||
#endif
|
||||
return best_size;
|
||||
}
|
||||
|
||||
hb_hashmap_t<unsigned, unsigned> num_ranges_per_class;
|
||||
@@ -306,6 +307,7 @@ struct class_def_size_estimator_t
|
||||
hb_set_t included_glyphs;
|
||||
hb_set_t included_class_def_glyphs;
|
||||
unsigned class_def_range_count;
|
||||
unsigned class_max;
|
||||
unsigned coverage_size_val;
|
||||
};
|
||||
|
||||
|
||||
@@ -217,9 +217,35 @@ static void test_class_and_coverage_size_estimates ()
|
||||
{0x10002, 1},
|
||||
};
|
||||
hb_always_assert (check_add_class_def_size (high_non_consecutive, 1));
|
||||
|
||||
gid_and_class_list_t high_class = {
|
||||
{5, 0x10000},
|
||||
};
|
||||
hb_always_assert (check_add_class_def_size (high_class, 0x10000));
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
static void test_class_def_format_3_values ()
|
||||
{
|
||||
alignas (OT::ClassDef) static const unsigned char data[] = {
|
||||
0x00, 0x03, // format
|
||||
0x00, 0x00, 0x05, // startGlyphID
|
||||
0x00, 0x00, 0x02, // glyphCount
|
||||
0x00, 0x00, 0x01, // classValue[0]
|
||||
0x01, 0x00, 0x00, // classValue[1]
|
||||
};
|
||||
const OT::ClassDef &class_def = *reinterpret_cast<const OT::ClassDef *> (data);
|
||||
|
||||
hb_always_assert (class_def.get_class (5) == 1);
|
||||
hb_always_assert (class_def.get_class (6) == 0x10000u);
|
||||
|
||||
hb_set_t glyphs;
|
||||
glyphs.add (6);
|
||||
hb_always_assert (class_def.intersects_class (&glyphs, 0x10000u));
|
||||
}
|
||||
#endif
|
||||
|
||||
static void test_running_class_and_coverage_size_estimates () {
|
||||
// #### With consecutive gids: switches formats ###
|
||||
gid_and_class_list_t consecutive_map = {
|
||||
@@ -307,4 +333,7 @@ main (int argc, char **argv)
|
||||
test_class_and_coverage_size_estimates ();
|
||||
test_running_class_and_coverage_size_estimates ();
|
||||
test_running_class_size_estimates_with_locally_consecutive_glyphs ();
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
test_class_def_format_3_values ();
|
||||
#endif
|
||||
}
|
||||
+29
-17
@@ -1643,7 +1643,7 @@ struct ClassDefFormat1_3
|
||||
if (classValue[iter - start]) return true;
|
||||
return false;
|
||||
}
|
||||
bool intersects_class (const hb_set_t *glyphs, uint16_t klass) const
|
||||
bool intersects_class (const hb_set_t *glyphs, unsigned klass) const
|
||||
{
|
||||
unsigned int count = classValue.len;
|
||||
if (klass == 0)
|
||||
@@ -1658,7 +1658,7 @@ struct ClassDefFormat1_3
|
||||
}
|
||||
/* TODO Speed up, using set overlap first? */
|
||||
/* TODO(iter) Rewrite as dagger. */
|
||||
const HBUINT16 *arr = classValue.arrayZ;
|
||||
const typename Types::HBUINT *arr = classValue.arrayZ;
|
||||
for (unsigned int i = 0; i < count; i++)
|
||||
if (arr[i] == klass && glyphs->has (startGlyph + i))
|
||||
return true;
|
||||
@@ -1729,7 +1729,7 @@ struct ClassDefFormat1_3
|
||||
HBUINT16 classFormat; /* Format identifier--format = 1 */
|
||||
typename Types::HBGlyphID
|
||||
startGlyph; /* First GlyphID of the classValueArray */
|
||||
typename Types::template ArrayOf<HBUINT16>
|
||||
typename Types::template ArrayOf<typename Types::HBUINT>
|
||||
classValue; /* Array of Class Values--one per GlyphID */
|
||||
public:
|
||||
DEFINE_SIZE_ARRAY (2 + 2 * Types::size, classValue);
|
||||
@@ -1924,7 +1924,7 @@ struct ClassDefFormat2_4
|
||||
return hb_any (+ hb_iter (rangeRecord)
|
||||
| hb_map ([glyphs] (const RangeRecord<Types> &range) { return range.intersects (*glyphs) && range.value; }));
|
||||
}
|
||||
bool intersects_class (const hb_set_t *glyphs, uint16_t klass) const
|
||||
bool intersects_class (const hb_set_t *glyphs, unsigned klass) const
|
||||
{
|
||||
if (klass == 0)
|
||||
{
|
||||
@@ -2110,12 +2110,12 @@ struct ClassDef
|
||||
|
||||
unsigned format = 2;
|
||||
hb_codepoint_t glyph_max = 0;
|
||||
unsigned class_max = 0;
|
||||
if (likely (glyphs))
|
||||
{
|
||||
hb_codepoint_t glyph_min = (*glyphs).first;
|
||||
glyph_max = glyph_min;
|
||||
|
||||
unsigned num_glyphs = 0;
|
||||
unsigned num_ranges = 1;
|
||||
hb_codepoint_t prev_gid = glyph_min;
|
||||
unsigned prev_klass = (*glyphs).second;
|
||||
@@ -2124,7 +2124,7 @@ struct ClassDef
|
||||
{
|
||||
hb_codepoint_t cur_gid = gid_klass_pair.first;
|
||||
unsigned cur_klass = gid_klass_pair.second;
|
||||
num_glyphs++;
|
||||
if (cur_klass > class_max) class_max = cur_klass;
|
||||
if (cur_gid == glyph_min) continue;
|
||||
if (cur_gid > glyph_max) glyph_max = cur_gid;
|
||||
if (cur_gid != prev_gid + 1 ||
|
||||
@@ -2135,20 +2135,32 @@ struct ClassDef
|
||||
prev_klass = cur_klass;
|
||||
}
|
||||
|
||||
if (num_glyphs && 1 + (glyph_max - glyph_min + 1) <= num_ranges * 3)
|
||||
format = 1;
|
||||
}
|
||||
uint64_t best_size = UINT64_MAX;
|
||||
unsigned glyph_span = glyph_max - glyph_min + 1;
|
||||
auto consider = [&] (unsigned candidate, uint64_t size)
|
||||
{
|
||||
if (size >= best_size) return;
|
||||
best_size = size;
|
||||
format = candidate;
|
||||
};
|
||||
|
||||
if (glyph_max <= 0xFFFFu && glyph_span <= 0xFFFFu && class_max <= 0xFFFFu)
|
||||
consider (1, 6 + (uint64_t) HBUINT16::static_size * glyph_span);
|
||||
if (glyph_max <= 0xFFFFu && num_ranges <= 0xFFFFu && class_max <= 0xFFFFu)
|
||||
consider (2, 4 + (uint64_t) num_ranges *
|
||||
(2 * HBGlyphID16::static_size + HBUINT16::static_size));
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
if (glyph_max > 0xFFFFu)
|
||||
format += 2;
|
||||
if (unlikely (glyph_max > 0xFFFFFFu))
|
||||
#else
|
||||
if (unlikely (glyph_max > 0xFFFFu))
|
||||
if (glyph_max <= 0xFFFFFFu && glyph_span <= 0xFFFFFFu && class_max <= 0xFFFFFFu)
|
||||
consider (3, 8 + (uint64_t) HBUINT24::static_size * glyph_span);
|
||||
if (glyph_max <= 0xFFFFFFu && num_ranges <= 0xFFFFFFu && class_max <= 0xFFFFu)
|
||||
consider (4, 5 + (uint64_t) num_ranges *
|
||||
(2 * HBGlyphID24::static_size + HBUINT16::static_size));
|
||||
#endif
|
||||
{
|
||||
c->check_success (false, HB_SERIALIZE_ERROR_INT_OVERFLOW);
|
||||
return_trace (false);
|
||||
if (unlikely (best_size == UINT64_MAX))
|
||||
{
|
||||
c->check_success (false, HB_SERIALIZE_ERROR_INT_OVERFLOW);
|
||||
return_trace (false);
|
||||
}
|
||||
}
|
||||
|
||||
u.format.v = format;
|
||||
|
||||
Reference in new issue
Block a user