diff --git a/src/OT/Layout/Common/CoverageFormat2.hh b/src/OT/Layout/Common/CoverageFormat2.hh index 43b56fe15..ea14be16f 100644 --- a/src/OT/Layout/Common/CoverageFormat2.hh +++ b/src/OT/Layout/Common/CoverageFormat2.hh @@ -95,10 +95,14 @@ struct CoverageFormat2_4 unsigned count = 0; unsigned range = (unsigned) -1; last = (hb_codepoint_t) -2; + unsigned unsorted = false; for (auto g: glyphs) { if (last + 1 != g) { + if (unlikely (last != (hb_codepoint_t) -2 && last + 1 > g)) + unsorted = true; + range++; rangeRecord.arrayZ[range].first = g; rangeRecord.arrayZ[range].value = count; @@ -107,7 +111,9 @@ struct CoverageFormat2_4 last = g; count++; } - rangeRecord.as_array ().qsort (RangeRecord::cmp_range); // To handle unsorted glyph order. + + if (unlikely (unsorted)) + rangeRecord.as_array ().qsort (RangeRecord::cmp_range); return_trace (true); } diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index b229bdcee..36f123b55 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -1769,6 +1769,7 @@ struct ClassDefFormat2_4 return_trace (true); } + unsigned unsorted = false; unsigned num_ranges = 1; hb_codepoint_t prev_gid = (*it).first; unsigned prev_klass = (*it).second; @@ -1789,6 +1790,10 @@ struct ClassDefFormat2_4 if (cur_gid != prev_gid + 1 || cur_klass != prev_klass) { + + if (unlikely (cur_gid < prev_gid)) + unsorted = true; + if (unlikely (!record)) break; record->last = prev_gid; num_ranges++; @@ -1808,7 +1813,9 @@ struct ClassDefFormat2_4 if (likely (record)) record->last = prev_gid; rangeRecord.len = num_ranges; - rangeRecord.as_array ().qsort (RangeRecord::cmp_range); // To handle unsorted glyph order. + + if (unlikely (unsorted)) + rangeRecord.as_array ().qsort (RangeRecord::cmp_range); return_trace (true); }