2010-12-20 18:26:13 +00:00
|
|
|
|
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"
|
2015-02-06 06:02:37 +00:00
|
|
|
#include "SkAnimTimer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
2009-10-13 13:31:27 +00:00
|
|
|
#include "SkParsePath.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2009-08-28 20:06:54 +00:00
|
|
|
#include "SkGeometry.h"
|
|
|
|
|
|
|
|
// http://code.google.com/p/skia/issues/detail?id=32
|
|
|
|
static void test_cubic() {
|
|
|
|
SkPoint src[4] = {
|
2011-04-14 17:53:24 +00:00
|
|
|
{ 556.25000f, 523.03003f },
|
|
|
|
{ 556.23999f, 522.96002f },
|
|
|
|
{ 556.21997f, 522.89001f },
|
|
|
|
{ 556.21997f, 522.82001f }
|
2009-08-28 20:06:54 +00:00
|
|
|
};
|
|
|
|
SkPoint dst[11];
|
|
|
|
dst[10].set(42, -42); // one past the end, that we don't clobber these
|
|
|
|
SkScalar tval[] = { 0.33333334f, 0.99999994f };
|
|
|
|
|
|
|
|
SkChopCubicAt(src, dst, tval, 2);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
for (int i = 0; i < 11; i++) {
|
|
|
|
SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-13 13:31:27 +00:00
|
|
|
static void test_cubic2() {
|
|
|
|
const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z";
|
|
|
|
SkPath path;
|
|
|
|
SkParsePath::FromSVGString(str, &path);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-13 13:31:27 +00:00
|
|
|
{
|
2010-12-20 18:26:13 +00:00
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
// windows doesn't have strtof
|
|
|
|
float x = (float)strtod("9.94099e+07", NULL);
|
|
|
|
#else
|
2009-10-13 13:31:27 +00:00
|
|
|
float x = strtof("9.94099e+07", NULL);
|
2010-12-20 18:26:13 +00:00
|
|
|
#endif
|
2009-10-13 13:31:27 +00:00
|
|
|
int ix = (int)x;
|
|
|
|
int fx = (int)(x * 65536);
|
|
|
|
int ffx = SkScalarToFixed(x);
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("%g %x %x %x\n", x, ix, fx, ffx);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-13 13:31:27 +00:00
|
|
|
SkRect r = path.getBounds();
|
|
|
|
SkIRect ir;
|
|
|
|
r.round(&ir);
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("[%g %g %g %g] [%x %x %x %x]\n",
|
|
|
|
SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop),
|
|
|
|
SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom),
|
|
|
|
ir.fLeft, ir.fTop, ir.fRight, ir.fBottom);
|
2009-10-13 13:31:27 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-10-13 13:31:27 +00:00
|
|
|
SkBitmap bitmap;
|
2014-02-17 02:55:57 +00:00
|
|
|
bitmap.allocN32Pixels(300, 200);
|
2009-10-13 13:31:27 +00:00
|
|
|
|
|
|
|
SkCanvas canvas(bitmap);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
canvas.drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
class PathView : public SampleView {
|
2015-02-06 06:02:37 +00:00
|
|
|
SkScalar fPrevSecs;
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2015-02-06 06:02:37 +00:00
|
|
|
SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath fPath[6];
|
|
|
|
bool fShowHairline;
|
2012-03-12 21:14:09 +00:00
|
|
|
bool fOnce;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
PathView() {
|
2015-02-06 06:02:37 +00:00
|
|
|
fPrevSecs = 0;
|
2012-03-12 21:14:09 +00:00
|
|
|
fOnce = false;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-12 21:14:09 +00:00
|
|
|
void init() {
|
|
|
|
if (fOnce) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fOnce = true;
|
|
|
|
|
2009-08-28 20:06:54 +00:00
|
|
|
test_cubic();
|
2009-10-13 13:31:27 +00:00
|
|
|
test_cubic2();
|
2009-08-28 20:06:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fShowHairline = false;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fDStroke = 1;
|
|
|
|
fStroke = 10;
|
|
|
|
fMinStroke = 10;
|
|
|
|
fMaxStroke = 180;
|
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
const SkScalar V = 85;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
fPath[0].moveTo(40, 70);
|
|
|
|
fPath[0].lineTo(70, 70 + SK_ScalarHalf);
|
|
|
|
fPath[0].lineTo(110, 70);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
fPath[1].moveTo(40, 70);
|
|
|
|
fPath[1].lineTo(70, 70 - SK_ScalarHalf);
|
|
|
|
fPath[1].lineTo(110, 70);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
fPath[2].moveTo(V, V);
|
|
|
|
fPath[2].lineTo(50, V);
|
|
|
|
fPath[2].lineTo(50, 50);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
fPath[3].moveTo(50, 50);
|
|
|
|
fPath[3].lineTo(50, V);
|
|
|
|
fPath[3].lineTo(V, V);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
fPath[4].moveTo(50, 50);
|
|
|
|
fPath[4].lineTo(50, V);
|
|
|
|
fPath[4].lineTo(52, 50);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
fPath[5].moveTo(52, 50);
|
|
|
|
fPath[5].lineTo(50, V);
|
|
|
|
fPath[5].lineTo(50, 50);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2011-05-11 05:58:58 +00:00
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SampleCode::TitleR(evt, "Paths");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeJoin(j);
|
2015-02-06 06:02:37 +00:00
|
|
|
paint.setStrokeWidth(fStroke);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
if (fShowHairline) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPath fill;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
paint.getFillPath(path, &fill);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
canvas->drawPath(fill, paint);
|
2011-05-11 05:58:58 +00:00
|
|
|
} else {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawPath(path, paint);
|
2011-05-11 05:58:58 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2012-03-12 21:14:09 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
|
|
|
this->init();
|
2015-02-06 06:02:37 +00:00
|
|
|
canvas->translate(50, 50);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
static const SkPaint::Join gJoins[] = {
|
|
|
|
SkPaint::kBevel_Join,
|
|
|
|
SkPaint::kMiter_Join,
|
|
|
|
SkPaint::kRound_Join
|
|
|
|
};
|
|
|
|
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->save();
|
2011-05-19 19:58:58 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
|
2008-12-17 15:59:43 +00:00
|
|
|
this->drawPath(canvas, fPath[j], gJoins[i]);
|
2015-02-06 06:02:37 +00:00
|
|
|
canvas->translate(200, 0);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-06 06:02:37 +00:00
|
|
|
canvas->translate(0, 200);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2015-02-06 06:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
|
|
|
|
SkScalar currSecs = timer.scaled(100);
|
|
|
|
SkScalar delta = currSecs - fPrevSecs;
|
|
|
|
fPrevSecs = currSecs;
|
|
|
|
|
|
|
|
fStroke += fDStroke * delta;
|
|
|
|
if (fStroke > fMaxStroke || fStroke < fMinStroke) {
|
|
|
|
fDStroke = -fDStroke;
|
|
|
|
}
|
|
|
|
return true;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
|
2008-12-17 15:59:43 +00:00
|
|
|
fShowHairline = !fShowHairline;
|
|
|
|
this->inval(NULL);
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2011-05-11 05:58:58 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2014-12-16 16:07:43 +00:00
|
|
|
DEF_SAMPLE( return new PathView; )
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-12-17 09:47:32 +00:00
|
|
|
#include "SkArcToPathEffect.h"
|
2014-12-16 16:07:43 +00:00
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
|
|
|
|
class ArcToView : public SampleView {
|
2014-12-17 09:47:32 +00:00
|
|
|
bool fDoFrame, fDoArcTo, fDoCorner, fDoConic;
|
|
|
|
SkPaint fPtsPaint, fArcToPaint, fSkeletonPaint, fCornerPaint;
|
2014-12-16 16:07:43 +00:00
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
N = 4
|
|
|
|
};
|
|
|
|
SkPoint fPts[N];
|
|
|
|
|
2014-12-17 09:47:32 +00:00
|
|
|
ArcToView()
|
|
|
|
: fDoFrame(false), fDoArcTo(false), fDoCorner(false), fDoConic(false)
|
|
|
|
{
|
2014-12-16 16:07:43 +00:00
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fPts[i].fX = 20 + rand.nextUScalar1() * 640;
|
|
|
|
fPts[i].fY = 20 + rand.nextUScalar1() * 480;
|
|
|
|
}
|
2014-12-17 09:47:32 +00:00
|
|
|
|
|
|
|
const SkScalar rad = 50;
|
2014-12-16 16:07:43 +00:00
|
|
|
|
|
|
|
fPtsPaint.setAntiAlias(true);
|
|
|
|
fPtsPaint.setStrokeWidth(15);
|
|
|
|
fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
|
|
|
|
|
2014-12-17 09:47:32 +00:00
|
|
|
fArcToPaint.setAntiAlias(true);
|
|
|
|
fArcToPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fArcToPaint.setStrokeWidth(9);
|
|
|
|
fArcToPaint.setColor(0x800000FF);
|
|
|
|
fArcToPaint.setPathEffect(SkArcToPathEffect::Create(rad))->unref();
|
2014-12-16 16:07:43 +00:00
|
|
|
|
|
|
|
fCornerPaint.setAntiAlias(true);
|
|
|
|
fCornerPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fCornerPaint.setStrokeWidth(13);
|
|
|
|
fCornerPaint.setColor(SK_ColorGREEN);
|
2014-12-17 09:47:32 +00:00
|
|
|
fCornerPaint.setPathEffect(SkCornerPathEffect::Create(rad*2))->unref();
|
2014-12-16 16:07:43 +00:00
|
|
|
|
|
|
|
fSkeletonPaint.setAntiAlias(true);
|
|
|
|
fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fSkeletonPaint.setColor(SK_ColorRED);
|
|
|
|
}
|
|
|
|
|
2014-12-17 09:47:32 +00:00
|
|
|
void toggle(bool& value) {
|
|
|
|
value = !value;
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-16 16:07:43 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
bool onQuery(SkEvent* evt) SK_OVERRIDE {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "ArcTo");
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-17 09:47:32 +00:00
|
|
|
SkUnichar uni;
|
|
|
|
if (SampleCode::CharQ(*evt, &uni)) {
|
|
|
|
switch (uni) {
|
|
|
|
case '1': this->toggle(fDoFrame); return true;
|
|
|
|
case '2': this->toggle(fDoArcTo); return true;
|
|
|
|
case '3': this->toggle(fDoCorner); return true;
|
|
|
|
case '4': this->toggle(fDoConic); return true;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 16:07:43 +00:00
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2014-12-17 09:47:32 +00:00
|
|
|
|
|
|
|
void makePath(SkPath* path) {
|
|
|
|
path->moveTo(fPts[0]);
|
|
|
|
for (int i = 1; i < N; ++i) {
|
|
|
|
path->lineTo(fPts[i]);
|
|
|
|
}
|
|
|
|
if (!fDoFrame) {
|
|
|
|
path->close();
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 16:07:43 +00:00
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
|
|
|
|
|
|
|
|
SkPath path;
|
2014-12-17 09:47:32 +00:00
|
|
|
this->makePath(&path);
|
2014-12-16 16:07:43 +00:00
|
|
|
|
2014-12-17 09:47:32 +00:00
|
|
|
if (fDoCorner) {
|
|
|
|
canvas->drawPath(path, fCornerPaint);
|
2014-12-16 16:07:43 +00:00
|
|
|
}
|
2014-12-17 09:47:32 +00:00
|
|
|
if (fDoArcTo) {
|
|
|
|
canvas->drawPath(path, fArcToPaint);
|
2014-12-16 16:07:43 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:47:32 +00:00
|
|
|
canvas->drawPath(path, fSkeletonPaint);
|
2014-12-16 16:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool onClick(Click* click) SK_OVERRIDE {
|
|
|
|
int32_t index;
|
|
|
|
if (click->fMeta.findS32("index", &index)) {
|
|
|
|
SkASSERT((unsigned)index < N);
|
|
|
|
fPts[index] = click->fCurr;
|
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
|
|
|
|
const SkScalar tol = 4;
|
|
|
|
const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2);
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) {
|
|
|
|
Click* click = new Click(this);
|
|
|
|
click->fMeta.setS32("index", i);
|
|
|
|
return click;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new ArcToView; )
|
|
|
|
|