2012-02-14 16:12:49 +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"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2016-03-09 17:50:50 +00:00
|
|
|
static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) {
|
2012-02-14 16:12:49 +00:00
|
|
|
const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
|
|
|
|
|
2016-03-09 17:50:50 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw),
|
|
|
|
SkShader::kClamp_TileMode);
|
2012-02-14 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool setFont(SkPaint* paint, const char name[]) {
|
2016-05-31 18:42:36 +00:00
|
|
|
paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
|
2016-05-12 17:09:30 +00:00
|
|
|
return SkToBool(paint->getTypeface());
|
2012-02-14 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Test a set of clipping problems discovered while writing blitAntiRect,
|
|
|
|
and test all the code paths through the clipping blitters.
|
|
|
|
Each region should show as a blue center surrounded by a 2px green
|
|
|
|
border, with no red.
|
|
|
|
*/
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-02-14 16:12:49 +00:00
|
|
|
#define HEIGHT 480
|
|
|
|
|
2014-08-22 15:30:20 +00:00
|
|
|
class GammaTextGM : public skiagm::GM {
|
2012-02-14 16:12:49 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2015-07-24 18:08:01 +00:00
|
|
|
SkString name("gammatext");
|
|
|
|
name.append(sk_tool_utils::major_platform_os_name());
|
|
|
|
return name;
|
2012-02-14 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-06-10 06:59:03 +00:00
|
|
|
return SkISize::Make(1024, HEIGHT);
|
2012-02-14 16:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void drawGrad(SkCanvas* canvas) {
|
2016-03-09 17:50:50 +00:00
|
|
|
const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-02-14 16:12:49 +00:00
|
|
|
canvas->clear(SK_ColorRED);
|
|
|
|
SkPaint paint;
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(make_heatGradient(pts));
|
2012-04-17 21:46:18 +00:00
|
|
|
SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
|
2012-02-14 16:12:49 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-02-14 16:12:49 +00:00
|
|
|
drawGrad(canvas);
|
|
|
|
|
|
|
|
const SkColor fg[] = {
|
|
|
|
0xFFFFFFFF,
|
|
|
|
0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
|
|
|
|
0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
|
|
|
|
0xFF000000,
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-02-14 16:12:49 +00:00
|
|
|
const char* text = "Hamburgefons";
|
|
|
|
size_t len = strlen(text);
|
|
|
|
|
|
|
|
SkPaint paint;
|
2015-07-24 18:08:01 +00:00
|
|
|
setFont(&paint, sk_tool_utils::platform_font_name("serif"));
|
2012-02-14 16:12:49 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(16));
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
|
2012-04-17 21:46:18 +00:00
|
|
|
SkScalar x = SkIntToScalar(10);
|
2012-02-14 16:12:49 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
|
|
|
|
paint.setColor(fg[i]);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-04-17 21:46:18 +00:00
|
|
|
SkScalar y = SkIntToScalar(40);
|
|
|
|
SkScalar stopy = SkIntToScalar(HEIGHT);
|
2012-02-14 16:12:49 +00:00
|
|
|
while (y < stopy) {
|
2014-11-13 20:46:24 +00:00
|
|
|
canvas->drawText(text, len, x, y, paint);
|
2012-02-14 16:12:49 +00:00
|
|
|
y += paint.getTextSize() * 2;
|
|
|
|
}
|
|
|
|
x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-08-22 15:30:20 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2012-02-14 16:12:49 +00:00
|
|
|
};
|
|
|
|
|
2015-02-19 15:45:14 +00:00
|
|
|
DEF_GM( return new GammaTextGM; )
|
2014-08-22 15:30:20 +00:00
|
|
|
|
2012-02-14 16:12:49 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-03-09 17:50:50 +00:00
|
|
|
static sk_sp<SkShader> make_gradient(SkColor c) {
|
2014-08-22 15:30:20 +00:00
|
|
|
const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
|
|
|
|
SkColor colors[2];
|
|
|
|
colors[0] = c;
|
|
|
|
colors[1] = SkColorSetA(c, 0);
|
2016-03-09 17:50:50 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2014-08-22 15:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_face(SkPaint* paint) {
|
2016-05-31 18:42:36 +00:00
|
|
|
paint->setTypeface(SkTypeface::MakeFromName("serif",
|
|
|
|
SkFontStyle::FromOldStyle(SkTypeface::kItalic)));
|
2014-08-22 15:30:20 +00:00
|
|
|
}
|
2012-02-14 16:12:49 +00:00
|
|
|
|
2016-03-09 17:50:50 +00:00
|
|
|
static void draw_pair(SkCanvas* canvas, SkPaint* paint, const sk_sp<SkShader>& shader) {
|
2014-08-22 15:30:20 +00:00
|
|
|
const char text[] = "Now is the time for all good";
|
|
|
|
const size_t len = strlen(text);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
paint->setShader(nullptr);
|
2014-08-22 15:30:20 +00:00
|
|
|
canvas->drawText(text, len, 10, 20, *paint);
|
2016-03-09 17:50:50 +00:00
|
|
|
paint->setShader(SkShader::MakeColorShader(paint->getColor()));
|
2014-08-22 15:30:20 +00:00
|
|
|
canvas->drawText(text, len, 10, 40, *paint);
|
|
|
|
paint->setShader(shader);
|
|
|
|
canvas->drawText(text, len, 10, 60, *paint);
|
2012-02-14 16:12:49 +00:00
|
|
|
}
|
2014-08-22 15:30:20 +00:00
|
|
|
|
|
|
|
class GammaShaderTextGM : public skiagm::GM {
|
2016-03-09 17:50:50 +00:00
|
|
|
sk_sp<SkShader> fShaders[3];
|
2014-08-22 15:30:20 +00:00
|
|
|
SkColor fColors[3];
|
|
|
|
|
|
|
|
public:
|
|
|
|
GammaShaderTextGM() {
|
|
|
|
const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
|
|
|
|
fColors[i] = colors[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-08-22 15:30:20 +00:00
|
|
|
return SkString("gammagradienttext");
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-08-22 15:30:20 +00:00
|
|
|
return SkISize::Make(300, 300);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2014-08-22 15:30:20 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
|
|
|
|
fShaders[i] = make_gradient(fColors[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-08-22 15:30:20 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setLCDRenderText(true);
|
|
|
|
paint.setTextSize(18);
|
|
|
|
set_face(&paint);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
|
|
|
|
paint.setColor(fColors[i]);
|
|
|
|
draw_pair(canvas, &paint, fShaders[i]);
|
|
|
|
canvas->translate(0, 80);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-08-22 15:30:20 +00:00
|
|
|
private:
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new GammaShaderTextGM; )
|