paragraph_unittests.cc 56.6 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 "lib/ftl/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 25 26
#include "txt/font_style.h"
#include "txt/font_weight.h"
#include "txt/paragraph.h"
#include "txt/paragraph_builder.h"
#include "txt/text_align.h"
#include "utils.h"
27

28 29
namespace txt {

30 31 32
using ParagraphTest = RenderTest;

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

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

  txt::TextStyle text_style;
  text_style.color = SK_ColorBLACK;
  builder.PushStyle(text_style);
44 45 46 47 48
  builder.AddText(u16_text);

  builder.Pop();

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

51
  paragraph->Paint(GetCanvas(), 10.0, 15.0);
52

53 54 55 56
  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]);
  }
57
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
58 59
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
G
Gary Qian 已提交
60
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
61 62 63
  ASSERT_TRUE(Snapshot());
}

64
TEST_F(ParagraphTest, SimpleRedParagraph) {
65 66 67 68 69 70
  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;
71
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
72 73 74 75

  txt::TextStyle text_style;
  text_style.color = SK_ColorRED;
  builder.PushStyle(text_style);
76 77 78 79 80 81

  builder.AddText(u16_text);

  builder.Pop();

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

84
  paragraph->Paint(GetCanvas(), 10.0, 15.0);
85

86 87 88 89
  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]);
  }
90
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
91 92
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
G
Gary Qian 已提交
93
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
94 95
  ASSERT_TRUE(Snapshot());
}
96

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

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

  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;
134
  text_style2.font_size = 50;
135 136
  text_style2.letter_spacing = 10;
  text_style2.word_spacing = 30;
137
  text_style2.font_weight = txt::FontWeight::w600;
138
  text_style2.color = SK_ColorGREEN;
139 140
  text_style2.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
  text_style2.decoration_color = SK_ColorBLACK;
141 142 143 144 145 146 147 148 149 150 151
  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;
152
  text_style4.font_size = 14;
153 154
  text_style4.color = SK_ColorBLUE;
  text_style4.font_family = "Roboto";
155 156
  text_style4.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
  text_style4.decoration_color = SK_ColorBLACK;
157 158 159 160
  builder.PushStyle(text_style4);

  builder.AddText(u16_text4);

161 162
  // Extra text to see if it goes to default when there is more text chunks than
  // styles.
163 164 165 166 167
  builder.AddText(u16_text5);

  builder.Pop();

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

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

172 173 174 175
  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]);
  }
176
  ASSERT_TRUE(Snapshot());
177
  ASSERT_EQ(paragraph->runs_.runs_.size(), 4ull);
178 179 180 181 182
  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 已提交
183 184 185 186
  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);
187 188
}

189
// Currently, this should render nothing without a supplied TextStyle.
190
TEST_F(ParagraphTest, DefaultStyleParagraph) {
191 192 193 194 195 196
  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;
197
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
198 199 200 201 202 203 204 205 206

  txt::TextStyle text_style;
  text_style.color = SK_ColorRED;

  builder.AddText(u16_text);

  builder.Pop();

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

209 210
  paragraph->Paint(GetCanvas(), 10.0, 15.0);

211 212 213 214
  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]);
  }
215 216
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
  ASSERT_EQ(paragraph->runs_.styles_.size(), 1ull);
217 218 219
  ASSERT_TRUE(Snapshot());
}

220
TEST_F(ParagraphTest, BoldParagraph) {
221 222 223 224 225 226
  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;
227
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
228 229

  txt::TextStyle text_style;
230
  text_style.font_family = "Roboto";
231
  text_style.font_size = 60;
232 233
  text_style.letter_spacing = 0;
  text_style.font_weight = txt::FontWeight::w900;
234 235 236 237 238 239 240 241
  text_style.color = SK_ColorRED;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
242
  paragraph->Layout(GetTestCanvasWidth());
243 244 245

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

246 247 248 249
  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]);
  }
