Avoid overallocating RunInfo glyph vector

The ShapeResult::RunInfo glyph_data vector has an effective maximum size
of (1 << 15) - 1 yet we always allocate up to the total number of glyphs
even if that exceeds the limit and multiple runs are needed to represent
the slice or shape result. This patch limits glyph allocation to the max
number supported, saving time on memory allocation.

Cq-Include-Trybots: luci.chromium.try:linux_layout_tests_layout_ng
Change-Id: I6fc30d2bf77d8967126f7cb563107f5ac8006adc
Reviewed-on: https://chromium-review.googlesource.com/1150296
Reviewed-by: Ian Kilpatrick <ikilpatrick@chromium.org>
Commit-Queue: Emil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578058}
diff --git a/third_party/blink/renderer/platform/fonts/shaping/shape_result.cc b/third_party/blink/renderer/platform/fonts/shaping/shape_result.cc
index 53bbbfc..2c108da 100644
--- a/third_party/blink/renderer/platform/fonts/shaping/shape_result.cc
+++ b/third_party/blink/renderer/platform/fonts/shaping/shape_result.cc
@@ -851,7 +851,7 @@
 
     uint16_t character_index =
         glyph_infos[start_glyph + i].cluster - start_cluster;
-    if (UNLIKELY(character_index > HarfBuzzRunGlyphData::kMaxCharacterIndex)) {
+    if (UNLIKELY(character_index >= HarfBuzzRunGlyphData::kMaxCharacterIndex)) {
       // If the character index exceeds the limit, abort and shrink the run to
       // what are actually stored.
       run->num_characters_ = character_index;
@@ -884,7 +884,6 @@
                             hb_buffer_t* harfbuzz_buffer) {
   DCHECK_GT(num_glyphs, 0u);
   std::unique_ptr<ShapeResult::RunInfo> run(std::move(run_to_insert));
-  DCHECK_EQ(num_glyphs, run->glyph_data_.size());
 
   if (run->IsHorizontal()) {
     // Inserting a horizontal run into a horizontal or vertical result. In both
diff --git a/third_party/blink/renderer/platform/fonts/shaping/shape_result_inline_headers.h b/third_party/blink/renderer/platform/fonts/shaping/shape_result_inline_headers.h
index 3609c766..561a86d 100644
--- a/third_party/blink/renderer/platform/fonts/shaping/shape_result_inline_headers.h
+++ b/third_party/blink/renderer/platform/fonts/shaping/shape_result_inline_headers.h
@@ -79,7 +79,7 @@
         direction_(dir),
         canvas_rotation_(canvas_rotation),
         script_(script),
-        glyph_data_(num_glyphs),
+        glyph_data_(std::min(num_glyphs, HarfBuzzRunGlyphData::kMaxCharacterIndex)),
         graphemes_(graphemes),
         start_index_(start_index),
         num_characters_(num_characters),