summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/fonts/shaping/shape_result_bloberizer_test.cc
blob: 9cf817a98e2930088b893f79c2b3cb75d7df8465 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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 <memory>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/renderer/platform/fonts/character_range.h"
#include "third_party/blink/renderer/platform/fonts/font.h"
#include "third_party/blink/renderer/platform/fonts/ng_text_fragment_paint_info.h"
#include "third_party/blink/renderer/platform/fonts/opentype/open_type_vertical_data.h"
#include "third_party/blink/renderer/platform/fonts/shaping/caching_word_shaper.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_test_info.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_view.h"
#include "third_party/blink/renderer/platform/fonts/simple_font_data.h"
#include "third_party/blink/renderer/platform/fonts/text_run_paint_info.h"

namespace blink {

namespace {

// Creating minimal test SimpleFontData objects,
// the font won't have any glyphs, but that's okay.
static scoped_refptr<SimpleFontData> CreateTestSimpleFontData(
    bool force_rotation = false) {
  FontPlatformData platform_data(
      SkTypeface::MakeDefault(), std::string(), 10, false, false,
      force_rotation ? FontOrientation::kVerticalUpright
                     : FontOrientation::kHorizontal);
  return SimpleFontData::Create(platform_data, nullptr);
}

class ShapeResultBloberizerTest : public testing::Test {
 protected:
  void SetUp() override {
    font_description.SetComputedSize(12.0);
    font_description.SetLocale(LayoutLocale::Get("en"));
    ASSERT_EQ(USCRIPT_LATIN, font_description.GetScript());
    font_description.SetGenericFamily(FontDescription::kStandardFamily);

    font = Font(font_description);
    ASSERT_TRUE(font.CanShapeWordByWord());
    fallback_fonts = nullptr;
    cache = std::make_unique<ShapeCache>();
  }

  FontCachePurgePreventer font_cache_purge_preventer;
  FontDescription font_description;
  Font font;
  std::unique_ptr<ShapeCache> cache;
  HashSet<const SimpleFontData*>* fallback_fonts;
  unsigned start_index = 0;
  unsigned num_glyphs = 0;
  hb_script_t script = HB_SCRIPT_INVALID;
};

struct ExpectedRun {
  int glyph_count;
  std::string utf8;
  // Currently RTL is output in reverse of logical order, but this is not
  // a requirement. This really just expects montonicity.
  enum ClusterDirection { Ascending, Descending } cluster_direction;
};
using ExpectedBlob = std::vector<const ExpectedRun>;

struct ExpectedRange {
  unsigned from;
  unsigned to;
  unsigned length() { return to - from; }
};

void CheckBlobBuffer(const ShapeResultBloberizer::BlobBuffer& blob_buffer,
                     const std::vector<const ExpectedBlob>& expected_blobs) {
  EXPECT_EQ(blob_buffer.size(), expected_blobs.size());
  const ShapeResultBloberizer::BlobInfo* blob_info_iter = blob_buffer.begin();
  auto&& expected_blob_iter = expected_blobs.begin();
  for (; blob_info_iter != blob_buffer.end() &&
         expected_blob_iter != expected_blobs.end();
       ++blob_info_iter, ++expected_blob_iter) {
    size_t blob_index = expected_blob_iter - expected_blobs.begin();
    const ExpectedBlob& expected_blob = *expected_blob_iter;
    SkTextBlob::Iter::Run run;
    size_t actual_run_count = 0;
    for (SkTextBlob::Iter it(*blob_info_iter->blob.get()); it.next(&run);) {
      ++actual_run_count;
    }
    EXPECT_EQ(actual_run_count, expected_blob.size()) << "Blob: " << blob_index;
    auto&& expected_run_iter = expected_blob.begin();
    SkTextBlob::Iter it(*blob_info_iter->blob.get());
    for (; it.next(&run) && expected_run_iter != expected_blob.end();
         ++expected_run_iter) {
      size_t run_index = expected_run_iter - expected_blob.begin();
      const ExpectedRun& expected_run = *expected_run_iter;
      EXPECT_EQ(expected_run.glyph_count, run.fGlyphCount)
          << "Blob: " << blob_index << " Run: " << run_index;

      int actual_size = run.fUtf8Size_forTest;
      int expected_size = expected_run.utf8.size();
      EXPECT_EQ(actual_size, expected_size)
          << "Blob: " << blob_index << " Run: " << run_index;
      for (int i = 0; i < actual_size && i < expected_size; ++i) {
        EXPECT_EQ(run.fUtf8_forTest[i], expected_run.utf8[i])
            << "Blob: " << blob_index << " Run: " << run_index << " i: " << i;
      }

      auto utf8_index_previous = run.fClusterIndex_forTest[0];
      for (int i = 0; i < run.fGlyphCount; ++i) {
        EXPECT_LE(0ul, run.fClusterIndex_forTest[i]);
        EXPECT_LT((int)run.fClusterIndex_forTest[i], run.fUtf8Size_forTest);
        auto expected_direction = expected_run.cluster_direction;
        if (expected_direction == ExpectedRun::ClusterDirection::Ascending) {
          EXPECT_LE(utf8_index_previous, run.fClusterIndex_forTest[i]);
        } else {
          EXPECT_GE(utf8_index_previous, run.fClusterIndex_forTest[i]);
        }
        utf8_index_previous = run.fClusterIndex_forTest[i];
      }
    }
  }
}

}  // anonymous namespace

TEST_F(ShapeResultBloberizerTest, StartsEmpty) {
  Font font;
  ShapeResultBloberizer bloberizer(font.GetFontDescription(), 1,
                                   ShapeResultBloberizer::Type::kNormal);

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingRunFontData(bloberizer),
            nullptr);
  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer).size(),
            0ul);
  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingRunOffsets(bloberizer).size(),
            0ul);
  EXPECT_FALSE(
      ShapeResultBloberizerTestInfo::HasPendingRunVerticalOffsets(bloberizer));
  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingBlobRunCount(bloberizer),
            0ul);
  EXPECT_EQ(ShapeResultBloberizerTestInfo::CommittedBlobCount(bloberizer), 0ul);

  EXPECT_TRUE(bloberizer.Blobs().IsEmpty());
}