250
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
251 252
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
G
Gary Qian 已提交
253
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
254 255 256
  ASSERT_TRUE(Snapshot());
}

257
TEST_F(ParagraphTest, LeftAlignParagraph) {
258 259 260
  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 "
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      "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;
283
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
284 285 286 287 288 289

  txt::TextStyle text_style;
  text_style.font_size = 26;
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
290
  text_style.height = 1;
291
  text_style.decoration = txt::TextDecoration(0x1);
292 293 294 295 296 297 298 299
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

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

  paragraph->Paint(GetCanvas(), 0, 0);
303 304 305

  ASSERT_TRUE(Snapshot());

306 307 308 309 310
  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);
311 312
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
313
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
314
  double expected_y = 24;
315 316 317

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
318
  expected_y += 30;
319 320 321 322
  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);
323
  expected_y += 30;
324 325 326 327
  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);
328
  expected_y += 30;
329 330 331 332
  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);
333
  expected_y += 30 * 10;
334 335 336 337 338 339 340 341 342
  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);

343 344 345
  // Tests for GetGlyphPositionAtCoordinate()
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(0, 0), 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 1), 0ull);
346 347
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 35), 68ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 70), 134ull);
348
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(2000, 35), 134ull);
349 350

  ASSERT_TRUE(Snapshot());
351 352
}

353
TEST_F(ParagraphTest, RightAlignParagraph) {
354
  const char* text =
355 356
      "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 "
357
      "sentences are okay too because they are nessecary. Very short. "
358 359 360 361 362 363
      "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 "
364
      "mollit anim id est laborum. "
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 372 373 374 375 376 377 378
      "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;
379
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
380 381 382 383 384 385

  txt::TextStyle text_style;
  text_style.font_size = 26;
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
386
  text_style.height = 1;
387
  text_style.decoration = txt::TextDecoration(0x1);
388 389 390 391 392 393 394 395
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

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

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

  ASSERT_TRUE(Snapshot());
401 402 403 404 405
  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);
406 407
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
408
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
409
  double expected_y = 24;
410 411 412

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
413
  expected_y += 30;
414 415 416 417 418 419 420
  ASSERT_DOUBLE_EQ(
      paragraph->records_[0].offset().x(),
      paragraph->width_ -
          paragraph->breaker_.getWidths()[paragraph->records_[0].line()]);

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

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

  ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
437
  expected_y += 30 * 10;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  ASSERT_DOUBLE_EQ(
      paragraph->records_[3].offset().x(),
      paragraph->width_ -
          paragraph->breaker_.getWidths()[paragraph->records_[3].line()]);

  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(),
      paragraph->width_ -
          paragraph->breaker_.getWidths()[paragraph->records_[13].line()]);

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

453 454 455
  ASSERT_TRUE(Snapshot());
}

456
TEST_F(ParagraphTest, CenterAlignParagraph) {
457 458 459 460 461 462 463 464 465 466 467
  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. "
468 469 470 471 472 473 474 475 476 477 478 479
      "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;
480
  paragraph_style.max_lines = 14;
481
  paragraph_style.text_align = TextAlign::center;
482
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
483 484 485

  txt::TextStyle text_style;
  text_style.font_size = 26;
486 487
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
488
  text_style.color = SK_ColorBLACK;
489
  text_style.height = 1;
490
  text_style.decoration = txt::TextDecoration(0x1);
491
  text_style.decoration_color = SK_ColorBLACK;
492 493 494 495 496 497 498
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

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

501
  paragraph->Paint(GetCanvas(), 0, 0);
502 503

  ASSERT_TRUE(Snapshot());
504 505 506 507 508
  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);
509 510
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
511
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
512
  double expected_y = 24;
513 514 515

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
516
  expected_y += 30;
