1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/utility/arc_curve_path_util.h"
#include <optional>
#include "base/check_op.h"
#include "base/logging.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkPathBuilder.h"
#include "third_party/skia/include/core/SkPoint.h"
#include "ui/gfx/geometry/size.h"
namespace ash::util {
namespace {
// Aliases ---------------------------------------------------------------------
using CornerLocation = ArcCurveCorner::CornerLocation;
// Helpers ---------------------------------------------------------------------
// Returns the corner point specified by `location` on the bounding rectangle
// of `size`.
SkPoint GetCornerPoint(const gfx::Size& size, CornerLocation location) {
bool at_left = false;
switch (location) {
case CornerLocation::kBottomLeft:
case CornerLocation::kTopLeft:
at_left = true;
break;
case CornerLocation::kBottomRight:
case CornerLocation::kTopRight:
at_left = false;
break;
}
bool at_bottom = false;
switch (location) {
case CornerLocation::kBottomLeft:
case CornerLocation::kBottomRight:
at_bottom = true;
break;
case CornerLocation::kTopLeft:
case CornerLocation::kTopRight:
at_bottom = false;
break;
}
return SkPoint::Make(at_left ? 0 : size.width(),
at_bottom ? size.height() : 0);
}
// Returns the next corner counterclockwise from `corner`.
CornerLocation GetNextCorner(CornerLocation corner) {
return corner == CornerLocation::kMax
? CornerLocation::kMin
: static_cast<CornerLocation>(static_cast<size_t>(corner) + 1);
}
// Returns the previous corner counterclockwise from `corner`.
CornerLocation GetPrevCorner(CornerLocation corner) {
return corner == CornerLocation::kMin
? CornerLocation::kMax
: static_cast<CornerLocation>(static_cast<size_t>(corner) - 1);
}
bool IsSizeAndCornerRadiusValid(const gfx::Size& size,
const std::optional<size_t>& corner_radius) {
if (size.IsEmpty()) {
LOG(ERROR) << "GetArcCurveRectPath() is called with an empty size: "
<< size.ToString();
return false;
}
if (corner_radius &&
(2 * corner_radius.value() >
static_cast<size_t>(std::min(size.width(), size.height())))) {
LOG(ERROR) << "GetArcCurveRectPath() is called with a size that is too "
"small for rounded corners; the size: "
<< size.ToString() << ", corner radius: " << *corner_radius;
return false;
}
return true;
}
} // namespace
// ArcCurveCorner --------------------------------------------------------------
ArcCurveCorner::ArcCurveCorner(CornerLocation location,
const gfx::Size& size,
float concave_radius,
float convex_radius)
: location(location),
size(size),
concave_radius(concave_radius),
convex_radius(convex_radius) {
CHECK_LE(convex_radius * 2 + concave_radius, size.width());
CHECK_LE(convex_radius * 2 + concave_radius, size.height());
}
// Utils -----------------------------------------------------------------------
SkPath GetArcCurveRectPath(const gfx::Size& size, const size_t corner_radius) {
if (!IsSizeAndCornerRadiusValid(size, corner_radius)) {
return SkPath();
}
const auto width = size.width();
const auto height = size.height();
const auto bottom_left = SkPoint::Make(0.f, height);
const auto bottom_right = SkPoint::Make(width, height);
const auto top_right = SkPoint::Make(width, 0.f);
const auto top_left = SkPoint::Make(0.f, 0.f);
// One-radius offsets that can be added to or subtracted from coordinates to
// indicate a unidirectional move, e.g., when calculating the endpoint of an
// arc.
const auto horizontal_offset = SkPoint::Make(corner_radius, 0.f);
const auto vertical_offset = SkPoint::Make(0.f, corner_radius);
return SkPathBuilder()
// Start just after the curve of the top-left rounded corner.
.moveTo(0.f, corner_radius)
.arcTo(bottom_left, bottom_left + horizontal_offset, corner_radius)
.arcTo(bottom_right, bottom_right - vertical_offset, corner_radius)
.arcTo(top_right, top_right - horizontal_offset, corner_radius)
.arcTo(top_left, top_left + vertical_offset, corner_radius)
.close()
.detach();
}
SkPath GetArcCurveRectPath(const gfx::Size& size,
const ArcCurveCorner& arc_curve_corner,
const std::optional<size_t>& corner_radius) {
if (!IsSizeAndCornerRadiusValid(size, corner_radius)) {
return SkPath();
}
if (const gfx::Size& arc_corner_size = arc_curve_corner.size;
size.height() < arc_corner_size.height() ||
size.width() < arc_corner_size.width()) {
LOG(ERROR) << "GetArcCurveRectPath() is called with a size that is too "
"small for the arc curve corner; the size: "
<< size.ToString() << ", arc_curve_corner size: "
<< arc_curve_corner.size.ToString();
return SkPath();
}
// Iterate all corners counterclockwise, starting from the top left corner.
// Therefore, the total iteration count should be 4.
SkPathBuilder builder;
CornerLocation current_corner = CornerLocation::kMin;
for (size_t iteration_index = 0; iteration_index < 4; ++iteration_index) {
const SkPoint current_corner_point = GetCornerPoint(size, current_corner);
// Calculate the normalized vector from the previous corner counterclockwise
// to `current_corner`. For example, if `current_corner` is the top left
// one, this vector should be (-1, 0).
SkVector prev_to_current_normalized_offset =
current_corner_point -
GetCornerPoint(size, GetPrevCorner(current_corner));
SkVector::Normalize(&prev_to_current_normalized_offset);
const bool is_arc_curve = current_corner == arc_curve_corner.location;
// Calculate the starting point of the path for `current_corner`.
const SkVector start_offset =
SkVector::Make(prev_to_current_normalized_offset.x() *
(is_arc_curve ? arc_curve_corner.size.width()
: corner_radius.value_or(0)),
prev_to_current_normalized_offset.y() *
(is_arc_curve ? arc_curve_corner.size.height()
: corner_radius.value_or(0)));
const SkPoint corner_path_start = current_corner_point - start_offset;
if (builder.snapshot().isEmpty()) {
builder.moveTo(corner_path_start);
} else {
builder.lineTo(corner_path_start);
}
// Calculate the normalized vector from `current_corner` to the next corner
// counterclockwise. For example, if `current_corner` is the top left one,
// this vector should be (0, 1).
SkVector current_to_next_normalized_offset =
GetCornerPoint(size, GetNextCorner(current_corner)) -
current_corner_point;
SkVector::Normalize(¤t_to_next_normalized_offset);
const SkVector offset_sum =
prev_to_current_normalized_offset + current_to_next_normalized_offset;
// Calculate the remaining offset after excluding the spacing required by
// the convex radius and the concave radius.
const float radius_sum_spacing =
2 * arc_curve_corner.convex_radius + arc_curve_corner.concave_radius;
const auto extra_spacing_offset =
SkVector::Make(arc_curve_corner.size.width(),
arc_curve_corner.size.height()) -
SkVector::Make(radius_sum_spacing, radius_sum_spacing);
if (is_arc_curve) {
// Draw the first convex curve.
SkPoint arc1 = corner_path_start + prev_to_current_normalized_offset *
arc_curve_corner.convex_radius;
SkPoint arc2 =
corner_path_start + offset_sum * arc_curve_corner.convex_radius;
builder.arcTo(arc1, arc2, arc_curve_corner.convex_radius);
// Draw the extra spacing.
arc2 = arc2 + SkVector::Make(extra_spacing_offset.x() *
current_to_next_normalized_offset.x(),
extra_spacing_offset.y() *
current_to_next_normalized_offset.y());
builder.lineTo(arc2);
// Draw the concave curve.
SkPoint last_point = arc2;
arc1 = last_point + current_to_next_normalized_offset *
arc_curve_corner.concave_radius;
arc2 = last_point + offset_sum * arc_curve_corner.concave_radius;
builder.arcTo(arc1, arc2, arc_curve_corner.concave_radius);
// Draw the extra spacing.
last_point = arc2;
arc2 =
last_point +
SkVector::Make(
extra_spacing_offset.x() * prev_to_current_normalized_offset.x(),
extra_spacing_offset.y() * prev_to_current_normalized_offset.y());
builder.lineTo(arc2);
// Draw the second convex curve.
last_point = arc2;
arc1 = last_point +
prev_to_current_normalized_offset * arc_curve_corner.convex_radius;
arc2 = last_point + offset_sum * arc_curve_corner.convex_radius;
builder.arcTo(arc1, arc2, arc_curve_corner.convex_radius);
} else if (corner_radius) {
const SkPoint arc1 =
corner_path_start +
prev_to_current_normalized_offset * corner_radius.value();
const SkPoint arc2 =
corner_path_start + offset_sum * corner_radius.value();
builder.arcTo(arc1, arc2, corner_radius.value());
}
if (current_corner == CornerLocation::kMax) {
builder.close();
} else {
current_corner = GetNextCorner(current_corner);
}
}
return builder.detach();
}
} // namespace ash::util
|