File: shape_result_bloberizer.cc

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 (699 lines) | stat: -rw-r--r-- 25,297 bytes parent folder | download | duplicates (2)
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_bloberizer.h"

#include <hb.h>

#include <algorithm>

#include "base/logging.h"
#include "cc/paint/paint_canvas.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/fonts/plain_text_node.h"
#include "third_party/blink/renderer/platform/fonts/shaping/caching_word_shaper.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_view.h"
#include "third_party/blink/renderer/platform/fonts/text_run_paint_info.h"
#include "third_party/blink/renderer/platform/text/text_break_iterator.h"
#include "third_party/blink/renderer/platform/text/text_run.h"

namespace blink {

namespace {

// A helper for FillGlyphsSlow().
inline const ShapeResult* GetShapeResult(
    const Member<const ShapeResult>& item) {
  return item.Get();
}

// A helper for FillGlyphsSlow().
inline const ShapeResult* GetShapeResult(const PlainTextItem& item) {
  return item.GetShapeResult();
}

}  // namespace

ShapeResultBloberizer::ShapeResultBloberizer(
    const FontDescription& font_description,
    Type type)
    : font_description_(font_description), type_(type) {}

bool ShapeResultBloberizer::HasPendingVerticalOffsets() const {
  // We exclusively store either horizontal/x-only offsets -- in which case
  // m_offsets.size == size, or vertical/xy offsets -- in which case
  // m_offsets.size == size * 2.
  DCHECK(pending_glyphs_.size() == pending_offsets_.size() ||
         pending_glyphs_.size() * 2 == pending_offsets_.size());
  return pending_glyphs_.size() != pending_offsets_.size();
}

void ShapeResultBloberizer::SetText(const StringView& text,
                                    unsigned from,
                                    unsigned to,
                                    base::span<const unsigned> cluster_starts) {
  if (current_text_.IsNull())
    CommitPendingRun();

  // Any outstanding 'current' state should have been moved to 'pending'.
  DCHECK(current_character_indexes_.empty());

  DVLOG(4) << "   SetText from: " << from << " to: " << to;

  // cluster_ends_ must be at least the size of the source run length,
  // but the run length may be negative (in which case no glyphs will be added).
  if (from < to) {
    DVLOG(4) << "   SetText text: "
             << StringView(text, from, to - from).ToString();
    cluster_ends_.resize(to - from);
    for (size_t i = 0; i < cluster_starts.size() - 1; ++i) {
      cluster_ends_[cluster_starts[i] - from] = cluster_starts[i + 1];
    }
  } else {
    cluster_ends_.Shrink(0);
  }

  DVLOG(4) << "   Cluster ends: " << base::span(cluster_ends_);

  cluster_ends_offset_ = from;
  current_text_ = text;
}

void ShapeResultBloberizer::CommitText() {
  if (current_character_indexes_.empty())
    return;

  unsigned from = current_character_indexes_[0];
  unsigned to = current_character_indexes_[0];
  for (unsigned character_index : current_character_indexes_) {
    unsigned character_index_end =
        cluster_ends_[character_index - cluster_ends_offset_];
    from = std::min(from, character_index);
    to = std::max(to, character_index_end);
  }

  DCHECK(!current_text_.IsNull());

  DVLOG(4) << "   CommitText from: " << from << " to: " << to;
  DVLOG(4)
      << "   CommitText glyphs: "
      << base::span(pending_glyphs_).last(current_character_indexes_.size());
  DVLOG(4) << "   CommitText cluster starts: "
           << base::span(current_character_indexes_);

  wtf_size_t pending_utf8_original_size = pending_utf8_.size();
  wtf_size_t pending_utf8_character_indexes_original_size =
      pending_utf8_character_indexes_.size();

  // Do the UTF-8 conversion here.
  // For each input code point track the location of output UTF-8 code point.

  unsigned current_text_length = current_text_.length();
  DCHECK_LE(to, current_text_length);

  unsigned size = to - from;
  Vector<uint32_t, 256> pending_utf8_character_index_from_character_index(size);
  if (current_text_.Is8Bit()) {
    const LChar* latin1 = UNSAFE_TODO(current_text_.Characters8());
    wtf_size_t utf8_size = pending_utf8_.size();
    for (unsigned i = from; i < to;) {
      pending_utf8_character_index_from_character_index[i - from] = utf8_size;

      LChar cp = UNSAFE_TODO(latin1[i++]);
      pending_utf8_.Grow(utf8_size + U8_LENGTH(cp));
      UNSAFE_TODO(U8_APPEND_UNSAFE(pending_utf8_.begin(), utf8_size, cp));
    }
  } else {
    const UChar* utf16 = UNSAFE_TODO(current_text_.Characters16());
    wtf_size_t utf8_size = pending_utf8_.size();
    for (unsigned i = from; i < to;) {
      pending_utf8_character_index_from_character_index[i - from] = utf8_size;

      UChar32 cp;
      UNSAFE_TODO(U16_NEXT_OR_FFFD(utf16, i, current_text_length, cp));
      pending_utf8_.Grow(utf8_size + U8_LENGTH(cp));
      UNSAFE_TODO(U8_APPEND_UNSAFE(pending_utf8_.begin(), utf8_size, cp));
    }
  }

  for (unsigned character_index : current_character_indexes_) {
    unsigned index = character_index - from;
    pending_utf8_character_indexes_.push_back(
        pending_utf8_character_index_from_character_index[index]);
  }

  current_character_indexes_.Shrink(0);

  DVLOG(4) << "  CommitText appended UTF-8: \""
           << std::string(
                  &pending_utf8_[pending_utf8_original_size],
                  UNSAFE_TODO(pending_utf8_.data() + pending_utf8_.size()))
           << "\"";
  DVLOG(4) << "  CommitText UTF-8 indexes: "
           << base::span(pending_utf8_character_indexes_)
                  .subspan(pending_utf8_character_indexes_original_size);
}

void ShapeResultBloberizer::CommitPendingRun() {
  if (pending_glyphs_.empty())
    return;

  if (pending_canvas_rotation_ != builder_rotation_) {
    // The pending run rotation doesn't match the current blob; start a new
    // blob.
    CommitPendingBlob();
    builder_rotation_ = pending_canvas_rotation_;
  }

  if (!current_character_indexes_.empty()) [[unlikely]] {
    CommitText();
  }

  SkFont run_font =
      pending_font_data_->PlatformData().CreateSkFont(&font_description_);

  const auto run_size = pending_glyphs_.size();
  const auto text_size = pending_utf8_.size();
  const auto& buffer = [&]() {
    if (HasPendingVerticalOffsets()) {
      if (text_size)
        return builder_.allocRunTextPos(run_font, run_size, text_size);
      else
        return builder_.allocRunPos(run_font, run_size);
    } else {
      if (text_size)
        return builder_.allocRunTextPosH(run_font, run_size, 0, text_size);
      else
        return builder_.allocRunPosH(run_font, run_size, 0);
    }
  }();
  builder_run_count_ += 1;

  if (text_size) {
    DVLOG(4) << "  CommitPendingRun text: \""
             << std::string(pending_utf8_.begin(), pending_utf8_.end()) << "\"";
    DVLOG(4) << "  CommitPendingRun glyphs: " << base::span(pending_glyphs_);
    DVLOG(4) << "  CommitPendingRun indexes: "
             << base::span(pending_utf8_character_indexes_);
    DCHECK_EQ(pending_utf8_character_indexes_.size(), run_size);
    std::ranges::copy(pending_utf8_character_indexes_, buffer.clusters);
    std::ranges::copy(pending_utf8_, buffer.utf8text);

    pending_utf8_.Shrink(0);
    pending_utf8_character_indexes_.Shrink(0);
  }

  std::ranges::copy(pending_glyphs_, buffer.glyphs);
  std::ranges::copy(pending_offsets_, buffer.pos);
  pending_glyphs_.Shrink(0);
  pending_offsets_.Shrink(0);
}

void ShapeResultBloberizer::CommitPendingBlob() {
  if (!builder_run_count_)
    return;

  blobs_.emplace_back(builder_.make(), builder_rotation_);
  builder_run_count_ = 0;
}

const ShapeResultBloberizer::BlobBuffer& ShapeResultBloberizer::Blobs() {
  CommitPendingRun();
  CommitPendingBlob();
  DCHECK(pending_glyphs_.empty());
  DCHECK_EQ(builder_run_count_, 0u);

  return blobs_;
}

inline bool ShapeResultBloberizer::IsSkipInkException(
    const StringView& text,
    unsigned character_index) {
  // We want to skip descenders in general, but it is undesirable renderings for
  // CJK characters.
  return type_ == ShapeResultBloberizer::Type::kTextIntercepts &&
         !Character::CanTextDecorationSkipInk(
             text.CodepointAt(character_index));
}

inline void ShapeResultBloberizer::AddEmphasisMark(
    const GlyphData& emphasis_data,
    CanvasRotationInVertical canvas_rotation,
    gfx::PointF glyph_center,
    float mid_glyph_offset) {
  const SimpleFontData* emphasis_font_data = emphasis_data.font_data;
  DCHECK(emphasis_font_data);

  bool is_vertical =
      emphasis_font_data->PlatformData().IsVerticalAnyUpright() &&
      IsCanvasRotationInVerticalUpright(emphasis_data.canvas_rotation);

  if (!is_vertical) {
    Add(emphasis_data.glyph, emphasis_font_data,
        CanvasRotationInVertical::kRegular, mid_glyph_offset - glyph_center.x(),
        0);
  } else {
    Add(emphasis_data.glyph, emphasis_font_data, emphasis_data.canvas_rotation,
        gfx::Vector2dF(-glyph_center.x(), mid_glyph_offset - glyph_center.y()),
        0);
  }
}

namespace {
class GlyphCallbackContext {
  STACK_ALLOCATED();

