2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkGradientShader.h"
14 static void makebm(SkBitmap* bm, int w, int h) {
15 bm->allocN32Pixels(w, h);
16 bm->eraseColor(SK_ColorTRANSPARENT);
19 SkScalar s = SkIntToScalar(SkMin32(w, h));
20 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
21 static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
22 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
23 static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
24 static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
29 paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
30 SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref();
31 canvas.drawPaint(paint);
32 paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
33 SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
34 canvas.drawPaint(paint);
37 ///////////////////////////////////////////////////////////////////////////////
39 struct LabeledMatrix {
44 static const char kText[] = "B";
45 static const int kTextLen = SK_ARRAY_COUNT(kText) - 1;
46 static const int kPointSize = 300;
48 class ShaderText3GM : public GM {
51 this->setBGColor(0xFFDDDDDD);
56 SkString onShortName() SK_OVERRIDE {
57 return SkString("shadertext3");
60 SkISize onISize() SK_OVERRIDE{ return SkISize::Make(800, 1000); }
62 void onOnceBeforeDraw() SK_OVERRIDE {
63 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
66 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
69 bmpPaint.setAntiAlias(true);
70 bmpPaint.setFilterQuality(kLow_SkFilterQuality);
71 bmpPaint.setAlpha(0x80);
72 canvas->drawBitmap(fBmp, 5.f, 5.f, &bmpPaint);
75 outlinePaint.setAntiAlias(true);
76 sk_tool_utils::set_portable_typeface(&outlinePaint);
77 outlinePaint.setTextSize(SkIntToScalar(kPointSize));
78 outlinePaint.setStyle(SkPaint::kStroke_Style);
79 outlinePaint.setStrokeWidth(0.f);
81 canvas->translate(15.f, 15.f);
83 // draw glyphs scaled up
84 canvas->scale(2.f, 2.f);
86 static const SkShader::TileMode kTileModes[] = {
87 SkShader::kRepeat_TileMode,
88 SkShader::kMirror_TileMode,
91 // position the baseline of the first run
92 canvas->translate(0.f, 0.75f * kPointSize);
96 for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) {
97 for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) {
99 localM.setTranslate(5.f, 5.f);
100 localM.postRotate(20);
101 localM.postScale(1.15f, .85f);
103 SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(fBmp,
109 fillPaint.setAntiAlias(true);
110 sk_tool_utils::set_portable_typeface(&fillPaint);
111 fillPaint.setTextSize(SkIntToScalar(kPointSize));
112 fillPaint.setFilterQuality(kLow_SkFilterQuality);
113 fillPaint.setShader(shader);
115 canvas->drawText(kText, kTextLen, 0, 0, fillPaint);
116 canvas->drawText(kText, kTextLen, 0, 0, outlinePaint);
117 SkScalar w = fillPaint.measureText(kText, kTextLen);
118 canvas->translate(w + 10.f, 0.f);
122 canvas->translate(0, 0.75f * kPointSize);
132 typedef GM INHERITED;
135 ///////////////////////////////////////////////////////////////////////////////
137 static GM* MyFactory(void*) { return new ShaderText3GM; }
138 static GMRegistry reg(MyFactory);