1 | // Copyright 2013 The Servo Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution. |
3 | // |
4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
5 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
7 | // option. This file may not be copied, modified, or distributed |
8 | // except according to those terms. |
9 | |
10 | //! A group of side offsets, which correspond to top/left/bottom/right for borders, padding, |
11 | //! and margins in CSS. |
12 | |
13 | use crate::length::Length; |
14 | use crate::num::Zero; |
15 | use crate::scale::Scale; |
16 | use crate::Vector2D; |
17 | |
18 | use core::cmp::{Eq, PartialEq}; |
19 | use core::fmt; |
20 | use core::hash::Hash; |
21 | use core::marker::PhantomData; |
22 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; |
23 | |
24 | #[cfg (feature = "bytemuck" )] |
25 | use bytemuck::{Pod, Zeroable}; |
26 | #[cfg (feature = "serde" )] |
27 | use serde::{Deserialize, Serialize}; |
28 | |
29 | /// A group of 2D side offsets, which correspond to top/right/bottom/left for borders, padding, |
30 | /// and margins in CSS, optionally tagged with a unit. |
31 | #[repr (C)] |
32 | #[cfg_attr (feature = "serde" , derive(Serialize, Deserialize))] |
33 | #[cfg_attr ( |
34 | feature = "serde" , |
35 | serde(bound(serialize = "T: Serialize" , deserialize = "T: Deserialize<'de>" )) |
36 | )] |
37 | pub struct SideOffsets2D<T, U> { |
38 | pub top: T, |
39 | pub right: T, |
40 | pub bottom: T, |
41 | pub left: T, |
42 | #[doc (hidden)] |
43 | pub _unit: PhantomData<U>, |
44 | } |
45 | |
46 | #[cfg (feature = "arbitrary" )] |
47 | impl<'a, T, U> arbitrary::Arbitrary<'a> for SideOffsets2D<T, U> |
48 | where |
49 | T: arbitrary::Arbitrary<'a>, |
50 | { |
51 | fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> { |
52 | let (top, right, bottom, left) = arbitrary::Arbitrary::arbitrary(u)?; |
53 | Ok(SideOffsets2D { |
54 | top, |
55 | right, |
56 | bottom, |
57 | left, |
58 | _unit: PhantomData, |
59 | }) |
60 | } |
61 | } |
62 | |
63 | #[cfg (feature = "bytemuck" )] |
64 | unsafe impl<T: Zeroable, U> Zeroable for SideOffsets2D<T, U> {} |
65 | |
66 | #[cfg (feature = "bytemuck" )] |
67 | unsafe impl<T: Pod, U: 'static> Pod for SideOffsets2D<T, U> {} |
68 | |
69 | impl<T: Copy, U> Copy for SideOffsets2D<T, U> {} |
70 | |
71 | impl<T: Clone, U> Clone for SideOffsets2D<T, U> { |
72 | fn clone(&self) -> Self { |
73 | SideOffsets2D { |
74 | top: self.top.clone(), |
75 | right: self.right.clone(), |
76 | bottom: self.bottom.clone(), |
77 | left: self.left.clone(), |
78 | _unit: PhantomData, |
79 | } |
80 | } |
81 | } |
82 | |
83 | impl<T, U> Eq for SideOffsets2D<T, U> where T: Eq {} |
84 | |
85 | impl<T, U> PartialEq for SideOffsets2D<T, U> |
86 | where |
87 | T: PartialEq, |
88 | { |
89 | fn eq(&self, other: &Self) -> bool { |
90 | self.top == other.top |
91 | && self.right == other.right |
92 | && self.bottom == other.bottom |
93 | && self.left == other.left |
94 | } |
95 | } |
96 | |
97 | impl<T, U> Hash for SideOffsets2D<T, U> |
98 | where |
99 | T: Hash, |
100 | { |
101 | fn hash<H: core::hash::Hasher>(&self, h: &mut H) { |
102 | self.top.hash(state:h); |
103 | self.right.hash(state:h); |
104 | self.bottom.hash(state:h); |
105 | self.left.hash(state:h); |
106 | } |
107 | } |
108 | |
109 | impl<T: fmt::Debug, U> fmt::Debug for SideOffsets2D<T, U> { |
110 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
111 | write!( |
112 | f, |
113 | "( {:?}, {:?}, {:?}, {:?})" , |
114 | self.top, self.right, self.bottom, self.left |
115 | ) |
116 | } |
117 | } |
118 | |
119 | impl<T: Default, U> Default for SideOffsets2D<T, U> { |
120 | fn default() -> Self { |
121 | SideOffsets2D { |
122 | top: Default::default(), |
123 | right: Default::default(), |
124 | bottom: Default::default(), |
125 | left: Default::default(), |
126 | _unit: PhantomData, |
127 | } |
128 | } |
129 | } |
130 | |
131 | impl<T, U> SideOffsets2D<T, U> { |
132 | /// Constructor taking a scalar for each side. |
133 | /// |
134 | /// Sides are specified in top-right-bottom-left order following |
135 | /// CSS's convention. |
136 | pub const fn new(top: T, right: T, bottom: T, left: T) -> Self { |
137 | SideOffsets2D { |
138 | top, |
139 | right, |
140 | bottom, |
141 | left, |
142 | _unit: PhantomData, |
143 | } |
144 | } |
145 | |
146 | /// Constructor taking a typed Length for each side. |
147 | /// |
148 | /// Sides are specified in top-right-bottom-left order following |
149 | /// CSS's convention. |
150 | pub fn from_lengths( |
151 | top: Length<T, U>, |
152 | right: Length<T, U>, |
153 | bottom: Length<T, U>, |
154 | left: Length<T, U>, |
155 | ) -> Self { |
156 | SideOffsets2D::new(top.0, right.0, bottom.0, left.0) |
157 | } |
158 | |
159 | /// Construct side offsets from min and a max vector offsets. |
160 | /// |
161 | /// The outer rect of the resulting side offsets is equivalent to translating |
162 | /// a rectangle's upper-left corner with the min vector and translating the |
163 | /// bottom-right corner with the max vector. |
164 | pub fn from_vectors_outer(min: Vector2D<T, U>, max: Vector2D<T, U>) -> Self |
165 | where |
166 | T: Neg<Output = T>, |
167 | { |
168 | SideOffsets2D { |
169 | left: -min.x, |
170 | top: -min.y, |
171 | right: max.x, |
172 | bottom: max.y, |
173 | _unit: PhantomData, |
174 | } |
175 | } |
176 | |
177 | /// Construct side offsets from min and a max vector offsets. |
178 | /// |
179 | /// The inner rect of the resulting side offsets is equivalent to translating |
180 | /// a rectangle's upper-left corner with the min vector and translating the |
181 | /// bottom-right corner with the max vector. |
182 | pub fn from_vectors_inner(min: Vector2D<T, U>, max: Vector2D<T, U>) -> Self |
183 | where |
184 | T: Neg<Output = T>, |
185 | { |
186 | SideOffsets2D { |
187 | left: min.x, |
188 | top: min.y, |
189 | right: -max.x, |
190 | bottom: -max.y, |
191 | _unit: PhantomData, |
192 | } |
193 | } |
194 | |
195 | /// Constructor, setting all sides to zero. |
196 | pub fn zero() -> Self |
197 | where |
198 | T: Zero, |
199 | { |
200 | SideOffsets2D::new(Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero()) |
201 | } |
202 | |
203 | /// Returns `true` if all side offsets are zero. |
204 | pub fn is_zero(&self) -> bool |
205 | where |
206 | T: Zero + PartialEq, |
207 | { |
208 | let zero = T::zero(); |
209 | self.top == zero && self.right == zero && self.bottom == zero && self.left == zero |
210 | } |
211 | |
212 | /// Constructor setting the same value to all sides, taking a scalar value directly. |
213 | pub fn new_all_same(all: T) -> Self |
214 | where |
215 | T: Copy, |
216 | { |
217 | SideOffsets2D::new(all, all, all, all) |
218 | } |
219 | |
220 | /// Constructor setting the same value to all sides, taking a typed Length. |
221 | pub fn from_length_all_same(all: Length<T, U>) -> Self |
222 | where |
223 | T: Copy, |
224 | { |
225 | SideOffsets2D::new_all_same(all.0) |
226 | } |
227 | |
228 | pub fn horizontal(&self) -> T |
229 | where |
230 | T: Copy + Add<T, Output = T>, |
231 | { |
232 | self.left + self.right |
233 | } |
234 | |
235 | pub fn vertical(&self) -> T |
236 | where |
237 | T: Copy + Add<T, Output = T>, |
238 | { |
239 | self.top + self.bottom |
240 | } |
241 | } |
242 | |
243 | impl<T, U> Add for SideOffsets2D<T, U> |
244 | where |
245 | T: Add<T, Output = T>, |
246 | { |
247 | type Output = Self; |
248 | fn add(self, other: Self) -> Self { |
249 | SideOffsets2D::new( |
250 | self.top + other.top, |
251 | self.right + other.right, |
252 | self.bottom + other.bottom, |
253 | self.left + other.left, |
254 | ) |
255 | } |
256 | } |
257 | |
258 | impl<T, U> AddAssign<Self> for SideOffsets2D<T, U> |
259 | where |
260 | T: AddAssign<T>, |
261 | { |
262 | fn add_assign(&mut self, other: Self) { |
263 | self.top += other.top; |
264 | self.right += other.right; |
265 | self.bottom += other.bottom; |
266 | self.left += other.left; |
267 | } |
268 | } |
269 | |
270 | impl<T, U> Sub for SideOffsets2D<T, U> |
271 | where |
272 | T: Sub<T, Output = T>, |
273 | { |
274 | type Output = Self; |
275 | fn sub(self, other: Self) -> Self { |
276 | SideOffsets2D::new( |
277 | self.top - other.top, |
278 | self.right - other.right, |
279 | self.bottom - other.bottom, |
280 | self.left - other.left, |
281 | ) |
282 | } |
283 | } |
284 | |
285 | impl<T, U> SubAssign<Self> for SideOffsets2D<T, U> |
286 | where |
287 | T: SubAssign<T>, |
288 | { |
289 | fn sub_assign(&mut self, other: Self) { |
290 | self.top -= other.top; |
291 | self.right -= other.right; |
292 | self.bottom -= other.bottom; |
293 | self.left -= other.left; |
294 | } |
295 | } |
296 | |
297 | impl<T, U> Neg for SideOffsets2D<T, U> |
298 | where |
299 | T: Neg<Output = T>, |
300 | { |
301 | type Output = Self; |
302 | fn neg(self) -> Self { |
303 | SideOffsets2D { |
304 | top: -self.top, |
305 | right: -self.right, |
306 | bottom: -self.bottom, |
307 | left: -self.left, |
308 | _unit: PhantomData, |
309 | } |
310 | } |
311 | } |
312 | |
313 | impl<T: Copy + Mul, U> Mul<T> for SideOffsets2D<T, U> { |
314 | type Output = SideOffsets2D<T::Output, U>; |
315 | |
316 | #[inline ] |
317 | fn mul(self, scale: T) -> Self::Output { |
318 | SideOffsets2D::new( |
319 | self.top * scale, |
320 | self.right * scale, |
321 | self.bottom * scale, |
322 | self.left * scale, |
323 | ) |
324 | } |
325 | } |
326 | |
327 | impl<T: Copy + MulAssign, U> MulAssign<T> for SideOffsets2D<T, U> { |
328 | #[inline ] |
329 | fn mul_assign(&mut self, other: T) { |
330 | self.top *= other; |
331 | self.right *= other; |
332 | self.bottom *= other; |
333 | self.left *= other; |
334 | } |
335 | } |
336 | |
337 | impl<T: Copy + Mul, U1, U2> Mul<Scale<T, U1, U2>> for SideOffsets2D<T, U1> { |
338 | type Output = SideOffsets2D<T::Output, U2>; |
339 | |
340 | #[inline ] |
341 | fn mul(self, scale: Scale<T, U1, U2>) -> Self::Output { |
342 | SideOffsets2D::new( |
343 | self.top * scale.0, |
344 | self.right * scale.0, |
345 | self.bottom * scale.0, |
346 | self.left * scale.0, |
347 | ) |
348 | } |
349 | } |
350 | |
351 | impl<T: Copy + MulAssign, U> MulAssign<Scale<T, U, U>> for SideOffsets2D<T, U> { |
352 | #[inline ] |
353 | fn mul_assign(&mut self, other: Scale<T, U, U>) { |
354 | *self *= other.0; |
355 | } |
356 | } |
357 | |
358 | impl<T: Copy + Div, U> Div<T> for SideOffsets2D<T, U> { |
359 | type Output = SideOffsets2D<T::Output, U>; |
360 | |
361 | #[inline ] |
362 | fn div(self, scale: T) -> Self::Output { |
363 | SideOffsets2D::new( |
364 | self.top / scale, |
365 | self.right / scale, |
366 | self.bottom / scale, |
367 | self.left / scale, |
368 | ) |
369 | } |
370 | } |
371 | |
372 | impl<T: Copy + DivAssign, U> DivAssign<T> for SideOffsets2D<T, U> { |
373 | #[inline ] |
374 | fn div_assign(&mut self, other: T) { |
375 | self.top /= other; |
376 | self.right /= other; |
377 | self.bottom /= other; |
378 | self.left /= other; |
379 | } |
380 | } |
381 | |
382 | impl<T: Copy + Div, U1, U2> Div<Scale<T, U1, U2>> for SideOffsets2D<T, U2> { |
383 | type Output = SideOffsets2D<T::Output, U1>; |
384 | |
385 | #[inline ] |
386 | fn div(self, scale: Scale<T, U1, U2>) -> Self::Output { |
387 | SideOffsets2D::new( |
388 | self.top / scale.0, |
389 | self.right / scale.0, |
390 | self.bottom / scale.0, |
391 | self.left / scale.0, |
392 | ) |
393 | } |
394 | } |
395 | |
396 | impl<T: Copy + DivAssign, U> DivAssign<Scale<T, U, U>> for SideOffsets2D<T, U> { |
397 | fn div_assign(&mut self, other: Scale<T, U, U>) { |
398 | *self /= other.0; |
399 | } |
400 | } |
401 | |
402 | #[test ] |
403 | fn from_vectors() { |
404 | use crate::{point2, vec2}; |
405 | type Box2D = crate::default::Box2D<i32>; |
406 | |
407 | let b = Box2D { |
408 | min: point2(10, 10), |
409 | max: point2(20, 20), |
410 | }; |
411 | |
412 | let outer = b.outer_box(SideOffsets2D::from_vectors_outer(vec2(-1, -2), vec2(3, 4))); |
413 | let inner = b.inner_box(SideOffsets2D::from_vectors_inner(vec2(1, 2), vec2(-3, -4))); |
414 | |
415 | assert_eq!( |
416 | outer, |
417 | Box2D { |
418 | min: point2(9, 8), |
419 | max: point2(23, 24) |
420 | } |
421 | ); |
422 | assert_eq!( |
423 | inner, |
424 | Box2D { |
425 | min: point2(11, 12), |
426 | max: point2(17, 16) |
427 | } |
428 | ); |
429 | } |
430 | |
431 | #[test ] |
432 | fn test_is_zero() { |
433 | let s1: SideOffsets2D<f32, ()> = SideOffsets2D::new_all_same(0.0); |
434 | assert!(s1.is_zero()); |
435 | |
436 | let s2: SideOffsets2D<f32, ()> = SideOffsets2D::new(1.0, 2.0, 3.0, 4.0); |
437 | assert!(!s2.is_zero()); |
438 | } |
439 | |
440 | #[cfg (test)] |
441 | mod ops { |
442 | use crate::Scale; |
443 | |
444 | pub enum Mm {} |
445 | pub enum Cm {} |
446 | |
447 | type SideOffsets2D<T> = crate::default::SideOffsets2D<T>; |
448 | type SideOffsets2DMm<T> = crate::SideOffsets2D<T, Mm>; |
449 | type SideOffsets2DCm<T> = crate::SideOffsets2D<T, Cm>; |
450 | |
451 | #[test ] |
452 | fn test_mul_scalar() { |
453 | let s = SideOffsets2D::new(1.0, 2.0, 3.0, 4.0); |
454 | |
455 | let result = s * 3.0; |
456 | |
457 | assert_eq!(result, SideOffsets2D::new(3.0, 6.0, 9.0, 12.0)); |
458 | } |
459 | |
460 | #[test ] |
461 | fn test_mul_assign_scalar() { |
462 | let mut s = SideOffsets2D::new(1.0, 2.0, 3.0, 4.0); |
463 | |
464 | s *= 2.0; |
465 | |
466 | assert_eq!(s, SideOffsets2D::new(2.0, 4.0, 6.0, 8.0)); |
467 | } |
468 | |
469 | #[test ] |
470 | fn test_mul_scale() { |
471 | let s = SideOffsets2DMm::new(0.0, 1.0, 3.0, 2.0); |
472 | let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1); |
473 | |
474 | let result = s * cm_per_mm; |
475 | |
476 | assert_eq!(result, SideOffsets2DCm::new(0.0, 0.1, 0.3, 0.2)); |
477 | } |
478 | |
479 | #[test ] |
480 | fn test_mul_assign_scale() { |
481 | let mut s = SideOffsets2DMm::new(2.0, 4.0, 6.0, 8.0); |
482 | let scale: Scale<f32, Mm, Mm> = Scale::new(0.1); |
483 | |
484 | s *= scale; |
485 | |
486 | assert_eq!(s, SideOffsets2DMm::new(0.2, 0.4, 0.6, 0.8)); |
487 | } |
488 | |
489 | #[test ] |
490 | fn test_div_scalar() { |
491 | let s = SideOffsets2D::new(10.0, 20.0, 30.0, 40.0); |
492 | |
493 | let result = s / 10.0; |
494 | |
495 | assert_eq!(result, SideOffsets2D::new(1.0, 2.0, 3.0, 4.0)); |
496 | } |
497 | |
498 | #[test ] |
499 | fn test_div_assign_scalar() { |
500 | let mut s = SideOffsets2D::new(10.0, 20.0, 30.0, 40.0); |
501 | |
502 | s /= 10.0; |
503 | |
504 | assert_eq!(s, SideOffsets2D::new(1.0, 2.0, 3.0, 4.0)); |
505 | } |
506 | |
507 | #[test ] |
508 | fn test_div_scale() { |
509 | let s = SideOffsets2DCm::new(0.1, 0.2, 0.3, 0.4); |
510 | let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1); |
511 | |
512 | let result = s / cm_per_mm; |
513 | |
514 | assert_eq!(result, SideOffsets2DMm::new(1.0, 2.0, 3.0, 4.0)); |
515 | } |
516 | |
517 | #[test ] |
518 | fn test_div_assign_scale() { |
519 | let mut s = SideOffsets2DMm::new(0.1, 0.2, 0.3, 0.4); |
520 | let scale: Scale<f32, Mm, Mm> = Scale::new(0.1); |
521 | |
522 | s /= scale; |
523 | |
524 | assert_eq!(s, SideOffsets2DMm::new(1.0, 2.0, 3.0, 4.0)); |
525 | } |
526 | } |
527 | |