2015-08-26 18:19:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkBlurMask.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkImage.h"
|
2018-03-14 17:01:17 +00:00
|
|
|
#include "SkMaskFilter.h"
|
2015-08-26 18:19:55 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
class TextBlobMixedSizes : public GM {
|
|
|
|
public:
|
|
|
|
// This gm tests that textblobs of mixed sizes with a large glyph will render properly
|
|
|
|
TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
|
|
|
|
// make textblob. To stress distance fields, we choose sizes appropriately
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setSubpixelText(true);
|
|
|
|
paint.setLCDRenderText(true);
|
2017-12-08 19:25:14 +00:00
|
|
|
paint.setTypeface(MakeResourceAsTypeface("fonts/HangingS.ttf"));
|
2015-08-26 18:19:55 +00:00
|
|
|
|
2015-09-18 18:28:59 +00:00
|
|
|
const char* text = "Skia";
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
// extra large
|
|
|
|
paint.setTextSize(262);
|
|
|
|
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
|
|
|
|
|
|
|
|
// large
|
|
|
|
SkRect bounds;
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
SkScalar yOffset = bounds.height();
|
|
|
|
paint.setTextSize(162);
|
|
|
|
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
|
|
|
|
|
|
|
|
// Medium
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
yOffset += bounds.height();
|
|
|
|
paint.setTextSize(72);
|
|
|
|
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
|
|
|
|
|
|
|
|
// Small
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
yOffset += bounds.height();
|
|
|
|
paint.setTextSize(32);
|
|
|
|
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
|
|
|
|
|
|
|
|
// micro (will fall out of distance field text even if distance field text is enabled)
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
yOffset += bounds.height();
|
|
|
|
paint.setTextSize(14);
|
|
|
|
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
|
|
|
|
|
2016-03-04 08:12:33 +00:00
|
|
|
// Zero size.
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
yOffset += bounds.height();
|
|
|
|
paint.setTextSize(0);
|
|
|
|
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, yOffset);
|
|
|
|
|
2015-08-26 18:19:55 +00:00
|
|
|
// build
|
2016-09-13 17:00:23 +00:00
|
|
|
fBlob = builder.make();
|
2015-08-26 18:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
2017-11-14 17:08:34 +00:00
|
|
|
return SkStringPrintf("textblobmixedsizes%s%s",
|
|
|
|
sk_tool_utils::platform_font_manager(),
|
|
|
|
fUseDFT ? "_df" : "");
|
2015-08-26 18:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* inputCanvas) override {
|
|
|
|
SkCanvas* canvas = inputCanvas;
|
2016-03-24 01:59:25 +00:00
|
|
|
sk_sp<SkSurface> surface;
|
2015-08-26 18:19:55 +00:00
|
|
|
if (fUseDFT) {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
// Create a new Canvas to enable DFT
|
|
|
|
GrContext* ctx = inputCanvas->getGrContext();
|
2016-06-20 15:25:02 +00:00
|
|
|
SkISize size = onISize();
|
2017-01-12 15:13:40 +00:00
|
|
|
sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace();
|
2016-06-20 15:25:02 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(),
|
|
|
|
kPremul_SkAlphaType, colorSpace);
|
2016-07-26 18:36:05 +00:00
|
|
|
SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag,
|
2015-08-26 18:19:55 +00:00
|
|
|
SkSurfaceProps::kLegacyFontHost_InitType);
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
|
2015-08-26 18:19:55 +00:00
|
|
|
canvas = surface.get() ? surface->getCanvas() : inputCanvas;
|
|
|
|
// init our new canvas with the old canvas's matrix
|
|
|
|
canvas->setMatrix(inputCanvas->getTotalMatrix());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
|
|
|
|
|
|
|
|
SkRect bounds = fBlob->bounds();
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const int kPadX = SkScalarFloorToInt(bounds.width() / 3);
|
|
|
|
const int kPadY = SkScalarFloorToInt(bounds.height() / 3);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
int rowCount = 0;
|
|
|
|
canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
|
|
|
|
canvas->save();
|
|
|
|
SkRandom random;
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
if (!fUseDFT) {
|
|
|
|
paint.setColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
|
|
|
|
}
|
|
|
|
paint.setAntiAlias(false);
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8));
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
// setup blur paint
|
|
|
|
SkPaint blurPaint(paint);
|
|
|
|
blurPaint.setColor(sk_tool_utils::color_to_565(SK_ColorBLACK));
|
2018-03-14 17:01:17 +00:00
|
|
|
blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma));
|
2016-09-01 18:24:54 +00:00
|
|
|
|
2015-08-26 18:19:55 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
canvas->save();
|
|
|
|
switch (i % 2) {
|
|
|
|
case 0:
|
|
|
|
canvas->rotate(random.nextF() * 45.f);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
canvas->rotate(-random.nextF() * 45.f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!fUseDFT) {
|
|
|
|
canvas->drawTextBlob(fBlob, 0, 0, blurPaint);
|
|
|
|
}
|
|
|
|
canvas->drawTextBlob(fBlob, 0, 0, paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0);
|
|
|
|
++rowCount;
|
|
|
|
if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY);
|
|
|
|
canvas->save();
|
|
|
|
rowCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
// render offscreen buffer
|
|
|
|
if (surface) {
|
|
|
|
SkAutoCanvasRestore acr(inputCanvas, true);
|
|
|
|
// since we prepended this matrix already, we blit using identity
|
|
|
|
inputCanvas->resetMatrix();
|
2016-03-17 17:51:11 +00:00
|
|
|
inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr);
|
2015-08-26 18:19:55 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> fBlob;
|
2015-08-26 18:19:55 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kWidth = 2100;
|
|
|
|
static constexpr int kHeight = 1900;
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
bool fUseDFT;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM( return new TextBlobMixedSizes(false); )
|
2015-08-26 18:19:55 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM( return new TextBlobMixedSizes(true); )
|
2015-08-26 18:19:55 +00:00
|
|
|
#endif
|
|
|
|
}
|