-
Notifications
You must be signed in to change notification settings - Fork 220
/
display_list.rs
337 lines (300 loc) · 10.8 KB
/
display_list.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use std::fmt;
use azul_css::{
LayoutPoint, LayoutSize, LayoutRect,
StyleBackgroundRepeat, StyleBackgroundPosition, ColorU, BoxShadowClipMode,
LinearGradient, RadialGradient, BoxShadowPreDisplayItem, StyleBackgroundSize,
CssPropertyValue,
StyleBorderTopWidth, StyleBorderRightWidth, StyleBorderBottomWidth, StyleBorderLeftWidth,
StyleBorderTopColor, StyleBorderRightColor, StyleBorderBottomColor, StyleBorderLeftColor,
StyleBorderTopStyle, StyleBorderRightStyle, StyleBorderBottomStyle, StyleBorderLeftStyle,
StyleBorderTopLeftRadius, StyleBorderTopRightRadius, StyleBorderBottomLeftRadius, StyleBorderBottomRightRadius,
};
use crate::{
app_resources::{ImageKey, FontInstanceKey, ImageInfo},
ui_solver::ExternalScrollId,
dom::ScrollTagId,
};
/// A tag that can be used to identify items during hit testing. If the tag
/// is missing then the item doesn't take part in hit testing at all. This
/// is composed of two numbers. In Servo, the first is an identifier while the
/// second is used to select the cursor that should be used during mouse
/// movement. In Gecko, the first is a scrollframe identifier, while the second
/// is used to store various flags that APZ needs to properly process input
/// events.
pub type ItemTag = (u64, u16);
pub type GlyphIndex = u32;
pub type FontInstanceFlags = u32;
// Common flags
pub const FONT_INSTANCE_FLAG_SYNTHETIC_BOLD: u32 = 1 << 1;
pub const FONT_INSTANCE_FLAG_EMBEDDED_BITMAPS: u32 = 1 << 2;
pub const FONT_INSTANCE_FLAG_SUBPIXEL_BGR: u32 = 1 << 3;
pub const FONT_INSTANCE_FLAG_TRANSPOSE: u32 = 1 << 4;
pub const FONT_INSTANCE_FLAG_FLIP_X: u32 = 1 << 5;
pub const FONT_INSTANCE_FLAG_FLIP_Y: u32 = 1 << 6;
pub const FONT_INSTANCE_FLAG_SUBPIXEL_POSITION: u32 = 1 << 7;
// Windows flags
pub const FONT_INSTANCE_FLAG_FORCE_GDI: u32 = 1 << 16;
// Mac flags
pub const FONT_INSTANCE_FLAG_FONT_SMOOTHING: u32 = 1 << 16;
// FreeType flags
pub const FONT_INSTANCE_FLAG_FORCE_AUTOHINT: u32 = 1 << 16;
pub const FONT_INSTANCE_FLAG_NO_AUTOHINT: u32 = 1 << 17;
pub const FONT_INSTANCE_FLAG_VERTICAL_LAYOUT: u32 = 1 << 18;
pub const FONT_INSTANCE_FLAG_LCD_VERTICAL: u32 = 1 << 19;
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct GlyphOptions {
pub render_mode: FontRenderMode,
pub flags: FontInstanceFlags,
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub enum FontRenderMode {
Mono,
Alpha,
Subpixel,
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct GlyphInstance {
pub index: GlyphIndex,
pub point: LayoutPoint,
pub size: LayoutSize,
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct CachedDisplayList {
pub root: DisplayListMsg,
}
impl CachedDisplayList {
pub fn empty(size: LayoutSize) -> Self {
Self { root: DisplayListMsg::Frame(DisplayListFrame::root(size)) }
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum DisplayListMsg {
Frame(DisplayListFrame),
ScrollFrame(DisplayListScrollFrame),
}
impl DisplayListMsg {
pub fn append_child(&mut self, child: Self) {
use self::DisplayListMsg::*;
match self {
Frame(f) => { f.children.push(child); },
ScrollFrame(sf) => { sf.frame.children.push(child); },
}
}
pub fn append_children(&mut self, mut children: Vec<Self>) {
use self::DisplayListMsg::*;
match self {
Frame(f) => { f.children.append(&mut children); },
ScrollFrame(sf) => { sf.frame.children.append(&mut children); },
}
}
pub fn get_size(&self) -> LayoutSize {
use self::DisplayListMsg::*;
match self {
Frame(f) => f.rect.size,
ScrollFrame(sf) => sf.frame.rect.size,
}
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct DisplayListScrollFrame {
/// Bounding rect of the (overflowing) content of the scroll frame
pub content_rect: LayoutRect,
/// The scroll ID is the hash of the DOM node, so that scrolling
/// positions can be tracked across multiple frames
pub scroll_id: ExternalScrollId,
/// The scroll tag is used for hit-testing
pub scroll_tag: ScrollTagId,
/// Content + children of the scroll clip
pub frame: DisplayListFrame,
}
#[derive(Clone, PartialEq, PartialOrd)]
pub struct DisplayListFrame {
pub rect: LayoutRect,
/// Border radius, set to none only if overflow: visible is set!
pub border_radius: StyleBorderRadius,
pub clip_rect: Option<LayoutRect>,
pub tag: Option<ItemTag>,
pub content: Vec<LayoutRectContent>,
pub children: Vec<DisplayListMsg>,
}
impl fmt::Debug for DisplayListFrame {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "rect: {:#?},", self.rect)?;
write!(f, "\r\nborder_radius: {:#?},", self.border_radius)?;
if let Some(clip_rect) = &self.clip_rect {
write!(f, "\r\nclip_rect: {:#?},", clip_rect)?;
}
if let Some(tag) = &self.tag {
write!(f, "\r\ntag: ({}, {}),", tag.0, tag.1)?;
}
if !self.content.is_empty() {
write!(f, "\r\ncontent: {:#?}", self.content)?;
}
if !self.children.is_empty() {
write!(f, "\r\nchildren: {:#?}", self.children)?;
}
Ok(())
}
}
impl DisplayListFrame {
pub fn root(dimensions: LayoutSize) -> Self {
DisplayListFrame {
tag: None,
clip_rect: None,
rect: LayoutRect {
origin: LayoutPoint { x: 0.0, y: 0.0 },
size: dimensions,
},
border_radius: StyleBorderRadius::default(),
content: vec![],
children: vec![],
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ImageRendering {
Auto,
CrispEdges,
Pixelated,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AlphaType {
Alpha,
PremultipliedAlpha,
}
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StyleBorderRadius {
pub top_left: Option<CssPropertyValue<StyleBorderTopLeftRadius>>,
pub top_right: Option<CssPropertyValue<StyleBorderTopRightRadius>>,
pub bottom_left: Option<CssPropertyValue<StyleBorderBottomLeftRadius>>,
pub bottom_right: Option<CssPropertyValue<StyleBorderBottomRightRadius>>,
}
impl fmt::Debug for StyleBorderRadius {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "StyleBorderRadius {{")?;
if let Some(tl) = &self.top_left {
write!(f, "\r\n\ttop-left: {},", tl)?;
}
if let Some(tr) = &self.top_right {
write!(f, "\r\n\ttop-right: {},", tr)?;
}
if let Some(bl) = &self.bottom_left {
write!(f, "\r\n\tbottom-left: {},", bl)?;
}
if let Some(br) = &self.bottom_right {
write!(f, "\r\n\tbottom-right: {},", br)?;
}
write!(f, "\r\n}}")
}
}
macro_rules! tlbr_debug {($struct_name:ident) => (
impl fmt::Debug for $struct_name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {{", stringify!($struct_name))?;
if let Some(t) = &self.top {
write!(f, "\r\n\ttop: {},", t)?;
}
if let Some(r) = &self.right {
write!(f, "\r\n\tright: {},", r)?;
}
if let Some(b) = &self.bottom {
write!(f, "\r\n\tbottom: {},", b)?;
}
if let Some(l) = &self.left {
write!(f, "\r\n\tleft: {},", l)?;
}
write!(f, "\r\n}}")
}
}
)}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StyleBorderWidths {
pub top: Option<CssPropertyValue<StyleBorderTopWidth>>,
pub right: Option<CssPropertyValue<StyleBorderRightWidth>>,
pub bottom: Option<CssPropertyValue<StyleBorderBottomWidth>>,
pub left: Option<CssPropertyValue<StyleBorderLeftWidth>>,
}
tlbr_debug!(StyleBorderWidths);
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StyleBorderColors {
pub top: Option<CssPropertyValue<StyleBorderTopColor>>,
pub right: Option<CssPropertyValue<StyleBorderRightColor>>,
pub bottom: Option<CssPropertyValue<StyleBorderBottomColor>>,
pub left: Option<CssPropertyValue<StyleBorderLeftColor>>,
}
tlbr_debug!(StyleBorderColors);
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StyleBorderStyles {
pub top: Option<CssPropertyValue<StyleBorderTopStyle>>,
pub right: Option<CssPropertyValue<StyleBorderRightStyle>>,
pub bottom: Option<CssPropertyValue<StyleBorderBottomStyle>>,
pub left: Option<CssPropertyValue<StyleBorderLeftStyle>>,
}
tlbr_debug!(StyleBorderStyles);
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StyleBoxShadow {
pub top: Option<CssPropertyValue<BoxShadowPreDisplayItem>>,
pub right: Option<CssPropertyValue<BoxShadowPreDisplayItem>>,
pub bottom: Option<CssPropertyValue<BoxShadowPreDisplayItem>>,
pub left: Option<CssPropertyValue<BoxShadowPreDisplayItem>>,
}
tlbr_debug!(StyleBoxShadow);
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum LayoutRectContent {
Text {
glyphs: Vec<GlyphInstance>,
font_instance_key: FontInstanceKey,
color: ColorU,
glyph_options: Option<GlyphOptions>,
clip: Option<LayoutRect>,
},
Background {
content: RectBackground,
size: Option<StyleBackgroundSize>,
offset: Option<StyleBackgroundPosition>,
repeat: Option<StyleBackgroundRepeat>,
},
Image {
size: LayoutSize,
offset: LayoutPoint,
image_rendering: ImageRendering,
alpha_type: AlphaType,
image_key: ImageKey,
background_color: ColorU,
},
Border {
widths: StyleBorderWidths,
colors: StyleBorderColors,
styles: StyleBorderStyles,
},
BoxShadow {
shadow: StyleBoxShadow,
clip_mode: BoxShadowClipMode,
},
}
#[derive(Clone, PartialEq, PartialOrd)]
pub enum RectBackground {
LinearGradient(LinearGradient),
RadialGradient(RadialGradient),
Image(ImageInfo),
Color(ColorU),
}
impl fmt::Debug for RectBackground {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::RectBackground::*;
match self {
LinearGradient(l) => write!(f, "{}", l),
RadialGradient(r) => write!(f, "{}", r),
Image(id) => write!(f, "image({:#?})", id),
Color(c) => write!(f, "{}", c),
}
}
}
impl RectBackground {
pub fn get_content_size(&self) -> Option<(f32, f32)> {
match self {
RectBackground::Image(info) => { let dim = info.get_dimensions(); Some((dim.0 as f32, dim.1 as f32)) }
_ => None,
}
}
}