2013-07-01 17:50:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SampleCode.h"
|
2013-07-09 16:30:38 +00:00
|
|
|
|
2013-07-01 17:50:29 +00:00
|
|
|
#include "SkCanvas.h"
|
2013-07-09 16:30:38 +00:00
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkDashPathEffect.h"
|
2013-07-01 17:50:29 +00:00
|
|
|
#include "SkPathUtils.h"
|
|
|
|
#include "SkRandom.h"
|
2013-07-09 16:30:38 +00:00
|
|
|
#include "SkView.h"
|
|
|
|
|
|
|
|
typedef void (*BitsToPath)(SkPath*, const char*, int, int, int);
|
|
|
|
|
|
|
|
static const BitsToPath gBitsToPath_fns[] = {
|
|
|
|
SkPathUtils::BitsToPath_Path,
|
|
|
|
SkPathUtils::BitsToPath_Region,
|
|
|
|
};
|
2013-07-01 17:50:29 +00:00
|
|
|
|
2013-07-09 16:30:38 +00:00
|
|
|
// hardcoded bitmap patterns
|
|
|
|
static const uint8_t gBits[][16] = {
|
|
|
|
{ 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00,
|
|
|
|
0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 },
|
2013-07-10 07:00:58 +00:00
|
|
|
|
2013-07-09 16:30:38 +00:00
|
|
|
{ 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0,
|
|
|
|
0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 },
|
2013-07-10 07:00:58 +00:00
|
|
|
|
2013-07-09 16:30:38 +00:00
|
|
|
{ 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70,
|
|
|
|
0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SamplePathUtils : public SampleView {
|
2013-07-01 17:50:29 +00:00
|
|
|
public:
|
2013-07-09 16:30:38 +00:00
|
|
|
static const int fNumBits = 3;
|
|
|
|
static const int fH = 8, fW = 12;
|
2013-07-10 07:00:58 +00:00
|
|
|
static const size_t fRowBytes = 2;
|
|
|
|
static const int fNumChars = fH * fRowBytes;
|
2013-07-09 16:30:38 +00:00
|
|
|
|
|
|
|
SkPaint fBmpPaint;
|
|
|
|
SkScalar fPhase;
|
|
|
|
|
|
|
|
SamplePathUtils() {
|
|
|
|
fBmpPaint.setAntiAlias(true); // Black paint for bitmap
|
|
|
|
fBmpPaint.setStyle(SkPaint::kFill_Style);
|
2013-07-10 07:00:58 +00:00
|
|
|
|
2013-07-09 16:30:38 +00:00
|
|
|
fPhase = 0.0f; // to animate the dashed path
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "PathUtils");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
2013-07-02 07:00:59 +00:00
|
|
|
|
2013-07-01 17:50:29 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2013-07-09 16:57:01 +00:00
|
|
|
SkScalar intervals[8] = { .5f, .3f, .5f, .3f, .5f, .3f, .5f, .3f };
|
2014-02-20 20:40:19 +00:00
|
|
|
SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, fPhase));
|
|
|
|
SkAutoTUnref<SkCornerPathEffect> corner(SkCornerPathEffect::Create(.25f));
|
|
|
|
SkAutoTUnref<SkComposePathEffect> compose(SkComposePathEffect::Create(dash, corner));
|
2013-07-09 16:30:38 +00:00
|
|
|
|
|
|
|
SkPaint outlinePaint;
|
|
|
|
outlinePaint.setAntiAlias(true); // dashed paint for bitmap
|
|
|
|
outlinePaint.setStyle(SkPaint::kStroke_Style);
|
2014-02-20 20:40:19 +00:00
|
|
|
outlinePaint.setPathEffect(compose);
|
2013-07-09 16:30:38 +00:00
|
|
|
|
|
|
|
canvas->scale(10.0f, 10.0f); // scales up
|
|
|
|
|
|
|
|
for (int i = 0; i < fNumBits; ++i) {
|
2013-07-10 07:00:58 +00:00
|
|
|
canvas->save();
|
2013-07-09 16:30:38 +00:00
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) {
|
|
|
|
SkPath path;
|
|
|
|
gBitsToPath_fns[j](&path, (char*) &gBits[i], fW, fH, fRowBytes);
|
|
|
|
|
|
|
|
//draw skPath and outline
|
2013-07-10 07:00:58 +00:00
|
|
|
canvas->drawPath(path, fBmpPaint);
|
2013-07-09 16:30:38 +00:00
|
|
|
canvas->translate(1.5f * fW, 0); // translates past previous bitmap
|
|
|
|
canvas->drawPath(path, outlinePaint);
|
|
|
|
canvas->translate(1.5f * fW, 0); // translates past previous bitmap
|
|
|
|
}
|
2013-07-01 17:50:29 +00:00
|
|
|
canvas->restore();
|
2013-07-09 16:30:38 +00:00
|
|
|
canvas->translate(0, 1.5f * fH); //translate to next row
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
2013-07-09 16:30:38 +00:00
|
|
|
|
|
|
|
// for animated pathEffect
|
2013-07-09 16:57:01 +00:00
|
|
|
fPhase += .01f;
|
2013-07-09 16:30:38 +00:00
|
|
|
this->inval(NULL);
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
2013-07-02 07:00:59 +00:00
|
|
|
|
2013-07-01 17:50:29 +00:00
|
|
|
private:
|
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-07-09 16:30:38 +00:00
|
|
|
static SkView* MyFactory() { return new SamplePathUtils; }
|
2013-07-01 17:50:29 +00:00
|
|
|
static SkViewRegister reg(MyFactory)
|
|
|
|
;
|