Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
textblobmixedsizes.cpp
Go to the documentation of this file.
1/*
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/gm.h"
13#include "include/core/SkFont.h"
18#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
30#include "src/base/SkRandom.h"
31#include "src/core/SkBlurMask.h"
32#include "tools/Resources.h"
33#include "tools/ToolUtils.h"
35
36#include <string.h>
37
38namespace skiagm {
39class TextBlobMixedSizes : public GM {
40public:
41 // This gm tests that textblobs of mixed sizes with a large glyph will render properly
42 TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
43
44protected:
45 void onOnceBeforeDraw() override {
46 SkTextBlobBuilder builder;
47
48 // make textblob. To stress distance fields, we choose sizes appropriately
50 if (!tf) {
52 }
53 SkFont font(tf, 262);
54 font.setSubpixel(true);
56
57 const char* text = "Skia";
58
59 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
60
61 // large
62 SkRect bounds;
63 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
64 SkScalar yOffset = bounds.height();
65 font.setSize(162);
66
67 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
68
69 // Medium
70 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
71 yOffset += bounds.height();
72 font.setSize(72);
73
74 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
75
76 // Small
77 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
78 yOffset += bounds.height();
79 font.setSize(32);
80
81 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
82
83 // micro (will fall out of distance field text even if distance field text is enabled)
84 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
85 yOffset += bounds.height();
86 font.setSize(14);
87
88 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
89
90 // Zero size.
91 font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
92 yOffset += bounds.height();
93 font.setSize(0);
94
95 ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
96
97 // build
98 fBlob = builder.make();
99 }
100
101 SkString getName() const override {
102 return SkStringPrintf("textblobmixedsizes%s",
103 fUseDFT ? "_df" : "");
104 }
105
106 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
107
108 void onDraw(SkCanvas* inputCanvas) override {
109 SkCanvas* canvas = inputCanvas;
111 if (fUseDFT) {
112 // Create a new Canvas to enable DFT
113 auto ctx = inputCanvas->recordingContext();
114 SkISize size = getISize();
115 sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
116 SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
117 kPremul_SkAlphaType, colorSpace);
118 SkSurfaceProps inputProps;
119 inputCanvas->getProps(&inputProps);
120 SkSurfaceProps props(
122 inputProps.pixelGeometry());
124 canvas = surface ? surface->getCanvas() : inputCanvas;
125 // init our new canvas with the old canvas's matrix
126 canvas->setMatrix(inputCanvas->getTotalMatrix());
127 }
128 canvas->drawColor(SK_ColorWHITE);
129
130 SkRect bounds = fBlob->bounds();
131
132 const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
133 const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
134
135 int rowCount = 0;
136 canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
137 canvas->save();
138 SkRandom random;
139
141 if (!fUseDFT) {
142 paint.setColor(SK_ColorWHITE);
143 }
144 paint.setAntiAlias(false);
145
147
148 // setup blur paint
149 SkPaint blurPaint(paint);
150 blurPaint.setColor(SK_ColorBLACK);
152
153 for (int i = 0; i < 4; i++) {
154 canvas->save();
155 switch (i % 2) {
156 case 0:
157 canvas->rotate(random.nextF() * 45.f);
158 break;
159 case 1:
160 canvas->rotate(-random.nextF() * 45.f);
161 break;
162 }
163 if (!fUseDFT) {
164 canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
165 }
166 canvas->drawTextBlob(fBlob, 0, 0, paint);
167 canvas->restore();
168 canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
169 ++rowCount;
170 if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
171 canvas->restore();
172 canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
173 canvas->save();
174 rowCount = 0;
175 }
176 }
177 canvas->restore();
178
179 // render offscreen buffer
180 if (surface) {
181 SkAutoCanvasRestore acr(inputCanvas, true);
182 // since we prepended this matrix already, we blit using identity
183 inputCanvas->resetMatrix();
184 inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
185 }
186 }
187
188private:
189 sk_sp<SkTextBlob> fBlob;
190
191 static constexpr int kWidth = 2100;
192 static constexpr int kHeight = 1900;
193
194 bool fUseDFT;
195
196 using INHERITED = GM;
197};
198
199//////////////////////////////////////////////////////////////////////////////
200
201DEF_GM( return new TextBlobMixedSizes(false); )
202DEF_GM( return new TextBlobMixedSizes(true); )
203} // namespace skiagm
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kNormal_SkBlurStyle
fuzzy inside and outside
Definition SkBlurTypes.h:12
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarFloorToInt(x)
Definition SkScalar.h:35
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
static SkScalar SK_SPI ConvertRadiusToSigma(SkScalar radius)
bool getProps(SkSurfaceProps *props) const
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
virtual GrRecordingContext * recordingContext() const
void rotate(SkScalar degrees)
SkMatrix getTotalMatrix() const
void resetMatrix()
int save()
Definition SkCanvas.cpp:451
void setMatrix(const SkM44 &matrix)
SkImageInfo imageInfo() const
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
float nextF()
Definition SkRandom.h:55
@ kUseDeviceIndependentFonts_Flag
uint32_t flags() const
SkPixelGeometry pixelGeometry() const
const SkRect & bounds() const
Definition SkTextBlob.h:53
void onDraw(SkCanvas *inputCanvas) override
SkString getName() const override
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
std::u16string text
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
sk_sp< SkTypeface > DefaultPortableTypeface()
void add_to_text_blob(SkTextBlobBuilder *builder, const char *text, const SkFont &font, SkScalar x, SkScalar y)
sk_sp< SkTypeface > CreateTypefaceFromResource(const char *resource, int ttcIndex)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
sk_sp< SkColorSpace > refColorSpace() const
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)