| 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" |
| 9 |
#include "include/core/SkBlurTypes.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/core/SkFontTypes.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkMaskFilter.h" |
| 17 |
#include "include/core/SkPaint.h" |
| 18 |
#include "include/core/SkRect.h" |
| 19 |
#include "include/core/SkRefCnt.h" |
| 20 |
#include "include/core/SkScalar.h" |
| 21 |
#include "include/core/SkSize.h" |
| 22 |
#include "include/core/SkString.h" |
| 23 |
#include "include/core/SkSurface.h" |
| 24 |
#include "include/core/SkSurfaceProps.h" |
| 25 |
#include "include/core/SkTextBlob.h" |
| 26 |
#include "include/core/SkTypeface.h" |
| 27 |
#include "include/core/SkTypes.h" |
| 28 |
#include "include/gpu/GpuTypes.h" |
| 29 |
#include "src/base/SkRandom.h" |
| 30 |
#include "src/core/SkBlurMask.h" |
| 31 |
#include "tools/Resources.h" |
| 32 |
#include "tools/ToolUtils.h" |
| 33 |
#include "tools/fonts/FontToolUtils.h" |
| 34 |
|
| 35 |
#if defined(SK_GANESH) |
| 36 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#if defined(SK_GRAPHITE) |
| 40 |
#include "include/gpu/graphite/Surface.h" |
| 41 |
#endif |
| 42 |
|
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
namespace skiagm { |
| 46 |
class TextBlobMixedSizes : public GM { |
| 47 |
public: |
| 48 |
// This gm tests that textblobs of mixed sizes with a large glyph will render properly |
| 49 |
TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {} |
| 50 |
|
| 51 |
protected: |
| 52 |
void onOnceBeforeDraw() override { |
| 53 |
SkTextBlobBuilder builder; |
| 54 |
|
| 55 |
// make textblob. To stress distance fields, we choose sizes appropriately |
| 56 |
sk_sp<SkTypeface> tf = ToolUtils::CreateTypefaceFromResource("fonts/HangingS.ttf"); |
| 57 |
if (!tf) { |
| 58 |
tf = ToolUtils::DefaultPortableTypeface(); |
| 59 |
} |
| 60 |
SkFont font(tf, 262); |
| 61 |
font.setSubpixel(true); |
| 62 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 63 |
|
| 64 |
const char* text = "Skia"; |
| 65 |
|
| 66 |
ToolUtils::add_to_text_blob(&builder, text, font, 0, 0); |
| 67 |
|
| 68 |
// large |
| 69 |
SkRect bounds; |
| 70 |
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
| 71 |
SkScalar yOffset = bounds.height(); |
| 72 |
font.setSize(162); |
| 73 |
|
| 74 |
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
| 75 |
|
| 76 |
// Medium |
| 77 |
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
| 78 |
yOffset += bounds.height(); |
| 79 |
font.setSize(72); |
| 80 |
|
| 81 |
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
| 82 |
|
| 83 |
// Small |
| 84 |
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
| 85 |
yOffset += bounds.height(); |
| 86 |
font.setSize(32); |
| 87 |
|
| 88 |
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
| 89 |
|
| 90 |
// micro (will fall out of distance field text even if distance field text is enabled) |
| 91 |
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
| 92 |
yOffset += bounds.height(); |
| 93 |
font.setSize(14); |
| 94 |
|
| 95 |
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
| 96 |
|
| 97 |
// Zero size. |
| 98 |
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
| 99 |
yOffset += bounds.height(); |
| 100 |
font.setSize(0); |
| 101 |
|
| 102 |
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
| 103 |
|
| 104 |
// build |
| 105 |
fBlob = builder.make(); |
| 106 |
} |
| 107 |
|
| 108 |
SkString getName() const override { |
| 109 |
return SkStringPrintf("textblobmixedsizes%s", |
| 110 |
fUseDFT ? "_df" : ""); |
| 111 |
} |
| 112 |
|
| 113 |
SkISize getISize() override { return SkISize::Make(kWidth, kHeight); } |
| 114 |
|
| 115 |
void onDraw(SkCanvas* inputCanvas) override { |
| 116 |
SkCanvas* canvas = inputCanvas; |
| 117 |
sk_sp<SkSurface> surface; |
| 118 |
if (fUseDFT) { |
| 119 |
// Create a new Canvas to enable DFT |
| 120 |
SkISize size = this->getISize(); |
| 121 |
if (!inputCanvas->getBaseLayerSize().isEmpty()) { |
| 122 |
size = inputCanvas->getBaseLayerSize(); |
| 123 |
} |
| 124 |
sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace(); |
| 125 |
SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), |
| 126 |
kPremul_SkAlphaType, colorSpace); |
| 127 |
SkSurfaceProps inputProps; |
| 128 |
inputCanvas->getProps(&inputProps); |
| 129 |
SkSurfaceProps props( |
| 130 |
SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(), |
| 131 |
inputProps.pixelGeometry()); |
| 132 |
#if defined(SK_GRAPHITE) |
| 133 |
if (auto recorder = inputCanvas->recorder()) { |
| 134 |
surface = SkSurfaces::RenderTarget(recorder, info, skgpu::Mipmapped::kNo, &props); |
| 135 |
} else |
| 136 |
#endif |
| 137 |
#if defined(SK_GANESH) |
| 138 |
if (auto ctx = inputCanvas->recordingContext()) { |
| 139 |
surface = SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info, 0, &props); |
| 140 |
} |
| 141 |
#endif |
| 142 |
canvas = surface ? surface->getCanvas() : inputCanvas; |
| 143 |
// init our new canvas with the old canvas's matrix |
| 144 |
canvas->setMatrix(inputCanvas->getTotalMatrix()); |
| 145 |
} |
| 146 |
canvas->drawColor(SK_ColorWHITE); |
| 147 |
|
| 148 |
SkRect bounds = fBlob->bounds(); |
| 149 |
|
| 150 |
const int kPadX = SkScalarFloorToInt(bounds.width() / 3); |
| 151 |
const int kPadY = SkScalarFloorToInt(bounds.height() / 3); |
| 152 |
|
| 153 |
int rowCount = 0; |
| 154 |
canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY)); |
| 155 |
canvas->save(); |
| 156 |
SkRandom random; |
| 157 |
|
| 158 |
SkPaint paint; |
| 159 |
if (!fUseDFT) { |
| 160 |
paint.setColor(SK_ColorWHITE); |
| 161 |
} |
| 162 |
paint.setAntiAlias(false); |
| 163 |
|
| 164 |
const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8)); |
| 165 |
|
| 166 |
// setup blur paint |
| 167 |
SkPaint blurPaint(paint); |
| 168 |
blurPaint.setColor(SK_ColorBLACK); |
| 169 |
blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma)); |
| 170 |
|
| 171 |
for (int i = 0; i < 4; i++) { |
| 172 |
canvas->save(); |
| 173 |
switch (i % 2) { |
| 174 |
case 0: |
| 175 |
canvas->rotate(random.nextF() * 45.f); |
| 176 |
break; |
| 177 |
case 1: |
| 178 |
canvas->rotate(-random.nextF() * 45.f); |
| 179 |
break; |
| 180 |
} |
| 181 |
if (!fUseDFT) { |
| 182 |
canvas->drawTextBlob(fBlob, 0, 0, blurPaint); |
| 183 |
} |
| 184 |
canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 185 |
canvas->restore(); |
| 186 |
canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0); |
| 187 |
++rowCount; |
| 188 |
if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) { |
| 189 |
canvas->restore(); |
| 190 |
canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY); |
| 191 |
canvas->save(); |
| 192 |
rowCount = 0; |
| 193 |
} |
| 194 |
} |
| 195 |
canvas->restore(); |
| 196 |
|
| 197 |
// render offscreen buffer |
| 198 |
if (surface) { |
| 199 |
SkAutoCanvasRestore acr(inputCanvas, true); |
| 200 |
// since we prepended this matrix already, we blit using identity |
| 201 |
inputCanvas->resetMatrix(); |
| 202 |
inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private: |
| 207 |
sk_sp<SkTextBlob> fBlob; |
| 208 |
|
| 209 |
static constexpr int kWidth = 2100; |
| 210 |
static constexpr int kHeight = 1900; |
| 211 |
|
| 212 |
bool fUseDFT; |
| 213 |
|
| 214 |
using INHERITED = GM; |
| 215 |
}; |
| 216 |
|
| 217 |
////////////////////////////////////////////////////////////////////////////// |
| 218 |
|
| 219 |
DEF_GM( return new TextBlobMixedSizes(false); ) |
| 220 |
DEF_GM( return new TextBlobMixedSizes(true); ) |
| 221 |
} // namespace skiagm |