Debian Codegraph @

gm/rect_poly_stroke.cpp

raw
1 /*
2 * Copyright 2025 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/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkRect.h"
13
14 using DrawRectProc = void(SkCanvas*, const SkRect&, const SkPaint&);
15
16 static void draw_rect_as_rect_proc(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
17 canvas->drawRect(rect, paint);
18 }
19 static void draw_rect_with_path_proc(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint) {
20 canvas->drawPath(SkPath::Rect(rect), paint);
21 }
22
23 constexpr int GM_WIDTH = 1150, GM_HEIGHT = 920;
24
25 DEF_SIMPLE_GM(rect_poly_stroke, canvas, GM_WIDTH, GM_HEIGHT) {
26 struct DrawRec {
27 DrawRectProc* proc;
28 SkColor color;
29 };
30 DrawRec const recs[] = {
31 {draw_rect_as_rect_proc, SK_ColorBLACK},
32 {draw_rect_with_path_proc, 0xFF000088},
33 };
34
35 constexpr float W = 100, H = 80;
36 const SkRect rects[] = {
37 {0, 0, W, H},
38 {0, 0, W, 0},
39 {0, 0, 0, H},
40 {0, 0, 0, 0}, // we don't expect this to draw anything
41 };
42 constexpr float spacing = 150;
43
44 const float degrees[] = { 0, -30 };
45
46 constexpr float thickness = 20;
47
48 const SkPaint::Join joins[] = {
49 SkPaint::kMiter_Join,
50 SkPaint::kRound_Join,
51 SkPaint::kBevel_Join,
52 };
53
54 canvas->translate(30, 50);
55
56 SkPaint paint;
57 paint.setAntiAlias(true);
58 paint.setStroke(true);
59 paint.setStrokeWidth(thickness);
60 for (auto j : joins) {
61 paint.setStrokeJoin(j);
62
63 canvas->save();
64 for (auto r : rects) {
65 for (auto angle : degrees) {
66 canvas->save();
67 for (auto rec : recs) {
68 canvas->save();
69 canvas->rotate(angle, r.centerX(), r.centerY());
70 {
71 paint.setStrokeWidth(thickness);
72 paint.setColor(rec.color);
73 rec.proc(canvas, r, paint);
74
75 paint.setStrokeWidth(0);
76 paint.setColor(SK_ColorGREEN);
77 rec.proc(canvas, r, paint);
78 }
79 canvas->restore();
80 canvas->translate(0, spacing);
81 }
82 canvas->restore();
83 canvas->translate(spacing, 0);
84 }
85 }
86 canvas->restore();
87 canvas->translate(0, 2*spacing);
88 }
89 }