blob: c5ed0e9724568252a45f6c4596deba33acc00b32 [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"
9#include "SkCanvas.h"
10#include "SkPathUtils.h"
11#include "SkView.h"
12//#include "SkPathOps.h" // loads fine here, won't in PathUtils src files
13#include "SkRandom.h"
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000014//#include "SkTime.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000015
16class samplePathUtils : public SampleView {
17public:
18 samplePathUtils() {
19 bmp_paint.setAntiAlias(true); // Black paint for bitmap
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000020 bmp_paint.setStyle(SkPaint::kFill_Style);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000021 bmp_paint.setColor(SK_ColorBLACK);
22 }
23
24protected:
25 static const int numModes = 3;
robertphillips@google.com95faedb2013-07-01 23:15:57 +000026 static const int h=8, w=12, stride=2; // stride is in bytes
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000027 static const int numChars = h * stride; // number of chars in entire array
28
29 SkPaint bmp_paint;
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000030
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000031 // overrides from SkEventSink
32 virtual bool onQuery(SkEvent* evt) {
33 if (SampleCode::TitleQ(*evt)) {
34 SampleCode::TitleR(evt, "PathUtils");
35 return true;
36 }
37 return this->INHERITED::onQuery(evt);
38 }
39
40 /////////////////////////////////////////////////////////////
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000041
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000042 virtual void onDrawContent(SkCanvas* canvas) {
43 // bitmap definitions
dierk@google.com74887b62013-07-01 19:30:14 +000044 const uint8_t bits[numModes][numChars] = {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000045 { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00,
46 0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 },
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000047
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000048 { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0,
49 0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 },
50
51 { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70,
52 0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 }
53 };
54
robertphillips@google.com95faedb2013-07-01 23:15:57 +000055 static const SkScalar kScale = 10.0f;
56
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000057 for (int i = 0; i < numModes; ++i) {
58 SkPath path; // generate and simplify each path
59 SkPathUtils::BitsToPath_Path(&path, (char*) &bits[i], h, w, stride);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000060
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000061 canvas->save(); // DRAWING
robertphillips@google.com95faedb2013-07-01 23:15:57 +000062 canvas->scale(kScale, kScale); // scales up each bitmap
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000063 canvas->translate(0, 1.5f * h * i);
64 canvas->drawPath(path, bmp_paint); // draw bitmap
65 canvas->restore();
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000066
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000067 // use the SkRegion method
68 SkPath pathR;
69 SkPathUtils::BitsToPath_Region(&pathR, (char*) &bits[i], h, w, stride);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000070
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000071 canvas->save();
robertphillips@google.com95faedb2013-07-01 23:15:57 +000072
73 canvas->scale(kScale, kScale); // scales up each bitmap
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000074 canvas->translate(1.5f * w, 1.5f * h * i); // translates past previous bitmap
75 canvas->drawPath(pathR, bmp_paint); // draw bitmap
76 canvas->restore();
77 }
78 }
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000079
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000080private:
81 typedef SkView INHERITED;
82};
83
84//////////////////////////////////////////////////////////////////////////////
85
86static SkView* MyFactory() { return new samplePathUtils; }
87static SkViewRegister reg(MyFactory)
88;