 public:
  GlyphCallbackContext(ShapeResultBloberizer* bloberizer,
                       const StringView& text)
      : bloberizer(bloberizer), text(text) {}
  GlyphCallbackContext(const GlyphCallbackContext&) = delete;
  GlyphCallbackContext& operator=(const GlyphCallbackContext&) = delete;

  ShapeResultBloberizer* bloberizer;
  const StringView& text;
};
}  // namespace

/*static*/ void ShapeResultBloberizer::AddGlyphToBloberizer(
    void* context,
    unsigned character_index,
    Glyph glyph,
    gfx::Vector2dF glyph_offset,
    float advance,
    bool is_horizontal,
    CanvasRotationInVertical rotation,
    const SimpleFontData* font_data) {
  GlyphCallbackContext* parsed_context =
      static_cast<GlyphCallbackContext*>(context);
  ShapeResultBloberizer* bloberizer = parsed_context->bloberizer;
  const StringView& text = parsed_context->text;

  if (bloberizer->IsSkipInkException(text, character_index))
    return;
  gfx::Vector2dF start_offset =
      is_horizontal ? gfx::Vector2dF(advance, 0) : gfx::Vector2dF(0, advance);
  bloberizer->Add(glyph, font_data, rotation, start_offset + glyph_offset,
                  character_index);
}

/*static*/ void ShapeResultBloberizer::AddFastHorizontalGlyphToBloberizer(
    void* context,
    unsigned character_index,
    Glyph glyph,
    gfx::Vector2dF glyph_offset,
    float advance,
    bool is_horizontal,
    CanvasRotationInVertical canvas_rotation,
    const SimpleFontData* font_data) {
  ShapeResultBloberizer* bloberizer =
      static_cast<ShapeResultBloberizer*>(context);
  DCHECK(!glyph_offset.y());
  DCHECK(is_horizontal);
  bloberizer->Add(glyph, font_data, canvas_rotation, advance + glyph_offset.x(),
                  character_index);
}

float ShapeResultBloberizer::FillGlyphsForResult(const ShapeResult* result,
                                                 const StringView& text,
                                                 unsigned from,
                                                 unsigned to,
                                                 float initial_advance,
                                                 unsigned run_offset) {
  GlyphCallbackContext context = {this, text};
  return result->ForEachGlyph(initial_advance, from, to, run_offset,
                              AddGlyphToBloberizer,
                              static_cast<void*>(&context));
}

namespace {
class ClusterCallbackContext {
  STACK_ALLOCATED();

