1use std::f32;
6
7use app_units::{Au, MAX_AU, MIN_AU};
8use euclid::default::{Point2D as UntypedPoint2D, Rect as UntypedRect, Size2D as UntypedSize2D};
9use euclid::{Box2D, Length, Point2D, Scale, SideOffsets2D, Size2D, Vector2D};
10use malloc_size_of_derive::MallocSizeOf;
11use webrender::FastTransform;
12use webrender_api::units::{
13 DeviceIntRect, DeviceIntSize, DevicePixel, FramebufferPixel, LayoutPixel, LayoutPoint,
14 LayoutRect, LayoutSize,
15};
16
17pub type FramebufferUintLength = Length<u32, FramebufferPixel>;
20
21#[derive(Clone, Copy, Debug, MallocSizeOf)]
35pub enum DeviceIndependentPixel {}
36
37pub type DeviceIndependentIntRect = Box2D<i32, DeviceIndependentPixel>;
38pub type DeviceIndependentIntPoint = Point2D<i32, DeviceIndependentPixel>;
39pub type DeviceIndependentIntSize = Size2D<i32, DeviceIndependentPixel>;
40pub type DeviceIndependentIntLength = Length<i32, DeviceIndependentPixel>;
41pub type DeviceIndependentIntSideOffsets = SideOffsets2D<i32, DeviceIndependentPixel>;
42pub type DeviceIndependentIntVector2D = Vector2D<i32, DeviceIndependentPixel>;
43
44pub type DeviceIndependentRect = Box2D<f32, DeviceIndependentPixel>;
45pub type DeviceIndependentBox2D = Box2D<f32, DeviceIndependentPixel>;
46pub type DeviceIndependentPoint = Point2D<f32, DeviceIndependentPixel>;
47pub type DeviceIndependentVector2D = Vector2D<f32, DeviceIndependentPixel>;
48pub type DeviceIndependentSize = Size2D<f32, DeviceIndependentPixel>;
49
50pub type FastLayoutTransform = FastTransform<LayoutPixel, LayoutPixel>;
51
52pub trait MaxRect {
57 fn max_rect() -> Self;
58}
59
60pub fn convert_rect_to_css_pixel(
62 rect: DeviceIntRect,
63 scale: Scale<f32, DeviceIndependentPixel, DevicePixel>,
64) -> DeviceIndependentIntRect {
65 (rect.to_f32() / scale).round().to_i32()
66}
67
68pub fn convert_size_to_css_pixel(
70 size: DeviceIntSize,
71 scale: Scale<f32, DeviceIndependentPixel, DevicePixel>,
72) -> DeviceIndependentIntSize {
73 (size.to_f32() / scale).round().to_i32()
74}
75
76impl MaxRect for UntypedRect<Au> {
77 #[inline]
78 fn max_rect() -> Self {
79 Self::new(
80 UntypedPoint2D::new(MIN_AU / 2, MIN_AU / 2),
81 UntypedSize2D::new(MAX_AU, MAX_AU),
82 )
83 }
84}
85
86impl MaxRect for LayoutRect {
87 #[inline]
88 fn max_rect() -> Self {
89 Self::from_origin_and_size(
90 LayoutPoint::new(f32::MIN / 2.0, f32::MIN / 2.0),
91 LayoutSize::new(f32::MAX, f32::MAX),
92 )
93 }
94}
95
96pub fn f32_rect_to_au_rect(rect: UntypedRect<f32>) -> UntypedRect<Au> {
98 UntypedRect::new(
99 Point2D::new(
100 Au::from_f32_px(rect.origin.x),
101 Au::from_f32_px(rect.origin.y),
102 ),
103 Size2D::new(
104 Au::from_f32_px(rect.size.width),
105 Au::from_f32_px(rect.size.height),
106 ),
107 )
108}
109
110pub fn au_rect_to_f32_rect(rect: UntypedRect<Au>) -> UntypedRect<f32> {
112 UntypedRect::new(
113 Point2D::new(rect.origin.x.to_f32_px(), rect.origin.y.to_f32_px()),
114 Size2D::new(rect.size.width.to_f32_px(), rect.size.height.to_f32_px()),
115 )
116}