1cb93a386Sopenharmony_ci// Copyright 2020 Google LLC.
2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3cb93a386Sopenharmony_ci#include "tools/fiddle/examples.h"
4cb93a386Sopenharmony_ciREG_FIDDLE(ChromeMDRefreshTabs, 256, 256, false, 0) {
5cb93a386Sopenharmony_ciSkPath GetInteriorPath(
6cb93a386Sopenharmony_ci        float scale, const SkISize& size, float endcap_width, float horizontal_inset = 0) {
7cb93a386Sopenharmony_ci    const float right = size.fWidth * scale;
8cb93a386Sopenharmony_ci    // The bottom of the tab needs to be pixel-aligned or else when we call
9cb93a386Sopenharmony_ci    // ClipPath with anti-aliasing enabled it can cause artifacts.
10cb93a386Sopenharmony_ci    const float bottom = std::ceil(size.fHeight * scale);
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci    // const float scaled_horizontal_inset = horizontal_inset * scale;
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci    const float endcap_radius = endcap_width / 2;
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci    // Construct the interior path by intersecting paths representing the left
17cb93a386Sopenharmony_ci    // and right halves of the tab.  Compared to computing the full path at once,
18cb93a386Sopenharmony_ci    // this makes it easier to avoid overdraw in the top center near minimum
19cb93a386Sopenharmony_ci    // width, and to implement cases where |horizontal_inset| != 0.
20cb93a386Sopenharmony_ci    SkPath right_path;
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci    right_path.moveTo(right, bottom);
23cb93a386Sopenharmony_ci    // right_path.moveTo(right - 1 - scaled_horizontal_inset, bottom);
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci    right_path.arcTo(endcap_radius, endcap_radius, 90, SkPath::kSmall_ArcSize,
26cb93a386Sopenharmony_ci                     SkPathDirection::kCW, right - endcap_radius, bottom - endcap_radius);
27cb93a386Sopenharmony_ci    // right_path.rCubicTo(-0.75 * scale, 0, -1.625 * scale, -0.5 * scale,
28cb93a386Sopenharmony_ci    // -2 * scale, -1.5 * scale);
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci    right_path.lineTo(right - endcap_radius, endcap_radius);
31cb93a386Sopenharmony_ci    // right_path.lineTo(
32cb93a386Sopenharmony_ci    //    right - 1 - scaled_horizontal_inset - (endcap_width - 2) * scale,
33cb93a386Sopenharmony_ci    //    2.5 * scale);
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    right_path.arcTo(endcap_radius, endcap_radius, 90, SkPath::kSmall_ArcSize,
36cb93a386Sopenharmony_ci                     SkPathDirection::kCCW, right - endcap_width, 0);
37cb93a386Sopenharmony_ci    // right_path.rCubicTo(-0.375 * scale, -1 * scale, -1.25 * scale, -1.5 * scale,
38cb93a386Sopenharmony_ci    //                    -2 * scale, -1.5 * scale);
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci    right_path.lineTo(0, 0);
41cb93a386Sopenharmony_ci    right_path.lineTo(0, bottom);
42cb93a386Sopenharmony_ci    right_path.close();
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    SkPath left_path;
45cb93a386Sopenharmony_ci    // const float scaled_endcap_width = 1 + endcap_width * scale;
46cb93a386Sopenharmony_ci    left_path.moveTo(endcap_width, 0);
47cb93a386Sopenharmony_ci    // left_path.moveTo(scaled_endcap_width + scaled_horizontal_inset, scale);
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci    left_path.arcTo(endcap_radius, endcap_radius, 90, SkPath::kSmall_ArcSize,
50cb93a386Sopenharmony_ci                    SkPathDirection::kCCW, endcap_radius, endcap_radius);
51cb93a386Sopenharmony_ci    // left_path.rCubicTo(-0.75 * scale, 0, -1.625 * scale, 0.5 * scale, -2 * scale,
52cb93a386Sopenharmony_ci    //                   1.5 * scale);
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci    left_path.lineTo(endcap_radius, bottom - endcap_radius);
55cb93a386Sopenharmony_ci    // left_path.lineTo(1 + scaled_horizontal_inset + 2 * scale,
56cb93a386Sopenharmony_ci    //                 bottom - 1.5 * scale);
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    left_path.arcTo(endcap_radius, endcap_radius, 90, SkPath::kSmall_ArcSize,
59cb93a386Sopenharmony_ci                    SkPathDirection::kCW, 0, bottom);
60cb93a386Sopenharmony_ci    // left_path.rCubicTo(-0.375 * scale, scale, -1.25 * scale, 1.5 * scale,
61cb93a386Sopenharmony_ci    //                   -2 * scale, 1.5 * scale);
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci    left_path.lineTo(right, bottom);
64cb93a386Sopenharmony_ci    left_path.lineTo(right, 0);
65cb93a386Sopenharmony_ci    left_path.close();
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    SkPath complete_path;
68cb93a386Sopenharmony_ci    Op(left_path, right_path, SkPathOp::kIntersect_SkPathOp, &complete_path);
69cb93a386Sopenharmony_ci    return complete_path;
70cb93a386Sopenharmony_ci}
71cb93a386Sopenharmony_ci
72cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) {
73cb93a386Sopenharmony_ci    SkPaint p;
74cb93a386Sopenharmony_ci    p.setColor(SK_ColorRED);
75cb93a386Sopenharmony_ci    p.setAntiAlias(true);
76cb93a386Sopenharmony_ci    p.setStyle(SkPaint::kStroke_Style);
77cb93a386Sopenharmony_ci    p.setStrokeWidth(1);
78cb93a386Sopenharmony_ci    SkPath path = GetInteriorPath(1.f, SkISize::Make(250, 36), 16);
79cb93a386Sopenharmony_ci    canvas->drawPath(path, p);
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci    //  canvas->drawLine(20, 20, 100, 100, p);
82cb93a386Sopenharmony_ci}
83cb93a386Sopenharmony_ci}  // END FIDDLE
84