517 518 519 520 521 522 523 524
  ASSERT_DOUBLE_EQ(
      paragraph->records_[0].offset().x(),
      (paragraph->width_ -
       paragraph->breaker_.getWidths()[paragraph->records_[0].line()]) /
          2);

  ASSERT_TRUE(paragraph->records_[1].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[1].offset().y(), expected_y);
525
  expected_y += 30;
526 527 528 529 530 531 532 533
  ASSERT_DOUBLE_EQ(
      paragraph->records_[1].offset().x(),
      (paragraph->width_ -
       paragraph->breaker_.getWidths()[paragraph->records_[1].line()]) /
          2);

  ASSERT_TRUE(paragraph->records_[2].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[2].offset().y(), expected_y);
534
  expected_y += 30;
535 536 537 538 539 540 541
  ASSERT_EQ(paragraph->records_[2].offset().x(),
            (paragraph->width_ -
             paragraph->breaker_.getWidths()[paragraph->records_[2].line()]) /
                2);

  ASSERT_TRUE(paragraph->records_[3].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[3].offset().y(), expected_y);
542
  expected_y += 30 * 10;
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  ASSERT_DOUBLE_EQ(
      paragraph->records_[3].offset().x(),
      (paragraph->width_ -
       paragraph->breaker_.getWidths()[paragraph->records_[3].line()]) /
          2);

  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(),
      (paragraph->width_ -
       paragraph->breaker_.getWidths()[paragraph->records_[13].line()]) /
          2);

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

560 561 562
  ASSERT_TRUE(Snapshot());
}

563
TEST_F(ParagraphTest, JustifyAlignParagraph) {
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
  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 "
579
      "velit esse cillum dolore eu fugiat.";
580 581 582 583 584 585 586
  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;
587
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
588 589 590

  txt::TextStyle text_style;
  text_style.font_size = 26;
591
  text_style.letter_spacing = 0;
592 593
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
594
  text_style.height = 1;
595
  text_style.decoration = txt::TextDecoration(0x1);
596 597 598 599 600 601 602 603
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

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

606
  paragraph->Paint(GetCanvas(), 0, 0);
607 608

  ASSERT_TRUE(Snapshot());
609 610 611 612
  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]);
  }
613
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
614 615
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
616
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
617
  double expected_y = 24;
618 619 620

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
621
  expected_y += 30;
622 623 624 625
  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);
626
  expected_y += 30;
627 628 629 630
  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);
631
  expected_y += 30;
632 633 634 635
  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);
636
  expected_y += 30 * 10;
637 638 639 640 641 642 643 644 645
  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);

646 647 648
  ASSERT_TRUE(Snapshot());
}

649
TEST_F(ParagraphTest, DecorationsParagraph) {
G
Gary Qian 已提交
650 651 652
  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
  paragraph_style.text_align = TextAlign::left;
653
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

  txt::TextStyle text_style;
  text_style.font_size = 26;
  text_style.letter_spacing = 0;
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
  text_style.height = 2;
  text_style.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
  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);

695
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
  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,
              txt::TextDecoration(0x1 | 0x2 | 0x4));
  }

  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);
}

725
TEST_F(ParagraphTest, ItalicsParagraph) {
726
  txt::ParagraphStyle paragraph_style;
727
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
728 729 730

  txt::TextStyle text_style;
  text_style.color = SK_ColorRED;
731
  text_style.font_size = 10;
732
  builder.PushStyle(text_style);
733
  builder.AddText("No italic ");
734

735 736 737
  text_style.font_style = txt::FontStyle::italic;
  builder.PushStyle(text_style);
  builder.AddText("Yes Italic ");
738 739

  builder.Pop();
740
  builder.AddText("No Italic again.");
741 742

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

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

747 748 749 750 751 752
  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);
753
  ASSERT_TRUE(Snapshot());
754 755
}

756
TEST_F(ParagraphTest, ChineseParagraph) {
757 758 759 760 761 762 763 764 765 766 767 768
  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;
769
  paragraph_style.text_align = TextAlign::justify;
770
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

  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";
  text_style.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
  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);
