blitz-pdf 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -9
- package/THIRD_PARTY_NOTICES.md +20 -0
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/licenses/Noto-OFL.txt +94 -0
- package/licenses/NotoCJK-OFL.txt +92 -0
- package/licenses/NotoEmoji-OFL.txt +93 -0
- package/licenses/Unifont-OFL.txt +90 -0
- package/licenses/font-sources.json +106 -0
- package/package.json +1 -1
- package/src/types.ts +2 -1
- package/vendor/README.md +43 -0
- package/vendor/blitz-paint/Cargo.toml +50 -0
- package/vendor/blitz-paint/LICENSE-APACHE +176 -0
- package/vendor/blitz-paint/LICENSE-MIT +23 -0
- package/vendor/blitz-paint/src/color.rs +66 -0
- package/vendor/blitz-paint/src/debug_overlay.rs +168 -0
- package/vendor/blitz-paint/src/filters.rs +40 -0
- package/vendor/blitz-paint/src/gradient.rs +536 -0
- package/vendor/blitz-paint/src/kurbo_css/css_box.rs +847 -0
- package/vendor/blitz-paint/src/kurbo_css/mod.rs +77 -0
- package/vendor/blitz-paint/src/kurbo_css/non_uniform_radii.rs +60 -0
- package/vendor/blitz-paint/src/layers.rs +95 -0
- package/vendor/blitz-paint/src/lib.rs +190 -0
- package/vendor/blitz-paint/src/render/background.rs +968 -0
- package/vendor/blitz-paint/src/render/border.rs +631 -0
- package/vendor/blitz-paint/src/render/box_shadow.rs +157 -0
- package/vendor/blitz-paint/src/render/clip_path.rs +492 -0
- package/vendor/blitz-paint/src/render/form_controls.rs +103 -0
- package/vendor/blitz-paint/src/render/mask.rs +144 -0
- package/vendor/blitz-paint/src/render.rs +1351 -0
- package/vendor/blitz-paint/src/sizing.rs +63 -0
- package/vendor/blitz-paint/src/text.rs +747 -0
- package/vendor/fontique/Cargo.toml +194 -0
- package/vendor/fontique/LICENSE-APACHE +201 -0
- package/vendor/fontique/LICENSE-MIT +25 -0
- package/vendor/fontique/README.md +54 -0
- package/vendor/fontique/examples/generic_families.rs +37 -0
- package/vendor/fontique/src/attributes.rs +42 -0
- package/vendor/fontique/src/backend/android.rs +218 -0
- package/vendor/fontique/src/backend/coretext.rs +206 -0
- package/vendor/fontique/src/backend/dwrite.rs +395 -0
- package/vendor/fontique/src/backend/fontconfig.rs +844 -0
- package/vendor/fontique/src/backend/mod.rs +75 -0
- package/vendor/fontique/src/charmap.rs +127 -0
- package/vendor/fontique/src/collection/mod.rs +1016 -0
- package/vendor/fontique/src/collection/query.rs +392 -0
- package/vendor/fontique/src/fallback.rs +293 -0
- package/vendor/fontique/src/family.rs +111 -0
- package/vendor/fontique/src/family_name.rs +153 -0
- package/vendor/fontique/src/font.rs +562 -0
- package/vendor/fontique/src/generic.rs +36 -0
- package/vendor/fontique/src/impl_fontconfig.rs +159 -0
- package/vendor/fontique/src/lib.rs +87 -0
- package/vendor/fontique/src/matching.rs +483 -0
- package/vendor/fontique/src/scan.rs +312 -0
- package/vendor/fontique/src/script.rs +271 -0
- package/vendor/fontique/src/source.rs +108 -0
- package/vendor/fontique/src/source_cache.rs +254 -0
- package/wasm/blitz_pdf_bg.wasm +0 -0
|
@@ -0,0 +1,847 @@
|
|
|
1
|
+
use kurbo::{Arc, BezPath, Insets, PathEl, Point, Rect, Shape as _, Vec2};
|
|
2
|
+
use std::{f64::consts::FRAC_PI_2, f64::consts::PI};
|
|
3
|
+
|
|
4
|
+
use super::non_uniform_radii::NonUniformRoundedRectRadii;
|
|
5
|
+
use super::{Corner, CssBoxKind, Direction, Edge, add_insets, get_corner_insets};
|
|
6
|
+
|
|
7
|
+
/// There are several nested boxes at play here:
|
|
8
|
+
/// We have 4 boxes, 4 corners, and clockwise/anticlockwise for a total of 16 different options
|
|
9
|
+
///
|
|
10
|
+
/// ```a
|
|
11
|
+
/// *--------------------------------------------------------* <--- CssBox::OutlineBox
|
|
12
|
+
/// | Outline |
|
|
13
|
+
/// | *----------------------------------------------* | <--- CssBox::BorderBox
|
|
14
|
+
/// | | Border | |
|
|
15
|
+
/// | | *------------------------------------* | | <--- CssBox::PaddingBox
|
|
16
|
+
/// | | | Padding | | |
|
|
17
|
+
/// | | | *--------------------------* | | | <--- CssBox::ContentBox
|
|
18
|
+
/// | | | | Content | | | |
|
|
19
|
+
/// | | | | | | | |
|
|
20
|
+
/// | | | | | | | |
|
|
21
|
+
/// | | | | | | | |
|
|
22
|
+
/// | | | *--------------------------* | | |
|
|
23
|
+
/// | | | | | |
|
|
24
|
+
/// | | *------------------------------------* | |
|
|
25
|
+
/// | | | |
|
|
26
|
+
/// | *----------------------------------------------* |
|
|
27
|
+
/// | |
|
|
28
|
+
/// *--------------------------------------------------------*
|
|
29
|
+
/// ```
|
|
30
|
+
///
|
|
31
|
+
#[derive(Debug, Clone)]
|
|
32
|
+
pub struct CssBox {
|
|
33
|
+
pub border_box: Rect,
|
|
34
|
+
pub padding_box: Rect,
|
|
35
|
+
pub content_box: Rect,
|
|
36
|
+
pub outline_box: Rect,
|
|
37
|
+
|
|
38
|
+
pub padding_width: Insets,
|
|
39
|
+
pub border_width: Insets,
|
|
40
|
+
pub outline_width: f64,
|
|
41
|
+
|
|
42
|
+
pub border_radii: NonUniformRoundedRectRadii,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
impl CssBox {
|
|
46
|
+
pub fn new(
|
|
47
|
+
border_box: Rect,
|
|
48
|
+
border: Insets,
|
|
49
|
+
padding: Insets,
|
|
50
|
+
outline_width: f64,
|
|
51
|
+
mut border_radii: NonUniformRoundedRectRadii,
|
|
52
|
+
) -> Self {
|
|
53
|
+
let padding_box = border_box - border;
|
|
54
|
+
let content_box = padding_box - padding;
|
|
55
|
+
let outline_box = border_box.inset(outline_width);
|
|
56
|
+
|
|
57
|
+
// Correct the border radii if they are too big if two border radii would intersect, then we need to shrink
|
|
58
|
+
// ALL border radii by the same factor such that they do not
|
|
59
|
+
let top_overlap_factor =
|
|
60
|
+
border_box.width() / (border_radii.top_left.x + border_radii.top_right.x);
|
|
61
|
+
let bottom_overlap_factor =
|
|
62
|
+
border_box.width() / (border_radii.bottom_left.x + border_radii.bottom_right.x);
|
|
63
|
+
let left_overlap_factor =
|
|
64
|
+
border_box.height() / (border_radii.top_left.y + border_radii.bottom_left.y);
|
|
65
|
+
let right_overlap_factor =
|
|
66
|
+
border_box.height() / (border_radii.top_right.y + border_radii.bottom_right.y);
|
|
67
|
+
|
|
68
|
+
let min_factor = top_overlap_factor
|
|
69
|
+
.min(bottom_overlap_factor)
|
|
70
|
+
.min(left_overlap_factor)
|
|
71
|
+
.min(right_overlap_factor)
|
|
72
|
+
.min(1.0);
|
|
73
|
+
if min_factor < 1.0 {
|
|
74
|
+
border_radii *= min_factor
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
Self {
|
|
78
|
+
padding_box,
|
|
79
|
+
border_box,
|
|
80
|
+
content_box,
|
|
81
|
+
outline_box,
|
|
82
|
+
outline_width,
|
|
83
|
+
padding_width: padding,
|
|
84
|
+
border_width: border,
|
|
85
|
+
border_radii,
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// Construct a BezPath representing one edge of a box's border.
|
|
90
|
+
/// Takes into account border-radius and the possibility that the edges
|
|
91
|
+
/// are different colors.
|
|
92
|
+
///
|
|
93
|
+
/// Will construct the border by:
|
|
94
|
+
/// - drawing an inner arc
|
|
95
|
+
/// - jumping to an outer arc
|
|
96
|
+
/// - jumping to the next outer arc (completing the edge with the previous)
|
|
97
|
+
/// - drawing an inner arc
|
|
98
|
+
pub fn border_edge_shape(&self, edge: Edge) -> BezPath {
|
|
99
|
+
use {Corner::*, CssBoxKind::*, Direction::*, Edge::*};
|
|
100
|
+
|
|
101
|
+
let mut path = BezPath::new();
|
|
102
|
+
|
|
103
|
+
let (c0, c1) = match edge {
|
|
104
|
+
Top => (TopLeft, TopRight),
|
|
105
|
+
Right => (TopRight, BottomRight),
|
|
106
|
+
Bottom => (BottomRight, BottomLeft),
|
|
107
|
+
Left => (BottomLeft, TopLeft),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// 1. First corner
|
|
111
|
+
// if the radius is bigger than the border, we need to draw the inner arc to fill in the gap
|
|
112
|
+
if self.is_sharp(c0, BorderBox) {
|
|
113
|
+
path.move_to(self.corner(c0, PaddingBox));
|
|
114
|
+
path.line_to(self.corner(c0, BorderBox));
|
|
115
|
+
} else {
|
|
116
|
+
match self.corner_needs_infill(c0) {
|
|
117
|
+
true => {
|
|
118
|
+
path.insert_arc(self.partial_corner_arc(c0, PaddingBox, edge, Anticlockwise))
|
|
119
|
+
}
|
|
120
|
+
false => path.move_to(self.corner(c0, PaddingBox)),
|
|
121
|
+
}
|
|
122
|
+
path.insert_arc(self.partial_corner_arc(c0, BorderBox, edge, Clockwise));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 2. Second corner
|
|
126
|
+
if self.is_sharp(c1, BorderBox) {
|
|
127
|
+
path.line_to(self.corner(c1, BorderBox));
|
|
128
|
+
path.line_to(self.corner(c1, PaddingBox));
|
|
129
|
+
} else {
|
|
130
|
+
path.insert_arc(self.partial_corner_arc(c1, BorderBox, edge, Clockwise));
|
|
131
|
+
match self.corner_needs_infill(c1) {
|
|
132
|
+
true => {
|
|
133
|
+
path.insert_arc(self.partial_corner_arc(c1, PaddingBox, edge, Anticlockwise))
|
|
134
|
+
}
|
|
135
|
+
false => path.line_to(self.corner(c1, PaddingBox)),
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
path
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// Whether any corner of this box has a non-zero border radius.
|
|
143
|
+
pub fn has_border_radius(&self) -> bool {
|
|
144
|
+
let r = &self.border_radii;
|
|
145
|
+
[r.top_left, r.top_right, r.bottom_right, r.bottom_left]
|
|
146
|
+
.iter()
|
|
147
|
+
.any(|radius| radius.x > 0.0 || radius.y > 0.0)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/// Construct a new [`CssBox`] representing a "slice" of this box's border,
|
|
151
|
+
/// running from `start_frac` to `end_frac` of the border width (measured as
|
|
152
|
+
/// a fraction from the outer border-box edge inwards).
|
|
153
|
+
///
|
|
154
|
+
/// The returned box's border region is exactly the requested slice, so
|
|
155
|
+
/// [`CssBox::border_edge_shape`] can then be used to render it. This is used
|
|
156
|
+
/// to draw the two lines of a `double` border.
|
|
157
|
+
pub fn border_slice(&self, start_frac: f64, end_frac: f64) -> CssBox {
|
|
158
|
+
use Corner::*;
|
|
159
|
+
|
|
160
|
+
let scale_insets = |frac: f64| Insets {
|
|
161
|
+
x0: self.border_width.x0 * frac,
|
|
162
|
+
y0: self.border_width.y0 * frac,
|
|
163
|
+
x1: self.border_width.x1 * frac,
|
|
164
|
+
y1: self.border_width.y1 * frac,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
let start_insets = scale_insets(start_frac);
|
|
168
|
+
let slice_border = scale_insets(end_frac - start_frac);
|
|
169
|
+
|
|
170
|
+
// Move the outer edge of the box inwards to the start of the slice.
|
|
171
|
+
let slice_border_box = self.border_box - start_insets;
|
|
172
|
+
|
|
173
|
+
// Border radii shrink as we move inwards through the border, matching the
|
|
174
|
+
// model used when computing inner (padding/content) box radii.
|
|
175
|
+
let reduce = |radius: Vec2, corner: Corner| {
|
|
176
|
+
let inset = get_corner_insets(start_insets, corner);
|
|
177
|
+
Vec2 {
|
|
178
|
+
x: (radius.x - inset.x).max(0.0),
|
|
179
|
+
y: (radius.y - inset.y).max(0.0),
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
let slice_radii = NonUniformRoundedRectRadii {
|
|
183
|
+
top_left: reduce(self.border_radii.top_left, TopLeft),
|
|
184
|
+
top_right: reduce(self.border_radii.top_right, TopRight),
|
|
185
|
+
bottom_right: reduce(self.border_radii.bottom_right, BottomRight),
|
|
186
|
+
bottom_left: reduce(self.border_radii.bottom_left, BottomLeft),
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
CssBox::new(
|
|
190
|
+
slice_border_box,
|
|
191
|
+
slice_border,
|
|
192
|
+
Insets::ZERO,
|
|
193
|
+
0.0,
|
|
194
|
+
slice_radii,
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/// Construct a bezpath drawing the outline
|
|
199
|
+
pub fn outline(&self) -> BezPath {
|
|
200
|
+
let mut path = BezPath::new();
|
|
201
|
+
|
|
202
|
+
// TODO: this has been known to produce quirky outputs with hugely rounded edges
|
|
203
|
+
self.shape(&mut path, CssBoxKind::OutlineBox, Direction::Clockwise);
|
|
204
|
+
path.move_to(self.corner(Corner::TopLeft, CssBoxKind::BorderBox));
|
|
205
|
+
|
|
206
|
+
self.shape(&mut path, CssBoxKind::BorderBox, Direction::Anticlockwise);
|
|
207
|
+
path.move_to(self.corner(Corner::TopLeft, CssBoxKind::BorderBox));
|
|
208
|
+
|
|
209
|
+
path
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/// Construct a bezpath drawing the frame border
|
|
213
|
+
pub fn border_box_path(&self) -> BezPath {
|
|
214
|
+
let mut path = BezPath::new();
|
|
215
|
+
self.shape(&mut path, CssBoxKind::BorderBox, Direction::Clockwise);
|
|
216
|
+
path
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/// Construct a bezpath drawing the frame padding
|
|
220
|
+
pub fn padding_box_path(&self) -> BezPath {
|
|
221
|
+
let mut path = BezPath::new();
|
|
222
|
+
self.shape(&mut path, CssBoxKind::PaddingBox, Direction::Clockwise);
|
|
223
|
+
path
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// Construct a bezpath drawing the frame content
|
|
227
|
+
pub fn content_box_path(&self) -> BezPath {
|
|
228
|
+
let mut path = BezPath::new();
|
|
229
|
+
self.shape(&mut path, CssBoxKind::ContentBox, Direction::Clockwise);
|
|
230
|
+
path
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Whether the border box is a full ellipse (which includes a circle): every
|
|
234
|
+
/// corner radius equals half the box in that axis, i.e. `border-radius: 50%`.
|
|
235
|
+
/// Such a border is a single continuous curve with no straight edges; it is best
|
|
236
|
+
/// drawn as a stroked ellipse rather than a filled two-contour annulus
|
|
237
|
+
/// (see `draw_border`), which otherwise leaves seam notches on a thin ring where
|
|
238
|
+
/// the quarter-arcs meet. Circles are already covered by the stroked-rounded-rect
|
|
239
|
+
/// path (see [`Self::is_uniform_corner_border`]); this catches the true ellipses
|
|
240
|
+
/// (unequal axes) that a rounded rect can't represent.
|
|
241
|
+
pub fn is_elliptical_border(&self) -> bool {
|
|
242
|
+
let (rx, ry) = (
|
|
243
|
+
self.border_box.width() / 2.0,
|
|
244
|
+
self.border_box.height() / 2.0,
|
|
245
|
+
);
|
|
246
|
+
let is_half = |c: Vec2| (c.x - rx).abs() < 0.01 && (c.y - ry).abs() < 0.01;
|
|
247
|
+
let radii = &self.border_radii;
|
|
248
|
+
is_half(radii.top_left)
|
|
249
|
+
&& is_half(radii.top_right)
|
|
250
|
+
&& is_half(radii.bottom_right)
|
|
251
|
+
&& is_half(radii.bottom_left)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/// Whether every corner has an equal x and y radius, i.e. each corner is a
|
|
255
|
+
/// circular (not elliptical) arc. Corners may still differ from one another, so
|
|
256
|
+
/// this also covers plain rectangles (radius 0) and ordinary rounded rectangles,
|
|
257
|
+
/// not just circles. Such a border can be drawn as a single stroked
|
|
258
|
+
/// `kurbo::RoundedRect` rather than the filled per-edge annulus in
|
|
259
|
+
/// `draw_border` — simpler and faster, since it avoids building and filling a
|
|
260
|
+
/// separate path per edge. (`draw_border` additionally requires uniform border
|
|
261
|
+
/// width/color and each radius to be 0 or ≥ the border width for the stroke to
|
|
262
|
+
/// reproduce the CSS shape exactly.)
|
|
263
|
+
pub fn is_uniform_corner_border(&self) -> bool {
|
|
264
|
+
let is_circular = |c: Vec2| (c.x - c.y).abs() < 0.01;
|
|
265
|
+
let radii = &self.border_radii;
|
|
266
|
+
is_circular(radii.top_left)
|
|
267
|
+
&& is_circular(radii.top_right)
|
|
268
|
+
&& is_circular(radii.bottom_right)
|
|
269
|
+
&& is_circular(radii.bottom_left)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
fn shape(&self, path: &mut BezPath, line: CssBoxKind, direction: Direction) {
|
|
273
|
+
use Corner::*;
|
|
274
|
+
|
|
275
|
+
let route = match direction {
|
|
276
|
+
Direction::Clockwise => [TopLeft, TopRight, BottomRight, BottomLeft],
|
|
277
|
+
Direction::Anticlockwise => [TopLeft, BottomLeft, BottomRight, TopRight],
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
for corner in route {
|
|
281
|
+
if self.is_sharp(corner, line) {
|
|
282
|
+
path.insert_point(self.corner(corner, line));
|
|
283
|
+
} else {
|
|
284
|
+
path.insert_arc(self.corner_arc(corner, line, direction));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/// Construct a bezpath drawing the frame
|
|
290
|
+
pub fn shadow_clip(&self, shadow_rect: Rect) -> BezPath {
|
|
291
|
+
let mut path = BezPath::new();
|
|
292
|
+
self.shadow_clip_shape(&mut path, shadow_rect);
|
|
293
|
+
path
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
fn shadow_clip_shape(&self, path: &mut BezPath, shadow_rect: Rect) {
|
|
297
|
+
use Corner::*;
|
|
298
|
+
|
|
299
|
+
for corner in [TopLeft, TopRight, BottomRight, BottomLeft] {
|
|
300
|
+
path.insert_point(self.shadow_clip_corner(corner, shadow_rect));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if self.is_sharp(TopLeft, CssBoxKind::BorderBox) {
|
|
304
|
+
path.move_to(self.corner(TopLeft, CssBoxKind::BorderBox));
|
|
305
|
+
} else {
|
|
306
|
+
const TOLERANCE: f64 = 0.1;
|
|
307
|
+
let arc = self.corner_arc(TopLeft, CssBoxKind::BorderBox, Direction::Anticlockwise);
|
|
308
|
+
let elements = arc.path_elements(TOLERANCE);
|
|
309
|
+
path.extend(elements);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
for corner in [/*TopLeft, */ BottomLeft, BottomRight, TopRight] {
|
|
313
|
+
if self.is_sharp(corner, CssBoxKind::BorderBox) {
|
|
314
|
+
path.insert_point(self.corner(corner, CssBoxKind::BorderBox));
|
|
315
|
+
} else {
|
|
316
|
+
path.insert_arc(self.corner_arc(
|
|
317
|
+
corner,
|
|
318
|
+
CssBoxKind::BorderBox,
|
|
319
|
+
Direction::Anticlockwise,
|
|
320
|
+
));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
fn corner(&self, corner: Corner, css_box: CssBoxKind) -> Point {
|
|
326
|
+
let Rect { x0, y0, x1, y1 } = match css_box {
|
|
327
|
+
CssBoxKind::OutlineBox => self.outline_box,
|
|
328
|
+
CssBoxKind::BorderBox => self.border_box,
|
|
329
|
+
CssBoxKind::PaddingBox => self.padding_box,
|
|
330
|
+
CssBoxKind::ContentBox => self.content_box,
|
|
331
|
+
};
|
|
332
|
+
match corner {
|
|
333
|
+
Corner::TopLeft => Point { x: x0, y: y0 },
|
|
334
|
+
Corner::TopRight => Point { x: x1, y: y0 },
|
|
335
|
+
Corner::BottomLeft => Point { x: x0, y: y1 },
|
|
336
|
+
Corner::BottomRight => Point { x: x1, y: y1 },
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
fn shadow_clip_corner(&self, corner: Corner, shadow_rect: Rect) -> Point {
|
|
341
|
+
let (x, y) = match corner {
|
|
342
|
+
Corner::TopLeft => (shadow_rect.x0, shadow_rect.y0),
|
|
343
|
+
Corner::TopRight => (shadow_rect.x1, shadow_rect.y0),
|
|
344
|
+
Corner::BottomRight => (shadow_rect.x1, shadow_rect.y1),
|
|
345
|
+
Corner::BottomLeft => (shadow_rect.x0, shadow_rect.y1),
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
Point { x, y }
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/// Check if the corner width is smaller than the radius.
|
|
352
|
+
/// If it is, we need to fill in the gap with an arc
|
|
353
|
+
fn corner_needs_infill(&self, corner: Corner) -> bool {
|
|
354
|
+
match corner {
|
|
355
|
+
Corner::TopLeft => {
|
|
356
|
+
self.border_radii.top_left.x > self.border_width.x0
|
|
357
|
+
&& self.border_radii.top_left.y > self.border_width.y0
|
|
358
|
+
}
|
|
359
|
+
Corner::TopRight => {
|
|
360
|
+
self.border_radii.top_right.x > self.border_width.x1
|
|
361
|
+
&& self.border_radii.top_right.y > self.border_width.y0
|
|
362
|
+
}
|
|
363
|
+
Corner::BottomRight => {
|
|
364
|
+
self.border_radii.bottom_right.x > self.border_width.x1
|
|
365
|
+
&& self.border_radii.bottom_right.y > self.border_width.y1
|
|
366
|
+
}
|
|
367
|
+
Corner::BottomLeft => {
|
|
368
|
+
self.border_radii.bottom_left.x > self.border_width.x0
|
|
369
|
+
&& self.border_radii.bottom_left.y > self.border_width.y1
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/// Get the complete arc for a corner, skipping the need for splitting the arc into pieces
|
|
375
|
+
fn corner_arc(&self, corner: Corner, css_box: CssBoxKind, direction: Direction) -> Arc {
|
|
376
|
+
let (center, radii) = self.ellipse(corner, css_box);
|
|
377
|
+
|
|
378
|
+
// Sweep clockwise for outer arcs, counter clockwise for inner arcs
|
|
379
|
+
let sweep_direction = match direction {
|
|
380
|
+
Direction::Anticlockwise => -1.0,
|
|
381
|
+
Direction::Clockwise => 1.0,
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
let offset = match corner {
|
|
385
|
+
Corner::TopLeft => -FRAC_PI_2,
|
|
386
|
+
Corner::TopRight => 0.0,
|
|
387
|
+
Corner::BottomRight => FRAC_PI_2,
|
|
388
|
+
Corner::BottomLeft => PI,
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
let offset = match direction {
|
|
392
|
+
Direction::Clockwise => offset,
|
|
393
|
+
Direction::Anticlockwise => offset + FRAC_PI_2,
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
Arc::new(
|
|
397
|
+
center,
|
|
398
|
+
radii,
|
|
399
|
+
// Note that we apply a fixed offset to get us in the unit circle coordinate system
|
|
400
|
+
// vello chooses the x axis as the start of the arc, so we need to offset by 3pi/2
|
|
401
|
+
offset + PI + FRAC_PI_2,
|
|
402
|
+
FRAC_PI_2 * sweep_direction,
|
|
403
|
+
0.0,
|
|
404
|
+
)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/// Get the arc for a half of a corner.
|
|
408
|
+
/// This handles the case where adjacent border sides have different colors and thus
|
|
409
|
+
/// the corner between the side changes color in the middle. We draw these as separate shapes
|
|
410
|
+
/// and thus need to to get the arc up to the "middle point".
|
|
411
|
+
///
|
|
412
|
+
/// The angle at which the color changes depends on the ratio of the border widths and radii of the corner
|
|
413
|
+
fn partial_corner_arc(
|
|
414
|
+
&self,
|
|
415
|
+
corner: Corner,
|
|
416
|
+
css_box: CssBoxKind,
|
|
417
|
+
edge: Edge,
|
|
418
|
+
direction: Direction,
|
|
419
|
+
) -> Arc {
|
|
420
|
+
use Corner::*;
|
|
421
|
+
use CssBoxKind::*;
|
|
422
|
+
use Edge::*;
|
|
423
|
+
|
|
424
|
+
let (center, radii) = self.ellipse(corner, css_box);
|
|
425
|
+
|
|
426
|
+
// We solve a tiny system of equations to find the start angle
|
|
427
|
+
// This is fixed to a single coordinate system, so we need to adjust the start angle
|
|
428
|
+
let theta = self.start_angle(corner, radii);
|
|
429
|
+
|
|
430
|
+
// Sweep clockwise for outer arcs, counter clockwise for inner arcs
|
|
431
|
+
let sweep_direction = match direction {
|
|
432
|
+
Direction::Anticlockwise => -1.0,
|
|
433
|
+
Direction::Clockwise => 1.0,
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// Easier to reason about if we think about just offsetting the turns from the start
|
|
437
|
+
let offset = match edge {
|
|
438
|
+
Top => 0.0,
|
|
439
|
+
Right => FRAC_PI_2,
|
|
440
|
+
Bottom => PI,
|
|
441
|
+
Left => PI + FRAC_PI_2,
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
// On left/right gets theta, on top/bottom gets pi/2 - theta
|
|
445
|
+
let theta = match edge {
|
|
446
|
+
Top | Bottom => FRAC_PI_2 - theta,
|
|
447
|
+
Right | Left => theta,
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// Depededning on the edge, we need to adjust the start angle
|
|
451
|
+
// We still sweep the same, but the theta split is different since we're cutting in half
|
|
452
|
+
// I imagine you could mnake this simpler using a bit more math
|
|
453
|
+
let start = match (edge, corner, css_box) {
|
|
454
|
+
// Top Edge
|
|
455
|
+
(Top, TopLeft, PaddingBox) => 0.0,
|
|
456
|
+
(Top, TopLeft, BorderBox) => -theta,
|
|
457
|
+
(Top, TopRight, BorderBox) => 0.0,
|
|
458
|
+
(Top, TopRight, PaddingBox) => theta,
|
|
459
|
+
|
|
460
|
+
// Right Edge
|
|
461
|
+
(Right, TopRight, PaddingBox) => 0.0,
|
|
462
|
+
(Right, TopRight, BorderBox) => -theta,
|
|
463
|
+
(Right, BottomRight, BorderBox) => 0.0,
|
|
464
|
+
(Right, BottomRight, PaddingBox) => theta,
|
|
465
|
+
|
|
466
|
+
// Bottom Edge
|
|
467
|
+
(Bottom, BottomRight, PaddingBox) => 0.0,
|
|
468
|
+
(Bottom, BottomRight, BorderBox) => -theta,
|
|
469
|
+
(Bottom, BottomLeft, BorderBox) => 0.0,
|
|
470
|
+
(Bottom, BottomLeft, PaddingBox) => theta,
|
|
471
|
+
|
|
472
|
+
// Left Edge
|
|
473
|
+
(Left, BottomLeft, PaddingBox) => 0.0,
|
|
474
|
+
(Left, BottomLeft, BorderBox) => -theta,
|
|
475
|
+
(Left, TopLeft, BorderBox) => 0.0,
|
|
476
|
+
(Left, TopLeft, PaddingBox) => theta,
|
|
477
|
+
|
|
478
|
+
_ => unreachable!("Invalid edge/corner combination"),
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
Arc::new(
|
|
482
|
+
center,
|
|
483
|
+
radii,
|
|
484
|
+
// Note that we apply a fixed offset to get us in the unit circle coordinate system
|
|
485
|
+
// vello chooses the x axis as the start of the arc, so we need to offset by 3pi/2
|
|
486
|
+
start + offset + PI + FRAC_PI_2,
|
|
487
|
+
theta * sweep_direction,
|
|
488
|
+
0.0,
|
|
489
|
+
)
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/// Check if a corner is sharp (IE the absolute radius is 0)
|
|
493
|
+
fn is_sharp(&self, corner: Corner, side: CssBoxKind) -> bool {
|
|
494
|
+
use Corner::*;
|
|
495
|
+
use CssBoxKind::*;
|
|
496
|
+
|
|
497
|
+
let corner_radii = match corner {
|
|
498
|
+
TopLeft => self.border_radii.top_left,
|
|
499
|
+
TopRight => self.border_radii.top_right,
|
|
500
|
+
BottomLeft => self.border_radii.bottom_left,
|
|
501
|
+
BottomRight => self.border_radii.bottom_right,
|
|
502
|
+
};
|
|
503
|
+
let is_sharp = (corner_radii.x == 0.0) | (corner_radii.y == 0.0);
|
|
504
|
+
if is_sharp {
|
|
505
|
+
return true;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
let css_box: Insets = match side {
|
|
509
|
+
OutlineBox => return false,
|
|
510
|
+
BorderBox => return false,
|
|
511
|
+
PaddingBox => self.border_width,
|
|
512
|
+
ContentBox => add_insets(self.border_width, self.padding_width),
|
|
513
|
+
};
|
|
514
|
+
match corner {
|
|
515
|
+
TopLeft => (corner_radii.x <= css_box.x0) | (corner_radii.y <= css_box.y0),
|
|
516
|
+
TopRight => (corner_radii.x <= css_box.x1) | (corner_radii.y <= css_box.y0),
|
|
517
|
+
BottomLeft => (corner_radii.x <= css_box.x0) | (corner_radii.y <= css_box.y1),
|
|
518
|
+
BottomRight => (corner_radii.x <= css_box.x1) | (corner_radii.y <= css_box.y1),
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/// The `(center, radii)` of the ellipse that a given corner traces along the
|
|
523
|
+
/// given box edge.
|
|
524
|
+
///
|
|
525
|
+
/// The radii are returned as an axis-aligned `(x, y)` pair, matching the CSS
|
|
526
|
+
/// `border-radius` horizontal/vertical radii. We deliberately do *not* return
|
|
527
|
+
/// a [`kurbo::Ellipse`]: `Ellipse::radii()` canonicalises the ellipse via an
|
|
528
|
+
/// SVD, which swaps the axes and introduces a `π/2` rotation whenever the
|
|
529
|
+
/// vertical radius exceeds the horizontal one (`ry > rx`). Callers here build
|
|
530
|
+
/// [`kurbo::Arc`]s with a fixed `x_rotation` of `0`, so that swap would draw
|
|
531
|
+
/// the corner with its axes transposed. Returning the raw radii avoids the
|
|
532
|
+
/// round-trip entirely.
|
|
533
|
+
fn ellipse(&self, corner: Corner, side: CssBoxKind) -> (Point, Vec2) {
|
|
534
|
+
use {Corner::*, CssBoxKind::*};
|
|
535
|
+
let CssBox {
|
|
536
|
+
border_box,
|
|
537
|
+
padding_width,
|
|
538
|
+
border_width,
|
|
539
|
+
border_radii,
|
|
540
|
+
..
|
|
541
|
+
} = self;
|
|
542
|
+
|
|
543
|
+
let corner_radii = match corner {
|
|
544
|
+
TopLeft => border_radii.top_left,
|
|
545
|
+
TopRight => border_radii.top_right,
|
|
546
|
+
BottomLeft => border_radii.bottom_left,
|
|
547
|
+
BottomRight => border_radii.bottom_right,
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
let center = match corner {
|
|
551
|
+
TopLeft => corner_radii,
|
|
552
|
+
TopRight => Vec2 {
|
|
553
|
+
x: border_box.width() - corner_radii.x,
|
|
554
|
+
y: corner_radii.y,
|
|
555
|
+
},
|
|
556
|
+
BottomLeft => Vec2 {
|
|
557
|
+
x: corner_radii.x,
|
|
558
|
+
y: border_box.height() - corner_radii.y,
|
|
559
|
+
},
|
|
560
|
+
BottomRight => Vec2 {
|
|
561
|
+
x: border_box.width() - corner_radii.x,
|
|
562
|
+
y: border_box.height() - corner_radii.y,
|
|
563
|
+
},
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
let radii: Vec2 = match side {
|
|
567
|
+
BorderBox => corner_radii,
|
|
568
|
+
OutlineBox => corner_radii + Vec2::new(self.outline_width, self.outline_width),
|
|
569
|
+
PaddingBox => corner_radii - get_corner_insets(*border_width, corner),
|
|
570
|
+
ContentBox => {
|
|
571
|
+
corner_radii - get_corner_insets(add_insets(*border_width, *padding_width), corner)
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
(border_box.origin() + center, radii)
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
fn start_angle(&self, corner: Corner, radii: Vec2) -> f64 {
|
|
579
|
+
let corner_insets = get_corner_insets(self.border_width, corner);
|
|
580
|
+
start_angle(corner_insets.y, corner_insets.x, radii)
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/// Makes it easier to insert objects into a bezpath without having to do checks
|
|
585
|
+
/// Mostly because I consider the vello api slightly defficient
|
|
586
|
+
trait BuildBezpath {
|
|
587
|
+
const TOLERANCE: f64;
|
|
588
|
+
fn insert_arc(&mut self, arc: Arc);
|
|
589
|
+
fn insert_point(&mut self, point: Point);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
impl BuildBezpath for BezPath {
|
|
593
|
+
/// Vello uses an inner tolerance for creating segments
|
|
594
|
+
/// We're just reusing the value here
|
|
595
|
+
const TOLERANCE: f64 = 0.1;
|
|
596
|
+
|
|
597
|
+
fn insert_arc(&mut self, arc: Arc) {
|
|
598
|
+
let mut elements = arc.path_elements(Self::TOLERANCE);
|
|
599
|
+
match elements.next().unwrap() {
|
|
600
|
+
PathEl::MoveTo(a) if !self.elements().is_empty() => self.push(PathEl::LineTo(a)),
|
|
601
|
+
el => self.push(el),
|
|
602
|
+
}
|
|
603
|
+
self.extend(elements)
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
fn insert_point(&mut self, point: Point) {
|
|
607
|
+
if self.elements().is_empty() {
|
|
608
|
+
self.push(PathEl::MoveTo(point));
|
|
609
|
+
} else {
|
|
610
|
+
self.push(PathEl::LineTo(point));
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/// Get the start angle of the arc based on the border width and the radii
|
|
616
|
+
fn start_angle(bt_width: f64, br_width: f64, radii: Vec2) -> f64 {
|
|
617
|
+
// slope of the border intersection split
|
|
618
|
+
let w = bt_width / br_width;
|
|
619
|
+
let x = radii.y / (w * radii.x);
|
|
620
|
+
|
|
621
|
+
/*
|
|
622
|
+
Any point on an ellipse is given by:
|
|
623
|
+
x = a cos(t)
|
|
624
|
+
y = b sin(t)
|
|
625
|
+
|
|
626
|
+
The equation of the border intersect is:
|
|
627
|
+
y = w (x - a) + b
|
|
628
|
+
|
|
629
|
+
where w is the ratio of the width to the height of the border
|
|
630
|
+
and b is the y intercept of the border
|
|
631
|
+
and x is the x intercept of the border
|
|
632
|
+
|
|
633
|
+
This formula is the result of solving the system of equations:
|
|
634
|
+
x = a cos(t)
|
|
635
|
+
y = b sin(t)
|
|
636
|
+
y = w (x - a) + b
|
|
637
|
+
|
|
638
|
+
b/(w*a) = (cos(t) - 1)/(sin(t) - 1)
|
|
639
|
+
|
|
640
|
+
Substituting s = tan(t/2) turns this into the quadratic
|
|
641
|
+
|
|
642
|
+
(k - 2) s² - 2k s + k = 0 where k = b/(w*a) = x
|
|
643
|
+
|
|
644
|
+
whose relevant root can be written (after rationalising to remove the
|
|
645
|
+
catastrophic cancellation / removable singularity the naive quadratic
|
|
646
|
+
formula has at k == 2) as:
|
|
647
|
+
|
|
648
|
+
s = √k / (√k + √2)
|
|
649
|
+
|
|
650
|
+
This form is well behaved for all k >= 0 (in particular around k == 2,
|
|
651
|
+
which occurs for perfectly ordinary elliptical corners, e.g. a 80px/30px
|
|
652
|
+
radius with 40px/10px border widths), always yielding t in [0, π/2).
|
|
653
|
+
*/
|
|
654
|
+
|
|
655
|
+
use std::f64::consts::SQRT_2;
|
|
656
|
+
let sqrt_x = x.sqrt();
|
|
657
|
+
let s = sqrt_x / (sqrt_x + SQRT_2);
|
|
658
|
+
s.atan() * 2.0
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
#[cfg(test)]
|
|
662
|
+
mod tests {
|
|
663
|
+
use super::*;
|
|
664
|
+
|
|
665
|
+
/// `start_angle` must return the angle `t` at which the border colour split
|
|
666
|
+
/// line crosses the corner ellipse, i.e. the solution of
|
|
667
|
+
/// `(cos t - 1) / (sin t - 1) == k` where `k = radii.y / (w * radii.x)`.
|
|
668
|
+
fn assert_solves(bt: f64, br: f64, radii: Vec2) {
|
|
669
|
+
let t = start_angle(bt, br, radii);
|
|
670
|
+
assert!(t.is_finite(), "start_angle returned {t} for {radii:?}");
|
|
671
|
+
assert!(
|
|
672
|
+
(0.0..=std::f64::consts::FRAC_PI_2).contains(&t),
|
|
673
|
+
"t={t} out of range"
|
|
674
|
+
);
|
|
675
|
+
let w = bt / br;
|
|
676
|
+
let k = radii.y / (w * radii.x);
|
|
677
|
+
let lhs = (t.cos() - 1.0) / (t.sin() - 1.0);
|
|
678
|
+
assert!(
|
|
679
|
+
(lhs - k).abs() < 1e-9,
|
|
680
|
+
"t={t} does not solve k={k} (got {lhs})"
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/// Regression test for elliptical corners where the vertical radius exceeds
|
|
685
|
+
/// the horizontal one (`ry > rx`). `kurbo::Ellipse::radii()` canonicalises
|
|
686
|
+
/// such an ellipse by swapping its axes and adding a `π/2` rotation; when
|
|
687
|
+
/// that rotation was dropped the corner arcs were drawn transposed, skewing
|
|
688
|
+
/// the whole box. The straight portions of each edge must stay axis aligned:
|
|
689
|
+
/// the top/bottom edges horizontal and the left/right edges vertical.
|
|
690
|
+
#[test]
|
|
691
|
+
fn edges_stay_axis_aligned_for_tall_corners() {
|
|
692
|
+
let b = CssBox::new(
|
|
693
|
+
Rect::new(0.0, 0.0, 400.0, 200.0),
|
|
694
|
+
Insets::uniform(10.0),
|
|
695
|
+
Insets::ZERO,
|
|
696
|
+
0.0,
|
|
697
|
+
NonUniformRoundedRectRadii {
|
|
698
|
+
top_left: Vec2::new(60.0, 20.0),
|
|
699
|
+
top_right: Vec2::new(20.0, 50.0), // ry > rx
|
|
700
|
+
bottom_right: Vec2::new(50.0, 10.0),
|
|
701
|
+
bottom_left: Vec2::new(30.0, 40.0), // ry > rx
|
|
702
|
+
},
|
|
703
|
+
);
|
|
704
|
+
|
|
705
|
+
// The outer border box corner y (top) / x (right) etc. that the straight
|
|
706
|
+
// part of each edge should run along.
|
|
707
|
+
let checks = [
|
|
708
|
+
(Edge::Top, 0.0), // outer top edge at y == 0
|
|
709
|
+
(Edge::Bottom, 200.0), // outer bottom edge at y == 200
|
|
710
|
+
(Edge::Left, 0.0), // outer left edge at x == 0
|
|
711
|
+
(Edge::Right, 400.0), // outer right edge at x == 400
|
|
712
|
+
];
|
|
713
|
+
// Collect every point (endpoints and Bézier control points) of a path.
|
|
714
|
+
// A cubic Bézier lies within the convex hull of its control points, so
|
|
715
|
+
// checking these is enough to prove the whole path stays in the box.
|
|
716
|
+
let points = |path: &BezPath| -> Vec<Point> {
|
|
717
|
+
path.elements()
|
|
718
|
+
.iter()
|
|
719
|
+
.flat_map(|el| match *el {
|
|
720
|
+
PathEl::MoveTo(p) | PathEl::LineTo(p) => vec![p],
|
|
721
|
+
PathEl::QuadTo(a, b) => vec![a, b],
|
|
722
|
+
PathEl::CurveTo(a, b, c) => vec![a, b, c],
|
|
723
|
+
PathEl::ClosePath => vec![],
|
|
724
|
+
})
|
|
725
|
+
.collect()
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
for (edge, expected) in checks {
|
|
729
|
+
let path = b.border_edge_shape(edge);
|
|
730
|
+
let pts = points(&path);
|
|
731
|
+
// The transposed-axis bug pushed points well outside the border box.
|
|
732
|
+
for p in &pts {
|
|
733
|
+
assert!(
|
|
734
|
+
(-0.01..=400.01).contains(&p.x) && (-0.01..=200.01).contains(&p.y),
|
|
735
|
+
"{edge:?}: point {p:?} escaped the border box"
|
|
736
|
+
);
|
|
737
|
+
}
|
|
738
|
+
// And the outer straight run must actually reach the box edge.
|
|
739
|
+
let reaches = pts.iter().any(|p| match edge {
|
|
740
|
+
Edge::Top | Edge::Bottom => (p.y - expected).abs() < 0.01,
|
|
741
|
+
Edge::Left | Edge::Right => (p.x - expected).abs() < 0.01,
|
|
742
|
+
});
|
|
743
|
+
assert!(
|
|
744
|
+
reaches,
|
|
745
|
+
"{edge:?}: no point reached the outer edge {expected}"
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
#[test]
|
|
751
|
+
fn should_solve_properly() {
|
|
752
|
+
// 0.643501
|
|
753
|
+
assert!((start_angle(4.0, 1.0, Vec2 { x: 1.0, y: 2.0 }) - 0.643501).abs() < 1e-5);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/// Regression test: when `k == radii.y / (w * radii.x)` is exactly 2 the
|
|
757
|
+
/// old closed form evaluated `0 / 0` and produced `NaN`, corrupting the
|
|
758
|
+
/// corner arc. This happens for ordinary elliptical corners such as an
|
|
759
|
+
/// 80px/30px radius with 40px/10px border widths (inner/padding ellipse
|
|
760
|
+
/// radii 40/20, widths 30/0 ... => k == 2).
|
|
761
|
+
#[test]
|
|
762
|
+
fn handles_k_equal_two() {
|
|
763
|
+
// k = radii.y / (w * radii.x) = 40 / ((10/40) * 80) = 2.0
|
|
764
|
+
assert_solves(10.0, 40.0, Vec2 { x: 80.0, y: 40.0 });
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
#[test]
|
|
768
|
+
fn solves_a_range_of_elliptical_corners() {
|
|
769
|
+
for &(bt, br) in &[(1.0, 1.0), (1.0, 4.0), (4.0, 1.0), (3.0, 7.0)] {
|
|
770
|
+
for &(rx, ry) in &[(80.0, 30.0), (30.0, 80.0), (60.0, 60.0), (120.0, 20.0)] {
|
|
771
|
+
assert_solves(bt, br, Vec2 { x: rx, y: ry });
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
#[test]
|
|
778
|
+
fn detects_elliptical_border() {
|
|
779
|
+
let corners = |x: f64, y: f64| NonUniformRoundedRectRadii {
|
|
780
|
+
top_left: Vec2::new(x, y),
|
|
781
|
+
top_right: Vec2::new(x, y),
|
|
782
|
+
bottom_right: Vec2::new(x, y),
|
|
783
|
+
bottom_left: Vec2::new(x, y),
|
|
784
|
+
};
|
|
785
|
+
let css_box = |w: f64, h: f64, radii: NonUniformRoundedRectRadii| {
|
|
786
|
+
CssBox::new(
|
|
787
|
+
Rect::new(0.0, 0.0, w, h),
|
|
788
|
+
Insets::uniform(1.0),
|
|
789
|
+
Insets::ZERO,
|
|
790
|
+
0.0,
|
|
791
|
+
radii,
|
|
792
|
+
)
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
// Circle: square box, every radius == half the side.
|
|
796
|
+
assert!(css_box(44.0, 44.0, corners(22.0, 22.0)).is_elliptical_border());
|
|
797
|
+
// Ellipse: non-square box, radii == half each axis.
|
|
798
|
+
assert!(css_box(120.0, 64.0, corners(60.0, 32.0)).is_elliptical_border());
|
|
799
|
+
// Rounded rectangle: radius smaller than half → has straight edges.
|
|
800
|
+
assert!(!css_box(44.0, 44.0, corners(10.0, 10.0)).is_elliptical_border());
|
|
801
|
+
// Sharp rectangle: no rounding.
|
|
802
|
+
assert!(!css_box(44.0, 44.0, corners(0.0, 0.0)).is_elliptical_border());
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
#[test]
|
|
806
|
+
fn detects_uniform_corner_border() {
|
|
807
|
+
let corners = |x: f64, y: f64| NonUniformRoundedRectRadii {
|
|
808
|
+
top_left: Vec2::new(x, y),
|
|
809
|
+
top_right: Vec2::new(x, y),
|
|
810
|
+
bottom_right: Vec2::new(x, y),
|
|
811
|
+
bottom_left: Vec2::new(x, y),
|
|
812
|
+
};
|
|
813
|
+
let css_box = |w: f64, h: f64, radii: NonUniformRoundedRectRadii| {
|
|
814
|
+
CssBox::new(
|
|
815
|
+
Rect::new(0.0, 0.0, w, h),
|
|
816
|
+
Insets::uniform(1.0),
|
|
817
|
+
Insets::ZERO,
|
|
818
|
+
0.0,
|
|
819
|
+
radii,
|
|
820
|
+
)
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
// Sharp rectangle: no rounding, still trivially "uniform" (0 == 0).
|
|
824
|
+
assert!(css_box(44.0, 44.0, corners(0.0, 0.0)).is_uniform_corner_border());
|
|
825
|
+
// Ordinary rounded rectangle: each corner is a circular arc.
|
|
826
|
+
assert!(css_box(44.0, 44.0, corners(10.0, 10.0)).is_uniform_corner_border());
|
|
827
|
+
// Circle: square box, every radius == half the side.
|
|
828
|
+
assert!(css_box(44.0, 44.0, corners(22.0, 22.0)).is_uniform_corner_border());
|
|
829
|
+
// Ellipse: non-square box, radii == half each axis → corners aren't circular.
|
|
830
|
+
assert!(!css_box(120.0, 64.0, corners(60.0, 32.0)).is_uniform_corner_border());
|
|
831
|
+
// Per-corner elliptical radius (rx != ry) anywhere disqualifies the box, even
|
|
832
|
+
// if it isn't a full border-radius: 50% ellipse.
|
|
833
|
+
let mixed = NonUniformRoundedRectRadii {
|
|
834
|
+
top_left: Vec2::new(10.0, 5.0),
|
|
835
|
+
..corners(10.0, 10.0)
|
|
836
|
+
};
|
|
837
|
+
assert!(!css_box(44.0, 44.0, mixed).is_uniform_corner_border());
|
|
838
|
+
|
|
839
|
+
// Corners may differ in radius from each other, as long as each is circular.
|
|
840
|
+
let differing = NonUniformRoundedRectRadii {
|
|
841
|
+
top_left: Vec2::new(10.0, 10.0),
|
|
842
|
+
top_right: Vec2::new(5.0, 5.0),
|
|
843
|
+
bottom_right: Vec2::new(8.0, 8.0),
|
|
844
|
+
bottom_left: Vec2::new(2.0, 2.0),
|
|
845
|
+
};
|
|
846
|
+
assert!(css_box(44.0, 44.0, differing).is_uniform_corner_border());
|
|
847
|
+
}
|