TEST_F(ShapeResultBloberizerTest, StoresGlyphsOffsets) {
  Font font;
  ShapeResultBloberizer bloberizer(font.GetFontDescription(), 1,
                                   ShapeResultBloberizer::Type::kNormal);

  scoped_refptr<SimpleFontData> font1 = CreateTestSimpleFontData();
  scoped_refptr<SimpleFontData> font2 = CreateTestSimpleFontData();

  // 2 pending glyphs
  ShapeResultBloberizerTestInfo::Add(bloberizer, 42, font1.get(),
                                     CanvasRotationInVertical::kRegular, 10, 0);
  ShapeResultBloberizerTestInfo::Add(bloberizer, 43, font1.get(),
                                     CanvasRotationInVertical::kRegular, 15, 1);

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingRunFontData(bloberizer),
            font1.get());
  EXPECT_FALSE(
      ShapeResultBloberizerTestInfo::HasPendingRunVerticalOffsets(bloberizer));
  {
    const auto& glyphs =
        ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer);
    EXPECT_EQ(glyphs.size(), 2ul);
    EXPECT_EQ(42, glyphs[0]);
    EXPECT_EQ(43, glyphs[1]);

    const auto& offsets =
        ShapeResultBloberizerTestInfo::PendingRunOffsets(bloberizer);
    EXPECT_EQ(offsets.size(), 2ul);
    EXPECT_EQ(10, offsets[0]);
    EXPECT_EQ(15, offsets[1]);
  }

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingBlobRunCount(bloberizer),
            0ul);
  EXPECT_EQ(ShapeResultBloberizerTestInfo::CommittedBlobCount(bloberizer), 0ul);

  // one more glyph, different font => pending run flush
  ShapeResultBloberizerTestInfo::Add(bloberizer, 44, font2.get(),
                                     CanvasRotationInVertical::kRegular, 12, 0);

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingRunFontData(bloberizer),
            font2.get());
  EXPECT_FALSE(
      ShapeResultBloberizerTestInfo::HasPendingRunVerticalOffsets(bloberizer));
  {
    const auto& glyphs =
        ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer);
    EXPECT_EQ(glyphs.size(), 1ul);
    EXPECT_EQ(44, glyphs[0]);

    const auto& offsets =
        ShapeResultBloberizerTestInfo::PendingRunOffsets(bloberizer);
    EXPECT_EQ(offsets.size(), 1ul);
    EXPECT_EQ(12, offsets[0]);
  }

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingBlobRunCount(bloberizer),
            1ul);
  EXPECT_EQ(ShapeResultBloberizerTestInfo::CommittedBlobCount(bloberizer), 0ul);

  // flush everything (1 blob w/ 2 runs)
  EXPECT_EQ(bloberizer.Blobs().size(), 1ul);
}