792 793
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
794 795 796 797 798 799
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 7ull);

  ASSERT_TRUE(Snapshot());
}

800
// TODO(garyq): Support RTL languages.
801
TEST_F(ParagraphTest, DISABLED_ArabicParagraph) {
802 803 804 805 806 807 808 809 810 811 812
  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;
  paragraph_style.rtl = true;
813
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

  txt::TextStyle text_style;
  text_style.color = SK_ColorBLACK;
  text_style.font_size = 35;
  text_style.letter_spacing = 2;
  text_style.font_family = "Katibeh";
  text_style.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
  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);
837 838
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
839 840 841 842 843 844 845 846 847 848 849
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 2ull);
  ASSERT_EQ(paragraph->paragraph_style_.rtl, true);

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

  ASSERT_TRUE(Snapshot());
}

850
TEST_F(ParagraphTest, GetGlyphPositionAtCoordinateParagraph) {
851 852
  const char* text =
      "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
853
      "67890 12345";
854 855 856 857 858 859 860
  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;
861
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
862 863 864 865 866 867 868 869 870 871 872 873 874 875

  txt::TextStyle text_style;
  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();
876
  paragraph->Layout(550);
877 878 879

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

880 881
  ASSERT_TRUE(Snapshot());

882 883 884
  // 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
885 886
  // drawn (namely, when using "justify" alignment) and therefore are not active
  // glyphs.
887 888
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(-10000, -10000), 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(-1, -1), 0ull);
889 890 891
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(0, 0), 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(3, 3), 0ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(35, 1), 1ull);
892 893 894 895
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(300, 2), 10ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(301, 2.2), 10ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(302, 2.6), 10ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(301, 2.1), 10ull);
896
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(100000, 20), 18ull);
897
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(450, 20), 16ull);
898
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(100000, 90), 36ull);
899
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(-100000, 90), 18ull);
900
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(20, -80), 0ull);
901 902
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 90), 18ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 180), 36ull);
903
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(10000, 180), 54ull);
904
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(70, 180), 38ull);
905
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(1, 270), 54ull);
906
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(35, 90), 19ull);
907
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(10000, 10000), 77ull);
908
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(85, 10000), 74ull);
909 910
}

911
TEST_F(ParagraphTest, GetRectsForRangeParagraph) {
912 913 914 915 916 917 918 919 920 921
  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;
922
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
923 924 925 926

  txt::TextStyle text_style;
  text_style.font_size = 50;
  text_style.letter_spacing = 0;
927
  text_style.font_weight = FontWeight::w500;
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
  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);
  std::vector<SkRect> rects = paragraph->GetRectsForRange(0, 0);
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
  EXPECT_EQ(rects.size(), 1ull);
  EXPECT_FLOAT_EQ(rects[0].left(), 0);
  EXPECT_FLOAT_EQ(rects[0].top(), 0);
958
  EXPECT_FLOAT_EQ(rects[0].right(), 28.417969);
959
  EXPECT_FLOAT_EQ(rects[0].bottom(), 59);
960

961 962 963 964 965 966 967
  rects = paragraph->GetRectsForRange(0, 1);
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
  EXPECT_EQ(rects.size(), 1ull);
  EXPECT_FLOAT_EQ(rects[0].left(), 0);
  EXPECT_FLOAT_EQ(rects[0].top(), 0);
968
  EXPECT_FLOAT_EQ(rects[0].right(), 28.417969);
969
  EXPECT_FLOAT_EQ(rects[0].bottom(), 59);
970

971
  paint.setColor(SK_ColorBLUE);
972
  rects = paragraph->GetRectsForRange(2, 8);
973 974 975
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
976
  EXPECT_EQ(rects.size(), 3ull);
977
  EXPECT_FLOAT_EQ(rects[0].left(), 56.835938);
