paragraph_unittests.cc 56.9 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 43 44

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

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

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

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

  builder.AddText(u16_text4);

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

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

  ASSERT_TRUE(Snapshot());

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

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

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

  ASSERT_TRUE(Snapshot());
352 353
}

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

454 455 456
  ASSERT_TRUE(Snapshot());
}

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

561 562 563
  ASSERT_TRUE(Snapshot());
}

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

647 648 649
  ASSERT_TRUE(Snapshot());
}

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

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

696
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
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 725
  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);
}

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

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

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

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

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

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

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

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

  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);
793 794
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
795 796 797 798 799 800
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 7ull);

  ASSERT_TRUE(Snapshot());
}

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

  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);
838 839
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
840 841
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
  ASSERT_EQ(paragraph->records_.size(), 2ull);
842
  ASSERT_EQ(paragraph->paragraph_style_.text_direction, TextDirection::rtl);
843 844 845 846 847 848 849 850

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

  ASSERT_TRUE(Snapshot());
}

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

  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();
877
  paragraph->Layout(550);
878 879 880

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

881 882
  ASSERT_TRUE(Snapshot());

883 884 885
  // 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
886 887
  // drawn (namely, when using "justify" alignment) and therefore are not active
  // glyphs.
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  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);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(300, 2).position, 10ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(301, 2.2).position, 10ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(302, 2.6).position, 10ull);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(301, 2.1).position, 10ull);
  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);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(20, -80).position, 0ull);
  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);
  ASSERT_EQ(paragraph->GetGlyphPositionAtCoordinate(85, 10000).position, 74ull);
916 917
}

918
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(GetRectsForRangeParagraph)) {
919 920 921 922 923 924 925 926 927 928
  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;
929
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
930 931 932 933

  txt::TextStyle text_style;
  text_style.font_size = 50;
  text_style.letter_spacing = 0;
934
  text_style.font_weight = FontWeight::w500;
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
  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);
  }
962
  EXPECT_EQ(rects.size(), 0ull);
963

964 965 966 967 968 969 970
  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);
971
  EXPECT_FLOAT_EQ(rects[0].right(), 28.417969);
972
  EXPECT_FLOAT_EQ(rects[0].bottom(), 59);
973

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

  paint.setColor(SK_ColorGREEN);
986
  rects = paragraph->GetRectsForRange(8, 21);
987 988 989
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
990
  EXPECT_EQ(rects.size(), 1ull);
991
  EXPECT_FLOAT_EQ(rects[0].left(), 177);
992
  EXPECT_FLOAT_EQ(rects[0].top(), 0);
993
  EXPECT_FLOAT_EQ(rects[0].right(), 506.08984);
994
  EXPECT_FLOAT_EQ(rects[0].bottom(), 59);
995 996 997 998 999 1000

  paint.setColor(SK_ColorRED);
  rects = paragraph->GetRectsForRange(30, 100);
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
1001
  EXPECT_EQ(rects.size(), 4ull);
1002
  EXPECT_FLOAT_EQ(rects[0].left(), 210.83594);
1003
  EXPECT_FLOAT_EQ(rects[0].top(), 59);
1004
  EXPECT_FLOAT_EQ(rects[0].right(), 463.44922);
1005
  EXPECT_FLOAT_EQ(rects[0].bottom(), 118);
1006 1007 1008

  // TODO(garyq): The following set of vals are definetly wrong and
  // end of paragraph handling needs to be fixed in a later patch.
1009 1010 1011 1012
  EXPECT_FLOAT_EQ(rects[3].left(), 0);
  EXPECT_FLOAT_EQ(rects[3].top(), 236);
  EXPECT_FLOAT_EQ(rects[3].right(), 142.08984);
  EXPECT_FLOAT_EQ(rects[3].bottom(), 295);
1013

1014
  paint.setColor(SK_ColorBLUE);
1015
  rects = paragraph->GetRectsForRange(19, 22);
1016 1017 1018
  for (size_t i = 0; i < rects.size(); ++i) {
    GetCanvas()->drawRect(rects[i], paint);
  }
1019
  EXPECT_EQ(rects.size(), 1ull);
