paragraph_unittests.cc 58.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2017 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
#include "flutter/fml/logging.h"
18 19 20
#include "render_test.h"
#include "third_party/icu/source/common/unicode/unistr.h"
#include "third_party/skia/include/core/SkColor.h"
21 22 23 24
#include "txt/font_style.h"
#include "txt/font_weight.h"
#include "txt/paragraph.h"
#include "txt/paragraph_builder.h"
25
#include "txt_test_utils.h"
26

27 28
#define DISABLE_ON_WINDOWS(TEST) DISABLE_TEST_WINDOWS(TEST)

29 30
namespace txt {

31 32 33
using ParagraphTest = RenderTest;

TEST_F(ParagraphTest, SimpleParagraph) {
34
  const char* text = "Hello World Text Dialog";
35 36 37 38 39
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
40
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
41 42

  txt::TextStyle text_style;
43
  text_style.font_family = "Roboto";
44 45
  text_style.color = SK_ColorBLACK;
  builder.PushStyle(text_style);
46 47 48 49 50
  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
51
  paragraph->Layout(GetTestCanvasWidth());
52

53
  paragraph->Paint(GetCanvas(), 10.0, 15.0);
54

55 56 57 58
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
59
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
60 61
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
G
Gary Qian 已提交
62
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
63 64 65
  ASSERT_TRUE(Snapshot());
}

66
TEST_F(ParagraphTest, SimpleRedParagraph) {
67 68 69 70 71 72
  const char* text = "I am RED";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
73
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
74 75

  txt::TextStyle text_style;
76
  text_style.font_family = "Roboto";
77 78
  text_style.color = SK_ColorRED;
  builder.PushStyle(text_style);
79 80 81 82 83 84

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
85
  paragraph->Layout(GetTestCanvasWidth());
86

87
  paragraph->Paint(GetCanvas(), 10.0, 15.0);
88

89 90 91 92
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
93
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
94 95
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
G
Gary Qian 已提交
96
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
97 98
  ASSERT_TRUE(Snapshot());
}
99

100
TEST_F(ParagraphTest, RainbowParagraph) {
101
  const char* text1 = "Red Roboto";
102 103 104
  auto icu_text1 = icu::UnicodeString::fromUTF8(text1);
  std::u16string u16_text1(icu_text1.getBuffer(),
                           icu_text1.getBuffer() + icu_text1.length());
105
  const char* text2 = "big Greeen Default";
106 107 108
  auto icu_text2 = icu::UnicodeString::fromUTF8(text2);
  std::u16string u16_text2(icu_text2.getBuffer(),
                           icu_text2.getBuffer() + icu_text2.length());
109
  const char* text3 = "Defcolor Homemade Apple";
110 111 112
  auto icu_text3 = icu::UnicodeString::fromUTF8(text3);
  std::u16string u16_text3(icu_text3.getBuffer(),
                           icu_text3.getBuffer() + icu_text3.length());
113
  const char* text4 = "Small Blue Roboto";
114 115 116
  auto icu_text4 = icu::UnicodeString::fromUTF8(text4);
  std::u16string u16_text4(icu_text4.getBuffer(),
                           icu_text4.getBuffer() + icu_text4.length());
117
  const char* text5 =
118 119
      "Continue Last Style With lots of words to check if it overlaps "
      "properly or not";
120 121 122 123 124
  auto icu_text5 = icu::UnicodeString::fromUTF8(text5);
  std::u16string u16_text5(icu_text5.getBuffer(),
                           icu_text5.getBuffer() + icu_text5.length());

  txt::ParagraphStyle paragraph_style;
125
  paragraph_style.max_lines = 2;
126
  paragraph_style.text_align = TextAlign::left;
127
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
128 129 130 131 132 133 134 135 136

  txt::TextStyle text_style1;
  text_style1.color = SK_ColorRED;
  text_style1.font_family = "Roboto";
  builder.PushStyle(text_style1);

  builder.AddText(u16_text1);

  txt::TextStyle text_style2;
137
  text_style2.font_size = 50;
138 139
  text_style2.letter_spacing = 10;
  text_style2.word_spacing = 30;
140
  text_style2.font_weight = txt::FontWeight::w600;
141
  text_style2.color = SK_ColorGREEN;
142
  text_style2.font_family = "Roboto";
143 144 145
  text_style2.decoration = TextDecoration::kUnderline |
                           TextDecoration::kOverline |
                           TextDecoration::kLineThrough;
146
  text_style2.decoration_color = SK_ColorBLACK;
147 148 149 150 151 152 153 154 155 156 157
  builder.PushStyle(text_style2);

  builder.AddText(u16_text2);

  txt::TextStyle text_style3;
  text_style3.font_family = "Homemade Apple";
  builder.PushStyle(text_style3);

  builder.AddText(u16_text3);

  txt::TextStyle text_style4;
158
  text_style4.font_size = 14;
159 160
  text_style4.color = SK_ColorBLUE;
  text_style4.font_family = "Roboto";
161 162 163
  text_style4.decoration = TextDecoration::kUnderline |
                           TextDecoration::kOverline |
                           TextDecoration::kLineThrough;
164
  text_style4.decoration_color = SK_ColorBLACK;
165 166 167 168
  builder.PushStyle(text_style4);

  builder.AddText(u16_text4);

169 170
  // Extra text to see if it goes to default when there is more text chunks than
  // styles.
171 172 173 174 175
  builder.AddText(u16_text5);

  builder.Pop();

  auto paragraph = builder.Build();
176
  paragraph->Layout(GetTestCanvasWidth());
177

178
  paragraph->Paint(GetCanvas(), 0, 0);
179

180 181 182 183
  u16_text1 += u16_text2 + u16_text3 + u16_text4;
  for (size_t i = 0; i < u16_text1.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text1[i]);
  }
184
  ASSERT_TRUE(Snapshot());
185
  ASSERT_EQ(paragraph->runs_.runs_.size(), 4ull);
186 187 188 189 190
  ASSERT_EQ(paragraph->runs_.styles_.size(), 5ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style1));
  ASSERT_TRUE(paragraph->runs_.styles_[2].equals(text_style2));
  ASSERT_TRUE(paragraph->runs_.styles_[3].equals(text_style3));
  ASSERT_TRUE(paragraph->runs_.styles_[4].equals(text_style4));
