blob: 9e57c2fe975245d56b7563d02757da0079ed45bd [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;
reede8f30622016-03-23 18:59:25 -0700101 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -0700102 if (fUseDFT) {
103#if SK_SUPPORT_GPU
104 // Create a new Canvas to enable DFT
105 GrContext* ctx = inputCanvas->getGrContext();
brianosman898235c2016-04-06 07:38:23 -0700106 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize(),
107 inputCanvas->imageInfo().profileType());
108 SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType);
brianosmanb461d342016-04-13 13:10:14 -0700109 uint32_t gammaCorrect = inputCanvas->getProps(&canvasProps)
110 ? canvasProps.flags() & SkSurfaceProps::kGammaCorrect_Flag : 0;
111 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag | gammaCorrect,
joshualitte46cf962015-08-26 11:19:55 -0700112 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700113 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
joshualitte46cf962015-08-26 11:19:55 -0700114 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
115 // init our new canvas with the old canvas's matrix
116 canvas->setMatrix(inputCanvas->getTotalMatrix());
117#endif
118 }
119 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
120
121 SkRect bounds = fBlob->bounds();
122
123 static const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
124 static const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
125
126 int rowCount = 0;
127 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
128 canvas->save();
129 SkRandom random;
130
131 SkPaint paint;
132 if (!fUseDFT) {
133 paint.setColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
134 }
135 paint.setAntiAlias(false);
136
137 static const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
138
139 // setup blur paint
140 SkPaint blurPaint(paint);
141 blurPaint.setColor(sk_tool_utils::color_to_565(SK_ColorBLACK));
reedefdfd512016-04-04 10:02:58 -0700142 blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, kSigma));
143
joshualitte46cf962015-08-26 11:19:55 -0700144 for (int i = 0; i < 4; i++) {
145 canvas->save();
146 switch (i % 2) {
147 case 0:
148 canvas->rotate(random.nextF() * 45.f);
149 break;
150 case 1:
151 canvas->rotate(-random.nextF() * 45.f);
152 break;
153 }
154 if (!fUseDFT) {
155 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
156 }
157 canvas->drawTextBlob(fBlob, 0, 0, paint);
158 canvas->restore();
159 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
160 ++rowCount;
161 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
162 canvas->restore();
163 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
164 canvas->save();
165 rowCount = 0;
166 }
167 }
168 canvas->restore();
169
170#if SK_SUPPORT_GPU
171 // render offscreen buffer
172 if (surface) {
173 SkAutoCanvasRestore acr(inputCanvas, true);
174 // since we prepended this matrix already, we blit using identity
175 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700176 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700177 }
178#endif
179 }
180
181private:
182 SkAutoTUnref<const SkTextBlob> fBlob;
183
reed6dc14aa2016-04-11 07:46:38 -0700184 static const int kWidth = 2100;
185 static const int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700186
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}