978
  EXPECT_FLOAT_EQ(rects[0].top(), 0);
979
  EXPECT_FLOAT_EQ(rects[0].right(), 153.07422);
980
  EXPECT_FLOAT_EQ(rects[0].bottom(), 59);
981

982
  EXPECT_FLOAT_EQ(rects[1].left(), 153.07422);
983
  EXPECT_FLOAT_EQ(rects[1].top(), 0);
984
  EXPECT_FLOAT_EQ(rects[1].right(), 165.52344);
985
  EXPECT_FLOAT_EQ(rects[1].bottom(), 59);
986

987
  EXPECT_FLOAT_EQ(rects[2].left(), 165.52344);
988
  EXPECT_FLOAT_EQ(rects[2].top(), 0);
989
  EXPECT_FLOAT_EQ(rects[2].right(), 177.97266);
990
  EXPECT_FLOAT_EQ(rects[2].bottom(), 59);
991 992

  paint.setColor(SK_ColorGREEN);
993
  rects = paragraph->GetRectsForRange(8, 21);
994 995 996
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
997
  EXPECT_EQ(rects.size(), 3ull);
998
  EXPECT_FLOAT_EQ(rects[0].left(), 177.97266);
999
  EXPECT_FLOAT_EQ(rects[0].top(), 0);
1000
  EXPECT_FLOAT_EQ(rects[0].right(), 352.48438);
1001
  EXPECT_FLOAT_EQ(rects[0].bottom(), 59);
1002

1003
  EXPECT_FLOAT_EQ(rects[1].left(), 352.48438);
1004
  EXPECT_FLOAT_EQ(rects[1].top(), 0);
1005
  EXPECT_FLOAT_EQ(rects[1].right(), 364.93359);
1006
  EXPECT_FLOAT_EQ(rects[1].bottom(), 59);
1007

1008
  EXPECT_FLOAT_EQ(rects[2].left(), 364.93359);
1009
  EXPECT_FLOAT_EQ(rects[2].top(), 0);
1010
  EXPECT_FLOAT_EQ(rects[2].right(), 507.02344);
1011
  EXPECT_FLOAT_EQ(rects[2].bottom(), 59);
1012 1013 1014 1015 1016 1017

  paint.setColor(SK_ColorRED);
  rects = paragraph->GetRectsForRange(30, 100);
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
1018
  EXPECT_EQ(rects.size(), 17ull);
1019
  EXPECT_FLOAT_EQ(rects[0].left(), 211.375);
1020
  EXPECT_FLOAT_EQ(rects[0].top(), 59);
1021
  EXPECT_FLOAT_EQ(rects[0].right(), 296.62891);
1022
  EXPECT_FLOAT_EQ(rects[0].bottom(), 118);
1023 1024 1025

  // TODO(garyq): The following set of vals are definetly wrong and
  // end of paragraph handling needs to be fixed in a later patch.
G
Gary Qian 已提交
1026
  EXPECT_FLOAT_EQ(rects[16].left(), 0);
1027
  EXPECT_FLOAT_EQ(rects[16].top(), 236);
1028
  EXPECT_FLOAT_EQ(rects[16].right(), 142.08984);
1029
  EXPECT_FLOAT_EQ(rects[16].bottom(), 295);
1030

1031
  paint.setColor(SK_ColorBLUE);
1032
  rects = paragraph->GetRectsForRange(19, 22);
1033 1034 1035
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
1036
  EXPECT_EQ(rects.size(), 2ull);
1037
  EXPECT_FLOAT_EQ(rects[1].left(), 507.02344);
1038
  EXPECT_FLOAT_EQ(rects[1].top(), 0);
1039
  EXPECT_FLOAT_EQ(rects[1].right(), 519.47266);
1040
  EXPECT_FLOAT_EQ(rects[1].bottom(), 59);
