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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlurTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorSpace.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSurfaceProps.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTextBlob.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
|
|
|
#include "tools/Resources.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-08-26 18:19:55 +00:00
|
|
|
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
|
2018-12-23 03:29:45 +00:00
|
|
|
SkFont font(MakeResourceAsTypeface("fonts/HangingS.ttf"), 262);
|
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
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
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
// large
|
|
|
|
SkRect bounds;
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2015-08-26 18:19:55 +00:00
|
|
|
SkScalar yOffset = bounds.height();
|
2018-12-23 03:29:45 +00:00
|
|
|
font.setSize(162);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
// Medium
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2015-08-26 18:19:55 +00:00
|
|
|
yOffset += bounds.height();
|
2018-12-23 03:29:45 +00:00
|
|
|
font.setSize(72);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
// Small
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2015-08-26 18:19:55 +00:00
|
|
|
yOffset += bounds.height();
|
2018-12-23 03:29:45 +00:00
|
|
|
font.setSize(32);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
// micro (will fall out of distance field text even if distance field text is enabled)
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2015-08-26 18:19:55 +00:00
|
|
|
yOffset += bounds.height();
|
2018-12-23 03:29:45 +00:00
|
|
|
font.setSize(14);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
2016-03-04 08:12:33 +00:00
|
|
|
// Zero size.
|
2019-05-07 19:38:46 +00:00
|
|
|
font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds);
|
2016-03-04 08:12:33 +00:00
|
|
|
yOffset += bounds.height();
|
2018-12-23 03:29:45 +00:00
|
|
|
font.setSize(0);
|
2016-03-04 08:12:33 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset);
|
2016-03-04 08:12:33 +00:00
|
|
|
|
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 {
|
2019-03-08 17:11:55 +00:00
|
|
|
return SkStringPrintf("textblobmixedsizes%s",
|
2017-11-14 17:08:34 +00:00
|
|
|
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) {
|
|
|
|
// Create a new Canvas to enable DFT
|
2020-07-07 14:20:27 +00:00
|
|
|
auto ctx = inputCanvas->recordingContext();
|
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);
|
2020-09-24 18:49:00 +00:00
|
|
|
SkSurfaceProps inputProps;
|
|
|
|
inputCanvas->getProps(&inputProps);
|
|
|
|
SkSurfaceProps props(
|
|
|
|
SkSurfaceProps::kUseDeviceIndependentFonts_Flag | inputProps.flags(),
|
|
|
|
inputProps.pixelGeometry());
|
2016-03-24 01:59:25 +00:00
|
|
|
surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props);
|
2020-08-16 12:48:02 +00:00
|
|
|
canvas = surface ? surface->getCanvas() : inputCanvas;
|
2015-08-26 18:19:55 +00:00
|
|
|
// init our new canvas with the old canvas's matrix
|
|
|
|
canvas->setMatrix(inputCanvas->getTotalMatrix());
|
|
|
|
}
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
2015-08-26 18:19:55 +00:00
|
|
|
|
|
|
|
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) {
|
2018-08-16 14:17:03 +00:00
|
|
|
paint.setColor(SK_ColorWHITE);
|
2015-08-26 18:19:55 +00:00
|
|
|
}
|
|
|
|
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);
|
2018-08-16 14:17:03 +00:00
|
|
|
blurPaint.setColor(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();
|
|
|
|
|
|
|
|
// render offscreen buffer
|
|
|
|
if (surface) {
|
|
|
|
SkAutoCanvasRestore acr(inputCanvas, true);
|
|
|
|
// since we prepended this matrix already, we blit using identity
|
|
|
|
inputCanvas->resetMatrix();
|
2021-01-23 23:10:39 +00:00
|
|
|
inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0);
|
2015-08-26 18:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2015-08-26 18:19:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM( return new TextBlobMixedSizes(false); )
|
|
|
|
DEF_GM( return new TextBlobMixedSizes(true); )
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|