paragraph_unittests.cc 57.8 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/fxl/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
#include "txt/font_style.h"
#include "txt/font_weight.h"
#include "txt/paragraph.h"
#include "txt/paragraph_builder.h"
#include "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
  text_style2.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
  text_style2.decoration_color = SK_ColorBLACK;
145 146 147 148 149 150 151 152 153 154 155
  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;
156
  text_style4.font_size = 14;
157 158
  text_style4.color = SK_ColorBLUE;
  text_style4.font_family = "Roboto";
159 160
  text_style4.decoration = txt::TextDecoration(0x1 | 0x2 | 0x4);
  text_style4.decoration_color = SK_ColorBLACK;
161 162 163 164
  builder.PushStyle(text_style4);

  builder.AddText(u16_text4);

165 166
  // Extra text to see if it goes to default when there is more text chunks than
  // styles.
167 168 169 170 171
  builder.AddText(u16_text5);

  builder.Pop();

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

211 212
  paragraph->Paint(GetCanvas(), 10.0, 15.0);

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

  auto paragraph = builder.Build();
244
  paragraph->Layout(GetTestCanvasWidth());
245 246 247

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

  paragraph->Paint(GetCanvas(), 0, 0);
306 307 308

  ASSERT_TRUE(Snapshot());

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

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

346
  // Tests for GetGlyphPositionAtCoordinate()
347 348 349 350 351
  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);
352 353

  ASSERT_TRUE(Snapshot());
354 355
}

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
417
  expected_y += 30;
418 419 420 421 422 423 424
  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);
425
  expected_y += 30;
426 427 428 429 430 431 432
  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);
433
  expected_y += 30;
434 435 436 437 438 439 440
  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);
441
  expected_y += 30 * 10;
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  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);

457 458 459
  ASSERT_TRUE(Snapshot());
}

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

  txt::TextStyle text_style;
489
  text_style.font_family = "Roboto";
490
  text_style.font_size = 26;
491 492
  text_style.letter_spacing = 1;
  text_style.word_spacing = 5;
493
  text_style.color = SK_ColorBLACK;
494
  text_style.height = 1;
495
  text_style.decoration = txt::TextDecoration(0x1);
496
  text_style.decoration_color = SK_ColorBLACK;
497 498 499 500 501 502 503
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

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

506
  paragraph->Paint(GetCanvas(), 0, 0);
507 508

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

  ASSERT_TRUE(paragraph->records_[0].style().equals(text_style));
  ASSERT_DOUBLE_EQ(paragraph->records_[0].offset().y(), expected_y);
521
  expected_y += 30;
522 523 524 525 526 527 528 529
  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);
530
  expected_y += 30;
531 532 533 534 535 536 537 538
  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);
539
  expected_y += 30;
540 541 542 543 544 545 546
  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);
547
  expected_y += 30 * 10;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
  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);

565 566 567
  ASSERT_TRUE(Snapshot());
}

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

  txt::TextStyle text_style;
595
  text_style.font_family = "Roboto";
596
  text_style.font_size = 26;
597
  text_style.letter_spacing = 0;
598 599
  text_style.word_spacing = 5;
  text_style.color = SK_ColorBLACK;
600
  text_style.height = 1;
601
  text_style.decoration = txt::TextDecoration(0x1);
602 603 604 605 606 607 608 609
  text_style.decoration_color = SK_ColorBLACK;
  builder.PushStyle(text_style);

  builder.AddText(u16_text);

  builder.Pop();

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

612
  paragraph->Paint(GetCanvas(), 0, 0);
613 614

  ASSERT_TRUE(Snapshot());
615 616 617 618
  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]);
  }
619
  ASSERT_EQ(paragraph->runs_.runs_.size(), 1ull);
620 621
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
622
  ASSERT_EQ(paragraph->records_.size(), paragraph_style.max_lines);
623
  double expected_y = 24;
624 625 626

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

652 653 654
  ASSERT_TRUE(Snapshot());
}

655
TEST_F(ParagraphTest, DecorationsParagraph) {
G
Gary Qian 已提交
656 657 658
  txt::ParagraphStyle paragraph_style;
  paragraph_style.max_lines = 14;
  paragraph_style.text_align = TextAlign::left;
659
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
660 661

  txt::TextStyle text_style;
662
  text_style.font_family = "Roboto";
G
Gary Qian 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
  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);

702
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
  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);
}

732
TEST_F(ParagraphTest, ItalicsParagraph) {
733
  txt::ParagraphStyle paragraph_style;
734
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
735 736

  txt::TextStyle text_style;
737
  text_style.font_family = "Roboto";
738
  text_style.color = SK_ColorRED;
739
  text_style.font_size = 10;
740
  builder.PushStyle(text_style);
741
  builder.AddText("No italic ");
742

743 744 745
  text_style.font_style = txt::FontStyle::italic;
  builder.PushStyle(text_style);
  builder.AddText("Yes Italic ");
746 747

  builder.Pop();
748
  builder.AddText("No Italic again.");
749 750

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

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

755 756 757 758 759 760
  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);
