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"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#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.";
|
|
|
|
|
|
|
|
// make textbloben
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextSize(32);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
|
|
|
|
// Setup our random scaler context
|
2016-05-31 18:42:36 +00:00
|
|
|
sk_sp<SkTypeface> orig(sk_tool_utils::create_portable_typeface(
|
2017-07-27 14:45:29 +00:00
|
|
|
"sans-serif", SkFontStyle::Bold()));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == orig) {
|
2016-05-12 17:09:30 +00:00
|
|
|
orig = SkTypeface::MakeDefault();
|
2015-08-01 14:33:41 +00:00
|
|
|
}
|
2016-05-12 17:09:30 +00:00
|
|
|
paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, false));
|
2015-08-01 14:33:41 +00:00
|
|
|
|
|
|
|
SkRect bounds;
|
|
|
|
paint.measureText(text, strlen(text), &bounds);
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
|
|
|
|
|
|
|
|
// A8
|
2015-08-01 17:33:40 +00:00
|
|
|
const char* bigtext1 = "The quick brown fox";
|
|
|
|
const char* bigtext2 = "jumps over the lazy dog.";
|
|
|
|
paint.setTextSize(160);
|
2015-08-01 14:33:41 +00:00
|
|
|
paint.setSubpixelText(false);
|
|
|
|
paint.setLCDRenderText(false);
|
2015-08-01 17:33:40 +00:00
|
|
|
paint.measureText(bigtext1, strlen(bigtext1), &bounds);
|
|
|
|
SkScalar offset = bounds.height();
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, bigtext1, paint, 0, offset);
|
|
|
|
|
|
|
|
paint.measureText(bigtext2, strlen(bigtext2), &bounds);
|
|
|
|
offset += bounds.height();
|
|
|
|
sk_tool_utils::add_to_text_blob(&builder, bigtext2, paint, 0, offset);
|
|
|
|
|
|
|
|
// color emoji
|
2017-11-13 16:03:16 +00:00
|
|
|
if (sk_sp<SkTypeface> origEmoji = sk_tool_utils::emoji_typeface()) {
|
2015-08-01 17:33:40 +00:00
|
|
|
const char* emojiText = sk_tool_utils::emoji_sample_text();
|
|
|
|
paint.measureText(emojiText, strlen(emojiText), &bounds);
|
|
|
|
offset += bounds.height();
|
2017-11-13 16:03:16 +00:00
|
|
|
paint.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false));
|
2015-08-01 17:33:40 +00:00
|
|
|
sk_tool_utils::add_to_text_blob(&builder, emojiText, paint, 0, offset);
|
|
|
|
}
|
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.
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE));
|
|
|
|
|
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);
|
2016-03-24 01:59:25 +00:00
|
|
|
auto surface(canvas->makeSurface(info, &props));
|
2015-08-01 14:33:41 +00:00
|
|
|
if (surface) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
SkCanvas* c = surface->getCanvas();
|
|
|
|
|
2015-08-01 17:33:40 +00:00
|
|
|
int stride = SkScalarCeilToInt(fBlob->bounds().height());
|
|
|
|
int yOffset = stride / 8;
|
|
|
|
for (int i = 0; i < 1; i++) {
|
2015-08-01 14:33:41 +00:00
|
|
|
// fiddle the canvas to force regen of textblobs
|
|
|
|
canvas->rotate(i % 2 ? 0.0f : -0.05f);
|
|
|
|
canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint);
|
|
|
|
yOffset += stride;
|
|
|
|
|
|
|
|
// This will draw as black boxes
|
|
|
|
c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
|
|
|
|
surface->draw(canvas, 0, 0, nullptr);
|
|
|
|
|
|
|
|
// free gpu resources and verify
|
|
|
|
yOffset += stride;
|
|
|
|
canvas->getGrContext()->freeGpuResources();
|
|
|
|
canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint);
|
|
|
|
|
|
|
|
yOffset += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
const char* text = "This test requires a surface";
|
|
|
|
size_t len = strlen(text);
|
|
|
|
SkPaint paint;
|
|
|
|
canvas->drawText(text, len, 10, 100, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
#endif
|