bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #include "gm.h" |
| 8 | #include "SkCanvas.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | #include "SkUnitMappers.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 14 | static void makebm(SkBitmap* bm, int w, int h) { |
| 15 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 16 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 17 | |
| 18 | SkCanvas canvas(*bm); |
| 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 }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 26 | |
| 27 | SkPaint paint; |
| 28 | |
| 29 | SkUnitMapper* um = NULL; |
| 30 | |
| 31 | um = new SkCosineMapper; |
| 32 | |
| 33 | SkAutoUnref au(um); |
| 34 | |
| 35 | paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos, |
| 36 | SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode, um))->unref(); |
| 37 | canvas.drawPaint(paint); |
| 38 | paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos, |
| 39 | SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref(); |
| 40 | canvas.drawPaint(paint); |
| 41 | } |
| 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | struct LabeledMatrix { |
| 46 | SkMatrix fMatrix; |
| 47 | const char* fLabel; |
| 48 | }; |
| 49 | |
| 50 | class ShaderText3GM : public GM { |
| 51 | public: |
| 52 | ShaderText3GM() { |
| 53 | this->setBGColor(0xFFDDDDDD); |
| 54 | } |
| 55 | |
| 56 | protected: |
| 57 | |
| 58 | SkString onShortName() { |
| 59 | return SkString("shadertext3"); |
| 60 | } |
| 61 | |
| 62 | SkISize onISize() { return make_isize(800, 1000); } |
| 63 | |
| 64 | virtual void onDraw(SkCanvas* canvas) { |
| 65 | static const char kText[] = "B"; |
| 66 | static const int kTextLen = SK_ARRAY_COUNT(kText) - 1; |
| 67 | static const int kPointSize = 300; |
| 68 | |
| 69 | static SkBitmap bmp; |
| 70 | if (bmp.isNull()) { |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 71 | makebm(&bmp, kPointSize / 4, kPointSize / 4); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 72 | } |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 74 | SkPaint bmpPaint; |
| 75 | bmpPaint.setAntiAlias(true); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 76 | bmpPaint.setFilterLevel(SkPaint::kLow_FilterLevel); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 77 | bmpPaint.setAlpha(0x80); |
| 78 | canvas->drawBitmap(bmp, 5.f, 5.f, &bmpPaint); |
| 79 | |
| 80 | SkPaint outlinePaint; |
| 81 | outlinePaint.setAntiAlias(true); |
| 82 | outlinePaint.setTextSize(SkIntToScalar(kPointSize)); |
| 83 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 84 | outlinePaint.setStrokeWidth(0.f); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 85 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 86 | canvas->translate(15.f, 15.f); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 87 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 88 | // draw glyphs scaled up |
| 89 | canvas->scale(2.f, 2.f); |
| 90 | |
| 91 | static const SkShader::TileMode kTileModes[] = { |
| 92 | SkShader::kRepeat_TileMode, |
| 93 | SkShader::kMirror_TileMode, |
| 94 | }; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 95 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 96 | // position the baseline of the first run |
| 97 | canvas->translate(0.f, 0.75f * kPointSize); |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 98 | |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 99 | canvas->save(); |
| 100 | int i = 0; |
skia.committer@gmail.com | f57c01b | 2012-10-13 02:01:56 +0000 | [diff] [blame] | 101 | for (size_t tm0 = 0; tm0 < SK_ARRAY_COUNT(kTileModes); ++tm0) { |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 102 | for (size_t tm1 = 0; tm1 < SK_ARRAY_COUNT(kTileModes); ++tm1) { |
| 103 | SkAutoTUnref<SkShader> shader(SkShader::CreateBitmapShader(bmp, |
| 104 | kTileModes[tm0], |
| 105 | kTileModes[tm1])); |
| 106 | SkMatrix localM; |
| 107 | localM.setTranslate(5.f, 5.f); |
| 108 | localM.postRotate(20); |
| 109 | localM.postScale(1.15f, .85f); |
| 110 | shader->setLocalMatrix(localM); |
| 111 | |
| 112 | SkPaint fillPaint; |
| 113 | fillPaint.setAntiAlias(true); |
| 114 | fillPaint.setTextSize(SkIntToScalar(kPointSize)); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 115 | fillPaint.setFilterLevel(SkPaint::kLow_FilterLevel); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 116 | fillPaint.setShader(shader); |
| 117 | |
| 118 | canvas->drawText(kText, kTextLen, 0, 0, fillPaint); |
| 119 | canvas->drawText(kText, kTextLen, 0, 0, outlinePaint); |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 120 | SkScalar w = fillPaint.measureText(kText, kTextLen); |
bsalomon@google.com | 4231609 | 2012-10-12 19:26:15 +0000 | [diff] [blame] | 121 | canvas->translate(w + 10.f, 0.f); |
| 122 | ++i; |
| 123 | if (!(i % 2)) { |
| 124 | canvas->restore(); |
| 125 | canvas->translate(0, 0.75f * kPointSize); |
| 126 | canvas->save(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | canvas->restore(); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | typedef GM INHERITED; |
| 135 | }; |
| 136 | |
| 137 | /////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | #ifndef SK_BUILD_FOR_ANDROID |
| 140 | static GM* MyFactory(void*) { return new ShaderText3GM; } |
| 141 | static GMRegistry reg(MyFactory); |
| 142 | #endif |
| 143 | } |