 public:
  ClusterCallbackContext(ShapeResultBloberizer* bloberizer,
                         const StringView& text,
                         const GlyphData& emphasis_data,
                         gfx::PointF glyph_center)
      : bloberizer(bloberizer),
        text(text),
        emphasis_data(emphasis_data),
        glyph_center(std::move(glyph_center)) {}
  ClusterCallbackContext(const ClusterCallbackContext&) = delete;
  ClusterCallbackContext& operator=(const ClusterCallbackContext&) = delete;

  ShapeResultBloberizer* bloberizer;
  const StringView& text;
  const GlyphData& emphasis_data;
  gfx::PointF glyph_center;
};
}  // namespace

/*static*/ void ShapeResultBloberizer::AddEmphasisMarkToBloberizer(
    void* context,
    unsigned character_index,
    float advance_so_far,
    unsigned graphemes_in_cluster,
    float cluster_advance,
    CanvasRotationInVertical canvas_rotation) {
  ClusterCallbackContext* parsed_context =
      static_cast<ClusterCallbackContext*>(context);
  ShapeResultBloberizer* bloberizer = parsed_context->bloberizer;
  const StringView& text = parsed_context->text;
  const GlyphData& emphasis_data = parsed_context->emphasis_data;
  gfx::PointF glyph_center = parsed_context->glyph_center;

  if (text.Is8Bit()) {
    if (Character::CanReceiveTextEmphasis(text[character_index])) {
      bloberizer->AddEmphasisMark(emphasis_data, canvas_rotation, glyph_center,
                                  advance_so_far + cluster_advance / 2);
    }
  } else {
    float glyph_advance_x = cluster_advance / graphemes_in_cluster;
    for (unsigned j = 0; j < graphemes_in_cluster; ++j) {
      // Do not put emphasis marks on space, separator, and control
      // characters.
      if (Character::CanReceiveTextEmphasis(
              text.CodepointAt(character_index))) {
        bloberizer->AddEmphasisMark(emphasis_data, canvas_rotation,
                                    glyph_center,
                                    advance_so_far + glyph_advance_x / 2);
      }
      advance_so_far += glyph_advance_x;
    }
  }
}

namespace {
class ClusterStarts {
  STACK_ALLOCATED();

