epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 8 | #include "SkBlurMask.h" |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 9 | #include "SkBlurDrawLooper.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
| 12 | #include "SkPathMeasure.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 14 | #define REPEAT_COUNT 1 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 16 | static void textStrokePath(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | SkPaint paint; |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 18 | SkPath path; |
| 19 | SkRect rect; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 21 | canvas->save(); |
| 22 | canvas->scale(SkIntToScalar(250),SkIntToScalar(250)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 24 | rect.set(0.0f, 0.21f, |
| 25 | 0.78f, 0.99f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 27 | path.addArc(rect, SkIntToScalar(280), SkIntToScalar(350)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | |
| 29 | paint.setAntiAlias(true); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 30 | paint.setStyle(SkPaint::kStroke_Style); |
| 31 | paint.setColor(0xFFFF0000); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 32 | paint.setTextSize(0.085f); |
| 33 | paint.setStrokeWidth(.005f); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 34 | |
| 35 | canvas->drawPath(path, paint); |
| 36 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 37 | paint.setLooper(SkBlurDrawLooper::Make(SK_ColorBLACK, SkBlurMask::ConvertRadiusToSigma(0.002f), |
| 38 | 0.0f, 0.0f)); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 39 | |
| 40 | const char* text = "DRAWING STROKED TEXT WITH A BLUR ON A PATH"; |
| 41 | size_t len = strlen(text); |
| 42 | |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 43 | canvas->drawTextOnPathHV(text, len, path, 0, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 44 | -0.025f, paint); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 45 | canvas->restore(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
| 47 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 48 | static void textPathMatrix(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | SkPaint paint; |
| 50 | SkPath path; |
| 51 | SkMatrix matrix; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 52 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 53 | path.moveTo(SkIntToScalar(050), SkIntToScalar(200)); |
| 54 | path.quadTo(SkIntToScalar(250), SkIntToScalar(000), |
| 55 | SkIntToScalar(450), SkIntToScalar(200)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
| 57 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | paint.setStyle(SkPaint::kStroke_Style); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 60 | canvas->drawPath(path, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | paint.setStyle(SkPaint::kFill_Style); |
| 62 | paint.setTextSize(SkIntToScalar(48)); |
| 63 | paint.setTextAlign(SkPaint::kRight_Align); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 65 | const char* text = "Reflection"; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | size_t len = strlen(text); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 67 | |
| 68 | SkPathMeasure meas(path, false); |
| 69 | SkScalar pathLen = meas.getLength(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 71 | canvas->drawTextOnPath(text, len, path, nullptr, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | paint.setColor(SK_ColorRED); |
| 74 | matrix.setScale(-SK_Scalar1, SK_Scalar1); |
| 75 | matrix.postTranslate(pathLen, 0); |
| 76 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 77 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | paint.setColor(SK_ColorBLUE); |
| 79 | matrix.setScale(SK_Scalar1, -SK_Scalar1); |
| 80 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | paint.setColor(SK_ColorGREEN); |
| 83 | matrix.setScale(-SK_Scalar1, -SK_Scalar1); |
| 84 | matrix.postTranslate(pathLen, 0); |
| 85 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 86 | } |
| 87 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 88 | class TextOnPathView : public SampleView { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | public: |
| 90 | SkPath fPath; |
| 91 | SkScalar fHOffset; |
| 92 | |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 93 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 94 | void onOnceBeforeDraw() override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | SkRect r; |
| 96 | r.set(SkIntToScalar(100), SkIntToScalar(100), |
| 97 | SkIntToScalar(300), SkIntToScalar(300)); |
| 98 | fPath.addOval(r); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 99 | fPath.offset(SkIntToScalar(-50), SkIntToScalar(-50)); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | fHOffset = SkIntToScalar(50); |
| 102 | } |
| 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 105 | bool onQuery(SkEvent* evt) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | if (SampleCode::TitleQ(*evt)) { |
| 107 | SampleCode::TitleR(evt, "Text On Path"); |
| 108 | return true; |
| 109 | } |
| 110 | return this->INHERITED::onQuery(evt); |
| 111 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | paint.setAntiAlias(true); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 116 | paint.setTextSize(SkIntToScalar(48)); |
| 117 | |
| 118 | const char* text = "Hamburgefons"; |
| 119 | size_t len = strlen(text); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | |
| 121 | for (int j = 0; j < REPEAT_COUNT; j++) { |
| 122 | SkScalar x = fHOffset; |
| 123 | |
| 124 | paint.setColor(SK_ColorBLACK); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 125 | canvas->drawTextOnPathHV(text, len, fPath, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | x, paint.getTextSize()/2, paint); |
| 127 | |
| 128 | paint.setColor(SK_ColorRED); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 129 | canvas->drawTextOnPathHV(text, len, fPath, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | x + SkIntToScalar(50), 0, paint); |
| 131 | |
| 132 | paint.setColor(SK_ColorBLUE); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 133 | canvas->drawTextOnPathHV(text, len, fPath, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | x + SkIntToScalar(100), -paint.getTextSize()/2, paint); |
| 135 | } |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | paint.setColor(SK_ColorGREEN); |
| 138 | paint.setStyle(SkPaint::kStroke_Style); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 139 | canvas->drawPath(fPath, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 140 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 141 | canvas->translate(SkIntToScalar(275), 0); |
| 142 | textStrokePath(canvas); |
| 143 | |
| 144 | canvas->translate(SkIntToScalar(-275), SkIntToScalar(250)); |
| 145 | textPathMatrix(canvas); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | if (REPEAT_COUNT > 1) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 148 | this->inval(nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 150 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 151 | SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | fHints += 1; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 153 | this->inval(nullptr); |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 154 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 156 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 157 | bool onClick(Click* click) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | return this->INHERITED::onClick(click); |
| 159 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 160 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | private: |
| 162 | int fHints; |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 163 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | ////////////////////////////////////////////////////////////////////////////// |
| 167 | |
| 168 | static SkView* MyFactory() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | return new TextOnPathView; |
| 170 | } |
| 171 | |
| 172 | static SkViewRegister reg(MyFactory); |