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