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