blob: 86ab0ac8fbecb4ab4e2e3ed97a90fe93138f992a [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"
9
10#include "Resources.h"
11#include "SkBlurMask.h"
12#include "SkBlurMaskFilter.h"
13#include "SkCanvas.h"
14#include "SkGradientShader.h"
15#include "SkImage.h"
16#include "SkRandom.h"
17#include "SkStream.h"
18#include "SkSurface.h"
19#include "SkTextBlob.h"
20#include "SkTypeface.h"
21
22namespace skiagm {
23class TextBlobMixedSizes : public GM {
24public:
25 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
26 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
27
28protected:
29 void onOnceBeforeDraw() override {
30 SkAutoTUnref<SkTypeface> typeface(GetResourceAsTypeface("/fonts/HangingS.ttf"));
31 SkTextBlobBuilder builder;
32
33 // make textblob. To stress distance fields, we choose sizes appropriately
34 SkPaint paint;
35 paint.setAntiAlias(true);
36 paint.setSubpixelText(true);
37 paint.setLCDRenderText(true);
38 paint.setTypeface(typeface);
39
joshualitt33e91f12015-09-18 11:28:59 -070040 const char* text = "Skia";
joshualitte46cf962015-08-26 11:19:55 -070041
42 // extra large
43 paint.setTextSize(262);
44
45 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
46
47 // large
48 SkRect bounds;
49 paint.measureText(text, strlen(text), &bounds);
50 SkScalar yOffset = bounds.height();
51 paint.setTextSize(162);
52
53 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
54
55 // Medium
56 paint.measureText(text, strlen(text), &bounds);
57 yOffset += bounds.height();
58 paint.setTextSize(72);
59
60 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
61
62 // Small
63 paint.measureText(text, strlen(text), &bounds);
64 yOffset += bounds.height();
65 paint.setTextSize(32);
66
67 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
68
69 // micro (will fall out of distance field text even if distance field text is enabled)
70 paint.measureText(text, strlen(text), &bounds);
71 yOffset += bounds.height();
72 paint.setTextSize(14);
73
74 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
75
kkinnunen68c63b32016-03-04 00:12:33 -080076 // Zero size.
77 paint.measureText(text, strlen(text), &bounds);
78 yOffset += bounds.height();
79 paint.setTextSize(0);
80
81 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
82
joshualitte46cf962015-08-26 11:19:55 -070083 // build
84 fBlob.reset(builder.build());
85 }
86
87 SkString onShortName() override {
88 SkString name("textblobmixedsizes");
89 if (fUseDFT) {
90 name.appendf("_df");
91 }
92 return name;
93 }
94
95 SkISize onISize() override {
96 return SkISize::Make(kWidth, kHeight);
97 }
98
99 void onDraw(SkCanvas* inputCanvas) override {
100 SkCanvas* canvas = inputCanvas;
101 SkAutoTUnref<SkSurface> surface;
102 if (fUseDFT) {
103#if SK_SUPPORT_GPU
104 // Create a new Canvas to enable DFT
105 GrContext* ctx = inputCanvas->getGrContext();
106 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize());
bsalomonafcd7cd2015-08-31 12:39:41 -0700107 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
joshualitte46cf962015-08-26 11:19:55 -0700108 SkSurfaceProps::kLegacyFontHost_InitType);
bsalomon5ec26ae2016-02-25 08:33:02 -0800109 surface.reset(SkSurface::NewRenderTarget(ctx, SkBudgeted::kNo, info, 0,
joshualitte46cf962015-08-26 11:19:55 -0700110 &props));
111 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
112 // init our new canvas with the old canvas's matrix
113 canvas->setMatrix(inputCanvas->getTotalMatrix());
114#endif
115 }
116 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
117
118 SkRect bounds = fBlob->bounds();
119
120 static const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
121 static const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
122
123 int rowCount = 0;
124 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
125 canvas->save();
126 SkRandom random;
127
128 SkPaint paint;
129 if (!fUseDFT) {
130 paint.setColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
131 }
132 paint.setAntiAlias(false);
133
134 static const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
135
136 // setup blur paint
137 SkPaint blurPaint(paint);
138 blurPaint.setColor(sk_tool_utils::color_to_565(SK_ColorBLACK));
139 SkAutoTUnref<SkMaskFilter> mf(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, kSigma));
140 blurPaint.setMaskFilter(mf);
141
142 for (int i = 0; i < 4; i++) {
143 canvas->save();
144 switch (i % 2) {
145 case 0:
146 canvas->rotate(random.nextF() * 45.f);
147 break;
148 case 1:
149 canvas->rotate(-random.nextF() * 45.f);
150 break;
151 }
152 if (!fUseDFT) {
153 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
154 }
155 canvas->drawTextBlob(fBlob, 0, 0, paint);
156 canvas->restore();
157 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
158 ++rowCount;
159 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
160 canvas->restore();
161 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
162 canvas->save();
163 rowCount = 0;
164 }
165 }
166 canvas->restore();
167
168#if SK_SUPPORT_GPU
169 // render offscreen buffer
170 if (surface) {
171 SkAutoCanvasRestore acr(inputCanvas, true);
172 // since we prepended this matrix already, we blit using identity
173 inputCanvas->resetMatrix();
174 SkImage* image = surface->newImageSnapshot();
halcanary96fcdcc2015-08-27 07:41:13 -0700175 inputCanvas->drawImage(image, 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700176 image->unref();
177 }
178#endif
179 }
180
181private:
182 SkAutoTUnref<const SkTextBlob> fBlob;
183
184 static const int kWidth = 2000;
185 static const int kHeight = 2000;
186
187 bool fUseDFT;
188
189 typedef GM INHERITED;
190};
191
192//////////////////////////////////////////////////////////////////////////////
193
halcanary385fe4d2015-08-26 13:07:48 -0700194DEF_GM( return new TextBlobMixedSizes(false); )
joshualitte46cf962015-08-26 11:19:55 -0700195#if SK_SUPPORT_GPU
halcanary385fe4d2015-08-26 13:07:48 -0700196DEF_GM( return new TextBlobMixedSizes(true); )
joshualitte46cf962015-08-26 11:19:55 -0700197#endif
198}