blob: 63b7ef26ccd42bb9798520990d0e68aa57944c81 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com8a1c16f2008-12-17 15:59:43 +00007#include "SampleCode.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
djsollen@google.comae8ae3d2012-03-15 15:01:34 +00009#include "SkBlurDrawLooper.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkPath.h"
12#include "SkPathMeasure.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000014#define REPEAT_COUNT 1
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000016static void textStrokePath(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 SkPaint paint;
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000018 SkPath path;
19 SkRect rect;
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000021 canvas->save();
22 canvas->scale(SkIntToScalar(250),SkIntToScalar(250));
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000024 rect.set(0.0f, 0.21f,
25 0.78f, 0.99f);
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000027 path.addArc(rect, SkIntToScalar(280), SkIntToScalar(350));
reed@android.com8a1c16f2008-12-17 15:59:43 +000028
29 paint.setAntiAlias(true);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000030 paint.setStyle(SkPaint::kStroke_Style);
31 paint.setColor(0xFFFF0000);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000032 paint.setTextSize(0.085f);
33 paint.setStrokeWidth(.005f);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000034
35 canvas->drawPath(path, paint);
36
reed7b380d02016-03-21 13:25:16 -070037 paint.setLooper(SkBlurDrawLooper::Make(SK_ColorBLACK, SkBlurMask::ConvertRadiusToSigma(0.002f),
38 0.0f, 0.0f));
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000039
40 const char* text = "DRAWING STROKED TEXT WITH A BLUR ON A PATH";
41 size_t len = strlen(text);
42
robertphillips@google.comc6ce7502012-05-08 13:15:37 +000043 canvas->drawTextOnPathHV(text, len, path, 0,
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000044 -0.025f, paint);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000045 canvas->restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +000046}
47
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000048static void textPathMatrix(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 SkPaint paint;
50 SkPath path;
51 SkMatrix matrix;
rmistry@google.comae933ce2012-08-23 18:19:56 +000052
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000053 path.moveTo(SkIntToScalar(050), SkIntToScalar(200));
54 path.quadTo(SkIntToScalar(250), SkIntToScalar(000),
55 SkIntToScalar(450), SkIntToScalar(200));
reed@android.com8a1c16f2008-12-17 15:59:43 +000056
57 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 paint.setStyle(SkPaint::kStroke_Style);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000060 canvas->drawPath(path, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 paint.setStyle(SkPaint::kFill_Style);
62 paint.setTextSize(SkIntToScalar(48));
63 paint.setTextAlign(SkPaint::kRight_Align);
rmistry@google.comae933ce2012-08-23 18:19:56 +000064
reed@android.comf2b98d62010-12-20 18:26:13 +000065 const char* text = "Reflection";
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 size_t len = strlen(text);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000067
68 SkPathMeasure meas(path, false);
69 SkScalar pathLen = meas.getLength();
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
halcanary96fcdcc2015-08-27 07:41:13 -070071 canvas->drawTextOnPath(text, len, path, nullptr, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 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.comae933ce2012-08-23 18:19:56 +000077
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 paint.setColor(SK_ColorBLUE);
79 matrix.setScale(SK_Scalar1, -SK_Scalar1);
80 canvas->drawTextOnPath(text, len, path, &matrix, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 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.comf2183392011-04-22 14:10:48 +000088class TextOnPathView : public SampleView {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089public:
90 SkPath fPath;
91 SkScalar fHOffset;
92
caryclark63c684a2015-02-25 09:04:04 -080093protected:
mtklein36352bf2015-03-25 18:17:31 -070094 void onOnceBeforeDraw() override {
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 SkRect r;
96 r.set(SkIntToScalar(100), SkIntToScalar(100),
97 SkIntToScalar(300), SkIntToScalar(300));
98 fPath.addOval(r);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +000099 fPath.offset(SkIntToScalar(-50), SkIntToScalar(-50));
reed@android.comf2b98d62010-12-20 18:26:13 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 fHOffset = SkIntToScalar(50);
102 }
103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -0700105 bool onQuery(SkEvent* evt) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 if (SampleCode::TitleQ(*evt)) {
107 SampleCode::TitleR(evt, "Text On Path");
108 return true;
109 }
110 return this->INHERITED::onQuery(evt);
111 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
mtklein36352bf2015-03-25 18:17:31 -0700113 void onDrawContent(SkCanvas* canvas) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 SkPaint paint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 paint.setAntiAlias(true);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000116 paint.setTextSize(SkIntToScalar(48));
117
118 const char* text = "Hamburgefons";
119 size_t len = strlen(text);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120
121 for (int j = 0; j < REPEAT_COUNT; j++) {
122 SkScalar x = fHOffset;
123
124 paint.setColor(SK_ColorBLACK);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000125 canvas->drawTextOnPathHV(text, len, fPath,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 x, paint.getTextSize()/2, paint);
127
128 paint.setColor(SK_ColorRED);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000129 canvas->drawTextOnPathHV(text, len, fPath,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 x + SkIntToScalar(50), 0, paint);
131
132 paint.setColor(SK_ColorBLUE);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000133 canvas->drawTextOnPathHV(text, len, fPath,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 x + SkIntToScalar(100), -paint.getTextSize()/2, paint);
135 }
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000136
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 paint.setColor(SK_ColorGREEN);
138 paint.setStyle(SkPaint::kStroke_Style);
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000139 canvas->drawPath(fPath, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000140
djsollen@google.comae8ae3d2012-03-15 15:01:34 +0000141 canvas->translate(SkIntToScalar(275), 0);
142 textStrokePath(canvas);
143
144 canvas->translate(SkIntToScalar(-275), SkIntToScalar(250));
145 textPathMatrix(canvas);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 if (REPEAT_COUNT > 1)
halcanary96fcdcc2015-08-27 07:41:13 -0700148 this->inval(nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000150
mtklein36352bf2015-03-25 18:17:31 -0700151 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 fHints += 1;
halcanary96fcdcc2015-08-27 07:41:13 -0700153 this->inval(nullptr);
reed@google.com4d5c26d2013-01-08 16:17:50 +0000154 return this->INHERITED::onFindClickHandler(x, y, modi);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156
mtklein36352bf2015-03-25 18:17:31 -0700157 bool onClick(Click* click) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 return this->INHERITED::onClick(click);
159 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161private:
162 int fHints;
reed@google.comf2183392011-04-22 14:10:48 +0000163 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164};
165
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166//////////////////////////////////////////////////////////////////////////////
167
168static SkView* MyFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 return new TextOnPathView;
170}
171
172static SkViewRegister reg(MyFactory);