2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
2012-03-15 15:01:34 +00:00
|
|
|
#include "SkBlurDrawLooper.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkPathMeasure.h"
|
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
#define REPEAT_COUNT 1
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
static void textStrokePath(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
2012-03-15 15:01:34 +00:00
|
|
|
SkPath path;
|
|
|
|
SkRect rect;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->scale(SkIntToScalar(250),SkIntToScalar(250));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-05-08 13:15:37 +00:00
|
|
|
rect.set(SkFloatToScalar(0.0f), SkFloatToScalar(0.21f),
|
|
|
|
SkFloatToScalar(0.78f), SkFloatToScalar(0.99f));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
path.addArc(rect, SkIntToScalar(280), SkIntToScalar(350));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
2012-03-15 15:01:34 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setColor(0xFFFF0000);
|
2012-03-20 14:24:38 +00:00
|
|
|
paint.setTextSize(SkFloatToScalar(0.085f));
|
|
|
|
paint.setStrokeWidth(SkFloatToScalar(.005f));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->drawPath(path, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
paint.setLooper(new SkBlurDrawLooper(SkFloatToScalar(0.002f),
|
|
|
|
SkFloatToScalar(0.0f),
|
|
|
|
SkFloatToScalar(0.0f),
|
|
|
|
(SkColor)0xFF000000))->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
const char* text = "DRAWING STROKED TEXT WITH A BLUR ON A PATH";
|
|
|
|
size_t len = strlen(text);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-05-08 13:15:37 +00:00
|
|
|
canvas->drawTextOnPathHV(text, len, path, 0,
|
|
|
|
SkFloatToScalar(-0.025f), paint);
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->restore();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
static void textPathMatrix(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
SkPath path;
|
|
|
|
SkMatrix matrix;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
path.moveTo(SkIntToScalar(050), SkIntToScalar(200));
|
|
|
|
path.quadTo(SkIntToScalar(250), SkIntToScalar(000),
|
|
|
|
SkIntToScalar(450), SkIntToScalar(200));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->drawPath(path, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setTextSize(SkIntToScalar(48));
|
|
|
|
paint.setTextAlign(SkPaint::kRight_Align);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2010-12-20 18:26:13 +00:00
|
|
|
const char* text = "Reflection";
|
2008-12-17 15:59:43 +00:00
|
|
|
size_t len = strlen(text);
|
2012-03-15 15:01:34 +00:00
|
|
|
|
|
|
|
SkPathMeasure meas(path, false);
|
|
|
|
SkScalar pathLen = meas.getLength();
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
canvas->drawTextOnPath(text, len, path, NULL, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
matrix.setScale(-SK_Scalar1, SK_Scalar1);
|
|
|
|
matrix.postTranslate(pathLen, 0);
|
|
|
|
canvas->drawTextOnPath(text, len, path, &matrix, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
matrix.setScale(SK_Scalar1, -SK_Scalar1);
|
|
|
|
canvas->drawTextOnPath(text, len, path, &matrix, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
matrix.setScale(-SK_Scalar1, -SK_Scalar1);
|
|
|
|
matrix.postTranslate(pathLen, 0);
|
|
|
|
canvas->drawTextOnPath(text, len, path, &matrix, paint);
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
class TextOnPathView : public SampleView {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
SkPath fPath;
|
|
|
|
SkScalar fHOffset;
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
TextOnPathView() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkRect r;
|
|
|
|
r.set(SkIntToScalar(100), SkIntToScalar(100),
|
|
|
|
SkIntToScalar(300), SkIntToScalar(300));
|
|
|
|
fPath.addOval(r);
|
2012-03-15 15:01:34 +00:00
|
|
|
fPath.offset(SkIntToScalar(-50), SkIntToScalar(-50));
|
2010-12-20 18:26:13 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fHOffset = SkIntToScalar(50);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Text On Path");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2012-03-15 15:01:34 +00:00
|
|
|
paint.setTextSize(SkIntToScalar(48));
|
|
|
|
|
|
|
|
const char* text = "Hamburgefons";
|
|
|
|
size_t len = strlen(text);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < REPEAT_COUNT; j++) {
|
|
|
|
SkScalar x = fHOffset;
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->drawTextOnPathHV(text, len, fPath,
|
2008-12-17 15:59:43 +00:00
|
|
|
x, paint.getTextSize()/2, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->drawTextOnPathHV(text, len, fPath,
|
2008-12-17 15:59:43 +00:00
|
|
|
x + SkIntToScalar(50), 0, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->drawTextOnPathHV(text, len, fPath,
|
2008-12-17 15:59:43 +00:00
|
|
|
x + SkIntToScalar(100), -paint.getTextSize()/2, paint);
|
|
|
|
}
|
2012-03-15 15:01:34 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->drawPath(fPath, paint);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-15 15:01:34 +00:00
|
|
|
canvas->translate(SkIntToScalar(275), 0);
|
|
|
|
textStrokePath(canvas);
|
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(-275), SkIntToScalar(250));
|
|
|
|
textPathMatrix(canvas);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (REPEAT_COUNT > 1)
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
|
|
|
|
fHints += 1;
|
|
|
|
this->inval(NULL);
|
|
|
|
return this->INHERITED::onFindClickHandler(x, y);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual bool onClick(Click* click) {
|
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
int fHints;
|
2011-04-22 14:10:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() {
|
|
|
|
return new TextOnPathView;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkViewRegister reg(MyFactory);
|