1/*
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"
9
10#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 },
28
29    { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0,
30      0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 },
31
32    { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70,
33      0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 }
34};
35
36
37class SamplePathUtils : public SampleView {
38public:
39    static const int fNumBits = 3;
40    static const int fH = 8, fW = 12;
41    static const size_t fRowBytes = 2;
42    static const int fNumChars = fH * fRowBytes;
43
44    SkPaint fBmpPaint;
45    SkScalar fPhase;
46
47    SamplePathUtils() {
48        fBmpPaint.setAntiAlias(true);  // Black paint for bitmap
49        fBmpPaint.setStyle(SkPaint::kFill_Style);
50
51        fPhase = 0.0f; // to animate the dashed path
52    }
53
54protected:
55    // 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    /////////////////////////////////////////////////////////////
65
66    virtual void onDrawContent(SkCanvas* canvas) {
67        SkScalar intervals[8] = { .5f, .3f, .5f, .3f, .5f, .3f, .5f, .3f };
68        SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, fPhase));
69        SkAutoTUnref<SkCornerPathEffect> corner(SkCornerPathEffect::Create(.25f));
70        SkAutoTUnref<SkComposePathEffect> compose(SkComposePathEffect::Create(dash, corner));
71
72        SkPaint outlinePaint;
73        outlinePaint.setAntiAlias(true);  // dashed paint for bitmap
74        outlinePaint.setStyle(SkPaint::kStroke_Style);
75        outlinePaint.setPathEffect(compose);
76
77        canvas->scale(10.0f, 10.0f);  // scales up
78
79        for (int i = 0; i < fNumBits; ++i) {
80            canvas->save();
81            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);
84
85                //draw skPath and outline
86                canvas->drawPath(path, fBmpPaint);
87                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            }
91            canvas->restore();
92            canvas->translate(0, 1.5f * fH); //translate to next row
93        }
94
95        // for animated pathEffect
96        fPhase += .01f;
97        this->inval(NULL);
98    }
99
100private:
101    typedef SkView INHERITED;
102};
103
104//////////////////////////////////////////////////////////////////////////////
105
106static SkView* MyFactory() { return new SamplePathUtils; }
107static SkViewRegister reg(MyFactory)
108;
109