blob: a342c38b24487ca097f0b2823541aeabf8bab172 [file] [log] [blame]
jvanverth9564ce62014-09-16 05:45:19 -07001/*
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 */
Hal Canaryc640d0d2018-06-13 09:59:02 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkColorSpace.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFontStyle.h"
14#include "include/core/SkFontTypes.h"
15#include "include/core/SkImageInfo.h"
16#include "include/core/SkMatrix.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPoint.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
22#include "include/core/SkSize.h"
23#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/core/SkSurfaceProps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkTextBlob.h"
27#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "include/core/SkTypes.h"
29#include "include/private/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "include/private/SkTo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "tools/ToolUtils.h"
jvanverth9564ce62014-09-16 05:45:19 -070032
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include <string.h>
34
35class GrContext;
36
jvanverthfca302c2014-10-20 13:12:54 -070037class DFTextGM : public skiagm::GM {
jvanverth9564ce62014-09-16 05:45:19 -070038public:
39 DFTextGM() {
40 this->setBGColor(0xFFFFFFFF);
jvanverth9564ce62014-09-16 05:45:19 -070041 }
42
43protected:
mtklein36352bf2015-03-25 18:17:31 -070044 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050045 fEmojiTypeface = ToolUtils::emoji_typeface();
46 fEmojiText = ToolUtils::emoji_sample_text();
jvanverthfca302c2014-10-20 13:12:54 -070047 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060050 return SkString("dftext");
jvanverth9564ce62014-09-16 05:45:19 -070051 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 SkISize onISize() override {
jvanverth9564ce62014-09-16 05:45:19 -070054 return SkISize::Make(1024, 768);
55 }
56
mtklein36352bf2015-03-25 18:17:31 -070057 virtual void onDraw(SkCanvas* inputCanvas) override {
jvanverthed14f1d2014-10-06 12:50:54 -070058 SkScalar textSizes[] = { 9.0f, 9.0f*2.0f, 9.0f*5.0f, 9.0f*2.0f*5.0f };
jvanverth9564ce62014-09-16 05:45:19 -070059 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f };
60
61 // set up offscreen rendering with distance field text
jvanverth9564ce62014-09-16 05:45:19 -070062 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -070063 SkISize size = onISize();
64 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), kPremul_SkAlphaType,
Mike Reed693fdbd2017-01-12 10:13:40 -050065 inputCanvas->imageInfo().refColorSpace());
brianosman3a0dbde2016-07-26 11:36:05 -070066 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
reed4a8126e2014-09-22 07:29:03 -070067 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -070068 auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props));
69 SkCanvas* canvas = surface ? surface->getCanvas() : inputCanvas;
jvanverthf9669f42014-10-07 07:59:45 -070070 // init our new canvas with the old canvas's matrix
71 canvas->setMatrix(inputCanvas->getTotalMatrix());
jvanverth76ce81e2014-09-22 14:26:53 -070072 // apply global scale to test glyph positioning
73 canvas->scale(1.05f, 1.05f);
jvanverth9564ce62014-09-16 05:45:19 -070074 canvas->clear(0xffffffff);
75
76 SkPaint paint;
77 paint.setAntiAlias(true);
jvanverth4854d132015-06-22 06:46:56 -070078
Mike Kleinea3f0142019-03-20 11:12:10 -050079 SkFont font(ToolUtils::create_portable_typeface("serif", SkFontStyle()));
Mike Reed42e7a7e2018-12-13 16:42:53 -050080 font.setSubpixel(true);
jvanverth9564ce62014-09-16 05:45:19 -070081
82 const char* text = "Hamburgefons";
83 const size_t textLen = strlen(text);
84
85 // check scaling up
86 SkScalar x = SkIntToScalar(0);
87 SkScalar y = SkIntToScalar(78);
88 for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) {
89 SkAutoCanvasRestore acr(canvas, true);
90 canvas->translate(x, y);
91 canvas->scale(scales[i], scales[i]);
Mike Reed42e7a7e2018-12-13 16:42:53 -050092 font.setSize(textSizes[i]);
Ben Wagner51e15a62019-05-07 15:38:46 -040093 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
Mike Reed42e7a7e2018-12-13 16:42:53 -050094 y += font.getMetrics(nullptr)*scales[i];
jvanverth9564ce62014-09-16 05:45:19 -070095 }
96
97 // check rotation
98 for (size_t i = 0; i < 5; ++i) {
99 SkScalar rotX = SkIntToScalar(10);
100 SkScalar rotY = y;
101
102 SkAutoCanvasRestore acr(canvas, true);
103 canvas->translate(SkIntToScalar(10 + i * 200), -80);
bungeman7438bfc2016-07-12 15:01:19 -0700104 canvas->rotate(SkIntToScalar(i * 5), rotX, rotY);
jvanverth9564ce62014-09-16 05:45:19 -0700105 for (int ps = 6; ps <= 32; ps += 3) {
Mike Reed42e7a7e2018-12-13 16:42:53 -0500106 font.setSize(SkIntToScalar(ps));
Ben Wagner51e15a62019-05-07 15:38:46 -0400107 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, rotX, rotY, font, paint);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500108 rotY += font.getMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700109 }
110 }
111
112 // check scaling down
Mike Reed42e7a7e2018-12-13 16:42:53 -0500113 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
jvanverth76ce81e2014-09-22 14:26:53 -0700114 x = SkIntToScalar(680);
jvanverth9564ce62014-09-16 05:45:19 -0700115 y = SkIntToScalar(20);
116 size_t arraySize = SK_ARRAY_COUNT(textSizes);
117 for (size_t i = 0; i < arraySize; ++i) {
118 SkAutoCanvasRestore acr(canvas, true);
119 canvas->translate(x, y);
120 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]);
121 canvas->scale(scaleFactor, scaleFactor);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500122 font.setSize(textSizes[i]);
Ben Wagner51e15a62019-05-07 15:38:46 -0400123 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500124 y += font.getMetrics(nullptr)*scaleFactor;
jvanverth9564ce62014-09-16 05:45:19 -0700125 }
126
jvanverth76ce81e2014-09-22 14:26:53 -0700127 // check pos text
128 {
129 SkAutoCanvasRestore acr(canvas, true);
130
131 canvas->scale(2.0f, 2.0f);
132
Mike Reed42e7a7e2018-12-13 16:42:53 -0500133 SkAutoTArray<SkGlyphID> glyphs(SkToInt(textLen));
Ben Wagner51e15a62019-05-07 15:38:46 -0400134 int count = font.textToGlyphs(text, textLen, SkTextEncoding::kUTF8, glyphs.get(), textLen);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500135 SkAutoTArray<SkPoint> pos(count);
136 font.setSize(textSizes[0]);
137 font.getPos(glyphs.get(), count, pos.get(), {340, 75});
jvanverth76ce81e2014-09-22 14:26:53 -0700138
Mike Reed42e7a7e2018-12-13 16:42:53 -0500139 auto blob = SkTextBlob::MakeFromPosText(glyphs.get(), count * sizeof(SkGlyphID),
Ben Wagner51e15a62019-05-07 15:38:46 -0400140 pos.get(), font, SkTextEncoding::kGlyphID);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500141 canvas->drawTextBlob(blob, 0, 0, paint);
jvanverth76ce81e2014-09-22 14:26:53 -0700142 }
143
144
jvanverth9564ce62014-09-16 05:45:19 -0700145 // check gamma-corrected blending
146 const SkColor fg[] = {
147 0xFFFFFFFF,
148 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
149 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
150 0xFF000000,
151 };
152
caryclarkc3dcb672015-07-21 12:27:36 -0700153 paint.setColor(0xFFF7F3F7);
caryclarkba9757a2015-07-23 14:07:58 -0700154 SkRect r = SkRect::MakeLTRB(670, 215, 820, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700155 canvas->drawRect(r, paint);
156
jvanverth76ce81e2014-09-22 14:26:53 -0700157 x = SkIntToScalar(680);
caryclarkba9757a2015-07-23 14:07:58 -0700158 y = SkIntToScalar(235);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500159 font.setSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700160 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
161 paint.setColor(fg[i]);
162
Ben Wagner51e15a62019-05-07 15:38:46 -0400163 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500164 y += font.getMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700165 }
166
caryclarkc3dcb672015-07-21 12:27:36 -0700167 paint.setColor(0xFF181C18);
caryclarkba9757a2015-07-23 14:07:58 -0700168 r = SkRect::MakeLTRB(820, 215, 970, 397);
jvanverth9564ce62014-09-16 05:45:19 -0700169 canvas->drawRect(r, paint);
170
jvanverth76ce81e2014-09-22 14:26:53 -0700171 x = SkIntToScalar(830);
caryclarkba9757a2015-07-23 14:07:58 -0700172 y = SkIntToScalar(235);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500173 font.setSize(SkIntToScalar(19));
jvanverth9564ce62014-09-16 05:45:19 -0700174 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
175 paint.setColor(fg[i]);
176
Ben Wagner51e15a62019-05-07 15:38:46 -0400177 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500178 y += font.getMetrics(nullptr);
jvanverth9564ce62014-09-16 05:45:19 -0700179 }
180
jvanverth354eba52015-03-16 11:32:49 -0700181 // check skew
182 {
Mike Reed42e7a7e2018-12-13 16:42:53 -0500183 font.setEdging(SkFont::Edging::kAntiAlias);
jvanverth354eba52015-03-16 11:32:49 -0700184 SkAutoCanvasRestore acr(canvas, true);
185 canvas->skew(0.0f, 0.151515f);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500186 font.setSize(SkIntToScalar(32));
Ben Wagner51e15a62019-05-07 15:38:46 -0400187 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 745, 70, font, paint);
jvanverth354eba52015-03-16 11:32:49 -0700188 }
189 {
Mike Reed42e7a7e2018-12-13 16:42:53 -0500190 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
jvanverth354eba52015-03-16 11:32:49 -0700191 SkAutoCanvasRestore acr(canvas, true);
192 canvas->skew(0.5f, 0.0f);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500193 font.setSize(SkIntToScalar(32));
Ben Wagner51e15a62019-05-07 15:38:46 -0400194 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 580, 125, font, paint);
jvanverth354eba52015-03-16 11:32:49 -0700195 }
196
Jim Van Verth2525a372018-06-07 09:41:28 -0400197 // check perspective
198 {
Mike Reed42e7a7e2018-12-13 16:42:53 -0500199 font.setEdging(SkFont::Edging::kAntiAlias);
Jim Van Verth2525a372018-06-07 09:41:28 -0400200 SkAutoCanvasRestore acr(canvas, true);
201 SkMatrix persp;
202 persp.setAll(0.9839f, 0, 0,
203 0.2246f, 0.6829f, 0,
204 0.0002352f, -0.0003844f, 1);
205 canvas->concat(persp);
206 canvas->translate(1100, -295);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500207 font.setSize(37.5f);
Ben Wagner51e15a62019-05-07 15:38:46 -0400208 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
Jim Van Verth2525a372018-06-07 09:41:28 -0400209 }
210 {
Mike Reed42e7a7e2018-12-13 16:42:53 -0500211 font.setSubpixel(false);
212 font.setEdging(SkFont::Edging::kAlias);
Jim Van Verth2525a372018-06-07 09:41:28 -0400213 SkAutoCanvasRestore acr(canvas, true);
214 SkMatrix persp;
215 persp.setAll(0.9839f, 0, 0,
216 0.2246f, 0.6829f, 0,
217 0.0002352f, -0.0003844f, 1);
218 canvas->concat(persp);
219 canvas->translate(1075, -245);
Jim Van Verth4ba1a6f2018-06-07 19:54:17 +0000220 canvas->scale(375, 375);
Mike Reed42e7a7e2018-12-13 16:42:53 -0500221 font.setSize(0.1f);
Ben Wagner51e15a62019-05-07 15:38:46 -0400222 canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, 0, 0, font, paint);
Jim Van Verth2525a372018-06-07 09:41:28 -0400223 }
224
jvanverthfca302c2014-10-20 13:12:54 -0700225 // check color emoji
caryclarkc3dcb672015-07-21 12:27:36 -0700226 if (fEmojiTypeface) {
Mike Reed42e7a7e2018-12-13 16:42:53 -0500227 SkFont emoiFont;
228 emoiFont.setSubpixel(true);
229 emoiFont.setTypeface(fEmojiTypeface);
230 emoiFont.setSize(SkIntToScalar(19));
Ben Wagner51e15a62019-05-07 15:38:46 -0400231 canvas->drawSimpleText(fEmojiText, strlen(fEmojiText), SkTextEncoding::kUTF8, 670, 90, emoiFont, paint);
caryclarkc3dcb672015-07-21 12:27:36 -0700232 }
Jim Van Verth2525a372018-06-07 09:41:28 -0400233
jvanverth9564ce62014-09-16 05:45:19 -0700234 // render offscreen buffer
jvanverth76ce81e2014-09-22 14:26:53 -0700235 if (surface) {
jvanverthf9669f42014-10-07 07:59:45 -0700236 SkAutoCanvasRestore acr(inputCanvas, true);
237 // since we prepended this matrix already, we blit using identity
238 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700239 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
jvanverth76ce81e2014-09-22 14:26:53 -0700240 }
jvanverth9564ce62014-09-16 05:45:19 -0700241 }
242
243private:
bungeman13b9c952016-05-12 10:09:30 -0700244 sk_sp<SkTypeface> fEmojiTypeface;
caryclarkc3dcb672015-07-21 12:27:36 -0700245 const char* fEmojiText;
jvanverthfca302c2014-10-20 13:12:54 -0700246
247 typedef skiagm::GM INHERITED;
jvanverth9564ce62014-09-16 05:45:19 -0700248};
249
halcanary385fe4d2015-08-26 13:07:48 -0700250DEF_GM(return new DFTextGM;)