G
Gary Qian 已提交
191 192 193 194
  ASSERT_EQ(paragraph->records_[0].style().color, text_style1.color);
  ASSERT_EQ(paragraph->records_[1].style().color, text_style2.color);
  ASSERT_EQ(paragraph->records_[2].style().color, text_style3.color);
  ASSERT_EQ(paragraph->records_[3].style().color, text_style4.color);
195 196
}

197
// Currently, this should render nothing without a supplied TextStyle.
198
TEST_F(ParagraphTest, DefaultStyleParagraph) {
199 200 201 202 203 204
  const char* text = "No TextStyle! Uh Oh!";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
205
  paragraph_style.font_family = "Roboto";
206
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
207 208 209 210 211 212

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
213
  paragraph->Layout(GetTestCanvasWidth());
214

215 216
  paragraph->Paint(GetCanvas(), 10.0, 15.0);

217 218 219 220
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
221 222
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
  ASSERT_EQ(paragraph->runs_.styles_.size(), 1ull);
223 224 225
  ASSERT_TRUE(Snapshot());
}

226
TEST_F(ParagraphTest, BoldParagraph) {
227 228 229 230 231 232
  const char* text = "This is Red max bold text!";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
233
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
234 235

  txt::TextStyle text_style;
236
  text_style.font_family = "Roboto";
237
  text_style.font_size = 60;
238 239
  text_style.letter_spacing = 0;
  text_style.font_weight = txt::FontWeight::w900;
240 241 242 243 244 245 246 247
  text_style.color = SK_ColorRED;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
248
  paragraph->Layout(GetTestCanvasWidth());
249 250 251

  paragraph->Paint(GetCanvas(), 10.0, 60.0);

252 253 254 255
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
256
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
257 258
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
G
Gary Qian 已提交
259
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
260 261 262
  ASSERT_TRUE(Snapshot());
}

263
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(LeftAlignParagraph)) {
264 265 266
  const char* text =
      "This is a very long sentence to test if the text will properly wrap "
      "around and go to the next line. Sometimes, short sentence. Longer "
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      "sentences are okay too because they are nessecary. Very short. "
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
      "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
      "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
      "mollit anim id est laborum. "
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
      "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
      "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
      "mollit anim id est laborum.";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
  paragraph_style.text_align = TextAlign::left;
289
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
290 291

  txt::TextStyle text_style;
292
  text_style.font_family = "Roboto";
293 294 295 296
  text_style.font_size = 26;
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
297
  text_style.height = 1;
298
  text_style.decoration = TextDecoration::kUnderline;
299 300 301 302 303 304 305 306
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
307
  paragraph->Layout(GetTestCanvasWidth() - 100);
308 309

  paragraph->Paint(GetCanvas(), 0, 0);
310 311 312

  ASSERT_TRUE(Snapshot());

313 314 315 316 317
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
318 319
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
320
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
321
  double expected_y = 24;
322 323 324

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
325
  expected_y += 30;
326 327 328 329
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
330
  expected_y += 30;
331 332 333 334
  ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
335
  expected_y += 30;
336 337 338 339
  ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
340
  expected_y += 30 * 10;
341 342 343 344 345 346 347 348 349
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
  ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().x(), 0);

  ASSERT_EQ(paragraph_style.text_align,
            paragraph->GetParagraphStyle().text_align);

350
  // Tests for GetGlyphPositionAtCoordinate()
351 352 353 354 355
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(0, 0).position, 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 1).position, 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 35).position, 68ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 70).position, 134ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(2000, 35).position, 134ull);
356 357

  ASSERT_TRUE(Snapshot());
358 359
}