1020
  EXPECT_FLOAT_EQ(rects[0].left(), 449.25391);
1021
  EXPECT_FLOAT_EQ(rects[0].top(), 0);
1022
  EXPECT_FLOAT_EQ(rects[0].right(), 519.44922);
1023
  EXPECT_FLOAT_EQ(rects[0].bottom(), 59);
1024 1025 1026 1027 1028 1029

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

1032 1033 1034
  ASSERT_TRUE(Snapshot());
}

1035 1036 1037 1038 1039 1040
SkRect GetCoordinatesForGlyphPosition(const txt::Paragraph& paragraph,
                                      size_t pos) {
  std::vector<SkRect> rects = paragraph.GetRectsForRange(pos, pos + 1);
  return !rects.empty() ? rects.front() : SkRect::MakeEmpty();
}

1041
TEST_F(ParagraphTest, GetWordBoundaryParagraph) {
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
  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;
1052
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1053 1054

  txt::TextStyle text_style;
1055 1056
  text_style.font_size = 52;
  text_style.letter_spacing = 1.19039;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  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);

1071 1072 1073 1074 1075 1076
  SkPaint paint;
  paint.setStyle(SkPaint::kStroke_Style);
  paint.setAntiAlias(true);
  paint.setStrokeWidth(1);
  paint.setColor(SK_ColorRED);

1077
  SkRect rect = GetCoordinatesForGlyphPosition(*paragraph, 0);
1078
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1079

