• 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 
UNIX_ONLY_TEST(SkText_ShapedText_LTR,reporter)41 UNIX_ONLY_TEST(SkText_ShapedText_LTR, reporter) {
42     TrivialFontChain* fontChain = new TrivialFontChain("Roboto", 40.0f, SkFontStyle::Normal());
43     if (fontChain->empty()) return;
44 
45     std::u16string utf16(u"Hello world\nHello world");
46     UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
47     if (!unicodeText.getUnicode()) return;
48 
49     FontBlock fontBlock(utf16.size(), sk_ref_sp<FontChain>(fontChain));
50     auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
51     auto shapedText = fontResolvedText->shape(&unicodeText, TextDirection::kLtr);
52     auto logicalRuns = shapedText->getLogicalRuns();
53 
54     auto newLine = utf16.find_first_of(u"\n");
55     REPORTER_ASSERT(reporter, logicalRuns.size() == 3);
56     REPORTER_ASSERT(reporter, logicalRuns[1].getRunType() == LogicalRunType::kLineBreak);
57     REPORTER_ASSERT(reporter, logicalRuns[1].getTextRange() == TextRange(newLine, newLine + 1));
58 }
59 
UNIX_ONLY_TEST(SkText_ShapedText_RTL,reporter)60 UNIX_ONLY_TEST(SkText_ShapedText_RTL, reporter) {
61     sk_sp<TrivialFontChain> fontChain = sk_make_sp<TrivialFontChain>("Roboto", 40.0f, SkFontStyle::Normal());
62     if (fontChain->empty()) return;
63 
64     std::u16string utf16(u"\u202EHELLO WORLD\nHELLO WORLD");
65     UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
66     if (!unicodeText.getUnicode()) return;
67 
68     FontBlock fontBlock(utf16.size(), fontChain);
69     auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
70     auto shapedText = fontResolvedText->shape(&unicodeText, TextDirection::kLtr);
71     auto logicalRuns = shapedText->getLogicalRuns();
72 
73     auto newLine = utf16.find_first_of(u"\n");
74     REPORTER_ASSERT(reporter, logicalRuns.size() == 3);
75     REPORTER_ASSERT(reporter, logicalRuns[1].getRunType() == LogicalRunType::kLineBreak);
76     REPORTER_ASSERT(reporter, logicalRuns[1].getTextRange() == TextRange(newLine, newLine + 1));
77 }
78