TEST_F(ShapeResultBloberizerTest, StoresGlyphsVerticalOffsets) {
  Font font;
  ShapeResultBloberizer bloberizer(font.GetFontDescription(), 1,
                                   ShapeResultBloberizer::Type::kNormal);

  scoped_refptr<SimpleFontData> font1 = CreateTestSimpleFontData();
  scoped_refptr<SimpleFontData> font2 = CreateTestSimpleFontData();

  // 2 pending glyphs
  ShapeResultBloberizerTestInfo::Add(bloberizer, 42, font1.get(),
                                     CanvasRotationInVertical::kRegular,
                                     FloatPoint(10, 0), 0);
  ShapeResultBloberizerTestInfo::Add(bloberizer, 43, font1.get(),
                                     CanvasRotationInVertical::kRegular,
                                     FloatPoint(15, 0), 1);

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingRunFontData(bloberizer),
            font1.get());
  EXPECT_TRUE(
      ShapeResultBloberizerTestInfo::HasPendingRunVerticalOffsets(bloberizer));
  {
    const auto& glyphs =
        ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer);
    EXPECT_EQ(glyphs.size(), 2ul);
    EXPECT_EQ(42, glyphs[0]);
    EXPECT_EQ(43, glyphs[1]);

    const auto& offsets =
        ShapeResultBloberizerTestInfo::PendingRunOffsets(bloberizer);
    EXPECT_EQ(offsets.size(), 4ul);
    EXPECT_EQ(10, offsets[0]);
    EXPECT_EQ(0, offsets[1]);
    EXPECT_EQ(15, offsets[2]);
    EXPECT_EQ(0, offsets[3]);
  }

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingBlobRunCount(bloberizer),
            0ul);
  EXPECT_EQ(ShapeResultBloberizerTestInfo::CommittedBlobCount(bloberizer), 0ul);

  // one more glyph, different font => pending run flush
  ShapeResultBloberizerTestInfo::Add(bloberizer, 44, font2.get(),
                                     CanvasRotationInVertical::kRegular,
                                     FloatPoint(12, 2), 2);

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingRunFontData(bloberizer),
            font2.get());
  EXPECT_TRUE(
      ShapeResultBloberizerTestInfo::HasPendingRunVerticalOffsets(bloberizer));
  {
    const auto& glyphs =
        ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer);
    EXPECT_EQ(glyphs.size(), 1ul);
    EXPECT_EQ(44, glyphs[0]);

    const auto& offsets =
        ShapeResultBloberizerTestInfo::PendingRunOffsets(bloberizer);
    EXPECT_EQ(offsets.size(), 2ul);
    EXPECT_EQ(12, offsets[0]);
    EXPECT_EQ(2, offsets[1]);
  }

  EXPECT_EQ(ShapeResultBloberizerTestInfo::PendingBlobRunCount(bloberizer),
            1ul);
  EXPECT_EQ(ShapeResultBloberizerTestInfo::CommittedBlobCount(bloberizer), 0ul);

  // flush everything (1 blob w/ 2 runs)
  EXPECT_EQ(bloberizer.Blobs().size(), 1ul);
}

