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