File: shape_result.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (574 lines) | stat: -rw-r--r-- 24,945 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_H_

#include <memory>

#include "base/containers/span.h"
#include "base/dcheck_is_on.h"
#include "base/types/strong_alias.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/fonts/canvas_rotation_in_vertical.h"
#include "third_party/blink/renderer/platform/fonts/glyph.h"
#include "third_party/blink/renderer/platform/fonts/opentype/open_type_math_stretch_data.h"
#include "third_party/blink/renderer/platform/fonts/shaping/glyph_index_result.h"
#include "third_party/blink/renderer/platform/fonts/simple_font_data.h"
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_vector.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/text/text_direction.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_uchar.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/vector2d_f.h"

#if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64)
#define USE_SIMD_FOR_COMPUTING_GLYPH_BOUNDS 1
#endif

struct hb_buffer_t;

namespace blink {

struct CharacterRange;
class Font;
struct GlyphIndexResult;
struct ShapeResultRun;
template <typename TextContainerType>
class PLATFORM_EXPORT ShapeResultSpacing;
class TextRun;
class ShapeResultView;
struct TabSize;

enum class AdjustMidCluster {
  // Adjust the middle of a grapheme cluster to the logical end boundary.
  kToEnd,
  // Adjust the middle of a grapheme cluster to the logical start boundary.
  kToStart
};

struct ShapeResultCharacterData {
  DISALLOW_NEW();

  ShapeResultCharacterData() = default;

  void SetCachedData(LayoutUnit new_x_position,
                     bool new_is_cluster_base,
                     bool new_safe_to_break_before) {
    x_position = new_x_position;
    is_cluster_base = new_is_cluster_base;
    safe_to_break_before = new_safe_to_break_before;
  }

  LayoutUnit x_position;
  // Set for the logical first character of a cluster.
  unsigned is_cluster_base : 1 = false;
  unsigned safe_to_break_before : 1 = false;
  unsigned has_auto_spacing_after : 1 = false;
};

// A space should be appended after `offset` with the width of `spacing`.
struct PLATFORM_EXPORT OffsetWithSpacing {
  wtf_size_t offset;
  float spacing;
};

struct DeprecatedInkBounds : public GarbageCollected<DeprecatedInkBounds> {
  void Trace(Visitor*) const {}
  gfx::RectF ink_bounds;
};

// There are two options for how OffsetForPosition behaves:
// IncludePartialGlyphs - decides what to do when the position hits more than
// 50% of the glyph. If enabled, we count that glyph, if disable we don't.
enum IncludePartialGlyphsOption {
  kOnlyFullGlyphs,
  kIncludePartialGlyphs,
};

// BreakGlyphsOption - allows OffsetForPosition to consider graphemes
// separations inside a glyph. It allows the function to return a point inside
// a glyph when multiple graphemes share a glyph (for example, in a ligature)
using BreakGlyphsOption = base::StrongAlias<class BreakGlyphsOptionTag, bool>;

// std::function is forbidden in Chromium and base::RepeatingCallback is way too
// expensive so we resort to a good old function pointer instead.
typedef void (*GlyphCallback)(void* context,
                              unsigned character_index,
                              Glyph,
                              gfx::Vector2dF glyph_offset,
                              float total_advance,
                              bool is_horizontal,
                              CanvasRotationInVertical,
                              const SimpleFontData*);

typedef void (*GraphemeClusterCallback)(void* context,
                                        unsigned character_index,
                                        float total_advance,
                                        unsigned graphemes_in_cluster,
                                        float cluster_advance,
                                        CanvasRotationInVertical);

class PLATFORM_EXPORT ShapeResult : public GarbageCollected<ShapeResult> {
 public:
  ShapeResult(unsigned start_index, unsigned num_characters, TextDirection);
  ShapeResult(const ShapeResult&);

  void Trace(Visitor*) const;

