paragraph_unittests.cc 57.4 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
namespace txt {

29 30 31
using ParagraphTest = RenderTest;

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

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

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

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

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

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

  builder.AddText(u16_text4);

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

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

  ASSERT_TRUE(Snapshot());

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

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

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

  ASSERT_TRUE(Snapshot());
350 351
}

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

452 453 454
  ASSERT_TRUE(Snapshot());
}

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

559 560 561
  ASSERT_TRUE(Snapshot());
}

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

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

  builder.AddText(u16_text);

  builder.Pop();

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

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

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

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

645 646 647
  ASSERT_TRUE(Snapshot());
}

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

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

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

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

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

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

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

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

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

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

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

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

  ASSERT_TRUE(Snapshot());
}

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

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

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

  ASSERT_TRUE(Snapshot());
}

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

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

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

879 880
  ASSERT_TRUE(Snapshot());

881 882 883
  // 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
884 885
  // drawn (namely, when using "justify" alignment) and therefore are not active
  // glyphs.
886 887 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
  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);
914 915
}

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

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

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

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

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

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

  // TODO(garyq): The following set of vals are definetly wrong and
  // end of paragraph handling needs to be fixed in a later patch.
1007 1008 1009 1010
  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);
1011

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

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

1030 1031 1032
  ASSERT_TRUE(Snapshot());
}

1033 1034 1035 1036 1037 1038
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();
}

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

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

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

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

  EXPECT_EQ(paragraph->GetWordBoundary(0), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(1), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(2), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(3), SkIPoint::Make(0, 5));
  EXPECT_EQ(paragraph->GetWordBoundary(4), SkIPoint::Make(0, 5));
1083
  rect = GetCoordinatesForGlyphPosition(*paragraph, 5);
1084
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1085 1086

  EXPECT_EQ(paragraph->GetWordBoundary(5), SkIPoint::Make(5, 6));
1087
  rect = GetCoordinatesForGlyphPosition(*paragraph, 6);
1088 1089
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

1090
  EXPECT_EQ(paragraph->GetWordBoundary(6), SkIPoint::Make(6, 7));
1091
  rect = GetCoordinatesForGlyphPosition(*paragraph, 7);
1092
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1093 1094 1095 1096 1097 1098

  EXPECT_EQ(paragraph->GetWordBoundary(7), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(8), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(9), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(10), SkIPoint::Make(7, 12));
  EXPECT_EQ(paragraph->GetWordBoundary(11), SkIPoint::Make(7, 12));
1099
  rect = GetCoordinatesForGlyphPosition(*paragraph, 12);
1100
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1101 1102

  EXPECT_EQ(paragraph->GetWordBoundary(12), SkIPoint::Make(12, 13));
1103
  rect = GetCoordinatesForGlyphPosition(*paragraph, 13);
1104
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
1105 1106

  EXPECT_EQ(paragraph->GetWordBoundary(13), SkIPoint::Make(13, 18));
1107
  rect = GetCoordinatesForGlyphPosition(*paragraph, 18);
1108 1109
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

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

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

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

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

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

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

1129 1130
  EXPECT_EQ(paragraph->GetWordBoundary(icu_text.length() - 1),
            SkIPoint::Make(icu_text.length() - 5, icu_text.length()));
1131
  rect = GetCoordinatesForGlyphPosition(*paragraph, icu_text.length());
1132 1133 1134
  GetCanvas()->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);

  ASSERT_TRUE(Snapshot());
1135 1136
}

1137
TEST_F(ParagraphTest, SpacingParagraph) {
G
Gary Qian 已提交
1138 1139 1140 1141 1142 1143 1144 1145
  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;
1146
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1147 1148 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

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

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

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

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

1266
TEST_F(ParagraphTest, KernScaleParagraph) {
1267 1268
  float scale = 3.0f;

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

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

  builder.Pop();

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

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

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

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
G
Gary Qian 已提交
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

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

  builder.Pop();

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

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

  ASSERT_TRUE(Snapshot());

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

  ASSERT_EQ(paragraph->records_.size(), 10ull);
  EXPECT_DOUBLE_EQ(paragraph->records_[0].offset().x(), 0);
1347
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().x(), 127.69921875);
G
Gary Qian 已提交
1348 1349 1350 1351
  EXPECT_DOUBLE_EQ(paragraph->records_[1].offset().y(),
                   paragraph->records_[0].offset().y());
  EXPECT_DOUBLE_EQ(paragraph->records_[2].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().x(), 0);
1352
  EXPECT_DOUBLE_EQ(paragraph->records_[4].offset().x(), 586.9921875);
G
Gary Qian 已提交
1353 1354 1355
  EXPECT_DOUBLE_EQ(paragraph->records_[3].offset().y(),
                   paragraph->records_[4].offset().y());
  EXPECT_DOUBLE_EQ(paragraph->records_[5].offset().x(), 0);
1356
  EXPECT_DOUBLE_EQ(paragraph->records_[6].offset().x(), 127.69921875);
G
Gary Qian 已提交
1357 1358
  EXPECT_DOUBLE_EQ(paragraph->records_[7].offset().x(), 0);
  EXPECT_DOUBLE_EQ(paragraph->records_[8].offset().x(), 0);
1359 1360
  EXPECT_DOUBLE_EQ(paragraph->records_[7].offset().y(), 336);
  EXPECT_DOUBLE_EQ(paragraph->records_[8].offset().y(), 406);
1361 1362
}

1363
TEST_F(ParagraphTest, EmojiParagraph) {
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
  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;
1374 1375

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

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

1390
  paragraph->Paint(GetCanvas(), 0, 0);
1391 1392 1393

  ASSERT_TRUE(Snapshot());

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

1398 1399 1400 1401 1402 1403 1404
  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 已提交
1405 1406
}

1407
TEST_F(ParagraphTest, HyphenBreakParagraph) {
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  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;
1419 1420

  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444

  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);
1445 1446
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1447
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1448
  ASSERT_EQ(paragraph->GetLineCount(), 5ull);
1449 1450 1451
  ASSERT_TRUE(Snapshot());
}

1452
TEST_F(ParagraphTest, RepeatLayoutParagraph) {
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
  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;
1463
  txt::ParagraphBuilder builder(paragraph_style, GetTestFontCollection());
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488

  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);
1489 1490
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1491
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1492
  ASSERT_EQ(paragraph->GetLineCount(), 12ull);
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504

  // 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);
1505 1506
  ASSERT_EQ(paragraph->runs_.styles_.size(), 2ull);
  ASSERT_TRUE(paragraph->runs_.styles_[1].equals(text_style));
1507
  ASSERT_EQ(paragraph->records_[0].style().color, text_style.color);
1508
  ASSERT_EQ(paragraph->GetLineCount(), 6ull);
1509 1510 1511
  ASSERT_TRUE(Snapshot());
}

1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
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);
}

1544
}  // namespace txt