TEST_F(ShapeResultBloberizerTest, MixedBlobRotation) {
  Font font;
  ShapeResultBloberizer bloberizer(font.GetFontDescription(), 1,
                                   ShapeResultBloberizer::Type::kNormal);

  scoped_refptr<SimpleFontData> test_font = CreateTestSimpleFontData();

  struct {
    CanvasRotationInVertical canvas_rotation;
    size_t expected_pending_glyphs;
    size_t expected_pending_runs;
    size_t expected_committed_blobs;
  } append_ops[] = {
      // append 2 horizontal glyphs -> these go into the pending glyph buffer
      {CanvasRotationInVertical::kRegular, 1u, 0u, 0u},
      {CanvasRotationInVertical::kRegular, 2u, 0u, 0u},

      // append 3 vertical rotated glyphs -> push the prev pending (horizontal)
      // glyphs into a new run in the current (horizontal) blob
      {CanvasRotationInVertical::kRotateCanvasUpright, 1u, 1u, 0u},
      {CanvasRotationInVertical::kRotateCanvasUpright, 2u, 1u, 0u},
      {CanvasRotationInVertical::kRotateCanvasUpright, 3u, 1u, 0u},

      // append 2 more horizontal glyphs -> flush the current (horizontal) blob,
      // push prev (vertical) pending glyphs into new vertical blob run
      {CanvasRotationInVertical::kRegular, 1u, 1u, 1u},
      {CanvasRotationInVertical::kRegular, 2u, 1u, 1u},

      // append 1 more vertical glyph -> flush current (vertical) blob, push
      // prev (horizontal) pending glyphs into a new horizontal blob run
      {CanvasRotationInVertical::kRotateCanvasUpright, 1u, 1u, 2u},
  };

  for (const auto& op : append_ops) {
    ShapeResultBloberizerTestInfo::Add(bloberizer, 42, test_font.get(),
                                       op.canvas_rotation, FloatPoint(), 0);
    EXPECT_EQ(
        op.expected_pending_glyphs,
        ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer).size());
    EXPECT_EQ(op.canvas_rotation,
              ShapeResultBloberizerTestInfo::PendingBlobRotation(bloberizer));
    EXPECT_EQ(op.expected_pending_runs,
              ShapeResultBloberizerTestInfo::PendingBlobRunCount(bloberizer));
    EXPECT_EQ(op.expected_committed_blobs,
              ShapeResultBloberizerTestInfo::CommittedBlobCount(bloberizer));
  }

  // flush everything -> 4 blobs total
  EXPECT_EQ(4u, bloberizer.Blobs().size());
}

// Tests that filling a glyph buffer for a specific range returns the same
// results when shaping word by word as when shaping the full run in one go.
TEST_F(ShapeResultBloberizerTest, CommonAccentLeftToRightFillGlyphBuffer) {
  // "/. ." with an accent mark over the first dot.
  const UChar kStr[] = {0x2F, 0x301, 0x2E, 0x20, 0x2E};
  TextRun text_run(kStr, base::make_span(kStr).size());
  TextRunPaintInfo run_info(text_run);
  run_info.to = 3;

  CachingWordShaper word_shaper(font);
  ShapeResultBuffer buffer;
  word_shaper.FillResultBuffer(run_info, &buffer);
  ShapeResultBloberizer::FillGlyphs bloberizer(
      font.GetFontDescription(), 1.0f, run_info, buffer,
      ShapeResultBloberizer::Type::kEmitText);

  Font reference_font(font_description);
  reference_font.SetCanShapeWordByWordForTesting(false);

  CachingWordShaper reference_word_shaper(reference_font);
  ShapeResultBuffer reference_buffer;
  reference_word_shaper.FillResultBuffer(run_info, &reference_buffer);
  ShapeResultBloberizer::FillGlyphs reference_bloberizer(
      reference_font.GetFontDescription(), 1.0f, run_info, reference_buffer,
      ShapeResultBloberizer::Type::kEmitText);

  const auto& glyphs =
      ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer);
  ASSERT_EQ(glyphs.size(), 3ul);
  const auto reference_glyphs =
      ShapeResultBloberizerTestInfo::PendingRunGlyphs(reference_bloberizer);
  ASSERT_EQ(reference_glyphs.size(), 3ul);

  EXPECT_EQ(reference_glyphs[0], glyphs[0]);
  EXPECT_EQ(reference_glyphs[1], glyphs[1]);
  EXPECT_EQ(reference_glyphs[2], glyphs[2]);

  CheckBlobBuffer(
      bloberizer.Blobs(),
      {{
          {3,
           text_run.ToStringView()
               .ToString()
               .Substring(run_info.from, run_info.to - run_info.from)
               .Utf8(),
           ExpectedRun::ClusterDirection::Ascending},
      }});
}

