Commit 90c34dc0 authored by Aaron Krajeski's avatar Aaron Krajeski Committed by Commit Bot

Add roundRect to path

Merging this CL with TODOs to keep this CL small. TODOs:
  - Single radius version of function called without an array
  - Accept generic dictionaries with "x" and "y" values as arguments
  - Add WPT tests

Bug: 1123971
Change-Id: I41816beca0678b7df873310d16f826f2f3ed5e07
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2388151Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Reviewed-by: default avatarPhilip Jägenstedt <foolip@chromium.org>
Commit-Queue: Aaron Krajeski <aaronhk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809309}
parent ae3fb04f
......@@ -95,6 +95,8 @@ bindings_modules_generated_union_type_files = [
"$bindings_modules_v8_output_dir/string_or_array_buffer_or_array_buffer_view_or_ndef_message_init.h",
"$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.cc",
"$bindings_modules_v8_output_dir/string_or_canvas_gradient_or_canvas_pattern.h",
"$bindings_modules_v8_output_dir/double_or_dom_point.h",
"$bindings_modules_v8_output_dir/double_or_dom_point.cc",
"$bindings_modules_v8_output_dir/string_or_string_sequence_or_constrain_dom_string_parameters.cc",
"$bindings_modules_v8_output_dir/string_or_string_sequence_or_constrain_dom_string_parameters.h",
"$bindings_modules_v8_output_dir/string_or_unsigned_long.cc",
......
......@@ -36,6 +36,7 @@
#include "third_party/blink/renderer/modules/canvas/canvas2d/canvas_path.h"
#include "base/numerics/safe_conversions.h"
#include "third_party/blink/renderer/core/geometry/dom_point.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
#include "third_party/blink/renderer/platform/transforms/affine_transform.h"
......@@ -448,4 +449,60 @@ void CanvasPath::rect(double double_x,
path_.AddRect(FloatRect(x, y, width, height));
}
void CanvasPath::roundRect(double double_x,
double double_y,
double double_width,
double double_height,
const HeapVector<DoubleOrDOMPoint, 0> radii,
ExceptionState& exception_state) {
const int num_radii = radii.size();
if (num_radii < 1 || num_radii > 4) {
exception_state.ThrowDOMException(
DOMExceptionCode::kIndexSizeError,
String::Number(num_radii) +
" radii provided. Between one and four radii are necessary.");
}
float x = base::saturated_cast<float>(double_x);
float y = base::saturated_cast<float>(double_y);
float width = base::saturated_cast<float>(double_width);
float height = base::saturated_cast<float>(double_height);
if (!IsTransformInvertible())
return;
if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(width) ||
!std::isfinite(height))
return;
FloatRect rect = FloatRect(x, y, width, height);
FloatSize r[num_radii];
for (int i = 0; i < num_radii; ++i) {
if (radii[i].IsDouble()) {
float a = base::saturated_cast<float>(radii[i].GetAsDouble());
r[i] = FloatSize(a, a);
} else { // This radius is a DOMPoint
DOMPoint* p = radii[i].GetAsDOMPoint();
r[i] = FloatSize(base::saturated_cast<float>(p->x()),
base::saturated_cast<float>(p->y()));
}
}
// Order of arguments here is so that this function behaves the same as the
// CSS border-radius property
switch (num_radii) {
case 1:
path_.AddRoundedRect(rect, r[0]);
break;
case 2:
path_.AddRoundedRect(rect, r[0], r[1], r[1], r[0]);
break;
case 3:
path_.AddRoundedRect(rect, r[0], r[1], r[1], r[2]);
break;
case 4:
path_.AddRoundedRect(rect, r[0], r[1], r[3], r[2]);
}
}
} // namespace blink
......@@ -30,6 +30,7 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_CANVAS_PATH_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_CANVAS_PATH_H_
#include "third_party/blink/renderer/bindings/modules/v8/double_or_dom_point.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/graphics/path.h"
#include "third_party/blink/renderer/platform/transforms/affine_transform.h"
......@@ -84,6 +85,12 @@ class MODULES_EXPORT CanvasPath {
double double_y,
double double_width,
double double_height);
void roundRect(double double_x,
double double_y,
double double_width,
double double_height,
const HeapVector<DoubleOrDOMPoint, 0> radii,
ExceptionState&);
virtual bool IsTransformInvertible() const { return true; }
virtual AffineTransform Transform() const {
......
......@@ -13,6 +13,8 @@ interface mixin CanvasPath {
void bezierCurveTo(unrestricted double cp1x, unrestricted double cp1y, unrestricted double cp2x, unrestricted double cp2y, unrestricted double x, unrestricted double y);
[RaisesException] void arcTo(unrestricted double x1, unrestricted double y1, unrestricted double x2, unrestricted double y2, unrestricted double radius);
void rect(unrestricted double x, unrestricted double y, unrestricted double width, unrestricted double height);
[RuntimeEnabled=NewCanvas2DAPI, RaisesException] void roundRect(unrestricted double x, unrestricted double y, unrestricted double w, unrestricted double h, sequence<(double or DOMPoint)> radii);
[RaisesException] void arc(unrestricted double x, unrestricted double y, unrestricted double radius, unrestricted double startAngle, unrestricted double endAngle, optional boolean anticlockwise = false);
[RaisesException] void ellipse(unrestricted double x, unrestricted double y, unrestricted double radiusX, unrestricted double radiusY, unrestricted double rotation, unrestricted double startAngle, unrestricted double endAngle, optional boolean anticlockwise = false);
};
......@@ -1062,6 +1062,7 @@ interface OffscreenCanvasRenderingContext2D
method resetTransform
method restore
method rotate
method roundRect
method save
method scale
method setLineDash
......@@ -1104,6 +1105,7 @@ interface Path2D
method moveTo
method quadraticCurveTo
method rect
method roundRect
interface PaymentInstruments
attribute @@toStringTag
method clear
......
......@@ -1028,6 +1028,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] method resetTransform
[Worker] method restore
[Worker] method rotate
[Worker] method roundRect
[Worker] method save
[Worker] method scale
[Worker] method setLineDash
......@@ -1070,6 +1071,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] method moveTo
[Worker] method quadraticCurveTo
[Worker] method rect
[Worker] method roundRect
[Worker] interface PaymentInstruments
[Worker] attribute @@toStringTag
[Worker] method clear
......
......@@ -1025,6 +1025,7 @@ interface CanvasRenderingContext2D
method resetTransform
method restore
method rotate
method roundRect
method save
method scale
method scrollPathIntoView
......@@ -5745,6 +5746,7 @@ interface OffscreenCanvasRenderingContext2D
method resetTransform
method restore
method rotate
method roundRect
method save
method scale
method setLineDash
......@@ -5865,6 +5867,7 @@ interface Path2D
method moveTo
method quadraticCurveTo
method rect
method roundRect
interface PaymentAddress
attribute @@toStringTag
getter addressLine
......
......@@ -982,6 +982,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] method resetTransform
[Worker] method restore
[Worker] method rotate
[Worker] method roundRect
[Worker] method save
[Worker] method scale
[Worker] method setLineDash
......@@ -1024,6 +1025,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] method moveTo
[Worker] method quadraticCurveTo
[Worker] method rect
[Worker] method roundRect
[Worker] interface PaymentInstruments
[Worker] attribute @@toStringTag
[Worker] method clear
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment