blob: 503a6eaa25ae6cef22dae3c5f93087a2df78aa91 [file] [log] [blame]
commit-bot@chromium.org064779a2013-07-01 17:50:29 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SampleCode.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +00009
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000010#include "SkCanvas.h"
11#include "SkCornerPathEffect.h"
12#include "SkDashPathEffect.h"
13#include "SkPathUtils.h"
14#include "SkRandom.h"
15#include "SkView.h"
16
17typedef void (*BitsToPath)(SkPath*, const char*, int, int, int);
18
19static const BitsToPath gBitsToPath_fns[] = {
20 SkPathUtils::BitsToPath_Path,
21 SkPathUtils::BitsToPath_Region,
22};
23
24// hardcoded bitmap patterns
25static const uint8_t gBits[][16] = {
26 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00,
27 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 },
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000028
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000029 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0,
30 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 },
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000031
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000032 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70,
33 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 }
34};
35
36
37class SamplePathUtils : public SampleView {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000038public:
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000039 static const int fNumBits = 3;
40 static const int fH = 8, fW = 12;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000041 static const size_t fRowBytes = 2;
42 static const int fNumChars = fH * fRowBytes;
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000043
44 SkPaint fBmpPaint;
45 SkScalar fPhase;
46
47 SamplePathUtils() {
48 fBmpPaint.setAntiAlias(true); // Black paint for bitmap
49 fBmpPaint.setStyle(SkPaint::kFill_Style);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000050
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000051 fPhase = 0.0f; // to animate the dashed path
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000052 }
53
54protected:
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000055 // overrides from SkEventSink
56 virtual bool onQuery(SkEvent* evt) {
57 if (SampleCode::TitleQ(*evt)) {
58 SampleCode::TitleR(evt, "PathUtils");
59 return true;
60 }
61 return this->INHERITED::onQuery(evt);
62 }
63
64 /////////////////////////////////////////////////////////////
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000065
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000066 virtual void onDrawContent(SkCanvas* canvas) {
robertphillips@google.com64527e92013-07-09 16:57:01 +000067 SkScalar intervals[8] = { .5f, .3f, .5f, .3f, .5f, .3f, .5f, .3f };
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000068 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, fPhase));
69 SkAutoTUnref<SkCornerPathEffect> corner(SkCornerPathEffect::Create(.25f));
70 SkAutoTUnref<SkComposePathEffect> compose(SkComposePathEffect::Create(dash, corner));
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000071
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000072 SkPaint outlinePaint;
73 outlinePaint.setAntiAlias(true); // dashed paint for bitmap
74 outlinePaint.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org0a2bf902014-02-20 20:40:19 +000075 outlinePaint.setPathEffect(compose);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000076
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000077 canvas->scale(10.0f, 10.0f); // scales up
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000078
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000079 for (int i = 0; i < fNumBits; ++i) {
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000080 canvas->save();
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000081 for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) {
82 SkPath path;
83 gBitsToPath_fns[j](&path, (char*) &gBits[i], fW, fH, fRowBytes);
robertphillips@google.com95faedb2013-07-01 23:15:57 +000084
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000085 //draw skPath and outline
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000086 canvas->drawPath(path, fBmpPaint);
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000087 canvas->translate(1.5f * fW, 0); // translates past previous bitmap
88 canvas->drawPath(path, outlinePaint);
89 canvas->translate(1.5f * fW, 0); // translates past previous bitmap
90 }
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000091 canvas->restore();
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000092 canvas->translate(0, 1.5f * fH); //translate to next row
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000093 }
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000094
95 // for animated pathEffect
robertphillips@google.com64527e92013-07-09 16:57:01 +000096 fPhase += .01f;
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +000097 this->inval(NULL);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000098 }
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000099
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000100private:
101 typedef SkView INHERITED;
102};
103
104//////////////////////////////////////////////////////////////////////////////
105
commit-bot@chromium.orgd43f6442013-07-09 16:30:38 +0000106static SkView* MyFactory() { return new SamplePathUtils; }
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000107static SkViewRegister reg(MyFactory)
108;