2011-11-28 16:31:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 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"
|
2011-11-28 16:31:28 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
2012-07-11 14:41:19 +00:00
|
|
|
#include "SkTypeface.h"
|
2011-11-28 16:31:28 +00:00
|
|
|
|
2011-11-30 14:29:55 +00:00
|
|
|
// test shader w/ transparency
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_grad(SkScalar width) {
|
2011-11-30 14:29:55 +00:00
|
|
|
SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE };
|
2011-11-28 16:31:28 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 }, { width, 0 } };
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kMirror_TileMode);
|
2011-11-28 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 14:29:55 +00:00
|
|
|
// test opaque shader
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_grad2(SkScalar width) {
|
2011-11-28 16:31:28 +00:00
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE };
|
|
|
|
SkPoint pts[] = { { 0, 0 }, { width, 0 } };
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
|
|
|
|
SkShader::kMirror_TileMode);
|
2011-11-28 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> make_chrome_solid() {
|
2012-07-11 14:41:19 +00:00
|
|
|
SkColor colors[] = { SK_ColorGREEN, SK_ColorGREEN };
|
|
|
|
SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2012-07-11 14:41:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 16:31:28 +00:00
|
|
|
namespace skiagm {
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-07-11 14:41:19 +00:00
|
|
|
// Replicate chrome layout test - clipped pathed gradient-shaded text
|
|
|
|
class ChromeGradTextGM1 : public GM {
|
|
|
|
public:
|
|
|
|
ChromeGradTextGM1() { }
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual SkString onShortName() { return SkString("chrome_gradtext1"); }
|
2014-06-10 06:59:03 +00:00
|
|
|
virtual SkISize onISize() { return SkISize::Make(500, 480); }
|
2012-07-11 14:41:19 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
|
|
|
|
canvas->clipRect(r);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
// Minimal repro doesn't require AA, LCD, or a nondefault typeface
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(make_chrome_solid());
|
2012-07-11 14:41:19 +00:00
|
|
|
|
2019-01-07 16:01:57 +00:00
|
|
|
SkFont font(sk_tool_utils::create_portable_typeface(), 500);
|
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
|
|
|
|
|
|
|
canvas->drawString("I", 0, 100, font, paint);
|
2012-07-11 14:41:19 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-07-15 18:57:00 +00:00
|
|
|
// Replicate chrome layout test - switching between solid & gradient text
|
2012-07-11 14:41:19 +00:00
|
|
|
class ChromeGradTextGM2 : public GM {
|
|
|
|
public:
|
|
|
|
ChromeGradTextGM2() { }
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual SkString onShortName() { return SkString("chrome_gradtext2"); }
|
2014-06-10 06:59:03 +00:00
|
|
|
virtual SkISize onISize() { return SkISize::Make(500, 480); }
|
2012-07-11 14:41:19 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
2019-01-07 16:01:57 +00:00
|
|
|
SkFont font(sk_tool_utils::create_portable_typeface());
|
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
2012-07-11 14:41:19 +00:00
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
2019-01-07 16:01:57 +00:00
|
|
|
canvas->drawString("Normal Fill Text", 0, 50, font, paint);
|
2012-07-11 14:41:19 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2019-01-07 16:01:57 +00:00
|
|
|
canvas->drawString("Normal Stroke Text", 0, 100, font, paint);
|
2012-07-11 14:41:19 +00:00
|
|
|
|
|
|
|
// Minimal repro doesn't require AA, LCD, or a nondefault typeface
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(make_chrome_solid());
|
2012-07-11 14:41:19 +00:00
|
|
|
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
2019-01-07 16:01:57 +00:00
|
|
|
canvas->drawString("Gradient Fill Text", 0, 150, font, paint);
|
2012-07-11 14:41:19 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2019-01-07 16:01:57 +00:00
|
|
|
canvas->drawString("Gradient Stroke Text", 0, 200, font, paint);
|
2012-07-11 14:41:19 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-28 16:31:28 +00:00
|
|
|
class GradTextGM : public GM {
|
|
|
|
public:
|
|
|
|
GradTextGM () {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2011-11-28 16:31:28 +00:00
|
|
|
return SkString("gradtext");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(500, 480); }
|
2011-11-28 16:31:28 +00:00
|
|
|
|
|
|
|
static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
const char* text = "When in the course of human events";
|
|
|
|
size_t len = strlen(text);
|
|
|
|
canvas->drawText(text, len, 0, 0, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_text3(SkCanvas* canvas, const SkPaint& paint) {
|
|
|
|
SkPaint p(paint);
|
|
|
|
|
|
|
|
p.setAntiAlias(false);
|
|
|
|
draw_text(canvas, p);
|
|
|
|
p.setAntiAlias(true);
|
2011-11-30 14:29:55 +00:00
|
|
|
canvas->translate(0, paint.getTextSize() * 4/3);
|
2011-11-28 16:31:28 +00:00
|
|
|
draw_text(canvas, p);
|
|
|
|
p.setLCDRenderText(true);
|
2011-11-30 14:29:55 +00:00
|
|
|
canvas->translate(0, paint.getTextSize() * 4/3);
|
2011-11-28 16:31:28 +00:00
|
|
|
draw_text(canvas, p);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2011-11-30 14:29:55 +00:00
|
|
|
SkPaint paint;
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&paint);
|
2011-11-30 14:29:55 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(26));
|
2011-11-28 16:31:28 +00:00
|
|
|
|
2011-11-30 14:29:55 +00:00
|
|
|
const SkISize& size = this->getISize();
|
|
|
|
SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
|
|
|
|
SkIntToScalar(size.height()) / 2);
|
|
|
|
canvas->drawRect(r, paint);
|
2011-11-28 16:31:28 +00:00
|
|
|
|
2011-11-30 14:29:55 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), paint.getTextSize());
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(make_grad(SkIntToScalar(80)));
|
2011-11-30 14:29:55 +00:00
|
|
|
draw_text3(canvas, paint);
|
|
|
|
|
|
|
|
canvas->translate(0, paint.getTextSize() * 2);
|
2011-11-28 16:31:28 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(make_grad2(SkIntToScalar(80)));
|
2011-11-30 14:29:55 +00:00
|
|
|
draw_text3(canvas, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-11-30 14:29:55 +00:00
|
|
|
canvas->translate(0, paint.getTextSize() * 2);
|
|
|
|
}
|
2011-11-28 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new GradTextGM; }
|
2012-07-11 14:41:19 +00:00
|
|
|
static GM* CMyFactory(void*) { return new ChromeGradTextGM1; }
|
|
|
|
static GM* CMyFactory2(void*) { return new ChromeGradTextGM2; }
|
2011-11-28 16:31:28 +00:00
|
|
|
|
2012-07-11 14:41:19 +00:00
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
static GMRegistry Creg(CMyFactory);
|
|
|
|
static GMRegistry Creg2(CMyFactory2);
|
2011-11-28 16:31:28 +00:00
|
|
|
}
|