1041 1042 1043 1044 1045 1046

  paint.setColor(SK_ColorRED);
  rects = paragraph->GetRectsForRange(21, 21);
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
1047
  EXPECT_EQ(rects.size(), 1ull);
1048

1049 1050 1051
  ASSERT_TRUE(Snapshot());
}

1052
TEST_F(ParagraphTest, GetWordBoundaryParagraph) {
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  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;
1063
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1064 1065

  txt::TextStyle text_style;
1066 1067
  text_style.font_size = 52;
  text_style.letter_spacing = 1.19039;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  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);

1082 1083 1084 1085 1086 1087 1088 1089
  SkPaint paint;
  paint.setStyle(SkPaint::kStroke_Style);
  paint.setAntiAlias(true);
  paint.setStrokeWidth(1);
  paint.setColor(SK_ColorRED);

  SkRect rect = paragraph->GetCoordinatesForGlyphPosition(0);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1090 1091 1092 1093 1094 1095

  EXPECT_EQ(paragraph->GetWordBoundary(0), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(1), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(2), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(3), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(4), SkIPoint::Make(0, 5));
1096 1097
  rect = paragraph->GetCoordinatesForGlyphPosition(5);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1098 1099

  EXPECT_EQ(paragraph->GetWordBoundary(5), SkIPoint::Make(5, 6));
1100 1101 1102
  rect = paragraph->GetCoordinatesForGlyphPosition(6);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1103
  EXPECT_EQ(paragraph->GetWordBoundary(6), SkIPoint::Make(6, 7));
1104 1105
  rect = paragraph->GetCoordinatesForGlyphPosition(7);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1106 1107 1108 1109 1110 1111

  EXPECT_EQ(paragraph->GetWordBoundary(7), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(8), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(9), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(10), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(11), SkIPoint::Make(7, 12));
1112 1113
  rect = paragraph->GetCoordinatesForGlyphPosition(12);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1114 1115

  EXPECT_EQ(paragraph->GetWordBoundary(12), SkIPoint::Make(12, 13));
1116 1117
  rect = paragraph->GetCoordinatesForGlyphPosition(13);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1118 1119

  EXPECT_EQ(paragraph->GetWordBoundary(13), SkIPoint::Make(13, 18));
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  rect = paragraph->GetCoordinatesForGlyphPosition(18);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

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

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

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

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

  EXPECT_EQ(paragraph->GetWordBoundary(30), SkIPoint::Make(30, 31));
  rect = paragraph->GetCoordinatesForGlyphPosition(31);
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

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

1142 1143
  EXPECT_EQ(paragraph->GetWordBoundary(icu_text.length() - 1),
            SkIPoint::Make(icu_text.length() - 5, icu_text.length()));
1144 1145 1146 1147
  rect = paragraph->GetCoordinatesForGlyphPosition(icu_text.length());
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

  ASSERT_TRUE(Snapshot());
1148 1149
}

1150
TEST_F(ParagraphTest, SpacingParagraph) {
G
Gary Qian 已提交
1151 1152 1153 1154 1155 1156 1157 1158
  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;
1159
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 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

  txt::TextStyle text_style;
  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);
1234 1235
}

1236
TEST_F(ParagraphTest, LongWordParagraph) {
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
  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;
1247
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1248 1249

  txt::TextStyle text_style;
G
Gary Qian 已提交
1250 1251
  text_style.font_family = "Roboto";
  text_style.font_size = 31;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  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 已提交
1263

1264 1265 1266 1267 1268 1269 1270 1271
  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);
1272 1273
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1274 1275 1276
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->GetLineCount(), 4);
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
1277 1278
}

