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.
|
|
|
|
*/
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2015-02-06 06:02:37 +00:00
|
|
|
#include "SkAnimTimer.h"
|
2018-01-23 16:24:08 +00:00
|
|
|
#include "SkBitmap.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
2019-01-08 19:00:08 +00:00
|
|
|
#include "SkFont.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
2018-08-21 15:45:46 +00:00
|
|
|
#include "SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#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"
|
|
|
|
|
2015-08-26 12:15:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-08-28 20:06:54 +00:00
|
|
|
// 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
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class PathView : public Sample {
|
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:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "Paths");
|
2008-12-17 15:59:43 +00:00
|
|
|
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
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2012-03-12 21:14:09 +00:00
|
|
|
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
|
|
|
}
|
2015-07-13 13:18:39 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
2015-02-06 06:02:37 +00:00
|
|
|
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
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
fShowHairline = !fShowHairline;
|
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:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample 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-16 16:07:43 +00:00
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class ArcToView : public Sample {
|
2017-07-23 02:12:59 +00:00
|
|
|
bool fDoFrame, fDoCorner, fDoConic;
|
|
|
|
SkPaint fPtsPaint, 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()
|
2017-07-23 02:12:59 +00:00
|
|
|
: fDoFrame(false), fDoCorner(false), fDoConic(false)
|
2014-12-17 09:47:32 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
2015-07-13 13:18:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
fCornerPaint.setAntiAlias(true);
|
|
|
|
fCornerPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fCornerPaint.setStrokeWidth(13);
|
|
|
|
fCornerPaint.setColor(SK_ColorGREEN);
|
2016-03-18 18:22:57 +00:00
|
|
|
fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-16 16:07:43 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "ArcTo");
|
2014-12-16 16:07:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-17 09:47:32 +00:00
|
|
|
SkUnichar uni;
|
2018-08-08 15:36:17 +00:00
|
|
|
if (Sample::CharQ(*evt, &uni)) {
|
2014-12-17 09:47:32 +00:00
|
|
|
switch (uni) {
|
|
|
|
case '1': this->toggle(fDoFrame); return true;
|
2017-07-23 02:12:59 +00:00
|
|
|
case '2': this->toggle(fDoCorner); return true;
|
|
|
|
case '3': this->toggle(fDoConic); return true;
|
2014-12-17 09:47:32 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 16:07:43 +00:00
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2015-07-13 13:18:39 +00:00
|
|
|
|
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
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2014-12-16 16:07:43 +00:00
|
|
|
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
|
|
|
canvas->drawPath(path, fSkeletonPaint);
|
2014-12-16 16:07:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onClick(Click* click) override {
|
2014-12-16 16:07:43 +00:00
|
|
|
int32_t index;
|
|
|
|
if (click->fMeta.findS32("index", &index)) {
|
|
|
|
SkASSERT((unsigned)index < N);
|
|
|
|
fPts[index] = click->fCurr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2014-12-16 16:07:43 +00:00
|
|
|
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:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2014-12-16 16:07:43 +00:00
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new ArcToView; )
|
2016-11-03 02:09:25 +00:00
|
|
|
|
|
|
|
/////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class FatStroke : public Sample {
|
2016-11-03 02:09:25 +00:00
|
|
|
bool fClosed, fShowStroke, fShowHidden, fShowSkeleton;
|
|
|
|
int fJoinType, fCapType;
|
2016-12-15 15:23:42 +00:00
|
|
|
float fWidth = 30;
|
2016-11-03 02:09:25 +00:00
|
|
|
SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
N = 4
|
|
|
|
};
|
|
|
|
SkPoint fPts[N];
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true),
|
|
|
|
fJoinType(0), fCapType(0)
|
|
|
|
{
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fPts[i].fX = 20 + rand.nextUScalar1() * 640;
|
|
|
|
fPts[i].fY = 20 + rand.nextUScalar1() * 480;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
fPtsPaint.setAntiAlias(true);
|
|
|
|
fPtsPaint.setStrokeWidth(10);
|
|
|
|
fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
fHiddenPaint.setAntiAlias(true);
|
|
|
|
fHiddenPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fHiddenPaint.setColor(0xFF0000FF);
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
fStrokePaint.setAntiAlias(true);
|
|
|
|
fStrokePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fStrokePaint.setStrokeWidth(50);
|
|
|
|
fStrokePaint.setColor(0x8000FF00);
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
fSkeletonPaint.setAntiAlias(true);
|
|
|
|
fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fSkeletonPaint.setColor(SK_ColorRED);
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
void toggle(bool& value) {
|
|
|
|
value = !value;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
void toggle3(int& value) {
|
|
|
|
value = (value + 1) % 3;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "FatStroke");
|
2016-11-03 02:09:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar uni;
|
2018-08-08 15:36:17 +00:00
|
|
|
if (Sample::CharQ(*evt, &uni)) {
|
2016-11-03 02:09:25 +00:00
|
|
|
switch (uni) {
|
|
|
|
case '1': this->toggle(fShowSkeleton); return true;
|
|
|
|
case '2': this->toggle(fShowStroke); return true;
|
|
|
|
case '3': this->toggle(fShowHidden); return true;
|
|
|
|
case '4': this->toggle3(fJoinType); return true;
|
|
|
|
case '5': this->toggle3(fCapType); return true;
|
|
|
|
case '6': this->toggle(fClosed); return true;
|
2017-11-22 21:36:07 +00:00
|
|
|
case '-': fWidth -= 5; return true;
|
|
|
|
case '=': fWidth += 5; return true;
|
2016-11-03 02:09:25 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
void makePath(SkPath* path) {
|
|
|
|
path->moveTo(fPts[0]);
|
|
|
|
for (int i = 1; i < N; ++i) {
|
|
|
|
path->lineTo(fPts[i]);
|
|
|
|
}
|
|
|
|
if (fClosed) {
|
|
|
|
path->close();
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
canvas->drawColor(0xFFEEEEEE);
|
|
|
|
|
|
|
|
SkPath path;
|
|
|
|
this->makePath(&path);
|
|
|
|
|
2016-12-15 15:23:42 +00:00
|
|
|
fStrokePaint.setStrokeWidth(fWidth);
|
2016-11-03 02:09:25 +00:00
|
|
|
fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType);
|
|
|
|
fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType);
|
|
|
|
|
|
|
|
if (fShowStroke) {
|
|
|
|
canvas->drawPath(path, fStrokePaint);
|
|
|
|
}
|
|
|
|
if (fShowHidden) {
|
|
|
|
SkPath hidden;
|
|
|
|
fStrokePaint.getFillPath(path, &hidden);
|
|
|
|
canvas->drawPath(hidden, fHiddenPaint);
|
|
|
|
}
|
|
|
|
if (fShowSkeleton) {
|
|
|
|
canvas->drawPath(path, fSkeletonPaint);
|
|
|
|
}
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
bool onClick(Click* click) override {
|
|
|
|
int32_t index;
|
|
|
|
if (click->fMeta.findS32("index", &index)) {
|
|
|
|
SkASSERT((unsigned)index < N);
|
|
|
|
fPts[index] = click->fCurr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2016-11-03 02:09:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-11-03 02:09:25 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2016-11-03 02:09:25 +00:00
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new FatStroke; )
|
2018-04-06 23:56:50 +00:00
|
|
|
|
|
|
|
static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) {
|
|
|
|
// F = At^3 + Bt^2 + Ct + D
|
|
|
|
SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f;
|
|
|
|
SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f;
|
|
|
|
SkVector C = (pts[1] - pts[0]) * 3.0f;
|
|
|
|
SkVector DA = pts[3] - pts[0];
|
|
|
|
|
|
|
|
// F' = 3At^2 + 2Bt + C
|
|
|
|
SkScalar a = 3 * A.cross(DA);
|
|
|
|
SkScalar b = 2 * B.cross(DA);
|
|
|
|
SkScalar c = C.cross(DA);
|
|
|
|
|
|
|
|
int n = SkFindUnitQuadRoots(a, b, c, t);
|
|
|
|
SkString str;
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
str.appendf(" %g", t[i]);
|
|
|
|
}
|
|
|
|
SkDebugf("roots %s\n", str.c_str());
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class CubicCurve : public Sample {
|
2018-04-06 23:56:50 +00:00
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
N = 4
|
|
|
|
};
|
|
|
|
SkPoint fPts[N];
|
|
|
|
|
|
|
|
CubicCurve() {
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
fPts[i].fX = 20 + rand.nextUScalar1() * 640;
|
|
|
|
fPts[i].fY = 20 + rand.nextUScalar1() * 480;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "CubicCurve");
|
2018-04-06 23:56:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
{
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(fPts[0]);
|
|
|
|
path.cubicTo(fPts[1], fPts[2], fPts[3]);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
SkScalar t[2];
|
|
|
|
int n = compute_parallel_to_base(fPts, t);
|
|
|
|
SkPoint loc;
|
|
|
|
SkVector tan;
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr);
|
|
|
|
tan.setLength(30);
|
|
|
|
canvas->drawLine(loc - tan, loc + tan, paint);
|
|
|
|
}
|
|
|
|
paint.setStrokeWidth(0.5f);
|
|
|
|
canvas->drawLine(fPts[0], fPts[3], paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setStrokeWidth(6);
|
|
|
|
SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr);
|
|
|
|
canvas->drawPoint(loc, paint);
|
|
|
|
|
|
|
|
paint.setColor(0xFF008800);
|
|
|
|
SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr);
|
|
|
|
canvas->drawPoint(loc, paint);
|
|
|
|
SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr);
|
|
|
|
canvas->drawPoint(loc, paint);
|
|
|
|
|
|
|
|
// n = SkFindCubicInflections(fPts, t);
|
|
|
|
// printf("inflections %d %g %g\n", n, t[0], t[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
for (SkPoint p : fPts) {
|
|
|
|
canvas->drawCircle(p.fX, p.fY, 8, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onClick(Click* click) override {
|
|
|
|
int32_t index;
|
|
|
|
if (click->fMeta.findS32("index", &index)) {
|
|
|
|
SkASSERT((unsigned)index < N);
|
|
|
|
fPts[index] = click->fCurr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2018-04-06 23:56:50 +00:00
|
|
|
const SkScalar tol = 8;
|
|
|
|
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:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2018-04-06 23:56:50 +00:00
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new CubicCurve; )
|
|
|
|
|
|
|
|
static SkPoint lerp(SkPoint a, SkPoint b, float t) {
|
|
|
|
return a * (1 - t) + b * t;
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:07:07 +00:00
|
|
|
static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) {
|
|
|
|
// deviation = F' x (d - a) == 0, solve for t(s)
|
|
|
|
// F = At^3 + Bt^2 + Ct + D
|
|
|
|
// F' = 3At^2 + 2Bt + C
|
|
|
|
// Z = d - a
|
|
|
|
// F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z)
|
|
|
|
//
|
|
|
|
SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0];
|
|
|
|
SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3;
|
|
|
|
SkVector C = (src[1] - src[0]) * 3;
|
|
|
|
SkVector Z = src[3] - src[0];
|
|
|
|
// now forumlate the quadratic coefficients we need to solve for t : F' x Z
|
|
|
|
return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts);
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class CubicCurve2 : public Sample {
|
2018-04-06 23:56:50 +00:00
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
N = 7
|
|
|
|
};
|
|
|
|
SkPoint fPts[N];
|
|
|
|
SkPoint* fQuad = fPts + 4;
|
|
|
|
SkScalar fT = 0.5f;
|
|
|
|
bool fShowSub = false;
|
2018-04-16 00:04:45 +00:00
|
|
|
bool fShowFlatness = false;
|
2018-04-16 21:07:07 +00:00
|
|
|
SkScalar fScale = 0.75;
|
2018-04-06 23:56:50 +00:00
|
|
|
|
|
|
|
CubicCurve2() {
|
|
|
|
fPts[0] = { 90, 300 };
|
|
|
|
fPts[1] = { 30, 60 };
|
|
|
|
fPts[2] = { 250, 30 };
|
|
|
|
fPts[3] = { 350, 200 };
|
|
|
|
|
|
|
|
fQuad[0] = fPts[0] + SkVector{ 300, 0};
|
|
|
|
fQuad[1] = fPts[1] + SkVector{ 300, 0};
|
|
|
|
fQuad[2] = fPts[2] + SkVector{ 300, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "CubicCurve2");
|
2018-04-06 23:56:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar uni;
|
2018-08-08 15:36:17 +00:00
|
|
|
if (Sample::CharQ(*evt, &uni)) {
|
2018-04-06 23:56:50 +00:00
|
|
|
switch (uni) {
|
|
|
|
case 's': fShowSub = !fShowSub; break;
|
2018-04-16 00:04:45 +00:00
|
|
|
case 'f': fShowFlatness = !fShowFlatness; break;
|
2018-04-06 23:56:50 +00:00
|
|
|
case '-': fT -= 1.0f / 32; break;
|
|
|
|
case '=': fT += 1.0f / 32; break;
|
|
|
|
default: goto DONE;
|
|
|
|
}
|
|
|
|
fT = std::min(1.0f, std::max(0.0f, fT));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
DONE:
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) {
|
|
|
|
SkPaint paint(p);
|
|
|
|
SkPoint storage[3 + 2 + 1];
|
|
|
|
SkPoint* tmp = storage;
|
|
|
|
const SkPoint* prev = pts;
|
|
|
|
int n = count;
|
|
|
|
for (int n = count; n > 0; --n) {
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
canvas->drawLine(prev[i], prev[i+1], paint);
|
|
|
|
tmp[i] = lerp(prev[i], prev[i+1], fT);
|
|
|
|
}
|
|
|
|
prev = tmp;
|
|
|
|
tmp += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
n = tmp - storage;
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
canvas->drawCircle(storage[i].fX, storage[i].fY, 4, paint);
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 15:31:34 +00:00
|
|
|
|
2018-04-16 00:04:45 +00:00
|
|
|
void showFlattness(SkCanvas* canvas) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
2018-04-17 21:10:51 +00:00
|
|
|
SkPaint paint2(paint);
|
|
|
|
paint2.setColor(0xFF008800);
|
|
|
|
|
2018-04-16 00:04:45 +00:00
|
|
|
paint.setColor(0xFF888888);
|
|
|
|
canvas->drawLine(fPts[0], fPts[3], paint);
|
|
|
|
canvas->drawLine(fQuad[0], fQuad[2], paint);
|
|
|
|
|
|
|
|
paint.setColor(0xFF0000FF);
|
|
|
|
SkPoint pts[2];
|
|
|
|
pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25;
|
|
|
|
pts[1] = (fQuad[0] + fQuad[2]) * 0.5;
|
|
|
|
canvas->drawLine(pts[0], pts[1], paint);
|
|
|
|
|
2018-04-16 21:07:07 +00:00
|
|
|
// cubic
|
|
|
|
|
|
|
|
SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale;
|
|
|
|
SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale;
|
2018-04-17 21:10:51 +00:00
|
|
|
SkVector v = (v0 + v1) * 0.5f;
|
2018-04-16 21:07:07 +00:00
|
|
|
|
|
|
|
SkPoint anchor;
|
|
|
|
SkScalar ts[2];
|
|
|
|
int n = find_max_deviation_cubic(fPts, ts);
|
|
|
|
if (n > 0) {
|
|
|
|
SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr);
|
2018-04-17 21:10:51 +00:00
|
|
|
canvas->drawLine(anchor, anchor + v, paint2);
|
2018-04-16 21:07:07 +00:00
|
|
|
canvas->drawLine(anchor, anchor + v0, paint);
|
|
|
|
if (n == 2) {
|
|
|
|
SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
|
2018-04-17 21:10:51 +00:00
|
|
|
canvas->drawLine(anchor, anchor + v, paint2);
|
2018-04-16 21:07:07 +00:00
|
|
|
}
|
|
|
|
canvas->drawLine(anchor, anchor + v1, paint);
|
|
|
|
}
|
|
|
|
// not sure we can get here
|
2018-04-16 00:04:45 +00:00
|
|
|
}
|
2018-04-06 23:56:50 +00:00
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
|
|
|
|
{
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(fPts[0]);
|
|
|
|
path.cubicTo(fPts[1], fPts[2], fPts[3]);
|
|
|
|
path.moveTo(fQuad[0]);
|
|
|
|
path.quadTo(fQuad[1], fQuad[2]);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fShowSub) {
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStrokeWidth(1.7f);
|
|
|
|
this->showFrame(canvas, fPts, 3, paint);
|
|
|
|
this->showFrame(canvas, fQuad, 2, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
2019-01-08 19:00:08 +00:00
|
|
|
SkFont font(nullptr, 20);
|
|
|
|
canvas->drawString(SkStringPrintf("t = %g", fT), 20, 20, font, paint);
|
2018-04-06 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 00:04:45 +00:00
|
|
|
if (fShowFlatness) {
|
|
|
|
this->showFlattness(canvas);
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:56:50 +00:00
|
|
|
paint.setStyle(SkPaint::kFill_Style);
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
for (SkPoint p : fPts) {
|
|
|
|
canvas->drawCircle(p.fX, p.fY, 7, paint);
|
|
|
|
}
|
2018-04-19 23:18:35 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
SkScalar ts[2];
|
|
|
|
int n = SkFindCubicInflections(fPts, ts);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
SkPoint p;
|
|
|
|
SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr);
|
|
|
|
canvas->drawCircle(p.fX, p.fY, 3, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool onClick(Click* click) override {
|
|
|
|
int32_t index;
|
|
|
|
if (click->fMeta.findS32("index", &index)) {
|
|
|
|
SkASSERT((unsigned)index < N);
|
|
|
|
fPts[index] = click->fCurr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2018-04-06 23:56:50 +00:00
|
|
|
const SkScalar tol = 8;
|
|
|
|
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:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2018-04-06 23:56:50 +00:00
|
|
|
};
|
|
|
|
DEF_SAMPLE( return new CubicCurve2; )
|
|
|
|
|