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