blob: c13b98118f68622ff5c7f4f126cd134e836503d4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "samplecode/Sample.h"
Mike Reed193d3a02018-02-08 15:45:51 -05008
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkCanvas.h"
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkGraphics.h"
13#include "include/core/SkPath.h"
14#include "include/core/SkRegion.h"
15#include "include/core/SkShader.h"
16#include "include/core/SkStream.h"
17#include "include/core/SkTextBlob.h"
18#include "include/core/SkTime.h"
19#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/effects/SkGradientShader.h"
21#include "include/utils/SkRandom.h"
22#include "modules/skshaper/include/SkShaper.h"
23#include "src/core/SkOSFile.h"
24#include "src/shaders/SkColorShader.h"
25#include "src/utils/SkUTF.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000026
Mike Reedb2bf28c2020-01-28 10:52:05 -050027typedef std::unique_ptr<SkShaper> (*ShaperFactory)();
28
reed@google.comd6f5a812011-03-02 14:34:11 +000029static const char gText[] =
rmistry@google.comae933ce2012-08-23 18:19:56 +000030 "When in the Course of human events it becomes necessary for one people "
31 "to dissolve the political bands which have connected them with another "
32 "and to assume among the powers of the earth, the separate and equal "
33 "station to which the Laws of Nature and of Nature's God entitle them, "
34 "a decent respect to the opinions of mankind requires that they should "
35 "declare the causes which impel them to the separation.";
reed@android.com0bb6d062010-05-17 14:50:04 +000036
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040037class TextBoxView : public Sample {
Mike Reedb2bf28c2020-01-28 10:52:05 -050038 SkString fName;
reed@google.comd6f5a812011-03-02 14:34:11 +000039public:
Mike Reedb2bf28c2020-01-28 10:52:05 -050040 TextBoxView(ShaperFactory fact, const char suffix[]) : fShaper(fact()) {
41 fName.printf("TextBox_%s", suffix);
42 }
reed@google.comd6f5a812011-03-02 14:34:11 +000043
reed@android.com0bb6d062010-05-17 14:50:04 +000044protected:
Mike Reedb2bf28c2020-01-28 10:52:05 -050045 SkString name() override { return fName; }
reed@google.comd6f5a812011-03-02 14:34:11 +000046
reed@google.com021b4052011-09-28 15:24:00 +000047 void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
48 SkAutoCanvasRestore acr(canvas, true);
49
50 canvas->clipRect(SkRect::MakeWH(w, h));
51 canvas->drawColor(bg);
Mike Reed193d3a02018-02-08 15:45:51 -050052
rmistry@google.comae933ce2012-08-23 18:19:56 +000053 SkScalar margin = 20;
reed@android.com0bb6d062010-05-17 14:50:04 +000054
rmistry@google.comae933ce2012-08-23 18:19:56 +000055 SkPaint paint;
reed@google.com021b4052011-09-28 15:24:00 +000056 paint.setColor(fg);
reed@android.com0bb6d062010-05-17 14:50:04 +000057
rmistry@google.comae933ce2012-08-23 18:19:56 +000058 for (int i = 9; i < 24; i += 2) {
Ben Wagner3bdb69c2019-04-01 19:01:09 -040059 SkTextBlobBuilderRunHandler builder(gText, { margin, margin });
Ben Wagner1ca50522019-10-01 17:54:28 -040060 SkFont srcFont(nullptr, SkIntToScalar(i));
61 srcFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Mike Reedf4a90672020-01-29 13:00:50 -050062 srcFont.setSubpixel(true);
Hal Canary4484b8f2019-01-08 14:00:08 -050063
Ben Wagner1ca50522019-10-01 17:54:28 -040064 const char* utf8 = gText;
65 size_t utf8Bytes = sizeof(gText) - 1;
66
67 std::unique_ptr<SkShaper::BiDiRunIterator> bidi(
68 SkShaper::MakeBiDiRunIterator(utf8, utf8Bytes, 0xfe));
69 if (!bidi) {
70 return;
71 }
72
73 std::unique_ptr<SkShaper::LanguageRunIterator> language(
74 SkShaper::MakeStdLanguageRunIterator(utf8, utf8Bytes));
75 if (!language) {
76 return;
77 }
78
79 SkFourByteTag undeterminedScript = SkSetFourByteTag('Z','y','y','y');
80 std::unique_ptr<SkShaper::ScriptRunIterator> script(
81 SkShaper::MakeScriptRunIterator(utf8, utf8Bytes, undeterminedScript));
82 if (!script) {
83 return;
84 }
85
86 std::unique_ptr<SkShaper::FontRunIterator> font(
87 SkShaper::MakeFontMgrRunIterator(utf8, utf8Bytes, srcFont, SkFontMgr::RefDefault(),
88 "Arial", SkFontStyle::Bold(), &*language));
89 if (!font) {
90 return;
91 }
92
93 fShaper->shape(utf8, utf8Bytes, *font, *bidi, *script, *language, w - margin, &builder);
Florin Malita9867f612018-12-12 10:54:49 -050094 canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint);
Mike Reed193d3a02018-02-08 15:45:51 -050095
Ben Wagner3bdb69c2019-04-01 19:01:09 -040096 canvas->translate(0, builder.endPoint().y());
rmistry@google.comae933ce2012-08-23 18:19:56 +000097 }
reed@android.com0bb6d062010-05-17 14:50:04 +000098 }
reed@google.comd6f5a812011-03-02 14:34:11 +000099
Mike Reed193d3a02018-02-08 15:45:51 -0500100 void onDrawContent(SkCanvas* canvas) override {
reed@google.com021b4052011-09-28 15:24:00 +0000101 SkScalar width = this->width() / 3;
102 drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
103 canvas->translate(width, 0);
104 drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
105 canvas->translate(width, 0);
106 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
107 canvas->translate(0, this->height()/2);
108 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
109 }
110
reed@android.com0bb6d062010-05-17 14:50:04 +0000111private:
Ben Wagnerb0591942019-02-15 14:46:18 -0500112 std::unique_ptr<SkShaper> fShaper;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400113 typedef Sample INHERITED;
reed@android.com0bb6d062010-05-17 14:50:04 +0000114};
115
Mike Reedb2bf28c2020-01-28 10:52:05 -0500116DEF_SAMPLE( return new TextBoxView([](){ return SkShaper::Make(); }, "default"); );
117#ifdef SK_BUILD_FOR_MAC
118DEF_SAMPLE( return new TextBoxView(SkShaper::MakeCoreText, "coretext"); );
119#endif
Mike Reedf4a90672020-01-29 13:00:50 -0500120
121class SampleShaper : public Sample {
122public:
123 SampleShaper() {}
124
125protected:
126 SkString name() override { return SkString("shaper"); }
127
128 void drawTest(SkCanvas* canvas, const char str[], SkScalar size,
129 std::unique_ptr<SkShaper> shaper) {
130 if (!shaper) return;
131
132 SkTextBlobBuilderRunHandler builder(str, {0, 0});
133 SkFont srcFont;
134 srcFont.setSize(size);
135 srcFont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
136 srcFont.setSubpixel(true);
137
138 size_t len = strlen(str);
139
140 std::unique_ptr<SkShaper::BiDiRunIterator> bidi(
141 SkShaper::MakeBiDiRunIterator(str, len, 0xfe));
142 if (!bidi) {
143 return;
144 }
145
146 std::unique_ptr<SkShaper::LanguageRunIterator> language(
147 SkShaper::MakeStdLanguageRunIterator(str, len));
148 if (!language) {
149 return;
150 }
151
152 SkFourByteTag undeterminedScript = SkSetFourByteTag('Z','y','y','y');
153 std::unique_ptr<SkShaper::ScriptRunIterator> script(
154 SkShaper::MakeScriptRunIterator(str, len, undeterminedScript));
155 if (!script) {
156 return;
157 }
158
159 std::unique_ptr<SkShaper::FontRunIterator> font(
160 SkShaper::MakeFontMgrRunIterator(str, len, srcFont, SkFontMgr::RefDefault(),
161 "Arial", SkFontStyle::Bold(), &*language));
162 if (!font) {
163 return;
164 }
165
166 shaper->shape(str, len, *font, *bidi, *script, *language, 2000, &builder);
167
168 canvas->drawTextBlob(builder.makeBlob(), 0, 0, SkPaint());
169 }
170
171 void onDrawContent(SkCanvas* canvas) override {
172 canvas->translate(10, 30);
173
174 const char text[] = "world";
175
176 for (SkScalar size = 30; size <= 30; size += 10) {
177 this->drawTest(canvas, text, size, SkShaper::Make());
178 canvas->translate(0, size + 5);
179 this->drawTest(canvas, text, size, SkShaper::MakeCoreText());
180 canvas->translate(0, size*2);
181 }
182 }
183
184private:
185 typedef Sample INHERITED;
186};
187DEF_SAMPLE( return new SampleShaper; );