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"
|
|
|
|
#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 {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
int fDStroke, fStroke, fMinStroke, fMaxStroke;
|
|
|
|
SkPath fPath[6];
|
|
|
|
bool fShowHairline;
|
2012-03-12 21:14:09 +00:00
|
|
|
bool fOnce;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
|
|
|
PathView() {
|
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;
|
|
|
|
|
|
|
|
const int V = 85;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fPath[0].moveTo(SkIntToScalar(40), SkIntToScalar(70));
|
|
|
|
fPath[0].lineTo(SkIntToScalar(70), SkIntToScalar(70) + SK_Scalar1/1);
|
|
|
|
fPath[0].lineTo(SkIntToScalar(110), SkIntToScalar(70));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fPath[1].moveTo(SkIntToScalar(40), SkIntToScalar(70));
|
|
|
|
fPath[1].lineTo(SkIntToScalar(70), SkIntToScalar(70) - SK_Scalar1/1);
|
|
|
|
fPath[1].lineTo(SkIntToScalar(110), SkIntToScalar(70));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fPath[2].moveTo(SkIntToScalar(V), SkIntToScalar(V));
|
|
|
|
fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(V));
|
|
|
|
fPath[2].lineTo(SkIntToScalar(50), SkIntToScalar(50));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fPath[3].moveTo(SkIntToScalar(50), SkIntToScalar(50));
|
|
|
|
fPath[3].lineTo(SkIntToScalar(50), SkIntToScalar(V));
|
|
|
|
fPath[3].lineTo(SkIntToScalar(V), SkIntToScalar(V));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fPath[4].moveTo(SkIntToScalar(50), SkIntToScalar(50));
|
|
|
|
fPath[4].lineTo(SkIntToScalar(50), SkIntToScalar(V));
|
|
|
|
fPath[4].lineTo(SkIntToScalar(52), SkIntToScalar(50));
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
fPath[5].moveTo(SkIntToScalar(52), SkIntToScalar(50));
|
|
|
|
fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(V));
|
|
|
|
fPath[5].lineTo(SkIntToScalar(50), SkIntToScalar(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
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
void nextStroke() {
|
2008-12-17 15:59:43 +00:00
|
|
|
fStroke += fDStroke;
|
|
|
|
if (fStroke > fMaxStroke || fStroke < fMinStroke)
|
|
|
|
fDStroke = -fDStroke;
|
|
|
|
}
|
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);
|
|
|
|
paint.setStrokeWidth(SkIntToScalar(fStroke));
|
|
|
|
|
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();
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
|
|
|
|
|
|
|
|
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]);
|
|
|
|
canvas->translate(SkIntToScalar(200), 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(200));
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
this->nextStroke();
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2013-01-08 16:17:50 +00:00
|
|
|
virtual 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
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new PathView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|