360
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(RightAlignParagraph)) {
361
  const char* text =
362 363
      "This is a very long sentence to test if the text will properly wrap "
      "around and go to the next line. Sometimes, short sentence. Longer "
364
      "sentences are okay too because they are nessecary. Very short. "
365 366 367 368 369 370
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
      "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
      "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
371
      "mollit anim id est laborum. "
372 373 374 375 376 377
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
      "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
      "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
378 379 380 381 382 383 384 385
      "mollit anim id est laborum.";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
  paragraph_style.text_align = TextAlign::right;
386
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
387 388

  txt::TextStyle text_style;
389
  text_style.font_family = "Roboto";
390 391 392 393
  text_style.font_size = 26;
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
394
  text_style.height = 1;
395
  text_style.decoration = TextDecoration::kUnderline;
396 397 398 399 400 401 402 403
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
404
  paragraph->Layout(GetTestCanvasWidth() - 100);
405 406

  paragraph->Paint(GetCanvas(), 0, 0);
407 408

  ASSERT_TRUE(Snapshot());
409 410 411 412 413
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
414 415
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
416
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
417
  double expected_y = 24;
418 419 420

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
421
  expected_y += 30;
422
  ASSERT_NEAR(
423 424
      paragraph->records_[0].offset().x(),
      paragraph->width_ -
425 426
          paragraph->breaker_.getWidths()[paragraph->records_[0].line()],
      2.0);
427 428 429

  ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
430
  expected_y += 30;
431
  ASSERT_NEAR(
432 433
      paragraph->records_[1].offset().x(),
      paragraph->width_ -
434 435
          paragraph->breaker_.getWidths()[paragraph->records_[1].line()],
      2.0);
436 437 438

  ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
439
  expected_y += 30;
440
  ASSERT_NEAR(
441 442
      paragraph->records_[2].offset().x(),
      paragraph->width_ -
443 444
          paragraph->breaker_.getWidths()[paragraph->records_[2].line()],
      2.0);
445 446 447

  ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
448
  expected_y += 30 * 10;
449
  ASSERT_NEAR(
450 451
      paragraph->records_[3].offset().x(),
      paragraph->width_ -
452 453
          paragraph->breaker_.getWidths()[paragraph->records_[3].line()],
      2.0);
454 455 456

  ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
457
  ASSERT_NEAR(
458 459
      paragraph->records_[13].offset().x(),
      paragraph->width_ -
460 461
          paragraph->breaker_.getWidths()[paragraph->records_[13].line()],
      2.0);
462 463 464 465

  ASSERT_EQ(paragraph_style.text_align,
            paragraph->GetParagraphStyle().text_align);

466 467 468
  ASSERT_TRUE(Snapshot());
}

469
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(CenterAlignParagraph)) {
470 471 472 473 474 475 476 477 478 479 480
  const char* text =
      "This is a very long sentence to test if the text will properly wrap "
      "around and go to the next line. Sometimes, short sentence. Longer "
      "sentences are okay too because they are nessecary. Very short. "
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
      "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
      "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
      "mollit anim id est laborum. "
481 482 483 484 485 486 487 488 489 490 491 492
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
      "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
      "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
      "mollit anim id est laborum.";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
493
  paragraph_style.max_lines = 14;
494
  paragraph_style.text_align = TextAlign::center;
495
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
496 497

  txt::TextStyle text_style;
498
  text_style.font_family = "Roboto";
499
  text_style.font_size = 26;
500 501
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
502
  text_style.color = SK_ColorBLACK;
503
  text_style.height = 1;
504
  text_style.decoration = TextDecoration::kUnderline;
505
  text_style.decoration_color = SK_ColorBLACK;
506 507 508 509 510 511 512
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
513
  paragraph->Layout(GetTestCanvasWidth() - 100);
514

515
  paragraph->Paint(GetCanvas(), 0, 0);
516 517

  ASSERT_TRUE(Snapshot());
518 519 520 521 522
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
523 524
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
525
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
526
  double expected_y = 24;
527 528 529

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
530
  expected_y += 30;
531 532 533 534 535
  ASSERT_NEAR(paragraph->records_[0].offset().x(),
              (paragraph->width_ -
               paragraph->breaker_.getWidths()[paragraph->records_[0].line()]) /
                  2,
              2.0);
536 537 538

  ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
539
  expected_y += 30;
540 541 542 543 544
  ASSERT_NEAR(paragraph->records_[1].offset().x(),
              (paragraph->width_ -
               paragraph->breaker_.getWidths()[paragraph->records_[1].line()]) /
                  2,
              2.0);
545 546 547

  ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
548
  expected_y += 30;
549 550 551 552 553
  ASSERT_NEAR(paragraph->records_[2].offset().x(),
              (paragraph->width_ -
               paragraph->breaker_.getWidths()[paragraph->records_[2].line()]) /
                  2,
              2.0);
554 555 556

  ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
557
  expected_y += 30 * 10;
558 559 560 561 562
  ASSERT_NEAR(paragraph->records_[3].offset().x(),
              (paragraph->width_ -
               paragraph->breaker_.getWidths()[paragraph->records_[3].line()]) /
                  2,
              2.0);
563 564 565

  ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
566
  ASSERT_NEAR(
567 568 569
      paragraph->records_[13].offset().x(),
      (paragraph->width_ -
       paragraph->breaker_.getWidths()[paragraph->records_[13].line()]) /
570 571
          2,
      2.0);
572 573 574 575

  ASSERT_EQ(paragraph_style.text_align,
            paragraph->GetParagraphStyle().text_align);

576 577 578
  ASSERT_TRUE(Snapshot());
}

579
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(JustifyAlignParagraph)) {
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  const char* text =
      "This is a very long sentence to test if the text will properly wrap "
      "around and go to the next line. Sometimes, short sentence. Longer "
      "sentences are okay too because they are nessecary. Very short. "
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
      "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
      "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
      "mollit anim id est laborum. "
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
      "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
      "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
      "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
595
      "velit esse cillum dolore eu fugiat.";
596 597 598 599 600 601 602
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
  paragraph_style.text_align = TextAlign::justify;
603
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
604 605

  txt::TextStyle text_style;
606
  text_style.font_family = "Roboto";
607
  text_style.font_size = 26;
608
  text_style.letter_spacing = 0;
609 610
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
611
  text_style.height = 1;
612
  text_style.decoration = TextDecoration::kUnderline;
613 614 615 616 617 618 619 620
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
621
  paragraph->Layout(GetTestCanvasWidth() - 100);
622

623
  paragraph->Paint(GetCanvas(), 0, 0);
624 625

  ASSERT_TRUE(Snapshot());
626 627 628 629
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
630
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
631 632
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
633
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
634
  double expected_y = 24;
635 636 637

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
638
  expected_y += 30;
639 640 641 642
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
643
  expected_y += 30;
644 645 646 647
  ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
648
  expected_y += 30;
649 650 651 652
  ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
653
  expected_y += 30 * 10;
654 655 656 657 658 659 660 661 662
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);

  ASSERT_TRUE(paragraph->records_[13].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().y(), expected_y);
  ASSERT_DOUBLE_EQ(paragraph->records_[13].offset().x(), 0);

  ASSERT_EQ(paragraph_style.text_align,
            paragraph->GetParagraphStyle().text_align);

