Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 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) { |
| 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 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 65 | if (unscale_at_end && (scale != 1)) path.transform(SkMatrix::Scale(1.f / scale, |
| 66 | 1.f / scale)); |
Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 67 | |
| 68 | return path; |
| 69 | } |
| 70 | |
| 71 | SkPath GetInteriorPath( |
| 72 | float scale, const SkISize& size, float endcap_width, float horizontal_inset = 0) { |
| 73 | const float right = size.fWidth * scale; |
| 74 | // The bottom of the tab needs to be pixel-aligned or else when we call |
| 75 | // ClipPath with anti-aliasing enabled it can cause artifacts. |
| 76 | const float bottom = std::ceil(size.fHeight * scale); |
| 77 | |
| 78 | // const float scaled_horizontal_inset = horizontal_inset * scale; |
| 79 | |
| 80 | const float scaled_endcap_radius = (endcap_width / 2) * scale; |
| 81 | |
| 82 | // Construct the interior path by intersecting paths representing the left |
| 83 | // and right halves of the tab. Compared to computing the full path at once, |
| 84 | // this makes it easier to avoid overdraw in the top center near minimum |
| 85 | // width, and to implement cases where |horizontal_inset| != 0. |
| 86 | SkPath right_path; |
| 87 | |
| 88 | right_path.moveTo(right, bottom); |
| 89 | // right_path.moveTo(right - 1 - scaled_horizontal_inset, bottom); |
| 90 | |
| 91 | right_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize, |
| 92 | SkPathDirection::kCW, right - scaled_endcap_radius, |
| 93 | bottom - scaled_endcap_radius); |
| 94 | // right_path.rCubicTo(-0.75 * scale, 0, -1.625 * scale, -0.5 * scale, |
| 95 | // -2 * scale, -1.5 * scale); |
| 96 | |
| 97 | right_path.lineTo(right - scaled_endcap_radius, scaled_endcap_radius); |
| 98 | // right_path.lineTo( |
| 99 | // right - 1 - scaled_horizontal_inset - (endcap_width - 2) * scale, |
| 100 | // 2.5 * scale); |
| 101 | |
| 102 | right_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize, |
| 103 | SkPathDirection::kCCW, right - scaled_endcap_radius * 2, 0); |
| 104 | // right_path.rCubicTo(-0.375 * scale, -1 * scale, -1.25 * scale, -1.5 * scale, |
| 105 | // -2 * scale, -1.5 * scale); |
| 106 | |
| 107 | right_path.lineTo(0, 0); |
| 108 | right_path.lineTo(0, bottom); |
| 109 | right_path.close(); |
| 110 | |
| 111 | SkPath left_path; |
| 112 | // const float scaled_endcap_width = 1 + endcap_width * scale; |
| 113 | left_path.moveTo(scaled_endcap_radius * 2, 0); |
| 114 | // left_path.moveTo(scaled_endcap_width + scaled_horizontal_inset, scale); |
| 115 | |
| 116 | left_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize, |
| 117 | SkPathDirection::kCCW, scaled_endcap_radius, scaled_endcap_radius); |
| 118 | // left_path.rCubicTo(-0.75 * scale, 0, -1.625 * scale, 0.5 * scale, -2 * scale, |
| 119 | // 1.5 * scale); |
| 120 | |
| 121 | left_path.lineTo(scaled_endcap_radius, bottom - scaled_endcap_radius); |
| 122 | // left_path.lineTo(1 + scaled_horizontal_inset + 2 * scale, |
| 123 | // bottom - 1.5 * scale); |
| 124 | |
| 125 | left_path.arcTo(scaled_endcap_radius, scaled_endcap_radius, 90, SkPath::kSmall_ArcSize, |
| 126 | SkPathDirection::kCW, 0, bottom); |
| 127 | // left_path.rCubicTo(-0.375 * scale, scale, -1.25 * scale, 1.5 * scale, |
| 128 | // -2 * scale, 1.5 * scale); |
| 129 | |
| 130 | left_path.lineTo(right, bottom); |
| 131 | left_path.lineTo(right, 0); |
| 132 | left_path.close(); |
| 133 | |
| 134 | SkPath complete_path; |
| 135 | Op(left_path, right_path, SkPathOp::kIntersect_SkPathOp, &complete_path); |
| 136 | return complete_path; |
| 137 | } |
| 138 | |
| 139 | void draw(SkCanvas* canvas) { |
| 140 | SkPaint p; |
| 141 | p.setColor(SK_ColorRED); |
| 142 | p.setAntiAlias(true); |
| 143 | p.setStyle(SkPaint::kStroke_Style); |
| 144 | p.setStrokeWidth(1); |
| 145 | SkPath path = GetInteriorPath(1.f, SkISize::Make(250, 36), 16); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 146 | path.transform(SkMatrix::Translate(0, 30)); |
Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 147 | canvas->drawPath(path, p); |
| 148 | |
| 149 | p.setColor(SK_ColorBLUE); |
| 150 | SkPath border_path = GetBorderPath(1.f, false, false, 16, SkISize::Make(250, 36)); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 151 | border_path.transform(SkMatrix::Translate(0, 30)); |
Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 152 | canvas->drawPath(border_path, p); |
| 153 | |
| 154 | // canvas->drawLine(20, 20, 100, 100, p); |
| 155 | } |
| 156 | } // END FIDDLE |