// Tests that filling a glyph buffer for a specific range returns the same
// results when shaping word by word as when shaping the full run in one go.
TEST_F(ShapeResultBloberizerTest, CommonAccentRightToLeftFillGlyphBuffer) {
  // "[] []" with an accent mark over the last square bracket.
  const UChar kStr[] = {0x5B, 0x5D, 0x20, 0x5B, 0x301, 0x5D};
  TextRun text_run(kStr, base::make_span(kStr).size());
  text_run.SetDirection(TextDirection::kRtl);
  TextRunPaintInfo run_info(text_run);
  run_info.from = 1;

  CachingWordShaper word_shaper(font);
  ShapeResultBuffer buffer;
  word_shaper.FillResultBuffer(run_info, &buffer);
  ShapeResultBloberizer::FillGlyphs bloberizer(
      font.GetFontDescription(), 1.0f, run_info, buffer,
      ShapeResultBloberizer::Type::kEmitText);

  Font reference_font(font_description);
  reference_font.SetCanShapeWordByWordForTesting(false);

  CachingWordShaper reference_word_shaper(reference_font);
  ShapeResultBuffer reference_buffer;
  reference_word_shaper.FillResultBuffer(run_info, &reference_buffer);
  ShapeResultBloberizer::FillGlyphs reference_bloberizer(
      reference_font.GetFontDescription(), 1.0f, run_info, reference_buffer,
      ShapeResultBloberizer::Type::kEmitText);

  const auto& glyphs =
      ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer);
  ASSERT_EQ(5u, glyphs.size());
  const auto reference_glyphs =
      ShapeResultBloberizerTestInfo::PendingRunGlyphs(reference_bloberizer);
  ASSERT_EQ(5u, reference_glyphs.size());

  EXPECT_EQ(reference_glyphs[0], glyphs[0]);
  EXPECT_EQ(reference_glyphs[1], glyphs[1]);
  EXPECT_EQ(reference_glyphs[2], glyphs[2]);
  EXPECT_EQ(reference_glyphs[3], glyphs[3]);
  EXPECT_EQ(reference_glyphs[4], glyphs[4]);
}

TEST_F(ShapeResultBloberizerTest, CommonAccentRightToLeftFillGlyphBufferNG) {
  // "[] []" with an accent mark over the last square bracket.
  const UChar kStr[] = {0x5B, 0x5D, 0x20, 0x5B, 0x301, 0x5D};
  String string(kStr, base::make_span(kStr).size());

  HarfBuzzShaper shaper(string);
  scoped_refptr<ShapeResult> result = shaper.Shape(&font, TextDirection::kRtl);

  scoped_refptr<ShapeResultView> result_view =
      ShapeResultView::Create(result.get());
  NGTextFragmentPaintInfo text_info{StringView(string), 1, string.length(),
                                    result_view.get()};
  ShapeResultBloberizer::FillGlyphsNG bloberizer_ng(
      font.GetFontDescription(), 1.0f, text_info.text, text_info.from,
      text_info.to, text_info.shape_result,
      ShapeResultBloberizer::Type::kEmitText);

  CheckBlobBuffer(
      bloberizer_ng.Blobs(),
      {{
          {5,
           string.Substring(text_info.from, text_info.to - text_info.from)
               .Utf8(),
           ExpectedRun::ClusterDirection::Descending},
      }});
}

TEST_F(ShapeResultBloberizerTest, FourByteUtf8CodepointsNG) {
  // Codepoints which encode to four UTF-8 code units.
  const UChar kStr[] = {0xD841, 0xDF31, 0xD841, 0xDF79};
  String string(kStr, base::make_span(kStr).size());

  HarfBuzzShaper shaper(string);
  scoped_refptr<ShapeResult> result = shaper.Shape(&font, TextDirection::kLtr);

  scoped_refptr<ShapeResultView> result_view =
      ShapeResultView::Create(result.get());
  NGTextFragmentPaintInfo text_info{StringView(string), 0, string.length(),
                                    result_view.get()};
  ShapeResultBloberizer::FillGlyphsNG bloberizer_ng(
      font.GetFontDescription(), 1.0f, text_info.text, text_info.from,
      text_info.to, text_info.shape_result,
      ShapeResultBloberizer::Type::kEmitText);

  CheckBlobBuffer(
      bloberizer_ng.Blobs(),
      {{
          {2,
           string.Substring(text_info.from, text_info.to - text_info.from)
               .Utf8(),
           ExpectedRun::ClusterDirection::Ascending},
      }});
}

