blob: 1072959ff846ddd1391ba80a936a6a5b2c41276d [file] [log] [blame]
joshualitte46cf962015-08-26 11:19:55 -07001/*
2 * Copyright 2015 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 */
7
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
joshualitte46cf962015-08-26 11:19:55 -070010
11#include "Resources.h"
12#include "SkBlurMask.h"
joshualitte46cf962015-08-26 11:19:55 -070013#include "SkCanvas.h"
14#include "SkGradientShader.h"
15#include "SkImage.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040016#include "SkMaskFilter.h"
joshualitte46cf962015-08-26 11:19:55 -070017#include "SkRandom.h"
18#include "SkStream.h"
19#include "SkSurface.h"
20#include "SkTextBlob.h"
21#include "SkTypeface.h"
22
23namespace skiagm {
24class TextBlobMixedSizes : public GM {
25public:
26 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
27 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
28
29protected:
30 void onOnceBeforeDraw() override {
joshualitte46cf962015-08-26 11:19:55 -070031 SkTextBlobBuilder builder;
32
33 // make textblob. To stress distance fields, we choose sizes appropriately
Mike Reed28bd8822018-12-22 22:29:45 -050034 SkFont font(MakeResourceAsTypeface("fonts/HangingS.ttf"), 262);
35 font.setSubpixel(true);
36 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
joshualitte46cf962015-08-26 11:19:55 -070037
joshualitt33e91f12015-09-18 11:28:59 -070038 const char* text = "Skia";
joshualitte46cf962015-08-26 11:19:55 -070039
Mike Reed28bd8822018-12-22 22:29:45 -050040 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, 0);
joshualitte46cf962015-08-26 11:19:55 -070041
42 // large
43 SkRect bounds;
Mike Reed28bd8822018-12-22 22:29:45 -050044 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070045 SkScalar yOffset = bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050046 font.setSize(162);
joshualitte46cf962015-08-26 11:19:55 -070047
Mike Reed28bd8822018-12-22 22:29:45 -050048 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070049
50 // Medium
Mike Reed28bd8822018-12-22 22:29:45 -050051 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070052 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050053 font.setSize(72);
joshualitte46cf962015-08-26 11:19:55 -070054
Mike Reed28bd8822018-12-22 22:29:45 -050055 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070056
57 // Small
Mike Reed28bd8822018-12-22 22:29:45 -050058 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070059 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050060 font.setSize(32);
joshualitte46cf962015-08-26 11:19:55 -070061
Mike Reed28bd8822018-12-22 22:29:45 -050062 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070063
64 // micro (will fall out of distance field text even if distance field text is enabled)
Mike Reed28bd8822018-12-22 22:29:45 -050065 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
joshualitte46cf962015-08-26 11:19:55 -070066 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050067 font.setSize(14);
joshualitte46cf962015-08-26 11:19:55 -070068
Mike Reed28bd8822018-12-22 22:29:45 -050069 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, yOffset);
joshualitte46cf962015-08-26 11:19:55 -070070
kkinnunen68c63b32016-03-04 00:12:33 -080071 // Zero size.
Mike Reed28bd8822018-12-22 22:29:45 -050072 font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
kkinnunen68c63b32016-03-04 00:12:33 -080073 yOffset += bounds.height();
Mike Reed28bd8822018-12-22 22:29:45 -050074 font.setSize(0);
kkinnunen68c63b32016-03-04 00:12:33 -080075
Mike Reed28bd8822018-12-22 22:29:45 -050076 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, yOffset);
kkinnunen68c63b32016-03-04 00:12:33 -080077
joshualitte46cf962015-08-26 11:19:55 -070078 // build
fmalita37283c22016-09-13 10:00:23 -070079 fBlob = builder.make();
joshualitte46cf962015-08-26 11:19:55 -070080 }
81
82 SkString onShortName() override {
Mike Kleinbea1f942019-03-08 11:11:55 -060083 return SkStringPrintf("textblobmixedsizes%s",
Hal Canary58822d62017-11-14 12:08:34 -050084 fUseDFT ? "_df" : "");
joshualitte46cf962015-08-26 11:19:55 -070085 }
86
87 SkISize onISize() override {
88 return SkISize::Make(kWidth, kHeight);
89 }
90
91 void onDraw(SkCanvas* inputCanvas) override {
92 SkCanvas* canvas = inputCanvas;
reede8f30622016-03-23 18:59:25 -070093 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -070094 if (fUseDFT) {
joshualitte46cf962015-08-26 11:19:55 -070095 // Create a new Canvas to enable DFT
96 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -070097 SkISize size = onISize();
Mike Reed693fdbd2017-01-12 10:13:40 -050098 sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
brianosman52ede1d2016-06-20 08:25:02 -070099 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
100 kPremul_SkAlphaType, colorSpace);
brianosman3a0dbde2016-07-26 11:36:05 -0700101 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
joshualitte46cf962015-08-26 11:19:55 -0700102 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700103 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
joshualitte46cf962015-08-26 11:19:55 -0700104 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
105 // init our new canvas with the old canvas's matrix
106 canvas->setMatrix(inputCanvas->getTotalMatrix());
joshualitte46cf962015-08-26 11:19:55 -0700107 }
Mike Kleind46dce32018-08-16 10:17:03 -0400108 canvas->drawColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700109
110 SkRect bounds = fBlob->bounds();
111
mtkleindbfd7ab2016-09-01 11:24:54 -0700112 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
113 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
joshualitte46cf962015-08-26 11:19:55 -0700114
115 int rowCount = 0;
116 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
117 canvas->save();
118 SkRandom random;
119
120 SkPaint paint;
121 if (!fUseDFT) {
Mike Kleind46dce32018-08-16 10:17:03 -0400122 paint.setColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700123 }
124 paint.setAntiAlias(false);
125
mtkleindbfd7ab2016-09-01 11:24:54 -0700126 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
joshualitte46cf962015-08-26 11:19:55 -0700127
128 // setup blur paint
129 SkPaint blurPaint(paint);
Mike Kleind46dce32018-08-16 10:17:03 -0400130 blurPaint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400131 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma));
mtkleindbfd7ab2016-09-01 11:24:54 -0700132
joshualitte46cf962015-08-26 11:19:55 -0700133 for (int i = 0; i < 4; i++) {
134 canvas->save();
135 switch (i % 2) {
136 case 0:
137 canvas->rotate(random.nextF() * 45.f);
138 break;
139 case 1:
140 canvas->rotate(-random.nextF() * 45.f);
141 break;
142 }
143 if (!fUseDFT) {
144 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
145 }
146 canvas->drawTextBlob(fBlob, 0, 0, paint);
147 canvas->restore();
148 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
149 ++rowCount;
150 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
151 canvas->restore();
152 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
153 canvas->save();
154 rowCount = 0;
155 }
156 }
157 canvas->restore();
158
joshualitte46cf962015-08-26 11:19:55 -0700159 // render offscreen buffer
160 if (surface) {
161 SkAutoCanvasRestore acr(inputCanvas, true);
162 // since we prepended this matrix already, we blit using identity
163 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700164 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700165 }
joshualitte46cf962015-08-26 11:19:55 -0700166 }
167
168private:
fmalita37283c22016-09-13 10:00:23 -0700169 sk_sp<SkTextBlob> fBlob;
joshualitte46cf962015-08-26 11:19:55 -0700170
mtkleindbfd7ab2016-09-01 11:24:54 -0700171 static constexpr int kWidth = 2100;
172 static constexpr int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700173
174 bool fUseDFT;
175
176 typedef GM INHERITED;
177};
178
179//////////////////////////////////////////////////////////////////////////////
180
halcanary385fe4d2015-08-26 13:07:48 -0700181DEF_GM( return new TextBlobMixedSizes(false); )
halcanary385fe4d2015-08-26 13:07:48 -0700182DEF_GM( return new TextBlobMixedSizes(true); )
joshualitte46cf962015-08-26 11:19:55 -0700183}