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 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 24 | rect.set(0.0f, 0.21f, 0.78f, 0.99f); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 26 | path.addArc(rect, SkIntToScalar(280), SkIntToScalar(350)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | |
| 28 | paint.setAntiAlias(true); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 29 | paint.setStyle(SkPaint::kStroke_Style); |
| 30 | paint.setColor(0xFFFF0000); |
djsollen@google.com | 166e653 | 2012-03-20 14:24:38 +0000 | [diff] [blame^] | 31 | paint.setTextSize(SkFloatToScalar(0.085f)); |
| 32 | paint.setStrokeWidth(SkFloatToScalar(.005f)); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 33 | |
| 34 | canvas->drawPath(path, paint); |
| 35 | |
| 36 | paint.setLooper(new SkBlurDrawLooper(SkFloatToScalar(0.002f), |
| 37 | SkFloatToScalar(0.0f), |
| 38 | SkFloatToScalar(0.0f), |
| 39 | (SkColor)0xFF000000))->unref(); |
| 40 | |
| 41 | const char* text = "DRAWING STROKED TEXT WITH A BLUR ON A PATH"; |
| 42 | size_t len = strlen(text); |
| 43 | |
| 44 | canvas->drawTextOnPathHV(text, len, path, 0, -0.025f, paint); |
| 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; |
| 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); |
| 58 | |
| 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); |
| 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 | |
| 71 | canvas->drawTextOnPath(text, len, path, NULL, paint); |
| 72 | |
| 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); |
| 77 | |
| 78 | paint.setColor(SK_ColorBLUE); |
| 79 | matrix.setScale(SK_Scalar1, -SK_Scalar1); |
| 80 | canvas->drawTextOnPath(text, len, path, &matrix, paint); |
| 81 | |
| 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 | |
| 93 | TextOnPathView() { |
| 94 | SkRect r; |
| 95 | r.set(SkIntToScalar(100), SkIntToScalar(100), |
| 96 | SkIntToScalar(300), SkIntToScalar(300)); |
| 97 | fPath.addOval(r); |
djsollen@google.com | ae8ae3d | 2012-03-15 15:01:34 +0000 | [diff] [blame] | 98 | fPath.offset(SkIntToScalar(-50), SkIntToScalar(-50)); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | fHOffset = SkIntToScalar(50); |
| 101 | } |
| 102 | |
| 103 | protected: |
| 104 | // overrides from SkEventSink |
| 105 | virtual bool onQuery(SkEvent* evt) { |
| 106 | if (SampleCode::TitleQ(*evt)) { |
| 107 | SampleCode::TitleR(evt, "Text On Path"); |
| 108 | return true; |
| 109 | } |
| 110 | return this->INHERITED::onQuery(evt); |
| 111 | } |
| 112 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 113 | virtual void onDrawContent(SkCanvas* canvas) { |
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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | |
| 147 | if (REPEAT_COUNT > 1) |
| 148 | this->inval(NULL); |
| 149 | } |
| 150 | |
| 151 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 152 | fHints += 1; |
| 153 | this->inval(NULL); |
| 154 | return this->INHERITED::onFindClickHandler(x, y); |
| 155 | } |
| 156 | |
| 157 | virtual bool onClick(Click* click) { |
| 158 | return this->INHERITED::onClick(click); |
| 159 | } |
| 160 | |
| 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); |