  static ShapeResult* CreateEmpty(const ShapeResult& other) {
    return MakeGarbageCollected<ShapeResult>(0, 0, other.Direction());
  }
  static const ShapeResult* CreateForTabulationCharacters(
      const Font* font,
      TextDirection direction,
      const TabSize& tab_size,
      float position,
      unsigned start_index,
      unsigned length);
  // The first glyph has |width| advance, and other glyphs have 0 advance.
  static const ShapeResult* CreateForSpaces(const Font* font,
                                            TextDirection direction,
                                            unsigned start_index,
                                            unsigned length,
                                            float width);
  static const ShapeResult* CreateForStretchyMathOperator(const Font*,
                                                          TextDirection,
                                                          Glyph,
                                                          float stretch_size);
  static const ShapeResult* CreateForStretchyMathOperator(
      const Font*,
      TextDirection,
      OpenTypeMathStretchData::StretchAxis,
      const OpenTypeMathStretchData::AssemblyParameters&);
  ~ShapeResult();

  // The logical width of this result.
  float Width() const { return width_; }
  LayoutUnit SnappedWidth() const { return LayoutUnit::FromFloatCeil(width_); }
  unsigned NumCharacters() const { return num_characters_; }

  bool HasLigatures() const;
  unsigned NumGlyphs() const;
  bool HasFallbackFonts(const SimpleFontData* primary_font) const;

  // TODO(eae): Remove start_x and return value once ShapeResultBuffer has been
  // removed.
  float IndividualCharacterRanges(Vector<CharacterRange>* ranges,
                                  float start_x = 0) const;

  // The character start/end index of a range shape result.
  unsigned StartIndex() const { return start_index_; }
  unsigned EndIndex() const { return start_index_ + num_characters_; }
  TextDirection Direction() const {
    return static_cast<TextDirection>(direction_);
  }
  bool IsLtr() const { return blink::IsLtr(Direction()); }
  bool IsRtl() const { return blink::IsRtl(Direction()); }

  // True if at least one glyph in this result has vertical offsets.
  //
  // Vertical result always has vertical offsets, but horizontal result may also
  // have vertical offsets.
  bool HasVerticalOffsets() const { return has_vertical_offsets_; }

  // Note: We should not reuse |ShapeResult| if we call |ApplySpacing()|.
  bool IsAppliedSpacing() const { return is_applied_spacing_; }

  // For memory reporting.
  size_t ByteSize() const;

  // True if |StartIndex()| is safe to break.
  bool IsStartSafeToBreak() const;

  // Returns the next or previous offsets respectively at which it is safe to
  // break without reshaping.
  // The |offset| given and the return value is for the original string, between
  // |StartIndex| and |EndIndex|.
  // TODO(eae): Remove these ones the cached versions are used everywhere.
  unsigned NextSafeToBreakOffset(unsigned offset) const;
  unsigned PreviousSafeToBreakOffset(unsigned offset) const;

  void AddUnsafeToBreak(base::span<const unsigned>);

  // Returns the offset, relative to StartIndex, whose (origin,
  // origin+advance) contains |x|.
  unsigned OffsetForPosition(float x, BreakGlyphsOption) const;
  // Returns the offset whose glyph boundary is nearest to |x|. Depends on
  // whether |x| is on the left-half or the right-half of the glyph, it
  // determines the left-boundary or the right-boundary, then computes the
  // offset from the bidi direction.
  unsigned CaretOffsetForHitTest(float x,
                                 const StringView& text,
                                 BreakGlyphsOption) const;
  // Returns the offset that can fit to between |x| and the left or the right
  // edge. The side of the edge is determined by |line_direction|.
  unsigned OffsetToFit(float x, TextDirection line_direction) const;
  unsigned OffsetForPosition(float x,
                             const StringView& text,
                             IncludePartialGlyphsOption include_partial_glyphs,
                             BreakGlyphsOption break_glyphs) const {
    if (include_partial_glyphs == kOnlyFullGlyphs) {
      // TODO(kojii): Consider prohibiting OnlyFullGlyphs +
      // BreakGlyphsOption(true), sed only in tests.
      if (break_glyphs)
        EnsureGraphemes(text);
      return OffsetForPosition(x, break_glyphs);
    }
    return CaretOffsetForHitTest(x, text, break_glyphs);
  }

  // Returns the position for a given offset, relative to StartIndex.
  float PositionForOffset(unsigned offset,
                          AdjustMidCluster = AdjustMidCluster::kToEnd) const;
  // Similar to |PositionForOffset| with mid-glyph (mid-ligature) support.
  float CaretPositionForOffset(
      unsigned offset,
      const StringView& text,
      AdjustMidCluster = AdjustMidCluster::kToEnd) const;
  LayoutUnit SnappedStartPositionForOffset(unsigned offset) const {
    return LayoutUnit::FromFloatFloor(PositionForOffset(offset));
  }
  LayoutUnit SnappedEndPositionForOffset(unsigned offset) const {
    return LayoutUnit::FromFloatCeil(PositionForOffset(offset));
  }