663 664 665
  ASSERT_TRUE(Snapshot());
}

666
TEST_F(ParagraphTest, DecorationsParagraph) {
G
Gary Qian 已提交
667 668 669
  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
  paragraph_style.text_align = TextAlign::left;
670
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
671 672

  txt::TextStyle text_style;
673
  text_style.font_family = "Roboto";
G
Gary Qian 已提交
674 675 676 677 678
  text_style.font_size = 26;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
  text_style.height = 2;
679 680 681
  text_style.decoration = TextDecoration::kUnderline |
                          TextDecoration::kOverline |
                          TextDecoration::kLineThrough;
G
Gary Qian 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  text_style.decoration_style = txt::TextDecorationStyle::kSolid;
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);
  builder.AddText("This text should be");

  text_style.decoration_style = txt::TextDecorationStyle::kDouble;
  text_style.decoration_color = SK_ColorBLUE;
  builder.PushStyle(text_style);
  builder.AddText(" decorated even when");

  text_style.decoration_style = txt::TextDecorationStyle::kDotted;
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);
  builder.AddText(" wrapped around to");

  text_style.decoration_style = txt::TextDecorationStyle::kDashed;
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);
  builder.AddText(" the next line.");

  text_style.decoration_style = txt::TextDecorationStyle::kWavy;
  text_style.decoration_color = SK_ColorRED;
  builder.PushStyle(text_style);

  builder.AddText(" Otherwise, bad things happen.");

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth() - 100);

  paragraph->Paint(GetCanvas(), 0, 0);

715
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
716 717 718 719 720
  ASSERT_EQ(paragraph->runs_.size(), 5ull);
  ASSERT_EQ(paragraph->records_.size(), 6ull);

  for (size_t i = 0; i < 6; ++i) {
    ASSERT_EQ(paragraph->records_[i].style().decoration,
721 722
              TextDecoration::kUnderline | TextDecoration::kOverline |
                  TextDecoration::kLineThrough);
G
Gary Qian 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
  }

  ASSERT_EQ(paragraph->records_[0].style().decoration_style,
            txt::TextDecorationStyle::kSolid);
  ASSERT_EQ(paragraph->records_[1].style().decoration_style,
            txt::TextDecorationStyle::kDouble);
  ASSERT_EQ(paragraph->records_[2].style().decoration_style,
            txt::TextDecorationStyle::kDotted);
  ASSERT_EQ(paragraph->records_[3].style().decoration_style,
            txt::TextDecorationStyle::kDashed);
  ASSERT_EQ(paragraph->records_[4].style().decoration_style,
            txt::TextDecorationStyle::kDashed);
  ASSERT_EQ(paragraph->records_[5].style().decoration_style,
            txt::TextDecorationStyle::kWavy);

  ASSERT_EQ(paragraph->records_[0].style().decoration_color, SK_ColorBLACK);
  ASSERT_EQ(paragraph->records_[1].style().decoration_color, SK_ColorBLUE);
  ASSERT_EQ(paragraph->records_[2].style().decoration_color, SK_ColorBLACK);
  ASSERT_EQ(paragraph->records_[3].style().decoration_color, SK_ColorBLACK);
  ASSERT_EQ(paragraph->records_[4].style().decoration_color, SK_ColorBLACK);
  ASSERT_EQ(paragraph->records_[5].style().decoration_color, SK_ColorRED);
}

746
TEST_F(ParagraphTest, ItalicsParagraph) {
747
  txt::ParagraphStyle paragraph_style;
748
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
749 750

  txt::TextStyle text_style;
751
  text_style.font_family = "Roboto";
752
  text_style.color = SK_ColorRED;
753
  text_style.font_size = 10;
754
  builder.PushStyle(text_style);
755
  builder.AddText("No italic ");
756

757 758 759
  text_style.font_style = txt::FontStyle::italic;
  builder.PushStyle(text_style);
  builder.AddText("Yes Italic ");
760 761

  builder.Pop();
762
  builder.AddText("No Italic again.");
763 764

  auto paragraph = builder.Build();
765
  paragraph->Layout(GetTestCanvasWidth());
766

767
  paragraph->Paint(GetCanvas(), 0, 0);
768

769 770 771 772 773 774
  ASSERT_EQ(paragraph->runs_.runs_.size(), 3ull);
  ASSERT_EQ(paragraph->runs_.styles_.size(), 3ull);
  ASSERT_EQ(paragraph->records_[1].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_[1].style().font_style, txt::FontStyle::italic);
  ASSERT_EQ(paragraph->records_[2].style().font_style, txt::FontStyle::normal);
  ASSERT_EQ(paragraph->records_[0].style().font_style, txt::FontStyle::normal);
775
  ASSERT_TRUE(Snapshot());
776 777
}

