blob: 8fdf558206136383eef6ceda8a8f438806747f71 [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 {
joshualitte46cf962015-08-26 11:19:55 -070030 SkTextBlobBuilder builder;
31
32 // make textblob. To stress distance fields, we choose sizes appropriately
33 SkPaint paint;
34 paint.setAntiAlias(true);
35 paint.setSubpixelText(true);
36 paint.setLCDRenderText(true);
bungeman13b9c952016-05-12 10:09:30 -070037 paint.setTypeface(MakeResourceAsTypeface("/fonts/HangingS.ttf"));
joshualitte46cf962015-08-26 11:19:55 -070038
joshualitt33e91f12015-09-18 11:28:59 -070039 const char* text = "Skia";
joshualitte46cf962015-08-26 11:19:55 -070040
41 // extra large
42 paint.setTextSize(262);
43
44 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
45
46 // large
47 SkRect bounds;
48 paint.measureText(text, strlen(text), &bounds);
49 SkScalar yOffset = bounds.height();
50 paint.setTextSize(162);
51
52 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
53
54 // Medium
55 paint.measureText(text, strlen(text), &bounds);
56 yOffset += bounds.height();
57 paint.setTextSize(72);
58
59 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
60
61 // Small
62 paint.measureText(text, strlen(text), &bounds);
63 yOffset += bounds.height();
64 paint.setTextSize(32);
65
66 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
67
68 // micro (will fall out of distance field text even if distance field text is enabled)
69 paint.measureText(text, strlen(text), &bounds);
70 yOffset += bounds.height();
71 paint.setTextSize(14);
72
73 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
74
kkinnunen68c63b32016-03-04 00:12:33 -080075 // Zero size.
76 paint.measureText(text, strlen(text), &bounds);
77 yOffset += bounds.height();
78 paint.setTextSize(0);
79
80 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
81
joshualitte46cf962015-08-26 11:19:55 -070082 // build
83 fBlob.reset(builder.build());
84 }
85
86 SkString onShortName() override {
87 SkString name("textblobmixedsizes");
88 if (fUseDFT) {
89 name.appendf("_df");
90 }
91 return name;
92 }
93
94 SkISize onISize() override {
95 return SkISize::Make(kWidth, kHeight);
96 }
97
98 void onDraw(SkCanvas* inputCanvas) override {
99 SkCanvas* canvas = inputCanvas;
reede8f30622016-03-23 18:59:25 -0700100 sk_sp<SkSurface> surface;
joshualitte46cf962015-08-26 11:19:55 -0700101 if (fUseDFT) {
102#if SK_SUPPORT_GPU
103 // Create a new Canvas to enable DFT
104 GrContext* ctx = inputCanvas->getGrContext();
brianosman944876f2016-06-17 13:43:27 -0700105 SkISize size = onISize();
106 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(inputCanvas->imageInfo().colorSpace());
107 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
108 kPremul_SkAlphaType, colorSpace);
brianosman898235c2016-04-06 07:38:23 -0700109 SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType);
brianosmanb461d342016-04-13 13:10:14 -0700110 uint32_t gammaCorrect = inputCanvas->getProps(&canvasProps)
111 ? canvasProps.flags() & SkSurfaceProps::kGammaCorrect_Flag : 0;
112 SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag | gammaCorrect,
joshualitte46cf962015-08-26 11:19:55 -0700113 SkSurfaceProps::kLegacyFontHost_InitType);
reede8f30622016-03-23 18:59:25 -0700114 surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
joshualitte46cf962015-08-26 11:19:55 -0700115 canvas = surface.get() ? surface->getCanvas() : inputCanvas;
116 // init our new canvas with the old canvas's matrix
117 canvas->setMatrix(inputCanvas->getTotalMatrix());
118#endif
119 }
120 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
121
122 SkRect bounds = fBlob->bounds();
123
124 static const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
125 static const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
126
127 int rowCount = 0;
128 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
129 canvas->save();
130 SkRandom random;
131
132 SkPaint paint;
133 if (!fUseDFT) {
134 paint.setColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
135 }
136 paint.setAntiAlias(false);
137
138 static const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
139
140 // setup blur paint
141 SkPaint blurPaint(paint);
142 blurPaint.setColor(sk_tool_utils::color_to_565(SK_ColorBLACK));
reedefdfd512016-04-04 10:02:58 -0700143 blurPaint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, kSigma));
144
joshualitte46cf962015-08-26 11:19:55 -0700145 for (int i = 0; i < 4; i++) {
146 canvas->save();
147 switch (i % 2) {
148 case 0:
149 canvas->rotate(random.nextF() * 45.f);
150 break;
151 case 1:
152 canvas->rotate(-random.nextF() * 45.f);
153 break;
154 }
155 if (!fUseDFT) {
156 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
157 }
158 canvas->drawTextBlob(fBlob, 0, 0, paint);
159 canvas->restore();
160 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
161 ++rowCount;
162 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
163 canvas->restore();
164 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
165 canvas->save();
166 rowCount = 0;
167 }
168 }
169 canvas->restore();
170
171#if SK_SUPPORT_GPU
172 // render offscreen buffer
173 if (surface) {
174 SkAutoCanvasRestore acr(inputCanvas, true);
175 // since we prepended this matrix already, we blit using identity
176 inputCanvas->resetMatrix();
reed9ce9d672016-03-17 10:51:11 -0700177 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
joshualitte46cf962015-08-26 11:19:55 -0700178 }
179#endif
180 }
181
182private:
183 SkAutoTUnref<const SkTextBlob> fBlob;
184
reed6dc14aa2016-04-11 07:46:38 -0700185 static const int kWidth = 2100;
186 static const int kHeight = 1900;
joshualitte46cf962015-08-26 11:19:55 -0700187
188 bool fUseDFT;
189
190 typedef GM INHERITED;
191};
192
193//////////////////////////////////////////////////////////////////////////////
194
halcanary385fe4d2015-08-26 13:07:48 -0700195DEF_GM( return new TextBlobMixedSizes(false); )
joshualitte46cf962015-08-26 11:19:55 -0700196#if SK_SUPPORT_GPU
halcanary385fe4d2015-08-26 13:07:48 -0700197DEF_GM( return new TextBlobMixedSizes(true); )
joshualitte46cf962015-08-26 11:19:55 -0700198#endif
199}