  // Computes and caches a position data object as needed.
  void EnsurePositionData() const;

  const ShapeResultCharacterData& CharacterData(unsigned offset) const;
  ShapeResultCharacterData& CharacterData(unsigned offset);

  // Fast versions of OffsetForPosition and PositionForOffset that operates on
  // a cache (that needs to be pre-computed using EnsurePositionData) and that
  // does not take partial glyphs into account.
  unsigned CachedOffsetForPosition(LayoutUnit x) const;
  LayoutUnit CachedPositionForOffset(unsigned offset) const;
  LayoutUnit CachedWidth(unsigned start_offset, unsigned end_offset) const;

  // Returns the next or previous offsets respectively at which it is safe to
  // break without reshaping. Operates on a cache (that needs to be pre-computed
  // using EnsurePositionData) and does not take partial glyphs into account.
  // The |offset| given and the return value is for the original string, between
  // |StartIndex| and |EndIndex|.
  unsigned CachedNextSafeToBreakOffset(unsigned offset) const;
  unsigned CachedPreviousSafeToBreakOffset(unsigned offset) const;

  // Apply spacings (letter-spacing, word-spacing, and justification) as
  // configured to |ShapeResultSpacing|.
  // |text_start_offset| adjusts the character index in the ShapeResult before
  // giving it to |ShapeResultSpacing|. It can be negative if
  // |StartIndex()| is larger than the text in |ShapeResultSpacing|.
  //
  // The function returns spacing amount on the right of the last glyph.
  float ApplySpacing(ShapeResultSpacing<String>&, int text_start_offset = 0);
  ShapeResult* ApplySpacingToCopy(ShapeResultSpacing<TextRun>&,
                                  const TextRun&) const;
  // Add `expansion` space before the first glyph.
  void ApplyLeadingExpansion(LayoutUnit expansion);
  // Add `expansion` space after the last glyph.
  void ApplyTrailingExpansion(LayoutUnit expansion);

  // Adds spacing between ideograph character and non-ideograph character for
  // the property of text-autospace.
  void ApplyTextAutoSpacing(
      const Vector<OffsetWithSpacing, 16>& offsets_with_spacing);

  // True if the auto-spacing is applied. See `ApplyTextAutoSpacing`.
  bool HasAutoSpacingAfter(unsigned offset) const;
  bool HasAutoSpacingBefore(unsigned offset) const;

  // Returns a line-end `ShapeResult` when breaking at `break_offset`, and the
  // glyph before `break_offset` has auto-spacing.
  const ShapeResult* UnapplyAutoSpacing(float spacing_width,
                                        unsigned start_offset,
                                        unsigned break_offset) const;

  // Adjust the offset from `OffsetForPosition` when the offset has
  // `HasAutoSpacingAfter`.
  unsigned AdjustOffsetForAutoSpacing(float spacing_width,
                                      unsigned offset,
                                      float position) const;

  // Append a copy of a range within an existing result to another result.
  //
  // For sequential copies the vector version below is prefered as it avoid a
  // linear scan to find the first run for the range.
  void CopyRange(unsigned start, unsigned end, ShapeResult*) const;

  struct ShapeRange {
    DISALLOW_NEW();

   public:
    ShapeRange(unsigned start, unsigned end, ShapeResult* target)
        : start(start), end(end), target(target) {}

    void Trace(Visitor* visitor) const { visitor->Trace(target); }

    unsigned start;
    unsigned end;
    Member<ShapeResult> target;
  };

  // Copy a set of sequential ranges. The ranges may not overlap and the offsets
  // must be sequential and monotically increasing.
  void CopyRanges(const ShapeRange* ranges, unsigned num_ranges) const;

  // Create a new ShapeResult instance from a range within an existing result.
  ShapeResult* SubRange(unsigned start_offset, unsigned end_offset) const;

  // Create a new ShapeResult instance with the start offset adjusted.
  const ShapeResult* CopyAdjustedOffset(unsigned start_offset) const;