778
TEST_F(ParagraphTest, ChineseParagraph) {
779 780 781 782 783 784 785 786 787 788 789 790
  const char* text =
      "左線読設重説切後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育"
      "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得庭"
      "際輝求佐抗蒼提夜合逃表。注統天言件自謙雅載報紙喪。作画稿愛器灯女書利変探"
      "訃第金線朝開化建。子戦年帝励害表月幕株漠新期刊人秘。図的海力生禁挙保天戦"
      "聞条年所在口。";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
791
  paragraph_style.text_align = TextAlign::justify;
792
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
793 794 795 796 797 798

  txt::TextStyle text_style;
  text_style.color = SK_ColorBLACK;
  text_style.font_size = 35;
  text_style.letter_spacing = 2;
  text_style.font_family = "Source Han Serif CN";
799 800 801
  text_style.decoration = TextDecoration::kUnderline |
                          TextDecoration::kOverline |
                          TextDecoration::kLineThrough;
802 803 804 805 806 807 808 809 810 811 812 813 814 815
  text_style.decoration_style = txt::TextDecorationStyle::kSolid;
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth() - 100);

  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
816 817
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
818 819 820 821 822 823
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 7ull);

  ASSERT_TRUE(Snapshot());
}

824
// TODO(garyq): Support RTL languages.
825
TEST_F(ParagraphTest, DISABLED_ArabicParagraph) {
826 827 828 829 830 831 832 833 834 835
  const char* text =
      "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
      "بمباركة التقليدية قام عن. تصفح";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
  paragraph_style.text_align = TextAlign::right;
836
  paragraph_style.text_direction = TextDirection::rtl;
837
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
838 839 840 841 842 843

  txt::TextStyle text_style;
  text_style.color = SK_ColorBLACK;
  text_style.font_size = 35;
  text_style.letter_spacing = 2;
  text_style.font_family = "Katibeh";
844 845 846
  text_style.decoration = TextDecoration::kUnderline |
                          TextDecoration::kOverline |
                          TextDecoration::kLineThrough;
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
  text_style.decoration_style = txt::TextDecorationStyle::kSolid;
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth() - 100);

  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());

  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
863 864
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
865 866
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 2ull);
867
  ASSERT_EQ(paragraph->paragraph_style_.text_direction, TextDirection::rtl);
868 869 870 871 872 873 874 875

  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[u16_text.length() - i]);
  }

  ASSERT_TRUE(Snapshot());
}

876
TEST_F(ParagraphTest, GetGlyphPositionAtCoordinateParagraph) {
877 878
  const char* text =
      "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
879
      "67890 12345";
880 881 882 883 884 885 886
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 10;
  paragraph_style.text_align = TextAlign::left;
887
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
888 889

  txt::TextStyle text_style;
890
  text_style.font_family = "Roboto";
891 892 893 894 895 896 897 898 899 900 901 902
  text_style.font_size = 50;
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1.5;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
903
  paragraph->Layout(550);
904 905 906

  paragraph->Paint(GetCanvas(), 0, 0);

907 908
  ASSERT_TRUE(Snapshot());

909 910 911
  // Tests for GetGlyphPositionAtCoordinate()
  // NOTE: resulting values can be a few off from their respective positions in
  // the original text because the final trailing whitespaces are sometimes not
912 913
  // drawn (namely, when using "justify" alignment) and therefore are not active
  // glyphs.
914 915 916 917 918 919
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(-10000, -10000).position,
            0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(-1, -1).position, 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(0, 0).position, 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(3, 3).position, 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(35, 1).position, 1ull);
920 921 922 923
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(300, 2).position, 11ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(301, 2.2).position, 11ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(302, 2.6).position, 11ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(301, 2.1).position, 11ull);
924 925 926 927 928 929 930
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(100000, 20).position,
            18ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(450, 20).position, 16ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(100000, 90).position,
            36ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(-100000, 90).position,
            18ull);
931
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(20, -80).position, 1ull);
932 933 934 935 936 937 938 939 940
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 90).position, 18ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 180).position, 36ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(10000, 180).position,
            54ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(70, 180).position, 38ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 270).position, 54ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(35, 90).position, 19ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(10000, 10000).position,
            77ull);
941
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(85, 10000).position, 75ull);
942 943
}

944
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) {
945 946 947 948 949 950 951 952 953 954
  const char* text =
      "12345,  \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
      "67890 12345";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 10;
  paragraph_style.text_align = TextAlign::left;
955
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
956 957

  txt::TextStyle text_style;
958
  text_style.font_family = "Roboto";
959 960
  text_style.font_size = 50;
  text_style.letter_spacing = 0;
961
  text_style.font_weight = FontWeight::w500;
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
  text_style.word_spacing = 0;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(550);

  paragraph->Paint(GetCanvas(), 0, 0);

  SkPaint paint;
  paint.setStyle(SkPaint::kStroke_Style);
  paint.setAntiAlias(true);
  paint.setStrokeWidth(1);

  // Tests for GetRectsForRange()
  // NOTE: The base truth values may still need adjustment as the specifics
  // are adjusted.
  paint.setColor(SK_ColorRED);
985 986 987 988
  std::vector<txt::Paragraph::TextBox> boxes =
      paragraph->GetRectsForRange(0, 0);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
989
  }
990
  EXPECT_EQ(boxes.size(), 0ull);
991

992 993 994
  boxes = paragraph->GetRectsForRange(0, 1);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
995
  }
996 997
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 0);
998
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
999 1000
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 28.417969);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
1001

1002
  paint.setColor(SK_ColorBLUE);
1003 1004 1005
  boxes = paragraph->GetRectsForRange(2, 8);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1006
  }
1007 1008
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 56.835938);
1009
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
1010 1011
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 177.44922);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
1012 1013

  paint.setColor(SK_ColorGREEN);
