bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +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" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 10 | #include "SkPath.h" |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 11 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 12 | static void makebm(SkBitmap* bm, int w, int h) { |
| 13 | bm->allocN32Pixels(w, h); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 14 | bm->eraseColor(SK_ColorTRANSPARENT); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 15 | |
| 16 | SkCanvas canvas(*bm); |
| 17 | SkScalar s = SkIntToScalar(SkMin32(w, h)); |
| 18 | static const SkPoint kPts0[] = { { 0, 0 }, { s, s } }; |
| 19 | static const SkPoint kPts1[] = { { s, 0 }, { 0, s } }; |
| 20 | static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 21 | static const SkColor kColors0[] = {0x40FF00FF, 0xF0FFFF00, 0x4000FFFF }; |
| 22 | static const SkColor kColors1[] = {0xF0FF00FF, 0x80FFFF00, 0xF000FFFF }; |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 24 | |
| 25 | SkPaint paint; |
| 26 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 27 | paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, |
| 28 | SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode)); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 29 | canvas.drawPaint(paint); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 30 | paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, |
| 31 | SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode)); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 32 | canvas.drawPaint(paint); |
| 33 | } |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | struct LabeledMatrix { |
| 38 | SkMatrix fMatrix; |
| 39 | const char* fLabel; |
| 40 | }; |
| 41 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 42 | DEF_SIMPLE_GM_BG(shadertext2, canvas, 1800, 900, |
| 43 | sk_tool_utils::color_to_565(0xFFDDDDDD)) { |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 44 | static const char kText[] = "SKIA"; |
| 45 | static const int kTextLen = SK_ARRAY_COUNT(kText) - 1; |
| 46 | static const int kPointSize = 55; |
| 47 | |
| 48 | SkTDArray<LabeledMatrix> matrices; |
| 49 | matrices.append()->fMatrix.reset(); |
| 50 | matrices.top().fLabel = "Identity"; |
| 51 | matrices.append()->fMatrix.setScale(1.2f, 0.8f); |
| 52 | matrices.top().fLabel = "Scale"; |
| 53 | matrices.append()->fMatrix.setRotate(10.f); |
| 54 | matrices.top().fLabel = "Rotate"; |
| 55 | matrices.append()->fMatrix.reset(); |
| 56 | matrices.top().fMatrix.setPerspX(-0.0015f); |
| 57 | matrices.top().fMatrix.setPerspY(+0.0015f); |
| 58 | matrices.top().fLabel = "Persp"; |
| 59 | |
| 60 | SkTDArray<LabeledMatrix> localMatrices; |
| 61 | localMatrices.append()->fMatrix.reset(); |
| 62 | localMatrices.top().fLabel = "Identity"; |
| 63 | localMatrices.append()->fMatrix.setScale(2.5f, 0.2f); |
| 64 | localMatrices.top().fLabel = "Scale"; |
| 65 | localMatrices.append()->fMatrix.setRotate(45.f); |
| 66 | localMatrices.top().fLabel = "Rotate"; |
| 67 | localMatrices.append()->fMatrix.reset(); |
| 68 | localMatrices.top().fMatrix.setPerspX(-0.007f); |
| 69 | localMatrices.top().fMatrix.setPerspY(+0.008f); |
| 70 | localMatrices.top().fLabel = "Persp"; |
| 71 | |
| 72 | static SkBitmap bmp; |
| 73 | if (bmp.isNull()) { |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 74 | makebm(&bmp, kPointSize / 2, kPointSize / 2); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 75 | } |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 76 | |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 77 | SkPaint fillPaint; |
| 78 | fillPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 79 | sk_tool_utils::set_portable_typeface(&fillPaint); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 80 | fillPaint.setTextSize(SkIntToScalar(kPointSize)); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 81 | fillPaint.setFilterQuality(kLow_SkFilterQuality); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 82 | |
| 83 | SkPaint outlinePaint; |
| 84 | outlinePaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 85 | sk_tool_utils::set_portable_typeface(&outlinePaint); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 86 | outlinePaint.setTextSize(SkIntToScalar(kPointSize)); |
| 87 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 88 | outlinePaint.setStrokeWidth(0.f); |
| 89 | |
| 90 | SkScalar w = fillPaint.measureText(kText, kTextLen); |
| 91 | static SkScalar kPadY = 0.5f * kPointSize; |
| 92 | static SkScalar kPadX = 1.5f * kPointSize; |
| 93 | |
| 94 | SkPaint strokePaint(fillPaint); |
| 95 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 96 | strokePaint.setStrokeWidth(kPointSize * 0.1f); |
| 97 | |
| 98 | SkPaint labelPaint; |
| 99 | labelPaint.setColor(0xff000000); |
| 100 | labelPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 101 | sk_tool_utils::set_portable_typeface(&labelPaint); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 102 | labelPaint.setTextSize(12.f); |
| 103 | |
| 104 | canvas->translate(15.f, 15.f); |
| 105 | canvas->drawBitmap(bmp, 0, 0); |
| 106 | canvas->translate(0, bmp.height() + labelPaint.getTextSize() + 15.f); |
| 107 | |
| 108 | static const char kLabelLabel[] = "localM / canvasM"; |
| 109 | canvas->drawText(kLabelLabel, strlen(kLabelLabel), 0, 0, labelPaint); |
| 110 | canvas->translate(0, 15.f); |
| 111 | |
| 112 | canvas->save(); |
| 113 | SkScalar maxLabelW = 0; |
| 114 | canvas->translate(0, kPadY / 2 + kPointSize); |
| 115 | for (int lm = 0; lm < localMatrices.count(); ++lm) { |
| 116 | canvas->drawText(matrices[lm].fLabel, strlen(matrices[lm].fLabel), |
| 117 | 0, labelPaint.getTextSize() - 1, labelPaint); |
| 118 | SkScalar labelW = labelPaint.measureText(matrices[lm].fLabel, |
| 119 | strlen(matrices[lm].fLabel)); |
| 120 | maxLabelW = SkMaxScalar(maxLabelW, labelW); |
| 121 | canvas->translate(0.f, 2 * kPointSize + 2.5f * kPadY); |
| 122 | } |
| 123 | canvas->restore(); |
| 124 | |
| 125 | canvas->translate(maxLabelW + kPadX / 2.f, 0.f); |
| 126 | |
| 127 | for (int s = 0; s < 2; ++s) { |
| 128 | SkPaint& paint = s ? strokePaint : fillPaint; |
| 129 | |
commit-bot@chromium.org | 35d4872 | 2014-02-13 18:36:36 +0000 | [diff] [blame] | 130 | SkScalar columnH = 0; |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 131 | for (int m = 0; m < matrices.count(); ++m) { |
| 132 | columnH = 0; |
| 133 | canvas->save(); |
| 134 | canvas->drawText(matrices[m].fLabel, strlen(matrices[m].fLabel), |
| 135 | 0, labelPaint.getTextSize() - 1, labelPaint); |
| 136 | canvas->translate(0, kPadY / 2 + kPointSize); |
| 137 | columnH += kPadY / 2 + kPointSize; |
| 138 | for (int lm = 0; lm < localMatrices.count(); ++lm) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 139 | paint.setShader(SkShader::MakeBitmapShader(bmp, SkShader::kMirror_TileMode, |
| 140 | SkShader::kRepeat_TileMode, |
| 141 | &localMatrices[lm].fMatrix)); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 142 | |
| 143 | canvas->save(); |
| 144 | canvas->concat(matrices[m].fMatrix); |
| 145 | canvas->drawText(kText, kTextLen, 0, 0, paint); |
| 146 | canvas->drawText(kText, kTextLen, 0, 0, outlinePaint); |
| 147 | canvas->restore(); |
| 148 | |
| 149 | SkPath path; |
| 150 | path.arcTo(SkRect::MakeXYWH(-0.1f * w, 0.f, |
| 151 | 1.2f * w, 2.f * kPointSize), |
| 152 | 225.f, 359.f, |
| 153 | false); |
| 154 | path.close(); |
| 155 | |
| 156 | canvas->translate(0.f, kPointSize + kPadY); |
| 157 | columnH += kPointSize + kPadY; |
| 158 | |
| 159 | canvas->save(); |
| 160 | canvas->concat(matrices[m].fMatrix); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | canvas->drawTextOnPath(kText, kTextLen, path, nullptr, paint); |
| 162 | canvas->drawTextOnPath(kText, kTextLen, path, nullptr, outlinePaint); |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 163 | canvas->restore(); |
| 164 | SkPaint stroke; |
| 165 | stroke.setStyle(SkPaint::kStroke_Style); |
| 166 | canvas->translate(0.f, kPointSize + kPadY); |
| 167 | columnH += kPointSize + kPadY; |
| 168 | } |
| 169 | canvas->restore(); |
| 170 | canvas->translate(w + kPadX, 0.f); |
| 171 | } |
| 172 | if (0 == s) { |
| 173 | canvas->drawLine(0.f, -kPadY, 0.f, columnH + kPadY, outlinePaint); |
| 174 | canvas->translate(kPadX / 2, 0.f); |
| 175 | static const char kFillLabel[] = "Filled"; |
| 176 | static const char kStrokeLabel[] = "Stroked"; |
| 177 | SkScalar y = columnH + kPadY / 2; |
| 178 | SkScalar fillX = -outlinePaint.measureText(kFillLabel, strlen(kFillLabel)) - kPadX; |
| 179 | SkScalar strokeX = kPadX; |
| 180 | canvas->drawText(kFillLabel, strlen(kFillLabel), fillX, y, labelPaint); |
| 181 | canvas->drawText(kStrokeLabel, strlen(kStrokeLabel), strokeX, y, labelPaint); |
| 182 | } |
| 183 | } |
bsalomon@google.com | 41fe45b | 2012-10-10 13:35:23 +0000 | [diff] [blame] | 184 | } |