blob: 6d79e2075517a1f3d034a956a0ea49b6767ae7d8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000010#include "SkBlurDrawLooper.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPath.h"
13#include "SkPathMeasure.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000015#define REPEAT_COUNT 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000017static void textStrokePath(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 SkPaint paint;
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000019 SkPath path;
20 SkRect rect;
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000022 canvas->save();
23 canvas->scale(SkIntToScalar(250),SkIntToScalar(250));
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000025 rect.set(0.0f, 0.21f,
26 0.78f, 0.99f);
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000028 path.addArc(rect, SkIntToScalar(280), SkIntToScalar(350));
reed@android.com8a1c16f2008-12-17 15:59:43 +000029
30 paint.setAntiAlias(true);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000031 paint.setStyle(SkPaint::kStroke_Style);
32 paint.setColor(0xFFFF0000);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000033 paint.setTextSize(0.085f);
34 paint.setStrokeWidth(.005f);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000035
36 canvas->drawPath(path, paint);
37
commit-bot@chromium.org73cb1532014-04-15 15:48:36 +000038 paint.setLooper(SkBlurDrawLooper::Create(SK_ColorBLACK,
39 SkBlurMask::ConvertRadiusToSigma(0.002f),
40 0.0f,
41 0.0f))->unref();
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000042
43 const char* text = "DRAWING STROKED TEXT WITH A BLUR ON A PATH";
44 size_t len = strlen(text);
45
robertphillips@google.comc6ce7502012-05-08 13:15:37 +000046 canvas->drawTextOnPathHV(text, len, path, 0,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000047 -0.025f, paint);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000048 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +000049}
50
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000051static void textPathMatrix(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 SkPaint paint;
53 SkPath path;
54 SkMatrix matrix;
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000056 path.moveTo(SkIntToScalar(050), SkIntToScalar(200));
57 path.quadTo(SkIntToScalar(250), SkIntToScalar(000),
58 SkIntToScalar(450), SkIntToScalar(200));
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
60 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 paint.setStyle(SkPaint::kStroke_Style);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000063 canvas->drawPath(path, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 paint.setStyle(SkPaint::kFill_Style);
65 paint.setTextSize(SkIntToScalar(48));
66 paint.setTextAlign(SkPaint::kRight_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@android.comf2b98d62010-12-20 18:26:13 +000068 const char* text = "Reflection";
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 size_t len = strlen(text);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000070
71 SkPathMeasure meas(path, false);
72 SkScalar pathLen = meas.getLength();
reed@android.com8a1c16f2008-12-17 15:59:43 +000073
74 canvas->drawTextOnPath(text, len, path, NULL, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 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.comae933ce2012-08-23 18:19:56 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 paint.setColor(SK_ColorBLUE);
82 matrix.setScale(SK_Scalar1, -SK_Scalar1);
83 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 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.comf2183392011-04-22 14:10:48 +000091class TextOnPathView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092public:
93 SkPath fPath;
94 SkScalar fHOffset;
95
rmistry@google.comae933ce2012-08-23 18:19:56 +000096 TextOnPathView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 SkRect r;
98 r.set(SkIntToScalar(100), SkIntToScalar(100),
99 SkIntToScalar(300), SkIntToScalar(300));
100 fPath.addOval(r);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000101 fPath.offset(SkIntToScalar(-50), SkIntToScalar(-50));
reed@android.comf2b98d62010-12-20 18:26:13 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 fHOffset = SkIntToScalar(50);
104 }
105
106protected:
107 // overrides from SkEventSink
108 virtual bool onQuery(SkEvent* evt) {
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.comae933ce2012-08-23 18:19:56 +0000115
reed@google.comf2183392011-04-22 14:10:48 +0000116 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 paint.setAntiAlias(true);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000119 paint.setTextSize(SkIntToScalar(48));
120
121 const char* text = "Hamburgefons";
122 size_t len = strlen(text);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123
124 for (int j = 0; j < REPEAT_COUNT; j++) {
125 SkScalar x = fHOffset;
126
127 paint.setColor(SK_ColorBLACK);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000128 canvas->drawTextOnPathHV(text, len, fPath,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 x, paint.getTextSize()/2, paint);
130
131 paint.setColor(SK_ColorRED);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000132 canvas->drawTextOnPathHV(text, len, fPath,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133 x + SkIntToScalar(50), 0, paint);
134
135 paint.setColor(SK_ColorBLUE);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000136 canvas->drawTextOnPathHV(text, len, fPath,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 x + SkIntToScalar(100), -paint.getTextSize()/2, paint);
138 }
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 paint.setColor(SK_ColorGREEN);
141 paint.setStyle(SkPaint::kStroke_Style);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000142 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000144 canvas->translate(SkIntToScalar(275), 0);
145 textStrokePath(canvas);
146
147 canvas->translate(SkIntToScalar(-275), SkIntToScalar(250));
148 textPathMatrix(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 if (REPEAT_COUNT > 1)
151 this->inval(NULL);
152 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
reed@google.com4d5c26d2013-01-08 16:17:50 +0000154 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 fHints += 1;
156 this->inval(NULL);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000157 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 virtual bool onClick(Click* click) {
161 return this->INHERITED::onClick(click);
162 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164private:
165 int fHints;
reed@google.comf2183392011-04-22 14:10:48 +0000166 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167};
168
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169//////////////////////////////////////////////////////////////////////////////
170
171static SkView* MyFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 return new TextOnPathView;
173}
174
175static SkViewRegister reg(MyFactory);