761
  ASSERT_TRUE(Snapshot());
762 763
}

764
TEST_F(ParagraphTest, ChineseParagraph) {
765 766 767 768 769 770 771 772 773 774 775 776
  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;
777
  paragraph_style.text_align = TextAlign::justify;
778
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

  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);
800 801
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
802 803 804 805 806 807
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 7ull);

  ASSERT_TRUE(Snapshot());
}

808
// TODO(garyq): Support RTL languages.
809
TEST_F(ParagraphTest, DISABLED_ArabicParagraph) {
810 811 812 813 814 815 816 817 818 819
  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;
820
  paragraph_style.text_direction = TextDirection::rtl;
821
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

  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);
845 846
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
847 848
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 2ull);
849
  ASSERT_EQ(paragraph->paragraph_style_.text_direction, TextDirection::rtl);
850 851 852 853 854 855 856 857

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

  ASSERT_TRUE(Snapshot());
}

858
TEST_F(ParagraphTest, GetGlyphPositionAtCoordinateParagraph) {
859 860
  const char* text =
      "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
861
      "67890 12345";
862 863 864 865 866 867 868
  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;
869
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
870 871

  txt::TextStyle text_style;
872
  text_style.font_family = "Roboto";
873 874 875 876 877 878 879 880 881 882 883 884
  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();
885
  paragraph->Layout(550);
886 887 888

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

889 890
  ASSERT_TRUE(Snapshot());

891 892 893
  // 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
894 895
  // drawn (namely, when using "justify" alignment) and therefore are not active
  // glyphs.
896 897 898 899 900 901
  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);
902 903 904 905
  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);
906 907 908 909 910 911 912
  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);
913
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(20, -80).position, 1ull);
914 915 916 917 918 919 920 921 922
  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);
923
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(85, 10000).position, 75ull);
924 925
}

926
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) {
927 928 929 930 931 932 933 934 935 936
  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;
937
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
938 939

  txt::TextStyle text_style;
940
  text_style.font_family = "Roboto";
941 942
  text_style.font_size = 50;
  text_style.letter_spacing = 0;
943
  text_style.font_weight = FontWeight::w500;
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
  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);
967 968 969 970
  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);
971
  }
972
  EXPECT_EQ(boxes.size(), 0ull);
973

974 975 976
  boxes = paragraph->GetRectsForRange(0, 1);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
977
  }
978 979
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 0);
980
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
981 982
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 28.417969);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
983

984
  paint.setColor(SK_ColorBLUE);
985 986 987
  boxes = paragraph->GetRectsForRange(2, 8);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
988
  }
989 990
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 56.835938);
991
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
992 993
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 177.44922);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
994 995

  paint.setColor(SK_ColorGREEN);
996 997 998
  boxes = paragraph->GetRectsForRange(8, 21);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
999
  }
1000 1001
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 177);
1002
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
1003 1004
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 506.08984);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
1005 1006

  paint.setColor(SK_ColorRED);
1007 1008 1009
  boxes = paragraph->GetRectsForRange(30, 100);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1010
  }
1011 1012
  EXPECT_EQ(boxes.size(), 4ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 210.83594);
1013
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 59.40625);
1014 1015
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 463.44922);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 118);
1016 1017 1018

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

1024
  paint.setColor(SK_ColorBLUE);
1025 1026 1027
  boxes = paragraph->GetRectsForRange(19, 22);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1028
  }
1029 1030
  EXPECT_EQ(boxes.size(), 1ull);
  EXPECT_FLOAT_EQ(boxes[0].rect.left(), 449.25391);
1031
  EXPECT_FLOAT_EQ(boxes[0].rect.top(), 0.40625);
1032 1033
  EXPECT_FLOAT_EQ(boxes[0].rect.right(), 519.44922);
  EXPECT_FLOAT_EQ(boxes[0].rect.bottom(), 59);
1034 1035

  paint.setColor(SK_ColorRED);
1036 1037 1038
  boxes = paragraph->GetRectsForRange(21, 21);
  for (size_t i = 0; i < boxes.size(); ++i) {
    GetCanvas()->drawRect(boxes[i].rect, paint);
1039
  }
1040
  EXPECT_EQ(boxes.size(), 0ull);
1041

1042 1043 1044
  ASSERT_TRUE(Snapshot());
}

1045 1046
SkRect GetCoordinatesForGlyphPosition(const txt::Paragraph& paragraph,
                                      size_t pos) {
1047 1048 1049
  std::vector<txt::Paragraph::TextBox> boxes =
      paragraph.GetRectsForRange(pos, pos + 1);
  return !boxes.empty() ? boxes.front().rect : SkRect::MakeEmpty();
1050 1051
}

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

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

1089
  SkRect rect = GetCoordinatesForGlyphPosition(*paragraph, 0);
1090
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1091

1092 1093 1094 1095 1096
  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));
