1pub use app_units::Au;
16use euclid::{Length, Rect, Scale, Size2D, Transform3D, Translation2D};
17use euclid::{Point2D, Point3D, Vector2D, Vector3D, SideOffsets2D, Box2D};
18use euclid::HomogeneousVector;
19use peek_poke::PeekPoke;
20use crate::image::DirtyRect;
22
23#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
26pub struct DevicePixel;
27
28pub type DeviceIntRect = Box2D<i32, DevicePixel>;
29pub type DeviceIntPoint = Point2D<i32, DevicePixel>;
30pub type DeviceIntSize = Size2D<i32, DevicePixel>;
31pub type DeviceIntLength = Length<i32, DevicePixel>;
32pub type DeviceIntSideOffsets = SideOffsets2D<i32, DevicePixel>;
33pub type DeviceIntVector2D = Vector2D<i32, DevicePixel>;
34
35pub type DeviceRect = Box2D<f32, DevicePixel>;
36pub type DeviceBox2D = Box2D<f32, DevicePixel>;
37pub type DevicePoint = Point2D<f32, DevicePixel>;
38pub type DeviceVector2D = Vector2D<f32, DevicePixel>;
39pub type DeviceSize = Size2D<f32, DevicePixel>;
40pub type DeviceHomogeneousVector = HomogeneousVector<f32, DevicePixel>;
41
42#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
45pub struct FramebufferPixel;
46
47pub type FramebufferIntPoint = Point2D<i32, FramebufferPixel>;
48pub type FramebufferIntSize = Size2D<i32, FramebufferPixel>;
49pub type FramebufferIntRect = Box2D<i32, FramebufferPixel>;
50
51#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
53pub struct PicturePixel;
54
55pub type PictureIntRect = Box2D<i32, PicturePixel>;
56pub type PictureIntPoint = Point2D<i32, PicturePixel>;
57pub type PictureIntSize = Size2D<i32, PicturePixel>;
58pub type PictureRect = Box2D<f32, PicturePixel>;
59pub type PicturePoint = Point2D<f32, PicturePixel>;
60pub type PictureSize = Size2D<f32, PicturePixel>;
61pub type PicturePoint3D = Point3D<f32, PicturePixel>;
62pub type PictureVector2D = Vector2D<f32, PicturePixel>;
63pub type PictureVector3D = Vector3D<f32, PicturePixel>;
64pub type PictureBox2D = Box2D<f32, PicturePixel>;
65
66#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
70pub struct RasterPixel;
71
72pub type RasterIntRect = Box2D<i32, RasterPixel>;
73pub type RasterIntPoint = Point2D<i32, RasterPixel>;
74pub type RasterIntSize = Size2D<i32, RasterPixel>;
75pub type RasterRect = Box2D<f32, RasterPixel>;
76pub type RasterPoint = Point2D<f32, RasterPixel>;
77pub type RasterSize = Size2D<f32, RasterPixel>;
78pub type RasterPoint3D = Point3D<f32, RasterPixel>;
79pub type RasterVector2D = Vector2D<f32, RasterPixel>;
80pub type RasterVector3D = Vector3D<f32, RasterPixel>;
81
82#[derive(Hash, Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, Ord, PartialOrd, Deserialize, Serialize, PeekPoke)]
84pub struct LayoutPixel;
85
86pub type LayoutRect = Box2D<f32, LayoutPixel>;
87pub type LayoutPoint = Point2D<f32, LayoutPixel>;
88pub type LayoutPoint3D = Point3D<f32, LayoutPixel>;
89pub type LayoutVector2D = Vector2D<f32, LayoutPixel>;
90pub type LayoutVector3D = Vector3D<f32, LayoutPixel>;
91pub type LayoutSize = Size2D<f32, LayoutPixel>;
92pub type LayoutSideOffsets = SideOffsets2D<f32, LayoutPixel>;
93
94pub type LayoutIntRect = Box2D<i32, LayoutPixel>;
95pub type LayoutIntPoint = Point2D<i32, LayoutPixel>;
96pub type LayoutIntSize = Size2D<i32, LayoutPixel>;
97
98#[derive(Hash, Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
100pub struct WorldPixel;
101
102pub type WorldRect = Box2D<f32, WorldPixel>;
103pub type WorldIntRect = Box2D<i32, WorldPixel>;
104pub type WorldPoint = Point2D<f32, WorldPixel>;
105pub type WorldSize = Size2D<f32, WorldPixel>;
106pub type WorldPoint3D = Point3D<f32, WorldPixel>;
107pub type WorldVector2D = Vector2D<f32, WorldPixel>;
108pub type WorldVector3D = Vector3D<f32, WorldPixel>;
109
110#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
112pub struct Tiles;
113pub type TileOffset = Point2D<i32, Tiles>;
114pub type TileRange = Box2D<i32, Tiles>;
115
116pub type DevicePixelScale = Scale<f32, WorldPixel, DevicePixel>;
118pub type LayoutToWorldScale = Scale<f32, LayoutPixel, WorldPixel>;
121pub type LayoutToDeviceScale = Scale<f32, LayoutPixel, DevicePixel>;
123
124pub type LayoutTransform = Transform3D<f32, LayoutPixel, LayoutPixel>;
125pub type LayoutToWorldTransform = Transform3D<f32, LayoutPixel, WorldPixel>;
126pub type WorldToLayoutTransform = Transform3D<f32, WorldPixel, LayoutPixel>;
127
128pub type LayoutToPictureTransform = Transform3D<f32, LayoutPixel, PicturePixel>;
129pub type PictureToLayoutTransform = Transform3D<f32, PicturePixel, LayoutPixel>;
130
131pub type LayoutToRasterTransform = Transform3D<f32, LayoutPixel, RasterPixel>;
132pub type RasterToLayoutTransform = Transform3D<f32, RasterPixel, LayoutPixel>;
133
134pub type PictureToRasterTransform = Transform3D<f32, PicturePixel, RasterPixel>;
135pub type RasterToPictureTransform = Transform3D<f32, RasterPixel, PicturePixel>;
136
137pub type RasterPixelScale = Scale<f32, PicturePixel, RasterPixel>;
139
140pub type LayoutPointAu = Point2D<Au, LayoutPixel>;
142pub type LayoutRectAu = Box2D<Au, LayoutPixel>;
143pub type LayoutSizeAu = Size2D<Au, LayoutPixel>;
144pub type LayoutVector2DAu = Vector2D<Au, LayoutPixel>;
145pub type LayoutSideOffsetsAu = SideOffsets2D<Au, LayoutPixel>;
146
147pub type ImageDirtyRect = DirtyRect<i32, DevicePixel>;
148pub type BlobDirtyRect = DirtyRect<i32, LayoutPixel>;
149
150pub type BlobToDeviceTranslation = Translation2D<i32, LayoutPixel, DevicePixel>;
151
152#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
158#[repr(C)]
159pub struct TexelRect {
160 pub uv0: DevicePoint,
161 pub uv1: DevicePoint,
162}
163
164impl TexelRect {
165 pub fn new(u0: f32, v0: f32, u1: f32, v1: f32) -> Self {
166 TexelRect {
167 uv0: DevicePoint::new(u0, v0),
168 uv1: DevicePoint::new(u1, v1),
169 }
170 }
171
172 pub fn invalid() -> Self {
173 TexelRect {
174 uv0: DevicePoint::new(-1.0, -1.0),
175 uv1: DevicePoint::new(-1.0, -1.0),
176 }
177 }
178}
179
180impl Into<TexelRect> for DeviceIntRect {
181 fn into(self) -> TexelRect {
182 TexelRect {
183 uv0: self.min.to_f32(),
184 uv1: self.max.to_f32(),
185 }
186 }
187}
188
189const MAX_AU_FLOAT: f32 = 1.0e6;
190
191pub trait AuHelpers<T> {
192 fn from_au(data: T) -> Self;
193 fn to_au(&self) -> T;
194}
195
196impl AuHelpers<LayoutSizeAu> for LayoutSize {
197 fn from_au(size: LayoutSizeAu) -> Self {
198 LayoutSize::new(
199 size.width.to_f32_px(),
200 size.height.to_f32_px(),
201 )
202 }
203
204 fn to_au(&self) -> LayoutSizeAu {
205 let width = self.width.min(2.0 * MAX_AU_FLOAT);
206 let height = self.height.min(2.0 * MAX_AU_FLOAT);
207
208 LayoutSizeAu::new(
209 Au::from_f32_px(width),
210 Au::from_f32_px(height),
211 )
212 }
213}
214
215impl AuHelpers<LayoutVector2DAu> for LayoutVector2D {
216 fn from_au(size: LayoutVector2DAu) -> Self {
217 LayoutVector2D::new(
218 size.x.to_f32_px(),
219 size.y.to_f32_px(),
220 )
221 }
222
223 fn to_au(&self) -> LayoutVector2DAu {
224 LayoutVector2DAu::new(
225 Au::from_f32_px(self.x),
226 Au::from_f32_px(self.y),
227 )
228 }
229}
230
231impl AuHelpers<LayoutPointAu> for LayoutPoint {
232 fn from_au(point: LayoutPointAu) -> Self {
233 LayoutPoint::new(
234 point.x.to_f32_px(),
235 point.y.to_f32_px(),
236 )
237 }
238
239 fn to_au(&self) -> LayoutPointAu {
240 let x = self.x.min(MAX_AU_FLOAT).max(-MAX_AU_FLOAT);
241 let y = self.y.min(MAX_AU_FLOAT).max(-MAX_AU_FLOAT);
242
243 LayoutPointAu::new(
244 Au::from_f32_px(x),
245 Au::from_f32_px(y),
246 )
247 }
248}
249
250impl AuHelpers<LayoutRectAu> for LayoutRect {
251 fn from_au(rect: LayoutRectAu) -> Self {
252 LayoutRect {
253 min: LayoutPoint::from_au(rect.min),
254 max: LayoutPoint::from_au(rect.max),
255 }
256 }
257
258 fn to_au(&self) -> LayoutRectAu {
259 LayoutRectAu {
260 min: self.min.to_au(),
261 max: self.max.to_au(),
262 }
263 }
264}
265
266impl AuHelpers<LayoutSideOffsetsAu> for LayoutSideOffsets {
267 fn from_au(offsets: LayoutSideOffsetsAu) -> Self {
268 LayoutSideOffsets::new(
269 offsets.top.to_f32_px(),
270 offsets.right.to_f32_px(),
271 offsets.bottom.to_f32_px(),
272 offsets.left.to_f32_px(),
273 )
274 }
275
276 fn to_au(&self) -> LayoutSideOffsetsAu {
277 LayoutSideOffsetsAu::new(
278 Au::from_f32_px(self.top),
279 Au::from_f32_px(self.right),
280 Au::from_f32_px(self.bottom),
281 Au::from_f32_px(self.left),
282 )
283 }
284}
285
286pub trait RectExt {
287 type Point;
288 fn top_left(&self) -> Self::Point;
289 fn top_right(&self) -> Self::Point;
290 fn bottom_left(&self) -> Self::Point;
291 fn bottom_right(&self) -> Self::Point;
292}
293
294impl<U> RectExt for Rect<f32, U> {
295 type Point = Point2D<f32, U>;
296 fn top_left(&self) -> Self::Point {
297 self.min()
298 }
299 fn top_right(&self) -> Self::Point {
300 Point2D::new(self.max_x(), self.min_y())
301 }
302 fn bottom_left(&self) -> Self::Point {
303 Point2D::new(self.min_x(), self.max_y())
304 }
305 fn bottom_right(&self) -> Self::Point {
306 self.max()
307 }
308}
309
310impl<U> RectExt for Box2D<f32, U> {
311 type Point = Point2D<f32, U>;
312 fn top_left(&self) -> Self::Point {
313 self.min
314 }
315 fn top_right(&self) -> Self::Point {
316 Point2D::new(self.max.x, self.min.y)
317 }
318 fn bottom_left(&self) -> Self::Point {
319 Point2D::new(self.min.x, self.max.y)
320 }
321 fn bottom_right(&self) -> Self::Point {
322 self.max
323 }
324}
325
326#[inline]
329pub fn layout_rect_as_picture_rect(layout_rect: &LayoutRect) -> PictureRect {
330 layout_rect.cast_unit()
331}
332
333#[inline]
334pub fn layout_vector_as_picture_vector(layout_vector: LayoutVector2D) -> PictureVector2D {
335 layout_vector.cast_unit()
336}
337
338#[inline]
339pub fn device_size_as_framebuffer_size(framebuffer_size: DeviceIntSize) -> FramebufferIntSize {
340 framebuffer_size.cast_unit()
341}
342
343#[inline]
344pub fn device_rect_as_framebuffer_rect(framebuffer_rect: &DeviceIntRect) -> FramebufferIntRect {
345 framebuffer_rect.cast_unit()
346}