 public:
  ClusterStarts() = default;
  ClusterStarts(const ClusterStarts&) = delete;
  ClusterStarts& operator=(const ClusterStarts&) = delete;

  static void Accumulate(void* context,
                         unsigned character_index,
                         Glyph,
                         gfx::Vector2dF,
                         float,
                         bool,
                         CanvasRotationInVertical,
                         const SimpleFontData*) {
    ClusterStarts* self = static_cast<ClusterStarts*>(context);

    if (self->cluster_starts_.empty() ||
        self->last_seen_character_index_ != character_index) {
      self->cluster_starts_.push_back(character_index);
      self->last_seen_character_index_ = character_index;
    }
  }

  void Finish(unsigned from, unsigned to) {
    std::sort(cluster_starts_.begin(), cluster_starts_.end());
    DCHECK_EQ(std::ranges::adjacent_find(cluster_starts_),
              cluster_starts_.end());
    DVLOG(4) << "  Cluster starts: " << base::span(cluster_starts_);
    if (!cluster_starts_.empty()) {
      // 'from' may point inside a cluster; the least seen index may be larger.
      DCHECK_LE(from, *cluster_starts_.begin());
      DCHECK_LT(*(UNSAFE_TODO(cluster_starts_.end() - 1)), to);
    }
    cluster_starts_.push_back(to);
  }

  base::span<const unsigned> Data() { return cluster_starts_; }