J
Jason Simmons 已提交
1080 1081 1082 1083 1084
  EXPECT_EQ(paragraph->GetWordBoundary(0), txt::Paragraph::Range(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(1), txt::Paragraph::Range(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(2), txt::Paragraph::Range(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(3), txt::Paragraph::Range(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(4), txt::Paragraph::Range(0, 5));
1085
  rect = GetCoordinatesForGlyphPosition(*paragraph, 5);
1086
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1087

J
Jason Simmons 已提交
1088
  EXPECT_EQ(paragraph->GetWordBoundary(5), txt::Paragraph::Range(5, 6));
1089
  rect = GetCoordinatesForGlyphPosition(*paragraph, 6);
1090 1091
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

J
Jason Simmons 已提交
1092
  EXPECT_EQ(paragraph->GetWordBoundary(6), txt::Paragraph::Range(6, 7));
1093
  rect = GetCoordinatesForGlyphPosition(*paragraph, 7);
1094
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1095

J
Jason Simmons 已提交
1096 1097 1098 1099 1100
  EXPECT_EQ(paragraph->GetWordBoundary(7), txt::Paragraph::Range(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(8), txt::Paragraph::Range(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(9), txt::Paragraph::Range(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(10), txt::Paragraph::Range(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(11), txt::Paragraph::Range(7, 12));
1101
  rect = GetCoordinatesForGlyphPosition(*paragraph, 12);
1102
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1103

J
Jason Simmons 已提交
1104
  EXPECT_EQ(paragraph->GetWordBoundary(12), txt::Paragraph::Range(12, 13));
1105
  rect = GetCoordinatesForGlyphPosition(*paragraph, 13);
1106
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1107

J
Jason Simmons 已提交
1108
  EXPECT_EQ(paragraph->GetWordBoundary(13), txt::Paragraph::Range(13, 18));
1109
  rect = GetCoordinatesForGlyphPosition(*paragraph, 18);
1110 1111
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

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

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

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

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

J
Jason Simmons 已提交
1124
  EXPECT_EQ(paragraph->GetWordBoundary(30), txt::Paragraph::Range(30, 31));
1125
  rect = GetCoordinatesForGlyphPosition(*paragraph, 31);
1126 1127
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

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

1131
  EXPECT_EQ(paragraph->GetWordBoundary(icu_text.length() - 1),
J
Jason Simmons 已提交
1132
            txt::Paragraph::Range(icu_text.length() - 5, icu_text.length()));
1133
  rect = GetCoordinatesForGlyphPosition(*paragraph, icu_text.length());
1134 1135 1136
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

  ASSERT_TRUE(Snapshot());
1137 1138
}

1139
TEST_F(ParagraphTest, SpacingParagraph) {
G
Gary Qian 已提交
1140 1141 1142 1143 1144 1145 1146 1147
  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;
1148
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 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

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

1225
TEST_F(ParagraphTest, LongWordParagraph) {
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
  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;
1236
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1237 1238

  txt::TextStyle text_style;
G
Gary Qian 已提交
1239 1240
  text_style.font_family = "Roboto";
  text_style.font_size = 31;
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
  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 已提交
1252

1253 1254 1255 1256 1257 1258 1259 1260
  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);
1261 1262
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1263
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1264
  ASSERT_EQ(paragraph->GetLineCount(), 4ull);
1265
  ASSERT_TRUE(Snapshot());
G
Gary Qian 已提交
1266 1267
}

1268
TEST_F(ParagraphTest, KernScaleParagraph) {
1269 1270
  float scale = 3.0f;

G
Gary Qian 已提交
1271 1272
  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1273
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1274 1275

  txt::TextStyle text_style;
1276
  text_style.font_family = "Droid Serif";
1277
  text_style.font_size = 100 / scale;
G
Gary Qian 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
  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");
1288
  text_style.font_size = 14 / scale;
G
Gary Qian 已提交
1289
  builder.PushStyle(text_style);
1290 1291 1292
  builder.AddText(
      " Dialog Text List lots of words to see if kerning works on a bigger set "
      "of characters AVAVAW");
G
Gary Qian 已提交
1293 1294 1295 1296

  builder.Pop();

  auto paragraph = builder.Build();
1297 1298
  paragraph->Layout(GetTestCanvasWidth() / scale);
  GetCanvas()->scale(scale, scale);
G
Gary Qian 已提交
1299
  paragraph->Paint(GetCanvas(), 0, 0);
1300
  GetCanvas()->scale(1.0, 1.0);
G
Gary Qian 已提交
1301 1302 1303 1304
  ASSERT_TRUE(Snapshot());

  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
1305 1306 1307
  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 已提交
1308 1309
}

1310
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(NewlineParagraph)) {
G
Gary Qian 已提交
1311 1312
  txt::ParagraphStyle paragraph_style;
  paragraph_style.break_strategy = minikin::kBreakStrategy_HighQuality;
1313 1314

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336

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

1337
  ASSERT_EQ(paragraph->records_.size(), 7ull);
G
Gary Qian 已提交
1338
  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
1339 1340
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().y(), 126);
G
Gary Qian 已提交
1341 1342
  EXPECT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);
1343 1344
  EXPECT_DOUBLE_EQ(paragraph->records_[4].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().y(), 266);
G
Gary Qian 已提交
1345
  EXPECT_DOUBLE_EQ(paragraph->records_[5].offset().x(), 0);
1346
  EXPECT_DOUBLE_EQ(paragraph->records_[6].offset().x(), 0);
1347 1348
}

1349
TEST_F(ParagraphTest, DISABLE_ON_WINDOWS(EmojiParagraph)) {
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
  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;
1360 1361

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374

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

1376
  paragraph->Paint(GetCanvas(), 0, 0);
1377 1378 1379

  ASSERT_TRUE(Snapshot());

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

1384 1385 1386 1387 1388 1389 1390
  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 已提交
1391 1392
}

1393
TEST_F(ParagraphTest, HyphenBreakParagraph) {
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
  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;
1405 1406

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430

  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);
1431 1432
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1433
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1434
  ASSERT_EQ(paragraph->GetLineCount(), 5ull);
1435 1436 1437
  ASSERT_TRUE(Snapshot());
}

1438
TEST_F(ParagraphTest, RepeatLayoutParagraph) {
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
  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;
1449
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474

  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);
1475 1476
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1477
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1478
  ASSERT_EQ(paragraph->GetLineCount(), 12ull);
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

  // 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);
1491 1492
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1493
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1494
  ASSERT_EQ(paragraph->GetLineCount(), 6ull);
1495 1496 1497
  ASSERT_TRUE(Snapshot());
}

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
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;
  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);
}

1530
}  // namespace txt