blob: 2246350ea813470c173448b559c66946f8dd3f54 [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 {
Hal Canary58822d62017-11-14 12:08:34 -050083 return SkStringPrintf("textblobmixedsizes%s%s",
84 sk_tool_utils::platform_font_manager(),
85 fUseDFT ? "_df" : "");
joshualitte46cf962015-08-26 11:19:55 -070086 }
87
88 SkISize onISize() override {
89 return SkISize::Make(kWidth, kHeight);
90 }
91
92 void onDraw(SkCanvas* inputCanvas) override {
93 SkCanvas* canvas = inputCanvas;
reede8f30622016-03-23 18:59:25 -070094 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -070095 if (fUseDFT) {
joshualitte46cf962015-08-26 11:19:55 -070096 // Create a new Canvas to enable DFT
97 GrContext* ctx = inputCanvas->getGrContext();
brianosman52ede1d2016-06-20 08:25:02 -070098 SkISize size = onISize();
Mike Reed693fdbd2017-01-12 10:13:40 -050099 sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
brianosman52ede1d2016-06-20 08:25:02 -0700100 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
101 kPremul_SkAlphaType, colorSpace);
brianosman3a0dbde2016-07-26 11:36:05 -0700102 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
joshualitte46cf962015-08-26 11:19:55 -0700103 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700104 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
joshualitte46cf962015-08-26 11:19:55 -0700105 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
106 // init our new canvas with the old canvas's matrix
107 canvas->setMatrix(inputCanvas->getTotalMatrix());
joshualitte46cf962015-08-26 11:19:55 -0700108 }
Mike Kleind46dce32018-08-16 10:17:03 -0400109 canvas->drawColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700110
111 SkRect bounds = fBlob->bounds();
112
mtkleindbfd7ab2016-09-01 11:24:54 -0700113 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
114 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
joshualitte46cf962015-08-26 11:19:55 -0700115
116 int rowCount = 0;
117 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
118 canvas->save();
119 SkRandom random;
120
121 SkPaint paint;
122 if (!fUseDFT) {
Mike Kleind46dce32018-08-16 10:17:03 -0400123 paint.setColor(SK_ColorWHITE);
joshualitte46cf962015-08-26 11:19:55 -0700124 }
125 paint.setAntiAlias(false);
126
mtkleindbfd7ab2016-09-01 11:24:54 -0700127 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
joshualitte46cf962015-08-26 11:19:55 -0700128
129 // setup blur paint
130 SkPaint blurPaint(paint);
Mike Kleind46dce32018-08-16 10:17:03 -0400131 blurPaint.setColor(SK_ColorBLACK);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400132 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma));
mtkleindbfd7ab2016-09-01 11:24:54 -0700133
joshualitte46cf962015-08-26 11:19:55 -0700134 for (int i = 0; i < 4; i++) {
135 canvas->save();
136 switch (i % 2) {
137 case 0:
138 canvas->rotate(random.nextF() * 45.f);
139 break;
140 case 1:
141 canvas->rotate(-random.nextF() * 45.f);
142 break;
143 }
144 if (!fUseDFT) {
145 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
146 }
147 canvas->drawTextBlob(fBlob, 0, 0, paint);
148 canvas->restore();
149 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
150 ++rowCount;
151 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
152 canvas->restore();
153 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
154 canvas->save();
155 rowCount = 0;
156 }
157 }
158 canvas->restore();
159
joshualitte46cf962015-08-26 11:19:55 -0700160 // render offscreen buffer
161 if (surface) {
162 SkAutoCanvasRestore acr(inputCanvas, true);
163 // since we prepended this matrix already, we blit using identity
164 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700165 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700166 }
joshualitte46cf962015-08-26 11:19:55 -0700167 }
168
169private:
fmalita37283c22016-09-13 10:00:23 -0700170 sk_sp<SkTextBlob> fBlob;
joshualitte46cf962015-08-26 11:19:55 -0700171
mtkleindbfd7ab2016-09-01 11:24:54 -0700172 static constexpr int kWidth = 2100;
173 static constexpr int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700174
175 bool fUseDFT;
176
177 typedef GM INHERITED;
178};
179
180//////////////////////////////////////////////////////////////////////////////
181
halcanary385fe4d2015-08-26 13:07:48 -0700182DEF_GM( return new TextBlobMixedSizes(false); )
halcanary385fe4d2015-08-26 13:07:48 -0700183DEF_GM( return new TextBlobMixedSizes(true); )
joshualitte46cf962015-08-26 11:19:55 -0700184}