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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkScalar.h"
15 #include "include/core/SkSurface.h"
16 #include "tools/ToolUtils.h"
17 
18 #define ZOOM    32
19 #define SMALL_W 9
20 #define SMALL_H 3
21 #define REPEAT_LOOP 5
22 
new_surface(int width,int height)23 static sk_sp<SkSurface> new_surface(int width, int height) {
24     return SkSurface::MakeRasterN32Premul(width, height);
25 }
26 
draw_pixel_centers(SkCanvas * canvas)27 static void draw_pixel_centers(SkCanvas* canvas) {
28     SkPaint paint;
29     paint.setColor(ToolUtils::color_to_565(0xFF0088FF));
30     paint.setAntiAlias(true);
31 
32     for (int y = 0; y < SMALL_H; ++y) {
33         for (int x = 0; x < SMALL_W; ++x) {
34             canvas->drawCircle(x + 0.5f, y + 0.5f, 1.5f / ZOOM, paint);
35         }
36     }
37 }
38 
draw_fatpath(SkCanvas * canvas,SkSurface * surface,const SkPath & path)39 static void draw_fatpath(SkCanvas* canvas, SkSurface* surface, const SkPath& path) {
40     SkPaint paint;
41 
42     surface->getCanvas()->clear(SK_ColorTRANSPARENT);
43     surface->getCanvas()->drawPath(path, paint);
44     surface->draw(canvas, 0, 0);
45 
46     paint.setAntiAlias(true);
47     paint.setColor(SK_ColorRED);
48     paint.setStyle(SkPaint::kStroke_Style);
49     canvas->drawPath(path, paint);
50 
51     draw_pixel_centers(canvas);
52 }
53 
DEF_SIMPLE_GM(fatpathfill,canvas,SMALL_W * ZOOM,SMALL_H * ZOOM * REPEAT_LOOP)54 DEF_SIMPLE_GM(fatpathfill, canvas,
55               SMALL_W * ZOOM,
56               SMALL_H * ZOOM * REPEAT_LOOP) {
57         auto surface(new_surface(SMALL_W, SMALL_H));
58 
59         canvas->scale(ZOOM, ZOOM);
60 
61         SkPaint paint;
62         paint.setStyle(SkPaint::kStroke_Style);
63         paint.setStrokeWidth(SK_Scalar1);
64 
65         for (int i = 0; i < REPEAT_LOOP; ++i) {
66             SkPath line, path;
67             line.moveTo(1, 2);
68             line.lineTo(SkIntToScalar(4 + i), 1);
69             paint.getFillPath(line, &path);
70             draw_fatpath(canvas, surface.get(), path);
71 
72             canvas->translate(0, SMALL_H);
73         }
74 }
75