• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(ChromeMDRefreshTab, 256, 256, false, 0) {
GetBorderPath(float scale,bool unscale_at_end,bool extend_to_top,float endcap_width,const SkISize & size)5 SkPath GetBorderPath(float scale,
6                      bool unscale_at_end,
7                      bool extend_to_top,
8                      float endcap_width,
9                      const SkISize& size) {
10     //  const float top = scale - 1;
11     const float right = size.fWidth * scale;
12     const float bottom = size.fHeight * scale;
13     const float scaled_endcap_radius = (endcap_width / 2) * scale;
14 
15     SkPath path;
16     path.moveTo(0, bottom - 1);
17     // bottom left
18     path.arcTo(scaled_endcap_radius - 1, scaled_endcap_radius - 1, 90, SkPath::kSmall_ArcSize,
19                SkPathDirection::kCCW, scaled_endcap_radius - 1,
20                bottom - 1 - scaled_endcap_radius);
21     // path.rLineTo(0, -1);
22     // path.rCubicTo(0.75 * scale, 0, 1.625 * scale, -0.5 * scale, 2 * scale,
23     //               -1.5 * scale);
24     path.lineTo(scaled_endcap_radius - 1, scaled_endcap_radius + 1);
25     // path.lineTo((endcap_width - 2) * scale, top + 1.5 * scale);
26     // top left
27     path.arcTo(scaled_endcap_radius + 1, scaled_endcap_radius + 1, 90, SkPath::kSmall_ArcSize,
28                SkPathDirection::kCW, scaled_endcap_radius * 2, -1);
29 
30     // if (extend_to_top) {
31     // Create the vertical extension by extending the side diagonals until
32     // they reach the top of the bounds.
33     //  const float dy = 2.5 * scale - 1;
34     //  const float dx = Tab::GetInverseDiagonalSlope() * dy;
35     //  path.rLineTo(dx, -dy);
36     //  path.lineTo(right - (endcap_width - 2) * scale - dx, 0);
37     //  path.rLineTo(dx, dy);
38     //} else {
39     //  path.rCubicTo(0.375 * scale, -scale, 1.25 * scale, -1.5 * scale, 2 * scale,
40     //                -1.5 * scale);
41     //  path.lineTo(right - endcap_width * scale, top);
42     //  path.rCubicTo(0.75 * scale, 0, 1.625 * scale, 0.5 * scale, 2 * scale,
43     //                1.5 * scale);
44     //}
45     path.lineTo(right - scaled_endcap_radius * 2, -1);
46     // path.lineTo(right - 2 * scale, bottom - 1 - 1.5 * scale);
47 
48     // top right
49     path.arcTo(scaled_endcap_radius + 1, scaled_endcap_radius + 1, 90, SkPath::kSmall_ArcSize,
50                SkPathDirection::kCW, right - scaled_endcap_radius + 1,
51                scaled_endcap_radius + 1);
52     // path.arcTo(SkRect::MakeLTRB(right - 2 * scale, bottom - 1 - 1.5 * scale,
53     //    right - 2 * scale + 4, bottom - 1 - 1.5 * scale + 4), 90, 90, true);
54     // path.rCubicTo(0.375 * scale, scale, 1.25 * scale, 1.5 * scale, 2 * scale,
55     //              1.5 * scale);
56 
57     path.lineTo(right - scaled_endcap_radius + 1, bottom - 1 - scaled_endcap_radius);
58     // path.rLineTo(0, 1);
59 
60     // bottom right
61     path.arcTo(scaled_endcap_radius - 1, scaled_endcap_radius - 1, 90, SkPath::kSmall_ArcSize,
62                SkPathDirection::kCCW, right, bottom - 1);
63     path.close();
64 
65     if (unscale_at_end && (scale != 1)) path.transform(SkMatrix::MakeScale(1.f / scale));
66 
67     return path;
68 }
69 
GetInteriorPath(float scale,const SkISize & size,float endcap_width,float horizontal_inset=0)70 SkPath GetInteriorPath(
71         float scale, const SkISize& size, float endcap_width, float horizontal_inset = 0) {
72     const float right = size.fWidth * scale;
73     // The bottom of the tab needs to be pixel-aligned or else when we call
74     // ClipPath with anti-aliasing enabled it can cause artifacts.
75     const float bottom = std::ceil(size.fHeight * scale);
76 
77     // const float scaled_horizontal_inset = horizontal_inset * scale;
78 
79     const float scaled_endcap_radius = (endcap_width / 2) * scale;
80 
81     // Construct the interior path by intersecting paths representing the left
82     // and right halves of the tab.  Compared to computing the full path at once,
83     // this makes it easier to avoid overdraw in the top center near minimum
84     // width, and to implement cases where |horizontal_inset| != 0.
85     SkPath right_path;
86 
87     right_path.moveTo(right, bottom);
88     // right_path.moveTo(right - 1 - scaled_horizontal_inset, bottom);
89 
90     right_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize,
91                      SkPathDirection::kCW, right - scaled_endcap_radius,
92                      bottom - scaled_endcap_radius);
93     // right_path.rCubicTo(-0.75 * scale, 0, -1.625 * scale, -0.5 * scale,
94     // -2 * scale, -1.5 * scale);
95 
96     right_path.lineTo(right - scaled_endcap_radius, scaled_endcap_radius);
97     // right_path.lineTo(
98     //    right - 1 - scaled_horizontal_inset - (endcap_width - 2) * scale,
99     //    2.5 * scale);
100 
101     right_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize,
102                      SkPathDirection::kCCW, right - scaled_endcap_radius * 2, 0);
103     // right_path.rCubicTo(-0.375 * scale, -1 * scale, -1.25 * scale, -1.5 * scale,
104     //                    -2 * scale, -1.5 * scale);
105 
106     right_path.lineTo(0, 0);
107     right_path.lineTo(0, bottom);
108     right_path.close();
109 
110     SkPath left_path;
111     // const float scaled_endcap_width = 1 + endcap_width * scale;
112     left_path.moveTo(scaled_endcap_radius * 2, 0);
113     // left_path.moveTo(scaled_endcap_width + scaled_horizontal_inset, scale);
114 
115     left_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize,
116                     SkPathDirection::kCCW, scaled_endcap_radius, scaled_endcap_radius);
117     // left_path.rCubicTo(-0.75 * scale, 0, -1.625 * scale, 0.5 * scale, -2 * scale,
118     //                   1.5 * scale);
119 
120     left_path.lineTo(scaled_endcap_radius, bottom - scaled_endcap_radius);
121     // left_path.lineTo(1 + scaled_horizontal_inset + 2 * scale,
122     //                 bottom - 1.5 * scale);
123 
124     left_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize,
125                     SkPathDirection::kCW, 0, bottom);
126     // left_path.rCubicTo(-0.375 * scale, scale, -1.25 * scale, 1.5 * scale,
127     //                   -2 * scale, 1.5 * scale);
128 
129     left_path.lineTo(right, bottom);
130     left_path.lineTo(right, 0);
131     left_path.close();
132 
133     SkPath complete_path;
134     Op(left_path, right_path, SkPathOp::kIntersect_SkPathOp, &complete_path);
135     return complete_path;
136 }
137 
draw(SkCanvas * canvas)138 void draw(SkCanvas* canvas) {
139     SkPaint p;
140     p.setColor(SK_ColorRED);
141     p.setAntiAlias(true);
142     p.setStyle(SkPaint::kStroke_Style);
143     p.setStrokeWidth(1);
144     SkPath path = GetInteriorPath(1.f, SkISize::Make(250, 36), 16);
145     path.transform(SkMatrix::MakeTrans(0, 30));
146     canvas->drawPath(path, p);
147 
148     p.setColor(SK_ColorBLUE);
149     SkPath border_path = GetBorderPath(1.f, false, false, 16, SkISize::Make(250, 36));
150     border_path.transform(SkMatrix::MakeTrans(0, 30));
151     canvas->drawPath(border_path, p);
152 
153     //  canvas->drawLine(20, 20, 100, 100, p);
154 }
155 }  // END FIDDLE
156