 private:
  Vector<unsigned, 256> cluster_starts_;
  unsigned last_seen_character_index_ = 0;
};
}  // namespace

ShapeResultBloberizer::FillGlyphs::FillGlyphs(
    const FontDescription& font_description,
    const TextRunPaintInfo& run_info,
    const ShapeResultBuffer& result_buffer,
    const Type type)
    : ShapeResultBloberizer(font_description, type) {
  if (CanUseFastPath(run_info.from, run_info.to, run_info.run.length(),
                     result_buffer.HasVerticalOffsets())) {
    DVLOG(4) << "FillGlyphs fast path";
    DCHECK(!result_buffer.HasVerticalOffsets());
    DCHECK_NE(type_, ShapeResultBloberizer::Type::kTextIntercepts);
    DCHECK_NE(type_, ShapeResultBloberizer::Type::kEmitText);
    advance_ =
        FillFastHorizontalGlyphs(result_buffer, run_info.run.Direction());
    return;
  }

  DVLOG(4) << "FillGlyphs slow path";

  auto results = result_buffer.results_;
  FillGlyphsSlow(run_info.run.ToStringView(), run_info.run.Direction(), results,
                 run_info.from, run_info.to);
}

ShapeResultBloberizer::FillGlyphs::FillGlyphs(
    const FontDescription& font_description,
    const PlainTextNode& node,
    const Type type)
    : ShapeResultBloberizer(font_description, type) {
  DCHECK(!node.ContainsRtlItems());
  const unsigned to = node.TextContent().length();
  if (CanUseFastPath(0, to, to, node.HasVerticalOffsets())) {
    DVLOG(4) << "FillGlyphs fast path";
    DCHECK_NE(type_, ShapeResultBloberizer::Type::kTextIntercepts);
    DCHECK_NE(type_, ShapeResultBloberizer::Type::kEmitText);
    float advance = 0;
    for (const auto& item : node.ItemList()) {
      advance = FillFastHorizontalGlyphs(item.GetShapeResult(), advance);
    }
    advance_ = advance;
    return;
  }

  FillGlyphsSlow(node.TextContent(), node.BaseDirection(), node.ItemList(), 0,
                 to);
}

template <typename ShapeList>
void ShapeResultBloberizer::FillGlyphs::FillGlyphsSlow(StringView text,
                                                       TextDirection direction,
                                                       const ShapeList& list,
                                                       unsigned from,
                                                       unsigned to) {
  if (type_ == Type::kEmitText) [[unlikely]] {
    unsigned word_offset = 0;
    ClusterStarts cluster_starts;
    for (const auto& item : list) {
      const ShapeResult* word_result = GetShapeResult(item);
      word_result->ForEachGlyph(0, from, to, word_offset,
                                ClusterStarts::Accumulate,
                                static_cast<void*>(&cluster_starts));
      word_offset += word_result->NumCharacters();
    }
    cluster_starts.Finish(from, to);
    SetText(text, from, to, cluster_starts.Data());
  }

  float advance = 0;
  if (IsRtl(direction)) {
    unsigned word_offset = text.length();
    for (const auto& item : base::Reversed(list)) {
      const ShapeResult* word_result = GetShapeResult(item);
      unsigned word_characters = word_result->NumCharacters();
      word_offset -= word_characters;
      DVLOG(4) << " FillGlyphs RTL run from: " << from << " to: " << to
               << " offset: " << word_offset << " length: " << word_characters;
      advance = FillGlyphsForResult(word_result, text, from, to, advance,
                                    word_offset);
    }
  } else {
    unsigned word_offset = 0;
    for (const auto& item : list) {
      const ShapeResult* word_result = GetShapeResult(item);
      unsigned word_characters = word_result->NumCharacters();
      DVLOG(4) << " FillGlyphs LTR run from: " << from << " to: " << to
               << " offset: " << word_offset << " length: " << word_characters;
      advance = FillGlyphsForResult(word_result, text, from, to, advance,
                                    word_offset);
      word_offset += word_characters;
    }
  }

  if (type_ == Type::kEmitText) [[unlikely]] {
    CommitText();
  }

  advance_ = advance;
}

ShapeResultBloberizer::FillGlyphsNG::FillGlyphsNG(
    const FontDescription& font_description,
    const StringView& text,
    unsigned from,
    unsigned to,
    const ShapeResultView* result,
    const Type type)
    : ShapeResultBloberizer(font_description, type) {
  DCHECK(result);
  DCHECK(to <= text.length());
  float initial_advance = 0;
  if (CanUseFastPath(from, to, result)) {
    DVLOG(4) << "FillGlyphsNG fast path";
    DCHECK(!result->HasVerticalOffsets());
    DCHECK_NE(type_, ShapeResultBloberizer::Type::kTextIntercepts);
    DCHECK_NE(type_, ShapeResultBloberizer::Type::kEmitText);
    advance_ = result->ForEachGlyph(initial_advance,
                                    &AddFastHorizontalGlyphToBloberizer,
                                    static_cast<void*>(this));
    return;
  }

  DVLOG(4) << "FillGlyphsNG slow path";
  unsigned run_offset = 0;
  if (type_ == Type::kEmitText) [[unlikely]] {
    ClusterStarts cluster_starts;
    result->ForEachGlyph(initial_advance, from, to, run_offset,
                         ClusterStarts::Accumulate,
                         static_cast<void*>(&cluster_starts));
    cluster_starts.Finish(from, to);
    SetText(text, from, to, cluster_starts.Data());
  }

  GlyphCallbackContext context = {this, text};
  advance_ =
      result->ForEachGlyph(initial_advance, from, to, run_offset,
                           AddGlyphToBloberizer, static_cast<void*>(&context));

  if (type_ == Type::kEmitText) [[unlikely]] {
    CommitText();
  }
}

ShapeResultBloberizer::FillTextEmphasisGlyphsNG::FillTextEmphasisGlyphsNG(
    const FontDescription& font_description,
    const StringView& text,
    unsigned from,
    unsigned to,
    const ShapeResultView* result,
    const GlyphData& emphasis)
    : ShapeResultBloberizer(font_description, Type::kNormal) {
  gfx::PointF glyph_center =
      emphasis.font_data->BoundsForGlyph(emphasis.glyph).CenterPoint();
  ClusterCallbackContext context = {this, text, emphasis, glyph_center};
  float initial_advance = 0;
  unsigned index_offset = 0;
  advance_ = result->ForEachGraphemeClusters(
      text, initial_advance, from, to, index_offset,
      AddEmphasisMarkToBloberizer, static_cast<void*>(&context));
}

bool ShapeResultBloberizer::CanUseFastPath(unsigned from,
                                           unsigned to,
                                           unsigned length,
                                           bool has_vertical_offsets) {
  return !from && to == length && !has_vertical_offsets &&
         type_ != ShapeResultBloberizer::Type::kTextIntercepts &&
         type_ != ShapeResultBloberizer::Type::kEmitText;
}

bool ShapeResultBloberizer::CanUseFastPath(
    unsigned from,
    unsigned to,
    const ShapeResultView* shape_result) {
  return from <= shape_result->StartIndex() && to >= shape_result->EndIndex() &&
         !shape_result->HasVerticalOffsets() &&
         type_ != ShapeResultBloberizer::Type::kTextIntercepts &&
         type_ != ShapeResultBloberizer::Type::kEmitText;
}

float ShapeResultBloberizer::FillFastHorizontalGlyphs(
    const ShapeResultBuffer& result_buffer,
    TextDirection text_direction) {
  DCHECK(!result_buffer.HasVerticalOffsets());
  DCHECK_NE(type_, ShapeResultBloberizer::Type::kTextIntercepts);

  float advance = 0;
  auto results = result_buffer.results_;

  for (unsigned i = 0; i < results.size(); ++i) {
    const auto& word_result =
        IsLtr(text_direction) ? results[i] : results[results.size() - 1 - i];
    advance = FillFastHorizontalGlyphs(word_result.Get(), advance);
  }

  return advance;
}

float ShapeResultBloberizer::FillFastHorizontalGlyphs(const ShapeResult* result,
                                                      float initial_advance) {
  DCHECK(!result->HasVerticalOffsets());
  DCHECK_NE(type_, ShapeResultBloberizer::Type::kTextIntercepts);

  return result->ForEachGlyph(initial_advance,
                              &AddFastHorizontalGlyphToBloberizer,
                              static_cast<void*>(this));
}

void DrawTextBlobs(const ShapeResultBloberizer::BlobBuffer& blobs,
                   cc::PaintCanvas& canvas,
                   const gfx::PointF& point,
                   const cc::PaintFlags& flags,
                   cc::NodeId node_id) {
  for (const auto& blob_info : blobs) {
    DCHECK(blob_info.blob);
    cc::PaintCanvasAutoRestore auto_restore(&canvas, false);
    switch (blob_info.rotation) {
      case CanvasRotationInVertical::kRegular:
        break;
      case CanvasRotationInVertical::kRotateCanvasUpright: {
        canvas.save();

        SkMatrix m;
        m.setSinCos(-1, 0, point.x(), point.y());
        canvas.concat(SkM44(m));
        break;
      }
      case CanvasRotationInVertical::kRotateCanvasUprightOblique: {
        canvas.save();

        SkMatrix m;
        m.setSinCos(-1, 0, point.x(), point.y());
        // TODO(yosin): We should use angle specified in CSS instead of
        // constant value -15deg.
        // Note: We draw glyph in right-top corner upper.
        // See CSS "transform: skew(0, -15deg)"
        SkMatrix skew_y;
        constexpr SkScalar kSkewY = -0.2679491924311227;  // tan(-15deg)
        skew_y.setSkew(0, kSkewY, point.x(), point.y());
        m.preConcat(skew_y);
        canvas.concat(SkM44(m));
        break;
      }
      case CanvasRotationInVertical::kOblique: {
        // TODO(yosin): We should use angle specified in CSS instead of
        // constant value 15deg.
        // Note: We draw glyph in right-top corner upper.
        // See CSS "transform: skew(0, -15deg)"
        canvas.save();
        SkMatrix skew_x;
        constexpr SkScalar kSkewX = 0.2679491924311227;  // tan(15deg)
        skew_x.setSkew(kSkewX, 0, point.x(), point.y());
        canvas.concat(SkM44(skew_x));
        break;
      }
    }
    if (node_id != cc::kInvalidNodeId) {
      canvas.drawTextBlob(blob_info.blob, point.x(), point.y(), node_id, flags);
    } else {
      canvas.drawTextBlob(blob_info.blob, point.x(), point.y(), flags);
    }
  }
}

}  // namespace blink