1014 1015 1016
  boxes = paragraph->GetRectsForRange(8, 21);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1017
  }
1018 1019
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 177);
1020
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
1021 1022
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 506.08984);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
1023 1024

  paint.setColor(SK_ColorRED);
1025 1026 1027
  boxes = paragraph->GetRectsForRange(30, 100);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1028
  }
1029 1030
  EXPECT_EQ(boxes.size(), 4ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 210.83594);
1031
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 59.40625);
1032 1033
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 463.44922);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 118);
1034 1035 1036

  // TODO(garyq): The following set of vals are definetly wrong and
  // end of paragraph handling needs to be fixed in a later patch.
1037
  EXPECT_FLOAT_EQ(boxes[3].rect.left(), 0);
1038
  EXPECT_FLOAT_EQ(boxes[3].rect.top(), 236.40625);
1039 1040
  EXPECT_FLOAT_EQ(boxes[3].rect.right(), 142.08984);
  EXPECT_FLOAT_EQ(boxes[3].rect.bottom(), 295);
1041

1042
  paint.setColor(SK_ColorBLUE);
1043 1044 1045
  boxes = paragraph->GetRectsForRange(19, 22);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1046
  }
1047 1048
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 449.25391);
1049
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
1050 1051
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 519.44922);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
1052 1053

  paint.setColor(SK_ColorRED);
1054 1055 1056
  boxes = paragraph->GetRectsForRange(21, 21);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1057
  }
1058
  EXPECT_EQ(boxes.size(), 0ull);
1059

1060 1061 1062
  ASSERT_TRUE(Snapshot());
}

1063 1064
SkRect GetCoordinatesForGlyphPosition(const txt::Paragraph& paragraph,
                                      size_t pos) {
1065 1066 1067
  std::vector<txt::Paragraph::TextBox> boxes =
      paragraph.GetRectsForRange(pos, pos + 1);
  return !boxes.empty() ? boxes.front().rect : SkRect::MakeEmpty();
1068 1069
}

