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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#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/effects/SkGradientShader.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2011-11-28 16:31:28 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
namespace {
|
|
|
|
|
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),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror);
|
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),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror);
|
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 } };
|
2019-04-03 14:27:45 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
|
2012-07-11 14:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replicate chrome layout test - clipped pathed gradient-shaded text
|
2019-07-18 15:51:19 +00:00
|
|
|
class ChromeGradTextGM1 : public skiagm::GM {
|
|
|
|
SkString onShortName() override { return SkString("chrome_gradtext1"); }
|
|
|
|
|
|
|
|
SkISize onISize() override { return {500, 480}; }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-07-11 14:41:19 +00:00
|
|
|
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-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), 500);
|
2019-01-07 16:01:57 +00:00
|
|
|
font.setEdging(SkFont::Edging::kAlias);
|
|
|
|
|
|
|
|
canvas->drawString("I", 0, 100, font, paint);
|
2012-07-11 14:41:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-15 18:57:00 +00:00
|
|
|
// Replicate chrome layout test - switching between solid & gradient text
|
2019-07-18 15:51:19 +00:00
|
|
|
class ChromeGradTextGM2 : public skiagm::GM {
|
|
|
|
SkString onShortName() override { return SkString("chrome_gradtext2"); }
|
|
|
|
|
|
|
|
SkISize onISize() override { return {500, 480}; }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-07-11 14:41:19 +00:00
|
|
|
SkPaint paint;
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2019-01-07 16:01:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2019-07-18 15:51:19 +00:00
|
|
|
} // namespace
|
2011-11-28 16:31:28 +00:00
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new ChromeGradTextGM1; )
|
|
|
|
DEF_GM( return new ChromeGradTextGM2; )
|
2019-01-07 21:01:22 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(gradtext, canvas, 500, 480) {
|
|
|
|
static constexpr float kTextSize = 26.0f;
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), kTextSize);
|
2019-01-07 21:01:22 +00:00
|
|
|
|
|
|
|
canvas->drawRect({0, 0, 500, 240}, SkPaint());
|
|
|
|
canvas->translate(20.0f, kTextSize);
|
|
|
|
|
|
|
|
SkPaint paints[2];
|
|
|
|
paints[0].setShader(make_grad(80.0f));
|
|
|
|
paints[1].setShader(make_grad2(80.0f));
|
|
|
|
|
|
|
|
static const SkFont::Edging edgings[3] = {
|
|
|
|
SkFont::Edging::kAlias,
|
|
|
|
SkFont::Edging::kAntiAlias,
|
|
|
|
SkFont::Edging::kSubpixelAntiAlias,
|
|
|
|
};
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
for (const SkPaint& paint : paints) {
|
|
|
|
for (SkFont::Edging edging : edgings) {
|
|
|
|
font.setEdging(edging);
|
|
|
|
canvas->drawString("When in the course of human events", 0, 0, font, paint);
|
|
|
|
canvas->translate(0, kTextSize * 4/3);
|
|
|
|
}
|
|
|
|
canvas->translate(0, kTextSize * 2/3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|