1 // Copyright 2021 Google LLC.
2 #include "include/core/SkBitmap.h"
3 #include "include/core/SkCanvas.h"
4 #include "include/core/SkColor.h"
5 #include "include/core/SkEncodedImageFormat.h"
6 #include "include/core/SkFontMgr.h"
7 #include "include/core/SkFontStyle.h"
8 #include "include/core/SkImageEncoder.h"
9 #include "include/core/SkPaint.h"
10 #include "include/core/SkPoint.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkSpan.h"
15 #include "include/core/SkStream.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTypeface.h"
18 #include "include/core/SkTypes.h"
19 #include "tests/Test.h"
20 #include "tools/Resources.h"
21
22 #include "experimental/sktext/include/Text.h"
23
24 #include <string.h>
25 #include <algorithm>
26 #include <limits>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31
32 struct GrContextOptions;
33
34 #define VeryLongCanvasWidth 1000000
35 #define TestCanvasWidth 1000
36 #define TestCanvasHeight 600
37
38 using namespace skia::text;
39
40 namespace {
operator ==(SkSpan<const char16_t> a,SkSpan<const char16_t> b)41 bool operator==(SkSpan<const char16_t> a, SkSpan<const char16_t> b) {
42 if (a.size() != b.size()) {
43 return false;
44 }
45 for (size_t i = 0; i < a.size(); ++i) {
46 if (a[i] != b[i]) {
47 return false;
48 }
49 }
50 return true;
51 }
52 }
53
UNIX_ONLY_TEST(SkText_UnicodeText_Flags,reporter)54 UNIX_ONLY_TEST(SkText_UnicodeText_Flags, reporter) {
55 REPORTER_ASSERT(reporter, true);
56 // 01234567890 1234567890
57 std::u16string utf16(u"Hello word\nHello world");
58 SkString utf8("Hello word\nHello world");
59 UnicodeText unicodeText16(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
60 UnicodeText unicodeText8(SkUnicode::Make(), utf8);
61
62 REPORTER_ASSERT(reporter, unicodeText16.getText16() == unicodeText8.getText16(), "UTF16 and UTF8 texts should be the same\n");
63 auto lineBreak = utf16.find_first_of(u"\n");
64 for (size_t i = 0; i < unicodeText16.getText16().size(); ++i) {
65 if (i == lineBreak) {
66 REPORTER_ASSERT(reporter, unicodeText16.hasProperty(i, CodeUnitFlags::kHardLineBreakBefore), "Pos16 %d should point to hard line break\n", lineBreak);
67 REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(i, CodeUnitFlags::kHardLineBreakBefore), "Pos8 %d should point to hard line break\n", lineBreak);
68 } else {
69 REPORTER_ASSERT(reporter, unicodeText16.hasProperty(i, CodeUnitFlags::kGraphemeStart), "Pos16 %d should be a grapheme start\n", i);
70 REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(i, CodeUnitFlags::kGraphemeStart), "Pos8 %d should be a grapheme start\n", i);
71 }
72 }
73
74 auto space1 = utf16.find_first_of(u" ");
75 auto space2 = utf16.find_last_of(u" ");
76
77 REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space1, CodeUnitFlags::kPartOfWhiteSpace), "Pos16 %d should be a part of whitespaces\n", space1);
78 REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space1 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos16 %d should have soft line break before\n", space1 + 1);
79 REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space2, CodeUnitFlags::kPartOfWhiteSpace), "Pos16 %d should be a part of whitespaces\n", space2);
80 REPORTER_ASSERT(reporter, unicodeText16.hasProperty(space2 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos16 %d should have soft line break before\n", space2 + 1);
81
82 REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space1, CodeUnitFlags::kPartOfWhiteSpace), "Pos8 %d should be a part of whitespaces\n", space1);
83 REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space1 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos8 %d should have soft line break before\n", space1 + 1);
84 REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space2, CodeUnitFlags::kPartOfWhiteSpace), "Pos8 %d should be a part of whitespaces\n", space2);
85 REPORTER_ASSERT(reporter, unicodeText8 .hasProperty(space2 + 1, CodeUnitFlags::kSoftLineBreakBefore), "Pos8 %d should have soft line break before\n", space2 + 1);
86 }
87
88 // TODO: Test RTL text
89