1 /*
2 * Copyright 2012 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 "gm.h"
9 #include "SkCanvas.h"
10 #include "SkPath.h"
11 #include "SkSurface.h"
12
13 #define ZOOM 32
14 #define SMALL_W 9
15 #define SMALL_H 3
16 #define REPEAT_LOOP 5
17
new_surface(int width,int height)18 static SkSurface* new_surface(int width, int height) {
19 SkImage::Info info = {
20 width,
21 height,
22 SkImage::kPMColor_ColorType,
23 SkImage::kPremul_AlphaType
24 };
25 return SkSurface::NewRaster(info);
26 }
27
draw_pixel_centers(SkCanvas * canvas)28 static void draw_pixel_centers(SkCanvas* canvas) {
29 SkPaint paint;
30 paint.setColor(0xFF0088FF);
31 paint.setAntiAlias(true);
32
33 for (int y = 0; y < SMALL_H; ++y) {
34 for (int x = 0; x < SMALL_W; ++x) {
35 canvas->drawCircle(x + 0.5f, y + 0.5f, 1.5f / ZOOM, paint);
36 }
37 }
38 }
39
draw_fatpath(SkCanvas * canvas,SkSurface * surface,const SkPath paths[],int count)40 static void draw_fatpath(SkCanvas* canvas, SkSurface* surface,
41 const SkPath paths[], int count) {
42 SkPaint paint;
43
44 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
45 for (int i = 0; i < count; ++i) {
46 surface->getCanvas()->drawPath(paths[i], paint);
47 }
48 surface->draw(canvas, 0, 0, NULL);
49
50 paint.setAntiAlias(true);
51 paint.setColor(SK_ColorRED);
52 paint.setStyle(SkPaint::kStroke_Style);
53 for (int j = 0; j < count; ++j) {
54 canvas->drawPath(paths[j], paint);
55 }
56
57 draw_pixel_centers(canvas);
58 }
59
60 class FatPathFillGM : public skiagm::GM {
61 public:
FatPathFillGM()62 FatPathFillGM() {}
63
64 protected:
onShortName()65 virtual SkString onShortName() {
66 return SkString("fatpathfill");
67 }
68
onISize()69 virtual SkISize onISize() {
70 return SkISize::Make(SMALL_W * ZOOM, SMALL_H * ZOOM * REPEAT_LOOP);
71 }
72
onDraw(SkCanvas * canvas)73 virtual void onDraw(SkCanvas* canvas) {
74 SkAutoTUnref<SkSurface> surface(new_surface(SMALL_W, SMALL_H));
75
76 canvas->scale(ZOOM, ZOOM);
77
78 SkPaint paint;
79 paint.setStyle(SkPaint::kStroke_Style);
80 paint.setStrokeWidth(SK_Scalar1);
81
82 for (int i = 0; i < REPEAT_LOOP; ++i) {
83 SkPath line, path;
84 line.moveTo(SkIntToScalar(1), SkIntToScalar(2));
85 line.lineTo(SkIntToScalar(4 + i), SkIntToScalar(1));
86 paint.getFillPath(line, &path);
87 draw_fatpath(canvas, surface, &path, 1);
88
89 canvas->translate(0, SMALL_H);
90 }
91 }
92
93 private:
94 typedef skiagm::GM INHERITED;
95 };
96
97 ///////////////////////////////////////////////////////////////////////////////
98
99 DEF_GM(return new FatPathFillGM;)
100