TEST_F(ShapeResultBloberizerTest, OffsetIntoTrailingSurrogateNG) {
  // Codepoints which encode to four UTF-8 code units.
  const UChar kStr[] = {0xD841, 0xDF31, 0xD841, 0xDF79};
  String string(kStr, base::make_span(kStr).size());

  HarfBuzzShaper shaper(string);
  scoped_refptr<ShapeResult> result = shaper.Shape(&font, TextDirection::kLtr);

  scoped_refptr<ShapeResultView> result_view =
      ShapeResultView::Create(result.get());
  // Start at offset 1 into text at trailing surrogate.
  NGTextFragmentPaintInfo text_info{StringView(string), 1, string.length(),
                                    result_view.get()};
  ShapeResultBloberizer::FillGlyphsNG bloberizer_ng(
      font.GetFontDescription(), 1.0f, text_info.text, text_info.from,
      text_info.to, text_info.shape_result,
      ShapeResultBloberizer::Type::kEmitText);

  // Do not expect the trailing surrogate to be in any output.
  const auto& glyphs =
      ShapeResultBloberizerTestInfo::PendingRunGlyphs(bloberizer_ng);
  EXPECT_EQ(1u, glyphs.size());

  CheckBlobBuffer(
      bloberizer_ng.Blobs(),
      {{
          {1,
           string
               .Substring(text_info.from + 1, text_info.to - text_info.from - 1)
               .Utf8(),
           ExpectedRun::ClusterDirection::Ascending},
      }});
}

TEST_F(ShapeResultBloberizerTest, LatinMultRunNG) {
  TextDirection direction = TextDirection::kLtr;
  String string = "Testing ShapeResultIterator::CopyRange";

  ExpectedRange range_a{0, 5};
  ExpectedRange range_b{5, 7};
  ExpectedRange range_c{7, 32};
  ExpectedRange range_d{32, 38};
  HarfBuzzShaper shaper_a(string.Substring(range_a.from, range_a.to));
  HarfBuzzShaper shaper_b(string.Substring(range_b.from, range_b.to));
  HarfBuzzShaper shaper_c(string.Substring(range_c.from, range_c.to));
  HarfBuzzShaper shaper_d(string.Substring(range_d.from, range_d.to));

  FontDescription font2_description(font_description);
  font2_description.SetComputedSize(20);
  Font font2(font2_description);

  // Combine four separate results into a single one to ensure we have a result
  // with multiple runs. Interleave fonts to ensure run changes.
  scoped_refptr<ShapeResult> result =
      ShapeResult::Create(&font, 0, 0, direction);
  shaper_a.Shape(&font, direction)
      ->CopyRange(0u, range_a.length(), result.get());
  shaper_b.Shape(&font2, direction)
      ->CopyRange(0u, range_b.length(), result.get());
  shaper_c.Shape(&font, direction)
      ->CopyRange(0u, range_c.length(), result.get());
  shaper_d.Shape(&font2, direction)
      ->CopyRange(0u, range_d.length(), result.get());

  scoped_refptr<ShapeResultView> result_view =
      ShapeResultView::Create(result.get());
  NGTextFragmentPaintInfo text_info{StringView(string), 1, string.length(),
                                    result_view.get()};
  ShapeResultBloberizer::FillGlyphsNG bloberizer_ng(
      font.GetFontDescription(), 1.0f, text_info.text, text_info.from,
      text_info.to, text_info.shape_result,
      ShapeResultBloberizer::Type::kEmitText);

  CheckBlobBuffer(
      bloberizer_ng.Blobs(),
      {{
          // "Testi"
          {static_cast<int>(range_a.length() - 1),
           string.Substring(range_a.from + 1, range_a.length() - 1).Utf8(),
           ExpectedRun::ClusterDirection::Ascending},
          // "ng"
          {static_cast<int>(range_b.length()),
           string.Substring(range_b.from, range_b.length()).Utf8(),
           ExpectedRun::ClusterDirection::Ascending},
          // " ShapeResultIterator::Cop"
          {static_cast<int>(range_c.length()),
           string.Substring(range_c.from, range_c.length()).Utf8(),
           ExpectedRun::ClusterDirection::Ascending},
          // "yRange"
          {static_cast<int>(range_d.length()),
           string.Substring(range_d.from, range_d.length()).Utf8(),
           ExpectedRun::ClusterDirection::Ascending},
      }});
}