  // Computes the list of fonts along with the number of glyphs for each font.
  struct RunFontData {
    DISALLOW_NEW();
    void Trace(Visitor* visitor) const { visitor->Trace(font_data_); }
    Member<SimpleFontData> font_data_;
    wtf_size_t glyph_count_;
  };
  void GetRunFontData(HeapVector<RunFontData>* font_data) const;

  // Iterates over, and calls the specified callback function, for all the
  // glyphs. Also tracks (and returns) a seeded total advance.
  // The second version of the method only invokes the callback for glyphs in
  // the specified range and stops after the range.
  // The context parameter will be given as the first parameter for the callback
  // function.
  //
  // TODO(eae): Remove the initial_advance and index_offset parameters once
  // ShapeResultBuffer has been removed as they're only used in cases where
  // multiple ShapeResult are combined in a ShapeResultBuffer.
  float ForEachGlyph(float initial_advance, GlyphCallback, void* context) const;
  float ForEachGlyph(float initial_advance,
                     unsigned from,
                     unsigned to,
                     unsigned index_offset,
                     GlyphCallback,
                     void* context) const;

  // Iterates over, and calls the specified callback function, for all the
  // grapheme clusters. As ShapeResuls do not contain the original text content
  // a StringView with the text must be supplied and must match the text that
  // was used generate the ShapeResult.
  // Also tracks (and returns) a seeded total advance.
  // The context parameter will be given as the first parameter for the callback
  // function.
  float ForEachGraphemeClusters(const StringView& text,
                                float initial_advance,
                                unsigned from,
                                unsigned to,
                                unsigned index_offset,
                                GraphemeClusterCallback,
                                void* context) const;

  // Computes and returns the ink bounds (or visual overflow rect). This is
  // quite expensive and involves measuring each glyph accumulating the bounds.
  gfx::RectF ComputeInkBounds() const;

  // Only used by CachingWordShapeIterator
  // TODO(eae): Remove once LayoutNG lands. https://crbug.com/591099
  void SetDeprecatedInkBounds(gfx::RectF ink_bounds) {
    if (!deprecated_ink_bounds_) {
      deprecated_ink_bounds_ = MakeGarbageCollected<DeprecatedInkBounds>();
    }
    deprecated_ink_bounds_->ink_bounds = ink_bounds;
  }
  gfx::RectF GetDeprecatedInkBounds() const {
    DCHECK(deprecated_ink_bounds_);
    return deprecated_ink_bounds_->ink_bounds;
  }

  String ToString() const;
  void ToString(StringBuilder*) const;

  ShapeResultRun* InsertRunForTesting(unsigned start_index,
                                      unsigned num_characters,
                                      TextDirection,
                                      Vector<uint16_t> safe_break_offsets = {});
#if DCHECK_IS_ON()
  void CheckConsistency() const;
#endif

 protected:
  // Ensure |grapheme_| is computed. |BreakGlyphs| is valid only when
  // |grapheme_| is computed.
  void EnsureGraphemes(const StringView& text) const;

  static unsigned CountGraphemesInCluster(base::span<const UChar>,
                                          uint16_t start_index,
                                          uint16_t end_index);

  template <typename Iterator>
  void AddUnsafeToBreak(Iterator offsets_begin, const Iterator offsets_end);

  void OffsetForPosition(float target_x,
                         BreakGlyphsOption,
                         GlyphIndexResult*) const;

  // Append a copy of a range within an existing result to another result.
  //
  // For sequential copies the run_index argument indicates the run to start at.
  // If set to zero it will always scan from the first run which is guaranteed
  // to produce the correct results at the cost of run-time performance.
  // Returns the appropriate run_index for the next sequential invocation.
  unsigned CopyRangeInternal(unsigned run_index,
                             unsigned start,
                             unsigned end,
                             ShapeResult* target) const;

  template <bool>
  void ComputePositionData() const;
  void RecalcCharacterPositions() const;

  template <typename TextContainerType>
  float ApplySpacingImpl(ShapeResultSpacing<TextContainerType>&,
                         int text_start_offset = 0);
  template <bool is_horizontal_run>
  void ComputeGlyphPositions(ShapeResultRun*,
                             unsigned start_glyph,
                             unsigned num_glyphs,
                             hb_buffer_t*);
  // Inserts as many glyphs as possible as a ShapeResultRun, and sets
  // |next_start_glyph| to the start index of the remaining glyphs to be
  // inserted.
  void InsertRun(ShapeResultRun*,
                 unsigned start_glyph,
                 unsigned num_glyphs,
                 unsigned* next_start_glyph,
                 hb_buffer_t*);
  void InsertRun(ShapeResultRun*);
  void ReorderRtlRuns(unsigned run_size_before);