1070
TEST_F(ParagraphTest, GetWordBoundaryParagraph) {
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
  const char* text =
      "12345  67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
      "67890 12345";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 10;
  paragraph_style.text_align = TextAlign::left;
1081
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1082 1083

  txt::TextStyle text_style;
1084
  text_style.font_family = "Roboto";
1085 1086
  text_style.font_size = 52;
  text_style.letter_spacing = 1.19039;
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1.5;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(550);

  paragraph->Paint(GetCanvas(), 0, 0);

1101 1102 1103 1104 1105 1106
  SkPaint paint;
  paint.setStyle(SkPaint::kStroke_Style);
  paint.setAntiAlias(true);
  paint.setStrokeWidth(1);
  paint.setColor(SK_ColorRED);

1107
  SkRect rect = GetCoordinatesForGlyphPosition(*paragraph, 0);
1108
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1109

1110 1111 1112 1113 1114
  EXPECT_EQ(paragraph->GetWordBoundary(0), txt::Paragraph::Range<size_t>(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(1), txt::Paragraph::Range<size_t>(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(2), txt::Paragraph::Range<size_t>(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(3), txt::Paragraph::Range<size_t>(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(4), txt::Paragraph::Range<size_t>(0, 5));
1115
  rect = GetCoordinatesForGlyphPosition(*paragraph, 5);
1116
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1117

1118
  EXPECT_EQ(paragraph->GetWordBoundary(5), txt::Paragraph::Range<size_t>(5, 6));
1119
  rect = GetCoordinatesForGlyphPosition(*paragraph, 6);
1120 1121
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1122
  EXPECT_EQ(paragraph->GetWordBoundary(6), txt::Paragraph::Range<size_t>(6, 7));
1123
  rect = GetCoordinatesForGlyphPosition(*paragraph, 7);
1124
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1125

1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  EXPECT_EQ(paragraph->GetWordBoundary(7),
            txt::Paragraph::Range<size_t>(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(8),
            txt::Paragraph::Range<size_t>(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(9),
            txt::Paragraph::Range<size_t>(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(10),
            txt::Paragraph::Range<size_t>(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(11),
            txt::Paragraph::Range<size_t>(7, 12));
1136
  rect = GetCoordinatesForGlyphPosition(*paragraph, 12);
1137
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1138

1139 1140
  EXPECT_EQ(paragraph->GetWordBoundary(12),
            txt::Paragraph::Range<size_t>(12, 13));
1141
  rect = GetCoordinatesForGlyphPosition(*paragraph, 13);
1142
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1143

1144 1145
  EXPECT_EQ(paragraph->GetWordBoundary(13),
            txt::Paragraph::Range<size_t>(13, 18));
1146
  rect = GetCoordinatesForGlyphPosition(*paragraph, 18);
1147 1148
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1149
  rect = GetCoordinatesForGlyphPosition(*paragraph, 19);
1150 1151
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1152
  rect = GetCoordinatesForGlyphPosition(*paragraph, 24);
1153 1154
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1155
  rect = GetCoordinatesForGlyphPosition(*paragraph, 25);
1156 1157
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1158
  rect = GetCoordinatesForGlyphPosition(*paragraph, 30);
1159 1160
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1161 1162
  EXPECT_EQ(paragraph->GetWordBoundary(30),
            txt::Paragraph::Range<size_t>(30, 31));
1163
  rect = GetCoordinatesForGlyphPosition(*paragraph, 31);
1164 1165
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1166
  rect = GetCoordinatesForGlyphPosition(*paragraph, icu_text.length() - 5);
1167 1168
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1169 1170 1171
  EXPECT_EQ(
      paragraph->GetWordBoundary(icu_text.length() - 1),
      txt::Paragraph::Range<size_t>(icu_text.length() - 5, icu_text.length()));
1172
  rect = GetCoordinatesForGlyphPosition(*paragraph, icu_text.length());
1173 1174 1175
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

  ASSERT_TRUE(Snapshot());
1176 1177
}

1178
TEST_F(ParagraphTest, SpacingParagraph) {
G
Gary Qian 已提交
1179 1180 1181 1182 1183 1184 1185 1186
  const char* text = "H";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 10;
  paragraph_style.text_align = TextAlign::left;
1187
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1188 1189

  txt::TextStyle text_style;
1190
  text_style.font_family = "Roboto";
G
Gary Qian 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  text_style.font_size = 50;
  text_style.letter_spacing = 20;
  text_style.word_spacing = 0;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);
  builder.Pop();

  text_style.font_size = 50;
  text_style.letter_spacing = 10;
  text_style.word_spacing = 0;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);
  builder.Pop();

  text_style.font_size = 50;
  text_style.letter_spacing = 20;
  text_style.word_spacing = 0;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);
  builder.Pop();

  text_style.font_size = 50;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 0;
  builder.PushStyle(text_style);
  builder.AddText("|");
  builder.Pop();

  text_style.font_size = 50;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 20;
  builder.PushStyle(text_style);
  builder.AddText("H ");
  builder.Pop();

  text_style.font_size = 50;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 0;
  builder.PushStyle(text_style);
  builder.AddText("H ");
  builder.Pop();

  text_style.font_size = 50;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 20;
  builder.PushStyle(text_style);
  builder.AddText("H ");
  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(550);

  paragraph->Paint(GetCanvas(), 0, 0);

  SkPaint paint;
  paint.setStyle(SkPaint::kStroke_Style);
  paint.setAntiAlias(true);
  paint.setStrokeWidth(1);
  paint.setColor(SK_ColorRED);

  ASSERT_TRUE(Snapshot());

  ASSERT_EQ(paragraph->records_.size(), 7ull);
  ASSERT_EQ(paragraph->records_[0].style().letter_spacing, 20);
  ASSERT_EQ(paragraph->records_[1].style().letter_spacing, 10);
  ASSERT_EQ(paragraph->records_[2].style().letter_spacing, 20);

  ASSERT_EQ(paragraph->records_[4].style().word_spacing, 20);
  ASSERT_EQ(paragraph->records_[5].style().word_spacing, 0);
  ASSERT_EQ(paragraph->records_[6].style().word_spacing, 20);
1263 1264
}

1265
TEST_F(ParagraphTest, LongWordParagraph) {
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
  const char* text =
      "A "
      "veryverylongwordtoseewherethiswillwraporifitwillatallandifitdoesthenthat"
      "wouldbeagoodthingbecausethebreakingisworking.";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1276
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1277 1278

  txt::TextStyle text_style;
G
Gary Qian 已提交
1279 1280
  text_style.font_family = "Roboto";
  text_style.font_size = 31;
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
  text_style.letter_spacing = 0;
  text_style.word_spacing = 0;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth() / 2);
G
Gary Qian 已提交
1292

1293 1294 1295 1296 1297 1298 1299 1300
  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_TRUE(Snapshot());
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
1301 1302
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1303
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1304
  ASSERT_EQ(paragraph->GetLineCount(), 4ull);
1305
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
1306 1307
}

1308
TEST_F(ParagraphTest, KernScaleParagraph) {
1309 1310
  float scale = 3.0f;

G
Gary Qian 已提交
1311 1312
  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1313
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1314 1315

  txt::TextStyle text_style;
1316
  text_style.font_family = "Droid Serif";
1317
  text_style.font_size = 100 / scale;
G
Gary Qian 已提交
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
  text_style.letter_spacing = 0;
  text_style.word_spacing = 0;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1;
  builder.PushStyle(text_style);
  builder.AddText("AVAVAWAH A0 V0 VA To The Lo");
  builder.PushStyle(text_style);
  builder.AddText("A");
  builder.PushStyle(text_style);
  builder.AddText("V");
1328
  text_style.font_size = 14 / scale;
G
Gary Qian 已提交
1329
  builder.PushStyle(text_style);
1330 1331 1332
  builder.AddText(
      " Dialog Text List lots of words to see if kerning works on a bigger set "
      "of characters AVAVAW");
G
Gary Qian 已提交
1333 1334 1335 1336

  builder.Pop();

  auto paragraph = builder.Build();
1337 1338
  paragraph->Layout(GetTestCanvasWidth() / scale);
  GetCanvas()->scale(scale, scale);
G
Gary Qian 已提交
1339
  paragraph->Paint(GetCanvas(), 0, 0);
1340
  GetCanvas()->scale(1.0, 1.0);
G
Gary Qian 已提交
1341 1342 1343 1344
  ASSERT_TRUE(Snapshot());

  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
1345 1346 1347
  EXPECT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 207.37109375f);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 230.87109375f);
  EXPECT_DOUBLE_EQ(paragraph->records_[4].offset().x(), 253.36328125f);
G
Gary Qian 已提交
1348 1349
}

1350
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(NewlineParagraph)) {
G
Gary Qian 已提交
1351
  txt::ParagraphStyle paragraph_style;
1352
  paragraph_style.font_family = "Roboto";
G
Gary Qian 已提交
1353
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1354 1355

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377

  txt::TextStyle text_style;
  text_style.font_family = "Roboto";
  text_style.font_size = 60;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 0;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1;
  builder.PushStyle(text_style);
  builder.AddText(
      "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 "
      "test1 test2 test3 test4");

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth() - 300);

  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_TRUE(Snapshot());

1378
  ASSERT_EQ(paragraph->records_.size(), 6ull);
G
Gary Qian 已提交
1379
  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
1380 1381
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().y(), 126);
G
Gary Qian 已提交
1382 1383
  EXPECT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);
