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_FontResolution1,reporter)41 UNIX_ONLY_TEST(SkText_FontResolution1, reporter) {
42 TrivialFontChain* fontChain = new TrivialFontChain("Roboto", 40.0f, SkFontStyle::Normal());
43 if (fontChain->empty()) return;
44
45 std::u16string utf16(u"Hello 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
52 auto resolvedFonts = fontResolvedText->resolvedFonts();
53
54 REPORTER_ASSERT(reporter, resolvedFonts.size() == 1);
55 REPORTER_ASSERT(reporter, resolvedFonts.front().textRange.width() == utf16.size());
56 REPORTER_ASSERT(reporter, resolvedFonts.front().typeface->uniqueID() == fontChain->getTypeface()->uniqueID());
57 REPORTER_ASSERT(reporter, resolvedFonts.front().size == 40.0f);
58 REPORTER_ASSERT(reporter, resolvedFonts.front().style == SkFontStyle::Normal());
59 }
60
UNIX_ONLY_TEST(SkText_FontResolution3,reporter)61 UNIX_ONLY_TEST(SkText_FontResolution3, reporter) {
62 MultipleFontChain* fontChain = new MultipleFontChain({ "Roboto", "Noto Color Emoji", "Noto Serif CJK JP" }, 40.0f, SkFontStyle::Normal());
63 if (fontChain->count() < 3) return;
64
65 std::u16string utf16(u"English English 字典 字典 ������ ������");
66 UnicodeText unicodeText(SkUnicode::Make(), SkSpan<uint16_t>((uint16_t*)utf16.data(), utf16.size()));
67 if (!unicodeText.getUnicode()) return;
68
69 FontBlock fontBlock(utf16.size(), sk_ref_sp<FontChain>(fontChain));
70 auto fontResolvedText = unicodeText.resolveFonts(SkSpan<FontBlock>(&fontBlock, 1));
71
72 auto resolvedFonts = fontResolvedText->resolvedFonts();
73
74 TextIndex prev = 0;
75 for (auto& rf : resolvedFonts) {
76 REPORTER_ASSERT(reporter, prev == rf.textRange.fStart);
77 REPORTER_ASSERT(reporter, rf.textRange.width() > 0.0f);
78 prev = rf.textRange.fEnd;
79 }
80
81 REPORTER_ASSERT(reporter, resolvedFonts.size() == 8 /* 1English 3English spaces + 2Emoji + 2JP */);
82 REPORTER_ASSERT(reporter, resolvedFonts[0].textRange.fStart == 0);
83 REPORTER_ASSERT(reporter, resolvedFonts[7].textRange.fEnd == utf16.size());
84 REPORTER_ASSERT(reporter, resolvedFonts[0].size == 40.0f);
85 REPORTER_ASSERT(reporter, resolvedFonts[0].style == SkFontStyle::Normal());
86 }
87