  template <bool is_horizontal_run, bool has_non_zero_glyph_offsets>
  void ComputeRunInkBounds(const ShapeResultRun&,
                           float run_advance,
                           gfx::RectF* ink_bounds) const;

  template <bool is_horizontal_run, bool has_non_zero_glyph_offsets>
  void ComputeRunInkBoundsScalar(const ShapeResultRun&,
                                 float run_advance,
                                 gfx::RectF* ink_bounds) const;
#if defined(USE_SIMD_FOR_COMPUTING_GLYPH_BOUNDS)
  template <bool is_horizontal_run, bool has_non_zero_glyph_offsets>
  void ComputeRunInkBoundsVectorized(const ShapeResultRun&,
                                     float run_advance,
                                     gfx::RectF* ink_bounds) const;
#endif  // defined(USE_SIMD_FOR_COMPUTING_GLYPH_BOUNDS)

  // Common signatures with ShapeResultView, to templatize algorithms.
  const HeapVector<Member<ShapeResultRun>, 1>& RunsOrParts() const {
    return runs_;
  }
  unsigned StartIndexOffsetForRun() const { return 0; }

  // Stores x-positions for quick mapping between offsets and x-positions.
  // Unlike the ShapeResultRun and GlyphData, which operates in glyph order,
  // this class stores a map between character index and the total accumulated
  // advance for each character. Allowing constant time mapping from character
  // index to x-position and O(log n) time, using binary search, from
  // x-position to character index.
  mutable HeapVector<ShapeResultCharacterData> character_position_;

  HeapVector<Member<ShapeResultRun>, 1> runs_;

  // Only used by CachingWordShapeIterator and stored here for memory reduction
  // reasons. See https://crbug.com/955776
  // TODO(eae): Remove once LayoutNG lands. https://crbug.com/591099
  Member<DeprecatedInkBounds> deprecated_ink_bounds_ = nullptr;

  // The total width. This is the sum of `ShapeResultRun::width_`.
  // It's mutable because `RecalcCharacterPositions()` recalculates this.
  // This should be in sync with `CharacterPositionData::width_`.
  mutable float width_ = 0;

  unsigned start_index_ = 0;
  unsigned num_characters_ : 29 = 0;

  // Overall direction for the TextRun, dictates which order each individual
  // sub run (represented by ShapeResultRun structs in the m_runs vector) can
  // have a different text direction.
  unsigned direction_ : 1 = static_cast<unsigned>(TextDirection::kLtr);

  // Tracks whether any runs contain glyphs with a y-offset != 0.
  unsigned has_vertical_offsets_ : 1 = false;

  // True once called |ApplySpacing()|.
  unsigned is_applied_spacing_ : 1 = false;

  // Note: When you add more bit flags, please consider to reduce size of
  // |num_characters_|.

 private:
  friend class HarfBuzzShaper;
  friend class ShapeResultBuffer;
  friend class ShapeResultBloberizer;
  friend class ShapeResultView;
  friend class ShapeResultTest;
  friend class StretchyOperatorShaper;

  template <bool has_non_zero_glyph_offsets>
  float ForEachGlyphImpl(float initial_advance,
                         GlyphCallback,
                         void* context,
                         const ShapeResultRun& run) const;

  template <bool has_non_zero_glyph_offsets>
  float ForEachGlyphImpl(float initial_advance,
                         unsigned from,
                         unsigned to,
                         unsigned index_offset,
                         GlyphCallback,
                         void* context,
                         const ShapeResultRun& run) const;

  // Internal implementation of `ApplyTextAutoSpacing`. The iterator can be
  // Vector::iterator or Vector::reverse_iterator, depending on the text
  // direction.
  template <TextDirection direction, class Iterator>
  void ApplyTextAutoSpacingCore(Iterator offset_begin, Iterator offset_end);
};

PLATFORM_EXPORT std::ostream& operator<<(std::ostream&, const ShapeResult&);

}  // namespace blink

WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::ShapeResult::ShapeRange)
WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::ShapeResult::RunFontData)
WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(
    blink::ShapeResultCharacterData)

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_H_