1279
TEST_F(ParagraphTest, KernScaleParagraph) {
1280 1281
  float scale = 3.0f;

G
Gary Qian 已提交
1282 1283
  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1284
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1285 1286

  txt::TextStyle text_style;
1287
  text_style.font_family = "Droid Serif";
1288
  text_style.font_size = 100 / scale;
G
Gary Qian 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
  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");
1299
  text_style.font_size = 14 / scale;
G
Gary Qian 已提交
1300
  builder.PushStyle(text_style);
1301 1302 1303
  builder.AddText(
      " Dialog Text List lots of words to see if kerning works on a bigger set "
      "of characters AVAVAW");
G
Gary Qian 已提交
1304 1305 1306 1307

  builder.Pop();

  auto paragraph = builder.Build();
1308 1309
  paragraph->Layout(GetTestCanvasWidth() / scale);
  GetCanvas()->scale(scale, scale);
G
Gary Qian 已提交
1310
  paragraph->Paint(GetCanvas(), 0, 0);
1311
  GetCanvas()->scale(1.0, 1.0);
G
Gary Qian 已提交
1312 1313 1314 1315
  ASSERT_TRUE(Snapshot());

  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
1316 1317 1318
  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 已提交
1319 1320
}

1321
TEST_F(ParagraphTest, NewlineParagraph) {
G
Gary Qian 已提交
1322 1323
  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1324 1325

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

  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());

  ASSERT_EQ(paragraph->runs_.size(), 9ull);
  ASSERT_EQ(paragraph->runs_.GetRun(1).end - paragraph->runs_.GetRun(1).start,
            1ull);
  ASSERT_EQ(paragraph->runs_.GetRun(3).end - paragraph->runs_.GetRun(3).start,
            1ull);
  ASSERT_EQ(paragraph->runs_.GetRun(5).end - paragraph->runs_.GetRun(5).start,
            1ull);
  ASSERT_EQ(paragraph->runs_.GetRun(7).end - paragraph->runs_.GetRun(7).start,
            1ull);

  ASSERT_EQ(paragraph->records_.size(), 10ull);
  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
1360
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 127.69921875);
G
Gary Qian 已提交
1361 1362 1363 1364
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().y(),
                   paragraph->records_[0].offset().y());
  EXPECT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);
1365
  EXPECT_DOUBLE_EQ(paragraph->records_[4].offset().x(), 586.9921875);
G
Gary Qian 已提交
1366 1367 1368
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().y(),
                   paragraph->records_[4].offset().y());
  EXPECT_DOUBLE_EQ(paragraph->records_[5].offset().x(), 0);
1369
  EXPECT_DOUBLE_EQ(paragraph->records_[6].offset().x(), 127.69921875);
G
Gary Qian 已提交
1370 1371
  EXPECT_DOUBLE_EQ(paragraph->records_[7].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[8].offset().x(), 0);
1372 1373
  EXPECT_DOUBLE_EQ(paragraph->records_[7].offset().y(), 336);
  EXPECT_DOUBLE_EQ(paragraph->records_[8].offset().y(), 406);
1374 1375
}

1376
TEST_F(ParagraphTest, EmojiParagraph) {
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
  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;
1387 1388

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

  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 已提交
1402

1403
  paragraph->Paint(GetCanvas(), 0, 0);
1404 1405 1406

  ASSERT_TRUE(Snapshot());

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

1411 1412 1413 1414 1415 1416 1417
  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 已提交
1418 1419
}

1420
TEST_F(ParagraphTest, HyphenBreakParagraph) {
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
  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;
1432 1433

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457

  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);
1458 1459
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1460 1461 1462 1463 1464
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->GetLineCount(), 5);
  ASSERT_TRUE(Snapshot());
}

1465
TEST_F(ParagraphTest, RepeatLayoutParagraph) {
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
  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 "
      "short words short words short words short words short words short words";
  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;
1476
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

  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);
1502 1503
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->GetLineCount(), 12);

  // 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);
1518 1519
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1520 1521 1522 1523 1524
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->GetLineCount(), 6);
  ASSERT_TRUE(Snapshot());
}

1525
}  // namespace txt