1384 1385
  EXPECT_DOUBLE_EQ(paragraph->records_[4].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().y(), 266);
G
Gary Qian 已提交
1386
  EXPECT_DOUBLE_EQ(paragraph->records_[5].offset().x(), 0);
1387 1388
}

1389
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(EmojiParagraph)) {
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
  const char* text =
      "😀😃😄😁😆😅😂🤣☺😇🙂😍😡😟😢😻👽💩👍👎🙏👌👋👄👁👦👼👨‍🚀👨‍🚒🙋‍♂️👳👨‍👨‍👧‍👧\
      💼👡👠☂🐶🐰🐻🐼🐷🐒🐵🐔🐧🐦🐋🐟🐡🕸🐌🐴🐊🐄🐪🐘🌸🌏🔥🌟🌚🌝💦💧\
      ❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴‍♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓\
      📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️‍🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
1400 1401

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

  txt::TextStyle text_style;
  text_style.color = SK_ColorBLACK;
  text_style.font_family = "Noto Color Emoji";
  text_style.font_size = 50;
  text_style.decoration = TextDecoration::kUnderline;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth());
G
Gary Qian 已提交
1415

1416
  paragraph->Paint(GetCanvas(), 0, 0);
1417 1418 1419

  ASSERT_TRUE(Snapshot());

1420 1421 1422
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
1423

1424 1425 1426 1427 1428 1429 1430
  ASSERT_EQ(paragraph->records_.size(), 8ull);

  EXPECT_EQ(paragraph->records_[0].line(), 0ull);
  EXPECT_EQ(paragraph->records_[1].line(), 1ull);
  EXPECT_EQ(paragraph->records_[2].line(), 2ull);
  EXPECT_EQ(paragraph->records_[3].line(), 3ull);
  EXPECT_EQ(paragraph->records_[7].line(), 7ull);
G
Gary Qian 已提交
1431 1432
}

1433
TEST_F(ParagraphTest, HyphenBreakParagraph) {
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
  const char* text =
      "A "
      "very-very-long-Hyphen-word-to-see-where-this-will-wrap-or-if-it-will-at-"
      "all-and-if-it-does-thent-hat-"
      "would-be-a-good-thing-because-the-breaking.";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1445 1446

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

  txt::TextStyle text_style;
  text_style.font_family = "Roboto";
  text_style.font_size = 31;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 0;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth() / 2);

  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_TRUE(Snapshot());
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
1471 1472
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1473
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1474
  ASSERT_EQ(paragraph->GetLineCount(), 5ull);
1475 1476 1477
  ASSERT_TRUE(Snapshot());
}

1478
TEST_F(ParagraphTest, RepeatLayoutParagraph) {
1479 1480 1481
  const char* text =
      "Sentence to layout at diff widths to get diff line counts. short words "
      "short words short words short words short words short words short words "
1482 1483
      "short words short words short words short words short words short words "
      "end";
1484 1485 1486 1487 1488 1489
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1490
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

  txt::TextStyle text_style;
  text_style.font_family = "Roboto";
  text_style.font_size = 31;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 0;
  text_style.color = SK_ColorBLACK;
  text_style.height = 1;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);

  builder.Pop();

  // First Layout.
  auto paragraph = builder.Build();
  paragraph->Layout(300);

  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_TRUE(Snapshot());
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
1516 1517
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1518
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1519
  ASSERT_EQ(paragraph->GetLineCount(), 12ull);
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531

  // Second Layout.
  SetUp();
  paragraph->Layout(600);
  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_TRUE(Snapshot());
  ASSERT_EQ(paragraph->text_.size(), std::string{text}.length());
  for (size_t i = 0; i < u16_text.length(); i++) {
    ASSERT_EQ(paragraph->text_[i], u16_text[i]);
  }
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
1532 1533
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1534
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1535
  ASSERT_EQ(paragraph->GetLineCount(), 6ull);
1536 1537 1538
  ASSERT_TRUE(Snapshot());
}

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
TEST_F(ParagraphTest, Ellipsize) {
  const char* text =
      "This is a very long sentence to test if the text will properly wrap "
      "around and go to the next line. Sometimes, short sentence. Longer "
      "sentences are okay too because they are nessecary. Very short. ";
  auto icu_text = icu::UnicodeString::fromUTF8(text);
  std::u16string u16_text(icu_text.getBuffer(),
                          icu_text.getBuffer() + icu_text.length());

  txt::ParagraphStyle paragraph_style;
  paragraph_style.ellipsis = u"\u2026";
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());

  txt::TextStyle text_style;
1553
  text_style.font_family = "Roboto";
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
  text_style.color = SK_ColorBLACK;
  builder.PushStyle(text_style);
  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
  paragraph->Layout(GetTestCanvasWidth());

  paragraph->Paint(GetCanvas(), 0, 0);

  ASSERT_TRUE(Snapshot());

  // Check that the ellipsizer limited the text to one line and did not wrap
  // to a second line.
  ASSERT_EQ(paragraph->records_.size(), 1ull);
}

1572
}  // namespace txt