TEST_F(ShapeResultBloberizerTest, SupplementaryMultiRunNG) {
  TextDirection direction = TextDirection::kLtr;
  // 𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕
  const UChar kStrSupp[] = {0xD841, 0xDF0E, 0xD841, 0xDF31, 0xD841, 0xDF79,
                            0xD843, 0xDC53, 0xD843, 0xDC78, 0xD843, 0xDC96,
                            0xD843, 0xDCCF, 0xD843, 0xDCD5};
  String string(kStrSupp, base::make_span(kStrSupp).size());

  ExpectedRange range_a{0, 6};
  ExpectedRange range_b{6, 12};
  ExpectedRange range_c{12, 16};
  HarfBuzzShaper shaper_a(string.Substring(range_a.from, range_a.to));
  HarfBuzzShaper shaper_b(string.Substring(range_b.from, range_b.to));
  HarfBuzzShaper shaper_c(string.Substring(range_c.from, range_c.to));

  FontDescription font2_description(font_description);
  font2_description.SetComputedSize(20);
  Font font2(font2_description);

  // Combine four separate results into a single one to ensure we have a result
  // with multiple runs. Interleave fonts to ensure run changes.
  scoped_refptr<ShapeResult> result =
      ShapeResult::Create(&font, 0, 0, direction);
  shaper_a.Shape(&font, direction)
      ->CopyRange(0u, range_a.length(), result.get());
  shaper_b.Shape(&font2, direction)
      ->CopyRange(0u, range_b.length(), result.get());
  shaper_c.Shape(&font, direction)
      ->CopyRange(0u, range_c.length(), result.get());

  scoped_refptr<ShapeResultView> result_view =
      ShapeResultView::Create(result.get());
  NGTextFragmentPaintInfo text_info{StringView(string), 0, string.length(),
                                    result_view.get()};
  ShapeResultBloberizer::FillGlyphsNG bloberizer_ng(
      font.GetFontDescription(), 1.0f, text_info.text, text_info.from,
      text_info.to, text_info.shape_result,
      ShapeResultBloberizer::Type::kEmitText);

  CheckBlobBuffer(bloberizer_ng.Blobs(),
                  {{
                      // "𠜎𠜱𠝹"
                      {static_cast<int>(range_a.length() / 2),
                       string.Substring(range_a.from, range_a.length()).Utf8(),
                       ExpectedRun::ClusterDirection::Ascending},
                      // "𠱓𠱸𠲖"
                      {static_cast<int>(range_b.length() / 2),
                       string.Substring(range_b.from, range_b.length()).Utf8(),
                       ExpectedRun::ClusterDirection::Ascending},
                      // "𠳏𠳕"
                      {static_cast<int>(range_c.length() / 2),
                       string.Substring(range_c.from, range_c.length()).Utf8(),
                       ExpectedRun::ClusterDirection::Ascending},
                  }});
}

// Tests that runs with zero glyphs (the ZWJ non-printable character in this
// case) are handled correctly. This test passes if it does not cause a crash.
TEST_F(ShapeResultBloberizerTest, SubRunWithZeroGlyphs) {
  // "Foo &zwnj; bar"
  const UChar kStr[] = {0x46, 0x6F, 0x6F, 0x20, 0x200C, 0x20, 0x62, 0x61, 0x71};
  TextRun text_run(kStr, base::make_span(kStr).size());

  CachingWordShaper shaper(font);
  FloatRect glyph_bounds;
  ASSERT_GT(shaper.Width(text_run, nullptr, &glyph_bounds), 0);

  TextRunPaintInfo run_info(text_run);
  run_info.to = 8;

  CachingWordShaper word_shaper(font);
  ShapeResultBuffer buffer;
  word_shaper.FillResultBuffer(run_info, &buffer);
  ShapeResultBloberizer::FillGlyphs bloberizer(
      font.GetFontDescription(), 1.0f, run_info, buffer,
      ShapeResultBloberizer::Type::kEmitText);

  shaper.GetCharacterRange(text_run, 0, 8);
}

}  // namespace blink