Debian Codegraph @

modules/skparagraph/tests/SkParagraphTest.cpp

raw
1 /*
2 * Copyright 2019 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #include "include/core/SkBitmap.h"
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkFontMgr.h"
11 #include "include/core/SkFontStyle.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSpan.h"
18 #include "include/core/SkStream.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypeface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/encode/SkPngEncoder.h"
23 #include "modules/skparagraph/include/DartTypes.h"
24 #include "modules/skparagraph/include/FontCollection.h"
25 #include "modules/skparagraph/include/Paragraph.h"
26 #include "modules/skparagraph/include/ParagraphCache.h"
27 #include "modules/skparagraph/include/ParagraphStyle.h"
28 #include "modules/skparagraph/include/TextShadow.h"
29 #include "modules/skparagraph/include/TextStyle.h"
30 #include "modules/skparagraph/include/TypefaceFontProvider.h"
31 #include "modules/skparagraph/src/OneLineShaper.h"
32 #include "modules/skparagraph/src/ParagraphBuilderImpl.h"
33 #include "modules/skparagraph/src/ParagraphImpl.h"
34 #include "modules/skparagraph/src/Run.h"
35 #include "modules/skparagraph/src/TextLine.h"
36 #include "modules/skparagraph/tests/SkShaperJSONWriter.h"
37 #include "modules/skparagraph/utils/TestFontCollection.h"
38 #include "modules/skshaper/utils/FactoryHelpers.h"
39 #include "src/base/SkTSort.h"
40 #include "src/core/SkOSFile.h"
41 #include "src/utils/SkOSPath.h"
42 #include "tests/Test.h"
43 #include "tools/Resources.h"
44 #include "tools/flags/CommandLineFlags.h"
45 #include "tools/fonts/FontToolUtils.h"
46
47 #include <string.h>
48 #include <algorithm>
49 #include <limits>
50 #include <memory>
51 #include <string>
52 #include <utility>
53 #include <vector>
54 #include <thread>
55
56 #include "modules/skunicode/include/SkUnicode.h"
57
58 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
59 #include "modules/skunicode/include/SkUnicode_icu.h"
60 #endif
61
62 #if defined(SK_UNICODE_LIBGRAPHEME_IMPLEMENTATION)
63 #include "modules/skunicode/include/SkUnicode_libgrapheme.h"
64 #endif
65
66 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
67 #include "modules/skunicode/include/SkUnicode_icu4x.h"
68 #endif
69
70 using namespace skia_private;
71
72 struct GrContextOptions;
73
74 static DEFINE_string(paragraph_fonts, "",
75 "subdirectory of //resources for fonts to use for these tests");
76 static DEFINE_bool(run_paragraph_tests_needing_system_fonts, true,
77 "Some tests are finicky and need certain system fonts. "
78 "Set this to false to skip those.");
79
80 #define VeryLongCanvasWidth 1000000
81 #define TestCanvasWidth 1000
82 #define TestCanvasHeight 600
83
84 using namespace skia::textlayout;
85 namespace {
86
87 SkScalar EPSILON1000 = 0.001f;
88 SkScalar EPSILON100 = 0.01f;
89 SkScalar EPSILON50 = 0.02f;
90 SkScalar EPSILON20 = 0.05f;
91 SkScalar EPSILON10 = 0.1f;
92 SkScalar EPSILON5 = 0.20f;
93 SkScalar EPSILON2 = 0.50f;
94
95 bool equal(const char* base, TextRange a, const char* b) {
96 return std::strncmp(b, base + a.start, a.width()) == 0;
97 }
98
99 std::u16string mirror(const std::string& text) {
100 std::u16string result;
101 result += u"\u202E";
102 for (auto i = text.size(); i > 0; --i) {
103 result += text[i - 1];
104 }
105 //result += u"\u202C";
106 return result;
107 }
108
109 std::u16string straight(const std::string& text) {
110 std::u16string result;
111 result += u"\u202D";
112 for (auto ch : text) {
113 result += ch;
114 }
115 return result;
116 }
117
118 class ResourceFontCollection : public FontCollection {
119 static const std::vector<sk_sp<SkTypeface>>& getTypefaces() {
120 static std::vector<sk_sp<SkTypeface>> typefaces = []() -> std::vector<sk_sp<SkTypeface>> {
121 if (FLAGS_paragraph_fonts.size() == 0) {
122 return {};
123 }
124 TArray<SkString> paths;
125 {
126 SkString fontResources = GetResourcePath(FLAGS_paragraph_fonts[0]);
127 const char* fontDir = fontResources.c_str();
128 SkOSFile::Iter iter(fontDir);
129 SkString path;
130 while (iter.next(&path)) {
131 if ((false)) {
132 SkDebugf("Found font file: %s\n", path.c_str());
133 }
134 SkString fullPath;
135 fullPath.printf("%s/%s", fontDir, path.c_str());
136 paths.emplace_back(fullPath);
137 }
138 if (paths.size()) {
139 SkTQSort(paths.begin(), paths.end(),
140 [](const SkString& a, const SkString& b) {
141 return strcmp(a.c_str(), b.c_str()) < 0;
142 });
143 }
144 }
145
146 sk_sp<SkFontMgr> mgr = ToolUtils::TestFontMgr();
147 std::vector<sk_sp<SkTypeface>> typefaces;
148 bool fontsFound = false;
149 for (auto&& path : paths) {
150 if ((false)) {
151 SkDebugf("Reading font: %s\n", path.c_str());
152 }
153 auto stream = SkStream::MakeFromFile(path.c_str());
154 SkASSERTF(stream, "%s not readable", path.c_str());
155 sk_sp<SkTypeface> typeface = mgr->makeFromStream(std::move(stream), {});
156 // Without --nativeFonts, DM will use the portable test font manager which does
157 // not know how to read in fonts from bytes.
158 if (typeface) {
159 if ((false)) {
160 SkString familyName;
161 typeface->getFamilyName(&familyName);
162 SkDebugf("Creating: %s size: %zu\n",
163 familyName.c_str(),
164 typeface->openExistingStream(nullptr)->getLength());
165 }
166 if (path.endsWith("Roboto-Italic.ttf")) {
167 fontsFound = true;
168 }
169 typefaces.emplace_back(std::move(typeface));
170 } else {
171 SkDEBUGF("%s was not turned into a Typeface. Did you set --nativeFonts?\n",
172 path.c_str());
173 }
174 }
175 SkASSERTF_RELEASE(typefaces.size(), "--paragraph_fonts set but no fonts found."
176 "Did you set --nativeFonts?");
177 SkASSERTF_RELEASE(fontsFound, "--paragraph_fonts set but Roboto-Italic.ttf not found");
178 return typefaces;
179 }();
180 return typefaces;
181 }
182 public:
183 ResourceFontCollection(bool testOnly = false)
184 : fFontsFound(false)
185 , fResolvedFonts(0)
186 , fFontProvider(sk_make_sp<TypefaceFontProvider>()) {
187 const std::vector<sk_sp<SkTypeface>>& typefaces = getTypefaces();
188 fFontsFound = !typefaces.empty();
189 for (auto&& typeface : typefaces) {
190 fFontProvider->registerTypeface(typeface);
191 }
192
193 if (testOnly) {
194 this->setTestFontManager(std::move(fFontProvider));
195 } else {
196 this->setAssetFontManager(std::move(fFontProvider));
197 }
198 this->disableFontFallback();
199 }
200
201 size_t resolvedFonts() const { return fResolvedFonts; }
202
203 // TODO: temp solution until we check in fonts
204 bool fontsFound() const { return fFontsFound; }
205
206 private:
207 bool fFontsFound;
208 size_t fResolvedFonts;
209 sk_sp<TypefaceFontProvider> fFontProvider;
210 };
211
212 class TestCanvas {
213 public:
214 TestCanvas(const char* testName) : name(testName) {
215 bits.allocN32Pixels(TestCanvasWidth, TestCanvasHeight);
216 canvas = new SkCanvas(bits);
217 canvas->clear(SK_ColorWHITE);
218 }
219
220 ~TestCanvas() {
221 SkString tmpDir = skiatest::GetTmpDir();
222 if (!tmpDir.isEmpty()) {
223 SkString path = SkOSPath::Join(tmpDir.c_str(), name);
224 SkFILEWStream file(path.c_str());
225 if (!SkPngEncoder::Encode(&file, bits.pixmap(), {})) {
226 SkDebugf("Cannot write a picture %s\n", name);
227 }
228 }
229 delete canvas;
230 }
231
232 void drawRects(SkColor color, std::vector<TextBox>& result, bool fill = false) {
233
234 SkPaint paint;
235 if (!fill) {
236 paint.setStyle(SkPaint::kStroke_Style);
237 paint.setAntiAlias(true);
238 paint.setStrokeWidth(1);
239 }
240 paint.setColor(color);
241 for (auto& r : result) {
242 canvas->drawRect(r.rect, paint);
243 }
244 }
245
246 void drawLine(SkColor color, SkRect rect, bool vertical = true) {
247
248 SkPaint paint;
249 paint.setStyle(SkPaint::kStroke_Style);
250 paint.setAntiAlias(true);
251 paint.setStrokeWidth(1);
252 paint.setColor(color);
253 if (vertical) {
254 canvas->drawLine(rect.fLeft, rect.fTop, rect.fLeft, rect.fBottom, paint);
255 } else {
256 canvas->drawLine(rect.fLeft, rect.fTop, rect.fRight, rect.fTop, paint);
257 }
258 }
259
260 void drawLines(SkColor color, std::vector<TextBox>& result) {
261
262 for (auto& r : result) {
263 drawLine(color, r.rect);
264 }
265 }
266
267 SkCanvas* get() { return canvas; }
268 private:
269 SkBitmap bits;
270 SkCanvas* canvas;
271 const char* name;
272 };
273
274 static sk_sp<SkUnicode> get_unicode() {
275 auto factory = SkShapers::BestAvailable();
276 return sk_ref_sp<SkUnicode>(factory->getUnicode());
277 }
278
279 } // namespace
280
281 // Skip tests which do not find the fonts, unless the user set --paragraph_fonts in which case
282 // we should make a loud error.
283 #define SKIP_IF_FONTS_NOT_FOUND(r, fontCollection) \
284 if (!fontCollection->fontsFound()) { \
285 if (FLAGS_paragraph_fonts.size() != 0) { \
286 ERRORF(r, "SkParagraphTests Fonts not found!\n"); \
287 } \
288 return; \
289 }
290
291 #define NEED_SYSTEM_FONTS(fontCollection) \
292 if (!FLAGS_run_paragraph_tests_needing_system_fonts) { \
293 return; \
294 } \
295 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr()); \
296 fontCollection->enableFontFallback();
297
298 UNIX_ONLY_TEST(SkParagraph_SimpleParagraph, reporter) {
299 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
300 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
301 const char* text = "Hello World Text Dialog";
302 const size_t len = strlen(text);
303
304 ParagraphStyle paragraph_style;
305 paragraph_style.turnHintingOff();
306 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
307
308 TextStyle text_style;
309 text_style.setFontFamilies({SkString("Roboto")});
310 text_style.setColor(SK_ColorBLACK);
311 builder.pushStyle(text_style);
312 builder.addText(text, len);
313 builder.pop();
314
315 auto paragraph = builder.Build();
316 paragraph->layout(TestCanvasWidth);
317 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
318
319 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
320 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
321 REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
322 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
323
324 size_t index = 0;
325 for (auto& line : impl->lines()) {
326 line.scanStyles(StyleType::kDecorations,
327 [&index, reporter]
328 (TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
329 REPORTER_ASSERT(reporter, index == 0);
330 REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK);
331 ++index;
332 });
333 }
334 }
335
336 UNIX_ONLY_TEST(SkParagraph_Rounding_Off_LineBreaks, reporter) {
337 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
338 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
339 const char* text = "AAAAAAAAAA";
340 const size_t len = strlen(text);
341
342 ParagraphStyle paragraph_style;
343 paragraph_style.turnHintingOff();
344 paragraph_style.setApplyRoundingHack(false);
345 TextStyle text_style;
346 text_style.setFontFamilies({SkString("Ahem")});
347 text_style.setColor(SK_ColorBLACK);
348
349 auto testFontSize = {1.5f, 10.0f/3, 10.0f/6, 10.0f};
350 for (auto fontSize : testFontSize) {
351 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
352 text_style.setFontSize(fontSize);
353 builder.pushStyle(text_style);
354 builder.addText(text, len);
355 builder.pop();
356
357 auto paragraph = builder.Build();
358 paragraph->layout(SK_ScalarInfinity);
359 // Slightly wider than the max intrinsic width.
360 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
361 paragraph->layout(paragraph->getMaxIntrinsicWidth());
362
363 ParagraphImpl* impl = static_cast<ParagraphImpl*>(paragraph.get());
364
365 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
366 auto& line = impl->lines()[0];
367
368 const LineMetrics metrics = line.getMetrics();
369 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(metrics.fWidth, fontSize * len, EPSILON2));
370 }
371 }
372
373 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderParagraph, reporter) {
374 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
375 TestCanvas canvas("SkParagraph_InlinePlaceholderParagraph.png");
376 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
377
378 const char* text = "012 34";
379 const size_t len = strlen(text);
380
381 ParagraphStyle paragraph_style;
382 paragraph_style.turnHintingOff();
383 paragraph_style.setMaxLines(14);
384 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
385
386 TextStyle text_style;
387 text_style.setFontFamilies({SkString("Roboto")});
388 text_style.setColor(SK_ColorBLACK);
389 text_style.setFontSize(26);
390 text_style.setWordSpacing(5);
391 text_style.setLetterSpacing(1);
392 text_style.setDecoration(TextDecoration::kUnderline);
393 text_style.setDecorationColor(SK_ColorBLACK);
394 builder.pushStyle(text_style);
395 builder.addText(text, len);
396
397 PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0);
398 builder.addPlaceholder(placeholder1);
399 builder.addText(text, len);
400 builder.addPlaceholder(placeholder1);
401
402 PlaceholderStyle placeholder2(5, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50);
403 builder.addPlaceholder(placeholder2);
404 builder.addPlaceholder(placeholder1);
405 builder.addPlaceholder(placeholder2);
406 builder.addText(text, len);
407 builder.addPlaceholder(placeholder2);
408 builder.addText(text, len);
409 builder.addText(text, len);
410 builder.addPlaceholder(placeholder2);
411 builder.addPlaceholder(placeholder2);
412 builder.addPlaceholder(placeholder2);
413 builder.addPlaceholder(placeholder2);
414 builder.addPlaceholder(placeholder2);
415 builder.addPlaceholder(placeholder1);
416 builder.addText(text, len);
417 builder.addText(text, len);
418 builder.addText(text, len);
419 builder.addText(text, len);
420 builder.addText(text, len);
421 builder.addPlaceholder(placeholder2);
422 builder.addPlaceholder(placeholder1);
423 builder.addText(text, len);
424 builder.addText(text, len);
425
426 builder.pop();
427
428 auto paragraph = builder.Build();
429 paragraph->layout(TestCanvasWidth);
430 paragraph->paint(canvas.get(), 0, 0);
431
432 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
433 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
434
435 auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style);
436 canvas.drawRects(SK_ColorRED, boxes);
437 REPORTER_ASSERT(reporter, boxes.size() == 1);
438
439 boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style);
440 canvas.drawRects(SK_ColorGREEN, boxes);
441 REPORTER_ASSERT(reporter, boxes.size() == 1);
442
443 boxes = paragraph->getRectsForPlaceholders();
444 canvas.drawRects(SK_ColorRED, boxes);
445
446 boxes = paragraph->getRectsForRange(4, 17, rect_height_style, rect_width_style);
447 canvas.drawRects(SK_ColorBLUE, boxes);
448
449 REPORTER_ASSERT(reporter, boxes.size() == 7);
450 if (boxes.size() >= 7) {
451 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 90.921f, EPSILON2));
452 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 50, EPSILON100));
453 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 90.921f + 50, EPSILON2));
454 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 100, EPSILON100));
455
456 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.left(), 231.343f, EPSILON2));
457 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.top(), 50, EPSILON100));
458 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.right(), 231.343f + 50, EPSILON2));
459 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.bottom(), 100, EPSILON100));
460
461 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.left(), 281.343f, EPSILON2));
462 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.top(), 0, EPSILON100));
463 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.right(), 281.343f + 5, EPSILON2));
464 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[4].rect.bottom(), 50, EPSILON100));
465
466 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.left(), 336.343f, EPSILON2));
467 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.top(), 0, EPSILON100));
468 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.right(), 336.343f + 5, EPSILON2));
469 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[6].rect.bottom(), 50, EPSILON100));
470 }
471 }
472
473 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderBaselineParagraph, reporter) {
474 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
475 TestCanvas canvas("SkParagraph_InlinePlaceholderBaselineParagraph.png");
476 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
477
478 const char* text = "012 34";
479 const size_t len = strlen(text);
480
481 ParagraphStyle paragraph_style;
482 paragraph_style.turnHintingOff();
483 paragraph_style.setMaxLines(14);
484 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
485
486 TextStyle text_style;
487 text_style.setFontFamilies({SkString("Roboto")});
488 text_style.setColor(SK_ColorBLACK);
489 text_style.setFontSize(26);
490 text_style.setWordSpacing(5);
491 text_style.setLetterSpacing(1);
492 text_style.setDecoration(TextDecoration::kUnderline);
493 text_style.setDecorationColor(SK_ColorBLACK);
494 builder.pushStyle(text_style);
495 builder.addText(text, len);
496
497 PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 38.347f);
498 builder.addPlaceholder(placeholder);
499 builder.addText(text, len);
500
501 builder.pop();
502
503 auto paragraph = builder.Build();
504 paragraph->layout(TestCanvasWidth);
505 paragraph->paint(canvas.get(), 0, 0);
506
507 auto boxes = paragraph->getRectsForPlaceholders();
508 canvas.drawRects(SK_ColorRED, boxes);
509
510 REPORTER_ASSERT(reporter, boxes.size() == 1);
511 if (boxes.size() >= 1) {
512 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2));
513 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
514 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2));
515 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100));
516 }
517
518 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
519 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
520
521 boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style);
522 canvas.drawRects(SK_ColorBLUE, boxes);
523
524 REPORTER_ASSERT(reporter, boxes.size() == 1);
525 if (boxes.size() >= 1) {
526 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2));
527 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 14.226f, EPSILON100));
528 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2));
529 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44.694f, EPSILON100));
530 }
531 }
532
533 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderAboveBaselineParagraph, reporter) {
534 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
535 TestCanvas canvas("SkParagraph_InlinePlaceholderAboveBaselineParagraph.png");
536 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
537
538 const char* text = "012 34";
539 const size_t len = strlen(text);
540
541 ParagraphStyle paragraph_style;
542 paragraph_style.turnHintingOff();
543 paragraph_style.setMaxLines(14);
544 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
545
546 TextStyle text_style;
547 text_style.setFontFamilies({SkString("Roboto")});
548 text_style.setColor(SK_ColorBLACK);
549 text_style.setFontSize(26);
550 text_style.setWordSpacing(5);
551 text_style.setLetterSpacing(1);
552 text_style.setDecoration(TextDecoration::kUnderline);
553 text_style.setDecorationColor(SK_ColorBLACK);
554 builder.pushStyle(text_style);
555 builder.addText(text, len);
556
557 PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kAboveBaseline, TextBaseline::kAlphabetic, 903129.129308f);
558 builder.addPlaceholder(placeholder);
559 builder.addText(text, len);
560
561 builder.pop();
562
563 auto paragraph = builder.Build();
564 paragraph->layout(TestCanvasWidth);
565 paragraph->paint(canvas.get(), 0, 0);
566
567 auto boxes = paragraph->getRectsForPlaceholders();
568 canvas.drawRects(SK_ColorRED, boxes);
569
570 REPORTER_ASSERT(reporter, boxes.size() == 1);
571 if (boxes.size() >= 1) {
572 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2));
573 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.347f, EPSILON100));
574 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2));
575 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 49.652f, EPSILON100));
576 }
577
578 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
579 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
580
581 boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style);
582 canvas.drawRects(SK_ColorBLUE, boxes);
583
584 REPORTER_ASSERT(reporter, boxes.size() == 1);
585 if (boxes.size() >= 1) {
586 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2));
587 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 25.531f, EPSILON100));
588 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2));
589 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56, EPSILON100));
590 }
591 }
592
593 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderBelowBaselineParagraph, reporter) {
594 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
595 TestCanvas canvas("SkParagraph_InlinePlaceholderBelowBaselineParagraph.png");
596 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
597
598 const char* text = "012 34";
599 const size_t len = strlen(text);
600
601 ParagraphStyle paragraph_style;
602 paragraph_style.turnHintingOff();
603 paragraph_style.setMaxLines(14);
604 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
605
606 TextStyle text_style;
607 text_style.setFontFamilies({SkString("Roboto")});
608 text_style.setColor(SK_ColorBLACK);
609 text_style.setFontSize(26);
610 text_style.setWordSpacing(5);
611 text_style.setLetterSpacing(1);
612 text_style.setDecoration(TextDecoration::kUnderline);
613 text_style.setDecorationColor(SK_ColorBLACK);
614 builder.pushStyle(text_style);
615 builder.addText(text, len);
616
617 PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBelowBaseline, TextBaseline::kAlphabetic, 903129.129308f);
618 builder.addPlaceholder(placeholder);
619 builder.addText(text, len);
620
621 builder.pop();
622
623 auto paragraph = builder.Build();
624 paragraph->layout(TestCanvasWidth);
625 paragraph->paint(canvas.get(), 0, 0);
626
627 auto boxes = paragraph->getRectsForPlaceholders();
628 canvas.drawRects(SK_ColorRED, boxes);
629
630 REPORTER_ASSERT(reporter, boxes.size() == 1);
631 if (boxes.size() >= 1) {
632 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON2));
633 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100));
634 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON2));
635 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100));
636 }
637
638 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
639 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
640
641 boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style);
642 canvas.drawRects(SK_ColorBLUE, boxes);
643
644 REPORTER_ASSERT(reporter, boxes.size() == 1);
645 if (boxes.size() >= 1) {
646 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON2));
647 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.121f, EPSILON100));
648 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON2));
649 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.347f, EPSILON100));
650 }
651 }
652
653 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderBottomParagraph, reporter) {
654 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
655 TestCanvas canvas("SkParagraph_InlinePlaceholderBottomParagraph.png");
656 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
657
658 const char* text = "012 34";
659 const size_t len = strlen(text);
660
661 ParagraphStyle paragraph_style;
662 paragraph_style.turnHintingOff();
663 paragraph_style.setMaxLines(14);
664 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
665
666 TextStyle text_style;
667 text_style.setFontFamilies({SkString("Roboto")});
668 text_style.setColor(SK_ColorBLACK);
669 text_style.setFontSize(26);
670 text_style.setWordSpacing(5);
671 text_style.setLetterSpacing(1);
672 text_style.setDecoration(TextDecoration::kUnderline);
673 text_style.setDecorationColor(SK_ColorBLACK);
674 builder.pushStyle(text_style);
675 builder.addText(text, len);
676
677 PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBottom, TextBaseline::kAlphabetic, 0);
678 builder.addPlaceholder(placeholder);
679 builder.addText(text, len);
680
681 builder.pop();
682
683 auto paragraph = builder.Build();
684 paragraph->layout(TestCanvasWidth);
685 paragraph->paint(canvas.get(), 0, 0);
686
687 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
688 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
689
690 auto boxes = paragraph->getRectsForPlaceholders();
691 canvas.drawRects(SK_ColorRED, boxes);
692 REPORTER_ASSERT(reporter, boxes.size() == 1);
693 if (boxes.size() >= 1) {
694 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50));
695 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
696 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50));
697 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100));
698 }
699
700 boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
701 canvas.drawRects(SK_ColorBLUE, boxes);
702 REPORTER_ASSERT(reporter, boxes.size() == 1);
703 if (boxes.size() >= 1) {
704 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f, EPSILON50));
705 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 19.531f, EPSILON100));
706 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f, EPSILON50));
707 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100));
708 }
709 }
710
711 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderTopParagraph, reporter) {
712 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
713 TestCanvas canvas("SkParagraph_InlinePlaceholderTopParagraph.png");
714 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
715
716 const char* text = "012 34";
717 const size_t len = strlen(text);
718
719 ParagraphStyle paragraph_style;
720 paragraph_style.turnHintingOff();
721 paragraph_style.setMaxLines(14);
722 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
723
724 TextStyle text_style;
725 text_style.setFontFamilies({SkString("Roboto")});
726 text_style.setColor(SK_ColorBLACK);
727 text_style.setFontSize(26);
728 text_style.setWordSpacing(5);
729 text_style.setLetterSpacing(1);
730 text_style.setDecoration(TextDecoration::kUnderline);
731 text_style.setDecorationColor(SK_ColorBLACK);
732 builder.pushStyle(text_style);
733 builder.addText(text, len);
734
735 PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kTop, TextBaseline::kAlphabetic, 0);
736 builder.addPlaceholder(placeholder);
737 builder.addText(text, len);
738
739 builder.pop();
740
741 auto paragraph = builder.Build();
742 paragraph->layout(TestCanvasWidth);
743 paragraph->paint(canvas.get(), 0, 0);
744
745 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
746 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
747
748 auto boxes = paragraph->getRectsForPlaceholders();
749 canvas.drawRects(SK_ColorRED, boxes);
750 REPORTER_ASSERT(reporter, boxes.size() == 1);
751 if (boxes.size() >= 1) {
752 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50));
753 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
754 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50));
755 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100));
756 }
757
758 boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
759 canvas.drawRects(SK_ColorBLUE, boxes);
760 REPORTER_ASSERT(reporter, boxes.size() == 1);
761 if (boxes.size() >= 1) {
762 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0.5f, EPSILON50));
763 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
764 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 16.097f, EPSILON50));
765 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 30.468f, EPSILON100));
766 }
767 }
768
769 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderMiddleParagraph, reporter) {
770 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
771 TestCanvas canvas("SkParagraph_InlinePlaceholderMiddleParagraph.png");
772 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
773
774 const char* text = "012 34";
775 const size_t len = strlen(text);
776
777 ParagraphStyle paragraph_style;
778 paragraph_style.turnHintingOff();
779 paragraph_style.setMaxLines(14);
780 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
781
782 TextStyle text_style;
783 text_style.setFontFamilies({SkString("Roboto")});
784 text_style.setColor(SK_ColorBLACK);
785 text_style.setFontSize(26);
786 text_style.setWordSpacing(5);
787 text_style.setLetterSpacing(1);
788 text_style.setDecoration(TextDecoration::kUnderline);
789 text_style.setDecorationColor(SK_ColorBLACK);
790 builder.pushStyle(text_style);
791 builder.addText(text, len);
792
793 PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kMiddle, TextBaseline::kAlphabetic, 0);
794 builder.addPlaceholder(placeholder);
795 builder.addText(text, len);
796
797 builder.pop();
798
799 auto paragraph = builder.Build();
800 paragraph->layout(TestCanvasWidth);
801 paragraph->paint(canvas.get(), 0, 0);
802
803 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
804 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
805
806 auto boxes = paragraph->getRectsForPlaceholders();
807 canvas.drawRects(SK_ColorRED, boxes);
808 REPORTER_ASSERT(reporter, boxes.size() == 1);
809 if (boxes.size() >= 1) {
810 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50));
811 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
812 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f + 55, EPSILON50));
813 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100));
814 }
815
816 boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style);
817 canvas.drawRects(SK_ColorBLUE, boxes);
818 REPORTER_ASSERT(reporter, boxes.size() == 1);
819 if (boxes.size() >= 1) {
820 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 75.324f, EPSILON50));
821 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 9.765f, EPSILON100));
822 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON50));
823 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 40.234f, EPSILON100));
824 }
825 }
826
827 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderIdeographicBaselineParagraph, reporter) {
828 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
829 TestCanvas canvas("SkParagraph_InlinePlaceholderIdeographicBaselineParagraph.png");
830 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
831
832 const char* text = "給能上目秘使";
833 const size_t len = strlen(text);
834
835 ParagraphStyle paragraph_style;
836 paragraph_style.turnHintingOff();
837 paragraph_style.setMaxLines(14);
838 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
839
840 TextStyle text_style;
841 text_style.setFontFamilies({SkString("Source Han Serif CN")});
842 text_style.setColor(SK_ColorBLACK);
843 text_style.setFontSize(26);
844 text_style.setWordSpacing(5);
845 text_style.setLetterSpacing(1);
846 text_style.setDecoration(TextDecoration::kUnderline);
847 text_style.setDecorationColor(SK_ColorBLACK);
848 builder.pushStyle(text_style);
849 builder.addText(text, len);
850 PlaceholderStyle placeholder(55, 50, PlaceholderAlignment::kBaseline, TextBaseline::kIdeographic, 38.347f);
851 builder.addPlaceholder(placeholder);
852 builder.addText(text, len);
853
854 builder.pop();
855
856 auto paragraph = builder.Build();
857 paragraph->layout(TestCanvasWidth);
858 paragraph->paint(canvas.get(), 0, 0);
859
860 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
861 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
862
863 auto boxes = paragraph->getRectsForPlaceholders();
864 canvas.drawRects(SK_ColorRED, boxes);
865 REPORTER_ASSERT(reporter, boxes.size() == 1);
866 if (boxes.size() >= 1) {
867 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 162.5f, EPSILON50));
868 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
869 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f + 55, EPSILON50));
870 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100));
871 }
872
873 boxes = paragraph->getRectsForRange(5, 6, rect_height_style, rect_width_style);
874 canvas.drawRects(SK_ColorBLUE, boxes);
875 REPORTER_ASSERT(reporter, boxes.size() == 1);
876 if (boxes.size() >= 1) {
877 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 135.5f, EPSILON50));
878 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 4.703f, EPSILON100));
879 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 162.5f, EPSILON50));
880 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 42.065f, EPSILON100));
881 }
882 }
883
884 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderBreakParagraph, reporter) {
885 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
886 TestCanvas canvas("SkParagraph_InlinePlaceholderBreakParagraph.png");
887 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
888
889 const char* text = "012 34";
890 const size_t len = strlen(text);
891
892 ParagraphStyle paragraph_style;
893 paragraph_style.turnHintingOff();
894 paragraph_style.setMaxLines(14);
895 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
896
897 TextStyle text_style;
898 text_style.setFontFamilies({SkString("Roboto")});
899 text_style.setColor(SK_ColorBLACK);
900 text_style.setFontSize(26);
901 text_style.setWordSpacing(5);
902 text_style.setLetterSpacing(1);
903 text_style.setDecoration(TextDecoration::kUnderline);
904 text_style.setDecorationColor(SK_ColorBLACK);
905 builder.pushStyle(text_style);
906 builder.addText(text, len);
907
908 PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50);
909 PlaceholderStyle placeholder2(25, 25, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 12.5f);
910
911 builder.addPlaceholder(placeholder1);
912 builder.addPlaceholder(placeholder1);
913 builder.addPlaceholder(placeholder1);
914 builder.addPlaceholder(placeholder2);
915 builder.addPlaceholder(placeholder1);
916 builder.addText(text, len);
917
918 builder.addPlaceholder(placeholder1);
919 builder.addPlaceholder(placeholder1);
920 builder.addPlaceholder(placeholder1);
921 builder.addPlaceholder(placeholder1);
922 builder.addPlaceholder(placeholder2); // 4 + 1
923 builder.addPlaceholder(placeholder1);
924 builder.addPlaceholder(placeholder1);
925 builder.addPlaceholder(placeholder1);
926 builder.addPlaceholder(placeholder1);
927 builder.addPlaceholder(placeholder1);
928 builder.addPlaceholder(placeholder1);
929 builder.addPlaceholder(placeholder2); // 6 + 1
930 builder.addPlaceholder(placeholder1);
931 builder.addPlaceholder(placeholder1);
932 builder.addPlaceholder(placeholder1);
933 builder.addPlaceholder(placeholder1);
934 builder.addPlaceholder(placeholder1);
935 builder.addPlaceholder(placeholder1);
936 builder.addPlaceholder(placeholder1);
937 builder.addPlaceholder(placeholder2); // 7 + 1
938
939 builder.addPlaceholder(placeholder1);
940 builder.addText(text, len);
941 builder.addPlaceholder(placeholder1);
942 builder.addPlaceholder(placeholder2);
943
944 builder.addText(text, len);
945 builder.addText(text, len);
946 builder.addText(text, len);
947 builder.addText(text, len);
948
949 builder.addPlaceholder(placeholder2);
950 builder.addPlaceholder(placeholder1);
951
952 builder.addText(text, len);
953
954 builder.addPlaceholder(placeholder2);
955
956 builder.addText(text, len);
957 builder.addText(text, len);
958 builder.addText(text, len);
959 builder.addText(text, len);
960 builder.addText(text, len);
961 builder.addText(text, len);
962 builder.addText(text, len);
963 builder.addText(text, len);
964 builder.addText(text, len);
965 builder.addText(text, len);
966 builder.addText(text, len);
967 builder.addText(text, len);
968 builder.addText(text, len);
969 builder.addText(text, len);
970 builder.addText(text, len);
971 builder.addText(text, len);
972 builder.addText(text, len);
973 builder.addText(text, len);
974 builder.addText(text, len);
975
976 builder.pop();
977
978 auto paragraph = builder.Build();
979 paragraph->layout(TestCanvasWidth - 100);
980 paragraph->paint(canvas.get(), 0, 0);
981
982 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
983 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
984
985 auto boxes = paragraph->getRectsForRange(0, 3, rect_height_style, rect_width_style);
986 canvas.drawRects(SK_ColorRED, boxes);
987 REPORTER_ASSERT(reporter, boxes.size() == 1);
988
989 boxes = paragraph->getRectsForRange(175, 176, rect_height_style, rect_width_style);
990 canvas.drawRects(SK_ColorGREEN, boxes);
991 REPORTER_ASSERT(reporter, boxes.size() == 1);
992 if (boxes.size() >= 1) {
993 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 31.695f, EPSILON50));
994 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 218.531f, EPSILON100));
995 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 47.292f, EPSILON50));
996 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 249, EPSILON100));
997 }
998
999 boxes = paragraph->getRectsForPlaceholders();
1000 canvas.drawRects(SK_ColorRED, boxes);
1001
1002 boxes = paragraph->getRectsForRange(4, 45, rect_height_style, rect_width_style);
1003 canvas.drawRects(SK_ColorBLUE, boxes);
1004 REPORTER_ASSERT(reporter, boxes.size() == 30);
1005 if (boxes.size() >= 30) {
1006 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 59.726f, EPSILON50));
1007 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.378f, EPSILON100));
1008 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 90.921f, EPSILON50));
1009 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 56.847f, EPSILON100));
1010
1011 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.left(), 606.343f, EPSILON20));
1012 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.top(), 38, EPSILON100));
1013 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.right(), 631.343f, EPSILON20));
1014 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[11].rect.bottom(), 63, EPSILON100));
1015
1016 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.left(), 0.5f, EPSILON50));
1017 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.top(), 63.5f, EPSILON100));
1018 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.right(), 50.5f, EPSILON50));
1019 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[17].rect.bottom(), 113.5f, EPSILON100));
1020 }
1021 }
1022
1023 UNIX_ONLY_TEST(SkParagraph_InlinePlaceholderGetRectsParagraph, reporter) {
1024 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1025 TestCanvas canvas("SkParagraph_InlinePlaceholderGetRectsParagraph.png");
1026 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1027
1028 const char* text = "012 34";
1029 const size_t len = strlen(text);
1030
1031 ParagraphStyle paragraph_style;
1032 paragraph_style.turnHintingOff();
1033 paragraph_style.setMaxLines(14);
1034 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1035
1036 TextStyle text_style;
1037 text_style.setFontFamilies({SkString("Roboto")});
1038 text_style.setColor(SK_ColorBLACK);
1039 text_style.setFontSize(26);
1040 text_style.setWordSpacing(5);
1041 text_style.setLetterSpacing(1);
1042 text_style.setDecoration(TextDecoration::kUnderline);
1043 text_style.setDecorationColor(SK_ColorBLACK);
1044 builder.pushStyle(text_style);
1045 builder.addText(text, len);
1046
1047 PlaceholderStyle placeholder1(50, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 50);
1048 PlaceholderStyle placeholder2(5, 20, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 10);
1049
1050 builder.addPlaceholder(placeholder1);
1051 builder.addPlaceholder(placeholder1);
1052 builder.addPlaceholder(placeholder1);
1053 builder.addPlaceholder(placeholder1);
1054 builder.addPlaceholder(placeholder1);
1055 builder.addPlaceholder(placeholder1);
1056 builder.addPlaceholder(placeholder1);
1057 builder.addPlaceholder(placeholder1);
1058 builder.addPlaceholder(placeholder2); // 8 + 1
1059 builder.addPlaceholder(placeholder1);
1060 builder.addPlaceholder(placeholder1);
1061 builder.addPlaceholder(placeholder1);
1062 builder.addPlaceholder(placeholder1);
1063 builder.addPlaceholder(placeholder1);
1064 builder.addPlaceholder(placeholder2); // 5 + 1
1065 builder.addPlaceholder(placeholder1);
1066 builder.addPlaceholder(placeholder1);
1067 builder.addPlaceholder(placeholder1);
1068 builder.addPlaceholder(placeholder1);
1069 builder.addPlaceholder(placeholder1);
1070 builder.addPlaceholder(placeholder1);
1071 builder.addPlaceholder(placeholder1);
1072 builder.addPlaceholder(placeholder1); // 8 + 0
1073
1074 builder.addText(text, len);
1075
1076 builder.addPlaceholder(placeholder1);
1077 builder.addPlaceholder(placeholder2);
1078 builder.addPlaceholder(placeholder2); // 1 + 2
1079 builder.addPlaceholder(placeholder1);
1080 builder.addPlaceholder(placeholder2);
1081 builder.addPlaceholder(placeholder2); // 1 + 2
1082
1083 builder.addText(text, len);
1084 builder.addText(text, len);
1085 builder.addText(text, len);
1086 builder.addText(text, len);
1087 builder.addText(text, len);
1088 builder.addText(text, len);
1089 builder.addText(text, len);
1090 builder.addText(text, len);
1091 builder.addText(text, len);
1092 builder.addText(text, len);
1093 builder.addText(text, len); // 11
1094
1095 builder.addPlaceholder(placeholder2);
1096 builder.addPlaceholder(placeholder1);
1097 builder.addPlaceholder(placeholder2);
1098 builder.addPlaceholder(placeholder1);
1099 builder.addPlaceholder(placeholder2);
1100
1101 builder.addText(text, len);
1102
1103 builder.pop();
1104
1105 auto paragraph = builder.Build();
1106 paragraph->layout(TestCanvasWidth);
1107 paragraph->paint(canvas.get(), 0, 0);
1108
1109 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
1110 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
1111
1112 auto boxes = paragraph->getRectsForPlaceholders();
1113 canvas.drawRects(SK_ColorRED, boxes);
1114
1115 REPORTER_ASSERT(reporter, boxes.size() == 34);
1116 if (boxes.size() >= 34) {
1117 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 90.921f, EPSILON50));
1118 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
1119 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 140.921f, EPSILON50));
1120 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 50, EPSILON100));
1121
1122 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.left(), 800.921f, EPSILON20));
1123 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.top(), 0, EPSILON100));
1124 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.right(), 850.921f, EPSILON20));
1125 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[16].rect.bottom(), 50, EPSILON100));
1126
1127 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.left(), 503.382f, EPSILON10));
1128 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.top(), 160, EPSILON100));
1129 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.right(), 508.382f, EPSILON10));
1130 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[33].rect.bottom(), 180, EPSILON100));
1131 }
1132
1133 boxes = paragraph->getRectsForRange(30, 50, rect_height_style, rect_width_style);
1134 canvas.drawRects(SK_ColorBLUE, boxes);
1135
1136 REPORTER_ASSERT(reporter, boxes.size() == 8);
1137 if (boxes.size() >= 8) {
1138 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 216.097f, EPSILON50));
1139 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 60, EPSILON100));
1140 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 290.921f, EPSILON50));
1141 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 120, EPSILON100));
1142
1143 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 290.921f, EPSILON20));
1144 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 60, EPSILON100));
1145 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 340.921f, EPSILON20));
1146 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 120, EPSILON100));
1147
1148 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.left(), 340.921f, EPSILON50));
1149 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.top(), 60, EPSILON100));
1150 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.right(), 345.921f, EPSILON50));
1151 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.bottom(), 120, EPSILON100));
1152 }
1153 }
1154
1155 UNIX_ONLY_TEST(SkParagraph_SimpleRedParagraph, reporter) {
1156 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1157 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1158 const char* text = "I am RED";
1159 const size_t len = strlen(text);
1160
1161 ParagraphStyle paragraph_style;
1162 paragraph_style.turnHintingOff();
1163 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1164
1165 TextStyle text_style;
1166 text_style.setFontFamilies({SkString("Roboto")});
1167 text_style.setColor(SK_ColorRED);
1168 builder.pushStyle(text_style);
1169 builder.addText(text, len);
1170 builder.pop();
1171
1172 auto paragraph = builder.Build();
1173 paragraph->layout(TestCanvasWidth);
1174 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
1175
1176 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1177 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
1178 REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
1179 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
1180
1181 size_t index = 0;
1182 for (auto& line : impl->lines()) {
1183 line.scanStyles(StyleType::kDecorations,
1184 [reporter, &index](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
1185 REPORTER_ASSERT(reporter, index == 0);
1186 REPORTER_ASSERT(reporter, style.getColor() == SK_ColorRED);
1187 ++index;
1188 return true;
1189 });
1190 }
1191 }
1192
1193 // Checked: DIFF+ (Space between 1 & 2 style blocks)
1194 UNIX_ONLY_TEST(SkParagraph_RainbowParagraph, reporter) {
1195 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1196 TestCanvas canvas("SkParagraph_RainbowParagraph.png");
1197 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1198 const char* text1 = "Red Roboto"; // [0:10)
1199 const char* text2 = "big Greeen Default"; // [10:28)
1200 const char* text3 = "Defcolor Homemade Apple"; // [28:51)
1201 const char* text4 = "Small Blue Roboto"; // [51:68)
1202 const char* text41 = "Small Blue ";
1203 const char* text5 =
1204 "Continue Last Style With lots of words to check if it overlaps "
1205 "properly or not"; // [68:)
1206 const char* text42 =
1207 "Roboto"
1208 "Continue Last Style With lots of words to check if it overlaps "
1209 "properly or not";
1210
1211 ParagraphStyle paragraph_style;
1212 paragraph_style.turnHintingOff();
1213 paragraph_style.setTextAlign(TextAlign::kLeft);
1214 paragraph_style.setMaxLines(2);
1215 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1216
1217 TextStyle text_style1;
1218 text_style1.setFontFamilies({SkString("Roboto")});
1219
1220 text_style1.setColor(SK_ColorRED);
1221 builder.pushStyle(text_style1);
1222 builder.addText(text1, strlen(text1));
1223
1224 TextStyle text_style2;
1225 text_style2.setFontFamilies({SkString("Roboto")});
1226 text_style2.setFontSize(50);
1227 text_style2.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
1228 SkFontStyle::kUpright_Slant));
1229 text_style2.setLetterSpacing(10);
1230 text_style2.setDecorationColor(SK_ColorBLACK);
1231 text_style2.setDecoration((TextDecoration)(
1232 TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
1233 text_style2.setWordSpacing(30);
1234 text_style2.setColor(SK_ColorGREEN);
1235 builder.pushStyle(text_style2);
1236 builder.addText(text2, strlen(text2));
1237
1238 TextStyle text_style3;
1239 text_style3.setFontFamilies({SkString("Homemade Apple")});
1240 text_style3.setColor(SK_ColorBLACK);
1241 builder.pushStyle(text_style3);
1242 builder.addText(text3, strlen(text3));
1243
1244 TextStyle text_style4;
1245 text_style4.setFontFamilies({SkString("Roboto")});
1246 text_style4.setFontSize(14);
1247 text_style4.setDecorationColor(SK_ColorBLACK);
1248 text_style4.setDecoration((TextDecoration)(
1249 TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
1250 text_style4.setColor(SK_ColorBLUE);
1251 builder.pushStyle(text_style4);
1252 builder.addText(text4, strlen(text4));
1253
1254 builder.addText(text5, strlen(text5));
1255 builder.pop();
1256
1257 auto paragraph = builder.Build();
1258 paragraph->layout(1000);
1259 paragraph->paint(canvas.get(), 0, 0);
1260
1261 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
1262
1263 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1264 REPORTER_ASSERT(reporter, impl->runs().size() == 4);
1265 REPORTER_ASSERT(reporter, impl->styles().size() == 4);
1266 REPORTER_ASSERT(reporter, impl->lines().size() == 2);
1267
1268 auto rects = paragraph->getRectsForRange(0, impl->text().size(), RectHeightStyle::kMax, RectWidthStyle::kTight);
1269 canvas.drawRects(SK_ColorMAGENTA, rects);
1270
1271 size_t index = 0;
1272 impl->lines()[0].scanStyles(
1273 StyleType::kAllAttributes,
1274 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
1275 switch (index) {
1276 case 0:
1277 REPORTER_ASSERT(reporter, style.equals(text_style1));
1278 REPORTER_ASSERT(reporter, equal(impl->text().data(), textRange, text1));
1279 break;
1280 case 1:
1281 REPORTER_ASSERT(reporter, style.equals(text_style2));
1282 REPORTER_ASSERT(reporter, equal(impl->text().data(), textRange, text2));
1283 break;
1284 case 2:
1285 REPORTER_ASSERT(reporter, style.equals(text_style3));
1286 REPORTER_ASSERT(reporter, equal(impl->text().data(), textRange, text3));
1287 break;
1288 case 3:
1289 REPORTER_ASSERT(reporter, style.equals(text_style4));
1290 REPORTER_ASSERT(reporter, equal(impl->text().data(), textRange, text41));
1291 break;
1292 default:
1293 REPORTER_ASSERT(reporter, false);
1294 break;
1295 }
1296 ++index;
1297 return true;
1298 });
1299 impl->lines()[1].scanStyles(
1300 StyleType::kAllAttributes,
1301 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
1302 switch (index) {
1303 case 4:
1304 REPORTER_ASSERT(reporter, style.equals(text_style4));
1305 REPORTER_ASSERT(reporter, equal(impl->text().data(), textRange, text42));
1306 break;
1307 default:
1308 REPORTER_ASSERT(reporter, false);
1309 break;
1310 }
1311 ++index;
1312 return true;
1313 });
1314 REPORTER_ASSERT(reporter, index == 5);
1315 }
1316
1317 UNIX_ONLY_TEST(SkParagraph_DefaultStyleParagraph, reporter) {
1318 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1319 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1320 TestCanvas canvas("SkParagraph_DefaultStyleParagraph.png");
1321 const char* text = "No TextStyle! Uh Oh!";
1322 const size_t len = strlen(text);
1323
1324 ParagraphStyle paragraph_style;
1325 TextStyle defaultStyle;
1326 defaultStyle.setFontFamilies({SkString("Roboto")});
1327 defaultStyle.setColor(SK_ColorBLACK);
1328 paragraph_style.setTextStyle(defaultStyle);
1329 paragraph_style.turnHintingOff();
1330 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1331 builder.addText(text, len);
1332
1333 auto paragraph = builder.Build();
1334 paragraph->layout(TestCanvasWidth);
1335 paragraph->paint(canvas.get(), 10.0, 15.0);
1336
1337 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
1338
1339 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1340
1341 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
1342 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
1343 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
1344
1345 size_t index = 0;
1346 impl->lines()[0].scanStyles(
1347 StyleType::kAllAttributes,
1348 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
1349 REPORTER_ASSERT(reporter, style.equals(paragraph_style.getTextStyle()));
1350 REPORTER_ASSERT(reporter, equal(impl->text().data(), textRange, text));
1351 ++index;
1352 return true;
1353 });
1354 REPORTER_ASSERT(reporter, index == 1);
1355 }
1356
1357 UNIX_ONLY_TEST(SkParagraph_BoldParagraph, reporter) {
1358 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1359 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1360 TestCanvas canvas("SkParagraph_BoldParagraph.png");
1361 const char* text = "This is Red max bold text!";
1362 const size_t len = strlen(text);
1363
1364 ParagraphStyle paragraph_style;
1365 paragraph_style.turnHintingOff();
1366 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1367
1368 TextStyle text_style;
1369 text_style.setFontFamilies({SkString("Roboto")});
1370 text_style.setColor(SK_ColorRED);
1371 text_style.setFontSize(60);
1372 text_style.setLetterSpacing(0);
1373 text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width,
1374 SkFontStyle::kUpright_Slant));
1375 builder.pushStyle(text_style);
1376 builder.addText(text, len);
1377 builder.pop();
1378
1379 auto paragraph = builder.Build();
1380 paragraph->layout(VeryLongCanvasWidth);
1381 paragraph->paint(canvas.get(), 10.0, 60.0);
1382
1383 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
1384
1385 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1386
1387 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
1388 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
1389 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
1390
1391 size_t index = 0;
1392 impl->lines()[0].scanStyles(
1393 StyleType::kAllAttributes,
1394 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
1395 REPORTER_ASSERT(reporter, style.equals(text_style));
1396 REPORTER_ASSERT(reporter, equal(impl->text().data(), textRange, text));
1397 ++index;
1398 return true;
1399 });
1400 REPORTER_ASSERT(reporter, index == 1);
1401 }
1402
1403 UNIX_ONLY_TEST(SkParagraph_HeightOverrideParagraph, reporter) {
1404 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1405 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1406 TestCanvas canvas("SkParagraph_HeightOverrideParagraph.png");
1407 const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
1408 const size_t len = strlen(text);
1409
1410 ParagraphStyle paragraph_style;
1411 paragraph_style.turnHintingOff();
1412 paragraph_style.setMaxLines(10);
1413 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1414
1415 TextStyle text_style;
1416 text_style.setFontFamilies({SkString("Roboto")});
1417 text_style.setFontSize(20);
1418 text_style.setColor(SK_ColorBLACK);
1419 text_style.setHeight(3.6345f);
1420 text_style.setHeightOverride(true);
1421 builder.pushStyle(text_style);
1422 builder.addText(text, len);
1423 builder.pop();
1424
1425 auto paragraph = builder.Build();
1426 paragraph->layout(550);
1427
1428 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1429 REPORTER_ASSERT(reporter, impl->runs().size() == 5);
1430 REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
1431 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
1432
1433 paragraph->paint(canvas.get(), 0, 0);
1434
1435 SkPaint paint;
1436 paint.setStyle(SkPaint::kStroke_Style);
1437 paint.setAntiAlias(true);
1438 paint.setStrokeWidth(1);
1439
1440 // Tests for GetRectsForRange()
1441 RectHeightStyle rect_height_style = RectHeightStyle::kIncludeLineSpacingMiddle;
1442 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
1443 paint.setColor(SK_ColorRED);
1444 std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
1445 canvas.drawRects(SK_ColorRED, boxes);
1446 REPORTER_ASSERT(reporter, boxes.size() == 0ull);
1447
1448 boxes = paragraph->getRectsForRange(0, 40, rect_height_style, rect_width_style);
1449 canvas.drawRects(SK_ColorBLUE, boxes);
1450 REPORTER_ASSERT(reporter, boxes.size() == 3ull);
1451 if (boxes.size() >= 3) {
1452 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 0, EPSILON100));
1453 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top(), 92.805f, EPSILON5));
1454 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 43.843f, EPSILON100));
1455 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.bottom(), 165.495f, EPSILON5));
1456 }
1457 }
1458
1459 UNIX_ONLY_TEST(SkParagraph_BasicHalfLeading, reporter) {
1460 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1461 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1462
1463 const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
1464 const size_t len = strlen(text);
1465
1466 TestCanvas canvas("SkParagraph_BasicHalfLeading.png");
1467
1468 ParagraphStyle paragraph_style;
1469 TextStyle text_style;
1470 text_style.setFontFamilies({SkString("Roboto")});
1471 text_style.setFontSize(20.0f);
1472 text_style.setColor(SK_ColorBLACK);
1473 text_style.setLetterSpacing(0.0f);
1474 text_style.setWordSpacing(0.0f);
1475 text_style.setHeightOverride(true);
1476 text_style.setHeight(3.6345f);
1477 text_style.setHalfLeading(true);
1478
1479 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1480
1481 builder.pushStyle(text_style);
1482 builder.addText(text);
1483
1484 auto paragraph = builder.Build();
1485 paragraph->layout(550);
1486
1487 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1488 REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
1489 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
1490
1491 paragraph->paint(canvas.get(), 0, 0);
1492
1493 const RectWidthStyle rect_width_style = RectWidthStyle::kTight;
1494 std::vector<TextBox> boxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kTight, rect_width_style);
1495 std::vector<TextBox> lineBoxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kMax, rect_width_style);
1496
1497 canvas.drawRects(SK_ColorBLUE, boxes);
1498 REPORTER_ASSERT(reporter, boxes.size() == 3ull);
1499 REPORTER_ASSERT(reporter, lineBoxes.size() == boxes.size());
1500 if (boxes.size() >= 3) {
1501 const auto line_spacing1 = boxes[1].rect.top() - boxes[0].rect.bottom();
1502 const auto line_spacing2 = boxes[2].rect.top() - boxes[1].rect.bottom();
1503
1504 // Uniform line spacing.
1505 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(line_spacing1, line_spacing2));
1506
1507 // line spacing is distributed evenly over and under the text.
1508 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[0].rect.bottom() - boxes[0].rect.bottom(), boxes[0].rect.top() - lineBoxes[0].rect.top()));
1509 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[1].rect.bottom() - boxes[1].rect.bottom(), boxes[1].rect.top() - lineBoxes[1].rect.top()));
1510 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[2].rect.bottom() - boxes[2].rect.bottom(), boxes[2].rect.top() - lineBoxes[2].rect.top()));
1511
1512 // Half leading does not move the text horizontally.
1513 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 0, EPSILON100));
1514 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 43.843f, EPSILON100));
1515 }
1516 }
1517
1518 UNIX_ONLY_TEST(SkParagraph_NearZeroHeightMixedDistribution, reporter) {
1519 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1520 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1521
1522 const char* text = "Cookies need love";
1523 const size_t len = strlen(text);
1524
1525 TestCanvas canvas("SkParagraph_ZeroHeightHalfLeading.png");
1526
1527 ParagraphStyle paragraph_style;
1528 paragraph_style.setTextHeightBehavior(TextHeightBehavior::kAll);
1529 TextStyle text_style;
1530 text_style.setFontFamilies({SkString("Roboto")});
1531 text_style.setFontSize(20.0f);
1532 text_style.setColor(SK_ColorBLACK);
1533 text_style.setLetterSpacing(0.0f);
1534 text_style.setWordSpacing(0.0f);
1535 text_style.setHeightOverride(true);
1536 text_style.setHeight(0.001f);
1537
1538 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1539
1540 // First run, half leading.
1541 text_style.setHalfLeading(true);
1542 builder.pushStyle(text_style);
1543 builder.addText(text);
1544
1545 // Second run, no half leading.
1546 text_style.setHalfLeading(false);
1547 builder.pushStyle(text_style);
1548 builder.addText(text);
1549
1550 auto paragraph = builder.Build();
1551 paragraph->layout(550);
1552 paragraph->paint(canvas.get(), 0, 0);
1553
1554 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1555 REPORTER_ASSERT(reporter, impl->runs().size() == 2);
1556 REPORTER_ASSERT(reporter, impl->styles().size() == 2); // paragraph style does not count
1557 REPORTER_ASSERT(reporter, impl->lines().size() == 1ull);
1558
1559 const RectWidthStyle rect_width_style = RectWidthStyle::kTight;
1560
1561 std::vector<TextBox> boxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kTight, rect_width_style);
1562 std::vector<TextBox> lineBoxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kMax, rect_width_style);
1563
1564 canvas.drawRects(SK_ColorBLUE, boxes);
1565 REPORTER_ASSERT(reporter, boxes.size() == 1ull);
1566 REPORTER_ASSERT(reporter, lineBoxes.size() == boxes.size());
1567
1568 // From font metrics.
1569 const auto metricsAscent = -18.5546875f;
1570 const auto metricsDescent = 4.8828125f;
1571
1572 // As the height multiplier converges to 0 (but not 0 since 0 is used as a
1573 // magic value to indicate there's no height multiplier), the `Run`s top
1574 // edge and bottom edge will converge to a horizontal line:
1575 // - When half leading is used the vertical line is roughly the center of
1576 // of the glyphs in the run ((fontMetrics.descent - fontMetrics.ascent) / 2)
1577 // - When half leading is disabled the line is the alphabetic baseline.
1578
1579 // Expected values in baseline coordinate space:
1580 const auto run1_ascent = (metricsAscent + metricsDescent) / 2;
1581 const auto run1_descent = (metricsAscent + metricsDescent) / 2;
1582 const auto run2_ascent = 0.0f;
1583 const auto run2_descent = 0.0f;
1584 const auto line_top = std::min(run1_ascent, run2_ascent);
1585 const auto line_bottom = std::max(run1_descent, run2_descent);
1586
1587 // Expected glyph height in linebox coordinate space:
1588 const auto glyphs_top = metricsAscent - line_top;
1589 const auto glyphs_bottom = metricsDescent - line_top;
1590
1591 // kTight reports the glyphs' bounding box in the linebox's coordinate
1592 // space.
1593 const auto actual_glyphs_top = boxes[0].rect.top() - lineBoxes[0].rect.top();
1594 const auto actual_glyphs_bottom = boxes[0].rect.bottom() - lineBoxes[0].rect.top();
1595
1596 // Use a relatively large epsilon since the heightMultiplier is not actually
1597 // 0.
1598 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(glyphs_top, actual_glyphs_top, EPSILON20));
1599 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(glyphs_bottom, actual_glyphs_bottom, EPSILON20));
1600
1601 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[0].rect.height(), line_bottom - line_top, EPSILON2));
1602 REPORTER_ASSERT(reporter, lineBoxes[0].rect.height() > 1);
1603
1604 // Half leading does not move the text horizontally.
1605 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
1606 }
1607
1608 UNIX_ONLY_TEST(SkParagraph_StrutHalfLeadingSimple, reporter) {
1609 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1610 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1611
1612 const char* text = "A";
1613 const size_t len = strlen(text);
1614
1615 TestCanvas canvas("SkParagraph_StrutHalfLeading.png");
1616
1617 ParagraphStyle paragraph_style;
1618 TextStyle text_style;
1619 text_style.setFontFamilies({SkString("Roboto")});
1620 text_style.setFontSize(100.0f);
1621 text_style.setColor(SK_ColorBLACK);
1622 text_style.setLetterSpacing(0.0f);
1623 text_style.setWordSpacing(0.0f);
1624 text_style.setHeightOverride(true);
1625 text_style.setHeight(2.0f);
1626 text_style.setHalfLeading(true);
1627
1628 StrutStyle strut_style;
1629 strut_style.setFontFamilies({SkString("Roboto")});
1630 strut_style.setFontSize(100.0f);
1631 strut_style.setHeightOverride(true);
1632 strut_style.setHeight(2.0f);
1633 strut_style.setHalfLeading(true);
1634 strut_style.setStrutEnabled(true);
1635 strut_style.setForceStrutHeight(true);
1636
1637 paragraph_style.setStrutStyle(strut_style);
1638 paragraph_style.setTextStyle(text_style);
1639 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1640
1641 builder.pushStyle(text_style);
1642 builder.addText(text);
1643
1644 auto paragraph = builder.Build();
1645 paragraph->layout(550);
1646
1647 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1648 REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
1649
1650 paragraph->paint(canvas.get(), 0, 0);
1651
1652 const RectWidthStyle rect_width_style = RectWidthStyle::kTight;
1653 std::vector<TextBox> boxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kTight, rect_width_style);
1654 std::vector<TextBox> lineBoxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kMax, rect_width_style);
1655
1656 canvas.drawRects(SK_ColorBLUE, boxes);
1657 REPORTER_ASSERT(reporter, lineBoxes.size() == boxes.size());
1658 if (lineBoxes.size() >= 1 && boxes.size() >= 1) {
1659 // line spacing is distributed evenly over and under the text.
1660 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[0].rect.bottom() - boxes[0].rect.bottom(), boxes[0].rect.top() - lineBoxes[0].rect.top()));
1661 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[0].rect.top(), 0.0f));
1662 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[0].rect.left(), 0.0f));
1663
1664 std::vector<LineMetrics> lineMetrics;
1665 paragraph->getLineMetrics(lineMetrics);
1666 LineMetrics& firstLine = lineMetrics[0];
1667 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(firstLine.fHeight, 200.0f));
1668
1669 // Half leading does not move the text horizontally.
1670 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
1671 }
1672 }
1673
1674 UNIX_ONLY_TEST(SkParagraph_StrutHalfLeadingMultiline, reporter) {
1675 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1676 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1677
1678 const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
1679 const size_t len = strlen(text);
1680
1681 TestCanvas canvas("SkParagraph_StrutHalfLeading.png");
1682
1683 ParagraphStyle paragraph_style;
1684 TextStyle text_style;
1685 text_style.setFontFamilies({SkString("Roboto")});
1686 text_style.setFontSize(20.0f);
1687 text_style.setColor(SK_ColorBLACK);
1688 text_style.setLetterSpacing(0.0f);
1689 text_style.setWordSpacing(0.0f);
1690 text_style.setHeightOverride(true);
1691 text_style.setHeight(3.0f);
1692 text_style.setHalfLeading(true);
1693
1694 StrutStyle strut_style;
1695 strut_style.setFontFamilies({SkString("Roboto")});
1696 strut_style.setFontSize(20.0f);
1697 strut_style.setHeightOverride(true);
1698 strut_style.setHeight(3.0f);
1699 strut_style.setHalfLeading(true);
1700 strut_style.setStrutEnabled(true);
1701 strut_style.setForceStrutHeight(true);
1702
1703 paragraph_style.setStrutStyle(strut_style);
1704 paragraph_style.setTextStyle(text_style);
1705 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1706
1707 builder.pushStyle(text_style);
1708 builder.addText(text);
1709
1710 auto paragraph = builder.Build();
1711 paragraph->layout(550);
1712
1713 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1714 REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
1715
1716 paragraph->paint(canvas.get(), 0, 0);
1717
1718 const RectWidthStyle rect_width_style = RectWidthStyle::kTight;
1719 std::vector<TextBox> boxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kTight, rect_width_style);
1720 std::vector<TextBox> lineBoxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kMax, rect_width_style);
1721
1722 canvas.drawRects(SK_ColorBLUE, boxes);
1723 REPORTER_ASSERT(reporter, boxes.size() == 3ull);
1724 REPORTER_ASSERT(reporter, lineBoxes.size() == boxes.size());
1725 if (boxes.size() >= 3) {
1726 const auto line_spacing1 = boxes[1].rect.top() - boxes[0].rect.bottom();
1727 const auto line_spacing2 = boxes[2].rect.top() - boxes[1].rect.bottom();
1728
1729 // Uniform line spacing.
1730 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(line_spacing1, line_spacing2));
1731
1732 // line spacing is distributed evenly over and under the text.
1733 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[0].rect.bottom() - boxes[0].rect.bottom(), boxes[0].rect.top() - lineBoxes[0].rect.top()));
1734 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[1].rect.bottom() - boxes[1].rect.bottom(), boxes[1].rect.top() - lineBoxes[1].rect.top()));
1735 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[2].rect.bottom() - boxes[2].rect.bottom(), boxes[2].rect.top() - lineBoxes[2].rect.top()));
1736
1737 // Half leading does not move the text horizontally.
1738 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 0, EPSILON100));
1739 }
1740 }
1741
1742 UNIX_ONLY_TEST(SkParagraph_TrimLeadingDistribution, reporter) {
1743 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1744 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1745
1746 const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
1747 const size_t len = strlen(text);
1748
1749 TestCanvas canvas("SkParagraph_TrimHalfLeading.png");
1750
1751 ParagraphStyle paragraph_style;
1752 paragraph_style.setTextHeightBehavior(TextHeightBehavior::kDisableAll);
1753 TextStyle text_style;
1754 text_style.setFontFamilies({SkString("Roboto")});
1755 text_style.setFontSize(20.0f);
1756 text_style.setColor(SK_ColorBLACK);
1757 text_style.setLetterSpacing(0.0f);
1758 text_style.setWordSpacing(0.0f);
1759 text_style.setHeightOverride(true);
1760 text_style.setHeight(3.6345f);
1761 text_style.setHalfLeading(true);
1762
1763 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1764
1765 builder.pushStyle(text_style);
1766 builder.addText(text);
1767
1768 auto paragraph = builder.Build();
1769 paragraph->layout(550);
1770 paragraph->paint(canvas.get(), 0, 0);
1771
1772 const RectWidthStyle rect_width_style = RectWidthStyle::kTight;
1773
1774 std::vector<TextBox> boxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kTight, rect_width_style);
1775 std::vector<TextBox> lineBoxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kMax, rect_width_style);
1776
1777 canvas.drawRects(SK_ColorBLUE, boxes);
1778 REPORTER_ASSERT(reporter, boxes.size() == 3ull);
1779 REPORTER_ASSERT(reporter, lineBoxes.size() == boxes.size());
1780 if (boxes.size() >= 3) {
1781 const auto line_spacing1 = boxes[1].rect.top() - boxes[0].rect.bottom();
1782 const auto line_spacing2 = boxes[2].rect.top() - boxes[1].rect.bottom();
1783
1784 // Uniform line spacing. The delta is introduced by the height rounding.
1785 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(line_spacing1, line_spacing2, 1));
1786
1787 // Trim the first line's top leading.
1788 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[0].rect.top(), boxes[0].rect.top()));
1789 // Trim the last line's bottom leading.
1790 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[2].rect.bottom(), boxes[2].rect.bottom()));
1791
1792 const auto halfLeading = lineBoxes[0].rect.bottom() - boxes[0].rect.bottom();
1793 // Large epsilon because of rounding.
1794 const auto epsilon = EPSILON10;
1795 // line spacing is distributed evenly over and under the text.
1796 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.top() - lineBoxes[1].rect.top(), halfLeading, epsilon));
1797 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lineBoxes[1].rect.bottom() - boxes[1].rect.bottom(), halfLeading));
1798 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.top() - lineBoxes[2].rect.top(), halfLeading, epsilon));
1799
1800 // Half leading does not move the text horizontally.
1801 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.left(), 0, EPSILON100));
1802 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.right(), 43.843f, EPSILON100));
1803 }
1804 }
1805
1806 UNIX_ONLY_TEST(SkParagraph_LeftAlignParagraph, reporter) {
1807 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1808 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1809 TestCanvas canvas("SkParagraph_LeftAlignParagraph.png");
1810 const char* text =
1811 "This is a very long sentence to test if the text will properly wrap "
1812 "around and go to the next line. Sometimes, short sentence. Longer "
1813 "sentences are okay too because they are nessecary. Very short. "
1814 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
1815 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
1816 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
1817 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
1818 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
1819 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
1820 "mollit anim id est laborum. "
1821 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
1822 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
1823 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
1824 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
1825 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
1826 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
1827 "mollit anim id est laborum.";
1828 const size_t len = strlen(text);
1829
1830 ParagraphStyle paragraph_style;
1831 paragraph_style.setMaxLines(14);
1832 paragraph_style.setTextAlign(TextAlign::kLeft);
1833 paragraph_style.turnHintingOff();
1834 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1835
1836 TextStyle text_style;
1837 text_style.setFontFamilies({SkString("Roboto")});
1838 text_style.setFontSize(26);
1839 text_style.setLetterSpacing(1);
1840 text_style.setWordSpacing(5);
1841 text_style.setColor(SK_ColorBLACK);
1842 text_style.setHeight(1);
1843 text_style.setDecoration(TextDecoration::kUnderline);
1844 text_style.setDecorationColor(SK_ColorBLACK);
1845 builder.pushStyle(text_style);
1846 builder.addText(text, len);
1847 builder.pop();
1848
1849 auto paragraph = builder.Build();
1850 paragraph->layout(TestCanvasWidth - 100);
1851 paragraph->paint(canvas.get(), 0, 0);
1852
1853 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1854
1855 REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
1856 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
1857 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
1858 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
1859 REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
1860
1861 double expected_y = 0;
1862 double epsilon = 0.01f;
1863 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
1864 REPORTER_ASSERT(reporter,
1865 SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
1866 expected_y += 30;
1867 REPORTER_ASSERT(reporter,
1868 SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
1869 expected_y += 30;
1870 REPORTER_ASSERT(reporter,
1871 SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
1872 expected_y += 30;
1873 REPORTER_ASSERT(reporter,
1874 SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
1875 expected_y += 30 * 10;
1876 REPORTER_ASSERT(reporter,
1877 SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
1878
1879 REPORTER_ASSERT(reporter,
1880 paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
1881
1882 // Tests for GetGlyphPositionAtCoordinate()
1883 REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(0, 0).position == 0);
1884 REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 1).position == 0);
1885 REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 35).position == 68);
1886 REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(1, 70).position == 134);
1887 REPORTER_ASSERT(reporter, impl->getGlyphPositionAtCoordinate(2000, 35).position == 134);
1888 }
1889
1890 UNIX_ONLY_TEST(SkParagraph_RightAlignParagraph, reporter) {
1891 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1892 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1893 TestCanvas canvas("SkParagraph_RightAlignParagraph.png");
1894 const char* text =
1895 "This is a very long sentence to test if the text will properly wrap "
1896 "around and go to the next line. Sometimes, short sentence. Longer "
1897 "sentences are okay too because they are nessecary. Very short. "
1898 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
1899 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
1900 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
1901 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
1902 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
1903 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
1904 "mollit anim id est laborum. "
1905 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
1906 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
1907 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
1908 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
1909 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
1910 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
1911 "mollit anim id est laborum.";
1912 const size_t len = strlen(text);
1913
1914 ParagraphStyle paragraph_style;
1915 paragraph_style.setMaxLines(14);
1916 paragraph_style.setTextAlign(TextAlign::kRight);
1917 paragraph_style.turnHintingOff();
1918 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
1919
1920 TextStyle text_style;
1921 text_style.setFontFamilies({SkString("Roboto")});
1922 text_style.setFontSize(26);
1923 text_style.setLetterSpacing(1);
1924 text_style.setWordSpacing(5);
1925 text_style.setColor(SK_ColorBLACK);
1926 text_style.setHeight(1);
1927 text_style.setDecoration(TextDecoration::kUnderline);
1928 text_style.setDecorationColor(SK_ColorBLACK);
1929 builder.pushStyle(text_style);
1930 builder.addText(text, len);
1931 builder.pop();
1932
1933 auto paragraph = builder.Build();
1934 paragraph->layout(TestCanvasWidth - 100);
1935
1936 paragraph->paint(canvas.get(), 0, 0);
1937
1938 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
1939
1940 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
1941 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
1942 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
1943 REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
1944
1945 double expected_y = 0;
1946 double epsilon = 0.01f;
1947 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
1948 REPORTER_ASSERT(reporter,
1949 SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
1950 expected_y += 30;
1951 REPORTER_ASSERT(reporter,
1952 SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
1953 expected_y += 30;
1954 REPORTER_ASSERT(reporter,
1955 SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
1956 expected_y += 30;
1957 REPORTER_ASSERT(reporter,
1958 SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
1959 expected_y += 30 * 10;
1960 REPORTER_ASSERT(reporter,
1961 SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
1962
1963 auto calculate = [](const TextLine& line) -> SkScalar {
1964 return TestCanvasWidth - 100 - line.offset().fX - line.width();
1965 };
1966
1967 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
1968 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
1969 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
1970 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
1971 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon));
1972
1973 REPORTER_ASSERT(reporter,
1974 paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
1975 }
1976
1977 UNIX_ONLY_TEST(SkParagraph_CenterAlignParagraph, reporter) {
1978 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
1979 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
1980 TestCanvas canvas("SkParagraph_CenterAlignParagraph.png");
1981 const char* text =
1982 "This is a very long sentence to test if the text will properly wrap "
1983 "around and go to the next line. Sometimes, short sentence. Longer "
1984 "sentences are okay too because they are nessecary. Very short. "
1985 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
1986 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
1987 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
1988 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
1989 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
1990 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
1991 "mollit anim id est laborum. "
1992 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
1993 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
1994 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
1995 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
1996 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
1997 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
1998 "mollit anim id est laborum.";
1999 const size_t len = strlen(text);
2000
2001 ParagraphStyle paragraph_style;
2002 paragraph_style.setMaxLines(14);
2003 paragraph_style.setTextAlign(TextAlign::kCenter);
2004 paragraph_style.turnHintingOff();
2005 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2006
2007 TextStyle text_style;
2008 text_style.setFontFamilies({SkString("Roboto")});
2009 text_style.setFontSize(26);
2010 text_style.setLetterSpacing(1);
2011 text_style.setWordSpacing(5);
2012 text_style.setColor(SK_ColorBLACK);
2013 text_style.setHeight(1);
2014 text_style.setDecoration(TextDecoration::kUnderline);
2015 text_style.setDecorationColor(SK_ColorBLACK);
2016 builder.pushStyle(text_style);
2017 builder.addText(text, len);
2018 builder.pop();
2019
2020 auto paragraph = builder.Build();
2021 paragraph->layout(TestCanvasWidth - 100);
2022 paragraph->paint(canvas.get(), 0, 0);
2023
2024 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2025
2026 REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
2027 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
2028 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
2029 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
2030 REPORTER_ASSERT(reporter, impl->lines().size() == paragraph_style.getMaxLines());
2031
2032 double expected_y = 0;
2033 double epsilon = 0.01f;
2034 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, epsilon));
2035 REPORTER_ASSERT(reporter,
2036 SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, epsilon));
2037 expected_y += 30;
2038 REPORTER_ASSERT(reporter,
2039 SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, epsilon));
2040 expected_y += 30;
2041 REPORTER_ASSERT(reporter,
2042 SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, epsilon));
2043 expected_y += 30;
2044 REPORTER_ASSERT(reporter,
2045 SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, epsilon));
2046 expected_y += 30 * 10;
2047 REPORTER_ASSERT(reporter,
2048 SkScalarNearlyEqual(impl->lines()[13].offset().fY, expected_y, epsilon));
2049
2050 auto calculate = [](const TextLine& line) -> SkScalar {
2051 return TestCanvasWidth - 100 - (line.offset().fX * 2 + line.width());
2052 };
2053
2054 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, epsilon));
2055 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, epsilon));
2056 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, epsilon));
2057 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, epsilon));
2058 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[13]), 0, epsilon));
2059
2060 REPORTER_ASSERT(reporter,
2061 paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
2062 }
2063
2064 UNIX_ONLY_TEST(SkParagraph_JustifyAlignParagraph, reporter) {
2065 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2066 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2067 TestCanvas canvas("SkParagraph_JustifyAlignParagraph.png");
2068 const char* text =
2069 "This is a very long sentence to test if the text will properly wrap "
2070 "around and go to the next line. Sometimes, short sentence. Longer "
2071 "sentences are okay too because they are nessecary. Very short. "
2072 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
2073 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
2074 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
2075 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
2076 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
2077 "occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
2078 "mollit anim id est laborum. "
2079 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod "
2080 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim "
2081 "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea "
2082 "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
2083 "velit esse cillum dolore eu fugiat.";
2084 const size_t len = strlen(text);
2085
2086 ParagraphStyle paragraph_style;
2087 paragraph_style.setMaxLines(14);
2088 paragraph_style.setTextAlign(TextAlign::kJustify);
2089 paragraph_style.turnHintingOff();
2090 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2091
2092 TextStyle text_style;
2093 text_style.setFontFamilies({SkString("Roboto")});
2094 text_style.setFontSize(26);
2095 text_style.setLetterSpacing(0);
2096 text_style.setWordSpacing(5);
2097 text_style.setColor(SK_ColorBLACK);
2098 text_style.setHeight(1);
2099 text_style.setDecoration(TextDecoration::kUnderline);
2100 text_style.setDecorationColor(SK_ColorBLACK);
2101 builder.pushStyle(text_style);
2102 builder.addText(text, len);
2103 builder.pop();
2104
2105 auto paragraph = builder.Build();
2106 paragraph->layout(TestCanvasWidth - 100);
2107 paragraph->paint(canvas.get(), 0, 0);
2108
2109 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
2110 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
2111 auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style);
2112 canvas.drawRects(SK_ColorRED, boxes);
2113
2114 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2115
2116 REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
2117 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
2118 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
2119 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
2120
2121 double expected_y = 0;
2122 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].baseline(), 24.121f, EPSILON100));
2123 REPORTER_ASSERT(reporter,
2124 SkScalarNearlyEqual(impl->lines()[0].offset().fY, expected_y, EPSILON100));
2125 expected_y += 30;
2126 REPORTER_ASSERT(reporter,
2127 SkScalarNearlyEqual(impl->lines()[1].offset().fY, expected_y, EPSILON100));
2128 expected_y += 30;
2129 REPORTER_ASSERT(reporter,
2130 SkScalarNearlyEqual(impl->lines()[2].offset().fY, expected_y, EPSILON100));
2131 expected_y += 30;
2132 REPORTER_ASSERT(reporter,
2133 SkScalarNearlyEqual(impl->lines()[3].offset().fY, expected_y, EPSILON100));
2134 expected_y += 30 * 9;
2135 REPORTER_ASSERT(reporter,
2136 SkScalarNearlyEqual(impl->lines()[12].offset().fY, expected_y, EPSILON100));
2137
2138 auto calculate = [](const TextLine& line) -> SkScalar {
2139 return line.offset().fX;
2140 };
2141
2142 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[0]), 0, EPSILON100));
2143 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[1]), 0, EPSILON100));
2144 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[2]), 0, EPSILON100));
2145 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(impl->lines()[3]), 0, EPSILON100));
2146
2147 REPORTER_ASSERT(reporter,
2148 paragraph_style.getTextAlign() == impl->paragraphStyle().getTextAlign());
2149 }
2150
2151 // Checked: DIFF (ghost spaces as a separate box in TxtLib)
2152 UNIX_ONLY_TEST(SkParagraph_JustifyRTL, reporter) {
2153 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
2154 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2155 TestCanvas canvas("SkParagraph_JustifyRTL.png");
2156 const char* text =
2157 "אאא בּבּבּבּ אאאא בּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ "
2158 "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ "
2159 "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ "
2160 "אאאאאבּבּבּבּבּבּאאאאאבּבּבּבּבּבּאאאאאבּבּבּבּבּבּ "
2161 "אאאאא בּבּבּבּבּבּ";
2162 const size_t len = strlen(text);
2163
2164 ParagraphStyle paragraph_style;
2165 paragraph_style.setMaxLines(14);
2166 paragraph_style.setTextAlign(TextAlign::kJustify);
2167 paragraph_style.setTextDirection(TextDirection::kRtl);
2168 paragraph_style.turnHintingOff();
2169 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2170
2171 TextStyle text_style;
2172 text_style.setFontFamilies({SkString("Ahem")});
2173 text_style.setFontSize(26);
2174 text_style.setColor(SK_ColorBLACK);
2175 builder.pushStyle(text_style);
2176 builder.addText(text, len);
2177 builder.pop();
2178
2179 auto paragraph = builder.Build();
2180 paragraph->layout(TestCanvasWidth - 100);
2181 paragraph->paint(canvas.get(), 0, 0);
2182
2183 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2184
2185 auto calculate = [](const TextLine& line) -> SkScalar {
2186 return TestCanvasWidth - 100 - line.width();
2187 };
2188 for (auto& line : impl->lines()) {
2189 if (&line == &impl->lines().back() || &line == &impl->lines()[impl->lines().size() - 2]) {
2190 // Second-last line will be also right-aligned because it is only one cluster
2191 REPORTER_ASSERT(reporter, calculate(line) > EPSILON100);
2192 REPORTER_ASSERT(reporter, line.offset().fX > EPSILON100);
2193 } else {
2194 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(calculate(line), 0, EPSILON100));
2195 }
2196 }
2197
2198 // Just make sure the the text is actually RTL
2199 for (auto& run : impl->runs()) {
2200 REPORTER_ASSERT(reporter, !run.leftToRight());
2201 }
2202
2203 // Tests for GetRectsForRange()
2204 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
2205 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
2206 auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style);
2207 canvas.drawRects(SK_ColorRED, boxes);
2208 REPORTER_ASSERT(reporter, boxes.size() == 3);
2209
2210 boxes = paragraph->getRectsForRange(226, 278, rect_height_style, rect_width_style);
2211 canvas.drawRects(SK_ColorYELLOW, boxes);
2212 REPORTER_ASSERT(reporter, boxes.size() == 1);
2213
2214 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 16, EPSILON100));
2215 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 130, EPSILON100));
2216 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100));
2217 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 156, EPSILON100));
2218
2219 boxes = paragraph->getRectsForRange(292, 296, rect_height_style, rect_width_style);
2220 canvas.drawRects(SK_ColorBLUE, boxes);
2221 REPORTER_ASSERT(reporter, boxes.size() == 1);
2222
2223 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 588, EPSILON100));
2224 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 156, EPSILON100));
2225 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 640, EPSILON100));
2226 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 182, EPSILON100));
2227 }
2228
2229 UNIX_ONLY_TEST(SkParagraph_JustifyRTLNewLine, reporter) {
2230 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
2231 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2232 TestCanvas canvas("SkParagraph_JustifyRTLNewLine.png");
2233 const char* text =
2234 "אאא בּבּבּבּ אאאא\nבּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ "
2235 "אאאאא בּבּבּבּבּ אאאבּבּבּבּבּבּאאאאא בּבּבּבּבּבּאאאאאבּבּבּבּבּבּ אאאאא בּבּבּבּבּ "
2236 "אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ אאאאא בּבּבּבּבּבּ";
2237 const size_t len = strlen(text);
2238
2239 ParagraphStyle paragraph_style;
2240 paragraph_style.setMaxLines(14);
2241 paragraph_style.setTextAlign(TextAlign::kJustify);
2242 paragraph_style.setTextDirection(TextDirection::kRtl);
2243 paragraph_style.turnHintingOff();
2244 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2245
2246 TextStyle text_style;
2247 text_style.setFontFamilies({SkString("Ahem")});
2248 text_style.setFontSize(26);
2249 text_style.setColor(SK_ColorBLACK);
2250 builder.pushStyle(text_style);
2251 builder.addText(text, len);
2252 builder.pop();
2253
2254 auto paragraph = builder.Build();
2255 paragraph->layout(TestCanvasWidth - 100);
2256 paragraph->paint(canvas.get(), 0, 0);
2257
2258 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2259
2260 SkPaint paint;
2261 paint.setStyle(SkPaint::kStroke_Style);
2262 paint.setAntiAlias(true);
2263 paint.setStrokeWidth(1);
2264
2265 // Tests for GetRectsForRange()
2266 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
2267 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
2268 paint.setColor(SK_ColorRED);
2269 auto boxes = paragraph->getRectsForRange(0, 30, rect_height_style, rect_width_style);
2270 for (size_t i = 0; i < boxes.size(); ++i) {
2271 canvas.get()->drawRect(boxes[i].rect, paint);
2272 }
2273 REPORTER_ASSERT(reporter, boxes.size() == 2ull);
2274 if (boxes.size() >= 2) {
2275 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 562, EPSILON100));
2276 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
2277 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100));
2278 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 26, EPSILON100));
2279 }
2280
2281 paint.setColor(SK_ColorBLUE);
2282 boxes = paragraph->getRectsForRange(240, 250, rect_height_style, rect_width_style);
2283 for (size_t i = 0; i < boxes.size(); ++i) {
2284 canvas.get()->drawRect(boxes[i].rect, paint);
2285 }
2286 REPORTER_ASSERT(reporter, boxes.size() == 1ull);
2287 if (boxes.size() >= 1) {
2288 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 68, EPSILON100));
2289 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 130, EPSILON100));
2290 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 120, EPSILON100));
2291 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 156, EPSILON100));
2292 }
2293
2294 // All lines should be justified to the width of the paragraph
2295 // except for #0 (new line) and #5 (the last one)
2296 for (auto& line : impl->lines()) {
2297 ptrdiff_t num = &line - impl->lines().data();
2298 if (num == 0 || num == 5) {
2299 REPORTER_ASSERT(reporter, line.width() < TestCanvasWidth - 100);
2300 } else {
2301 REPORTER_ASSERT(reporter,
2302 SkScalarNearlyEqual(line.width(), TestCanvasWidth - 100, EPSILON100),
2303 "#%zd: %f <= %d\n", num, line.width(), TestCanvasWidth - 100);
2304 }
2305 }
2306 }
2307
2308 UNIX_ONLY_TEST(SkParagraph_LeadingSpaceRTL, reporter) {
2309 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
2310 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2311 TestCanvas canvas("SkParagraph_LeadingSpaceRTL.png");
2312
2313 const char* text = " leading space";
2314 const size_t len = strlen(text);
2315
2316 ParagraphStyle paragraph_style;
2317 paragraph_style.setMaxLines(14);
2318 paragraph_style.setTextAlign(TextAlign::kJustify);
2319 paragraph_style.setTextDirection(TextDirection::kRtl);
2320 paragraph_style.turnHintingOff();
2321 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2322
2323 TextStyle text_style;
2324 text_style.setFontFamilies({SkString("Ahem")});
2325 text_style.setFontSize(26);
2326 text_style.setColor(SK_ColorBLACK);
2327 builder.pushStyle(text_style);
2328 builder.addText(text, len);
2329 builder.pop();
2330
2331 auto paragraph = builder.Build();
2332 paragraph->layout(TestCanvasWidth - 100);
2333 paragraph->paint(canvas.get(), 0, 0);
2334
2335 SkPaint paint;
2336 paint.setStyle(SkPaint::kStroke_Style);
2337 paint.setAntiAlias(true);
2338 paint.setStrokeWidth(1);
2339
2340 // Tests for GetRectsForRange()
2341 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
2342 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
2343 paint.setColor(SK_ColorRED);
2344 auto boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style);
2345 for (size_t i = 0; i < boxes.size(); ++i) {
2346 canvas.get()->drawRect(boxes[i].rect, paint);
2347 }
2348 REPORTER_ASSERT(reporter, boxes.size() == 2ull);
2349 }
2350
2351 UNIX_ONLY_TEST(SkParagraph_DecorationsParagraph, reporter) {
2352 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2353 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2354 TestCanvas canvas("SkParagraph_DecorationsParagraph.png");
2355 const char* text1 = "This text should be";
2356 const char* text2 = " decorated even when";
2357 const char* text3 = " wrapped around to";
2358 const char* text4 = " the next line.";
2359 const char* text5 = " Otherwise, bad things happen.";
2360
2361 ParagraphStyle paragraph_style;
2362 paragraph_style.setMaxLines(14);
2363 paragraph_style.setTextAlign(TextAlign::kLeft);
2364 paragraph_style.turnHintingOff();
2365 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2366
2367 TextStyle text_style;
2368 text_style.setFontFamilies({SkString("Roboto")});
2369 text_style.setFontSize(26);
2370 text_style.setLetterSpacing(0);
2371 text_style.setWordSpacing(5);
2372 text_style.setColor(SK_ColorBLACK);
2373 text_style.setHeight(2);
2374 text_style.setDecoration(TextDecoration::kUnderline);
2375 text_style.setDecorationColor(SK_ColorBLACK);
2376 text_style.setDecoration((TextDecoration)(
2377 TextDecoration::kUnderline | TextDecoration::kOverline | TextDecoration::kLineThrough));
2378 text_style.setDecorationStyle(TextDecorationStyle::kSolid);
2379 text_style.setDecorationColor(SK_ColorBLACK);
2380 text_style.setDecorationThicknessMultiplier(2.0);
2381 builder.pushStyle(text_style);
2382 builder.addText(text1, strlen(text1));
2383
2384 text_style.setDecorationStyle(TextDecorationStyle::kDouble);
2385 text_style.setDecorationColor(SK_ColorBLUE);
2386 text_style.setDecorationThicknessMultiplier(1.0);
2387 builder.pushStyle(text_style);
2388 builder.addText(text2, strlen(text2));
2389
2390 text_style.setDecorationStyle(TextDecorationStyle::kDotted);
2391 text_style.setDecorationColor(SK_ColorBLACK);
2392 builder.pushStyle(text_style);
2393 builder.addText(text3, strlen(text3));
2394
2395 text_style.setDecorationStyle(TextDecorationStyle::kDashed);
2396 text_style.setDecorationColor(SK_ColorBLACK);
2397 text_style.setDecorationThicknessMultiplier(3.0);
2398 builder.pushStyle(text_style);
2399 builder.addText(text4, strlen(text4));
2400
2401 text_style.setDecorationStyle(TextDecorationStyle::kWavy);
2402 text_style.setDecorationColor(SK_ColorRED);
2403 text_style.setDecorationThicknessMultiplier(1.0);
2404 builder.pushStyle(text_style);
2405 builder.addText(text5, strlen(text5));
2406 builder.pop();
2407
2408 auto paragraph = builder.Build();
2409 paragraph->layout(TestCanvasWidth - 100);
2410 paragraph->paint(canvas.get(), 0, 0);
2411
2412 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2413
2414 size_t index = 0;
2415 for (auto& line : impl->lines()) {
2416 line.scanStyles(
2417 StyleType::kDecorations,
2418 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
2419 auto decoration = (TextDecoration)(TextDecoration::kUnderline |
2420 TextDecoration::kOverline |
2421 TextDecoration::kLineThrough);
2422 REPORTER_ASSERT(reporter, style.getDecorationType() == decoration);
2423 switch (index) {
2424 case 0:
2425 REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
2426 TextDecorationStyle::kSolid);
2427 REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
2428 REPORTER_ASSERT(reporter,
2429 style.getDecorationThicknessMultiplier() == 2.0);
2430 break;
2431 case 1: // The style appears on 2 lines so it has 2 pieces
2432 REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
2433 TextDecorationStyle::kDouble);
2434 REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLUE);
2435 REPORTER_ASSERT(reporter,
2436 style.getDecorationThicknessMultiplier() == 1.0);
2437 break;
2438 case 2:
2439 REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
2440 TextDecorationStyle::kDotted);
2441 REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
2442 REPORTER_ASSERT(reporter,
2443 style.getDecorationThicknessMultiplier() == 1.0);
2444 break;
2445 case 3:
2446 case 4:
2447 REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
2448 TextDecorationStyle::kDashed);
2449 REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorBLACK);
2450 REPORTER_ASSERT(reporter,
2451 style.getDecorationThicknessMultiplier() == 3.0);
2452 break;
2453 case 5:
2454 REPORTER_ASSERT(reporter, style.getDecorationStyle() ==
2455 TextDecorationStyle::kWavy);
2456 REPORTER_ASSERT(reporter, style.getDecorationColor() == SK_ColorRED);
2457 REPORTER_ASSERT(reporter,
2458 style.getDecorationThicknessMultiplier() == 1.0);
2459 break;
2460 default:
2461 REPORTER_ASSERT(reporter, false);
2462 break;
2463 }
2464 ++index;
2465 return true;
2466 });
2467 }
2468 }
2469
2470 // TODO: Add test for wavy decorations.
2471
2472 UNIX_ONLY_TEST(SkParagraph_ItalicsParagraph, reporter) {
2473 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2474 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2475 TestCanvas canvas("SkParagraph_ItalicsParagraph.png");
2476 const char* text1 = "No italic ";
2477 const char* text2 = "Yes Italic ";
2478 const char* text3 = "No Italic again.";
2479
2480 ParagraphStyle paragraph_style;
2481 paragraph_style.turnHintingOff();
2482 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2483
2484 TextStyle text_style;
2485 text_style.setFontFamilies({SkString("Roboto")});
2486 text_style.setFontSize(10);
2487 text_style.setColor(SK_ColorRED);
2488 builder.pushStyle(text_style);
2489 builder.addText(text1, strlen(text1));
2490
2491 text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width,
2492 SkFontStyle::kItalic_Slant));
2493 builder.pushStyle(text_style);
2494 builder.addText(text2, strlen(text2));
2495 builder.pop();
2496 builder.addText(text3, strlen(text3));
2497
2498 auto paragraph = builder.Build();
2499 paragraph->layout(TestCanvasWidth);
2500 paragraph->paint(canvas.get(), 0, 0);
2501
2502 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2503
2504 REPORTER_ASSERT(reporter, impl->runs().size() == 3);
2505 REPORTER_ASSERT(reporter, impl->styles().size() == 3);
2506 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
2507 auto& line = impl->lines()[0];
2508 size_t index = 0;
2509 line.scanStyles(
2510 StyleType::kForeground,
2511 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
2512 switch (index) {
2513 case 0:
2514 REPORTER_ASSERT(
2515 reporter,
2516 style.getFontStyle().slant() == SkFontStyle::kUpright_Slant);
2517 break;
2518 case 1:
2519 REPORTER_ASSERT(reporter,
2520 style.getFontStyle().slant() == SkFontStyle::kItalic_Slant);
2521 break;
2522 case 2:
2523 REPORTER_ASSERT(
2524 reporter,
2525 style.getFontStyle().slant() == SkFontStyle::kUpright_Slant);
2526 break;
2527 default:
2528 REPORTER_ASSERT(reporter, false);
2529 break;
2530 }
2531 ++index;
2532 return true;
2533 });
2534 }
2535
2536 UNIX_ONLY_TEST(SkParagraph_ChineseParagraph, reporter) {
2537 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2538 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2539 TestCanvas canvas("SkParagraph_ChineseParagraph.png");
2540 const char* text =
2541 " 左線読設重説切後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育"
2542 "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得庭"
2543 "際輝求佐抗蒼提夜合逃表。注統天言件自謙雅載報紙喪。作画稿愛器灯女書利変探"
2544 "訃第金線朝開化建。子戦年帝励害表月幕株漠新期刊人秘。図的海力生禁挙保天戦"
2545 "聞条年所在口。";
2546 const size_t len = strlen(text);
2547
2548 ParagraphStyle paragraph_style;
2549 paragraph_style.setMaxLines(14);
2550 paragraph_style.setTextAlign(TextAlign::kJustify);
2551 paragraph_style.turnHintingOff();
2552 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2553
2554 auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline |
2555 TextDecoration::kLineThrough);
2556
2557 TextStyle text_style;
2558 text_style.setFontFamilies({SkString("Source Han Serif CN")});
2559 text_style.setFontSize(35);
2560 text_style.setColor(SK_ColorBLACK);
2561 text_style.setLetterSpacing(2);
2562 text_style.setHeight(1);
2563 text_style.setDecoration(decoration);
2564 text_style.setDecorationColor(SK_ColorBLACK);
2565 text_style.setDecorationStyle(TextDecorationStyle::kSolid);
2566 builder.pushStyle(text_style);
2567 builder.addText(text, len);
2568 builder.pop();
2569
2570 auto paragraph = builder.Build();
2571 paragraph->layout(TestCanvasWidth - 100);
2572 paragraph->paint(canvas.get(), 0, 0);
2573
2574 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
2575
2576 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2577
2578 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
2579 REPORTER_ASSERT(reporter, impl->lines().size() == 7);
2580 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
2581 if (impl->styles().size() >= 1) {
2582 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
2583 }
2584 }
2585
2586 // Checked: disabled for TxtLib
2587 UNIX_ONLY_TEST(SkParagraph_ArabicParagraph, reporter) {
2588 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2589 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2590 TestCanvas canvas("SkParagraph_ArabicParagraph.png");
2591 const char* text =
2592 "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
2593 "بمباركة التقليدية قام عن. تصفح";
2594 const size_t len = strlen(text);
2595
2596 ParagraphStyle paragraph_style;
2597 paragraph_style.setMaxLines(14);
2598 paragraph_style.setTextAlign(TextAlign::kJustify);
2599 paragraph_style.turnHintingOff();
2600 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2601
2602 auto decoration = (TextDecoration)(TextDecoration::kUnderline | TextDecoration::kOverline |
2603 TextDecoration::kLineThrough);
2604
2605 TextStyle text_style;
2606 text_style.setFontFamilies({SkString("Katibeh")});
2607 text_style.setFontSize(35);
2608 text_style.setColor(SK_ColorBLACK);
2609 text_style.setLetterSpacing(2);
2610 text_style.setDecoration(decoration);
2611 text_style.setDecorationColor(SK_ColorBLACK);
2612 text_style.setDecorationStyle(TextDecorationStyle::kSolid);
2613 builder.pushStyle(text_style);
2614 builder.addText(text, len);
2615 builder.pop();
2616
2617 auto paragraph = builder.Build();
2618 paragraph->layout(TestCanvasWidth - 100);
2619 paragraph->paint(canvas.get(), 0, 0);
2620
2621 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
2622
2623 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2624
2625 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
2626 REPORTER_ASSERT(reporter, impl->lines().size() == 2);
2627 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
2628 if (impl->styles().size() >= 1) {
2629 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
2630 }
2631 }
2632
2633 // Checked: DIFF (2 boxes and each space is a word)
2634 UNIX_ONLY_TEST(SkParagraph_ArabicRectsParagraph, reporter) {
2635
2636 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2637 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2638 TestCanvas canvas("SkParagraph_ArabicRectsParagraph.png");
2639 const char* text = "بمباركة التقليدية قام عن. تصفح يد ";
2640 const size_t len = strlen(text);
2641
2642 ParagraphStyle paragraph_style;
2643 paragraph_style.turnHintingOff();
2644 paragraph_style.setMaxLines(14);
2645 paragraph_style.setTextAlign(TextAlign::kRight);
2646 paragraph_style.setTextDirection(TextDirection::kRtl);
2647 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2648
2649 TextStyle text_style;
2650 text_style.setFontFamilies({SkString("Noto Naskh Arabic")});
2651 text_style.setFontSize(26);
2652 text_style.setWordSpacing(5);
2653 text_style.setColor(SK_ColorBLACK);
2654 text_style.setDecoration(TextDecoration::kUnderline);
2655 text_style.setDecorationColor(SK_ColorBLACK);
2656 builder.pushStyle(text_style);
2657 builder.addText(text, len);
2658 builder.pop();
2659
2660 auto paragraph = builder.Build();
2661 paragraph->layout(TestCanvasWidth - 100);
2662
2663 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2664 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
2665
2666 paragraph->paint(canvas.get(), 0, 0);
2667
2668 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
2669 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
2670 std::vector<TextBox> boxes = paragraph->getRectsForRange(0, 100, rect_height_style, rect_width_style);
2671 canvas.drawRects(SK_ColorRED, boxes);
2672
2673 REPORTER_ASSERT(reporter, boxes.size() == 1ull);
2674 if (boxes.size() >= 1) {
2675 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 538.120f, EPSILON100)); // DIFF: 510.09375
2676 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.280f, EPSILON100));
2677 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 900, EPSILON100));
2678 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100));
2679 }
2680 }
2681
2682 // Checked DIFF+
2683 // This test shows now 2 boxes for [36:40) range:
2684 // [36:38) for arabic text and [38:39) for the last space
2685 // that has default paragraph direction (LTR) and is placed at the end of the paragraph
2686 UNIX_ONLY_TEST(SkParagraph_ArabicRectsLTRLeftAlignParagraph, reporter) {
2687
2688 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2689 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2690 TestCanvas canvas("SkParagraph_ArabicRectsLTRLeftAlignParagraph.png");
2691 const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد ";
2692 const size_t len = strlen(text);
2693
2694 ParagraphStyle paragraph_style;
2695 paragraph_style.turnHintingOff();
2696 paragraph_style.setMaxLines(14);
2697 paragraph_style.setTextAlign(TextAlign::kLeft);
2698 paragraph_style.setTextDirection(TextDirection::kLtr);
2699 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2700
2701 TextStyle text_style;
2702 text_style.setFontFamilies({SkString("Noto Naskh Arabic")});
2703 text_style.setFontSize(26);
2704 text_style.setWordSpacing(5);
2705 text_style.setColor(SK_ColorBLACK);
2706 text_style.setDecoration(TextDecoration::kUnderline);
2707 text_style.setDecorationColor(SK_ColorBLACK);
2708 builder.pushStyle(text_style);
2709 builder.addText(text, len);
2710 builder.pop();
2711
2712 auto paragraph = builder.Build();
2713 paragraph->layout(TestCanvasWidth - 100);
2714
2715 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2716 REPORTER_ASSERT(reporter, impl->runs().size() == 3);
2717
2718 paragraph->paint(canvas.get(), 0, 0);
2719
2720 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
2721 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
2722 // There are 39 codepoints: [0:39); asking for [36:40) would give the same as for [36:39)
2723 std::vector<TextBox> boxes = paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style);
2724 canvas.drawRects(SK_ColorRED, boxes);
2725
2726 REPORTER_ASSERT(reporter, boxes.size() == 2ull);
2727 if (boxes.size() >= 2) {
2728 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 65.65f, EPSILON100)); // DIFF: 89.40625
2729 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.27f, EPSILON100));
2730 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 86.89f, EPSILON100)); // DIFF: 121.87891
2731 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44.0f, EPSILON100));
2732 }
2733 }
2734
2735 // Checked DIFF+
2736 UNIX_ONLY_TEST(SkParagraph_ArabicRectsLTRRightAlignParagraph, reporter) {
2737
2738 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2739 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2740 TestCanvas canvas("SkParagraph_ArabicRectsLTRRightAlignParagraph.png");
2741 const char* text = "Helloبمباركة التقليدية قام عن. تصفح يد ";
2742 const size_t len = strlen(text);
2743
2744 ParagraphStyle paragraph_style;
2745 paragraph_style.turnHintingOff();
2746 paragraph_style.setMaxLines(14);
2747 paragraph_style.setTextAlign(TextAlign::kRight);
2748 paragraph_style.setTextDirection(TextDirection::kLtr);
2749 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
2750
2751 TextStyle text_style;
2752 text_style.setFontFamilies({SkString("Noto Naskh Arabic")});
2753 text_style.setFontSize(26);
2754 text_style.setWordSpacing(5);
2755 text_style.setColor(SK_ColorBLACK);
2756 text_style.setDecoration(TextDecoration::kUnderline);
2757 text_style.setDecorationColor(SK_ColorBLACK);
2758 builder.pushStyle(text_style);
2759 builder.addText(text, len);
2760 builder.pop();
2761
2762 auto paragraph = builder.Build();
2763 paragraph->layout(TestCanvasWidth - 100);
2764
2765 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
2766 REPORTER_ASSERT(reporter, impl->runs().size() == 3);
2767
2768 paragraph->paint(canvas.get(), 0, 0);
2769
2770 RectHeightStyle rect_height_style = RectHeightStyle::kMax;
2771 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
2772 std::vector<TextBox> boxes =
2773 paragraph->getRectsForRange(36, 40, rect_height_style, rect_width_style);
2774 canvas.drawRects(SK_ColorRED, boxes);
2775
2776 REPORTER_ASSERT(reporter, boxes.size() == 2ull); // DIFF
2777 if (boxes.size() >= 2) {
2778 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 561.1f, EPSILON100)); // DIFF
2779 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), -0.27f, EPSILON100));
2780 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 582.34f, EPSILON100)); // DIFF
2781 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 44, EPSILON100));
2782 }
2783 }
2784
2785 UNIX_ONLY_TEST(SkParagraph_GetGlyphPositionAtCoordinateParagraph, reporter) {
2786 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2787 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2788 TestCanvas canvas("SkParagraph_GetGlyphPositionAtCoordinateParagraph.png");
2789 const char* text =
2790 "12345 67890 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
2791 "67890 12345";
2792 const size_t len = strlen(text);
2793
2794 ParagraphStyle paragraphStyle;
2795 paragraphStyle.setTextAlign(TextAlign::kLeft);
2796 paragraphStyle.setMaxLines(10);
2797 paragraphStyle.turnHintingOff();
2798 TextStyle textStyle;
2799 textStyle.setFontFamilies({SkString("Roboto")});
2800 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width,
2801 SkFontStyle::kUpright_Slant));
2802 textStyle.setFontSize(50);
2803 textStyle.setLetterSpacing(1);
2804 textStyle.setWordSpacing(5);
2805 textStyle.setHeight(1);
2806 textStyle.setColor(SK_ColorBLACK);
2807
2808 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
2809 builder.pushStyle(textStyle);
2810 builder.addText(text, len);
2811 builder.pop();
2812
2813 auto paragraph = builder.Build();
2814 paragraph->layout(550);
2815 paragraph->paint(canvas.get(), 0, 0);
2816
2817 // Tests for getGlyphPositionAtCoordinate()
2818 // NOTE: resulting values can be a few off from their respective positions in
2819 // the original text because the final trailing whitespaces are sometimes not
2820 // drawn (namely, when using "justify" alignment) and therefore are not active
2821 // glyphs.
2822 REPORTER_ASSERT(reporter,
2823 paragraph->getGlyphPositionAtCoordinate(-10000, -10000).position == 0);
2824 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-1, -1).position == 0);
2825 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(0, 0).position == 0);
2826 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(3, 3).position == 0);
2827 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 1).position == 1);
2828 REPORTER_ASSERT(reporter,
2829 paragraph->getGlyphPositionAtCoordinate(300, 2).position == 11);
2830 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.2f).position == 11);
2831 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(302, 2.6f).position == 11);
2832 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(301, 2.1f).position == 11);
2833 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 20).position == 18);
2834 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(450, 20).position == 16);
2835 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(100000, 90).position == 36);
2836 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(-100000, 90).position == 18);
2837 REPORTER_ASSERT(reporter,
2838 paragraph->getGlyphPositionAtCoordinate(20, -80).position == 1);
2839 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 90).position == 18);
2840 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 170).position == 36);
2841 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 180).position == 72);
2842 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(70, 180).position == 56);
2843 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(1, 270).position == 72);
2844 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(35, 90).position == 19);
2845 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(10000, 10000).position == 77);
2846 REPORTER_ASSERT(reporter, paragraph->getGlyphPositionAtCoordinate(85, 10000).position == 75);
2847 }
2848
2849 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeParagraph, reporter) {
2850 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2851 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2852 TestCanvas canvas("SkParagraph_GetRectsForRangeParagraph.png");
2853 const char* text =
2854 "12345, \"67890\" 12345 67890 12345 67890 12345 67890 12345 67890 12345 "
2855 "67890 12345";
2856 const size_t len = strlen(text);
2857
2858 ParagraphStyle paragraphStyle;
2859 paragraphStyle.setTextAlign(TextAlign::kLeft);
2860 paragraphStyle.setMaxLines(10);
2861 paragraphStyle.turnHintingOff();
2862 TextStyle textStyle;
2863 textStyle.setFontFamilies({SkString("Roboto")});
2864 textStyle.setFontSize(50);
2865 textStyle.setColor(SK_ColorBLACK);
2866 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
2867 SkFontStyle::kUpright_Slant));
2868
2869 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
2870 builder.pushStyle(textStyle);
2871 builder.addText(text, len);
2872 builder.pop();
2873
2874 auto paragraph = builder.Build();
2875 paragraph->layout(550);
2876 paragraph->paint(canvas.get(), 0, 0);
2877
2878 RectHeightStyle heightStyle = RectHeightStyle::kMax;
2879 RectWidthStyle widthStyle = RectWidthStyle::kTight;
2880
2881 SkPaint paint;
2882 paint.setStyle(SkPaint::kStroke_Style);
2883 paint.setAntiAlias(true);
2884 paint.setStrokeWidth(1);
2885
2886 {
2887 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
2888 REPORTER_ASSERT(reporter, result.empty());
2889 }
2890 {
2891 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
2892 canvas.drawRects(SK_ColorRED, result);
2893 REPORTER_ASSERT(reporter, result.size() == 1);
2894 if (result.size() >= 1) {
2895 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100));
2896 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
2897 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 28.417f, EPSILON100));
2898 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
2899 }
2900 }
2901 {
2902 auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
2903 canvas.drawRects(SK_ColorBLUE, result);
2904 REPORTER_ASSERT(reporter, result.size() == 1);
2905 if (result.size() >= 1) {
2906 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 56.835f, EPSILON100));
2907 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
2908 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 177.97f, EPSILON100));
2909 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
2910 }
2911 }
2912 {
2913 auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
2914 canvas.drawRects(SK_ColorGREEN, result);
2915 REPORTER_ASSERT(reporter, result.size() == 1);
2916 if (result.size() >= 1) {
2917 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 177.97f, EPSILON100));
2918 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
2919 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 507.031f, EPSILON100));
2920 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
2921 }
2922 }
2923 {
2924 auto result = paragraph->getRectsForRange(30, 100, heightStyle, widthStyle);
2925 canvas.drawRects(SK_ColorRED, result);
2926 REPORTER_ASSERT(reporter, result.size() == 4);
2927 if (result.size() >= 4) {
2928 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 211.375f, EPSILON100));
2929 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, EPSILON100));
2930 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 463.623f, EPSILON100));
2931 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100));
2932 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 0, EPSILON100));
2933 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 236.406f, EPSILON100));
2934 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 142.089f, EPSILON100));
2935 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 295, EPSILON100));
2936 }
2937 }
2938 {
2939 auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
2940 canvas.drawRects(SK_ColorBLUE, result);
2941 REPORTER_ASSERT(reporter, result.size() == 1);
2942 if (result.size() >= 1) {
2943 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 450.1875f, EPSILON20));
2944 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
2945 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 519.47266f, EPSILON20));
2946 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
2947 }
2948 }
2949 {
2950 auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
2951 REPORTER_ASSERT(reporter, result.empty());
2952 }
2953 }
2954
2955 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeTight, reporter) {
2956 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
2957 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
2958 TestCanvas canvas("SkParagraph_GetRectsForRangeTight.png");
2959 const char* text =
2960 "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
2961 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
2962 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
2963 const size_t len = strlen(text);
2964 /*
2965 ( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)
2966 S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S S
2967 G G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GGG G G G G G GG
2968 W W W W W W W W W W W W W W W W W W W W
2969
2970 */
2971 ParagraphStyle paragraphStyle;
2972 paragraphStyle.setTextAlign(TextAlign::kLeft);
2973 paragraphStyle.setMaxLines(10);
2974 paragraphStyle.turnHintingOff();
2975 TextStyle textStyle;
2976 textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
2977 textStyle.setFontSize(50);
2978 textStyle.setColor(SK_ColorBLACK);
2979 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
2980 SkFontStyle::kUpright_Slant));
2981
2982 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
2983 builder.pushStyle(textStyle);
2984 builder.addText(text, len);
2985 builder.pop();
2986
2987 auto paragraph = builder.Build();
2988 paragraph->layout(550);
2989 paragraph->paint(canvas.get(), 0, 0);
2990
2991 RectHeightStyle heightStyle = RectHeightStyle::kTight;
2992 RectWidthStyle widthStyle = RectWidthStyle::kTight;
2993 {
2994 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
2995 REPORTER_ASSERT(reporter, result.empty());
2996 }
2997 {
2998 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
2999 canvas.drawRects(SK_ColorRED, result);
3000 REPORTER_ASSERT(reporter, result.size() == 1);
3001 if (result.size() >= 1) {
3002 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100));
3003 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100));
3004 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 16.898f, EPSILON100));
3005 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100));
3006 }
3007 }
3008 {
3009 auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
3010 canvas.drawRects(SK_ColorBLUE, result);
3011 REPORTER_ASSERT(reporter, result.size() == 1);
3012 if (result.size() >= 1) {
3013 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 66.899f, EPSILON100));
3014 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100));
3015 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 264.099f, EPSILON100));
3016 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100));
3017 }
3018 }
3019 {
3020 auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
3021 canvas.drawRects(SK_ColorGREEN, result);
3022 REPORTER_ASSERT(reporter, result.size() == 2);
3023 if (result.size() >= 2) {
3024 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 264.099f, EPSILON100));
3025 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0, EPSILON100));
3026 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 595.085f, EPSILON50));
3027 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 74, EPSILON100));
3028 }
3029 }
3030 }
3031
3032 // Checked: DIFF+
3033 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle, reporter) {
3034 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3035 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3036 TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingMiddle.png");
3037 const char* text =
3038 "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
3039 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
3040 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
3041 const size_t len = strlen(text);
3042
3043 ParagraphStyle paragraphStyle;
3044 paragraphStyle.setTextAlign(TextAlign::kLeft);
3045 paragraphStyle.setMaxLines(10);
3046 paragraphStyle.turnHintingOff();
3047 TextStyle textStyle;
3048 textStyle.setFontFamilies({SkString("Roboto")});
3049 textStyle.setFontSize(50);
3050 textStyle.setHeight(1.6f);
3051 textStyle.setHeightOverride(true);
3052 textStyle.setColor(SK_ColorBLACK);
3053 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
3054 SkFontStyle::kUpright_Slant));
3055
3056 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3057 builder.pushStyle(textStyle);
3058 builder.addText(text, len);
3059 builder.pop();
3060
3061 auto paragraph = builder.Build();
3062 paragraph->layout(550);
3063 paragraph->paint(canvas.get(), 0, 0);
3064
3065 RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingMiddle;
3066 RectWidthStyle widthStyle = RectWidthStyle::kMax;
3067 {
3068 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
3069 REPORTER_ASSERT(reporter, result.empty());
3070 }
3071
3072 {
3073 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
3074 canvas.drawRects(SK_ColorRED, result);
3075 REPORTER_ASSERT(reporter, result.size() == 1);
3076 if (result.size() >= 1) {
3077 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100));
3078 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3079 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100));
3080 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100));
3081 }
3082 }
3083 {
3084 auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
3085 canvas.drawRects(SK_ColorBLUE, result);
3086 REPORTER_ASSERT(reporter, result.size() == 1);
3087 if (result.size() >= 1) {
3088 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100));
3089 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3090 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100));
3091 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100));
3092 }
3093 }
3094 {
3095 auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
3096 canvas.drawRects(SK_ColorGREEN, result);
3097 REPORTER_ASSERT(reporter, result.size() == 1);
3098 if (result.size() >= 1) {
3099 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON20));
3100 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3101 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON20));
3102 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100));
3103 }
3104 }
3105 {
3106 auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
3107 canvas.drawRects(SK_ColorRED, result);
3108 REPORTER_ASSERT(reporter, result.size() == 8);
3109 if (result.size() >= 8) {
3110 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON20));
3111 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 88.473305f, EPSILON100));
3112 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20));
3113 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 168.47331f, EPSILON100));
3114
3115 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20));
3116 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 88.473305f, EPSILON100));
3117 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20));
3118 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 168.47331f, EPSILON100));
3119
3120 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100));
3121 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 168.47331f, EPSILON100));
3122 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20));
3123 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 248.47331f, EPSILON100));
3124
3125 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20));
3126 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 168.47331f, EPSILON100));
3127 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20));
3128 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 248.47331f, EPSILON100));
3129
3130 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100));
3131 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 248.47331f, EPSILON100));
3132 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20));
3133 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 328.47333f, EPSILON100));
3134
3135 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100));
3136 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 328.47333f, EPSILON100));
3137 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20));
3138 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 408.4733f, EPSILON100));
3139 }
3140 }
3141 {
3142 auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
3143 canvas.drawRects(SK_ColorBLUE, result);
3144 REPORTER_ASSERT(reporter, result.size() == 2); // DIFF
3145 if (result.size() >= 2) {
3146 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON20));
3147 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3148 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON20));
3149 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 88.473305f, EPSILON100));
3150
3151 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON20));
3152 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100));
3153 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20));
3154 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 88.473305f, EPSILON100));
3155 }
3156 }
3157 {
3158 auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
3159 REPORTER_ASSERT(reporter, result.empty());
3160 }
3161 }
3162
3163 // Checked: NO DIFF+
3164 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingTop, reporter) {
3165 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3166 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3167 TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingTop.png");
3168 const char* text =
3169 "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
3170 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
3171 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
3172 const size_t len = strlen(text);
3173
3174 ParagraphStyle paragraphStyle;
3175 paragraphStyle.setTextAlign(TextAlign::kLeft);
3176 paragraphStyle.setMaxLines(10);
3177 paragraphStyle.turnHintingOff();
3178 TextStyle textStyle;
3179 textStyle.setFontFamilies({SkString("Roboto")});
3180 textStyle.setFontSize(50);
3181 textStyle.setHeight(1.6f);
3182 textStyle.setHeightOverride(true);
3183 textStyle.setColor(SK_ColorBLACK);
3184 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
3185 SkFontStyle::kUpright_Slant));
3186
3187 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3188 builder.pushStyle(textStyle);
3189 builder.addText(text, len);
3190 builder.pop();
3191
3192 auto paragraph = builder.Build();
3193 paragraph->layout(550);
3194 paragraph->paint(canvas.get(), 0, 0);
3195
3196 RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingTop;
3197 RectWidthStyle widthStyle = RectWidthStyle::kMax;
3198 {
3199 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
3200 REPORTER_ASSERT(reporter, result.empty());
3201 }
3202
3203 {
3204 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
3205 canvas.drawRects(SK_ColorRED, result);
3206 REPORTER_ASSERT(reporter, result.size() == 1);
3207 if (result.size() >= 1) {
3208 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100));
3209 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3210 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.4296889f, EPSILON100));
3211 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100));
3212 }
3213 }
3214 {
3215 auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
3216 canvas.drawRects(SK_ColorBLUE, result);
3217 REPORTER_ASSERT(reporter, result.size() == 1);
3218 if (result.size() >= 1) {
3219 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.429688f, EPSILON100));
3220 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3221 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.00781f, EPSILON100));
3222 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100));
3223 }
3224 }
3225 {
3226 auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
3227 canvas.drawRects(SK_ColorGREEN, result);
3228 REPORTER_ASSERT(reporter, result.size() == 1);
3229 if (result.size() >= 1) {
3230 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100));
3231 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3232 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.0625f, EPSILON50));
3233 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100));
3234 }
3235 }
3236 {
3237 auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
3238 canvas.drawRects(SK_ColorMAGENTA, result);
3239 REPORTER_ASSERT(reporter, result.size() == 8);
3240 if (result.size() >= 8) {
3241 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.00781f, EPSILON100));
3242 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 80, EPSILON100));
3243 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20));
3244 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 160, EPSILON100));
3245
3246 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20));
3247 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 80, EPSILON100));
3248 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20));
3249 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 160, EPSILON100));
3250
3251 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON100));
3252 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 160, EPSILON100));
3253 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20));
3254 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 240, EPSILON100));
3255
3256 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20));
3257 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 160, EPSILON100));
3258 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.02344f, EPSILON20));
3259 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 240, EPSILON100));
3260
3261 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON100));
3262 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 240, EPSILON100));
3263 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.02344f, EPSILON20));
3264 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 320, EPSILON100));
3265
3266 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON100));
3267 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 320, EPSILON100));
3268 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.02344f, EPSILON20));
3269 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 400, EPSILON100));
3270 }
3271 }
3272 {
3273 auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
3274 canvas.drawRects(SK_ColorBLACK, result);
3275 REPORTER_ASSERT(reporter, result.size() == 2); // DIFF
3276 if (result.size() >= 2) {
3277 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.72656f, EPSILON20));
3278 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946615f, EPSILON100));
3279 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.23047f, EPSILON20));
3280 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 80, EPSILON100));
3281
3282 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.23047f, EPSILON50));
3283 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946615f, EPSILON100));
3284 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.02344f, EPSILON20));
3285 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 80, EPSILON100));
3286 }
3287 }
3288 {
3289 auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
3290 REPORTER_ASSERT(reporter, result.empty());
3291 }
3292 }
3293
3294 // Checked: NO DIFF+
3295 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeIncludeLineSpacingBottom, reporter) {
3296 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3297 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3298 TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeLineSpacingBottom.png");
3299 const char* text =
3300 "( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
3301 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)("
3302 " ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)( ´・‿・`)";
3303 const size_t len = strlen(text);
3304
3305 ParagraphStyle paragraphStyle;
3306 paragraphStyle.setTextAlign(TextAlign::kLeft);
3307 paragraphStyle.setMaxLines(10);
3308 paragraphStyle.turnHintingOff();
3309 TextStyle textStyle;
3310 textStyle.setFontFamilies({SkString("Roboto")});
3311 textStyle.setFontSize(50);
3312 textStyle.setHeight(1.6f);
3313 textStyle.setHeightOverride(true);
3314 textStyle.setColor(SK_ColorBLACK);
3315 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
3316 SkFontStyle::kUpright_Slant));
3317
3318 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3319 builder.pushStyle(textStyle);
3320 builder.addText(text, len);
3321 builder.pop();
3322
3323 auto paragraph = builder.Build();
3324 paragraph->layout(550);
3325 paragraph->paint(canvas.get(), 0, 0);
3326
3327 RectHeightStyle heightStyle = RectHeightStyle::kIncludeLineSpacingBottom;
3328 RectWidthStyle widthStyle = RectWidthStyle::kMax;
3329 {
3330 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
3331 REPORTER_ASSERT(reporter, result.empty());
3332 }
3333
3334 {
3335 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
3336 canvas.drawRects(SK_ColorRED, result);
3337 REPORTER_ASSERT(reporter, result.size() == 1);
3338 if (result.size() >= 1) {
3339 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100));
3340 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100));
3341 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 17.429f, EPSILON100));
3342 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100));
3343 }
3344 }
3345 {
3346 auto result = paragraph->getRectsForRange(2, 8, heightStyle, widthStyle);
3347 canvas.drawRects(SK_ColorBLUE, result);
3348 REPORTER_ASSERT(reporter, result.size() == 1);
3349 if (result.size() >= 1) {
3350 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 67.4298f, EPSILON100));
3351 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100));
3352 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 190.007f, EPSILON100));
3353 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100));
3354 }
3355 }
3356 {
3357 auto result = paragraph->getRectsForRange(8, 21, heightStyle, widthStyle);
3358 canvas.drawRects(SK_ColorGREEN, result);
3359 REPORTER_ASSERT(reporter, result.size() == 1);
3360 if (result.size() >= 1) {
3361 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON100));
3362 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100));
3363 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 508.062f, EPSILON50));
3364 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100));
3365 }
3366 }
3367 {
3368 auto result = paragraph->getRectsForRange(30, 150, heightStyle, widthStyle);
3369 canvas.drawRects(SK_ColorMAGENTA, result);
3370 REPORTER_ASSERT(reporter, result.size() == 8);
3371 if (result.size() >= 8) {
3372 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 190.007f, EPSILON20));
3373 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 96.946f, EPSILON100));
3374 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 525.687f, EPSILON20));
3375 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 176.946f, EPSILON100));
3376
3377 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 525.687f, EPSILON20));
3378 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 96.946f, EPSILON100));
3379 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20));
3380 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 176.946f, EPSILON100));
3381
3382 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.left(), 0, EPSILON20));
3383 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.top(), 176.946f, EPSILON100));
3384 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.right(), 531.574f, EPSILON20));
3385 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[2].rect.bottom(), 256.946f, EPSILON100));
3386
3387 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.left(), 531.574f, EPSILON20));
3388 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.top(), 176.946f, EPSILON100));
3389 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.right(), 570.023f, EPSILON20));
3390 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[3].rect.bottom(), 256.946f, EPSILON100));
3391
3392 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.left(), 0, EPSILON20));
3393 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.top(), 256.946f, EPSILON100));
3394 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.right(), 570.023f, EPSILON20));
3395 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[4].rect.bottom(), 336.946f, EPSILON100));
3396
3397 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.left(), 0, EPSILON20));
3398 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.top(), 336.946f, EPSILON100));
3399 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.right(), 570.023f, EPSILON20));
3400 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[5].rect.bottom(), 416.946f, EPSILON100));
3401 }
3402 }
3403 {
3404 auto result = paragraph->getRectsForRange(19, 22, heightStyle, widthStyle);
3405 canvas.drawRects(SK_ColorBLACK, result);
3406 REPORTER_ASSERT(reporter, result.size() == 2); // DIFF
3407 if (result.size() >= 2) {
3408 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 463.726f, EPSILON20));
3409 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 16.946f, EPSILON100));
3410 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 530.230f, EPSILON20));
3411 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 96.946f, EPSILON100));
3412
3413 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.left(), 530.230f, EPSILON20));
3414 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.top(), 16.946f, EPSILON100));
3415 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.right(), 570.023f, EPSILON20));
3416 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[1].rect.bottom(), 96.946f, EPSILON100));
3417 }
3418 }
3419 {
3420 auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
3421 REPORTER_ASSERT(reporter, result.empty());
3422 }
3423 }
3424
3425 // This is the test I cannot accommodate
3426 // Any text range gets a smallest glyph rectangle
3427 DEF_TEST_DISABLED(SkParagraph_GetRectsForRangeIncludeCombiningCharacter, reporter) {
3428 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3429 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3430 TestCanvas canvas("SkParagraph_GetRectsForRangeIncludeCombiningCharacter.png");
3431 const char* text = "ดีสวัสดีชาวโลกที่น่ารัก";
3432 const size_t len = strlen(text);
3433
3434 ParagraphStyle paragraphStyle;
3435 paragraphStyle.setTextAlign(TextAlign::kLeft);
3436 paragraphStyle.setMaxLines(10);
3437 paragraphStyle.turnHintingOff();
3438 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3439
3440 TextStyle textStyle;
3441 textStyle.setFontFamilies({SkString("Roboto")});
3442 textStyle.setFontSize(50);
3443 textStyle.setLetterSpacing(1);
3444 textStyle.setWordSpacing(5);
3445 textStyle.setHeight(1);
3446 textStyle.setColor(SK_ColorBLACK);
3447
3448 builder.pushStyle(textStyle);
3449 builder.addText(text, len);
3450 builder.pop();
3451
3452 auto paragraph = builder.Build();
3453 paragraph->layout(TestCanvasWidth - 100);
3454 paragraph->paint(canvas.get(), 0, 0);
3455
3456 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
3457 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
3458
3459 RectHeightStyle heightStyle = RectHeightStyle::kTight;
3460 RectWidthStyle widthStyle = RectWidthStyle::kTight;
3461 {
3462 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
3463 REPORTER_ASSERT(reporter, result.empty());
3464 }
3465 {
3466 auto first = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
3467 auto second = paragraph->getRectsForRange(1, 2, heightStyle, widthStyle);
3468 auto last = paragraph->getRectsForRange(0, 2, heightStyle, widthStyle);
3469 REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1);
3470 REPORTER_ASSERT(reporter, second[0].rect == last[0].rect);
3471 }
3472 {
3473 auto first = paragraph->getRectsForRange(3, 4, heightStyle, widthStyle);
3474 auto second = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
3475 auto last = paragraph->getRectsForRange(3, 5, heightStyle, widthStyle);
3476 REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 1 && last.size() == 1);
3477 REPORTER_ASSERT(reporter, second[0].rect == last[0].rect);
3478 }
3479 {
3480 auto first = paragraph->getRectsForRange(14, 15, heightStyle, widthStyle);
3481 auto second = paragraph->getRectsForRange(15, 16, heightStyle, widthStyle);
3482 auto third = paragraph->getRectsForRange(16, 17, heightStyle, widthStyle);
3483 auto last = paragraph->getRectsForRange(14, 17, heightStyle, widthStyle);
3484 REPORTER_ASSERT(reporter, first.size() == 0 && second.size() == 0 && third.size() == 1 && last.size() == 1);
3485 REPORTER_ASSERT(reporter, third[0].rect == last[0].rect);
3486 }
3487 }
3488
3489 // Checked: NO DIFF
3490 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeCenterParagraph, reporter) {
3491 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3492 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3493 TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraph.png");
3494 // Minikin uses a hard coded list of unicode characters that he treats as invisible - as spaces.
3495 // It's absolutely wrong - invisibility is a glyph attribute, not character/grapheme.
3496 // Any attempt to substitute one for another leads to errors
3497 // (for instance, some fonts can use these hard coded characters for something that is visible)
3498 const char* text = "01234   "; // includes ideographic space and english space.
3499 const size_t len = strlen(text);
3500
3501 ParagraphStyle paragraphStyle;
3502 paragraphStyle.setTextAlign(TextAlign::kCenter);
3503 paragraphStyle.setMaxLines(10);
3504 paragraphStyle.turnHintingOff();
3505 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3506
3507 TextStyle textStyle;
3508 textStyle.setFontFamilies({SkString("Roboto")});
3509 textStyle.setFontSize(50);
3510 textStyle.setHeight(1);
3511 textStyle.setColor(SK_ColorBLACK);
3512 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
3513 SkFontStyle::kUpright_Slant));
3514
3515 builder.pushStyle(textStyle);
3516 builder.addText(text, len);
3517 builder.pop();
3518
3519 auto paragraph = builder.Build();
3520 paragraph->layout(550);
3521 paragraph->paint(canvas.get(), 0, 0);
3522
3523 // Some of the formatting lazily done on paint
3524 RectHeightStyle heightStyle = RectHeightStyle::kMax;
3525 RectWidthStyle widthStyle = RectWidthStyle::kTight;
3526 {
3527 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
3528 REPORTER_ASSERT(reporter, result.empty());
3529 }
3530
3531 {
3532 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
3533 canvas.drawRects(SK_ColorRED, result);
3534 REPORTER_ASSERT(reporter, result.size() == 1);
3535 if (result.size() >= 1) {
3536 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100));
3537 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
3538 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100));
3539 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
3540 }
3541 }
3542
3543 {
3544 auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle);
3545 canvas.drawRects(SK_ColorBLUE, result);
3546 REPORTER_ASSERT(reporter, result.size() == 1);
3547 if (result.size() >= 1) {
3548 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, EPSILON100));
3549 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
3550 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, EPSILON100));
3551 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
3552 }
3553 }
3554
3555 {
3556 auto result = paragraph->getRectsForRange(4, 5, heightStyle, widthStyle);
3557 canvas.drawRects(SK_ColorGREEN, result);
3558 REPORTER_ASSERT(reporter, result.size() == 1);
3559 if (result.size() >= 1) {
3560 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100));
3561 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
3562 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 346.044f, EPSILON100));
3563 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
3564 }
3565 }
3566
3567 {
3568 auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle);
3569 canvas.drawRects(SK_ColorBLACK, result);
3570 REPORTER_ASSERT(reporter, result.size() == 1); // DIFF
3571 if (result.size() >= 1) {
3572 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, EPSILON100));
3573 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
3574 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100));
3575 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
3576 }
3577 }
3578
3579 {
3580 auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
3581 canvas.drawRects(SK_ColorRED, result);
3582 REPORTER_ASSERT(reporter, result.size() == 1);
3583 if (result.size() >= 1) {
3584 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, EPSILON100));
3585 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
3586 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, EPSILON100));
3587 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
3588 }
3589 }
3590
3591 {
3592 auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
3593 REPORTER_ASSERT(reporter, result.empty());
3594 }
3595 }
3596
3597 // Checked DIFF+
3598 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered, reporter) {
3599 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3600 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3601 TestCanvas canvas("SkParagraph_GetRectsForRangeCenterParagraphNewlineCentered.png");
3602 const char* text = "01234\n";
3603 const size_t len = strlen(text);
3604
3605 ParagraphStyle paragraphStyle;
3606 paragraphStyle.setTextAlign(TextAlign::kCenter);
3607 paragraphStyle.setMaxLines(10);
3608 paragraphStyle.turnHintingOff();
3609 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3610
3611 TextStyle textStyle;
3612 textStyle.setFontFamilies({SkString("Roboto")});
3613 textStyle.setFontSize(50);
3614 textStyle.setHeight(1);
3615 textStyle.setColor(SK_ColorBLACK);
3616 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
3617 SkFontStyle::kUpright_Slant));
3618
3619 builder.pushStyle(textStyle);
3620 builder.addText(text, len);
3621 builder.pop();
3622
3623 auto paragraph = builder.Build();
3624 paragraph->layout(550);
3625
3626 paragraph->paint(canvas.get(), 0, 0);
3627
3628 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
3629 REPORTER_ASSERT(reporter, impl->lines().size() == 2);
3630
3631 RectHeightStyle heightStyle = RectHeightStyle::kMax;
3632 RectWidthStyle widthStyle = RectWidthStyle::kTight;
3633 {
3634 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
3635 REPORTER_ASSERT(reporter, result.empty());
3636 }
3637
3638 {
3639 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
3640 canvas.drawRects(SK_ColorRED, result);
3641 REPORTER_ASSERT(reporter, result.size() == 1);
3642 if (result.size() >= 1) {
3643 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, EPSILON100));
3644 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, EPSILON100));
3645 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, EPSILON100));
3646 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, EPSILON100));
3647 }
3648 }
3649
3650 {
3651 auto result = paragraph->getRectsForRange(6, 7, heightStyle, widthStyle);
3652 canvas.drawRects(SK_ColorBLUE, result);
3653 REPORTER_ASSERT(reporter, result.size() == 1);
3654 if (result.size() >= 1) {
3655 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 275.0f, EPSILON100));
3656 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.406f, EPSILON100));
3657 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275.0f, EPSILON100));
3658 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, EPSILON100));
3659 }
3660 }
3661 }
3662
3663 // Checked NO DIFF
3664 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeCenterMultiLineParagraph, reporter) {
3665 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3666 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3667 TestCanvas canvas("SkParagraph_GetRectsForRangeCenterMultiLineParagraph.png");
3668 const char* text = "01234   \n0123  "; // includes ideographic space and english space.
3669 const size_t len = strlen(text);
3670
3671 ParagraphStyle paragraphStyle;
3672 paragraphStyle.setTextAlign(TextAlign::kCenter);
3673 paragraphStyle.setMaxLines(10);
3674 paragraphStyle.turnHintingOff();
3675 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3676
3677 TextStyle textStyle;
3678 textStyle.setFontFamilies({SkString("Roboto")});
3679 textStyle.setFontSize(50);
3680 textStyle.setHeight(1);
3681 textStyle.setColor(SK_ColorBLACK);
3682 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
3683 SkFontStyle::kUpright_Slant));
3684
3685 builder.pushStyle(textStyle);
3686 builder.addText(text, len);
3687 builder.pop();
3688
3689 auto paragraph = builder.Build();
3690 paragraph->layout(550);
3691
3692 paragraph->paint(canvas.get(), 0, 0);
3693
3694 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
3695
3696 REPORTER_ASSERT(reporter, impl->lines().size() == 2);
3697
3698 RectHeightStyle heightStyle = RectHeightStyle::kMax;
3699 RectWidthStyle widthStyle = RectWidthStyle::kTight;
3700 SkScalar epsilon = 0.01f;
3701 {
3702 auto result = paragraph->getRectsForRange(0, 0, heightStyle, widthStyle);
3703 REPORTER_ASSERT(reporter, result.empty());
3704 }
3705 {
3706 auto result = paragraph->getRectsForRange(0, 1, heightStyle, widthStyle);
3707 canvas.drawRects(SK_ColorRED, result);
3708 REPORTER_ASSERT(reporter, result.size() == 1);
3709 if (result.size() >= 1) {
3710 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 203.955f, epsilon));
3711 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
3712 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 232.373f, epsilon));
3713 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
3714 }
3715 }
3716 {
3717 auto result = paragraph->getRectsForRange(2, 4, heightStyle, widthStyle);
3718 canvas.drawRects(SK_ColorBLUE, result);
3719 REPORTER_ASSERT(reporter, result.size() == 1);
3720 if (result.size() >= 1) {
3721 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 260.791f, epsilon));
3722 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
3723 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 317.626f, epsilon));
3724 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
3725 }
3726 }
3727 {
3728 auto result = paragraph->getRectsForRange(4, 6, heightStyle, widthStyle);
3729 canvas.drawRects(SK_ColorGREEN, result);
3730 REPORTER_ASSERT(reporter, result.size() == 1);
3731 if (result.size() >= 1) {
3732 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 317.626f, epsilon));
3733 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
3734 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon));
3735 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
3736 }
3737 }
3738 {
3739 auto result = paragraph->getRectsForRange(5, 6, heightStyle, widthStyle);
3740 canvas.drawRects(SK_ColorYELLOW, result);
3741 REPORTER_ASSERT(reporter, result.size() == 1);
3742 if (result.size() >= 1) {
3743 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 346.044f, epsilon));
3744 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 0.40625f, epsilon));
3745 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 358.494f, epsilon));
3746 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 59, epsilon));
3747 }
3748 }
3749 {
3750 auto result = paragraph->getRectsForRange(10, 12, heightStyle, widthStyle);
3751 canvas.drawRects(SK_ColorCYAN, result);
3752 REPORTER_ASSERT(reporter, result.size() == 1);
3753 if (result.size() >= 1) {
3754 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 218.164f, epsilon));
3755 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
3756 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 275, epsilon));
3757 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
3758 }
3759 }
3760 {
3761 auto result = paragraph->getRectsForRange(14, 18, heightStyle, widthStyle);
3762 canvas.drawRects(SK_ColorBLACK, result);
3763 REPORTER_ASSERT(reporter, result.size() == 1);
3764 if (result.size() >= 1) {
3765 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 331.835f, epsilon));
3766 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 59.40625f, epsilon));
3767 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 419.189f, epsilon));
3768 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 118, epsilon));
3769 }
3770 }
3771 {
3772 auto result = paragraph->getRectsForRange(21, 21, heightStyle, widthStyle);
3773 REPORTER_ASSERT(reporter, result.empty());
3774 }
3775 }
3776
3777 // Checked: DIFF (line height rounding error)
3778 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeStrut, reporter) {
3779 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3780 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3781 TestCanvas canvas("SkParagraph_GetRectsForRangeStrut.png");
3782 const char* text = "Chinese 字典";
3783 const size_t len = strlen(text);
3784
3785 StrutStyle strutStyle;
3786 strutStyle.setStrutEnabled(true);
3787 strutStyle.setFontFamilies({SkString("Roboto")});
3788 strutStyle.setFontSize(14.0);
3789
3790 ParagraphStyle paragraphStyle;
3791 paragraphStyle.setStrutStyle(strutStyle);
3792
3793 TextStyle textStyle;
3794 textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
3795 textStyle.setFontSize(20);
3796 textStyle.setColor(SK_ColorBLACK);
3797
3798 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3799 builder.pushStyle(textStyle);
3800 builder.addText(text, len);
3801 builder.pop();
3802
3803 auto paragraph = builder.Build();
3804 paragraph->layout(550);
3805 paragraph->paint(canvas.get(), 0, 0);
3806
3807 {
3808 auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax);
3809 canvas.drawRects(SK_ColorGREEN, result);
3810 REPORTER_ASSERT(reporter, result.size() == 1);
3811 }
3812
3813 {
3814 auto result = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax);
3815 canvas.drawRects(SK_ColorRED, result);
3816 REPORTER_ASSERT(reporter, result.size() == 1);
3817 if (result.size() >= 1) {
3818 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.left(), 0, EPSILON100));
3819 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 10.611f, EPSILON2));
3820 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.right(), 118.605f, EPSILON50));
3821 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.bottom(), 27.017f, EPSILON2));
3822 }
3823 }
3824 }
3825
3826 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeStrutWithHeight, reporter) {
3827 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3828 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3829 const char* text = "A";
3830 const size_t len = strlen(text);
3831
3832 StrutStyle strutStyle;
3833 strutStyle.setStrutEnabled(true);
3834 strutStyle.setFontFamilies({SkString("Roboto")});
3835 strutStyle.setFontSize(14.0);
3836 strutStyle.setHeightOverride(true);
3837 strutStyle.setHeight(2.0);
3838 strutStyle.setLeading(3.0);
3839
3840 ParagraphStyle paragraphStyle;
3841 paragraphStyle.setStrutStyle(strutStyle);
3842
3843 TextStyle textStyle;
3844 textStyle.setFontFamilies({SkString("Roboto")});
3845 textStyle.setFontSize(10);
3846 textStyle.setColor(SK_ColorBLACK);
3847
3848 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3849 builder.pushStyle(textStyle);
3850 builder.addText(text, len);
3851 builder.pop();
3852
3853 auto paragraph = builder.Build();
3854 paragraph->layout(550);
3855
3856 auto result = paragraph->getRectsForRange(0, 1, RectHeightStyle::kStrut, RectWidthStyle::kMax);
3857 REPORTER_ASSERT(reporter, result.size() == 1);
3858 if (result.size() >= 1) {
3859 // Half of the strut leading: 3.0 * 14.0 / 2
3860 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 21.0, EPSILON100));
3861 // Strut height 2.0 * 14.0
3862 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.height(), 28.0, EPSILON100));
3863 }
3864 }
3865
3866 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeStrutWithHeightAndHalfLeading, reporter) {
3867 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3868 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3869 const char* text = "A";
3870 const size_t len = strlen(text);
3871
3872 StrutStyle strutStyle;
3873 strutStyle.setStrutEnabled(true);
3874 strutStyle.setFontFamilies({SkString("Roboto")});
3875 strutStyle.setFontSize(14.0);
3876 strutStyle.setHeightOverride(true);
3877 strutStyle.setHeight(2.0);
3878 strutStyle.setLeading(3.0);
3879 strutStyle.setHalfLeading(true);
3880
3881 ParagraphStyle paragraphStyle;
3882 paragraphStyle.setStrutStyle(strutStyle);
3883
3884 TextStyle textStyle;
3885 textStyle.setFontFamilies({SkString("Roboto")});
3886 textStyle.setFontSize(10);
3887 textStyle.setColor(SK_ColorBLACK);
3888
3889 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3890 builder.pushStyle(textStyle);
3891 builder.addText(text, len);
3892 builder.pop();
3893
3894 auto paragraph = builder.Build();
3895 paragraph->layout(550);
3896
3897 // Produces the same results as halfLeading = false.
3898 auto result = paragraph->getRectsForRange(0, 1, RectHeightStyle::kStrut, RectWidthStyle::kMax);
3899 REPORTER_ASSERT(reporter, result.size() == 1);
3900 if (result.size() >= 1) {
3901 // Half of the strut leading: 3.0 * 14.0 / 2
3902 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.top(), 21.0, EPSILON100));
3903 // Strut height 2.0 * 14.0
3904 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(result[0].rect.height(), 28.0, EPSILON100));
3905 }
3906 }
3907
3908 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeStrutFallback, reporter) {
3909 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3910 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3911 TestCanvas canvas("SkParagraph_GetRectsForRangeStrutFallback.png");
3912 const char* text = "Chinese 字典";
3913 const size_t len = strlen(text);
3914
3915 StrutStyle strutStyle;
3916 strutStyle.setStrutEnabled(false);
3917
3918 ParagraphStyle paragraphStyle;
3919 paragraphStyle.setStrutStyle(strutStyle);
3920
3921 TextStyle textStyle;
3922 textStyle.setFontFamilies({SkString("Noto Sans CJK JP")});
3923 textStyle.setFontSize(20);
3924 textStyle.setColor(SK_ColorBLACK);
3925
3926 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3927 builder.pushStyle(textStyle);
3928 builder.addText(text, len);
3929 builder.pop();
3930
3931 auto paragraph = builder.Build();
3932 paragraph->layout(550);
3933 paragraph->paint(canvas.get(), 0, 0);
3934
3935 auto result1 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kTight, RectWidthStyle::kMax);
3936 canvas.drawRects(SK_ColorGREEN, result1);
3937 REPORTER_ASSERT(reporter, result1.size() == 1);
3938
3939 auto result2 = paragraph->getRectsForRange(0, 10, RectHeightStyle::kStrut, RectWidthStyle::kMax);
3940 canvas.drawRects(SK_ColorRED, result2);
3941 REPORTER_ASSERT(reporter, result2.size() == 1);
3942
3943 if (result1.size() >= 1 && result2.size() >= 1) {
3944 REPORTER_ASSERT(reporter, result1[0].rect == result2[0].rect);
3945 }
3946 }
3947
3948 // Checked: DIFF (small in numbers)
3949 UNIX_ONLY_TEST(SkParagraph_GetWordBoundaryParagraph, reporter) {
3950 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
3951 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
3952 TestCanvas canvas("SkParagraph_GetWordBoundaryParagraph.png");
3953 const char* text = "12345 67890 12345 67890 12345 67890 12345 "
3954 "67890 12345 67890 12345 67890 12345";
3955 const size_t len = strlen(text);
3956 ParagraphStyle paragraphStyle;
3957 paragraphStyle.setTextAlign(TextAlign::kLeft);
3958 paragraphStyle.setMaxLines(10);
3959 paragraphStyle.turnHintingOff();
3960 TextStyle textStyle;
3961 textStyle.setFontFamilies({SkString("Roboto")});
3962 textStyle.setFontSize(52);
3963 textStyle.setLetterSpacing(1.19039f);
3964 textStyle.setWordSpacing(5);
3965 textStyle.setHeight(1.5);
3966 textStyle.setHeightOverride(true);
3967 textStyle.setColor(SK_ColorBLACK);
3968
3969 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
3970 builder.pushStyle(textStyle);
3971 builder.addText(text, len);
3972 builder.pop();
3973
3974 auto paragraph = builder.Build();
3975 paragraph->layout(550);
3976 paragraph->paint(canvas.get(), 0, 0);
3977
3978 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(0) == SkRange<size_t>(0, 5));
3979 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(1) == SkRange<size_t>(0, 5));
3980 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(2) == SkRange<size_t>(0, 5));
3981 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(3) == SkRange<size_t>(0, 5));
3982 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(4) == SkRange<size_t>(0, 5));
3983 auto boxes = paragraph->getRectsForRange(5, 6, RectHeightStyle::kMax, RectWidthStyle::kTight);
3984 canvas.drawLines(SK_ColorRED, boxes);
3985
3986 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(5) == SkRange<size_t>(5, 7));
3987 boxes = paragraph->getRectsForRange(6, 7, RectHeightStyle::kMax, RectWidthStyle::kTight);
3988 canvas.drawLines(SK_ColorRED, boxes);
3989
3990 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(6) == SkRange<size_t>(5, 7));
3991 boxes = paragraph->getRectsForRange(7, 8, RectHeightStyle::kMax, RectWidthStyle::kTight);
3992 canvas.drawLines(SK_ColorRED, boxes);
3993
3994 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(7) == SkRange<size_t>(7, 12));
3995 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(8) == SkRange<size_t>(7, 12));
3996 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(9) == SkRange<size_t>(7, 12));
3997 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(10) == SkRange<size_t>(7, 12));
3998 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(11) == SkRange<size_t>(7, 12));
3999 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(12) == SkRange<size_t>(12, 13));
4000 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(13) == SkRange<size_t>(13, 18));
4001 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(30) == SkRange<size_t>(30, 31));
4002
4003 boxes = paragraph->getRectsForRange(12, 13, RectHeightStyle::kMax, RectWidthStyle::kTight);
4004 canvas.drawLines(SK_ColorRED, boxes);
4005 boxes = paragraph->getRectsForRange(13, 14, RectHeightStyle::kMax, RectWidthStyle::kTight);
4006 canvas.drawLines(SK_ColorRED, boxes);
4007 boxes = paragraph->getRectsForRange(18, 19, RectHeightStyle::kMax, RectWidthStyle::kTight);
4008 canvas.drawLines(SK_ColorRED, boxes);
4009 boxes = paragraph->getRectsForRange(19, 20, RectHeightStyle::kMax, RectWidthStyle::kTight);
4010 canvas.drawLines(SK_ColorRED, boxes);
4011 boxes = paragraph->getRectsForRange(24, 25, RectHeightStyle::kMax, RectWidthStyle::kTight);
4012 canvas.drawLines(SK_ColorRED, boxes);
4013 boxes = paragraph->getRectsForRange(25, 26, RectHeightStyle::kMax, RectWidthStyle::kTight);
4014 canvas.drawLines(SK_ColorRED, boxes);
4015 boxes = paragraph->getRectsForRange(30, 31, RectHeightStyle::kMax, RectWidthStyle::kTight);
4016 canvas.drawLines(SK_ColorRED, boxes);
4017 boxes = paragraph->getRectsForRange(31, 32, RectHeightStyle::kMax, RectWidthStyle::kTight);
4018 canvas.drawLines(SK_ColorRED, boxes);
4019
4020 auto outLen = static_cast<ParagraphImpl*>(paragraph.get())->text().size();
4021 REPORTER_ASSERT(reporter, paragraph->getWordBoundary(outLen - 1) == SkRange<size_t>(outLen - 5, outLen));
4022 }
4023
4024 // Checked: DIFF (unclear)
4025 UNIX_ONLY_TEST(SkParagraph_SpacingParagraph, reporter) {
4026 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4027 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4028 TestCanvas canvas("SkParagraph_SpacingParagraph.png");
4029 ParagraphStyle paragraph_style;
4030 paragraph_style.setMaxLines(10);
4031 paragraph_style.setTextAlign(TextAlign::kLeft);
4032 paragraph_style.turnHintingOff();
4033 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4034
4035 TextStyle text_style;
4036 text_style.setFontFamilies({SkString("Roboto")});
4037 text_style.setFontSize(50);
4038 text_style.setLetterSpacing(20);
4039 text_style.setWordSpacing(0);
4040 text_style.setColor(SK_ColorBLACK);
4041 builder.pushStyle(text_style);
4042 builder.addText("H", 1);
4043 builder.pop();
4044
4045 text_style.setLetterSpacing(10);
4046 text_style.setWordSpacing(0);
4047 builder.pushStyle(text_style);
4048 builder.addText("H", 1);
4049 builder.pop();
4050
4051 text_style.setLetterSpacing(20);
4052 text_style.setWordSpacing(0);
4053 builder.pushStyle(text_style);
4054 builder.addText("H", 1);
4055 builder.pop();
4056
4057 text_style.setLetterSpacing(0);
4058 text_style.setWordSpacing(0);
4059 builder.pushStyle(text_style);
4060 builder.addText("|", 1);
4061 builder.pop();
4062
4063 const char* hSpace = "H ";
4064 const size_t len = strlen(hSpace);
4065
4066 text_style.setLetterSpacing(0);
4067 text_style.setWordSpacing(20);
4068 builder.pushStyle(text_style);
4069 builder.addText(hSpace, len);
4070 builder.pop();
4071
4072 text_style.setLetterSpacing(0);
4073 text_style.setWordSpacing(0);
4074 builder.pushStyle(text_style);
4075 builder.addText(hSpace, len);
4076 builder.pop();
4077
4078 text_style.setLetterSpacing(0);
4079 text_style.setLetterSpacing(0);
4080 text_style.setWordSpacing(20);
4081 builder.pushStyle(text_style);
4082 builder.addText(hSpace, len);
4083 builder.pop();
4084
4085 auto paragraph = builder.Build();
4086 paragraph->layout(550);
4087 paragraph->paint(canvas.get(), 0, 0);
4088
4089 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4090 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
4091 size_t index = 0;
4092 impl->lines().begin()->scanStyles(StyleType::kLetterSpacing,
4093 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
4094 ++index;
4095 return true;
4096 });
4097 REPORTER_ASSERT(reporter, index == 4);
4098 index = 0;
4099 impl->lines().begin()->scanStyles(StyleType::kWordSpacing,
4100 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
4101 ++index;
4102 return true;
4103 });
4104 REPORTER_ASSERT(reporter, index == 4);
4105 }
4106
4107 // Checked: NO DIFF
4108 UNIX_ONLY_TEST(SkParagraph_LongWordParagraph, reporter) {
4109 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4110 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4111 TestCanvas canvas("SkParagraph_LongWordParagraph.png");
4112 const char* text =
4113 "A "
4114 "veryverylongwordtoseewherethiswillwraporifitwillatallandifitdoesthenthat"
4115 "wouldbeagoodthingbecausethebreakingisworking.";
4116 const size_t len = strlen(text);
4117
4118 ParagraphStyle paragraph_style;
4119 paragraph_style.turnHintingOff();
4120 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4121
4122 TextStyle text_style;
4123 text_style.setFontFamilies({SkString("Roboto")});
4124 text_style.setColor(SK_ColorRED);
4125 text_style.setFontSize(31);
4126 text_style.setLetterSpacing(0);
4127 text_style.setWordSpacing(0);
4128 text_style.setColor(SK_ColorBLACK);
4129 text_style.setHeight(1);
4130 builder.pushStyle(text_style);
4131 builder.addText(text, len);
4132 builder.pop();
4133
4134 auto paragraph = builder.Build();
4135 paragraph->layout(TestCanvasWidth / 2);
4136 paragraph->paint(canvas.get(), 0, 0);
4137
4138 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4139 REPORTER_ASSERT(reporter, impl->text().size() == std::string{text}.length());
4140 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
4141 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
4142 if (impl->styles().size() >= 1) {
4143 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
4144 }
4145 REPORTER_ASSERT(reporter, impl->lines().size() == 4);
4146 if (impl->lines().size() >= 4) {
4147 REPORTER_ASSERT(reporter, impl->lines()[0].width() > TestCanvasWidth / 2 - 20);
4148 REPORTER_ASSERT(reporter, impl->lines()[1].width() > TestCanvasWidth / 2 - 20);
4149 REPORTER_ASSERT(reporter, impl->lines()[2].width() > TestCanvasWidth / 2 - 20);
4150 }
4151 }
4152
4153 // Checked: DIFF?
4154 UNIX_ONLY_TEST(SkParagraph_KernScaleParagraph, reporter) {
4155 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4156 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4157 TestCanvas canvas("SkParagraph_KernScaleParagraph.png");
4158
4159 const char* text1 = "AVAVAWAH A0 V0 VA To The Lo";
4160 const char* text2 = " Dialog Text List lots of words to see "
4161 "if kerning works on a bigger set of characters AVAVAW";
4162 float scale = 3.0f;
4163 ParagraphStyle paragraph_style;
4164 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4165 TextStyle text_style;
4166 text_style.setFontFamilies({SkString("Droid Serif")});
4167 text_style.setFontSize(100 / scale);
4168 text_style.setColor(SK_ColorBLACK);
4169
4170 builder.pushStyle(text_style);
4171 builder.addText(text1, strlen(text1));
4172 builder.pushStyle(text_style);
4173 builder.addText("A", 1);
4174 builder.pushStyle(text_style);
4175 builder.addText("V", 1);
4176 text_style.setFontSize(14 / scale);
4177 builder.pushStyle(text_style);
4178 builder.addText(text2, strlen(text2));
4179 builder.pop();
4180
4181 auto paragraph = builder.Build();
4182 paragraph->layout(TestCanvasWidth / scale);
4183 canvas.get()->scale(scale, scale);
4184 paragraph->paint(canvas.get(), 0, 0);
4185 canvas.get()->scale(1, 1);
4186
4187 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4188
4189 // First and second lines must have the same width, the third one must be bigger
4190 REPORTER_ASSERT(reporter, impl->lines().size() == 3);
4191 if (impl->lines().size() >= 3) {
4192 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].width(), 285.858f, EPSILON100));
4193 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].width(), 329.709f, EPSILON100));
4194 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].width(), 120.619f, EPSILON100));
4195 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[0].height(), 39.00f, EPSILON100));
4196 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[1].height(), 39.00f, EPSILON100));
4197 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->lines()[2].height(), 05.00f, EPSILON100));
4198 }
4199 }
4200
4201 // Checked: DIFF+
4202 UNIX_ONLY_TEST(SkParagraph_NewlineParagraph, reporter) {
4203 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4204 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4205 TestCanvas canvas("SkParagraph_NewlineParagraph.png");
4206 const char* text =
4207 "line1\nline2 test1 test2 test3 test4 test5 test6 test7\nline3\n\nline4 "
4208 "test1 test2 test3 test4";
4209 const size_t len = strlen(text);
4210
4211 ParagraphStyle paragraph_style;
4212 paragraph_style.turnHintingOff();
4213 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4214
4215 TextStyle text_style;
4216 text_style.setFontFamilies({SkString("Roboto")});
4217 text_style.setColor(SK_ColorRED);
4218 text_style.setFontSize(60);
4219 text_style.setColor(SK_ColorBLACK);
4220 text_style.setHeight(1);
4221 builder.pushStyle(text_style);
4222 builder.addText(text, len);
4223 builder.pop();
4224
4225 auto paragraph = builder.Build();
4226 paragraph->layout(TestCanvasWidth - 300);
4227 paragraph->paint(canvas.get(), 0, 0);
4228
4229 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4230 // Minikin does not count empty lines but SkParagraph does
4231 REPORTER_ASSERT(reporter, impl->lines().size() == 7);
4232
4233 if (impl->lines().size() >= 7) {
4234 REPORTER_ASSERT(reporter, impl->lines()[0].offset().fY == 0);
4235 REPORTER_ASSERT(reporter, impl->lines()[1].offset().fY == 70);
4236 REPORTER_ASSERT(reporter, impl->lines()[2].offset().fY == 140);
4237 REPORTER_ASSERT(reporter, impl->lines()[3].offset().fY == 210);
4238 REPORTER_ASSERT(reporter, impl->lines()[4].offset().fY == 280); // Empty line
4239 REPORTER_ASSERT(reporter, impl->lines()[5].offset().fY == 350);
4240 REPORTER_ASSERT(reporter, impl->lines()[6].offset().fY == 420);
4241 }
4242 }
4243
4244 // TODO: Fix underline
4245 UNIX_ONLY_TEST(SkParagraph_EmojiParagraph, reporter) {
4246 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4247 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4248 TestCanvas canvas("SkParagraph_EmojiParagraph.png");
4249 const char* text =
4250 "😀😃😄😁😆😅😂🤣☺😇🙂😍😡😟😢😻👽💩👍👎🙏👌👋👄👁👦👼👨‍🚀👨‍🚒🙋‍♂️👳👨‍👨‍👧‍👧\
4251 💼👡👠☂🐶🐰🐻🐼🐷🐒🐵🐔🐧🐦🐋🐟🐡🕸🐌🐴🐊🐄🐪🐘🌸🌏🔥🌟🌚🌝💦💧\
4252 ❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴‍♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓\
4253 📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️‍🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴";
4254 const size_t len = strlen(text);
4255
4256 ParagraphStyle paragraph_style;
4257 paragraph_style.turnHintingOff();
4258 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4259
4260 TextStyle text_style;
4261 text_style.setFontFamilies({SkString("Noto Color Emoji")});
4262 text_style.setFontSize(50);
4263 text_style.setDecoration(TextDecoration::kUnderline);
4264 text_style.setColor(SK_ColorBLACK);
4265 builder.pushStyle(text_style);
4266 builder.addText(text, len);
4267 builder.pop();
4268
4269 auto paragraph = builder.Build();
4270 paragraph->layout(TestCanvasWidth);
4271 paragraph->paint(canvas.get(), 0, 0);
4272
4273 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
4274
4275 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4276
4277 REPORTER_ASSERT(reporter, impl->lines().size() == 8);
4278
4279 for (auto& line : impl->lines()) {
4280 if (&line != impl->lines().data() + impl->lines().size() - 1) {
4281 // The actual value is 50_size / 109_ppemX * 136_advance = ~62.385319
4282 // FreeType reports advances in 24.6 fixed point, so each is 62.390625
4283 REPORTER_ASSERT(reporter,
4284 line.width() == 998.25f ||
4285 (998.1 < line.width() && line.width() < 998.2),
4286 "width: %f", line.width());
4287 } else {
4288 REPORTER_ASSERT(reporter, line.width() < 998.25f, "width: %f", line.width());
4289 }
4290 REPORTER_ASSERT(reporter, line.height() == 59, "height: %f", line.height());
4291 }
4292 }
4293
4294 // Checked: DIFF+
4295 UNIX_ONLY_TEST(SkParagraph_EmojiMultiLineRectsParagraph, reporter) {
4296 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4297 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4298 TestCanvas canvas("SkParagraph_EmojiMultiLineRectsParagraph.png");
4299 const char* text =
4300 "👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧i🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸"
4301 "👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸"
4302 "👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸"
4303 "👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸👩‍👩‍👦👩‍👩‍👧‍👧🇺🇸"
4304 "❄🍕🍔🍟🥝🍱🕶🎩🏈⚽🚴‍♀️🎻🎼🎹🚨🚎🚐⚓🛳🚀🚁🏪🏢🖱⏰📱💾💉📉🛏🔑🔓"
4305 "📁🗓📊❤💯🚫🔻♠♣🕓❗🏳🏁🏳️‍🌈🇮🇹🇱🇷🇺🇸🇬🇧🇨🇳🇧🇴";
4306 const size_t len = strlen(text);
4307
4308 ParagraphStyle paragraph_style;
4309 paragraph_style.turnHintingOff();
4310 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4311
4312 TextStyle text_style;
4313 text_style.setFontFamilies({SkString("Noto Color Emoji")});
4314 text_style.setFontSize(50);
4315 text_style.setColor(SK_ColorBLACK);
4316 builder.pushStyle(text_style);
4317 builder.addText(text, len);
4318 builder.pop();
4319
4320 auto paragraph = builder.Build();
4321 paragraph->layout(TestCanvasWidth - 300);
4322 paragraph->paint(canvas.get(), 0, 0);
4323
4324 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
4325 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
4326
4327 auto result = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
4328 REPORTER_ASSERT(reporter, result.size() == 0);
4329
4330 result = paragraph->getRectsForRange(0, 119, rect_height_style, rect_width_style);
4331 REPORTER_ASSERT(reporter, result.size() == 2);
4332 canvas.drawRects(SK_ColorRED, result);
4333
4334 result = paragraph->getRectsForRange(122, 132, rect_height_style, rect_width_style);
4335 REPORTER_ASSERT(reporter, result.size() == 0);
4336 // We changed the selection algorithm and now the selection is empty
4337 canvas.drawRects(SK_ColorBLUE, result);
4338
4339 auto pos = paragraph->getGlyphPositionAtCoordinate(610, 100).position;
4340 result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style);
4341 REPORTER_ASSERT(reporter, result.size() == 2);
4342 canvas.drawRects(SK_ColorGREEN, result);
4343
4344 pos = paragraph->getGlyphPositionAtCoordinate(580, 100).position;
4345 result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style);
4346 REPORTER_ASSERT(reporter, result.size() == 2);
4347 canvas.drawRects(SK_ColorGREEN, result);
4348
4349 pos = paragraph->getGlyphPositionAtCoordinate(560, 100).position;
4350 result = paragraph->getRectsForRange(0, pos, rect_height_style, rect_width_style);
4351 REPORTER_ASSERT(reporter, result.size() == 2);
4352 canvas.drawRects(SK_ColorGREEN, result);
4353 }
4354
4355 // Checked: DIFF (line breaking)
4356 UNIX_ONLY_TEST(SkParagraph_RepeatLayoutParagraph, reporter) {
4357 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4358 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4359 TestCanvas canvas("SkParagraph_RepeatLayoutParagraph.png");
4360 const char* text =
4361 "Sentence to layout at diff widths to get diff line counts. short words "
4362 "short words short words short words short words short words short words "
4363 "short words short words short words short words short words short words "
4364 "end";
4365 const size_t len = strlen(text);
4366
4367 ParagraphStyle paragraph_style;
4368 paragraph_style.turnHintingOff();
4369 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4370
4371 TextStyle text_style;
4372 text_style.setFontFamilies({SkString("Roboto")});
4373 text_style.setFontSize(31);
4374 text_style.setColor(SK_ColorBLACK);
4375 builder.pushStyle(text_style);
4376 builder.addText(text, len);
4377 builder.pop();
4378
4379 auto paragraph = builder.Build();
4380 paragraph->layout(300);
4381
4382 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4383 // Some of the formatting lazily done on paint
4384 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
4385 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
4386 REPORTER_ASSERT(reporter, impl->lines().size() == 12);
4387
4388 paragraph->layout(600);
4389 paragraph->paint(canvas.get(), 0, 0);
4390 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
4391 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
4392 REPORTER_ASSERT(reporter, impl->lines().size() == 6);
4393 }
4394
4395 // Checked: NO DIFF
4396 UNIX_ONLY_TEST(SkParagraph_Ellipsize, reporter) {
4397 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4398 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4399 TestCanvas canvas("SkParagraph_Ellipsize.png");
4400 const char* text =
4401 "This is a very long sentence to test if the text will properly wrap "
4402 "around and go to the next line. Sometimes, short sentence. Longer "
4403 "sentences are okay too because they are nessecary. Very short. ";
4404 const size_t len = strlen(text);
4405
4406 ParagraphStyle paragraph_style;
4407 paragraph_style.setMaxLines(1);
4408 std::u16string ellipsis = u"\u2026";
4409 paragraph_style.setEllipsis(ellipsis);
4410 std::u16string e = paragraph_style.getEllipsisUtf16();
4411 paragraph_style.turnHintingOff();
4412 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4413
4414 TextStyle text_style;
4415 text_style.setFontFamilies({SkString("Roboto")});
4416 text_style.setColor(SK_ColorBLACK);
4417 builder.pushStyle(text_style);
4418 builder.addText(text, len);
4419 builder.pop();
4420
4421 auto paragraph = builder.Build();
4422 paragraph->layout(TestCanvasWidth);
4423 paragraph->paint(canvas.get(), 0, 0);
4424
4425 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4426
4427 // Check that the ellipsizer limited the text to one line and did not wrap to a second line.
4428 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
4429 if (impl->lines().size() >= 1) {
4430 auto& line = impl->lines()[0];
4431 REPORTER_ASSERT(reporter, line.ellipsis() != nullptr);
4432 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
4433 }
4434 }
4435
4436 // Checked: NO DIFF
4437 UNIX_ONLY_TEST(SkParagraph_UnderlineShiftParagraph, reporter) {
4438 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4439 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4440 TestCanvas canvas("SkParagraph_UnderlineShiftParagraph.png");
4441 const char* text1 = "fluttser ";
4442 const char* text2 = "mdje";
4443 const char* text3 = "fluttser mdje";
4444
4445 ParagraphStyle paragraph_style;
4446 paragraph_style.turnHintingOff();
4447 paragraph_style.setTextAlign(TextAlign::kLeft);
4448 paragraph_style.setMaxLines(2);
4449 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4450
4451 TextStyle text_style;
4452 text_style.setFontFamilies({SkString("Roboto")});
4453 text_style.setColor(SK_ColorBLACK);
4454 builder.pushStyle(text_style);
4455 builder.addText(text1, strlen(text1));
4456 text_style.setDecoration(TextDecoration::kUnderline);
4457 text_style.setDecorationColor(SK_ColorBLACK);
4458 builder.pushStyle(text_style);
4459 builder.addText(text2, strlen(text2));
4460 builder.pop();
4461
4462 auto paragraph = builder.Build();
4463 paragraph->layout(TestCanvasWidth);
4464 paragraph->paint(canvas.get(), 0, 0);
4465
4466 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4467
4468 ParagraphBuilderImpl builder1(paragraph_style, fontCollection, get_unicode());
4469 text_style.setDecoration(TextDecoration::kNoDecoration);
4470 builder1.pushStyle(text_style);
4471 builder1.addText(text3, strlen(text3));
4472 builder1.pop();
4473
4474 auto paragraph1 = builder1.Build();
4475 paragraph1->layout(TestCanvasWidth);
4476 paragraph1->paint(canvas.get(), 0, 25);
4477
4478 auto impl1 = static_cast<ParagraphImpl*>(paragraph1.get());
4479
4480 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
4481 REPORTER_ASSERT(reporter, impl1->lines().size() == 1);
4482
4483 auto rect = paragraph->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight)
4484 .front()
4485 .rect;
4486 auto rect1 = paragraph1->getRectsForRange(0, 12, RectHeightStyle::kMax, RectWidthStyle::kTight)
4487 .front()
4488 .rect;
4489 REPORTER_ASSERT(reporter, rect.fLeft == rect1.fLeft);
4490 REPORTER_ASSERT(reporter, rect.fRight == rect1.fRight);
4491
4492 for (size_t i = 0; i < 12; ++i) {
4493 // Not all ranges produce a rectangle ("fl" goes into one cluster so [0:1) is empty)
4494 auto r1 = paragraph->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight);
4495 auto r2 = paragraph1->getRectsForRange(i, i + 1, RectHeightStyle::kMax, RectWidthStyle::kTight);
4496
4497 REPORTER_ASSERT(reporter, r1.size() == r2.size());
4498 if (!r1.empty() && !r2.empty()) {
4499 REPORTER_ASSERT(reporter, r1.front().rect.fLeft == r2.front().rect.fLeft);
4500 REPORTER_ASSERT(reporter, r1.front().rect.fRight == r2.front().rect.fRight);
4501 }
4502 }
4503 }
4504
4505 // Checked: NO DIFF
4506 UNIX_ONLY_TEST(SkParagraph_SimpleShadow, reporter) {
4507 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4508 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4509 TestCanvas canvas("SkParagraph_SimpleShadow.png");
4510 const char* text = "Hello World Text Dialog";
4511 const size_t len = strlen(text);
4512
4513 ParagraphStyle paragraph_style;
4514 paragraph_style.turnHintingOff();
4515 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4516
4517 TextStyle text_style;
4518 text_style.setFontFamilies({SkString("Roboto")});
4519 text_style.setColor(SK_ColorBLACK);
4520 text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0));
4521 builder.pushStyle(text_style);
4522 builder.addText(text, len);
4523
4524 auto paragraph = builder.Build();
4525 paragraph->layout(TestCanvasWidth);
4526 paragraph->paint(canvas.get(), 10.0, 15.0);
4527
4528 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4529
4530 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
4531 REPORTER_ASSERT(reporter, impl->styles().size() == 1);
4532 size_t index = 0;
4533 for (auto& line : impl->lines()) {
4534 line.scanStyles(StyleType::kShadow,
4535 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
4536 REPORTER_ASSERT(reporter, index == 0 && style.equals(text_style));
4537 ++index;
4538 return true;
4539 });
4540 }
4541 }
4542
4543 // Checked: NO DIFF
4544 UNIX_ONLY_TEST(SkParagraph_ComplexShadow, reporter) {
4545 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4546 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4547 TestCanvas canvas("SkParagraph_ComplexShadow.png");
4548 const char* text = "Text Chunk ";
4549 const size_t len = strlen(text);
4550
4551 ParagraphStyle paragraph_style;
4552 paragraph_style.turnHintingOff();
4553 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4554
4555 TextStyle text_style;
4556 text_style.setFontFamilies({SkString("Roboto")});
4557 text_style.setColor(SK_ColorBLACK);
4558 text_style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(2.0f, 2.0f), 1.0f));
4559 builder.pushStyle(text_style);
4560 builder.addText(text, len);
4561
4562 text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(2.0f, 2.0f), 5.0f));
4563 text_style.addShadow(TextShadow(SK_ColorGREEN, SkPoint::Make(10.0f, -5.0f), 3.0f));
4564 builder.pushStyle(text_style);
4565 builder.addText(text, len);
4566 builder.pop();
4567
4568 builder.addText(text, len);
4569
4570 text_style.addShadow(TextShadow(SK_ColorRED, SkPoint::Make(0.0f, 1.0f), 0.0f));
4571 builder.pushStyle(text_style);
4572 builder.addText(text, len);
4573 builder.pop();
4574
4575 builder.addText(text, len);
4576
4577 auto paragraph = builder.Build();
4578 paragraph->layout(TestCanvasWidth);
4579 paragraph->paint(canvas.get(), 10.0, 15.0);
4580
4581 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4582
4583 size_t index = 0;
4584 for (auto& line : impl->lines()) {
4585 line.scanStyles(StyleType::kShadow,
4586 [&](TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
4587 ++index;
4588 switch (index) {
4589 case 1:
4590 REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
4591 break;
4592 case 2:
4593 REPORTER_ASSERT(reporter, style.getShadowNumber() == 3);
4594 break;
4595 case 3:
4596 REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
4597 break;
4598 case 4:
4599 REPORTER_ASSERT(reporter, style.getShadowNumber() == 4);
4600 REPORTER_ASSERT(reporter, style.equals(text_style));
4601 break;
4602 case 5:
4603 REPORTER_ASSERT(reporter, style.getShadowNumber() == 1);
4604 break;
4605 default:
4606 REPORTER_ASSERT(reporter, false);
4607 }
4608 return true;
4609 });
4610 }
4611 }
4612
4613 // Checked: NO DIFF
4614 UNIX_ONLY_TEST(SkParagraph_BaselineParagraph, reporter) {
4615 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4616 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4617 TestCanvas canvas("SkParagraph_BaselineParagraph.png");
4618 const char* text =
4619 "左線読設Byg後碁給能上目秘使約。満毎冠行来昼本可必図将発確年。今属場育"
4620 "図情闘陰野高備込制詩西校客。審対江置講今固残必託地集済決維駆年策。立得";
4621 const size_t len = strlen(text);
4622
4623 ParagraphStyle paragraph_style;
4624 paragraph_style.turnHintingOff();
4625 paragraph_style.setMaxLines(14);
4626 paragraph_style.setTextAlign(TextAlign::kJustify);
4627 paragraph_style.setHeight(1.5);
4628 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4629
4630 TextStyle text_style;
4631 text_style.setFontFamilies({SkString("Source Han Serif CN")});
4632 text_style.setColor(SK_ColorBLACK);
4633 text_style.setFontSize(55);
4634 text_style.setLetterSpacing(2);
4635 text_style.setDecorationStyle(TextDecorationStyle::kSolid);
4636 text_style.setDecorationColor(SK_ColorBLACK);
4637 builder.pushStyle(text_style);
4638 builder.addText(text, len);
4639 builder.pop();
4640
4641 auto paragraph = builder.Build();
4642 paragraph->layout(TestCanvasWidth - 100);
4643 paragraph->paint(canvas.get(), 0, 0);
4644
4645 SkRect rect1 = SkRect::MakeXYWH(0, paragraph->getIdeographicBaseline(),
4646 paragraph->getMaxWidth(),
4647 paragraph->getIdeographicBaseline());
4648 SkRect rect2 = SkRect::MakeXYWH(0, paragraph->getAlphabeticBaseline(),
4649 paragraph->getMaxWidth(),
4650 paragraph->getAlphabeticBaseline());
4651 canvas.drawLine(SK_ColorRED, rect1, false);
4652 canvas.drawLine(SK_ColorGREEN, rect2, false);
4653
4654 REPORTER_ASSERT(reporter,
4655 SkScalarNearlyEqual(paragraph->getIdeographicBaseline(), 79.035f, EPSILON100));
4656 REPORTER_ASSERT(reporter,
4657 SkScalarNearlyEqual(paragraph->getAlphabeticBaseline(), 63.305f, EPSILON100));
4658 }
4659
4660 // Checked: NO DIFF (number of runs only)
4661 UNIX_ONLY_TEST(SkParagraph_FontFallbackParagraph, reporter) {
4662 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4663 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4664 TestCanvas canvas("SkParagraph_FontFallbackParagraph.png");
4665
4666 const char* text1 = "Roboto 字典 "; // Roboto + unresolved
4667 const char* text2 = "Homemade Apple 字典"; // Homemade Apple + Noto Sans...
4668 const char* text3 = "Chinese 字典"; // Homemade Apple + Source Han
4669
4670 ParagraphStyle paragraph_style;
4671 paragraph_style.turnHintingOff();
4672 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4673
4674 TextStyle text_style;
4675 text_style.setFontFamilies({
4676 SkString("Not a real font"),
4677 SkString("Also a fake font"),
4678 SkString("So fake it is obvious"),
4679 SkString("Next one should be a real font..."),
4680 SkString("Roboto"),
4681 SkString("another fake one in between"),
4682 SkString("Homemade Apple"),
4683 });
4684 text_style.setColor(SK_ColorBLACK);
4685 builder.pushStyle(text_style);
4686 builder.addText(text1, strlen(text1));
4687
4688 text_style.setFontFamilies({
4689 SkString("Not a real font"),
4690 SkString("Also a fake font"),
4691 SkString("So fake it is obvious"),
4692 SkString("Homemade Apple"),
4693 SkString("Next one should be a real font..."),
4694 SkString("Roboto"),
4695 SkString("another fake one in between"),
4696 SkString("Noto Sans CJK JP"),
4697 SkString("Source Han Serif CN"),
4698 });
4699 builder.pushStyle(text_style);
4700 builder.addText(text2, strlen(text2));
4701
4702 text_style.setFontFamilies({
4703 SkString("Not a real font"),
4704 SkString("Also a fake font"),
4705 SkString("So fake it is obvious"),
4706 SkString("Homemade Apple"),
4707 SkString("Next one should be a real font..."),
4708 SkString("Roboto"),
4709 SkString("another fake one in between"),
4710 SkString("Source Han Serif CN"),
4711 SkString("Noto Sans CJK JP"),
4712 });
4713 builder.pushStyle(text_style);
4714 builder.addText(text3, strlen(text3));
4715
4716 builder.pop();
4717
4718 auto paragraph = builder.Build();
4719 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == -1); // Not shaped yet
4720 paragraph->layout(TestCanvasWidth);
4721 paragraph->paint(canvas.get(), 10.0, 15.0);
4722
4723 size_t spaceRun = 1;
4724 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 2); // From the text1 ("字典" - excluding the last space)
4725
4726 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4727
4728 REPORTER_ASSERT(reporter, impl->runs().size() == 6 + spaceRun);
4729 if (impl->runs().size() == 6 + spaceRun) {
4730 // Font resolution in Skia produces 6 runs because 2 parts of "Roboto 字典 " have different
4731 // script (Minikin merges the first 2 into one because of unresolved)
4732 // [Apple + Unresolved + ' '] 0, 1, 2
4733 // [Apple + Noto] 3, 4
4734 // [Apple + Han] 5, 6
4735 auto robotoAdvance = impl->runs()[0].advance().fX +
4736 impl->runs()[1].advance().fX;
4737 robotoAdvance += impl->runs()[2].advance().fX;
4738
4739 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(robotoAdvance, 64.199f, EPSILON50));
4740 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[2 + spaceRun].advance().fX, 139.125f, EPSILON100));
4741 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[3 + spaceRun].advance().fX, 27.999f, EPSILON100));
4742 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[4 + spaceRun].advance().fX, 62.248f, EPSILON100));
4743 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(impl->runs()[5 + spaceRun].advance().fX, 27.999f, EPSILON100));
4744
4745 // When a different font is resolved, then the metrics are different.
4746 REPORTER_ASSERT(reporter, impl->runs()[3 + spaceRun].correctAscent() != impl->runs()[5 + spaceRun].correctAscent());
4747 REPORTER_ASSERT(reporter, impl->runs()[3 + spaceRun].correctDescent() != impl->runs()[5 + spaceRun].correctDescent());
4748 }
4749 }
4750
4751 // Checked: NO DIFF
4752 UNIX_ONLY_TEST(SkParagraph_StrutParagraph1, reporter) {
4753 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4754 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4755 TestCanvas canvas("SkParagraph_StrutParagraph1.png");
4756 // The chinese extra height should be absorbed by the strut.
4757 const char* text = "01234満毎冠p来É本可\nabcd\n満毎É行p昼本可";
4758 const size_t len = strlen(text);
4759
4760 ParagraphStyle paragraph_style;
4761 paragraph_style.setMaxLines(10);
4762 paragraph_style.setTextAlign(TextAlign::kLeft);
4763 paragraph_style.turnHintingOff();
4764
4765 StrutStyle strut_style;
4766 strut_style.setStrutEnabled(true);
4767 strut_style.setFontFamilies({SkString("BlahFake"), SkString("Ahem")});
4768 strut_style.setFontSize(50);
4769 strut_style.setHeight(1.8f);
4770 strut_style.setHeightOverride(true);
4771 strut_style.setLeading(0.1f);
4772 paragraph_style.setStrutStyle(strut_style);
4773
4774 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4775
4776 TextStyle text_style;
4777 text_style.setFontFamilies({SkString("Ahem")});
4778 text_style.setFontSize(50);
4779 text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant));
4780 text_style.setColor(SK_ColorBLACK);
4781 text_style.setHeight(0.5f);
4782 builder.pushStyle(text_style);
4783 builder.addText(text, len);
4784 builder.pop();
4785
4786 auto paragraph = builder.Build();
4787 paragraph->layout(550);
4788 paragraph->paint(canvas.get(), 0, 0);
4789
4790 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4791 REPORTER_ASSERT(reporter, impl->lines().size() == 4);
4792
4793 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
4794 RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
4795 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
4796 {
4797 auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
4798 REPORTER_ASSERT(reporter, boxes.empty());
4799 }
4800 {
4801 auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
4802 canvas.drawRects(SK_ColorRED, boxes);
4803 REPORTER_ASSERT(reporter, boxes.size() == 1);
4804 if (boxes.size() >= 1) {
4805 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
4806 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100));
4807 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100));
4808 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100));
4809 }
4810 }
4811 {
4812 auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
4813 canvas.drawRects(SK_ColorRED, boxes);
4814 REPORTER_ASSERT(reporter, boxes.size() == 1);
4815 if (boxes.size() >= 1) {
4816 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
4817 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
4818 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100));
4819 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100));
4820 }
4821 }
4822 {
4823 auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
4824 canvas.drawRects(SK_ColorRED, boxes);
4825 REPORTER_ASSERT(reporter, boxes.size() == 1);
4826 if (boxes.size() >= 1) {
4827 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100));
4828 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 34.5f, EPSILON100));
4829 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100));
4830 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 84.5f, EPSILON100));
4831 }
4832 }
4833 {
4834 auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
4835 canvas.drawRects(SK_ColorRED, boxes);
4836 REPORTER_ASSERT(reporter, boxes.size() == 1);
4837 if (boxes.size() >= 1) {
4838 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100));
4839 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
4840 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100));
4841 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 95, EPSILON100));
4842 }
4843 }
4844 {
4845 auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
4846 canvas.drawRects(SK_ColorRED, boxes);
4847 REPORTER_ASSERT(reporter, boxes.size() == 1);
4848 if (boxes.size() >= 1) {
4849 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
4850 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 190, EPSILON100));
4851 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100));
4852 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 285, EPSILON100));
4853 }
4854 }
4855 {
4856 auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
4857 canvas.drawRects(SK_ColorRED, boxes);
4858 REPORTER_ASSERT(reporter, boxes.size() == 1);
4859 if (boxes.size() >= 1) {
4860 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100));
4861 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 285, EPSILON100));
4862 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100));
4863 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 380, EPSILON100));
4864 }
4865 }
4866 }
4867
4868 // Checked: NO DIFF
4869 UNIX_ONLY_TEST(SkParagraph_StrutParagraph2, reporter) {
4870 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4871 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4872 TestCanvas canvas("SkParagraph_StrutParagraph2.png");
4873 // The chinese extra height should be absorbed by the strut.
4874 const char* text = "01234ABCDEFGH\nabcd\nABCDEFGH";
4875 const size_t len = strlen(text);
4876
4877 ParagraphStyle paragraph_style;
4878 paragraph_style.setMaxLines(10);
4879 paragraph_style.setTextAlign(TextAlign::kLeft);
4880 paragraph_style.turnHintingOff();
4881
4882 StrutStyle strut_style;
4883
4884 strut_style.setStrutEnabled(true);
4885 strut_style.setFontFamilies({SkString("Ahem")});
4886 strut_style.setFontSize(50);
4887 strut_style.setHeight(1.6f);
4888 strut_style.setHeightOverride(true);
4889 paragraph_style.setStrutStyle(strut_style);
4890
4891 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
4892
4893 TextStyle text_style;
4894 text_style.setFontFamilies({SkString("Ahem")});
4895 text_style.setFontSize(50);
4896 text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
4897 SkFontStyle::kUpright_Slant));
4898 text_style.setColor(SK_ColorBLACK);
4899 text_style.setHeight(1);
4900 builder.pushStyle(text_style);
4901 builder.addText(text, len);
4902 builder.pop();
4903
4904 auto paragraph = builder.Build();
4905 paragraph->layout(550);
4906 paragraph->paint(canvas.get(), 0, 0);
4907
4908 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
4909 // Font is not resolved and the first line does not fit
4910 REPORTER_ASSERT(reporter, impl->lines().size() == 4);
4911
4912 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
4913 RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
4914 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
4915 {
4916 auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
4917 REPORTER_ASSERT(reporter, boxes.empty());
4918 }
4919 {
4920 auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
4921 canvas.drawRects(SK_ColorRED, boxes);
4922 REPORTER_ASSERT(reporter, boxes.size() == 1);
4923 if (boxes.size() >= 1) {
4924 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
4925 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100));
4926 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100));
4927 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100));
4928 }
4929 }
4930 {
4931 auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
4932 canvas.drawRects(SK_ColorRED, boxes);
4933 REPORTER_ASSERT(reporter, boxes.size() == 1);
4934 if (boxes.size() >= 1) {
4935 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
4936 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
4937 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, EPSILON100));
4938 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100));
4939 }
4940 }
4941 {
4942 auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
4943 canvas.drawRects(SK_ColorRED, boxes);
4944 REPORTER_ASSERT(reporter, boxes.size() == 1);
4945 if (boxes.size() >= 1) {
4946 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100));
4947 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 24, EPSILON100));
4948 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100));
4949 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 74, EPSILON100));
4950 }
4951 }
4952 {
4953 auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
4954 canvas.drawRects(SK_ColorRED, boxes);
4955 REPORTER_ASSERT(reporter, boxes.size() == 1);
4956 if (boxes.size() >= 1) {
4957 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, EPSILON100));
4958 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, EPSILON100));
4959 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, EPSILON100));
4960 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 80, EPSILON100));
4961 }
4962 }
4963 {
4964 auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
4965 canvas.drawRects(SK_ColorRED, boxes);
4966 REPORTER_ASSERT(reporter, boxes.size() == 1);
4967 if (boxes.size() >= 1) {
4968 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
4969 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 160, EPSILON100));
4970 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, EPSILON100));
4971 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, EPSILON100));
4972 }
4973 }
4974 {
4975 auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
4976 canvas.drawRects(SK_ColorRED, boxes);
4977 REPORTER_ASSERT(reporter, boxes.size() == 1);
4978 if (boxes.size() >= 1) {
4979 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, EPSILON100));
4980 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 240, EPSILON100));
4981 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, EPSILON100));
4982 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 320, EPSILON100));
4983 }
4984 }
4985 }
4986
4987 // Checked: NO DIFF
4988 UNIX_ONLY_TEST(SkParagraph_StrutParagraph3, reporter) {
4989 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
4990 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
4991 TestCanvas canvas("SkParagraph_StrutParagraph3.png");
4992
4993 // The chinese extra height should be absorbed by the strut.
4994 const char* text = "01234満毎p行来昼本可\nabcd\n満毎冠行来昼本可";
4995 const size_t len = strlen(text);
4996
4997 ParagraphStyle paragraph_style;
4998 paragraph_style.setMaxLines(10);
4999 paragraph_style.setTextAlign(TextAlign::kLeft);
5000 paragraph_style.turnHintingOff();
5001
5002 StrutStyle strut_style;
5003 strut_style.setStrutEnabled(true);
5004 strut_style.setFontFamilies({SkString("Ahem")});
5005 strut_style.setFontSize(50);
5006 strut_style.setHeight(1.2f);
5007 strut_style.setHeightOverride(true);
5008 paragraph_style.setStrutStyle(strut_style);
5009
5010 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5011
5012 TextStyle text_style;
5013 text_style.setFontFamilies({SkString("Ahem")});
5014 text_style.setFontSize(50);
5015 text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
5016 SkFontStyle::kUpright_Slant));
5017 text_style.setColor(SK_ColorBLACK);
5018 text_style.setHeight(1);
5019 builder.pushStyle(text_style);
5020 builder.addText(text, len);
5021 builder.pop();
5022
5023 auto paragraph = builder.Build();
5024 paragraph->layout(550);
5025 paragraph->paint(canvas.get(), 0, 0);
5026
5027 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5028 // Font is not resolved and the first line does not fit
5029 REPORTER_ASSERT(reporter, impl->lines().size() == 4);
5030
5031 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
5032 RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
5033 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
5034 SkScalar epsilon = 0.001f;
5035 {
5036 auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
5037 REPORTER_ASSERT(reporter, boxes.empty());
5038 }
5039 {
5040 auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
5041 canvas.drawRects(SK_ColorRED, boxes);
5042 REPORTER_ASSERT(reporter, boxes.size() == 1);
5043 if (boxes.size() >= 1) {
5044 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
5045 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon));
5046 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
5047 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon));
5048 }
5049 }
5050 {
5051 auto boxes = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
5052 canvas.drawRects(SK_ColorRED, boxes);
5053 REPORTER_ASSERT(reporter, boxes.size() == 1);
5054 if (boxes.size() >= 1) {
5055 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
5056 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
5057 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 50, epsilon));
5058 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon));
5059 }
5060 }
5061 {
5062 auto boxes = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
5063 canvas.drawRects(SK_ColorRED, boxes);
5064 REPORTER_ASSERT(reporter, boxes.size() == 1);
5065 if (boxes.size() >= 1) {
5066 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
5067 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 8, epsilon));
5068 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
5069 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 58, epsilon));
5070 }
5071 }
5072 {
5073 auto boxes = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
5074 canvas.drawRects(SK_ColorRED, boxes);
5075 REPORTER_ASSERT(reporter, boxes.size() == 1);
5076 if (boxes.size() >= 1) {
5077 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 300, epsilon));
5078 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 0, epsilon));
5079 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 500, epsilon));
5080 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 60, epsilon));
5081 }
5082 }
5083 {
5084 auto boxes = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
5085 canvas.drawRects(SK_ColorRED, boxes);
5086 REPORTER_ASSERT(reporter, boxes.size() == 1);
5087 if (boxes.size() >= 1) {
5088 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, epsilon));
5089 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 120, epsilon));
5090 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 100, epsilon));
5091 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 180, epsilon));
5092 }
5093 }
5094 {
5095 auto boxes = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
5096 canvas.drawRects(SK_ColorRED, boxes);
5097 REPORTER_ASSERT(reporter, boxes.size() == 1);
5098 if (boxes.size() >= 1) {
5099 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 50, epsilon));
5100 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 180, epsilon));
5101 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 300, epsilon));
5102 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 240, epsilon));
5103 }
5104 }
5105 }
5106
5107 // Checked: NO DIFF
5108 UNIX_ONLY_TEST(SkParagraph_StrutForceParagraph, reporter) {
5109 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5110 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5111 TestCanvas canvas("SkParagraph_StrutForceParagraph.png");
5112 const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
5113 const size_t len = strlen(text);
5114
5115 ParagraphStyle paragraph_style;
5116 paragraph_style.setMaxLines(10);
5117 paragraph_style.setTextAlign(TextAlign::kLeft);
5118 paragraph_style.turnHintingOff();
5119
5120 StrutStyle strut_style;
5121 strut_style.setStrutEnabled(true);
5122 strut_style.setFontFamilies({SkString("Ahem")});
5123 strut_style.setFontSize(50);
5124 strut_style.setHeight(1.5f);
5125 strut_style.setHeightOverride(true);
5126 strut_style.setLeading(0.1f);
5127 strut_style.setForceStrutHeight(true);
5128 paragraph_style.setStrutStyle(strut_style);
5129
5130 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5131
5132 TextStyle text_style;
5133 text_style.setFontFamilies({SkString("Ahem")});
5134 text_style.setFontSize(50);
5135 text_style.setLetterSpacing(0);
5136 text_style.setColor(SK_ColorBLACK);
5137 text_style.setHeight(1);
5138 builder.pushStyle(text_style);
5139 builder.addText(text, len);
5140 builder.pop();
5141
5142 auto paragraph = builder.Build();
5143 paragraph->layout(550);
5144 paragraph->paint(canvas.get(), 0, 0);
5145
5146 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5147 // Font is not resolved and the first line does not fit
5148 REPORTER_ASSERT(reporter, impl->lines().size() == 4);
5149
5150 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
5151 RectHeightStyle rect_height_max_style = RectHeightStyle::kMax;
5152 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
5153
5154 auto boxes1 = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
5155 REPORTER_ASSERT(reporter, boxes1.empty());
5156
5157 auto boxes2 = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
5158 canvas.drawRects(SK_ColorRED, boxes2);
5159 REPORTER_ASSERT(reporter, boxes2.size() == 1);
5160 if (boxes2.size() >= 1) {
5161 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.left(), 0, EPSILON100));
5162 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.top(), 22.5f, EPSILON100));
5163 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.right(), 50, EPSILON100));
5164 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes2[0].rect.bottom(), 72.5f, EPSILON100));
5165 }
5166
5167 auto boxes3 = paragraph->getRectsForRange(0, 1, rect_height_max_style, rect_width_style);
5168 canvas.drawRects(SK_ColorRED, boxes3);
5169 REPORTER_ASSERT(reporter, boxes3.size() == 1);
5170 if (boxes3.size() >= 1) {
5171 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.left(), 0, EPSILON100));
5172 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.top(), 0, EPSILON100));
5173 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.right(), 50, EPSILON100));
5174 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes3[0].rect.bottom(), 80, EPSILON100));
5175 }
5176
5177 auto boxes4 = paragraph->getRectsForRange(6, 10, rect_height_style, rect_width_style);
5178 canvas.drawRects(SK_ColorRED, boxes4);
5179 REPORTER_ASSERT(reporter, boxes4.size() == 1);
5180 if (boxes4.size() >= 1) {
5181 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.left(), 300, EPSILON100));
5182 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.top(), 22.5f, EPSILON100));
5183 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.right(), 500, EPSILON100));
5184 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes4[0].rect.bottom(), 72.5f, EPSILON100));
5185 }
5186
5187 auto boxes5 = paragraph->getRectsForRange(6, 10, rect_height_max_style, rect_width_style);
5188 canvas.drawRects(SK_ColorRED, boxes5);
5189 REPORTER_ASSERT(reporter, boxes5.size() == 1);
5190 if (boxes5.size() >= 1) {
5191 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.left(), 300, EPSILON100));
5192 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.top(), 0, EPSILON100));
5193 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.right(), 500, EPSILON100));
5194 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes5[0].rect.bottom(), 80, EPSILON100));
5195 }
5196
5197 auto boxes6 = paragraph->getRectsForRange(14, 16, rect_height_max_style, rect_width_style);
5198 canvas.drawRects(SK_ColorRED, boxes6);
5199 REPORTER_ASSERT(reporter, boxes6.size() == 1);
5200 if (boxes6.size() >= 1) {
5201 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.left(), 0, EPSILON100));
5202 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.top(), 160, EPSILON100));
5203 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.right(), 100, EPSILON100));
5204 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes6[0].rect.bottom(), 240, EPSILON100));
5205 }
5206
5207 auto boxes7 = paragraph->getRectsForRange(20, 25, rect_height_max_style, rect_width_style);
5208 canvas.drawRects(SK_ColorRED, boxes7);
5209 REPORTER_ASSERT(reporter, boxes7.size() == 1);
5210 if (boxes7.size() >= 1) {
5211 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.left(), 50, EPSILON100));
5212 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.top(), 240, EPSILON100));
5213 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.right(), 300, EPSILON100));
5214 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes7[0].rect.bottom(), 320, EPSILON100));
5215 }
5216 }
5217
5218 // Checked: NO DIFF
5219 UNIX_ONLY_TEST(SkParagraph_StrutDefaultParagraph, reporter) {
5220 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5221 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5222 TestCanvas canvas("SkParagraph_StrutDefaultParagraph.png");
5223
5224 const char* text = "01234満毎冠行来昼本可\nabcd\n満毎冠行来昼本可";
5225 const size_t len = strlen(text);
5226
5227 ParagraphStyle paragraph_style;
5228 paragraph_style.setMaxLines(10);
5229 paragraph_style.setTextAlign(TextAlign::kLeft);
5230 paragraph_style.turnHintingOff();
5231
5232 StrutStyle strut_style;
5233 strut_style.setStrutEnabled(true);
5234 strut_style.setFontFamilies({SkString("Ahem")});
5235 strut_style.setFontSize(50);
5236 strut_style.setHeight(1.5f);
5237 strut_style.setLeading(0.1f);
5238 strut_style.setForceStrutHeight(false);
5239 paragraph_style.setStrutStyle(strut_style);
5240
5241 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5242
5243 TextStyle text_style;
5244 text_style.setFontFamilies({SkString("Ahem")});
5245 text_style.setFontSize(20);
5246 text_style.setColor(SK_ColorBLACK);
5247 builder.pushStyle(text_style);
5248 builder.addText(text, len);
5249 builder.pop();
5250
5251 auto paragraph = builder.Build();
5252 paragraph->layout(550);
5253 paragraph->paint(canvas.get(), 0, 0);
5254
5255 RectHeightStyle rect_height_style = RectHeightStyle::kTight;
5256 RectHeightStyle rect_height_strut_style = RectHeightStyle::kStrut;
5257 RectWidthStyle rect_width_style = RectWidthStyle::kTight;
5258 {
5259 auto boxes = paragraph->getRectsForRange(0, 0, rect_height_style, rect_width_style);
5260 REPORTER_ASSERT(reporter, boxes.empty());
5261 }
5262 {
5263 auto boxes = paragraph->getRectsForRange(0, 1, rect_height_style, rect_width_style);
5264 canvas.drawRects(SK_ColorRED, boxes);
5265 REPORTER_ASSERT(reporter, boxes.size() == 1);
5266 if (boxes.size() >= 1) {
5267 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
5268 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 26.5f, EPSILON100));
5269 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 20, EPSILON100));
5270 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 46.5f, EPSILON100));
5271 }
5272 }
5273 {
5274 auto boxes = paragraph->getRectsForRange(0, 2, rect_height_strut_style, rect_width_style);
5275 canvas.drawRects(SK_ColorRED, boxes);
5276 REPORTER_ASSERT(reporter, boxes.size() == 1);
5277 if (boxes.size() >= 1) {
5278 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.left(), 0, EPSILON100));
5279 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.top(), 2.5f, EPSILON100));
5280 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.right(), 40, EPSILON100));
5281 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.bottom(), 52.5f, EPSILON100));
5282 }
5283 }
5284 }
5285
5286 UNIX_ONLY_TEST(SkParagraph_FontFeaturesParagraph, reporter) {
5287 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5288 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5289
5290 TestCanvas canvas("SkParagraph_FontFeaturesParagraph.png");
5291
5292 const char* text = "12ab\n";
5293
5294 ParagraphStyle paragraph_style;
5295 paragraph_style.turnHintingOff();
5296 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5297
5298 TextStyle text_style;
5299 text_style.setFontStyle(SkFontStyle::Italic()); // Regular Roboto doesn't have font features
5300 text_style.setFontFamilies({SkString("Roboto")});
5301 text_style.setColor(SK_ColorBLACK);
5302
5303 text_style.addFontFeature(SkString("tnum"), 1);
5304 builder.pushStyle(text_style);
5305 builder.addText(text);
5306
5307 text_style.resetFontFeatures();
5308 text_style.addFontFeature(SkString("tnum"), 0);
5309 text_style.addFontFeature(SkString("pnum"), 1);
5310 builder.pushStyle(text_style);
5311 builder.addText(text);
5312
5313 builder.pop();
5314 builder.pop();
5315
5316 auto paragraph = builder.Build();
5317 paragraph->layout(TestCanvasWidth);
5318
5319 paragraph->paint(canvas.get(), 10.0, 15.0);
5320
5321 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5322 REPORTER_ASSERT(reporter, paragraph->lineNumber() == 3ull);
5323 if (paragraph->lineNumber() >= 3) {
5324 auto& tnum_line = impl->lines()[0];
5325 auto& pnum_line = impl->lines()[1];
5326
5327 REPORTER_ASSERT(reporter, tnum_line.clusters().width() == 4ull);
5328 REPORTER_ASSERT(reporter, pnum_line.clusters().width() == 4ull);
5329 // Tabular numbers should have equal widths.
5330 REPORTER_ASSERT(reporter, impl->clusters()[0].width() == impl->clusters()[1].width());
5331 // Proportional numbers should have variable widths.
5332 REPORTER_ASSERT(reporter, impl->clusters()[5].width() != impl->clusters()[6].width());
5333 // Alphabetic characters should be unaffected.
5334 REPORTER_ASSERT(reporter, impl->clusters()[2].width() == impl->clusters()[7].width());
5335 }
5336 }
5337
5338 // Not in Minikin
5339 UNIX_ONLY_TEST(SkParagraph_WhitespacesInMultipleFonts, reporter) {
5340 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5341 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5342 const char* text = "English English 字典 字典 😀😃😄 😀😃😄";
5343 const size_t len = strlen(text);
5344
5345 ParagraphStyle paragraph_style;
5346 paragraph_style.turnHintingOff();
5347 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5348
5349 TextStyle text_style;
5350 text_style.setFontFamilies(
5351 {SkString("Roboto"), SkString("Noto Color Emoji"), SkString("Source Han Serif CN")});
5352 text_style.setFontSize(60);
5353 builder.pushStyle(text_style);
5354 builder.addText(text, len);
5355 builder.pop();
5356
5357 auto paragraph = builder.Build();
5358 paragraph->layout(TestCanvasWidth);
5359
5360 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
5361
5362 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5363 for (size_t i = 0; i < impl->runs().size() - 1; ++i) {
5364 auto first = impl->runs()[i].textRange();
5365 auto next = impl->runs()[i + 1].textRange();
5366 REPORTER_ASSERT(reporter, first.end == next.start);
5367 }
5368 }
5369
5370 // Disable until I sort out fonts
5371 DEF_TEST_DISABLED(SkParagraph_JSON1, reporter) {
5372 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5373 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5374 const char* text = "👨‍👩‍👧‍👦";
5375 const size_t len = strlen(text);
5376
5377 ParagraphStyle paragraph_style;
5378 paragraph_style.turnHintingOff();
5379 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5380
5381 TextStyle text_style;
5382 text_style.setFontFamilies({SkString("Noto Color Emoji")});
5383 builder.pushStyle(text_style);
5384 builder.addText(text, len);
5385 builder.pop();
5386
5387 auto paragraph = builder.Build();
5388 paragraph->layout(TestCanvasWidth);
5389
5390 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5391 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
5392 auto& run = impl->runs().front();
5393
5394 auto cluster = 0;
5395 SkShaperJSONWriter::VisualizeClusters(
5396 text, 0, std::strlen(text), run.glyphs(), run.clusterIndexes(),
5397 [&](int codePointCount, SkSpan<const char> utf1to1, SkSpan<const SkGlyphID> glyph1to1) {
5398 if (cluster == 0) {
5399 std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
5400 SkASSERT(std::strcmp(text, utf1to1.data()) == 0);
5401 SkASSERT(glyph1to1.size() == 1);
5402 SkASSERT(*glyph1to1.begin() == 1611);
5403 }
5404 ++cluster;
5405 });
5406 REPORTER_ASSERT(reporter, cluster <= 2);
5407 }
5408
5409 // Disable until I sort out fonts
5410 DEF_TEST_DISABLED(SkParagraph_JSON2, reporter) {
5411 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5412 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5413 const char* text = "p〠q";
5414 const size_t len = strlen(text);
5415
5416 ParagraphStyle paragraph_style;
5417 paragraph_style.turnHintingOff();
5418 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5419
5420 TextStyle text_style;
5421 text_style.setFontFamilies({SkString("Noto Sans CJK JP")});
5422 text_style.setColor(SK_ColorBLACK);
5423 text_style.setFontSize(50);
5424 builder.pushStyle(text_style);
5425 builder.addText(text, len);
5426 builder.pop();
5427
5428 auto paragraph = builder.Build();
5429 paragraph->layout(TestCanvasWidth);
5430
5431 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5432 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
5433
5434 auto cluster = 0;
5435 for (auto& run : impl->runs()) {
5436 SkShaperJSONWriter::VisualizeClusters(
5437 impl->text().data() + run.textRange().start, 0, run.textRange().width(),
5438 run.glyphs(), run.clusterIndexes(),
5439 [&](int codePointCount, SkSpan<const char> utf1to1,
5440 SkSpan<const SkGlyphID> glyph1to1) {
5441 if (cluster == 0) {
5442 std::string toCheckUtf8{utf1to1.data(), utf1to1.size()};
5443 SkASSERT(std::strcmp(text, utf1to1.data()) == 0);
5444 SkASSERT(glyph1to1.size() == 3);
5445 }
5446 ++cluster;
5447 });
5448 }
5449
5450 REPORTER_ASSERT(reporter, cluster <= 2);
5451 }
5452
5453 UNIX_ONLY_TEST(SkParagraph_CacheText, reporter) {
5454 ParagraphCache cache;
5455 cache.turnOn(true);
5456 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5457 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5458
5459 ParagraphStyle paragraph_style;
5460 paragraph_style.turnHintingOff();
5461
5462 TextStyle text_style;
5463 text_style.setFontFamilies({SkString("Roboto")});
5464 text_style.setColor(SK_ColorBLACK);
5465
5466 auto test = [&](const char* text, int count, bool expectedToBeFound) {
5467 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5468 builder.pushStyle(text_style);
5469 builder.addText(text, strlen(text));
5470 builder.pop();
5471 auto paragraph = builder.Build();
5472
5473 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5474 REPORTER_ASSERT(reporter, count == cache.count());
5475 auto found = cache.findParagraph(impl);
5476 REPORTER_ASSERT(reporter, found == expectedToBeFound);
5477 auto added = cache.updateParagraph(impl);
5478 REPORTER_ASSERT(reporter, added != expectedToBeFound);
5479 };
5480
5481 test("text1", 0, false);
5482 test("text1", 1, true);
5483 test("text2", 1, false);
5484 test("text2", 2, true);
5485 test("text3", 2, false);
5486 }
5487
5488 UNIX_ONLY_TEST(SkParagraph_CacheFonts, reporter) {
5489 ParagraphCache cache;
5490 cache.turnOn(true);
5491 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5492 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5493
5494 ParagraphStyle paragraph_style;
5495 paragraph_style.turnHintingOff();
5496
5497 TextStyle text_style;
5498 text_style.setColor(SK_ColorBLACK);
5499
5500 const char* text = "text";
5501 const size_t len = strlen(text);
5502
5503 auto test = [&](int count, bool expectedToBeFound) {
5504 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5505 builder.pushStyle(text_style);
5506 builder.addText(text, len);
5507 builder.pop();
5508 auto paragraph = builder.Build();
5509 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5510
5511 REPORTER_ASSERT(reporter, count == cache.count());
5512 auto found = cache.findParagraph(impl);
5513 REPORTER_ASSERT(reporter, found == expectedToBeFound);
5514 auto added = cache.updateParagraph(impl);
5515 REPORTER_ASSERT(reporter, added != expectedToBeFound);
5516 };
5517
5518 text_style.setFontFamilies({SkString("Roboto")});
5519 test(0, false);
5520 test(1, true);
5521 text_style.setFontFamilies({SkString("Homemade Apple")});
5522 test(1, false);
5523 test(2, true);
5524 text_style.setFontFamilies({SkString("Noto Color Emoji")});
5525 test(2, false);
5526 }
5527
5528 UNIX_ONLY_TEST(SkParagraph_CacheFontRanges, reporter) {
5529 ParagraphCache cache;
5530 cache.turnOn(true);
5531 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5532 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5533
5534 ParagraphStyle paragraph_style;
5535 paragraph_style.turnHintingOff();
5536
5537 TextStyle text_style;
5538 text_style.setFontFamilies({SkString("Roboto")});
5539 text_style.setColor(SK_ColorBLACK);
5540
5541 auto test = [&](const char* text1,
5542 const char* text2,
5543 const char* font1,
5544 const char* font2,
5545 int count,
5546 bool expectedToBeFound) {
5547 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5548 text_style.setFontFamilies({SkString(font1)});
5549 builder.pushStyle(text_style);
5550 builder.addText(text1, strlen(text1));
5551 builder.pop();
5552 text_style.setFontFamilies({SkString(font2)});
5553 builder.pushStyle(text_style);
5554 builder.addText(text2, strlen(text2));
5555 builder.pop();
5556 auto paragraph = builder.Build();
5557 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5558
5559 REPORTER_ASSERT(reporter, count == cache.count());
5560 auto found = cache.findParagraph(impl);
5561 REPORTER_ASSERT(reporter, found == expectedToBeFound);
5562 auto added = cache.updateParagraph(impl);
5563 REPORTER_ASSERT(reporter, added != expectedToBeFound);
5564 };
5565
5566 test("text", "", "Roboto", "Homemade Apple", 0, false);
5567 test("t", "ext", "Roboto", "Homemade Apple", 1, false);
5568 test("te", "xt", "Roboto", "Homemade Apple", 2, false);
5569 test("tex", "t", "Roboto", "Homemade Apple", 3, false);
5570 test("text", "", "Roboto", "Homemade Apple", 4, true);
5571 }
5572
5573 UNIX_ONLY_TEST(SkParagraph_CacheStyles, reporter) {
5574 ParagraphCache cache;
5575 cache.turnOn(true);
5576 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5577 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5578
5579 ParagraphStyle paragraph_style;
5580 paragraph_style.turnHintingOff();
5581
5582 TextStyle text_style;
5583 text_style.setFontFamilies({SkString("Roboto")});
5584 text_style.setColor(SK_ColorBLACK);
5585
5586 const char* text = "text";
5587 const size_t len = strlen(text);
5588
5589 auto test = [&](int count, bool expectedToBeFound) {
5590 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5591 builder.pushStyle(text_style);
5592 builder.addText(text, len);
5593 builder.pop();
5594 auto paragraph = builder.Build();
5595 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5596
5597 REPORTER_ASSERT(reporter, count == cache.count());
5598 auto found = cache.findParagraph(impl);
5599 REPORTER_ASSERT(reporter, found == expectedToBeFound);
5600 auto added = cache.updateParagraph(impl);
5601 REPORTER_ASSERT(reporter, added != expectedToBeFound);
5602 };
5603
5604 test(0, false);
5605 test(1, true);
5606 text_style.setLetterSpacing(10);
5607 test(1, false);
5608 test(2, true);
5609 text_style.setWordSpacing(10);
5610 test(2, false);
5611 }
5612
5613 UNIX_ONLY_TEST(SkParagraph_ParagraphWithLineBreak, reporter) {
5614 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5615 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5616 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
5617 fontCollection->enableFontFallback();
5618
5619 TestCanvas canvas("SkParagraph_ParagraphWithLineBreak.png");
5620
5621 ParagraphStyle paragraph_style;
5622 TextStyle text_style;
5623 text_style.setFontSize(16);
5624 text_style.setFontFamilies({SkString("Roboto")});
5625 text_style.setColor(SK_ColorBLACK);
5626 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5627 builder.addText("abc\n\ndef");
5628 text_style.setColor(SK_ColorBLACK);
5629
5630 auto paragraph = builder.Build();
5631 paragraph->layout(TestCanvasWidth);
5632 paragraph->paint(canvas.get(), 0, 0);
5633
5634 // Select a position at the second (empty) line
5635 auto pos = paragraph->getGlyphPositionAtCoordinate(0, 21);
5636 REPORTER_ASSERT(reporter, pos.affinity == Affinity::kDownstream && pos.position == 4);
5637 auto rect = paragraph->getRectsForRange(4, 5, RectHeightStyle::kTight, RectWidthStyle::kTight);
5638 REPORTER_ASSERT(reporter, rect.size() == 1 && rect[0].rect.width() == 0);
5639 }
5640
5641 // This test does not produce an image
5642 UNIX_ONLY_TEST(SkParagraph_NullInMiddleOfText, reporter) {
5643 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5644 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5645 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
5646
5647 const SkString text("null terminator ->\u0000<- on purpose did you see it?");
5648
5649 ParagraphStyle paragraph_style;
5650 TextStyle text_style;
5651 text_style.setColor(SK_ColorBLACK);
5652 text_style.setFontSize(16);
5653 text_style.setFontFamilies({SkString("Roboto")});
5654 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5655 builder.addText(text.c_str(), text.size());
5656
5657 auto paragraph = builder.Build();
5658 paragraph->layout(TestCanvasWidth);
5659 REPORTER_ASSERT(reporter, paragraph->getHeight() > 0);
5660 }
5661
5662 // This test does not produce an image
5663 UNIX_ONLY_TEST(SkParagraph_PlaceholderOnly, reporter) {
5664 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5665 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5666
5667 ParagraphStyle paragraph_style;
5668 TextStyle text_style;
5669 text_style.setFontFamilies({SkString("Roboto")});
5670 text_style.setBackgroundColor(SkPaint(SkColors::kRed));
5671 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5672
5673 PlaceholderStyle placeholder(0, 0, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0);
5674 builder.addPlaceholder(placeholder);
5675
5676 auto paragraph = builder.Build();
5677 paragraph->layout(TestCanvasWidth);
5678 auto result = paragraph->getRectsForPlaceholders();
5679 REPORTER_ASSERT(reporter, result.size() == 1);
5680 }
5681
5682 UNIX_ONLY_TEST(SkParagraph_Fallbacks, reporter) {
5683 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5684 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5685 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr(), "Arial");
5686 fontCollection->enableFontFallback();
5687 TestCanvas canvas("SkParagraph_Fallbacks.png");
5688
5689 const char* multiScript = "A1!aÀàĀāƁƀḂⱠꜲꬰəͲἀἏЀЖԠꙐꙮՁخ‎ࡔࠇܦআਉઐଘஇఘಧൺඣᭆᯔᮯ᳇ꠈᜅᩌꪈ༇ꥄꡙꫤ᧰៘꧁꧂ᜰᨏᯤᢆᣭᗗꗃⵞ𐒎߷ጩꬤ𖠺‡₩℻Ⅷ↹⋇⏳ⓖ╋▒◛⚧⑆שׁ🅕㊼龜ポ䷤🂡\n";
5690 const size_t len = strlen(multiScript);
5691
5692 const char* androidFonts[] = {
5693 "sans-serif",
5694 "sans-serif-condensed",
5695 "serif",
5696 "monospace",
5697 "serif-monospace",
5698 "casual",
5699 "cursive",
5700 "sans-serif-smallcaps",
5701 };
5702
5703 for (auto& font : androidFonts) {
5704
5705 ParagraphStyle paragraph_style;
5706 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5707
5708 TextStyle text_style;
5709 text_style.setColor(SK_ColorBLACK);
5710 text_style.setLocale(SkString("en_US"));
5711 text_style.setFontSize(20);
5712
5713 text_style.setFontFamilies({ SkString(font) });
5714 builder.pushStyle(text_style);
5715 builder.addText(multiScript, len);
5716
5717 builder.pop();
5718
5719 auto paragraph = builder.Build();
5720 paragraph->layout(TestCanvasWidth);
5721 paragraph->paint(canvas.get(), 0, 0);
5722 canvas.get()->translate(0, paragraph->getHeight() + 10);
5723 }
5724 }
5725
5726 UNIX_ONLY_TEST(SkParagraph_Bidi1, reporter) {
5727 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5728 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5729 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
5730 fontCollection->enableFontFallback();
5731 TestCanvas canvas("SkParagraph_Bidi1.png");
5732
5733 std::u16string abc = u"\u202Dabc";
5734 std::u16string DEF = u"\u202EDEF";
5735 std::u16string ghi = u"\u202Dghi";
5736 std::u16string JKL = u"\u202EJKL";
5737 std::u16string mno = u"\u202Dmno";
5738
5739 std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno";
5740
5741 ParagraphStyle paragraph_style;
5742 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5743
5744 TextStyle text_style;
5745 text_style.setFontFamilies({ SkString("sans-serif")});
5746 text_style.setFontSize(40);
5747
5748 text_style.setColor(SK_ColorCYAN);
5749 text_style.setFontStyle(SkFontStyle(SkFontStyle::kThin_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant));
5750 builder.pushStyle(text_style);
5751 builder.addText(abc);
5752
5753 text_style.setColor(SK_ColorGREEN);
5754 text_style.setFontStyle(SkFontStyle(SkFontStyle::kLight_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant));
5755 builder.pushStyle(text_style);
5756 builder.addText(DEF);
5757
5758 text_style.setColor(SK_ColorYELLOW);
5759 text_style.setFontStyle(SkFontStyle(SkFontStyle::kNormal_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant));
5760 builder.pushStyle(text_style);
5761 builder.addText(ghi);
5762
5763 text_style.setColor(SK_ColorMAGENTA);
5764 text_style.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant));
5765 builder.pushStyle(text_style);
5766 builder.addText(JKL);
5767
5768 text_style.setColor(SK_ColorBLUE);
5769 text_style.setFontStyle(SkFontStyle(SkFontStyle::kBlack_Weight, SkFontStyle::kNormal_Width, SkFontStyle::kUpright_Slant));
5770 builder.pushStyle(text_style);
5771 builder.addText(mno);
5772
5773 auto paragraph = builder.Build();
5774 paragraph->layout(400);
5775 paragraph->paint(canvas.get(), 0, 0);
5776 }
5777
5778 UNIX_ONLY_TEST(SkParagraph_Bidi2, reporter) {
5779 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5780 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5781 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
5782 fontCollection->enableFontFallback();
5783 TestCanvas canvas("SkParagraph_Bidi2.png");
5784
5785 std::u16string abcD = u"\u202Dabc\u202ED";
5786 std::u16string EFgh = u"EF\u202Dgh";
5787 std::u16string iJKLmno = u"i\u202EJKL\u202Dmno";
5788
5789 std::u16string abcDEFghiJKLmno = u"\u202Dabc\u202EDEF\u202Dghi\u202EJKL\u202Dmno";
5790
5791 ParagraphStyle paragraph_style;
5792 TextStyle text_style;
5793 text_style.setFontFamilies({ SkString("sans-serif")});
5794 text_style.setFontSize(40);
5795 text_style.setColor(SK_ColorBLACK);
5796 {
5797 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5798 builder.pushStyle(text_style);
5799 builder.addText(abcD);
5800 builder.pushStyle(text_style);
5801 builder.addText(EFgh);
5802 builder.pushStyle(text_style);
5803 builder.addText(iJKLmno);
5804 auto paragraph = builder.Build();
5805 paragraph->layout(360);
5806 paragraph->paint(canvas.get(), 0, 0);
5807 }
5808 canvas.get()->translate(0, 400);
5809 {
5810 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5811 builder.pushStyle(text_style);
5812 builder.addText(abcDEFghiJKLmno);
5813 auto paragraph = builder.Build();
5814 paragraph->layout(360);
5815 paragraph->paint(canvas.get(), 0, 0);
5816 }
5817 }
5818
5819 // This test does not produce an image
5820 UNIX_ONLY_TEST(SkParagraph_NewlineOnly, reporter) {
5821 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5822 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5823 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
5824
5825 TextStyle text_style;
5826 text_style.setFontFamilies({SkString("Ahem")});
5827 text_style.setColor(SK_ColorBLACK);
5828 StrutStyle strut_style;
5829 strut_style.setStrutEnabled(false);
5830 ParagraphStyle paragraph_style;
5831 paragraph_style.setStrutStyle(strut_style);
5832 paragraph_style.setTextStyle(text_style);
5833 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5834 builder.addText("\n");
5835 auto paragraph = builder.Build();
5836 paragraph->layout(1000);
5837 REPORTER_ASSERT(reporter, paragraph->getHeight() == 28);
5838 }
5839
5840 UNIX_ONLY_TEST(SkParagraph_FontResolutions, reporter) {
5841 TestCanvas canvas("SkParagraph_FontResolutions.png");
5842
5843 sk_sp<TestFontCollection> fontCollection =
5844 sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false);
5845 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5846
5847 if (!fontCollection->addFontFromFile("abc/abc.ttf", "abc")) {
5848 ERRORF(reporter, "abc/abc.ttf not found");
5849 return;
5850 }
5851 if (!fontCollection->addFontFromFile("abc/abc+grave.ttf", "abc+grave")) {
5852 ERRORF(reporter, "abc/abc+grave.ttf not found");
5853 return;
5854 }
5855 if (!fontCollection->addFontFromFile("abc/abc+agrave.ttf", "abc+agrave")) {
5856 ERRORF(reporter, "abc/abc+agrave.ttf not found");
5857 return;
5858 }
5859
5860 TextStyle text_style;
5861 text_style.setFontFamilies({SkString("abc")});
5862 text_style.setFontSize(50);
5863
5864 ParagraphStyle paragraph_style;
5865 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5866
5867 text_style.setFontFamilies({SkString("abc"), SkString("abc+grave")});
5868 text_style.setColor(SK_ColorBLUE);
5869 builder.pushStyle(text_style);
5870 builder.addText(u"a\u0300");
5871 text_style.setColor(SK_ColorMAGENTA);
5872 builder.pushStyle(text_style);
5873 builder.addText(u"à");
5874
5875 text_style.setFontFamilies({SkString("abc"), SkString("abc+agrave")});
5876
5877 text_style.setColor(SK_ColorRED);
5878 builder.pushStyle(text_style);
5879 builder.addText(u"a\u0300");
5880 text_style.setColor(SK_ColorGREEN);
5881 builder.pushStyle(text_style);
5882 builder.addText(u"à");
5883
5884 auto paragraph = builder.Build();
5885 paragraph->layout(TestCanvasWidth);
5886
5887 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
5888 REPORTER_ASSERT(reporter, impl->runs().size() == 2);
5889 if (impl->runs().size() >= 2) {
5890 REPORTER_ASSERT(reporter, impl->runs().front().size() == 4);
5891 if (impl->runs().front().size() >= 4) {
5892 REPORTER_ASSERT(reporter, impl->runs().front().glyphs()[0] == impl->runs().front().glyphs()[2]);
5893 REPORTER_ASSERT(reporter, impl->runs().front().glyphs()[1] == impl->runs().front().glyphs()[3]);
5894 }
5895
5896 REPORTER_ASSERT(reporter, impl->runs().back().size() == 2);
5897 if (impl->runs().back().size() >= 2) {
5898 REPORTER_ASSERT(reporter, impl->runs().back().glyphs()[0] == impl->runs().back().glyphs()[1]);
5899 }
5900 }
5901
5902 paragraph->paint(canvas.get(), 100, 100);
5903 }
5904
5905 UNIX_ONLY_TEST(SkParagraph_FontStyle, reporter) {
5906 TestCanvas canvas("SkParagraph_FontStyle.png");
5907
5908 sk_sp<TestFontCollection> fontCollection = sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false, true);
5909 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5910
5911 TextStyle text_style;
5912 text_style.setFontFamilies({SkString("Roboto")});
5913 text_style.setColor(SK_ColorBLACK);
5914 text_style.setFontSize(20);
5915 SkFontStyle fs = SkFontStyle(
5916 SkFontStyle::Weight::kLight_Weight,
5917 SkFontStyle::Width::kNormal_Width,
5918 SkFontStyle::Slant::kUpright_Slant
5919 );
5920 text_style.setFontStyle(fs);
5921 ParagraphStyle paragraph_style;
5922 paragraph_style.setTextStyle(text_style);
5923 TextStyle boldItalic;
5924 boldItalic.setFontFamilies({SkString("Roboto")});
5925 boldItalic.setColor(SK_ColorRED);
5926 SkFontStyle bi = SkFontStyle(
5927 SkFontStyle::Weight::kBold_Weight,
5928 SkFontStyle::Width::kNormal_Width,
5929 SkFontStyle::Slant::kItalic_Slant
5930 );
5931 boldItalic.setFontStyle(bi);
5932 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5933 builder.addText("Default text\n");
5934 builder.pushStyle(boldItalic);
5935 builder.addText("Bold and Italic\n");
5936 builder.pop();
5937 builder.addText("back to normal");
5938 auto paragraph = builder.Build();
5939 paragraph->layout(250);
5940 paragraph->paint(canvas.get(), 0, 0);
5941 }
5942
5943 UNIX_ONLY_TEST(SkParagraph_Shaping, reporter) {
5944 TestCanvas canvas("SkParagraph_Shaping.png");
5945
5946 sk_sp<TestFontCollection> fontCollection =
5947 sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), true);
5948 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5949
5950 TextStyle text_style;
5951 text_style.setFontFamilies({SkString("Roboto")});
5952 text_style.setColor(SK_ColorGRAY);
5953 text_style.setFontSize(14);
5954 SkFontStyle b = SkFontStyle(
5955 SkFontStyle::Weight::kNormal_Weight,
5956 SkFontStyle::Width::kNormal_Width,
5957 SkFontStyle::Slant::kUpright_Slant
5958 );
5959 text_style.setFontStyle(b);
5960 ParagraphStyle paragraph_style;
5961 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5962 builder.pushStyle(text_style);
5963 builder.addText("Eat0 apple0 pies0 | Eat1 apple1 pies1 | Eat2 apple2 pies2");
5964 auto paragraph = builder.Build();
5965 paragraph->layout(380);
5966 paragraph->paint(canvas.get(), 0, 0);
5967 }
5968
5969 UNIX_ONLY_TEST(SkParagraph_Ellipsis, reporter) {
5970 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
5971 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
5972 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
5973 TestCanvas canvas("SkParagraph_Ellipsis.png");
5974
5975 const char* text = "This\n"
5976 "is a wrapping test. It should wrap at manual newlines, and if softWrap is true, also at spaces.";
5977 TextStyle text_style;
5978 text_style.setFontFamilies({SkString("Ahem")});
5979 text_style.setColor(SK_ColorBLACK);
5980 text_style.setFontSize(10);
5981
5982 auto relayout = [&](size_t lines, bool ellipsis,
5983 SkScalar width, SkScalar height, SkScalar minWidth, SkScalar maxWidth, SkColor bg) {
5984 ParagraphStyle paragraph_style;
5985 SkPaint paint;
5986 paint.setColor(bg);
5987 text_style.setForegroundColor(paint);
5988 paragraph_style.setTextStyle(text_style);
5989 paragraph_style.setMaxLines(lines);
5990 if (ellipsis) {
5991 paragraph_style.setEllipsis(u"\u2026");
5992 }
5993 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
5994 builder.addText(text);
5995 auto paragraph = builder.Build();
5996 paragraph->layout(50);
5997 paragraph->paint(canvas.get(), 0, 0);
5998 canvas.get()->translate(50, paragraph->getHeight() + 10);
5999 auto result = paragraph->getRectsForRange(0, strlen(text), RectHeightStyle::kTight, RectWidthStyle::kTight);
6000 SkPaint background;
6001 background.setColor(SK_ColorRED);
6002 background.setStyle(SkPaint::kStroke_Style);
6003 background.setAntiAlias(true);
6004 background.setStrokeWidth(1);
6005 canvas.get()->drawRect(result.front().rect, background);
6006
6007 SkASSERT(width == paragraph->getMaxWidth());
6008 SkASSERT(height == paragraph->getHeight());
6009 SkASSERT(nearlyEqual(minWidth, paragraph->getMinIntrinsicWidth(), EPSILON100));
6010 SkASSERT(nearlyEqual(maxWidth, paragraph->getMaxIntrinsicWidth(), EPSILON100));
6011 };
6012
6013 SkPaint paint;
6014 paint.setColor(SK_ColorLTGRAY);
6015 canvas.get()->drawRect(SkRect::MakeXYWH(0, 0, 50, 500), paint);
6016
6017 relayout(1, false, 50, 10, 950, 950, SK_ColorRED);
6018 relayout(3, false, 50, 30, 90, 950, SK_ColorBLUE);
6019 relayout(std::numeric_limits<size_t>::max(), false, 50, 200, 90, 950, SK_ColorGREEN);
6020
6021 relayout(1, true, 50, 10, 950, 950, SK_ColorYELLOW);
6022 relayout(3, true, 50, 30, 90, 950, SK_ColorMAGENTA);
6023 relayout(std::numeric_limits<size_t>::max(), true, 50, 20, 950, 950, SK_ColorCYAN);
6024
6025 relayout(1, false, 50, 10, 950, 950, SK_ColorRED);
6026 relayout(3, false, 50, 30, 90, 950, SK_ColorBLUE);
6027 relayout(std::numeric_limits<size_t>::max(), false, 50, 200, 90, 950, SK_ColorGREEN);
6028 }
6029
6030 UNIX_ONLY_TEST(SkParagraph_MemoryLeak, reporter) {
6031 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6032 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6033 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
6034
6035 std::string text;
6036 for (size_t i = 0; i < 10; i++)
6037 {
6038 SkPaint paint;
6039 paint.setAntiAlias(true);
6040 paint.setColor(SK_ColorBLACK);
6041
6042 TextStyle textStyle;
6043 textStyle.setForegroundColor(paint);
6044 textStyle.setFontFamilies({ SkString("Roboto") });
6045
6046 ParagraphStyle paragraphStyle;
6047 paragraphStyle.setTextStyle(textStyle);
6048
6049 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
6050 text += "Text ";
6051 builder.addText(text.c_str());
6052
6053 auto paragraph = builder.Build();
6054 paragraph->layout(100);
6055
6056 //used to add a delay so I can monitor memory usage
6057 //std::this_thread::sleep_for(std::chrono::milliseconds(1000));
6058 }
6059 }
6060
6061 UNIX_ONLY_TEST(SkParagraph_FormattingInfinity, reporter) {
6062 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6063 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6064 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
6065 TestCanvas canvas("SkParagraph_FormattingInfinity.png");
6066
6067 const char* text = "Some text\nAnother line";
6068
6069 SkPaint paint;
6070 paint.setAntiAlias(true);
6071 paint.setColor(SK_ColorBLACK);
6072
6073 TextStyle textStyle;
6074 textStyle.setForegroundColor(paint);
6075 textStyle.setFontFamilies({ SkString("Roboto") });
6076 ParagraphStyle paragraphStyle;
6077 paragraphStyle.setTextStyle(textStyle);
6078
6079 auto draw = [&](const char* prefix, TextAlign textAlign, TextDirection textDirection) {
6080 paragraphStyle.setTextAlign(textAlign);
6081 paragraphStyle.setTextDirection(textDirection);
6082 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
6083 builder.addText(text);
6084 auto paragraph = builder.Build();
6085 paragraph->layout(SK_ScalarInfinity);
6086 paragraph->paint(canvas.get(), 0, 0);
6087 canvas.get()->translate(0, 100);
6088 };
6089
6090 draw("left", TextAlign::kLeft, TextDirection::kLtr);
6091 draw("right", TextAlign::kRight, TextDirection::kLtr);
6092 draw("center", TextAlign::kCenter, TextDirection::kLtr);
6093 draw("justify LTR", TextAlign::kJustify, TextDirection::kLtr);
6094 draw("justify RTL", TextAlign::kJustify, TextDirection::kRtl);
6095 }
6096
6097 UNIX_ONLY_TEST(SkParagraph_Infinity, reporter) {
6098 SkASSERT(nearlyEqual(1, SK_ScalarInfinity) == false);
6099 SkASSERT(nearlyEqual(1, SK_ScalarNegativeInfinity) == false);
6100 SkASSERT(nearlyEqual(1, SK_ScalarNaN) == false);
6101
6102 SkASSERT(nearlyEqual(SK_ScalarInfinity, SK_ScalarInfinity) == true);
6103 SkASSERT(nearlyEqual(SK_ScalarInfinity, SK_ScalarNegativeInfinity) == false);
6104 SkASSERT(nearlyEqual(SK_ScalarInfinity, SK_ScalarNaN) == false);
6105
6106 SkASSERT(nearlyEqual(SK_ScalarNegativeInfinity, SK_ScalarInfinity) == false);
6107 SkASSERT(nearlyEqual(SK_ScalarNegativeInfinity, SK_ScalarNegativeInfinity) == true);
6108 SkASSERT(nearlyEqual(SK_ScalarNegativeInfinity, SK_ScalarNaN) == false);
6109
6110 SkASSERT(nearlyEqual(SK_ScalarNaN, SK_ScalarInfinity) == false);
6111 SkASSERT(nearlyEqual(SK_ScalarNaN, SK_ScalarNegativeInfinity) == false);
6112 SkASSERT(nearlyEqual(SK_ScalarNaN, SK_ScalarNaN) == false);
6113 }
6114
6115 UNIX_ONLY_TEST(SkParagraph_LineMetrics, reporter) {
6116
6117 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6118 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6119
6120 TestCanvas canvas("SkParagraph_LineMetrics.png");
6121
6122 const char* text = "One line of text\n";
6123 const size_t len = strlen(text);
6124
6125 ParagraphStyle paragraph_style;
6126 paragraph_style.turnHintingOff();
6127 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6128
6129 TextStyle text_style;
6130 text_style.setFontFamilies({SkString("Roboto")});
6131 text_style.setColor(SK_ColorBLACK);
6132
6133 text_style.setFontSize(8);
6134 builder.pushStyle(text_style);
6135 builder.addText(text, len);
6136 builder.pop();
6137
6138 text_style.setFontSize(12);
6139 builder.pushStyle(text_style);
6140 builder.addText(text, len);
6141 builder.pop();
6142
6143 text_style.setFontSize(18);
6144 builder.pushStyle(text_style);
6145 builder.addText(text, len);
6146 builder.pop();
6147
6148 text_style.setFontSize(30);
6149 builder.pushStyle(text_style);
6150 builder.addText(text, len - 1); // Skip the last \n
6151 builder.pop();
6152
6153 auto paragraph = builder.Build();
6154 paragraph->layout(TestCanvasWidth);
6155
6156 std::vector<LineMetrics> metrics;
6157 paragraph->getLineMetrics(metrics);
6158
6159 SkDEBUGCODE(auto impl = static_cast<ParagraphImpl*>(paragraph.get());)
6160 SkASSERT(metrics.size() == impl->lines().size());
6161 for (size_t i = 0; i < metrics.size(); ++i) {
6162 SkDEBUGCODE(auto& line = impl->lines()[i];)
6163 SkDEBUGCODE(auto baseline = metrics[i].fBaseline;)
6164 SkDEBUGCODE(auto top = line.offset().fY;)
6165 SkDEBUGCODE(auto bottom = line.offset().fY + line.height();)
6166 SkASSERT( baseline > top && baseline <= bottom);
6167 }
6168
6169 paragraph->paint(canvas.get(), 0, 0);
6170 auto rects = paragraph->getRectsForRange(0, len * 4, RectHeightStyle::kMax, RectWidthStyle::kTight);
6171
6172 SkPaint red;
6173 red.setColor(SK_ColorRED);
6174 red.setStyle(SkPaint::kStroke_Style);
6175 red.setAntiAlias(true);
6176 red.setStrokeWidth(1);
6177
6178 for (auto& rect : rects) {
6179 canvas.get()->drawRect(rect.rect, red);
6180 }
6181
6182 SkPaint green;
6183 green.setColor(SK_ColorGREEN);
6184 green.setStyle(SkPaint::kStroke_Style);
6185 green.setAntiAlias(true);
6186 green.setStrokeWidth(1);
6187 for (auto& metric : metrics) {
6188 auto x0 = 0.0;
6189 auto x1 = metric.fWidth;
6190 auto y = metric.fBaseline;
6191 canvas.get()->drawLine(x0, y, x1, y, green);
6192 }
6193 }
6194
6195 DEF_TEST_DISABLED(SkParagraph_PlaceholderHeightInf, reporter) {
6196 TestCanvas canvas("SkParagraph_PlaceholderHeightInf.png");
6197
6198 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6199 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6200
6201 TextStyle text_style;
6202 text_style.setFontFamilies({SkString("Ahem")});
6203 text_style.setColor(SK_ColorBLACK);
6204 text_style.setFontSize(14);
6205
6206 PlaceholderStyle placeholder_style;
6207 placeholder_style.fWidth = 16.0f;
6208 placeholder_style.fHeight = SK_ScalarInfinity;
6209 placeholder_style.fAlignment = PlaceholderAlignment::kBottom;
6210 placeholder_style.fBaseline = TextBaseline::kAlphabetic;
6211 placeholder_style.fBaselineOffset = SK_ScalarInfinity;
6212
6213 ParagraphStyle paragraph_style;
6214 //paragraph_style.setDrawOptions(DrawOptions::kRecord);
6215 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6216 builder.pushStyle(text_style);
6217 builder.addText("Limited by budget");
6218 builder.addPlaceholder(placeholder_style);
6219 auto paragraph = builder.Build();
6220 paragraph->layout(SK_ScalarInfinity);
6221 paragraph->paint(canvas.get(), 0, 0);
6222
6223 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
6224 REPORTER_ASSERT(reporter, SkIsFinite(impl->getPicture()->cullRect().height()));
6225 REPORTER_ASSERT(reporter, SkIsFinite(impl->getPicture()->cullRect().width()));
6226 }
6227
6228 UNIX_ONLY_TEST(SkParagraph_LineMetricsTextAlign, reporter) {
6229
6230 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6231 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6232
6233 TestCanvas canvas("SkParagraph_LineMetricsTextAlign.png");
6234
6235 ParagraphStyle paragraph_style;
6236 paragraph_style.turnHintingOff();
6237 TextStyle text_style;
6238 text_style.setFontFamilies({SkString("Roboto")});
6239 text_style.setColor(SK_ColorBLACK);
6240
6241 auto layout = [&](TextAlign text_align) -> std::pair<SkScalar, SkScalar> {
6242 paragraph_style.setTextAlign(text_align);
6243 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6244 builder.pushStyle(text_style);
6245 builder.addText("Some text that takes more than 200 px");
6246 auto paragraph = builder.Build();
6247 paragraph->layout(200);
6248 paragraph->paint(canvas.get(), 0, 0);
6249 canvas.get()->translate(0, paragraph->getHeight());
6250 std::vector<LineMetrics> metrics;
6251 paragraph->getLineMetrics(metrics);
6252 REPORTER_ASSERT(reporter, metrics.size() > 0);
6253 return { metrics[0].fLeft, metrics[0].fWidth };
6254 };
6255
6256 SkScalar left[4];
6257 SkScalar width[4];
6258 std::tie(left[0], width[0]) = layout(TextAlign::kLeft);
6259 std::tie(left[1], width[1]) = layout(TextAlign::kCenter);
6260 std::tie(left[2], width[2]) = layout(TextAlign::kRight);
6261 std::tie(left[3], width[3]) = layout(TextAlign::kJustify);
6262
6263 // delta = line_width - text_width
6264 REPORTER_ASSERT(reporter, left[0] == 0); // Starts from 0
6265 REPORTER_ASSERT(reporter, left[1] > left[0]); // Starts from delta / 2
6266 REPORTER_ASSERT(reporter, left[2] > left[1]); // Starts from delta
6267 REPORTER_ASSERT(reporter, left[3] == left[0]); // Starts from 0
6268 REPORTER_ASSERT(reporter, width[1] == width[0]);
6269 REPORTER_ASSERT(reporter, width[2] == width[0]);
6270 REPORTER_ASSERT(reporter, width[3] > width[0]); // delta == 0
6271 }
6272
6273 UNIX_ONLY_TEST(SkParagraph_FontResolutionInRTL, reporter) {
6274 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
6275 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6276 TestCanvas canvas("SkParagraph_FontResolutionInRTL.png");
6277 const char* text = " אאא בּבּבּבּ אאאא בּבּ אאא בּבּבּ אאאאא בּבּבּבּ אאאא בּבּבּבּבּ ";
6278 const size_t len = strlen(text);
6279
6280 ParagraphStyle paragraph_style;
6281 paragraph_style.setMaxLines(14);
6282 paragraph_style.setTextAlign(TextAlign::kRight);
6283 paragraph_style.setTextDirection(TextDirection::kRtl);
6284 paragraph_style.turnHintingOff();
6285 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6286
6287 TextStyle text_style;
6288 text_style.setFontFamilies({SkString("Ahem")});
6289 text_style.setFontSize(26);
6290 text_style.setColor(SK_ColorBLACK);
6291 builder.pushStyle(text_style);
6292 builder.addText(text, len);
6293 builder.pop();
6294
6295 auto paragraph = builder.Build();
6296 paragraph->layout(TestCanvasWidth);
6297 paragraph->paint(canvas.get(), 0, 0);
6298
6299 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
6300 REPORTER_ASSERT(reporter, impl->runs().size() == (10 + 11));
6301 }
6302
6303 UNIX_ONLY_TEST(SkParagraph_FontResolutionInLTR, reporter) {
6304 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
6305 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6306 TestCanvas canvas("SkParagraph_FontResolutionInLTR.png");
6307 auto text = u"abc \u01A2 \u01A2 def";
6308
6309 ParagraphStyle paragraph_style;
6310 paragraph_style.setMaxLines(14);
6311 paragraph_style.turnHintingOff();
6312 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6313
6314 TextStyle text_style;
6315 text_style.setFontFamilies({SkString("Roboto")});
6316 text_style.setFontSize(26);
6317 text_style.setColor(SK_ColorBLACK);
6318 builder.pushStyle(text_style);
6319 builder.addText(text);
6320 builder.pop();
6321
6322 auto paragraph = builder.Build();
6323 paragraph->layout(TestCanvasWidth);
6324 paragraph->paint(canvas.get(), 0, 0);
6325
6326 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
6327 REPORTER_ASSERT(reporter, impl->runs().size() == 5);
6328 if (impl->runs().size() >= 5) {
6329 REPORTER_ASSERT(reporter, impl->runs()[0].textRange().width() == 4); // "abc "
6330 REPORTER_ASSERT(reporter, impl->runs()[1].textRange().width() == 2); // "{unresolved}"
6331 REPORTER_ASSERT(reporter, impl->runs()[2].textRange().width() == 1); // " "
6332 REPORTER_ASSERT(reporter, impl->runs()[3].textRange().width() == 2); // "{unresolved}"
6333 REPORTER_ASSERT(reporter, impl->runs()[4].textRange().width() == 4); // " def"
6334 }
6335 }
6336
6337 UNIX_ONLY_TEST(SkParagraph_Intrinsic, reporter) {
6338 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6339 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6340 SkString text(std::string(3000, 'a'));
6341
6342 ParagraphStyle paragraph_style;
6343 paragraph_style.turnHintingOff();
6344 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6345
6346 TextStyle text_style;
6347 text_style.setFontFamilies({SkString("Google Sans")});
6348 text_style.setFontSize(12.0f);
6349 text_style.setColor(SK_ColorBLACK);
6350 builder.pushStyle(text_style);
6351 builder.addText(text.c_str());
6352
6353 auto paragraph = builder.Build();
6354 paragraph->layout(300000.0f);
6355 REPORTER_ASSERT(reporter, paragraph->getMinIntrinsicWidth() <= paragraph->getMaxIntrinsicWidth());
6356 }
6357
6358 // This test does not produce an image
6359 UNIX_ONLY_TEST(SkParagraph_NoCache1, reporter) {
6360
6361 ParagraphCache cache;
6362 cache.turnOn(true);
6363
6364 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
6365 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6366 // Long arabic text with english spaces
6367 const char* text =
6368 "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
6369 "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
6370 "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
6371 "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
6372 "من أسر وإعلان الخاصّة وهولندا،, عل قائمة الضغوط بالمطالبة تلك. الصفحة "
6373 "عل بمباركة التقليدية قام عن. تصفح";
6374
6375 SkString str;
6376
6377 ParagraphStyle paragraph_style;
6378 paragraph_style.setTextDirection(TextDirection::kLtr);
6379 TextStyle text_style;
6380 text_style.setFontFamilies({SkString("Ahem")});
6381 text_style.setFontSize(14);
6382 text_style.setColor(SK_ColorBLACK);
6383
6384 auto test = [&](const char* test, const char* text, bool editing) {
6385 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6386 //SkDebugf("test %s:\n", test);
6387 builder.pushStyle(text_style);
6388 builder.addText(text);
6389 builder.pop();
6390
6391 auto cache = fontCollection->getParagraphCache();
6392 auto countBefore = cache->count();
6393 auto paragraph = builder.Build();
6394 paragraph->layout(TestCanvasWidth);
6395 auto countAfter = cache->count();
6396
6397 if (test == nullptr) {
6398 return;
6399 }
6400
6401 REPORTER_ASSERT(reporter, (countBefore == countAfter) == editing);
6402 };
6403
6404 str.append(text);
6405 test("Long arabic text", str.c_str(), false);
6406
6407 str.append("عل");
6408 test("+2 character at the end", str.c_str(), true);
6409
6410 str = SkString(text);
6411 test("-2 characters from the end", str.c_str(), true);
6412
6413 str.insert(0, "عل");
6414 test("+2 character at the start", str.c_str(), true);
6415
6416 test("-2 characters from the start", text, true);
6417
6418 // Make sure that different strings are not flagged as editing
6419 test("different strings", "0123456789 0123456789 0123456789 0123456789 0123456789", false);
6420 }
6421
6422 UNIX_ONLY_TEST(SkParagraph_HeightCalculations, reporter) {
6423 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6424 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6425
6426 TestCanvas canvas("SkParagraph_HeightCalculations.png");
6427
6428 auto draw = [&](TextHeightBehavior hb, const char* text, SkScalar height) {
6429 ParagraphStyle paragraph_style;
6430 paragraph_style.setTextHeightBehavior(hb);
6431
6432 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6433 TextStyle text_style;
6434 text_style.setFontFamilies({SkString("Roboto")});
6435 text_style.setFontSize(14.0f);
6436 text_style.setHeight(5.0f);
6437 text_style.setHeightOverride(true);
6438 text_style.setColor(SK_ColorBLACK);
6439 builder.pushStyle(text_style);
6440 builder.addText(text);
6441
6442 auto paragraph = builder.Build();
6443 paragraph->layout(500);
6444 paragraph->paint(canvas.get(), 0, 0);
6445 canvas.get()->translate(0, paragraph->getHeight());
6446 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(paragraph->getHeight(), height));
6447 };
6448
6449 draw(TextHeightBehavior::kAll, "Hello\nLine 2\nLine 3", 210);
6450 draw(TextHeightBehavior::kDisableAll, "Hello\nLine 2\nLine 3", 157);
6451 draw(TextHeightBehavior::kDisableFirstAscent, "Hello", 28);
6452 }
6453
6454 UNIX_ONLY_TEST(SkParagraph_RTL_With_Styles, reporter) {
6455
6456 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6457 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6458
6459 TestCanvas canvas("SkParagraph_RTL_With_Styles.png");
6460
6461 SkPaint whiteSpaces;
6462 whiteSpaces.setColor(SK_ColorLTGRAY);
6463
6464 SkPaint breakingSpace;
6465 breakingSpace.setColor(SK_ColorYELLOW);
6466
6467 SkPaint text;
6468 text.setColor(SK_ColorWHITE);
6469
6470 const char* arabic = "قففغغغغقففغغغغقففغغغ";
6471
6472 ParagraphStyle paragraph_style;
6473 paragraph_style.setTextAlign(TextAlign::kRight);
6474 TextStyle text_style;
6475 text_style.setColor(SK_ColorBLACK);
6476 text_style.setFontFamilies({SkString("Roboto")});
6477
6478 paragraph_style.setTextDirection(TextDirection::kRtl);
6479 paragraph_style.setTextAlign(TextAlign::kRight);
6480 text_style.setFontSize(20);
6481 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6482 text_style.setBackgroundColor(whiteSpaces);
6483 builder.pushStyle(text_style);
6484 builder.addText(" ");
6485 text_style.setBackgroundColor(text);
6486 builder.pushStyle(text_style);
6487 builder.addText(arabic);
6488
6489 auto paragraph = builder.Build();
6490 paragraph->layout(300);
6491 paragraph->paint(canvas.get(), 0, 0);
6492 }
6493
6494 UNIX_ONLY_TEST(SkParagraph_PositionInsideEmoji, reporter) {
6495
6496 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6497 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6498
6499 TestCanvas canvas("SkParagraph_PositionInsideEmoji.png");
6500
6501 std::u16string text = u"\U0001f469\u200D\U0001f469\u200D\U0001f467\u200D\U0001f467\U0001f469\U0001f469\U0001f467\U0001f467";
6502
6503 ParagraphStyle paragraph_style;
6504 TextStyle text_style;
6505 text_style.setColor(SK_ColorBLACK);
6506 text_style.setFontFamilies({SkString("Noto Color Emoji")});
6507 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6508 builder.pushStyle(text_style);
6509 builder.addText(text);
6510
6511 auto paragraph = builder.Build();
6512 paragraph->layout(TestCanvasWidth);
6513 paragraph->paint(canvas.get(), 0, 0);
6514
6515 // UTF8 UTF16
6516 // 4 [0:2)
6517 // 3 + 4 [2:5)
6518 // 3 + 4 [5:8)
6519 // 3 + 4 [8:11)
6520 // 4 [11:13)
6521 // 4 [13:15)
6522 // 4 [15:17)
6523 // 4 [17:19)
6524
6525 auto family = paragraph->getRectsForRange(0, 11, RectHeightStyle::kTight, RectWidthStyle::kTight); // 00.0000000 + 17.4699993
6526 auto face01 = paragraph->getRectsForRange(11, 13, RectHeightStyle::kTight, RectWidthStyle::kTight); // 17.4699993 + 17.4699993
6527 auto face02 = paragraph->getRectsForRange(13, 15, RectHeightStyle::kTight, RectWidthStyle::kTight); // 34.9399986 + 17.4699993
6528 auto face03 = paragraph->getRectsForRange(15, 17, RectHeightStyle::kTight, RectWidthStyle::kTight); // 52.4099998 + 17.4699993
6529 auto face04 = paragraph->getRectsForRange(17, 19, RectHeightStyle::kTight, RectWidthStyle::kTight); // 69.8799973 + 17.4699993
6530
6531 int32_t words[] = { 11, 13, 15, 17, 19, 21};
6532 auto j = 0;
6533 for (auto i : words) {
6534 auto rects = paragraph->getRectsForRange(j, i, RectHeightStyle::kTight, RectWidthStyle::kTight);
6535 if (rects.empty()) {
6536 continue;
6537 }
6538 auto X = rects[0].rect.centerX();
6539 auto Y = rects[0].rect.centerY();
6540 auto res1 = paragraph->getGlyphPositionAtCoordinate(X - 5, Y);
6541 //SkDebugf("[%d:%d) @%f,%f: %d %s\n", j, i, X - 5, Y, res1.position, res1.affinity == Affinity::kDownstream ? "D" : "U");
6542 auto res2 = paragraph->getGlyphPositionAtCoordinate(X + 5, Y);
6543 //SkDebugf("[%d:%d) @%f,%f: %d %s\n\n", j, i, X + 5, Y, res2.position, res2.affinity == Affinity::kDownstream ? "D" : "U");
6544 REPORTER_ASSERT(reporter, i == res2.position && res1.position == j);
6545 j = i;
6546 }
6547 }
6548
6549 UNIX_ONLY_TEST(SkParagraph_SingleLineHeight1, reporter) {
6550 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6551 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6552
6553 TestCanvas canvas("SkParagraph_SingleLineHeight1.png");
6554
6555 auto paint = [&](const char* text) {
6556 ParagraphStyle paragraph_style;
6557 paragraph_style.setTextHeightBehavior(TextHeightBehavior::kDisableAll);
6558 paragraph_style.setMaxLines(1);
6559 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6560 TextStyle text_style;
6561 text_style.setColor(SK_ColorBLACK);
6562 text_style.setFontFamilies({SkString("Ahem")});
6563 text_style.setFontSize(14);
6564 text_style.setHeight(2);
6565 text_style.setHeightOverride(true);
6566 builder.pushStyle(text_style);
6567 builder.addText(text);
6568 auto paragraph = builder.Build();
6569 paragraph->layout(80);
6570 paragraph->paint(canvas.get(), 0, 0);
6571 REPORTER_ASSERT(reporter, paragraph->getHeight() == 14.0f);
6572 };
6573
6574 paint("Loooooooooooooooooooooooooooooooooooong text");
6575 paint("");
6576 }
6577
6578 UNIX_ONLY_TEST(SkParagraph_SingleLineHeight2, reporter) {
6579 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6580 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6581
6582 TestCanvas canvas("SkParagraph_SingleLineHeight2.png");
6583
6584 auto paint = [&](const char* text) {
6585 ParagraphStyle paragraph_style;
6586 paragraph_style.setMaxLines(1);
6587 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6588 TextStyle text_style;
6589 text_style.setColor(SK_ColorBLACK);
6590 text_style.setFontFamilies({SkString("Ahem")});
6591 text_style.setFontSize(14);
6592 text_style.setHeight(2);
6593 text_style.setHeightOverride(true);
6594 builder.pushStyle(text_style);
6595 builder.addText(text);
6596 auto paragraph = builder.Build();
6597 paragraph->layout(80);
6598 paragraph->paint(canvas.get(), 0, 0);
6599 REPORTER_ASSERT(reporter, paragraph->getHeight() == 28.0f);
6600 };
6601
6602 paint("Loooooooooooooooooooooooooooooooooooong text");
6603 paint("");
6604 }
6605
6606 UNIX_ONLY_TEST(SkParagraph_PlaceholderWidth, reporter) {
6607
6608 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6609 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6610
6611 TestCanvas canvas("SkParagraph_PlaceholderWidth.png");
6612
6613 const char* text = "1 23 456 7890"; // 13 * 50 = 650
6614
6615 ParagraphStyle paragraph_style;
6616 TextStyle text_style;
6617 text_style.setColor(SK_ColorBLACK);
6618 text_style.setFontSize(50);
6619 text_style.setFontFamilies({SkString("Ahem")});
6620 PlaceholderStyle placeholder(300, 50, PlaceholderAlignment::kBaseline, TextBaseline::kAlphabetic, 0);
6621
6622 auto draw = [&](bool withPlaceholder) {
6623 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6624 builder.pushStyle(text_style);
6625 builder.addText(text);
6626 if (withPlaceholder) {
6627 SkPaint red;
6628 red.setColor(SK_ColorRED);
6629 text_style.setBackgroundColor(red);
6630 builder.pushStyle(text_style);
6631 builder.addPlaceholder(placeholder);
6632 }
6633 builder.addText(text);
6634
6635 auto paragraph = builder.Build();
6636 paragraph->layout(950);
6637 paragraph->paint(canvas.get(), 0, 0);
6638 canvas.get()->translate(0, paragraph->getHeight());
6639 return paragraph->getMinIntrinsicWidth();
6640 };
6641
6642 auto len1 = draw(true);
6643 auto len2 = draw(false);
6644
6645 // placeholder: 300 "78901": 250
6646 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(len1, 300.0f, EPSILON100));
6647 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(len2, 250.0f, EPSILON100));
6648 }
6649
6650 UNIX_ONLY_TEST(SkParagraph_GlyphPositionsInEmptyLines, reporter) {
6651
6652 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6653 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6654
6655 TestCanvas canvas("SkParagraph_GlyphPositionsInEmptyLines.png");
6656 ParagraphStyle paragraph_style;
6657 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6658 TextStyle text_style;
6659 text_style.setFontFamilies({SkString("Roboto") });
6660 text_style.setFontSize(20);
6661 text_style.setColor(SK_ColorBLACK);
6662 builder.pushStyle(text_style);
6663 builder.addText("A\n\n");
6664 builder.pop();
6665 auto paragraph = builder.Build();
6666 paragraph->layout(300);
6667 paragraph->paint(canvas.get(), 0, 0);
6668
6669 auto res1 = paragraph->
6670 getGlyphPositionAtCoordinate(paragraph->getMinIntrinsicWidth(),1);
6671 REPORTER_ASSERT(reporter, res1.position == 1 && res1.affinity == Affinity::kUpstream);
6672
6673 auto res2 = paragraph->
6674 getGlyphPositionAtCoordinate(0,paragraph->getHeight() * 0.5);
6675 REPORTER_ASSERT(reporter, res2.position == 2 && res2.affinity == Affinity::kDownstream);
6676
6677 auto res3 = paragraph->
6678 getGlyphPositionAtCoordinate(0,paragraph->getHeight() - 1);
6679 REPORTER_ASSERT(reporter, res3.position == 3 && res3.affinity == Affinity::kDownstream);
6680 }
6681
6682 UNIX_ONLY_TEST(SkParagraph_RTLGlyphPositions, reporter) {
6683
6684 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6685 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6686
6687 TestCanvas canvas("SkParagraph_RTLGlyphPositions.png");
6688 ParagraphStyle paragraph_style;
6689 paragraph_style.setTextDirection(TextDirection::kRtl);
6690 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6691 TextStyle text_style;
6692 text_style.setFontFamilies({SkString("Roboto") });
6693 text_style.setFontSize(20);
6694 text_style.setColor(SK_ColorBLACK);
6695 builder.pushStyle(text_style);
6696 builder.addText("אאאא");
6697 builder.pop();
6698 auto paragraph = builder.Build();
6699 paragraph->layout(500);
6700 paragraph->paint(canvas.get(), 0, 0);
6701
6702 std::vector<std::pair<SkScalar, PositionWithAffinity>> checks = {
6703 std::make_pair(550, PositionWithAffinity(0, Affinity::kDownstream)),
6704 std::make_pair(500, PositionWithAffinity(0, Affinity::kDownstream)),
6705 std::make_pair(494, PositionWithAffinity(1, Affinity::kUpstream)),
6706 std::make_pair(488, PositionWithAffinity(1, Affinity::kDownstream)),
6707 std::make_pair(485, PositionWithAffinity(2, Affinity::kUpstream)),
6708 std::make_pair(480, PositionWithAffinity(2, Affinity::kDownstream)),
6709 std::make_pair(475, PositionWithAffinity(3, Affinity::kUpstream)),
6710 std::make_pair(471, PositionWithAffinity(3, Affinity::kDownstream)),
6711 std::make_pair(467, PositionWithAffinity(4, Affinity::kUpstream)),
6712 std::make_pair( 0, PositionWithAffinity(4, Affinity::kUpstream)),
6713 };
6714
6715 for (auto check : checks) {
6716 auto pos = paragraph->getGlyphPositionAtCoordinate(check.first, 0);
6717 REPORTER_ASSERT(reporter, pos.affinity == check.second.affinity);
6718 REPORTER_ASSERT(reporter, pos.position == check.second.position);
6719 }
6720 }
6721
6722 UNIX_ONLY_TEST(SkParagraph_RTLGlyphPositionsInEmptyLines, reporter) {
6723
6724 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6725 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6726
6727 TestCanvas canvas("SkParagraph_RTLGlyphPositionsInEmptyLines.png");
6728
6729 ParagraphStyle paragraph_style;
6730 paragraph_style.setTextDirection(TextDirection::kRtl);
6731 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6732 TextStyle text_style;
6733 text_style.setFontFamilies({SkString("Roboto") });
6734 text_style.setFontSize(20);
6735 text_style.setColor(SK_ColorBLACK);
6736 builder.pushStyle(text_style);
6737 //builder.addText("בבבב\n\nאאאא");
6738 builder.addText("בבבב\n\nאאאא");
6739 builder.pop();
6740 auto paragraph = builder.Build();
6741 paragraph->layout(500);
6742 paragraph->paint(canvas.get(), 0, 0);
6743
6744 auto height = paragraph->getHeight();
6745 auto res1 = paragraph->getGlyphPositionAtCoordinate(0, 0);
6746 REPORTER_ASSERT(reporter, res1.position == 4 && res1.affinity == Affinity::kUpstream);
6747 auto res2 = paragraph->getGlyphPositionAtCoordinate(0, height / 2);
6748 REPORTER_ASSERT(reporter, res2.position == 5 && res2.affinity == Affinity::kDownstream);
6749 auto res3 = paragraph->getGlyphPositionAtCoordinate(0, height);
6750 REPORTER_ASSERT(reporter, res3.position == 10 && res3.affinity == Affinity::kUpstream);
6751 }
6752
6753 UNIX_ONLY_TEST(SkParagraph_LTRGlyphPositionsForTrailingSpaces, reporter) {
6754
6755 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6756 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6757
6758 TestCanvas canvas("SkParagraph_LTRGlyphPositionsForTrailingSpaces.png");
6759
6760 ParagraphStyle paragraph_style;
6761 TextStyle text_style;
6762 text_style.setFontFamilies({SkString("Ahem") });
6763 text_style.setFontSize(10);
6764 text_style.setColor(SK_ColorBLACK);
6765
6766 auto test = [&](const char* text) {
6767 auto str = straight(text);
6768 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6769 builder.pushStyle(text_style);
6770 builder.addText(str);
6771 builder.pop();
6772 SkPaint gray; gray.setColor(SK_ColorGRAY);
6773 auto paragraph = builder.Build();
6774 paragraph->layout(100);
6775 canvas.get()->translate(0, 20);
6776 canvas.get()->drawRect(SkRect::MakeXYWH(0, 0, paragraph->getMaxIntrinsicWidth(), paragraph->getHeight()), gray);
6777 paragraph->paint(canvas.get(), 0, 0);
6778 canvas.get()->translate(0, paragraph->getHeight());
6779
6780 for (size_t i = 0; i < str.size(); ++i) {
6781 auto res = paragraph->getGlyphPositionAtCoordinate(i * 10, 2);
6782 //SkDebugf("@%f[%d]: %d %s\n", i * 10.0f, i, res.position, res.affinity == Affinity::kDownstream ? "D" : "U");
6783 // There is a hidden codepoint at the beginning (to make it symmetric to RTL)
6784 REPORTER_ASSERT(reporter, res.position == SkToInt(i) + (i > 0 ? 1 : 0));
6785 // The ending looks slightly different...
6786 REPORTER_ASSERT(reporter, res.affinity == (res.position == SkToInt(str.size()) ? Affinity::kUpstream : Affinity::kDownstream));
6787 }
6788 };
6789
6790 test(" ");
6791 test("hello ");
6792 }
6793
6794 UNIX_ONLY_TEST(SkParagraph_RTLGlyphPositionsForTrailingSpaces, reporter) {
6795
6796 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6797 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6798
6799 TestCanvas canvas("SkParagraph_RTLGlyphPositionsForTrailingSpaces.png");
6800
6801 ParagraphStyle paragraph_style;
6802 paragraph_style.setTextDirection(TextDirection::kRtl);
6803 paragraph_style.setTextAlign(TextAlign::kRight);
6804 TextStyle text_style;
6805 text_style.setFontFamilies({SkString("Ahem") });
6806 text_style.setFontSize(10);
6807 text_style.setColor(SK_ColorBLACK);
6808 canvas.get()->translate(200, 0);
6809
6810 auto test = [&](const char* text, int whitespaces) {
6811 auto str = mirror(text);
6812 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6813 builder.pushStyle(text_style);
6814 builder.addText(str);
6815 builder.pop();
6816 SkPaint gray; gray.setColor(SK_ColorGRAY);
6817 auto paragraph = builder.Build();
6818 paragraph->layout(100);
6819 canvas.get()->translate(0, 20);
6820 auto res = paragraph->getRectsForRange(0, str.size(), RectHeightStyle::kTight, RectWidthStyle::kTight);
6821 bool even = true;
6822 for (auto& r : res) {
6823 if (even) {
6824 gray.setColor(SK_ColorGRAY);
6825 } else {
6826 gray.setColor(SK_ColorLTGRAY);
6827 }
6828 even = !even;
6829 canvas.get()->drawRect(r.rect, gray);
6830 }
6831 gray.setColor(SK_ColorRED);
6832 canvas.get()->drawRect(SkRect::MakeXYWH(0, 0, 1, paragraph->getHeight()), gray);
6833 paragraph->paint(canvas.get(), 0, 0);
6834 canvas.get()->translate(0, paragraph->getHeight());
6835
6836 for (int i = 0; i < SkToInt(str.size()); ++i) {
6837 // Additional 1.0f to make sure the offset is not too close to the
6838 // edge of glyphs.
6839 auto pointX = (whitespaces + i) * 10.0f + 1.0f;
6840 auto pos = paragraph->getGlyphPositionAtCoordinate(pointX, 2);
6841 //SkDebugf("@%f[%d]: %d %s\n", pointX, i, pos.position, pos.affinity == Affinity::kDownstream ? "D" : "U");
6842 // At the beginning there is a control codepoint that makes the string RTL
6843 REPORTER_ASSERT(reporter, (pos.position + i) == SkToInt(str.size()) - (pos.affinity == Affinity::kDownstream ? 1 : 0));
6844 }
6845 };
6846
6847 test(" ", 6);
6848 test(" hello", -10);
6849 }
6850
6851 UNIX_ONLY_TEST(SkParagraph_LTRLineMetricsDoesNotIncludeNewLine, reporter) {
6852
6853 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6854 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6855
6856 TestCanvas canvas("SkParagraph_LTRLineMetricsDoesNotIncludeNewLine.png");
6857
6858 ParagraphStyle paragraph_style;
6859 paragraph_style.setTextDirection(TextDirection::kRtl);
6860 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6861 TextStyle text_style;
6862 text_style.setFontFamilies({SkString("Roboto") });
6863 text_style.setFontSize(20);
6864 text_style.setColor(SK_ColorBLACK);
6865 builder.pushStyle(text_style);
6866 builder.addText("one two\n\nthree four\nwith spaces \n \n______________________");
6867 builder.pop();
6868 auto paragraph = builder.Build();
6869 paragraph->layout(190);
6870 paragraph->paint(canvas.get(), 0, 0);
6871
6872 std::vector<std::tuple<size_t, size_t, size_t, size_t>> expected = {
6873 { 0, 7, 7, 8 }, // one two\n
6874 { 8, 8, 8, 9 }, // \n
6875 { 9, 19, 19, 20 }, // three four\n
6876 { 20, 31, 36, 37 }, // with spaces \n
6877 { 37, 37, 41, 42 }, // { just spaces }\n
6878 { 42, 63, 63, 63 }, // _____________________
6879 { 63, 64, 64, 64 }, // _
6880 };
6881 std::vector<LineMetrics> metrics;
6882 paragraph->getLineMetrics(metrics);
6883 for (auto& metric : metrics) {
6884 //SkDebugf("Line[%d:%d <= %d <=%d)\n", metric.fStartIndex, metric.fEndExcludingWhitespaces, metric.fEndIndex, metric.fEndIncludingNewline);
6885 auto result = expected[metric.fLineNumber];
6886 REPORTER_ASSERT(reporter, metric.fStartIndex ==std::get<0>(result));
6887 REPORTER_ASSERT(reporter, metric.fEndExcludingWhitespaces == std::get<1>(result));
6888 REPORTER_ASSERT(reporter, metric.fEndIndex == std::get<2>(result));
6889 REPORTER_ASSERT(reporter, metric.fEndIncludingNewline == std::get<3>(result));
6890 }
6891 }
6892
6893 UNIX_ONLY_TEST(SkParagraph_RTLLineMetricsDoesNotIncludeNewLine, reporter) {
6894
6895 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6896 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6897
6898 TestCanvas canvas("SkParagraph_RTLLineMetricsDoesNotIncludeNewLine.png");
6899 canvas.get()->translate(100, 100);
6900
6901 ParagraphStyle paragraph_style;
6902 paragraph_style.setTextDirection(TextDirection::kRtl);
6903 paragraph_style.setTextAlign(TextAlign::kRight);
6904 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6905 TextStyle text_style;
6906 text_style.setFontFamilies({SkString("Roboto") });
6907 text_style.setFontSize(20);
6908 text_style.setColor(SK_ColorBLACK);
6909 builder.pushStyle(text_style);
6910 builder.addText(mirror("______________________\none two\n\nthree four\nwith spaces \n "));
6911 builder.pop();
6912 auto paragraph = builder.Build();
6913 paragraph->layout(190);
6914 paragraph->paint(canvas.get(), 0, 0);
6915 //auto impl = static_cast<ParagraphImpl*>(paragraph.get());
6916
6917 SkPaint gray;
6918 gray.setColor(SK_ColorGRAY);
6919 gray.setStyle(SkPaint::kStroke_Style);
6920 gray.setAntiAlias(true);
6921 gray.setStrokeWidth(1);
6922 canvas.get()->drawRect(SkRect::MakeXYWH(0, 0, paragraph->getMaxWidth(), paragraph->getHeight()), gray);
6923
6924 SkPaint red;
6925 red.setColor(SK_ColorRED);
6926 red.setStyle(SkPaint::kStroke_Style);
6927 red.setAntiAlias(true);
6928 red.setStrokeWidth(1);
6929
6930 SkPaint blue;
6931 blue.setColor(SK_ColorRED);
6932 blue.setStyle(SkPaint::kStroke_Style);
6933 blue.setAntiAlias(true);
6934 blue.setStrokeWidth(1);
6935
6936 auto boxes = paragraph->getRectsForRange(0, 100, RectHeightStyle::kTight, RectWidthStyle::kTight);
6937 bool even = false;
6938 for (auto& box : boxes) {
6939 canvas.get()->drawRect(box.rect, even ? red : blue);
6940 even = !even;
6941 }
6942
6943 // RTL codepoint u"\u202E" messes everything up
6944 // (adds one invisible codepoint to the first line
6945 // and shift all the indexes by 1 right)
6946 std::vector<std::tuple<int, int, int, int>> expected = {
6947 { 0, 1, 5, 6 }, // { just spaces; the end of the text considered as a new line in libtxt?!? }
6948 { 6, 22, 22, 23 }, // with spaces \n
6949 { 23, 33, 33, 34 }, // three four\n
6950 { 34, 34, 34, 35 }, // \n
6951 { 35, 42, 42, 43 }, // one two\n
6952 { 43, 64, 64, 64 }, // _____________________
6953 { 64, 65, 65, 65 } // _
6954 };
6955
6956 std::vector<LineMetrics> metrics;
6957 paragraph->getLineMetrics(metrics);
6958 for (auto& metric : metrics) {
6959 //SkDebugf("Line[%d:%d <= %d <=%d]\n", metric.fStartIndex, metric.fEndExcludingWhitespaces, metric.fEndIndex, metric.fEndIncludingNewline);
6960 auto result = expected[metric.fLineNumber];
6961 REPORTER_ASSERT(reporter, metric.fStartIndex == SkToU32(std::get<0>(result)));
6962 REPORTER_ASSERT(reporter, metric.fEndExcludingWhitespaces == SkToU32(std::get<1>(result)));
6963 REPORTER_ASSERT(reporter, metric.fEndIndex == SkToU32(std::get<2>(result)));
6964 REPORTER_ASSERT(reporter, metric.fEndIncludingNewline == SkToU32(std::get<3>(result)));
6965 }
6966 }
6967
6968 UNIX_ONLY_TEST(SkParagraph_PlaceholderPosition, reporter) {
6969 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
6970 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
6971
6972 TestCanvas canvas("SkParagraph_PlaceholderPosition.png");
6973 canvas.get()->translate(100, 100);
6974
6975 TextStyle text_style;
6976 text_style.setColor(SK_ColorBLACK);
6977 text_style.setFontFamilies({SkString("Ahem")});
6978 text_style.setFontSize(10.0f);
6979 ParagraphStyle paragraph_style;
6980 paragraph_style.setTextStyle(text_style);
6981 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
6982 builder.pushStyle(text_style);
6983 builder.addText("abcd");
6984
6985 PlaceholderStyle placeholder_style;
6986 placeholder_style.fHeight = 10;
6987 placeholder_style.fWidth = 10;
6988 placeholder_style.fBaseline = TextBaseline::kAlphabetic;
6989 placeholder_style.fAlignment = PlaceholderAlignment::kBottom;
6990 builder.addPlaceholder(placeholder_style);
6991
6992 auto paragraph = builder.Build();
6993 paragraph->layout(500);
6994 paragraph->paint(canvas.get(), 0, 0);
6995
6996 auto result = paragraph->getGlyphPositionAtCoordinate(41.0f, 0.0f);
6997 REPORTER_ASSERT(reporter, result.position == 4 && result.affinity == Affinity::kDownstream);
6998 }
6999
7000 UNIX_ONLY_TEST(SkParagraph_LineEnd, reporter) {
7001 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7002 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7003
7004 TestCanvas canvas("SkParagraph_LineEnd.png");
7005 canvas.get()->translate(100, 100);
7006
7007 TextStyle text_style;
7008 text_style.setColor(SK_ColorBLACK);
7009 text_style.setFontFamilies({SkString("Ahem")});
7010 text_style.setFontSize(10.0f);
7011 ParagraphStyle paragraph_style;
7012 paragraph_style.setTextStyle(text_style);
7013 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7014 builder.pushStyle(text_style);
7015 builder.addText("Hello ");
7016 builder.addText("hello ");
7017 builder.addText("hello\n");
7018 builder.addText("hello \n");
7019 builder.addText("world");
7020
7021 auto paragraph = builder.Build();
7022 paragraph->layout(60.0f);
7023 paragraph->paint(canvas.get(), 0, 0);
7024
7025 std::vector<LineMetrics> lm;
7026 paragraph->getLineMetrics(lm);
7027 /*
7028 for (auto& lm : lm) {
7029 SkDebugf("%d %d %d\n", (int)lm.fEndExcludingWhitespaces, (int)lm.fEndIndex, (int)lm.fEndIncludingNewline);
7030 }
7031 */
7032 REPORTER_ASSERT(reporter, lm.size() >= 4);
7033 if (lm.size() >= 4) {
7034 REPORTER_ASSERT(reporter, lm[0].fEndExcludingWhitespaces == 05 && lm[0].fEndIndex == 06 && lm[0].fEndIncludingNewline == 06);
7035 REPORTER_ASSERT(reporter, lm[1].fEndExcludingWhitespaces == 11 && lm[1].fEndIndex == 14 && lm[1].fEndIncludingNewline == 14);
7036 REPORTER_ASSERT(reporter, lm[2].fEndExcludingWhitespaces == 19 && lm[2].fEndIndex == 19 && lm[2].fEndIncludingNewline == 20);
7037 REPORTER_ASSERT(reporter, lm[3].fEndExcludingWhitespaces == 25 && lm[3].fEndIndex == 28 && lm[3].fEndIncludingNewline == 29);
7038 }
7039 }
7040
7041 UNIX_ONLY_TEST(SkParagraph_Utf16Indexes, reporter) {
7042 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7043 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7044
7045 TestCanvas canvas("SkParagraph_Utf16Indexes.png");
7046 canvas.get()->translate(100, 100);
7047
7048 TextStyle text_style;
7049 text_style.setColor(SK_ColorBLACK);
7050 text_style.setFontFamilies({SkString("Ahem")});
7051 text_style.setFontSize(10.0f);
7052 ParagraphStyle paragraph_style;
7053 paragraph_style.setTextStyle(text_style);
7054 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7055 builder.pushStyle(text_style);
7056 builder.addText("áéíóú\nxxxx");
7057 auto paragraph = builder.Build();
7058 paragraph->layout(60.0f);
7059 paragraph->paint(canvas.get(), 0, 0);
7060 std::vector<LineMetrics> lm;
7061 paragraph->getLineMetrics(lm);
7062 //for (auto& lm : lm) {
7063 // SkDebugf("%d %d %d\n", (int)lm.fEndExcludingWhitespaces, (int)lm.fEndIndex, (int)lm.fEndIncludingNewline);
7064 //}
7065 REPORTER_ASSERT(reporter, lm.size() == 2);
7066 if (lm.size() >= 2) {
7067 REPORTER_ASSERT(reporter, lm[0].fEndExcludingWhitespaces == 05 && lm[0].fEndIndex == 05 && lm[0].fEndIncludingNewline == 06);
7068 REPORTER_ASSERT(reporter, lm[1].fEndExcludingWhitespaces == 10 && lm[1].fEndIndex == 10 && lm[1].fEndIncludingNewline == 10);
7069 }
7070 }
7071
7072 UNIX_ONLY_TEST(SkParagraph_RTLFollowedByLTR, reporter) {
7073 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7074 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7075
7076 TestCanvas canvas("SkParagraph_RTLFollowedByLTR.png");
7077 canvas.get()->translate(100, 100);
7078
7079 TextStyle text_style;
7080 text_style.setFontFamilies({SkString("Ahem")});
7081 text_style.setFontSize(10);
7082 text_style.setColor(SK_ColorBLACK);
7083
7084 ParagraphStyle paragraph_style;
7085 paragraph_style.setTextStyle(text_style);
7086 paragraph_style.setTextDirection(TextDirection::kLtr);
7087 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7088 builder.pushStyle(text_style);
7089 builder.addText(u"\u05D0\u05D0\u05D0ABC");
7090 auto paragraph = builder.Build();
7091 paragraph->layout(100);
7092 paragraph->paint(canvas.get(), 0, 0);
7093
7094 auto boxes = paragraph->getRectsForRange(
7095 0, paragraph->getMaxWidth(), RectHeightStyle::kTight, RectWidthStyle::kTight);
7096 REPORTER_ASSERT(reporter, boxes.size() == 2);
7097 REPORTER_ASSERT(
7098 reporter,
7099 boxes[0].direction == TextDirection::kRtl && boxes[1].direction == TextDirection::kLtr);
7100 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.fLeft, 0.0f));
7101 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.fRight, boxes[1].rect.fLeft));
7102 REPORTER_ASSERT(reporter,
7103 SkScalarNearlyEqual(boxes[1].rect.fRight, paragraph->getMaxIntrinsicWidth()));
7104
7105 std::vector<std::pair<SkScalar, PositionWithAffinity>> checks = {
7106 std::make_pair(-10, PositionWithAffinity(3, Affinity::kUpstream)),
7107 std::make_pair( 0, PositionWithAffinity(3, Affinity::kUpstream)),
7108 std::make_pair( 5, PositionWithAffinity(2, Affinity::kDownstream)),
7109 std::make_pair( 10, PositionWithAffinity(2, Affinity::kUpstream)),
7110 std::make_pair( 15, PositionWithAffinity(1, Affinity::kDownstream)),
7111 std::make_pair( 20, PositionWithAffinity(1, Affinity::kUpstream)),
7112 std::make_pair( 25, PositionWithAffinity(0, Affinity::kDownstream)),
7113 std::make_pair( 30, PositionWithAffinity(3, Affinity::kDownstream)),
7114 std::make_pair( 35, PositionWithAffinity(4, Affinity::kUpstream)),
7115 std::make_pair( 40, PositionWithAffinity(4, Affinity::kDownstream)),
7116 std::make_pair( 45, PositionWithAffinity(5, Affinity::kUpstream)),
7117 std::make_pair( 50, PositionWithAffinity(5, Affinity::kDownstream)),
7118 std::make_pair( 55, PositionWithAffinity(6, Affinity::kUpstream)),
7119 std::make_pair( 60, PositionWithAffinity(6, Affinity::kUpstream)),
7120 };
7121
7122 for (auto check : checks) {
7123 auto pos = paragraph->getGlyphPositionAtCoordinate(check.first, 0);
7124 REPORTER_ASSERT(reporter, pos.affinity == check.second.affinity);
7125 REPORTER_ASSERT(reporter, pos.position == check.second.position);
7126 }
7127 }
7128
7129 UNIX_ONLY_TEST(SkParagraph_StrutTopLine, reporter) {
7130 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7131 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7132
7133 TestCanvas canvas("SkParagraph_StrutTopLine.png");
7134
7135 TextStyle text_style;
7136 text_style.setFontFamilies({SkString("Ahem")});
7137 text_style.setFontSize(10);
7138 SkPaint black;
7139 black.setColor(SK_ColorBLACK);
7140 text_style.setForegroundColor(black);
7141
7142 ParagraphStyle paragraph_style;
7143 paragraph_style.setTextStyle(text_style);
7144 paragraph_style.setTextDirection(TextDirection::kLtr);
7145 StrutStyle strut_style;
7146 strut_style.setStrutEnabled(true);
7147 strut_style.setFontFamilies({SkString("Ahem")});
7148 strut_style.setFontSize(16);
7149 strut_style.setHeight(4.0f);
7150 strut_style.setHeightOverride(true);
7151 strut_style.setLeading(-1.0f);
7152 strut_style.setForceStrutHeight(true);
7153 paragraph_style.setStrutStyle(strut_style);
7154 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7155
7156 builder.pushStyle(text_style);
7157 builder.addText(u"Atwater Peel Sherbrooke Bonaventure\nhi\nwasssup!");
7158
7159 auto paragraph = builder.Build();
7160 paragraph->layout(797);
7161 paragraph->paint(canvas.get(), 0, 0);
7162 auto boxes = paragraph->getRectsForRange(0, 60, RectHeightStyle::kIncludeLineSpacingTop, RectWidthStyle::kMax);
7163 REPORTER_ASSERT(reporter, boxes.size() == 4);
7164 if (boxes.size() >= 4) {
7165 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.fTop, 38.4f));
7166 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.fBottom, 64.0f));
7167
7168 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.fTop, 64.0f));
7169 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.fBottom, 128.0f));
7170
7171 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.fTop, 64.0f));
7172 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.fBottom, 128.0f));
7173
7174 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.fTop, 128.0f));
7175 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.fBottom, 192.0f));
7176 }
7177 }
7178
7179 UNIX_ONLY_TEST(SkParagraph_DifferentFontsTopLine, reporter) {
7180 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7181 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7182
7183 TestCanvas canvas("SkParagraph_DifferentFontsTopLine.png");
7184
7185 TextStyle text_style;
7186 text_style.setFontFamilies({SkString("Ahem")});
7187 text_style.setFontSize(10);
7188 SkPaint black;
7189 black.setColor(SK_ColorBLACK);
7190 text_style.setForegroundColor(black);
7191
7192 ParagraphStyle paragraph_style;
7193 paragraph_style.setTextStyle(text_style);
7194 paragraph_style.setTextDirection(TextDirection::kLtr);
7195 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7196
7197 text_style.setFontSize(30.0);
7198 builder.pushStyle(text_style);
7199 builder.addText(u"Atwater Peel ");
7200 text_style.setFontSize(15.0);
7201 builder.pushStyle(text_style);
7202 builder.addText(u"Sherbrooke Bonaventure ");
7203 text_style.setFontSize(10.0);
7204 builder.pushStyle(text_style);
7205 builder.addText(u"hi wassup!");
7206
7207 auto paragraph = builder.Build();
7208 paragraph->layout(797);
7209 paragraph->paint(canvas.get(), 0, 0);
7210 auto boxes = paragraph->getRectsForRange(0, 60, RectHeightStyle::kIncludeLineSpacingTop, RectWidthStyle::kMax);
7211 REPORTER_ASSERT(reporter, boxes.size() == 4);
7212 if (boxes.size() >= 4) {
7213 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.fTop, 00.0f));
7214 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[0].rect.fBottom, 30.0f));
7215
7216 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.fTop, 00.0f));
7217 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[1].rect.fBottom, 30.0f));
7218
7219 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.fTop, 00.0f));
7220 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[2].rect.fBottom, 30.0f));
7221
7222 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.fTop, 30.0f));
7223 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(boxes[3].rect.fBottom, 40.0f));
7224 }
7225 }
7226
7227 UNIX_ONLY_TEST(SkParagraph_SimpleParagraphReset, reporter) {
7228 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7229 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7230 const char* text = "Hello World Text Dialog";
7231 const size_t len = strlen(text);
7232
7233 ParagraphStyle paragraph_style;
7234 paragraph_style.turnHintingOff();
7235 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7236
7237 for (int iteration = 0; iteration < 2; iteration += 1) {
7238 builder.Reset();
7239 REPORTER_ASSERT(reporter, builder.peekStyle().equals(paragraph_style.getTextStyle()));
7240
7241 TextStyle text_style;
7242 text_style.setFontFamilies({SkString("Roboto")});
7243 text_style.setColor(SK_ColorBLACK);
7244 builder.pushStyle(text_style);
7245 builder.addText(text, len);
7246 builder.pop();
7247
7248 auto paragraph = builder.Build();
7249 paragraph->layout(TestCanvasWidth);
7250 REPORTER_ASSERT(reporter, paragraph->unresolvedGlyphs() == 0);
7251
7252 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
7253 REPORTER_ASSERT(reporter, impl->runs().size() == 1);
7254 REPORTER_ASSERT(reporter, impl->styles().size() == 1); // paragraph style does not count
7255 REPORTER_ASSERT(reporter, impl->styles()[0].fStyle.equals(text_style));
7256
7257 size_t index = 0;
7258 for (auto& line : impl->lines()) {
7259 line.scanStyles(StyleType::kDecorations,
7260 [&index, reporter]
7261 (TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
7262 REPORTER_ASSERT(reporter, index == 0);
7263 REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK);
7264 ++index;
7265 });
7266 }
7267 }
7268 }
7269
7270 UNIX_ONLY_TEST(SkParagraph_EllipsisGetRectForRange, reporter) {
7271 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7272 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7273 TestCanvas canvas("SkParagraph_EllipsisGetRectForRange.png");
7274 const char* text =
7275 "This is a very long sentence to test if the text will properly wrap "
7276 "around and go to the next line. Sometimes, short sentence. Longer "
7277 "sentences are okay too because they are nessecary. Very short. ";
7278 const size_t len = strlen(text);
7279
7280 ParagraphStyle paragraph_style;
7281 paragraph_style.setMaxLines(1);
7282 std::u16string ellipsis = u"\u2026";
7283 paragraph_style.setEllipsis(ellipsis);
7284 std::u16string e = paragraph_style.getEllipsisUtf16();
7285 paragraph_style.turnHintingOff();
7286 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7287
7288 TextStyle text_style;
7289 text_style.setFontFamilies({SkString("Roboto")});
7290 text_style.setColor(SK_ColorBLACK);
7291 builder.pushStyle(text_style);
7292 builder.addText(text, len);
7293 builder.pop();
7294
7295 auto paragraph = builder.Build();
7296 paragraph->layout(TestCanvasWidth);
7297 paragraph->paint(canvas.get(), 0, 0);
7298
7299 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
7300
7301 // Check that the ellipsizer limited the text to one line and did not wrap to a second line.
7302 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
7303
7304 auto boxes1 = impl->getRectsForRange(0, 2, RectHeightStyle::kTight, RectWidthStyle::kTight);
7305 REPORTER_ASSERT(reporter, boxes1.size() == 1);
7306
7307 auto boxes2 = impl->getRectsForRange(0, 3, RectHeightStyle::kTight, RectWidthStyle::kTight);
7308 REPORTER_ASSERT(reporter, boxes2.size() == 1);
7309
7310 canvas.drawRects(SK_ColorRED, boxes1);
7311 canvas.drawRects(SK_ColorRED, boxes2);
7312 }
7313
7314 // This test does not produce an image
7315 UNIX_ONLY_TEST(SkParagraph_StrutAndTextBehavior, reporter) {
7316 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7317 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7318 const char* text = "";
7319 const size_t len = strlen(text);
7320
7321 TextStyle text_style;
7322 text_style.setFontFamilies({SkString("Ahem")});
7323 text_style.setFontSize(16.0);
7324 text_style.setColor(SK_ColorBLACK);
7325 StrutStyle strut_style;
7326 strut_style.setStrutEnabled(true);
7327 strut_style.setForceStrutHeight(true);
7328 strut_style.setHeight(1.5);
7329 strut_style.setHeightOverride(true);
7330 strut_style.setFontFamilies({SkString("Ahem")});
7331 strut_style.setFontSize(16.0);
7332 ParagraphStyle paragraph_style;
7333 paragraph_style.setStrutStyle(strut_style);
7334 paragraph_style.setTextStyle(text_style);
7335
7336 auto layout = [&](TextHeightBehavior tb) {
7337 paragraph_style.setTextHeightBehavior(tb);
7338 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7339 builder.pushStyle(text_style);
7340 builder.addText(text, len);
7341 auto paragraph = builder.Build();
7342 paragraph->layout(SK_ScalarInfinity);
7343 return paragraph->getHeight();
7344 };
7345
7346 auto height1 = layout(TextHeightBehavior::kDisableAll);
7347 auto height2 = layout(TextHeightBehavior::kAll);
7348
7349 // Regardless of TextHeightBehavior strut sets the line height
7350 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(height1, 24.0f));
7351 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(height2, 24.0f));
7352 }
7353
7354 UNIX_ONLY_TEST(SkParagraph_NonMonotonicGlyphsLTR, reporter) {
7355 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7356 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7357 NEED_SYSTEM_FONTS(fontCollection)
7358
7359 TestCanvas canvas("SkParagraph_NonMonotonicGlyphsLTR.png");
7360 std::u16string text =
7361 u"\u0068\u0301\u0350\u0312\u0357\u030C\u0369\u0305\u036C\u0304\u0310\u033F\u0366\u0350 ";
7362 /*
7363 u"\u0343\u0364\u0369\u0311\u0309\u030E\u0365\u031B\u0340\u0337\u0335\u035E\u0334\u0328"
7364 u"\u0360\u0360\u0315\u035F\u0340\u0340\u0362\u0360\u0322\u031B\u031B\u0337\u0340\u031E"
7365 u"\u031F\u032A\u0331\u0345\u032F\u0332\u032E\u0333\u0353\u0320\u0345\u031C\u031F\u033C"
7366 u"\u0325\u0355\u032C\u0325\u033Aa\u0307\u0312\u034B\u0308\u0312\u0346\u0313\u0346\u0304"
7367 u"\u0307\u0344\u0305\u0342\u0368\u0346\u036A\u035B\u030F\u0365\u0307\u0340\u0328\u0322"
7368 u"\u0361\u0489\u034F\u0328\u0334\u035F\u0335\u0362\u0489\u0360\u0358\u035E\u0360\u035D"
7369 u"\u0341\u0337\u0337\u032E\u0326\u032D\u0359\u0318\u033C\u032F\u0333\u035A\u034D\u0319"
7370 u"\u031C\u0353\u033C\u0345\u0359\u0331\u033B\u0331\u033C";
7371 */
7372
7373 TextStyle text_style;
7374 text_style.setFontSize(14);
7375 text_style.setFontFamilies({SkString("Roboto")});
7376 text_style.setColor(SK_ColorBLACK);
7377
7378 ParagraphStyle paragraph_style;
7379 paragraph_style.setTextStyle(text_style);
7380 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7381
7382 builder.pushStyle(text_style);
7383 builder.addText(text);
7384 auto paragraph = builder.Build();
7385 paragraph->layout(SK_ScalarInfinity);
7386
7387 paragraph->layout(paragraph->getMinIntrinsicWidth() + 1);
7388 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
7389 REPORTER_ASSERT(reporter, impl->runs().size() > 1); // It's not the simple case
7390 bool hasNonMonotonicPlacement = false;
7391 for (auto& run : impl->runs()) {
7392 for (auto& offset : run.offsets()) {
7393 if (offset.fX < 0) {
7394 hasNonMonotonicPlacement = true;
7395 }
7396 }
7397 if (hasNonMonotonicPlacement) {
7398 break;
7399 }
7400 }
7401 REPORTER_ASSERT(reporter, hasNonMonotonicPlacement); // There are non-monotonic placement
7402 REPORTER_ASSERT(reporter, impl->lineNumber() == 1); // But it's still one line
7403 paragraph->paint(canvas.get(), 0, 0);
7404 }
7405
7406 UNIX_ONLY_TEST(SkParagraph_NonMonotonicGlyphsRTL, reporter) {
7407 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7408 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7409 NEED_SYSTEM_FONTS(fontCollection)
7410
7411 TestCanvas canvas("SkParagraph_NonMonotonicGlyphsRTL.png");
7412 const char* text = "ٱلْرَّحْمَـانُ";
7413 const size_t len = strlen(text);
7414
7415 TextStyle text_style;
7416 text_style.setFontSize(14);
7417 text_style.setColor(SK_ColorBLACK);
7418
7419 ParagraphStyle paragraph_style;
7420 paragraph_style.setTextStyle(text_style);
7421 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7422
7423 builder.pushStyle(text_style);
7424 builder.addText(text, len);
7425 auto paragraph = builder.Build();
7426 paragraph->layout(SK_ScalarInfinity);
7427
7428 paragraph->layout(paragraph->getMinIntrinsicWidth() + 1);
7429 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
7430 bool hasNonMonotonicPlacement = false;
7431 for (auto& run : impl->runs()) {
7432 for (auto& offset : run.offsets()) {
7433 if (offset.fX < 0) {
7434 hasNonMonotonicPlacement = true;
7435 }
7436 }
7437 if (hasNonMonotonicPlacement) {
7438 break;
7439 }
7440 }
7441 REPORTER_ASSERT(reporter, impl->lineNumber() == 1); // But it's still one line
7442 paragraph->paint(canvas.get(), 0, 0);
7443 }
7444
7445 void performGetRectsForRangeConcurrently(skiatest::Reporter* reporter) {
7446 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7447 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7448
7449 auto const text = std::u16string(42000, 'x');
7450 ParagraphStyle paragraphStyle;
7451 TextStyle textStyle;
7452 textStyle.setFontFamilies({SkString("Roboto")});
7453 textStyle.setFontSize(14);
7454 textStyle.setColor(SK_ColorBLACK);
7455 textStyle.setFontStyle(SkFontStyle(SkFontStyle::kMedium_Weight, SkFontStyle::kNormal_Width,
7456 SkFontStyle::kUpright_Slant));
7457
7458 ParagraphBuilderImpl builder(paragraphStyle, fontCollection, get_unicode());
7459 builder.pushStyle(textStyle);
7460 builder.addText(text);
7461 builder.pop();
7462
7463 auto paragraph = builder.Build();
7464 paragraph->layout(std::numeric_limits<float>::max());
7465
7466 RectHeightStyle heightStyle = RectHeightStyle::kMax;
7467 RectWidthStyle widthStyle = RectWidthStyle::kMax;
7468 auto t1 = std::thread([&] {
7469 auto result = paragraph->getRectsForRange(0, 2, heightStyle, widthStyle);
7470 REPORTER_ASSERT(reporter, !result.empty());
7471 });
7472 auto t2 = std::thread([&] {
7473 auto result = paragraph->getRectsForRange(5, 10, heightStyle, widthStyle);
7474 REPORTER_ASSERT(reporter, !result.empty());
7475 });
7476 t1.join();
7477 t2.join();
7478 }
7479
7480 UNIX_ONLY_TEST(SkParagraph_GetRectsForRangeConcurrently, reporter) {
7481 auto const threads_count = 100;
7482 std::thread threads[threads_count];
7483 for (auto& thread : threads) {
7484 thread = std::thread(performGetRectsForRangeConcurrently, reporter);
7485 }
7486 for (auto& thread : threads) {
7487 thread.join();
7488 }
7489 }
7490
7491 UNIX_ONLY_TEST(SkParagraph_TabSubstitution, reporter) {
7492 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
7493 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7494
7495 TestCanvas canvas("SkParagraph_TabSubstitution.png");
7496
7497 ParagraphStyle paragraph_style;
7498 paragraph_style.setReplaceTabCharacters(true);
7499
7500 TextStyle text_style;
7501 text_style.setColor(SK_ColorBLACK);
7502 text_style.setFontFamilies({SkString("Roboto")});
7503 text_style.setFontSize(100);
7504
7505 ParagraphBuilderImpl builder1(paragraph_style, fontCollection, get_unicode());
7506 builder1.pushStyle(text_style);
7507 builder1.addText("There is a tab>\t<right here");
7508 auto paragraph1 = builder1.Build();
7509 paragraph1->layout(TestCanvasWidth);
7510 paragraph1->paint(canvas.get(), 0, 0);
7511
7512 paragraph_style.setReplaceTabCharacters(false);
7513 ParagraphBuilderImpl builder2(paragraph_style, fontCollection, get_unicode());
7514 builder2.pushStyle(text_style);
7515 builder2.addText("There is a tab>\t<right here");
7516 auto paragraph2 = builder2.Build();
7517 paragraph2->layout(TestCanvasWidth);
7518 paragraph2->paint(canvas.get(), 0, 0);
7519
7520 // Second paragraph has an unresolved \t (glyph == 0)
7521 REPORTER_ASSERT(reporter, ((ParagraphImpl*)paragraph1.get())->runs()[0].glyphs()[15] != 0);
7522 REPORTER_ASSERT(reporter, ((ParagraphImpl*)paragraph2.get())->runs()[0].glyphs()[15] == 0);
7523 // Notice, that the cache didn't work for the second paragraph - as it should not
7524 REPORTER_ASSERT(reporter, 2 == fontCollection->getParagraphCache()->count());
7525 }
7526
7527 UNIX_ONLY_TEST(SkParagraph_lineMetricsWithEllipsis, reporter) {
7528 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7529 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7530 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
7531 fontCollection->enableFontFallback();
7532
7533 ParagraphStyle paragraph_style;
7534 paragraph_style.setMaxLines(1);
7535 std::u16string ellipsis = u"\u2026";
7536 paragraph_style.setEllipsis(ellipsis);
7537
7538 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7539 builder.addText("hello");
7540
7541 auto paragraph = builder.Build();
7542 paragraph->layout(1.);
7543
7544 std::vector<LineMetrics> lm;
7545 paragraph->getLineMetrics(lm);
7546 REPORTER_ASSERT(reporter, lm.size() == 1);
7547 }
7548
7549 UNIX_ONLY_TEST(SkParagraph_lineMetricsAfterUpdate, reporter) {
7550 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7551 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7552 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
7553 fontCollection->enableFontFallback();
7554
7555 auto text = std::u16string(u"hello world");
7556
7557 ParagraphStyle paragraph_style;
7558
7559 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7560 builder.addText(text);
7561
7562 auto paragraph = builder.Build();
7563 paragraph->layout(200.);
7564
7565 std::vector<LineMetrics> lm;
7566 paragraph->getLineMetrics(lm);
7567 REPORTER_ASSERT(reporter, lm.size() == 1, "size: %zu", lm.size());
7568
7569 paragraph->updateFontSize(0, text.size(), 42);
7570 paragraph->layout(200.);
7571 paragraph->getLineMetrics(lm);
7572 REPORTER_ASSERT(reporter, lm.size() == 2, "size: %zu", lm.size());
7573 }
7574
7575 // Google logo is shown in one style (the first one)
7576 UNIX_ONLY_TEST(SkParagraph_MultiStyle_Logo, reporter) {
7577 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
7578 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7579
7580 TestCanvas canvas("SkParagraph_MultiStyle_Logo.png");
7581
7582 canvas.get()->drawColor(SK_ColorWHITE);
7583 SkScalar width = TestCanvasWidth;
7584 SkScalar height = TestCanvasHeight/2;
7585
7586 SkAutoCanvasRestore acr(canvas.get(), true);
7587 canvas.get()->clipRect(SkRect::MakeWH(width, height));
7588
7589 TextStyle style;
7590 style.setFontFamilies({SkString("Google Sans")});
7591 style.setFontSize(30);
7592
7593 TextStyle style0(style);
7594 style0.setDecoration(TextDecoration::kUnderline);
7595 style0.setDecorationColor(SK_ColorBLACK);
7596
7597 TextStyle style1(style);
7598 style1.setDecoration(TextDecoration::kOverline);
7599 style1.setDecorationColor(SK_ColorBLACK);
7600
7601 ParagraphStyle paraStyle;
7602 paraStyle.setTextStyle(style);
7603 paraStyle.setMaxLines(std::numeric_limits<size_t>::max());
7604
7605 const char* logo1 = "google_";
7606 const char* logo2 = "logo";
7607 const char* logo3 = "go";
7608 const char* logo4 = "ogle_logo";
7609 const char* logo5 = "google_lo";
7610 const char* logo6 = "go";
7611
7612 ParagraphBuilderImpl builder(paraStyle, fontCollection, get_unicode());
7613 style0.setDecorationStyle(TextDecorationStyle::kDouble);
7614 style0.setForegroundColor(SkPaint(SkColors::kBlack));
7615 style0.setBackgroundColor(SkPaint(SkColors::kLtGray));
7616 builder.pushStyle(style0);
7617 builder.addText(logo1, strlen(logo1));
7618 style1.setDecorationStyle(TextDecorationStyle::kWavy);
7619 style1.setForegroundColor(SkPaint(SkColors::kBlue));
7620 style1.setBackgroundColor(SkPaint(SkColors::kYellow));
7621 builder.pushStyle(style1);
7622 builder.addText(logo2, strlen(logo2));
7623 builder.addText(" ", 1);
7624
7625 style0.setDecorationStyle(TextDecorationStyle::kSolid);
7626 style0.setForegroundColor(SkPaint(SkColors::kBlue));
7627 style0.setBackgroundColor(SkPaint(SkColors::kWhite));
7628 builder.pushStyle(style0);
7629 builder.addText(logo3, strlen(logo3));
7630 style1.setDecorationStyle(TextDecorationStyle::kDotted);
7631 style1.setForegroundColor(SkPaint(SkColors::kBlack));
7632 style1.setBackgroundColor(SkPaint(SkColors::kMagenta));
7633 builder.pushStyle(style1);
7634 builder.addText(logo4, strlen(logo4));
7635 builder.addText(" ", 1);
7636
7637 style0.setDecorationStyle(TextDecorationStyle::kDashed);
7638 style0.setForegroundColor(SkPaint(SkColors::kGreen));
7639 style0.setBackgroundColor(SkPaint(SkColors::kGray));
7640 builder.pushStyle(style0);
7641 builder.addText(logo5, strlen(logo5));
7642 style1.setDecorationStyle(TextDecorationStyle::kDouble);
7643 style1.setForegroundColor(SkPaint(SkColors::kBlue));
7644 style1.setBackgroundColor(SkPaint(SkColors::kCyan));
7645 builder.pushStyle(style1);
7646 builder.addText(logo6, strlen(logo6));
7647
7648 auto paragraph = builder.Build();
7649 paragraph->layout(width - 40);
7650 paragraph->paint(canvas.get(), 20, 20);
7651
7652 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
7653 REPORTER_ASSERT(reporter, impl->lines().size() == 1);
7654
7655 size_t index = 0;
7656 SkScalar left = 0.0f;
7657 impl->lines().data()->scanStyles(StyleType::kDecorations,
7658 [&index, &left, reporter]
7659 (TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
7660 switch (index) {
7661 case 0: REPORTER_ASSERT(reporter, context.pos == 0 && context.size == 1);
7662 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fLeft, left));
7663 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fRight, 62.6944885f));
7664 break;
7665 case 1: REPORTER_ASSERT(reporter, context.pos == 0 && context.size == 2);
7666 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fLeft, left));
7667 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fRight, 105.479904f));
7668 break;
7669 case 2: REPORTER_ASSERT(reporter, context.pos == 2 && context.size == 1);
7670 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fLeft, left));
7671 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fRight, 123.3926165f));
7672 break;
7673 case 3: REPORTER_ASSERT(reporter, context.pos == 2 && context.size == 2);
7674 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fLeft, left));
7675 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fRight, 210.959808f));
7676 break;
7677 case 4: REPORTER_ASSERT(reporter, context.pos == 4 && context.size == 1);
7678 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fLeft, left));
7679 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fRight, 291.567017f));
7680 break;
7681 case 5: REPORTER_ASSERT(reporter, context.pos == 4 && context.size == 1); // No space at the end
7682 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fLeft, left));
7683 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(context.clip.fRight, 309.479736f));
7684 break;
7685 default: REPORTER_ASSERT(reporter, false); break;
7686 }
7687 left = context.clip.fRight;
7688 ++index;
7689 });
7690 }
7691
7692 // Ligature FFI should allow painting and querying by codepoints
7693 UNIX_ONLY_TEST(SkParagraph_MultiStyle_FFI, reporter) {
7694 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
7695 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7696
7697 TestCanvas canvas("SkParagraph_MultiStyle_FFI.png");
7698
7699 canvas.get()->drawColor(SK_ColorWHITE);
7700
7701 ParagraphStyle paragraph_style;
7702 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7703 TextStyle text_style;
7704 text_style.setColor(SK_ColorBLACK);
7705 text_style.setFontFamilies({SkString("Roboto")});
7706 text_style.setFontSize(60);
7707 text_style.setBackgroundColor(SkPaint(SkColors::kGray));
7708 builder.pushStyle(text_style);
7709 builder.addText("f");
7710 text_style.setBackgroundColor(SkPaint(SkColors::kYellow));
7711 builder.pushStyle(text_style);
7712 builder.addText("f");
7713 text_style.setBackgroundColor(SkPaint(SkColors::kLtGray));
7714 builder.pushStyle(text_style);
7715 builder.addText("i");
7716 auto paragraph = builder.Build();
7717 paragraph->layout(TestCanvasWidth);
7718 paragraph->paint(canvas.get(), 0, 0);
7719 auto width = paragraph->getLongestLine();
7720 auto height = paragraph->getHeight();
7721
7722 auto f1Pos = paragraph->getGlyphPositionAtCoordinate(width/6 - 5, height/2);
7723 auto f2Pos = paragraph->getGlyphPositionAtCoordinate(width/2 - 5, height/2);
7724 auto iPos = paragraph->getGlyphPositionAtCoordinate(width*5/6 - 5, height/2);
7725
7726 // Positions are aligned with graphemes (no pointing inside ffi grapheme)
7727 REPORTER_ASSERT(reporter, f1Pos.position == 0 && f1Pos.affinity == Affinity::kDownstream);
7728 REPORTER_ASSERT(reporter, f2Pos.position == 1 && f2Pos.affinity == Affinity::kDownstream);
7729 REPORTER_ASSERT(reporter, iPos.position == 2 && iPos.affinity == Affinity::kDownstream);
7730
7731 // Bounding boxes show the extact position (inside ffi grapheme)
7732 auto f1 = paragraph->getRectsForRange(0, 1, RectHeightStyle::kTight,
7733 RectWidthStyle::kTight);
7734 REPORTER_ASSERT(reporter, f1.size() == 1);
7735 REPORTER_ASSERT(reporter, f1[0].direction == TextDirection::kLtr);
7736 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f1[0].rect.fLeft, 0.000000f, EPSILON100));
7737 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f1[0].rect.fRight, 17.070000f, EPSILON100));
7738
7739 auto f2 = paragraph->getRectsForRange(1, 2, RectHeightStyle::kTight,
7740 RectWidthStyle::kTight);
7741 REPORTER_ASSERT(reporter, f2.size() == 1);
7742 REPORTER_ASSERT(reporter, f2[0].direction == TextDirection::kLtr);
7743 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f2[0].rect.fLeft, 17.070000f, EPSILON100));
7744 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(f2[0].rect.fRight, 34.139999f, EPSILON100));
7745
7746 auto fi = paragraph->getRectsForRange(2, 3, RectHeightStyle::kTight,
7747 RectWidthStyle::kTight);
7748 REPORTER_ASSERT(reporter, fi.size() == 1);
7749 REPORTER_ASSERT(reporter, fi[0].direction == TextDirection::kLtr);
7750 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(fi[0].rect.fLeft, 34.139999f, EPSILON100));
7751 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(fi[0].rect.fRight, 51.209999f, EPSILON100));
7752 }
7753
7754 // Multiple code points/single glyph emoji family should be treated as a single glyph
7755 UNIX_ONLY_TEST(SkParagraph_MultiStyle_EmojiFamily, reporter) {
7756 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
7757 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7758
7759 TestCanvas canvas("SkParagraph_MultiStyle_EmojiFamily.png");
7760
7761 canvas.get()->drawColor(SK_ColorWHITE);
7762
7763 ParagraphStyle paragraph_style;
7764 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7765 TextStyle text_style;
7766 text_style.setColor(SK_ColorBLACK);
7767 text_style.setFontFamilies({SkString("Noto Color Emoji")});
7768 text_style.setFontSize(40);
7769 builder.pushStyle(text_style);
7770 builder.addText(u"\U0001F468\u200D\U0001F469\u200D\U0001F467\u200D\U0001F466");
7771 auto paragraph = builder.Build();
7772 paragraph->layout(TestCanvasWidth);
7773 SkPaint paint;
7774 paint.setStyle(SkPaint::kStroke_Style);
7775 paint.setAntiAlias(true);
7776 paint.setStrokeWidth(1);
7777 paint.setColor(SK_ColorLTGRAY);
7778 canvas.get()->drawRect(SkRect::MakeXYWH(0, 0, paragraph->getLongestLine(), paragraph->getHeight()), paint);
7779 paragraph->paint(canvas.get(), 0, 0);
7780 auto width = paragraph->getLongestLine();
7781 auto height = paragraph->getHeight();
7782
7783 auto pos00 = paragraph->getGlyphPositionAtCoordinate(width/4, height/4);
7784 auto pos10 = paragraph->getGlyphPositionAtCoordinate(width*3/4, height/2);
7785 auto pos01 = paragraph->getGlyphPositionAtCoordinate(width/4, height/2);
7786 auto pos11 = paragraph->getGlyphPositionAtCoordinate(width*3/4, height*3/4);
7787
7788 // Positioning is aligned with the closest grapheme edge (left or right)
7789 REPORTER_ASSERT(reporter, pos00.position == 0 && pos00.affinity == Affinity::kDownstream);
7790 REPORTER_ASSERT(reporter, pos01.position == 0 && pos01.affinity == Affinity::kDownstream);
7791 REPORTER_ASSERT(reporter, pos10.position == 11 && pos10.affinity == Affinity::kUpstream);
7792 REPORTER_ASSERT(reporter, pos11.position == 11 && pos11.affinity == Affinity::kUpstream);
7793
7794 // Bounding boxes around a part of a grapheme are empty
7795 auto f1 = paragraph->getRectsForRange(0, 2, RectHeightStyle::kTight, RectWidthStyle::kTight);
7796 REPORTER_ASSERT(reporter, f1.size() == 0);
7797
7798 auto f2 = paragraph->getRectsForRange(4, 6, RectHeightStyle::kTight, RectWidthStyle::kTight);
7799 REPORTER_ASSERT(reporter, f2.size() == 0);
7800
7801 auto f3 = paragraph->getRectsForRange(8, 10, RectHeightStyle::kTight, RectWidthStyle::kTight);
7802 REPORTER_ASSERT(reporter, f3.size() == 0);
7803
7804 auto f4 = paragraph->getRectsForRange(8, 10, RectHeightStyle::kTight, RectWidthStyle::kTight);
7805 REPORTER_ASSERT(reporter, f4.size() == 0);
7806 }
7807
7808 // Arabic Ligature case should be painted into multi styles but queried as a single glyph
7809 UNIX_ONLY_TEST(SkParagraph_MultiStyle_Arabic, reporter) {
7810 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
7811 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7812
7813 TestCanvas canvas("SkParagraph_MultiStyle_Arabic.png");
7814
7815 canvas.get()->drawColor(SK_ColorWHITE);
7816
7817 TextStyle text_style;
7818 text_style.setFontFamilies({SkString("Noto Naskh Arabic")});
7819 text_style.setFontSize(50);
7820 text_style.setColor(SK_ColorBLACK);
7821 ParagraphStyle paragraph_style;
7822 paragraph_style.setTextStyle(text_style);
7823 paragraph_style.setTextDirection(TextDirection::kRtl);
7824 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7825 text_style.setColor(SK_ColorBLUE);
7826 builder.pushStyle(text_style);
7827 builder.addText("ك");
7828 text_style.setColor(SK_ColorRED);
7829 builder.pushStyle(text_style);
7830 builder.addText("ِّ");
7831 text_style.setColor(SK_ColorBLUE);
7832 builder.pushStyle(text_style);
7833 builder.addText("ـ");
7834 auto paragraph = builder.Build();
7835 paragraph->layout(TestCanvasWidth);
7836 paragraph->paint(canvas.get(), 0, 0);
7837
7838 auto width = paragraph->getLongestLine();
7839 auto height = paragraph->getHeight();
7840
7841 // Positioning is align with the grapheme
7842 auto f1Pos = paragraph->getGlyphPositionAtCoordinate(TestCanvasWidth - width*5/6, height/2);
7843 auto f2Pos = paragraph->getGlyphPositionAtCoordinate(TestCanvasWidth - width/3, height/2);
7844 auto iPos = paragraph->getGlyphPositionAtCoordinate(TestCanvasWidth - width/6, height/2);
7845 REPORTER_ASSERT(reporter, f1Pos.position == 4 && f1Pos.affinity == Affinity::kUpstream);
7846 REPORTER_ASSERT(reporter, f2Pos.position == 1 && f2Pos.affinity == Affinity::kUpstream);
7847 REPORTER_ASSERT(reporter, iPos.position == 0 && iPos.affinity == Affinity::kDownstream);
7848
7849 // Bounding boxes around a part of a grapheme are empty
7850 auto f1 = paragraph->getRectsForRange(0, 1, RectHeightStyle::kTight, RectWidthStyle::kTight);
7851 REPORTER_ASSERT(reporter, f1.size() == 0);
7852
7853 auto f2 = paragraph->getRectsForRange(1, 2, RectHeightStyle::kTight, RectWidthStyle::kTight);
7854 REPORTER_ASSERT(reporter, f2.size() == 0);
7855
7856 auto fi = paragraph->getRectsForRange(2, 3, RectHeightStyle::kTight, RectWidthStyle::kTight);
7857 REPORTER_ASSERT(reporter, fi.size() == 0);
7858 }
7859
7860 // Zalgo text should be painted into multi styles but queried as a single glyph
7861 UNIX_ONLY_TEST(SkParagraph_MultiStyle_Zalgo, reporter) {
7862 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
7863 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7864 NEED_SYSTEM_FONTS(fontCollection)
7865
7866 TestCanvas canvas("SkParagraph_MultiStyle_Zalgo.png");
7867
7868 canvas.get()->drawColor(SK_ColorWHITE);
7869
7870 //SkString text(">S͛ͭ̋͆̈̔̇͗̍͑̎ͪͮͧͣ̽ͫͣ́ͬ̀͌͑͂͗͒̍̔̄ͧ̏̉̌̊̊̿̀̌̃̄͐̓̓̚̚҉̵̡͜͟͝͠͏̸̵̡̧͜҉̷̡͇̜̘̻̺̘̟̝͙̬̘̩͇̭̼̥̖̤̦͎k͉̩̘͚̜̹̗̗͍̤̥̱͉̳͕͖̤̲̣͚̮̞̬̲͍͔̯̻̮̞̭͈̗̫͓̂ͨ̉ͪ̒͋͛̀̍͊ͧ̿̅͆̓̔̔ͬ̇̑̿ͩ͗ͮ̎͌̿̄ͅP̴̵̡̡̛̪͙̼̣̟̩̭̫̱͙̬͔͉͍̘̠͉̦̝̘̥̟̗͖̫̤͕̙̬̦͍̱̖̮̱͑͐̎̃̒͐͋̚͘͞a̶̶̵̵̵̶̶̡̧̢̢̺͔̣͖̭̺͍̤͚̱̜̰̥͕̬̥̲̞̥̘͇͚̺̰͚̪̺͔̤͍̓̿͆̎͋̓ͦ̈́ͦ̌́̄͗̌̓͌̕͜͜͟͢͝͡ŕ͎̝͕͉̻͎̤̭͚̗̳̖̙̘͚̫͖͓͚͉͔͈̟̰̟̬̗͓̟͚̱̕͡ͅͅͅa̸̶̢̛̛̽ͮͩ̅͒ͫ͗͂̎ͦ̈́̓̚͘͜͢͡҉̷̵̶̢̡̜̮̦̜̥̜̯̙͓͔̼̗̻͜͜ͅḡ̢̛͕̗͖̖̤̦̘͔ͨͨ̊͒ͩͭͤ̍̅̃ͪ̋̏̓̍̋͗̋ͨ̏̽̈́̔̀̋̉ͫ̅̂ͭͫ̏͒͋ͥ̚͜r̶̢̧̧̥̤̼̀̂̒ͪ͌̿͌̅͛ͨͪ͒̍ͥ̉ͤ̌̿̆́ͭ͆̃̒ͤ͛̊ͧ̽͘͝͠a̧̢̧̢͑͑̓͑ͮ̃͂̄͛́̈́͋̂͌̽̄͒̔́̇ͨͧͭ͐ͦ̋ͨ̍ͦ̍̋͆̔ͧ͑͋͌̈̓͛͛̚͢͜͜͏̴̢̧̛̳͍̹͚̰̹̻͔p̨̡͆ͦͣ͊̽̔͂̉ͣ̔ͣ̌̌̉̃̋̂͒ͫ̄̎̐͗̉̌̃̽̽́̀̚͘͜͟҉̱͉h̭̮̘̗͔̜̯͔͈̯̺͔̗̣̭͚̱̰̙̼̹͚̣̻̥̲̮͍̤͜͝<");
7871 std::u16string text16 = u">S\u035B\u036D\u030B\u0346\u0308\u0314\u0307\u0357\u030D\u0351\u030E\u036A\u036E\u0367\u0363\u033D\u036B\u0363\u0301\u036C\u0300\u034C\u0351\u0342\u0357\u0352\u030D\u0314\u0304\u0367\u030F\u031A\u0309\u030C\u030A\u030A\u033F\u0300\u030C\u0303\u0304\u0350\u0313\u031A\u0313\u0363\u0321\u035C\u035D\u035F\u0360\u0335\u034F\u0321\u0327\u0338\u035C\u0335\u0363\u0337\u0321\u0347\u031C\u0318\u033B\u033A\u0318\u031F\u031D\u0359\u032C\u0318\u0329\u0347\u032D\u033C\u0325\u0316\u0324\u0326\u034Ek\u0302\u0368\u0309\u036A\u0312\u034B\u035B\u0300\u030D\u034A\u0367\u033F\u0305\u0346\u0313\u0314\u0314\u036C\u0307\u0311\u033F\u0369\u0357\u036E\u030E\u034C\u033F\u0304\u0349\u0329\u0318\u035A\u031C\u0339\u0317\u0317\u034D\u0324\u0325\u0331\u0349\u0333\u0355\u0345\u0356\u0324\u0332\u0323\u035A\u032E\u031E\u032C\u0332\u034D\u0354\u032F\u033B\u032E\u031E\u032D\u0348\u0317\u032B\u0353P\u031A\u0351\u0350\u030E\u0303\u0312\u0350\u034B\u0334\u031B\u035E\u0358\u0321\u0335\u0321\u032A\u0359\u033C\u0323\u031F\u0329\u032D\u032B\u0331\u0359\u032C\u0354\u0349\u034D\u0318\u0320\u0349\u0326\u031D\u0318\u0325\u031F\u0317\u0356\u032B\u0324\u0355\u0319\u032C\u0326\u034D\u0331\u0316\u032E\u0331a\u0313\u033F\u0346\u030E\u034B\u0313\u0366\u0344\u0366\u030C\u0301\u0304\u0357\u030C\u0313\u034C\u035C\u0336\u035C\u0321\u0336\u035D\u0315\u0335\u0335\u0335\u035F\u0336\u0336\u0327\u0322\u0361\u0362\u0322\u033A\u0354\u0323\u0356\u032D\u033A\u034D\u0324\u035A\u0331\u031C\u0330\u0325\u0355\u032C\u0325\u0332\u031E\u0325\u0318\u0347\u035A\u033A\u0330\u035A\u032A\u033A\u0354\u0324\u034Dr\u0301\u0361\u0315\u034E\u031D\u0355\u0349\u033B\u034E\u0324\u0345\u0345\u032D\u035A\u0317\u0333\u0316\u0319\u0318\u035A\u0345\u032B\u0356\u0353\u035A\u0349\u0354\u0348\u031F\u0330\u031F\u032C\u0317\u0353\u031F\u035A\u0331a\u033D\u036E\u0369\u0305\u0352\u031A\u036B\u0357\u0342\u030E\u0366\u0344\u0343\u0338\u035C\u0361\u0322\u031B\u0358\u031B\u0362\u0336\u0363\u0337\u035C\u0322\u035C\u0321\u0335\u0336\u0345\u031C\u032E\u0326\u031C\u0325\u031C\u032F\u0319\u0353\u0354\u033C\u0317\u033Bg\u0304\u0368\u0368\u030A\u0352\u0369\u036D\u0364\u030D\u0305\u0303\u036A\u030B\u030F\u0313\u030D\u031A\u030B\u0357\u030B\u0368\u030F\u033D\u0344\u0314\u0300\u030B\u0309\u036B\u0305\u0302\u036D\u036B\u030F\u0352\u034B\u0365\u0322\u031B\u035C\u0355\u0317\u0356\u0316\u0324\u0326\u0318\u0354r\u0300\u0302\u0312\u036A\u034C\u033F\u034C\u0305\u035B\u0368\u036A\u0352\u030D\u0365\u0309\u0364\u030C\u033F\u0306\u0301\u036D\u0346\u0303\u0312\u0364\u035B\u030A\u0367\u033D\u035D\u0360\u0322\u0358\u0327\u0327\u0336\u0325\u0324\u033Ca\u0351\u0351\u0313\u0351\u036E\u0303\u0342\u0304\u035B\u0301\u0344\u034B\u0302\u034C\u033D\u0304\u0352\u0314\u0301\u0307\u0368\u0367\u036D\u0350\u0366\u031A\u030B\u0368\u030D\u0366\u030D\u030B\u0346\u0314\u0367\u0351\u034B\u034C\u0308\u0343\u035B\u035B\u0327\u0322\u0327\u0362\u035C\u035C\u0322\u034F\u0322\u031B\u0334\u0327\u0333\u034D\u0339\u035A\u0330\u0339\u033B\u0354p\u0346\u0366\u031A\u0363\u034A\u033D\u0314\u0342\u0309\u0363\u0314\u0363\u030C\u030C\u0309\u0303\u030B\u0302\u0352\u036B\u0304\u030E\u0310\u0357\u0309\u030C\u0303\u033D\u033D\u0328\u0341\u0358\u0340\u0321\u035C\u035F\u0363\u0331\u0349h\u035C\u035D\u032D\u032E\u0318\u0317\u0354\u031C\u032F\u0354\u0348\u032F\u033A\u0354\u0317\u0323\u032D\u035A\u0331\u0330\u0319\u033C\u0339\u035A\u0323\u033B\u0325\u0332\u032E\u034D\u0324<";
7872 SkString text = SkUnicode::convertUtf16ToUtf8(text16);
7873 auto K = text.find("k");
7874 auto P = text.find("P");
7875 auto h = text.find("h");
7876 ParagraphStyle paragraph_style;
7877 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7878 TextStyle text_style;
7879 text_style.setFontFamilies({SkString("Roboto")});
7880 text_style.setFontSize(20);
7881 text_style.setColor(SK_ColorRED);
7882 builder.pushStyle(text_style);
7883 builder.addText(text.data(), K + 3);
7884 text_style.setColor(SK_ColorBLUE);
7885 text_style.setBackgroundColor(SkPaint(SkColors::kYellow));
7886 builder.pushStyle(text_style);
7887 builder.addText(text.data() + K + 3, P - K - 3 + 6);
7888 text_style.setColor(SK_ColorGREEN);
7889 builder.pushStyle(text_style);
7890 builder.addText(text.data() + P + 6, h - P - 6);
7891 text_style.setColor(SK_ColorBLACK);
7892 text_style.setBackgroundColor(SkPaint(SkColors::kLtGray));
7893 builder.pushStyle(text_style);
7894 builder.addText(text.data() + h, text.size() - h);
7895 auto paragraph = builder.Build();
7896 paragraph->layout(TestCanvasWidth);
7897 paragraph->paint(canvas.get(), 0, paragraph->getHeight() / 2);
7898 //auto width = paragraph->getLongestLine();
7899 auto height = paragraph->getHeight();
7900
7901 auto resSK = paragraph->getRectsForRange(0, K, RectHeightStyle::kTight,
7902 RectWidthStyle::kTight);
7903 REPORTER_ASSERT(reporter, resSK.size() != 0);
7904 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(resSK[0].rect.width(), 10.45f, EPSILON100));
7905
7906 auto resKP = paragraph->getRectsForRange(K, P, RectHeightStyle::kTight,
7907 RectWidthStyle::kTight);
7908 REPORTER_ASSERT(reporter, resKP.size() != 0);
7909 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(resKP[0].rect.width(), 11.22f));
7910 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(resKP[0].rect.width(), 11.22f, EPSILON100));
7911
7912 auto resPh = paragraph->getRectsForRange(P, h, RectHeightStyle::kTight,
7913 RectWidthStyle::kTight);
7914 REPORTER_ASSERT(reporter, resPh.size() != 0);
7915 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(resPh[0].rect.width(), 67.26f, EPSILON20));
7916
7917 auto posK = paragraph->getGlyphPositionAtCoordinate(resSK.back().rect.fRight, height/2);
7918 auto posP = paragraph->getGlyphPositionAtCoordinate(resKP.back().rect.fRight, height/2);
7919 auto posH = paragraph->getGlyphPositionAtCoordinate(resPh.back().rect.fRight, height/2);
7920 REPORTER_ASSERT(reporter, posK.position == 148 && posP.position == 264 && posH.position == 572);
7921 }
7922
7923 // RTL Ellipsis
7924 UNIX_ONLY_TEST(SkParagraph_RtlEllipsis1, reporter) {
7925 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
7926 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7927
7928 TestCanvas canvas("SkParagraph_RtlEllipsis1.png");
7929
7930 canvas.get()->drawColor(SK_ColorWHITE);
7931
7932 TextStyle text_style;
7933 text_style.setFontFamilies({SkString("Noto Naskh Arabic"), SkString("Roboto")});
7934 text_style.setFontSize(100);
7935 text_style.setColor(SK_ColorBLACK);
7936 ParagraphStyle paragraph_style;
7937 paragraph_style.setTextStyle(text_style);
7938 paragraph_style.setTextDirection(TextDirection::kRtl);
7939 paragraph_style.setEllipsis(u"\u2026");
7940 paragraph_style.setTextAlign(TextAlign::kStart);
7941 paragraph_style.setMaxLines(1);
7942 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7943 builder.pushStyle(text_style);
7944 builder.addText(u"1 2 3 4 5 6 7 8 9");
7945 auto paragraph = builder.Build();
7946 paragraph->layout(474);
7947 paragraph->paint(canvas.get(), 0, 0);
7948 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
7949 REPORTER_ASSERT(reporter, paragraph->lineNumber() == 1);
7950 auto& line = impl->lines()[0];
7951 bool first = true;
7952 line.iterateThroughVisualRuns(true,
7953 [&]
7954 (const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) {
7955 REPORTER_ASSERT(reporter, first == (run->isEllipsis()));
7956 first = false;
7957 return true;
7958 });
7959 }
7960
7961 UNIX_ONLY_TEST(SkParagraph_RtlEllipsis2, reporter) {
7962 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>(true);
7963 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
7964
7965 TestCanvas canvas("SkParagraph_RtlEllipsis2.png");
7966
7967 canvas.get()->drawColor(SK_ColorWHITE);
7968
7969 TextStyle text_style;
7970 text_style.setFontFamilies({SkString("Noto Naskh Arabic"), SkString("Roboto")});
7971 text_style.setFontSize(100);
7972 text_style.setColor(SK_ColorBLACK);
7973 ParagraphStyle paragraph_style;
7974 paragraph_style.setTextStyle(text_style);
7975 paragraph_style.setTextDirection(TextDirection::kRtl);
7976 paragraph_style.setEllipsis(u"\u2026");
7977 paragraph_style.setTextAlign(TextAlign::kStart);
7978 paragraph_style.setMaxLines(2);
7979 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
7980 builder.pushStyle(text_style);
7981 builder.addText(u"تظاهرات و تجمعات اعتراضی در سراسر کشور ۲۳ مهر");
7982 auto paragraph = builder.Build();
7983 paragraph->layout(474);
7984 paragraph->paint(canvas.get(), 0, 0);
7985 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
7986 REPORTER_ASSERT(reporter, paragraph->lineNumber() == 2);
7987 auto& line = impl->lines()[1];
7988 bool first = true;
7989 line.iterateThroughVisualRuns(true,
7990 [&]
7991 (const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) {
7992 REPORTER_ASSERT(reporter, first == (run->isEllipsis()));
7993 first = false;
7994 return true;
7995 });
7996 }
7997
7998 static bool has_empty_typeface(SkFont f) {
7999 SkTypeface* face = f.getTypeface();
8000 if (!face) {
8001 return true; // Should be impossible, but just in case...
8002 }
8003 return face->countGlyphs() == 0 && face->getBounds().isEmpty();
8004 }
8005
8006 UNIX_ONLY_TEST(SkParagraph_TextEditingFunctionality, reporter) {
8007 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8008 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8009 TestCanvas canvas("SkParagraph_TextEditingFunctionality.png");
8010 const char* text =
8011 "This is a very long sentence to test if the text will properly wrap "
8012 "around and go to the next line. Sometimes, short sentence. Longer "
8013 "sentences are okay too because they are nessecary. Very short. "
8014 "This is a very long sentence to test if the text will properly wrap "
8015 "around and go to the next line. Sometimes, short sentence. Longer "
8016 "sentences are okay too because they are nessecary. Very short. ";
8017
8018 const size_t len = strlen(text);
8019
8020 ParagraphStyle paragraph_style;
8021 paragraph_style.setEllipsis(u"\u2026");
8022 paragraph_style.setMaxLines(3);
8023 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8024 TextStyle text_style;
8025 text_style.setFontFamilies({SkString("Roboto")});
8026 text_style.setFontSize(20);
8027 text_style.setColor(SK_ColorBLACK);
8028 builder.pushStyle(text_style);
8029 builder.addText(text, len);
8030 builder.pop();
8031
8032 auto paragraph = builder.Build();
8033 paragraph->layout(TestCanvasWidth);
8034 paragraph->paint(canvas.get(), 0, 0);
8035
8036 auto lineNumber = paragraph->getLineNumberAt(0);
8037 REPORTER_ASSERT(reporter, lineNumber == 0);
8038 lineNumber = paragraph->getLineNumberAt(len / 2);
8039 REPORTER_ASSERT(reporter, lineNumber == 1);
8040 lineNumber = paragraph->getLineNumberAt(len - 1);
8041 REPORTER_ASSERT(reporter, lineNumber == -1);
8042 lineNumber = paragraph->getLineNumberAt(len);
8043 REPORTER_ASSERT(reporter, lineNumber == -1);
8044 lineNumber = paragraph->getLineNumberAt(len + 10);
8045 REPORTER_ASSERT(reporter, lineNumber == -1);
8046
8047 LineMetrics lineMetrics;
8048 auto foundMetrics = paragraph->getLineMetricsAt(0, &lineMetrics);
8049 REPORTER_ASSERT(reporter, foundMetrics && lineMetrics.fLineNumber == 0);
8050 foundMetrics = paragraph->getLineMetricsAt(1, &lineMetrics);
8051 REPORTER_ASSERT(reporter, foundMetrics && lineMetrics.fLineNumber == 1);
8052 foundMetrics = paragraph->getLineMetricsAt(3, &lineMetrics);
8053 REPORTER_ASSERT(reporter, !foundMetrics);
8054 foundMetrics = paragraph->getLineMetricsAt(10, &lineMetrics);
8055 REPORTER_ASSERT(reporter, !foundMetrics);
8056
8057 std::vector<LineMetrics> metrics;
8058 paragraph->getLineMetrics(metrics);
8059 auto actualText = paragraph->getActualTextRange(0, false);
8060 REPORTER_ASSERT(reporter, actualText.end == metrics[0].fEndExcludingWhitespaces);
8061 actualText = paragraph->getActualTextRange(1, false);
8062 REPORTER_ASSERT(reporter, actualText.end == metrics[1].fEndExcludingWhitespaces);
8063 actualText = paragraph->getActualTextRange(2, false);
8064 REPORTER_ASSERT(reporter, actualText.end == metrics[2].fEndExcludingWhitespaces);
8065
8066 Paragraph::GlyphClusterInfo glyphInfo;
8067 auto foundCluster = paragraph->getGlyphClusterAt(0, &glyphInfo);
8068 REPORTER_ASSERT(reporter, foundCluster && glyphInfo.fClusterTextRange.start == 0);
8069 foundCluster = paragraph->getGlyphClusterAt(len / 2, &glyphInfo);
8070 REPORTER_ASSERT(reporter, foundCluster && glyphInfo.fClusterTextRange.start == len / 2);
8071 foundCluster = paragraph->getGlyphClusterAt(len, &glyphInfo);
8072 REPORTER_ASSERT(reporter, !foundCluster);
8073
8074 auto foundClosest = paragraph->getClosestGlyphClusterAt(0, 10, &glyphInfo);
8075 REPORTER_ASSERT(reporter, foundClosest && glyphInfo.fClusterTextRange.start == 0 &&
8076 glyphInfo.fClusterTextRange.end == 1);
8077 foundClosest = paragraph->getClosestGlyphClusterAt(TestCanvasWidth / 2, 20, &glyphInfo);
8078 REPORTER_ASSERT(reporter, foundClosest && glyphInfo.fClusterTextRange.start == 61 &&
8079 glyphInfo.fClusterTextRange.end == 62);
8080 foundClosest = paragraph->getClosestGlyphClusterAt(TestCanvasWidth + 10, 30, &glyphInfo);
8081
8082 REPORTER_ASSERT(reporter, foundClosest && glyphInfo.fClusterTextRange.start == 228 &&
8083 glyphInfo.fClusterTextRange.end == 229);
8084
8085 auto font = paragraph->getFontAt(10);
8086 REPORTER_ASSERT(reporter, !has_empty_typeface(font));
8087 SkString fontFamily;
8088 font.getTypeface()->getFamilyName(&fontFamily);
8089 REPORTER_ASSERT(reporter, fontFamily.equals("Roboto"));
8090
8091 auto fonts = paragraph->getFonts();
8092 REPORTER_ASSERT(reporter, fonts.size() == 1);
8093 REPORTER_ASSERT(reporter, fonts[0].fTextRange.start == 0 && fonts[0].fTextRange.end == len);
8094 REPORTER_ASSERT(reporter, !has_empty_typeface(fonts[0].fFont));
8095 font.getTypeface()->getFamilyName(&fontFamily);
8096 REPORTER_ASSERT(reporter, fontFamily.equals("Roboto"));
8097 }
8098
8099 UNIX_ONLY_TEST(SkParagraph_getLineNumberAt_Ellipsis, reporter) {
8100 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8101 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8102 fontCollection->setDefaultFontManager(ToolUtils::TestFontMgr());
8103 TestCanvas canvas("SkParagraph_Ellipsis.png");
8104
8105 // The second line will be ellipsized. The 10th glyph ("0") will be replaced
8106 // by U+2026.
8107 const char* text = "This\n" // [0, 5)
8108 "1234567890ABCD"; // [5, len)
8109
8110 const size_t len = strlen(text);
8111
8112 ParagraphStyle paragraph_style;
8113 paragraph_style.setEllipsis(u"\u2026");
8114 paragraph_style.setMaxLines(2);
8115
8116 TextStyle text_style;
8117 text_style.setFontFamilies({SkString("Ahem")});
8118 text_style.setColor(SK_ColorBLACK);
8119 text_style.setFontSize(10);
8120
8121 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8122 builder.pushStyle(text_style);
8123 builder.addText(text, len);
8124 builder.pop();
8125 auto paragraph = builder.Build();
8126
8127 // Roughly 10 characters wide.
8128 paragraph->layout(100);
8129
8130 REPORTER_ASSERT(reporter, paragraph->getLineNumberAt(0) == 0);
8131 REPORTER_ASSERT(reporter, paragraph->getLineNumberAt(4) == 0);
8132 REPORTER_ASSERT(reporter, paragraph->getLineNumberAt(5) == 1);
8133 REPORTER_ASSERT(reporter, paragraph->getLineNumberAt(len) == -1);
8134 REPORTER_ASSERT(reporter, paragraph->getLineNumberAt(len - 1) == -1);
8135 // "0" should be ellipsized away so the call return -1 instead of 1.
8136 REPORTER_ASSERT(reporter, paragraph->getLineNumberAt(14) == -1);
8137 }
8138
8139 UNIX_ONLY_TEST(SkParagraph_API_USES_UTF16, reporter) {
8140 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8141 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8142 // Each of these characters consists of 3 UTF-8 code points, or 1 UTF-16
8143 // code point.
8144 const char* text = "一丁丂七";
8145 const size_t len = strlen(text);
8146
8147 ParagraphStyle paragraph_style;
8148 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8149 TextStyle text_style;
8150 text_style.setFontFamilies({SkString("Roboto")});
8151 text_style.setFontSize(20);
8152 text_style.setColor(SK_ColorBLACK);
8153 builder.pushStyle(text_style);
8154 builder.addText(text, len);
8155 builder.pop();
8156
8157 auto paragraph = builder.Build();
8158 paragraph->layout(TestCanvasWidth);
8159
8160 REPORTER_ASSERT(reporter, !has_empty_typeface(paragraph->getFontAtUTF16Offset(0)));
8161 REPORTER_ASSERT(reporter, has_empty_typeface(paragraph->getFontAtUTF16Offset(4)));
8162
8163 REPORTER_ASSERT(reporter, paragraph->getGlyphInfoAtUTF16Offset(0, nullptr));
8164 REPORTER_ASSERT(reporter, !paragraph->getGlyphInfoAtUTF16Offset(4, nullptr));
8165 // Verify the output also uses UTF-16
8166 Paragraph::GlyphInfo glyphInfo;
8167 REPORTER_ASSERT(reporter, paragraph->getGlyphInfoAtUTF16Offset(3, &glyphInfo));
8168 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.start == 3);
8169 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.end == 4);
8170
8171 REPORTER_ASSERT(reporter, paragraph->getLineNumberAtUTF16Offset(0) == 0);
8172 REPORTER_ASSERT(reporter, paragraph->getLineNumberAtUTF16Offset(4) == -1);
8173
8174 // The last logical character ("七") is considered hit.
8175 REPORTER_ASSERT(reporter, paragraph->getClosestUTF16GlyphInfoAt(999.0f, 999.0f, &glyphInfo));
8176 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.start == 3);
8177 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.end == 4);
8178
8179 // The first logical character ("一") is considered hit.
8180 REPORTER_ASSERT(reporter, paragraph->getClosestUTF16GlyphInfoAt(-999.0f, 0.0f, &glyphInfo));
8181 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.start == 0);
8182 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.end == 1);
8183 }
8184
8185 UNIX_ONLY_TEST(SkParagraph_Empty_Paragraph_Metrics, reporter) {
8186 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8187 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8188 const char* text = "";
8189
8190 ParagraphStyle paragraph_style;
8191 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8192 TextStyle text_style;
8193 text_style.setFontFamilies({SkString("Roboto")});
8194 text_style.setFontSize(20);
8195 text_style.setColor(SK_ColorBLACK);
8196 builder.pushStyle(text_style);
8197 builder.addText(text, 0);
8198 builder.pop();
8199
8200 auto paragraph = builder.Build();
8201 paragraph->layout(TestCanvasWidth);
8202
8203 REPORTER_ASSERT(reporter, has_empty_typeface(paragraph->getFontAt(0)));
8204 REPORTER_ASSERT(reporter, !paragraph->getGlyphClusterAt(0, nullptr));
8205 REPORTER_ASSERT(reporter, paragraph->getLineNumberAt(0) == -1);
8206 REPORTER_ASSERT(reporter, !paragraph->getClosestGlyphClusterAt(10.0, 5.0, nullptr));
8207 }
8208
8209 UNIX_ONLY_TEST(SkParagraph_GlyphCluster_Ligature, reporter) {
8210 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8211 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8212 const char* text = "fi";
8213 const size_t len = strlen(text);
8214
8215 ParagraphStyle paragraph_style;
8216 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8217 TextStyle text_style;
8218 text_style.setFontFamilies({SkString("Roboto")});
8219 text_style.setFontSize(20);
8220 text_style.setColor(SK_ColorBLACK);
8221 builder.pushStyle(text_style);
8222 builder.addText(text, len);
8223 builder.pop();
8224
8225 auto paragraph = builder.Build();
8226 paragraph->layout(TestCanvasWidth);
8227
8228 Paragraph::GlyphClusterInfo glyphInfo;
8229 REPORTER_ASSERT(reporter, paragraph->getGlyphClusterAt(0, &glyphInfo));
8230 REPORTER_ASSERT(reporter, glyphInfo.fClusterTextRange.start == 0);
8231 REPORTER_ASSERT(reporter, glyphInfo.fClusterTextRange.end == len);
8232 }
8233
8234 UNIX_ONLY_TEST(SkParagraph_GlyphInfo_LigatureDiacritics, reporter) {
8235 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8236 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8237 // Ligature + diacritics
8238 // text = lam + hamza + alef + hamza
8239 // lam and alef form a laam-alif ligature and the 2 hamza are diacritical
8240 // marks the combines into the ligated base glyphs.
8241 const char* text = "لٔأ";
8242 const size_t len = strlen(text);
8243
8244 ParagraphStyle paragraph_style;
8245 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8246 TextStyle text_style;
8247 text_style.setFontFamilies({SkString("Katibeh")});
8248 text_style.setFontSize(100);
8249 text_style.setColor(SK_ColorBLACK);
8250 builder.pushStyle(text_style);
8251 builder.addText(text, len);
8252 builder.pop();
8253
8254 auto paragraph = builder.Build();
8255 paragraph->layout(TestCanvasWidth);
8256 TestCanvas canvas("SkParagraph_laam_alif_diacritics.png");
8257 paragraph->paint(canvas.get(), 50, 50);
8258
8259 Paragraph::GlyphInfo glyphInfo;
8260
8261 const auto boxes = paragraph->getRectsForRange(0, len, RectHeightStyle::kTight, RectWidthStyle::kTight);
8262 REPORTER_ASSERT(reporter, boxes.size() == 1);
8263 const SkRect fullRect = boxes[0].rect;
8264
8265 REPORTER_ASSERT(reporter, paragraph->getGlyphInfoAtUTF16Offset(0, &glyphInfo));
8266 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.start == 0);
8267 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.end == 2);
8268 const SkRect rect0 = glyphInfo.fGraphemeLayoutBounds;
8269
8270 REPORTER_ASSERT(reporter, paragraph->getGlyphInfoAtUTF16Offset(1, &glyphInfo));
8271 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.start == 0);
8272 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.end == 2);
8273 const SkRect rect1 = glyphInfo.fGraphemeLayoutBounds;
8274 REPORTER_ASSERT(reporter, rect0 == rect1);
8275
8276 REPORTER_ASSERT(reporter, paragraph->getGlyphInfoAtUTF16Offset(2, &glyphInfo));
8277 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.start == 2);
8278 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.end == 4);
8279 const SkRect rect2 = glyphInfo.fGraphemeLayoutBounds;
8280 // The latter half of the text forms a different grapheme.
8281 REPORTER_ASSERT(reporter, rect2 != rect1);
8282
8283 REPORTER_ASSERT(reporter, paragraph->getGlyphInfoAtUTF16Offset(3, &glyphInfo));
8284 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.start == 2);
8285 REPORTER_ASSERT(reporter, glyphInfo.fGraphemeClusterTextRange.end == 4);
8286 const SkRect rect3 = glyphInfo.fGraphemeLayoutBounds;
8287 REPORTER_ASSERT(reporter, rect2 == rect3);
8288
8289 // RTL text.
8290 REPORTER_ASSERT(reporter, fullRect.left() == rect2.left());
8291 REPORTER_ASSERT(reporter, fullRect.right() == rect0.right());
8292
8293 // Currently it seems the 2 grapheme rects do not overlap each other.
8294 // REPORTER_ASSERT(reporter, rect2.right() < rect0.left());
8295 }
8296
8297 UNIX_ONLY_TEST(SkParagraph_SingleDummyPlaceholder, reporter) {
8298 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8299 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8300 const char* text = "Single dummy placeholder";
8301 const size_t len = strlen(text);
8302
8303 ParagraphStyle paragraph_style;
8304 paragraph_style.turnHintingOff();
8305 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8306
8307 TextStyle text_style;
8308 text_style.setFontFamilies({SkString("Roboto")});
8309 text_style.setColor(SK_ColorBLACK);
8310 builder.pushStyle(text_style);
8311 builder.addText(text, len);
8312
8313 auto paragraph = builder.Build();
8314 paragraph->layout(TestCanvasWidth);
8315
8316 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
8317 REPORTER_ASSERT(reporter, impl->placeholders().size() == 1);
8318
8319 size_t index = 0;
8320 for (auto& line : impl->lines()) {
8321 line.scanStyles(StyleType::kDecorations,
8322 [&index, reporter]
8323 (TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context) {
8324 REPORTER_ASSERT(reporter, index == 0);
8325 REPORTER_ASSERT(reporter, style.getColor() == SK_ColorBLACK);
8326 ++index;
8327 });
8328 }
8329 }
8330
8331 UNIX_ONLY_TEST(SkParagraph_EndWithLineSeparator, reporter) {
8332 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8333 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8334
8335 const char* text = "A text ending with line separator.\u2028";
8336 const size_t len = strlen(text);
8337
8338 ParagraphStyle paragraph_style;
8339
8340 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8341 TextStyle textStyle;
8342 textStyle.setFontFamilies({SkString("Roboto")});
8343 builder.pushStyle(textStyle);
8344 builder.addText(text, len);
8345
8346 auto paragraph = builder.Build();
8347 paragraph->layout(SK_ScalarMax);
8348
8349 int visitedCount = 0;
8350 paragraph->visit([&visitedCount, reporter](int lineNumber, const Paragraph::VisitorInfo* info) {
8351 visitedCount++;
8352 if (lineNumber == 1) {
8353 // Visitor for second line created from line separator should only be called for 'end of line'.
8354 // 'end of line' is denoted by 'info' being nullptr.
8355 REPORTER_ASSERT(reporter, info == nullptr);
8356 }
8357 });
8358 REPORTER_ASSERT(reporter, visitedCount == 3, "visitedCount: %d", visitedCount);
8359 }
8360
8361 [[maybe_unused]] static void SkParagraph_EmojiFontResolution(sk_sp<SkUnicode> icu, skiatest::Reporter* reporter) {
8362 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8363 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8364
8365 const char* text = "♻️🏴󠁧󠁢󠁳󠁣󠁴󠁿";
8366 const char* text1 = "♻️";
8367 const size_t len = strlen(text);
8368 const size_t len1 = strlen(text1);
8369
8370 ParagraphStyle paragraph_style;
8371 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8372 TextStyle text_style;
8373 text_style.setFontFamilies({SkString("")});
8374 builder.pushStyle(text_style);
8375 builder.addText(text, len);
8376 auto paragraph = builder.Build();
8377 paragraph->layout(SK_ScalarMax);
8378
8379 auto impl = static_cast<ParagraphImpl*>(paragraph.get());
8380 REPORTER_ASSERT(reporter, impl->runs().size() == 1, "size: %zu", impl->runs().size());
8381
8382 ParagraphBuilderImpl builder1(paragraph_style, fontCollection, get_unicode());
8383 builder1.pushStyle(text_style);
8384 builder1.addText(text1, len1);
8385 auto paragraph1 = builder1.Build();
8386 paragraph1->layout(SK_ScalarMax);
8387
8388 auto impl1 = static_cast<ParagraphImpl*>(paragraph1.get());
8389 REPORTER_ASSERT(reporter, impl1->runs().size() == 1, "size: %zu", impl1->runs().size());
8390 if (impl1->runs().size() == 1) {
8391 SkString ff;
8392 impl->run(0).font().getTypeface()->getFamilyName(&ff);
8393 SkString ff1;
8394 impl1->run(0).font().getTypeface()->getFamilyName(&ff1);
8395 REPORTER_ASSERT(reporter, ff.equals(ff1));
8396 }
8397 }
8398
8399 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
8400 UNIX_ONLY_TEST(SkParagraph_ICU_EmojiFontResolution, reporter) {
8401 SkParagraph_EmojiFontResolution(SkUnicodes::ICU::Make(), reporter);
8402 }
8403 #endif
8404
8405 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
8406 UNIX_ONLY_TEST(SkParagraph_ICU4X_EmojiFontResolution, reporter) {
8407 SkParagraph_EmojiFontResolution(SkUnicodes::ICU4X::Make(), reporter);
8408 }
8409 #endif
8410
8411 [[maybe_unused]] static void SkUnicode_Emoji(sk_sp<SkUnicode> icu, skiatest::Reporter* reporter) {
8412 auto test = [&](const char* text, SkUnichar expected) {
8413 SkString str(text);
8414 if ((false)) {
8415 SkDebugf("'%s'\n", text);
8416 const char* begin = str.data();
8417 const char* end = str.data() + str.size();
8418 while (begin != end) {
8419 auto unicode = SkUTF::NextUTF8WithReplacement(&begin, end);
8420 SkDebugf(" %d: %s %s\n", unicode,
8421 icu->isEmoji(unicode) ? "isEmoji" : "",
8422 icu->isEmojiComponent(unicode) ? "isEmojiComponent" : ""
8423 );
8424 }
8425
8426 SkDebugf("Graphemes:");
8427 skia_private::TArray<SkUnicode::CodeUnitFlags, true> codeUnitProperties;
8428 icu->computeCodeUnitFlags(str.data(), str.size(), false, &codeUnitProperties);
8429 int index = 0;
8430 for (auto& cp : codeUnitProperties) {
8431 if (SkUnicode::hasGraphemeStartFlag(cp)) {
8432 SkDebugf(" %d", index);
8433 }
8434 ++index;
8435 }
8436 SkDebugf("\n");
8437 }
8438
8439 SkSpan<const char> textSpan(str.data(), str.size());
8440 const char* begin = str.data();
8441 const char* end = begin + str.size();
8442 auto emojiStart = OneLineShaper::getEmojiSequenceStart(icu.get(), &begin, end);
8443 REPORTER_ASSERT(reporter, expected == emojiStart);
8444 };
8445
8446 test("", -1);
8447 test("0", -1);
8448 test("2nd", -1);
8449 test("99", -1);
8450 test("0️⃣", 48);
8451 test("0️⃣12", 48);
8452 test("#", -1);
8453 test("#️⃣", 35);
8454 test("#️⃣#", 35);
8455 test("#️⃣#️⃣", 35);
8456 test("*", -1);
8457 test("*️⃣", 42);
8458 test("*️⃣abc", 42);
8459 test("*️⃣😊", 42);
8460 test("😊", 128522);
8461 test("😊abc", 128522);
8462 test("😊*️⃣",128522);
8463 test("👨‍👩‍👦‍👦", 128104);
8464
8465 // These 2 have emoji components as the first codepoint
8466 test("🇷🇺", 127479); // Flag sequence
8467 test("0️⃣", 48); // Keycap sequence
8468
8469 // These have a simple emoji as a first codepoint
8470 test("🏴󠁧󠁢󠁥󠁮󠁧󠁿", 127988); // Tag sequence
8471 test("👋🏼", 128075); // Modifier sequence
8472 test("👨‍👩‍👧‍👦", 128104); // ZWJ sequence
8473 }
8474
8475 // Checked: disabled for TxtLib
8476 UNIX_ONLY_TEST(SkParagraph_ArabicMeansNoLetterSpacing, reporter) {
8477 sk_sp<ResourceFontCollection> fontCollection = sk_make_sp<ResourceFontCollection>();
8478 SKIP_IF_FONTS_NOT_FOUND(reporter, fontCollection)
8479 TestCanvas canvas("SkParagraph_ArabicParagraph.png");
8480 const char* arabic = "سلام";
8481 const char* english = "Hello";
8482
8483 ParagraphStyle paragraph_style;
8484 TextStyle text_style;
8485 text_style.setFontSize(10);
8486 text_style.setColor(SK_ColorBLACK);
8487
8488 auto layout = [&](const char* familyName, const char* text, double letterSpacing) -> double {
8489 ParagraphBuilderImpl builder(paragraph_style, fontCollection, get_unicode());
8490 text_style.setLetterSpacing(letterSpacing);
8491 text_style.setFontFamilies({SkString(familyName)});
8492 builder.pushStyle(text_style);
8493 builder.addText(text, strlen(text));
8494 builder.pop();
8495
8496 auto paragraph = builder.Build();
8497 paragraph->layout(SK_ScalarInfinity);
8498 return paragraph->getLongestLine();
8499 };
8500
8501 { // Letter spacing does not affect the layout for Arabic
8502 auto noLetterSpacing = layout("Katibeh", arabic, 0.0);
8503 auto withLetterSpacing = layout("Katibeh", arabic, 100.0);
8504 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(noLetterSpacing, withLetterSpacing, EPSILON1000));
8505 }
8506 {
8507 // Letter spacing affects English text (number of letter * letter spacing)
8508 auto noLetterSpacing = layout("Roboto", english, 0.0);
8509 auto withLetterSpacing = layout("Roboto", english, 100.0);
8510 REPORTER_ASSERT(reporter, SkScalarNearlyEqual((withLetterSpacing - noLetterSpacing), 100.0 * strlen(english), EPSILON1000));
8511 }
8512 }
8513
8514 #if defined(SK_UNICODE_ICU_IMPLEMENTATION)
8515 UNIX_ONLY_TEST(SkParagraph_ICU_EmojiRuns, reporter) {
8516 SkUnicode_Emoji(SkUnicodes::ICU::Make(), reporter);
8517 }
8518 #endif
8519
8520 #if defined(SK_UNICODE_ICU4X_IMPLEMENTATION)
8521 UNIX_ONLY_TEST(SkParagraph_ICU4X_EmojiRuns, reporter) {
8522 SkUnicode_Emoji(SkUnicodes::ICU4X::Make(), reporter);
8523 }
8524 #endif