• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "experimental/sktext/src/Paint.h"
24 
25 #include <string.h>
26 #include <algorithm>
27 #include <limits>
28 #include <memory>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 struct GrContextOptions;
34 
35 #define VeryLongCanvasWidth 1000000
36 #define TestCanvasWidth 1000
37 #define TestCanvasHeight 600
38 
39 using namespace skia::text;
40 
41 struct TestLine {
42     size_t index;
43     TextRange lineText;
44     bool hardBreak;
45     SkRect bounds;
46     GlyphRange trailingSpaces;
47     Range<RunIndex> runRange;
48     size_t glyphCount;
49 };
50 
51 struct TestRun {
52     const SkFont& font;
53     DirTextRange dirTextRange;  // Currently we make sure that the run edges are the grapheme cluster edges
54     SkRect bounds;              // bounds contains the physical boundaries of the run
55     size_t trailingSpaces;      // Depending of TextDirection it goes right to the end (LTR) or left to the start (RTL)
56     SkSpan<const uint16_t> glyphs;
57     SkSpan<const SkPoint> positions;
58     SkSpan<const TextIndex> clusters;
59 };
60 
61 class TestVisitor : public Visitor {
62 public:
onBeginLine(size_t index,TextRange lineText,bool hardBreak,SkRect bounds)63     void onBeginLine(size_t index, TextRange lineText, bool hardBreak, SkRect bounds) override {
64         SkASSERT(fTestLines.size() == index);
65         fTestLines.push_back({ index, lineText, hardBreak, bounds, EMPTY_RANGE, Range<RunIndex>(fTestRuns.size(), fTestRuns.size()), 0 });
66     }
onEndLine(size_t index,TextRange lineText,GlyphRange trailingSpaces,size_t glyphCount)67     void onEndLine(size_t index, TextRange lineText, GlyphRange trailingSpaces, size_t glyphCount) override {
68         SkASSERT(fTestLines.size() == index + 1);
69         fTestLines.back().trailingSpaces = trailingSpaces;
70         fTestLines.back().runRange.fEnd = fTestRuns.size();
71         fTestLines.back().glyphCount = glyphCount;
72     }
onGlyphRun(const SkFont & font,DirTextRange dirTextRange,SkRect bounds,TextIndex trailingSpaces,size_t glyphCount,const uint16_t glyphs[],const SkPoint positions[],const TextIndex clusters[])73     void onGlyphRun(const SkFont& font,
74                     DirTextRange dirTextRange,
75                     SkRect bounds,
76                     TextIndex trailingSpaces,
77                     size_t glyphCount,          // Just the number of glyphs
78                     const uint16_t glyphs[],
79                     const SkPoint positions[],        // Positions relative to the line
80                     const TextIndex clusters[]) override
81     {
82         fTestRuns.push_back({font, dirTextRange, bounds, trailingSpaces,
83                             SkSpan<const uint16_t>(&glyphs[0], glyphCount),
84                             SkSpan<const SkPoint>(&positions[0], glyphCount + 1),
85                             SkSpan<const TextIndex>(&clusters[0], glyphCount + 1),
86                             });
87     }
onPlaceholder(TextRange,const SkRect & bounds)88     void onPlaceholder(TextRange, const SkRect& bounds) override { }
89 
90     std::vector<TestLine> fTestLines;
91     std::vector<TestRun> fTestRuns;
92 };
93 
UNIX_ONLY_TEST(SkText_SelectableText_Bounds,reporter)94 UNIX_ONLY_TEST(SkText_SelectableText_Bounds, reporter) {
95     sk_sp<TrivialFontChain> fontChain = sk_make_sp<TrivialFontChain>("Roboto", 40.0f, SkFontStyle::Normal());
96     if (fontChain->empty()) return;
97 
98     std::u16string utf16(u"    Leading spaces\nTrailing spaces    \nLong text with collapsed      spaces inside wrapped into few lines");
99     UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
100     if (!unicodeText.getUnicode()) return;
101 
102     FontBlock fontBlock(utf16.size(), fontChain);
103     auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
104     auto shapedText = fontResolvedText->shape(&unicodeText, TextDirection::kLtr);
105     auto wrappedText = shapedText->wrap(&unicodeText, 440.0f, 500.0f);
106     auto selectableText = wrappedText->prepareToEdit(&unicodeText);
107 
108     TestVisitor testVisitor;
109     wrappedText->visit(&testVisitor);
110 
111     REPORTER_ASSERT(reporter, selectableText->countLines() == 5);
112     for (LineIndex lineIndex = 0; lineIndex < selectableText->countLines(); ++lineIndex) {
113         auto& testLine = testVisitor.fTestLines[lineIndex];
114         auto boxLine = selectableText->getLine(lineIndex);
115         SkScalar left = boxLine.fBounds.fLeft;
116         for (auto& box : boxLine.fBoxGlyphs) {
117             REPORTER_ASSERT(reporter, boxLine.fBounds.contains(box) || box.isEmpty());
118             REPORTER_ASSERT(reporter, left <= box.fLeft);
119             left = box.fRight;
120         }
121 
122         GlyphIndex trailingSpaces = boxLine.fBoxGlyphs.size() - 1;
123         for (RunIndex runIndex = testLine.runRange.fEnd; runIndex > testLine.runRange.fStart; --runIndex) {
124             auto& testRun = testVisitor.fTestRuns[runIndex - 1];
125             if (testRun.trailingSpaces == 0) {
126                 trailingSpaces -= testRun.glyphs.size();
127             } else {
128                 trailingSpaces -= (testRun.glyphs.size() - testRun.trailingSpaces);
129                 break;
130             }
131         }
132 
133         REPORTER_ASSERT(reporter, boxLine.fTrailingSpacesEnd == testLine.trailingSpaces.fEnd);
134         REPORTER_ASSERT(reporter, boxLine.fTextEnd == trailingSpaces);
135         REPORTER_ASSERT(reporter, boxLine.fTextRange == testLine.lineText);
136         REPORTER_ASSERT(reporter, boxLine.fIndex == lineIndex);
137         REPORTER_ASSERT(reporter, boxLine.fIsHardBreak == testLine.hardBreak);
138         REPORTER_ASSERT(reporter, boxLine.fBounds == testLine.bounds);
139     }
140 }
141 
UNIX_ONLY_TEST(SkText_SelectableText_Navigation_FirstLast,reporter)142 UNIX_ONLY_TEST(SkText_SelectableText_Navigation_FirstLast, reporter) {
143     sk_sp<TrivialFontChain> fontChain = sk_make_sp<TrivialFontChain>("Roboto", 40.0f, SkFontStyle::Normal());
144     if (fontChain->empty()) return;
145 
146     std::u16string utf16(u"    Leading spaces\nTrailing spaces    \nLong text with collapsed      spaces inside wrapped into few lines");
147     UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
148     if (!unicodeText.getUnicode()) return;
149 
150     FontBlock fontBlock(utf16.size(), fontChain);
151     auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
152     auto shapedText = fontResolvedText->shape(&unicodeText, TextDirection::kLtr);
153     auto wrappedText = shapedText->wrap(&unicodeText, 440.0f, 500.0f);
154     auto selectableText = wrappedText->prepareToEdit(&unicodeText);
155 
156     TestVisitor testVisitor;
157     wrappedText->visit(&testVisitor);
158 
159     // First position
160     auto firstLine = testVisitor.fTestLines.front();
161     auto firstRun = testVisitor.fTestRuns.front();
162     auto firstPosition = selectableText->firstPosition(PositionType::kGraphemeCluster);
163     REPORTER_ASSERT(reporter, firstPosition.fLineIndex == 0);
164     REPORTER_ASSERT(reporter, firstPosition.fTextRange == TextRange(0, 0));
165     REPORTER_ASSERT(reporter, firstPosition.fGlyphRange == GlyphRange(0, 0));
166     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(firstPosition.fBoundaries.fLeft, 0.0f));
167     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(firstPosition.fBoundaries.fTop, 0.0f));
168     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(firstPosition.fBoundaries.width(), 0.0f));
169     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(firstPosition.fBoundaries.height(), firstLine.bounds.height()));
170 
171     // Last position
172     auto lastLine = testVisitor.fTestLines.back();
173     auto lastRun = testVisitor.fTestRuns.back();
174     auto lastPosition = selectableText->lastPosition(PositionType::kGraphemeCluster);
175     REPORTER_ASSERT(reporter, lastPosition.fLineIndex == testVisitor.fTestLines.size() - 1);
176     REPORTER_ASSERT(reporter, lastPosition.fTextRange == TextRange(utf16.size(), utf16.size()));
177     REPORTER_ASSERT(reporter, lastPosition.fGlyphRange == GlyphRange(lastRun.glyphs.size(), lastRun.glyphs.size()));
178     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lastPosition.fBoundaries.fLeft, lastRun.positions[lastRun.glyphs.size()].fX));
179     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lastPosition.fBoundaries.fTop, lastLine.bounds.fTop));
180     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lastPosition.fBoundaries.width(), 0.0f));
181     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(lastPosition.fBoundaries.height(), lastLine.bounds.height()));
182 }
183 
UNIX_ONLY_TEST(SkText_SelectableText_ScanRightByGraphemeClusters,reporter)184 UNIX_ONLY_TEST(SkText_SelectableText_ScanRightByGraphemeClusters, reporter) {
185     sk_sp<TrivialFontChain> fontChain = sk_make_sp<TrivialFontChain>("Roboto", 40.0f, SkFontStyle::Normal());
186     if (fontChain->empty()) return;
187 
188     std::u16string utf16(u"    Small Text   \n");
189     UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
190     if (!unicodeText.getUnicode()) return;
191 
192     FontBlock fontBlock(utf16.size(), fontChain);
193     auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
194     auto shapedText = fontResolvedText->shape(&unicodeText, TextDirection::kLtr);
195     auto wrappedText = shapedText->wrap(&unicodeText, 440.0f, 500.0f);
196     auto selectableText = wrappedText->prepareToEdit(&unicodeText);
197 
198     TestVisitor testVisitor;
199     wrappedText->visit(&testVisitor);
200 
201     auto firstPosition = selectableText->firstPosition(PositionType::kGraphemeCluster);
202     auto lastPosition = selectableText->lastPosition(PositionType::kGraphemeCluster);
203 
204     auto position = firstPosition;
205     while (!(position.fGlyphRange == lastPosition.fGlyphRange)) {
206         auto next = selectableText->nextPosition(position);
207         REPORTER_ASSERT(reporter, position.fTextRange.fEnd == next.fTextRange.fStart);
208         if (position.fLineIndex == next.fLineIndex - 1) {
209             auto line = selectableText->getLine(next.fLineIndex);
210             REPORTER_ASSERT(reporter, next.fGlyphRange.fStart == 0);
211             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(next.fBoundaries.fLeft, 0.0f));
212             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(next.fBoundaries.fTop, line.fBounds.fTop));
213             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(next.fBoundaries.height(), line.fBounds.height()));
214         } else {
215             REPORTER_ASSERT(reporter, position.fLineIndex == next.fLineIndex);
216             REPORTER_ASSERT(reporter, position.fGlyphRange.fEnd == next.fGlyphRange.fStart);
217             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(position.fBoundaries.fRight, next.fBoundaries.fLeft));
218             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(position.fBoundaries.fTop, next.fBoundaries.fTop));
219             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(position.fBoundaries.height(), next.fBoundaries.height()));
220         }
221         position = next;
222     }
223 }
224 
UNIX_ONLY_TEST(SkText_SelectableText_ScanLeftByGraphemeClusters,reporter)225 UNIX_ONLY_TEST(SkText_SelectableText_ScanLeftByGraphemeClusters, reporter) {
226     sk_sp<TrivialFontChain> fontChain = sk_make_sp<TrivialFontChain>("Roboto", 40.0f, SkFontStyle::Normal());
227     if (fontChain->empty()) return;
228 
229     std::u16string utf16(u"    Small Text   \n");
230     UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
231     if (!unicodeText.getUnicode()) return;
232 
233     FontBlock fontBlock(utf16.size(), fontChain);
234     auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
235     auto shapedText = fontResolvedText->shape(&unicodeText, TextDirection::kLtr);
236     auto wrappedText = shapedText->wrap(&unicodeText, 440.0f, 500.0f);
237     auto selectableText = wrappedText->prepareToEdit(&unicodeText);
238 
239     TestVisitor testVisitor;
240     wrappedText->visit(&testVisitor);
241 
242     auto firstPosition = selectableText->firstPosition(PositionType::kGraphemeCluster);
243     auto lastPosition = selectableText->lastPosition(PositionType::kGraphemeCluster);
244 
245     auto position = lastPosition;
246     while (!(position.fGlyphRange == firstPosition.fGlyphRange)) {
247         auto prev = selectableText->previousPosition(position);
248         REPORTER_ASSERT(reporter, position.fTextRange.fEnd == prev.fTextRange.fStart);
249         if (position.fLineIndex == prev.fLineIndex + 1) {
250             auto line = selectableText->getLine(prev.fLineIndex);
251             REPORTER_ASSERT(reporter, prev.fGlyphRange.fEnd == line.fBoxGlyphs.size());
252             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(prev.fBoundaries.fRight, line.fBounds.fRight));
253             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(prev.fBoundaries.fTop, line.fBounds.fTop));
254             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(prev.fBoundaries.height(), line.fBounds.height()));
255         } else {
256             REPORTER_ASSERT(reporter, position.fLineIndex == prev.fLineIndex);
257             REPORTER_ASSERT(reporter, position.fGlyphRange.fStart == prev.fGlyphRange.fEnd);
258             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(position.fBoundaries.fLeft, prev.fBoundaries.fRight));
259             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(position.fBoundaries.fTop, prev.fBoundaries.fTop));
260             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(position.fBoundaries.height(), prev.fBoundaries.height()));
261         }
262         position = prev;
263     }
264 }
265 
UNIX_ONLY_TEST(SkText_SelectableText_Navigation_UpDown,reporter)266 UNIX_ONLY_TEST(SkText_SelectableText_Navigation_UpDown, reporter) {
267     sk_sp<TrivialFontChain> fontChain = sk_make_sp<TrivialFontChain>("Roboto", 40.0f, SkFontStyle::Normal());
268     if (fontChain->empty()) return;
269 
270     std::u16string utf16(u"    Leading spaces\nTrailing spaces    \nLong text with collapsed      spaces inside wrapped into few lines");
271     UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
272     if (!unicodeText.getUnicode()) return;
273 
274     FontBlock fontBlock(utf16.size(), fontChain);
275     auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
276     auto shapedText = fontResolvedText->shape(&unicodeText, TextDirection::kLtr);
277     auto wrappedText = shapedText->wrap(&unicodeText, 440.0f, 500.0f);
278     auto selectableText = wrappedText->prepareToEdit(&unicodeText);
279 
280     TestVisitor testVisitor;
281     wrappedText->visit(&testVisitor);
282 
283     // Upper position
284     auto position = selectableText->lastInLinePosition(PositionType::kGraphemeCluster, 0);
285     while (position.fLineIndex > 0) {
286         auto down = selectableText->downPosition(position);
287         REPORTER_ASSERT(reporter, position.fLineIndex + 1 == down.fLineIndex);
288         REPORTER_ASSERT(reporter, position.fBoundaries.centerX() >= down.fBoundaries.centerX());
289         position = down;
290     }
291 
292     // Down position
293     position = selectableText->lastInLinePosition(PositionType::kGraphemeCluster, selectableText->countLines() - 1);
294     while (position.fLineIndex < selectableText->countLines() - 1) {
295         auto down = selectableText->downPosition(position);
296         REPORTER_ASSERT(reporter, position.fLineIndex - 1 == down.fLineIndex);
297         REPORTER_ASSERT(reporter, position.fBoundaries.centerX() >= down.fBoundaries.centerX());
298         position = down;
299     }
300 }
301