1097
  rect = GetCoordinatesForGlyphPosition(*paragraph, 5);
1098
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1099

1100
  EXPECT_EQ(paragraph->GetWordBoundary(5), txt::Paragraph::Range<size_t>(5, 6));
1101
  rect = GetCoordinatesForGlyphPosition(*paragraph, 6);
1102 1103
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1104
  EXPECT_EQ(paragraph->GetWordBoundary(6), txt::Paragraph::Range<size_t>(6, 7));
1105
  rect = GetCoordinatesForGlyphPosition(*paragraph, 7);
1106
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1107

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  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));
1118
  rect = GetCoordinatesForGlyphPosition(*paragraph, 12);
1119
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1120

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

1126 1127
  EXPECT_EQ(paragraph->GetWordBoundary(13),
            txt::Paragraph::Range<size_t>(13, 18));
1128
  rect = GetCoordinatesForGlyphPosition(*paragraph, 18);
1129 1130
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

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

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

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

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

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

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

1151 1152 1153
  EXPECT_EQ(
      paragraph->GetWordBoundary(icu_text.length() - 1),
      txt::Paragraph::Range<size_t>(icu_text.length() - 5, icu_text.length()));
1154
  rect = GetCoordinatesForGlyphPosition(*paragraph, icu_text.length());
1155 1156 1157
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

  ASSERT_TRUE(Snapshot());
1158 1159
}

1160
TEST_F(ParagraphTest, SpacingParagraph) {
G
Gary Qian 已提交
1161 1162 1163 1164 1165 1166 1167 1168
  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;
1169
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1170 1171

  txt::TextStyle text_style;
1172
  text_style.font_family = "Roboto";
G
Gary Qian 已提交
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 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
  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);
1245 1246
}

1247
TEST_F(ParagraphTest, LongWordParagraph) {
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  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;
1258
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1259 1260

  txt::TextStyle text_style;
G
Gary Qian 已提交
1261 1262
  text_style.font_family = "Roboto";
  text_style.font_size = 31;
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
  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 已提交
1274

1275 1276 1277 1278 1279 1280 1281 1282
  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);
1283 1284
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1285
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1286
  ASSERT_EQ(paragraph->GetLineCount(), 4ull);
1287
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
1288 1289
}

1290
TEST_F(ParagraphTest, KernScaleParagraph) {
1291 1292
  float scale = 3.0f;

G
Gary Qian 已提交
1293 1294
  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1295
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1296 1297

  txt::TextStyle text_style;
1298
  text_style.font_family = "Droid Serif";
1299
  text_style.font_size = 100 / scale;
G
Gary Qian 已提交
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
  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");
1310
  text_style.font_size = 14 / scale;
G
Gary Qian 已提交
1311
  builder.PushStyle(text_style);
1312 1313 1314
  builder.AddText(
      " Dialog Text List lots of words to see if kerning works on a bigger set "
      "of characters AVAVAW");
G
Gary Qian 已提交
1315 1316 1317 1318

  builder.Pop();

  auto paragraph = builder.Build();
1319 1320
  paragraph->Layout(GetTestCanvasWidth() / scale);
  GetCanvas()->scale(scale, scale);
G
Gary Qian 已提交
1321
  paragraph->Paint(GetCanvas(), 0, 0);
1322
  GetCanvas()->scale(1.0, 1.0);
G
Gary Qian 已提交
1323 1324 1325 1326
  ASSERT_TRUE(Snapshot());

  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
1327 1328 1329
  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 已提交
1330 1331
}

1332
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(NewlineParagraph)) {
G
Gary Qian 已提交
1333
  txt::ParagraphStyle paragraph_style;
1334
  paragraph_style.font_family = "Roboto";
G
Gary Qian 已提交
1335
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1336 1337

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
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());

1360
  ASSERT_EQ(paragraph->records_.size(), 6ull);
G
Gary Qian 已提交
1361
  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
1362 1363
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().y(), 126);
G
Gary Qian 已提交
1364 1365
  EXPECT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);
1366 1367
  EXPECT_DOUBLE_EQ(paragraph->records_[4].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().y(), 266);
G
Gary Qian 已提交
1368
  EXPECT_DOUBLE_EQ(paragraph->records_[5].offset().x(), 0);
1369 1370
}

1371
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(EmojiParagraph)) {
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
  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;
1382 1383

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396

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

1398
  paragraph->Paint(GetCanvas(), 0, 0);
1399 1400 1401

  ASSERT_TRUE(Snapshot());

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

1406 1407 1408 1409 1410 1411 1412
  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 已提交
1413 1414
}

1415
TEST_F(ParagraphTest, HyphenBreakParagraph) {
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  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;
1427 1428

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

  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);
1453 1454
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1455
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1456
  ASSERT_EQ(paragraph->GetLineCount(), 5ull);
1457 1458 1459
  ASSERT_TRUE(Snapshot());
}

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

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

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

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
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;
1535
  text_style.font_family = "Roboto";
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
  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);
}

1554
}  // namespace txt