2012-10-12 19:26:15 +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 "SkGradientShader.h"
|
|
|
|
#include "SkUnitMappers.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
static void makebm(SkBitmap* bm, int w, int h) {
|
|
|
|
bm->allocN32Pixels(w, h);
|
2012-12-06 21:47:40 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
2012-10-12 19:26:15 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
SkScalar s = SkIntToScalar(SkMin32(w, h));
|
|
|
|
static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
|
|
|
|
static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
|
|
|
|
static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
|
|
|
|
static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
|
|
|
|
static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2012-10-12 19:26:15 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
SkUnitMapper* um = NULL;
|
|
|
|
|
|
|
|
um = new SkCosineMapper;
|
|
|
|
|
|
|
|
SkAutoUnref au(um);
|
|
|
|
|
|
|
|
paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
|
|
|
|
SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode, um))->unref();
|
|
|
|
canvas.drawPaint(paint);
|
|
|
|
paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
|
|
|
|
SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
|
|
|
|
canvas.drawPaint(paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct LabeledMatrix {
|
|
|
|
SkMatrix fMatrix;
|
|
|
|
const char* fLabel;
|
|
|
|
};
|
|
|
|
|
2014-03-26 21:21:40 +00:00
|
|
|
static const char kText[] = "B";
|
|
|
|
static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
|
|
|
|
static const int kPointSize = 300;
|
|
|
|
|
2012-10-12 19:26:15 +00:00
|
|
|
class ShaderText3GM : public GM {
|
|
|
|
public:
|
|
|
|
ShaderText3GM() {
|
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2014-03-26 21:21:40 +00:00
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
2012-10-12 19:26:15 +00:00
|
|
|
return SkString("shadertext3");
|
|
|
|
}
|
|
|
|
|
2014-03-26 21:21:40 +00:00
|
|
|
virtual SkISize onISize() SK_OVERRIDE{ return make_isize(800, 1000); }
|
2012-10-12 19:26:15 +00:00
|
|
|
|
2014-03-26 21:21:40 +00:00
|
|
|
virtual void onOnceBeforeDraw() SK_OVERRIDE {
|
|
|
|
makebm(&fBmp, kPointSize / 4, kPointSize / 4);
|
|
|
|
}
|
2012-10-12 19:26:15 +00:00
|
|
|
|
2014-03-26 21:21:40 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2012-10-12 19:26:15 +00:00
|
|
|
SkPaint bmpPaint;
|
|
|
|
bmpPaint.setAntiAlias(true);
|
2013-10-31 17:28:30 +00:00
|
|
|
bmpPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
|
2012-10-12 19:26:15 +00:00
|
|
|
bmpPaint.setAlpha(0x80);
|
2014-03-26 21:21:40 +00:00
|
|
|
canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
|
2012-10-12 19:26:15 +00:00
|
|
|
|
|
|
|
SkPaint outlinePaint;
|
|
|
|
outlinePaint.setAntiAlias(true);
|
|
|
|
outlinePaint.setTextSize(SkIntToScalar(kPointSize));
|
|
|
|
outlinePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
outlinePaint.setStrokeWidth(0.f);
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2012-10-12 19:26:15 +00:00
|
|
|
canvas->translate(15.f, 15.f);
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2012-10-12 19:26:15 +00:00
|
|
|
// draw glyphs scaled up
|
|
|
|
canvas->scale(2.f, 2.f);
|
|
|
|
|
|
|
|
static const SkShader::TileMode kTileModes[] = {
|
|
|
|
SkShader::kRepeat_TileMode,
|
|
|
|
SkShader::kMirror_TileMode,
|
|
|
|
};
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2012-10-12 19:26:15 +00:00
|
|
|
// position the baseline of the first run
|
|
|
|
canvas->translate(0.f, 0.75f * kPointSize);
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2012-10-12 19:26:15 +00:00
|
|
|
canvas->save();
|
|
|
|
int i = 0;
|
2012-10-13 02:01:56 +00:00
|
|
|
for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
|
2012-10-12 19:26:15 +00:00
|
|
|
for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
|
2014-03-26 21:21:40 +00:00
|
|
|
SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(fBmp,
|
2012-10-12 19:26:15 +00:00
|
|
|
kTileModes[tm0],
|
|
|
|
kTileModes[tm1]));
|
|
|
|
SkMatrix localM;
|
|
|
|
localM.setTranslate(5.f, 5.f);
|
|
|
|
localM.postRotate(20);
|
|
|
|
localM.postScale(1.15f, .85f);
|
|
|
|
shader->setLocalMatrix(localM);
|
|
|
|
|
|
|
|
SkPaint fillPaint;
|
|
|
|
fillPaint.setAntiAlias(true);
|
|
|
|
fillPaint.setTextSize(SkIntToScalar(kPointSize));
|
2013-10-31 17:28:30 +00:00
|
|
|
fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
|
2012-10-12 19:26:15 +00:00
|
|
|
fillPaint.setShader(shader);
|
|
|
|
|
|
|
|
canvas->drawText(kText, kTextLen, 0, 0, fillPaint);
|
|
|
|
canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
|
2012-12-12 15:58:25 +00:00
|
|
|
SkScalar w = fillPaint.measureText(kText, kTextLen);
|
2012-10-12 19:26:15 +00:00
|
|
|
canvas->translate(w + 10.f, 0.f);
|
|
|
|
++i;
|
|
|
|
if (!(i % 2)) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, 0.75f * kPointSize);
|
|
|
|
canvas->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-03-26 21:21:40 +00:00
|
|
|
SkBitmap fBmp;
|
2012-10-12 19:26:15 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new ShaderText3GM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
}
|