2015-08-01 14:33:41 +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-01 14:33:41 +00:00
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
2017-11-10 16:33:43 +00:00
|
|
|
#include "SkRandomScalerContext.h"
|
2015-08-01 14:33:41 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "SkTextBlob.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
class TextBlobRandomFont : public GM {
|
|
|
|
public:
|
|
|
|
// This gm tests that textblobs can be translated and scaled with a font that returns random
|
|
|
|
// but deterministic masks
|
|
|
|
TextBlobRandomFont() { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkTextBlobBuilder builder;
|
|
|
|
|
|
|
|
const char* text = "The quick brown fox jumps over the lazy dog.";
|
|
|
|
|
|
|
|
SkPaint paint;
|
2018-04-30 19:39:15 +00:00
|
|
|
paint.setAntiAlias(true);
|
2018-12-22 22:37:30 +00:00
|
|
|
paint.setColor(SK_ColorMAGENTA);
|
|
|
|
|
|
|
|
// make textbloben
|
|
|
|
SkFont font;
|
|
|
|
font.setSize(32);
|
|
|
|
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
// Setup our random scaler context
|
2018-04-30 19:39:15 +00:00
|
|
|
auto typeface = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
|
|
|
|
if (!typeface) {
|
|
|
|
typeface = SkTypeface::MakeDefault();
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
2018-12-22 22:37:30 +00:00
|
|
|
font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false));
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2018-04-30 19:39:15 +00:00
|
|
|
SkScalar y = 0;
|
2015-08-01 14:33:41 +00:00
|
|
|
SkRect bounds;
|
2018-12-22 22:37:30 +00:00
|
|
|
font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2018-12-22 22:37:30 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
// A8
|
2015-08-01 17:33:40 +00:00
|
|
|
const char* bigtext1 = "The quick brown fox";
|
|
|
|
const char* bigtext2 = "jumps over the lazy dog.";
|
2018-12-22 22:37:30 +00:00
|
|
|
font.setSize(160);
|
|
|
|
font.setSubpixel(false);
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
|
|
|
font.measureText(bigtext1, strlen(bigtext1), kUTF8_SkTextEncoding, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2018-12-22 22:37:30 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, bigtext1, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 17:33:40 +00:00
|
|
|
|
2018-12-22 22:37:30 +00:00
|
|
|
font.measureText(bigtext2, strlen(bigtext2), kUTF8_SkTextEncoding, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2018-12-22 22:37:30 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, bigtext2, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 17:33:40 +00:00
|
|
|
|
|
|
|
// color emoji
|
2017-11-13 16:03:16 +00:00
|
|
|
if (sk_sp<SkTypeface> origEmoji = sk_tool_utils::emoji_typeface()) {
|
2018-12-22 22:37:30 +00:00
|
|
|
font.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false));
|
2015-08-01 17:33:40 +00:00
|
|
|
const char* emojiText = sk_tool_utils::emoji_sample_text();
|
2018-12-22 22:37:30 +00:00
|
|
|
font.measureText(emojiText, strlen(emojiText), kUTF8_SkTextEncoding, &bounds);
|
2018-04-30 19:39:15 +00:00
|
|
|
y -= bounds.fTop;
|
2018-12-22 22:37:30 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, emojiText, font, 0, y);
|
2018-04-30 19:39:15 +00:00
|
|
|
y += bounds.fBottom;
|
2015-08-01 17:33:40 +00:00
|
|
|
}
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
// build
|
2016-09-13 17:00:23 +00:00
|
|
|
fBlob = builder.make();
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
|
|
|
return SkString("textblobrandomfont");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
// This GM exists to test a specific feature of the GPU backend.
|
2018-04-30 19:39:15 +00:00
|
|
|
// This GM uses sk_tool_utils::makeSurface which doesn't work well with vias.
|
|
|
|
// This GM uses SkRandomTypeface which doesn't work well with serialization.
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == canvas->getGrContext()) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2015-08-01 14:33:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2016-12-02 17:07:17 +00:00
|
|
|
SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(),
|
|
|
|
kPremul_SkAlphaType,
|
2017-01-12 15:13:40 +00:00
|
|
|
canvas->imageInfo().refColorSpace());
|
2016-07-26 18:36:05 +00:00
|
|
|
SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
|
2018-09-05 18:41:40 +00:00
|
|
|
auto surface(sk_tool_utils::makeSurface(canvas, info, &props));
|
2018-04-30 19:39:15 +00:00
|
|
|
if (!surface) {
|
|
|
|
const char* text = "This test requires a surface";
|
|
|
|
size_t len = strlen(text);
|
2015-08-01 14:33:41 +00:00
|
|
|
SkPaint paint;
|
2018-04-30 19:39:15 +00:00
|
|
|
canvas->drawText(text, len, 10, 100, paint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2018-05-24 18:32:26 +00:00
|
|
|
SkCanvas* surfaceCanvas = surface->getCanvas();
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2018-04-30 19:39:15 +00:00
|
|
|
SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height());
|
|
|
|
SkScalar yOffset = 5;
|
2018-05-24 18:32:26 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
// Originally we would alternate between rotating and not to force blob regeneration,
|
|
|
|
// but that code seems to have rotted. Keeping the rotate to match the old GM as
|
|
|
|
// much as possible, and it seems like a reasonable stress test for transformed
|
|
|
|
// color emoji.
|
|
|
|
canvas->rotate(-0.05f);
|
|
|
|
canvas->drawTextBlob(fBlob, 10, yOffset, paint);
|
|
|
|
yOffset += stride;
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
// this will test lcd masks when not requested
|
|
|
|
// on cpu this currently causes unspecified behavior, so avoid until it is fixed
|
|
|
|
if (canvas->getGrContext()) {
|
|
|
|
// Rotate in the surface canvas, not the final canvas, to avoid aliasing
|
|
|
|
surfaceCanvas->rotate(-0.05f);
|
|
|
|
surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint);
|
|
|
|
surface->draw(canvas, 0, 0, nullptr);
|
|
|
|
}
|
|
|
|
yOffset += stride;
|
|
|
|
|
|
|
|
// free gpu resources and verify
|
|
|
|
if (canvas->getGrContext()) {
|
|
|
|
canvas->getGrContext()->freeGpuResources();
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
2018-05-24 18:32:26 +00:00
|
|
|
|
|
|
|
canvas->rotate(-0.05f);
|
|
|
|
canvas->drawTextBlob(fBlob, 10, yOffset, paint);
|
|
|
|
yOffset += stride;
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-13 17:00:23 +00:00
|
|
|
sk_sp<SkTextBlob> fBlob;
|
2015-08-01 14:33:41 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kWidth = 2000;
|
|
|
|
static constexpr int kHeight = 1600;
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new TextBlobRandomFont;)
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|