1use bytemuck::{Pod, Zeroable};
6use rcus_layout::{LayoutNode, LayoutTree, Rect, Size};
7
8#[repr(C)]
14#[derive(Clone, Copy, Debug, Pod, Zeroable)]
15pub struct BoxVertex {
16 pub position: [f32; 2],
17 pub local: [f32; 2],
18 pub half: [f32; 2],
19 pub radius: f32,
20 pub border_width: f32,
21 pub fill: [f32; 4],
22 pub border: [f32; 4],
23 pub shadow_offset: [f32; 2],
24 pub shadow_blur: f32,
25 pub shadow_spread: f32,
26 pub shadow_color: [f32; 4],
27}
28
29#[repr(C)]
30#[derive(Clone, Copy, Debug, Pod, Zeroable)]
31pub struct TextVertex {
32 pub position: [f32; 2],
34 pub uv: [f32; 2],
36 pub color: [f32; 4],
37 pub layer: f32,
39}
40
41#[derive(Clone, Copy, Debug)]
43pub struct Scissor {
44 pub x: f32,
45 pub y: f32,
46 pub width: f32,
47 pub height: f32,
48}
49
50pub struct Frame {
52 pub boxes: Vec<BoxVertex>,
54 pub glyphs: Vec<TextVertex>,
56 pub scissors: Vec<Scissor>,
58 pub glyph_scissors: Vec<Option<u16>>,
60 pub box_scissors: Vec<Option<u16>>,
62}
63
64impl Frame {
65 pub fn new() -> Self {
66 Self {
67 boxes: Vec::with_capacity(256),
68 glyphs: Vec::with_capacity(4096),
69 scissors: Vec::new(),
70 glyph_scissors: Vec::new(),
71 box_scissors: Vec::new(),
72 }
73 }
74
75 pub fn is_empty(&self) -> bool {
76 self.boxes.is_empty() && self.glyphs.is_empty()
77 }
78}
79
80impl Default for Frame {
81 fn default() -> Self {
82 Self::new()
83 }
84}
85
86#[derive(Clone, Copy, Debug, Default)]
88pub struct BoxStyle {
89 pub radius: f32,
90 pub border_width: f32,
91 pub fill: [f32; 4],
92 pub border: [f32; 4],
93 pub shadow_offset: [f32; 2],
94 pub shadow_blur: f32,
95 pub shadow_spread: f32,
96 pub shadow_color: [f32; 4],
97}
98
99impl BoxStyle {
100 pub fn fill(color: [f32; 4]) -> Self {
103 Self {
104 fill: color,
105 ..Self::default()
106 }
107 }
108
109 fn reach(&self) -> f32 {
111 if self.shadow_color[3] <= 0.0 {
112 return 0.0;
113 }
114 self.shadow_blur.abs()
115 + self.shadow_spread.abs()
116 + self.shadow_offset[0].abs().max(self.shadow_offset[1].abs())
117 }
118}
119
120pub fn push_box(out: &mut Vec<BoxVertex>, viewport: Size, rect: Rect, style: BoxStyle) {
127 if rect.width <= 0.0 || rect.height <= 0.0 || viewport.width <= 0.0 || viewport.height <= 0.0 {
128 return;
129 }
130 if style.fill[3] <= 0.0 && style.border[3] <= 0.0 && style.reach() <= 0.0 {
131 return;
132 }
133 let reach = style.reach();
134 let left = rect.x - reach;
135 let top = rect.y - reach;
136 let right = rect.right() + reach;
137 let bottom = rect.bottom() + reach;
138 let centre_x = rect.x + rect.width / 2.0;
139 let centre_y = rect.y + rect.height / 2.0;
140 let half = [rect.width / 2.0, rect.height / 2.0];
141 let radius = style.radius.max(0.0).min(half[0].min(half[1]));
142 let border_width =
143 style
144 .border_width
145 .max(0.0)
146 .min(half[0].min(half[1]))
147 .min(if style.border[3] > 0.0 {
148 half[0].min(half[1])
149 } else {
150 0.0
151 });
152
153 let x0 = left / viewport.width * 2.0 - 1.0;
154 let x1 = right / viewport.width * 2.0 - 1.0;
155 let y0 = 1.0 - top / viewport.height * 2.0;
156 let y1 = 1.0 - bottom / viewport.height * 2.0;
157 for (px, py) in [
158 (left, top),
159 (right, top),
160 (right, bottom),
161 (left, top),
162 (right, bottom),
163 (left, bottom),
164 ] {
165 out.push(BoxVertex {
166 position: [
167 px / viewport.width * 2.0 - 1.0,
168 1.0 - py / viewport.height * 2.0,
169 ],
170 local: [px - centre_x, py - centre_y],
171 half,
172 radius,
173 border_width,
174 fill: style.fill,
175 border: style.border,
176 shadow_offset: style.shadow_offset,
177 shadow_blur: style.shadow_blur,
178 shadow_spread: style.shadow_spread,
179 shadow_color: style.shadow_color,
180 });
181 }
182 let _ = (x0, x1, y0, y1);
185}
186
187#[derive(Clone, Copy, Debug)]
189pub struct GlyphQuad {
190 pub left: f32,
191 pub top: f32,
192 pub width: f32,
193 pub height: f32,
194 pub uv: [f32; 4],
196 pub color: [f32; 4],
197 pub layer: f32,
198}
199
200pub fn push_glyph(out: &mut Vec<TextVertex>, quad: GlyphQuad) {
202 let GlyphQuad {
203 left,
204 top,
205 width,
206 height,
207 uv,
208 color,
209 layer,
210 } = quad;
211 let (u0, v0) = (uv[0], uv[1]);
212 let (u1, v1) = (uv[0] + uv[2], uv[1] + uv[3]);
213 for (x, y, u, v) in [
214 (left, top, u0, v0),
215 (left + width, top, u1, v0),
216 (left + width, top + height, u1, v1),
217 (left, top, u0, v0),
218 (left + width, top + height, u1, v1),
219 (left, top + height, u0, v1),
220 ] {
221 out.push(TextVertex {
222 position: [x, y],
223 uv: [u, v],
224 color,
225 layer,
226 });
227 }
228}
229
230pub fn build_quads(tree: &LayoutTree) -> Frame {
232 let mut frame = Frame::new();
233 let viewport = tree.viewport;
234 push_box(
235 &mut frame.boxes,
236 viewport,
237 Rect::new(0.0, 0.0, viewport.width, viewport.height),
238 BoxStyle::fill([0.02, 0.02, 0.03, 1.0]),
239 );
240 frame.box_scissors.extend(std::iter::repeat_n(None, 6));
242 collect_quads(&tree.root, viewport, None, &mut frame);
243 frame
244}
245
246fn collect_quads(node: &LayoutNode, viewport: Size, inherited: Option<Rect>, frame: &mut Frame) {
247 let mut clip = inherited;
248 if node.clip {
249 let rect = match clip {
250 Some(parent) => intersect(parent, node.rect),
251 None => node.rect,
252 };
253 if rect.width <= 0.0 || rect.height <= 0.0 {
254 return;
255 }
256 clip = Some(rect);
257 }
258 let scissor = clip.filter(|rect| {
259 !(rect.x <= node.rect.x
260 && rect.y <= node.rect.y
261 && rect.right() >= node.rect.right()
262 && rect.bottom() >= node.rect.bottom())
263 });
264 let scissor_index = scissor.map(|rect| {
265 frame.scissors.push(Scissor {
266 x: rect.x,
267 y: rect.y,
268 width: rect.width,
269 height: rect.height,
270 });
271 (frame.scissors.len() - 1) as u16
272 });
273 let fill = node.background;
274 if fill.is_some() || node.border_color.is_some() || node.shadow.is_some() {
275 let mut style = BoxStyle {
276 fill: fill.map_or([0.0, 0.0, 0.0, 0.0], |color| {
277 [color.r, color.g, color.b, color.a]
278 }),
279 ..BoxStyle::default()
280 };
281 style.radius = node.corner_radius();
282 if let Some(border) = node.border_color {
283 if node.border_width > 0.0 && border.a > 0.0 {
284 style.border_width = node.border_width;
285 style.border = [border.r, border.g, border.b, border.a];
286 }
287 }
288 if let Some(shadow) = node.shadow {
289 if !shadow.is_empty() {
290 style.shadow_offset = [shadow.offset_x, shadow.offset_y];
291 style.shadow_blur = shadow.blur;
292 style.shadow_spread = shadow.spread;
293 style.shadow_color = [
294 shadow.color.r,
295 shadow.color.g,
296 shadow.color.b,
297 shadow.color.a,
298 ];
299 }
300 }
301 push_box(&mut frame.boxes, viewport, node.rect, style);
302 frame
303 .box_scissors
304 .extend(std::iter::repeat_n(scissor_index, 6));
305 }
306 for child in &node.children {
307 collect_quads(child, viewport, clip, frame);
308 }
309}
310
311pub fn intersect(a: Rect, b: Rect) -> Rect {
312 let x = a.x.max(b.x);
313 let y = a.y.max(b.y);
314 let right = a.right().min(b.right());
315 let bottom = a.bottom().min(b.bottom());
316 Rect::new(x, y, (right - x).max(0.0), (bottom - y).max(0.0))
317}