1#![cfg_attr(windows, allow(dead_code))]
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8use crate::{
9 AtlasTextureId, AtlasTile, Background, Bounds, ContentMask, Corners, Edges, Hsla, Pixels,
10 Point, Radians, ScaledPixels, Size, bounds_tree::BoundsTree, point,
11};
12use std::{
13 fmt::Debug,
14 iter::Peekable,
15 ops::{Add, Range, Sub},
16 slice,
17};
18
19#[allow(non_camel_case_types, unused)]
20#[expect(missing_docs)]
21pub type PathVertex_ScaledPixels = PathVertex<ScaledPixels>;
22
23#[expect(missing_docs)]
24pub type DrawOrder = u32;
25
26#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
31#[repr(transparent)]
32pub struct PaddedBool32(u32);
33
34impl From<bool> for PaddedBool32 {
35 fn from(value: bool) -> Self {
36 PaddedBool32(value as u32)
37 }
38}
39
40#[derive(Default)]
41#[expect(missing_docs)]
42pub struct Scene {
43 pub(crate) paint_operations: Vec<PaintOperation>,
44 pub(crate) primitive_bounds: BoundsTree<ScaledPixels>,
45 layer_stack: Vec<DrawOrder>,
46 pub shadows: Vec<Shadow>,
47 pub quads: Vec<Quad>,
48 pub paths: Vec<Path<ScaledPixels>>,
49 pub underlines: Vec<Underline>,
50 pub monochrome_sprites: Vec<MonochromeSprite>,
51 pub subpixel_sprites: Vec<SubpixelSprite>,
52 pub polychrome_sprites: Vec<PolychromeSprite>,
53 pub surfaces: Vec<PaintSurface>,
54 pub(crate) sort_scratch: crate::fast::scene::SortScratch,
55 pub layers: crate::fast::layers::scene::SceneLayers,
56}
57
58#[expect(missing_docs)]
59impl Scene {
60 pub fn clear(&mut self) {
61 self.paint_operations.clear();
62 self.primitive_bounds.clear();
63 self.layer_stack.clear();
64 self.paths.clear();
65 self.shadows.clear();
66 self.quads.clear();
67 self.underlines.clear();
68 self.monochrome_sprites.clear();
69 self.subpixel_sprites.clear();
70 self.polychrome_sprites.clear();
71 self.surfaces.clear();
72 crate::fast::layers::scene::SceneLayers::clear(&mut self.layers);
73 }
74
75 pub fn len(&self) -> usize {
76 self.paint_operations.len()
77 }
78
79 pub fn push_layer(&mut self, bounds: Bounds<ScaledPixels>) {
80 let order = self.primitive_bounds.insert(bounds);
81 self.layer_stack.push(order);
82 self.paint_operations
83 .push(PaintOperation::StartLayer(bounds));
84 }
85
86 pub fn pop_layer(&mut self) {
87 self.layer_stack.pop();
88 self.paint_operations.push(PaintOperation::EndLayer);
89 }
90
91 pub fn insert_primitive(&mut self, primitive: impl Into<Primitive>) {
92 let mut primitive = primitive.into();
93 let clipped_bounds = primitive
94 .bounds()
95 .intersect(&primitive.content_mask().bounds);
96
97 if clipped_bounds.is_empty() {
98 return;
99 }
100
101 let order = self
102 .layer_stack
103 .last()
104 .copied()
105 .unwrap_or_else(|| self.primitive_bounds.insert(clipped_bounds));
106 match &mut primitive {
107 Primitive::Shadow(shadow) => {
108 shadow.order = order;
109 self.shadows.push(*shadow);
110 }
111 Primitive::Quad(quad) => {
112 quad.order = order;
113 self.quads.push(*quad);
114 }
115 Primitive::Path(path) => {
116 path.order = order;
117 path.id = PathId(self.paths.len());
118 self.paths.push(path.clone());
119 }
120 Primitive::Underline(underline) => {
121 underline.order = order;
122 self.underlines.push(*underline);
123 }
124 Primitive::MonochromeSprite(sprite) => {
125 sprite.order = order;
126 self.monochrome_sprites.push(*sprite);
127 }
128 Primitive::SubpixelSprite(sprite) => {
129 sprite.order = order;
130 self.subpixel_sprites.push(*sprite);
131 }
132 Primitive::PolychromeSprite(sprite) => {
133 sprite.order = order;
134 self.polychrome_sprites.push(*sprite);
135 }
136 Primitive::Surface(surface) => {
137 surface.order = order;
138 self.surfaces.push(surface.clone());
139 }
140 }
141 self.paint_operations
142 .push(PaintOperation::Primitive(primitive));
143 }
144
145 pub fn replay(&mut self, range: Range<usize>, prev_scene: &Scene) {
146 crate::fast::layers::paint::replay_layers(self, range.clone(), prev_scene);
147 for operation in &prev_scene.paint_operations[range] {
148 match operation {
149 PaintOperation::Primitive(primitive) => self.insert_primitive(primitive.clone()),
150 PaintOperation::StartLayer(bounds) => self.push_layer(*bounds),
151 PaintOperation::EndLayer => self.pop_layer(),
152 }
153 }
154 }
155
156 pub fn finish(&mut self) {
157 crate::fast::scene::sort_in_drawing_order(self);
158 }
159
160 #[cfg_attr(
161 all(
162 any(target_os = "linux", target_os = "freebsd"),
163 not(any(feature = "x11", feature = "wayland"))
164 ),
165 allow(dead_code)
166 )]
167 pub fn batches(&self) -> impl Iterator<Item = PrimitiveBatch> + '_ {
168 BatchIterator {
169 shadows_start: 0,
170 shadows_iter: self.shadows.iter().peekable(),
171 quads_start: 0,
172 quads_iter: self.quads.iter().peekable(),
173 paths_start: 0,
174 paths_iter: self.paths.iter().peekable(),
175 underlines_start: 0,
176 underlines_iter: self.underlines.iter().peekable(),
177 monochrome_sprites_start: 0,
178 monochrome_sprites_iter: self.monochrome_sprites.iter().peekable(),
179 subpixel_sprites_start: 0,
180 subpixel_sprites_iter: self.subpixel_sprites.iter().peekable(),
181 polychrome_sprites_start: 0,
182 polychrome_sprites_iter: self.polychrome_sprites.iter().peekable(),
183 surfaces_start: 0,
184 surfaces_iter: self.surfaces.iter().peekable(),
185 }
186 }
187}
188
189#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Default)]
190#[cfg_attr(
191 all(
192 any(target_os = "linux", target_os = "freebsd"),
193 not(any(feature = "x11", feature = "wayland"))
194 ),
195 allow(dead_code)
196)]
197pub(crate) enum PrimitiveKind {
198 Shadow,
199 #[default]
200 Quad,
201 Path,
202 Underline,
203 MonochromeSprite,
204 SubpixelSprite,
205 PolychromeSprite,
206 Surface,
207}
208
209pub(crate) enum PaintOperation {
210 Primitive(Primitive),
211 StartLayer(Bounds<ScaledPixels>),
212 EndLayer,
213}
214
215#[derive(Clone)]
216#[expect(missing_docs)]
217pub enum Primitive {
218 Shadow(Shadow),
219 Quad(Quad),
220 Path(Path<ScaledPixels>),
221 Underline(Underline),
222 MonochromeSprite(MonochromeSprite),
223 SubpixelSprite(SubpixelSprite),
224 PolychromeSprite(PolychromeSprite),
225 Surface(PaintSurface),
226}
227
228#[expect(missing_docs)]
229impl Primitive {
230 pub fn bounds(&self) -> &Bounds<ScaledPixels> {
231 match self {
232 Primitive::Shadow(shadow) => &shadow.bounds,
233 Primitive::Quad(quad) => &quad.bounds,
234 Primitive::Path(path) => &path.bounds,
235 Primitive::Underline(underline) => &underline.bounds,
236 Primitive::MonochromeSprite(sprite) => &sprite.bounds,
237 Primitive::SubpixelSprite(sprite) => &sprite.bounds,
238 Primitive::PolychromeSprite(sprite) => &sprite.bounds,
239 Primitive::Surface(surface) => &surface.bounds,
240 }
241 }
242
243 pub fn content_mask(&self) -> &ContentMask<ScaledPixels> {
244 match self {
245 Primitive::Shadow(shadow) => &shadow.content_mask,
246 Primitive::Quad(quad) => &quad.content_mask,
247 Primitive::Path(path) => &path.content_mask,
248 Primitive::Underline(underline) => &underline.content_mask,
249 Primitive::MonochromeSprite(sprite) => &sprite.content_mask,
250 Primitive::SubpixelSprite(sprite) => &sprite.content_mask,
251 Primitive::PolychromeSprite(sprite) => &sprite.content_mask,
252 Primitive::Surface(surface) => &surface.content_mask,
253 }
254 }
255}
256
257#[cfg_attr(
258 all(
259 any(target_os = "linux", target_os = "freebsd"),
260 not(any(feature = "x11", feature = "wayland"))
261 ),
262 allow(dead_code)
263)]
264struct BatchIterator<'a> {
265 shadows_start: usize,
266 shadows_iter: Peekable<slice::Iter<'a, Shadow>>,
267 quads_start: usize,
268 quads_iter: Peekable<slice::Iter<'a, Quad>>,
269 paths_start: usize,
270 paths_iter: Peekable<slice::Iter<'a, Path<ScaledPixels>>>,
271 underlines_start: usize,
272 underlines_iter: Peekable<slice::Iter<'a, Underline>>,
273 monochrome_sprites_start: usize,
274 monochrome_sprites_iter: Peekable<slice::Iter<'a, MonochromeSprite>>,
275 subpixel_sprites_start: usize,
276 subpixel_sprites_iter: Peekable<slice::Iter<'a, SubpixelSprite>>,
277 polychrome_sprites_start: usize,
278 polychrome_sprites_iter: Peekable<slice::Iter<'a, PolychromeSprite>>,
279 surfaces_start: usize,
280 surfaces_iter: Peekable<slice::Iter<'a, PaintSurface>>,
281}
282
283impl<'a> Iterator for BatchIterator<'a> {
284 type Item = PrimitiveBatch;
285
286 fn next(&mut self) -> Option<Self::Item> {
287 let mut orders_and_kinds = [
288 (
289 self.shadows_iter.peek().map(|s| s.order),
290 PrimitiveKind::Shadow,
291 ),
292 (self.quads_iter.peek().map(|q| q.order), PrimitiveKind::Quad),
293 (self.paths_iter.peek().map(|q| q.order), PrimitiveKind::Path),
294 (
295 self.underlines_iter.peek().map(|u| u.order),
296 PrimitiveKind::Underline,
297 ),
298 (
299 self.monochrome_sprites_iter.peek().map(|s| s.order),
300 PrimitiveKind::MonochromeSprite,
301 ),
302 (
303 self.subpixel_sprites_iter.peek().map(|s| s.order),
304 PrimitiveKind::SubpixelSprite,
305 ),
306 (
307 self.polychrome_sprites_iter.peek().map(|s| s.order),
308 PrimitiveKind::PolychromeSprite,
309 ),
310 (
311 self.surfaces_iter.peek().map(|s| s.order),
312 PrimitiveKind::Surface,
313 ),
314 ];
315 orders_and_kinds.sort_by_key(|(order, kind)| (order.unwrap_or(u32::MAX), *kind));
316
317 let first = orders_and_kinds[0];
318 let second = orders_and_kinds[1];
319 let (batch_kind, max_order_and_kind) = if first.0.is_some() {
320 (first.1, (second.0.unwrap_or(u32::MAX), second.1))
321 } else {
322 return None;
323 };
324
325 match batch_kind {
326 PrimitiveKind::Shadow => {
327 let shadows_start = self.shadows_start;
328 let mut shadows_end = shadows_start + 1;
329 self.shadows_iter.next();
330 while self
331 .shadows_iter
332 .next_if(|shadow| (shadow.order, batch_kind) < max_order_and_kind)
333 .is_some()
334 {
335 shadows_end += 1;
336 }
337 self.shadows_start = shadows_end;
338 Some(PrimitiveBatch::Shadows(shadows_start..shadows_end))
339 }
340 PrimitiveKind::Quad => {
341 let quads_start = self.quads_start;
342 let mut quads_end = quads_start + 1;
343 self.quads_iter.next();
344 while self
345 .quads_iter
346 .next_if(|quad| (quad.order, batch_kind) < max_order_and_kind)
347 .is_some()
348 {
349 quads_end += 1;
350 }
351 self.quads_start = quads_end;
352 Some(PrimitiveBatch::Quads(quads_start..quads_end))
353 }
354 PrimitiveKind::Path => {
355 let paths_start = self.paths_start;
356 let mut paths_end = paths_start + 1;
357 self.paths_iter.next();
358 while self
359 .paths_iter
360 .next_if(|path| (path.order, batch_kind) < max_order_and_kind)
361 .is_some()
362 {
363 paths_end += 1;
364 }
365 self.paths_start = paths_end;
366 Some(PrimitiveBatch::Paths(paths_start..paths_end))
367 }
368 PrimitiveKind::Underline => {
369 let underlines_start = self.underlines_start;
370 let mut underlines_end = underlines_start + 1;
371 self.underlines_iter.next();
372 while self
373 .underlines_iter
374 .next_if(|underline| (underline.order, batch_kind) < max_order_and_kind)
375 .is_some()
376 {
377 underlines_end += 1;
378 }
379 self.underlines_start = underlines_end;
380 Some(PrimitiveBatch::Underlines(underlines_start..underlines_end))
381 }
382 PrimitiveKind::MonochromeSprite => {
383 let texture_id = self.monochrome_sprites_iter.peek().unwrap().tile.texture_id;
384 let sprites_start = self.monochrome_sprites_start;
385 let mut sprites_end = sprites_start + 1;
386 self.monochrome_sprites_iter.next();
387 while self
388 .monochrome_sprites_iter
389 .next_if(|sprite| {
390 (sprite.order, batch_kind) < max_order_and_kind
391 && sprite.tile.texture_id == texture_id
392 })
393 .is_some()
394 {
395 sprites_end += 1;
396 }
397 self.monochrome_sprites_start = sprites_end;
398 Some(PrimitiveBatch::MonochromeSprites {
399 texture_id,
400 range: sprites_start..sprites_end,
401 })
402 }
403 PrimitiveKind::SubpixelSprite => {
404 let texture_id = self.subpixel_sprites_iter.peek().unwrap().tile.texture_id;
405 let sprites_start = self.subpixel_sprites_start;
406 let mut sprites_end = sprites_start + 1;
407 self.subpixel_sprites_iter.next();
408 while self
409 .subpixel_sprites_iter
410 .next_if(|sprite| {
411 (sprite.order, batch_kind) < max_order_and_kind
412 && sprite.tile.texture_id == texture_id
413 })
414 .is_some()
415 {
416 sprites_end += 1;
417 }
418 self.subpixel_sprites_start = sprites_end;
419 Some(PrimitiveBatch::SubpixelSprites {
420 texture_id,
421 range: sprites_start..sprites_end,
422 })
423 }
424 PrimitiveKind::PolychromeSprite => {
425 let texture_id = self.polychrome_sprites_iter.peek().unwrap().tile.texture_id;
426 let sprites_start = self.polychrome_sprites_start;
427 let mut sprites_end = sprites_start + 1;
428 self.polychrome_sprites_iter.next();
429 while self
430 .polychrome_sprites_iter
431 .next_if(|sprite| {
432 (sprite.order, batch_kind) < max_order_and_kind
433 && sprite.tile.texture_id == texture_id
434 })
435 .is_some()
436 {
437 sprites_end += 1;
438 }
439 self.polychrome_sprites_start = sprites_end;
440 Some(PrimitiveBatch::PolychromeSprites {
441 texture_id,
442 range: sprites_start..sprites_end,
443 })
444 }
445 PrimitiveKind::Surface => {
446 let surfaces_start = self.surfaces_start;
447 let mut surfaces_end = surfaces_start + 1;
448 self.surfaces_iter.next();
449 while self
450 .surfaces_iter
451 .next_if(|surface| (surface.order, batch_kind) < max_order_and_kind)
452 .is_some()
453 {
454 surfaces_end += 1;
455 }
456 self.surfaces_start = surfaces_end;
457 Some(PrimitiveBatch::Surfaces(surfaces_start..surfaces_end))
458 }
459 }
460 }
461}
462
463#[derive(Debug)]
464#[cfg_attr(
465 all(
466 any(target_os = "linux", target_os = "freebsd"),
467 not(any(feature = "x11", feature = "wayland"))
468 ),
469 allow(dead_code)
470)]
471#[allow(missing_docs)]
472pub enum PrimitiveBatch {
473 Shadows(Range<usize>),
474 Quads(Range<usize>),
475 Paths(Range<usize>),
476 Underlines(Range<usize>),
477 MonochromeSprites {
478 texture_id: AtlasTextureId,
479 range: Range<usize>,
480 },
481 #[cfg_attr(target_os = "macos", allow(dead_code))]
482 SubpixelSprites {
483 texture_id: AtlasTextureId,
484 range: Range<usize>,
485 },
486 PolychromeSprites {
487 texture_id: AtlasTextureId,
488 range: Range<usize>,
489 },
490 Surfaces(Range<usize>),
491}
492
493impl PrimitiveBatch {
494 #[expect(missing_docs)]
495 pub fn label(&self) -> String {
496 match self {
497 Self::Shadows(range) => format!("shadows ({})", range.len()),
498 Self::Quads(range) => format!("quads ({})", range.len()),
499 Self::Paths(range) => format!("paths ({})", range.len()),
500 Self::Underlines(range) => format!("underlines ({})", range.len()),
501 Self::MonochromeSprites { texture_id, range } => {
502 format!(
503 "monochrome sprites ({}) on atlas {}",
504 range.len(),
505 texture_id.index
506 )
507 }
508 Self::SubpixelSprites { texture_id, range } => {
509 format!(
510 "subpixel sprites ({}) on atlas {}",
511 range.len(),
512 texture_id.index
513 )
514 }
515 Self::PolychromeSprites { texture_id, range } => {
516 format!(
517 "polychrome sprites ({}) on atlas {}",
518 range.len(),
519 texture_id.index
520 )
521 }
522 Self::Surfaces(range) => format!("surfaces ({})", range.len()),
523 }
524 }
525}
526
527#[derive(Default, Debug, Copy, Clone)]
528#[repr(C)]
529#[expect(missing_docs)]
530pub struct Quad {
531 pub order: DrawOrder,
532 pub border_style: BorderStyle,
533 pub bounds: Bounds<ScaledPixels>,
534 pub content_mask: ContentMask<ScaledPixels>,
535 pub background: Background,
536 pub border_color: Hsla,
537 pub corner_radii: Corners<ScaledPixels>,
538 pub border_widths: Edges<ScaledPixels>,
539}
540
541impl From<Quad> for Primitive {
542 fn from(quad: Quad) -> Self {
543 Primitive::Quad(quad)
544 }
545}
546
547#[derive(Debug, Copy, Clone)]
548#[repr(C)]
549#[expect(missing_docs)]
550pub struct Underline {
551 pub order: DrawOrder,
552 pub pad: u32, pub bounds: Bounds<ScaledPixels>,
554 pub content_mask: ContentMask<ScaledPixels>,
555 pub color: Hsla,
556 pub thickness: ScaledPixels,
557 pub wavy: PaddedBool32,
558}
559
560impl From<Underline> for Primitive {
561 fn from(underline: Underline) -> Self {
562 Primitive::Underline(underline)
563 }
564}
565
566#[derive(Debug, Copy, Clone)]
567#[repr(C)]
568#[expect(missing_docs)]
569pub struct Shadow {
570 pub order: DrawOrder,
571 pub blur_radius: ScaledPixels,
572 pub bounds: Bounds<ScaledPixels>,
573 pub corner_radii: Corners<ScaledPixels>,
574 pub content_mask: ContentMask<ScaledPixels>,
575 pub color: Hsla,
576 pub element_bounds: Bounds<ScaledPixels>,
577 pub element_corner_radii: Corners<ScaledPixels>,
578 pub inset: u32,
580 pub pad: u32, }
582
583impl From<Shadow> for Primitive {
584 fn from(shadow: Shadow) -> Self {
585 Primitive::Shadow(shadow)
586 }
587}
588
589#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
591#[repr(C)]
592pub enum BorderStyle {
593 #[default]
595 Solid = 0,
596 Dashed = 1,
598}
599
600#[derive(Debug, Clone, Copy, PartialEq)]
602#[repr(C)]
603pub struct TransformationMatrix {
604 pub rotation_scale: [[f32; 2]; 2],
607 pub translation: [f32; 2],
609}
610
611impl Eq for TransformationMatrix {}
612
613impl TransformationMatrix {
614 pub fn unit() -> Self {
616 Self {
617 rotation_scale: [[1.0, 0.0], [0.0, 1.0]],
618 translation: [0.0, 0.0],
619 }
620 }
621
622 pub fn translate(mut self, point: Point<ScaledPixels>) -> Self {
624 self.compose(Self {
625 rotation_scale: [[1.0, 0.0], [0.0, 1.0]],
626 translation: [point.x.0, point.y.0],
627 })
628 }
629
630 pub fn rotate(self, angle: Radians) -> Self {
632 self.compose(Self {
633 rotation_scale: [
634 [angle.0.cos(), -angle.0.sin()],
635 [angle.0.sin(), angle.0.cos()],
636 ],
637 translation: [0.0, 0.0],
638 })
639 }
640
641 pub fn scale(self, size: Size<f32>) -> Self {
643 self.compose(Self {
644 rotation_scale: [[size.width, 0.0], [0.0, size.height]],
645 translation: [0.0, 0.0],
646 })
647 }
648
649 #[inline]
653 pub fn compose(self, other: TransformationMatrix) -> TransformationMatrix {
654 if other == Self::unit() {
655 return self;
656 }
657 TransformationMatrix {
659 rotation_scale: [
660 [
661 self.rotation_scale[0][0] * other.rotation_scale[0][0]
662 + self.rotation_scale[0][1] * other.rotation_scale[1][0],
663 self.rotation_scale[0][0] * other.rotation_scale[0][1]
664 + self.rotation_scale[0][1] * other.rotation_scale[1][1],
665 ],
666 [
667 self.rotation_scale[1][0] * other.rotation_scale[0][0]
668 + self.rotation_scale[1][1] * other.rotation_scale[1][0],
669 self.rotation_scale[1][0] * other.rotation_scale[0][1]
670 + self.rotation_scale[1][1] * other.rotation_scale[1][1],
671 ],
672 ],
673 translation: [
674 self.translation[0]
675 + self.rotation_scale[0][0] * other.translation[0]
676 + self.rotation_scale[0][1] * other.translation[1],
677 self.translation[1]
678 + self.rotation_scale[1][0] * other.translation[0]
679 + self.rotation_scale[1][1] * other.translation[1],
680 ],
681 }
682 }
683
684 pub fn apply(&self, point: Point<Pixels>) -> Point<Pixels> {
686 let input = [point.x.0, point.y.0];
687 let mut output = self.translation;
688 for (i, output_cell) in output.iter_mut().enumerate() {
689 for (k, input_cell) in input.iter().enumerate() {
690 *output_cell += self.rotation_scale[i][k] * *input_cell;
691 }
692 }
693 Point::new(output[0].into(), output[1].into())
694 }
695}
696
697impl Default for TransformationMatrix {
698 fn default() -> Self {
699 Self::unit()
700 }
701}
702
703#[derive(Copy, Clone, Debug)]
704#[repr(C)]
705#[expect(missing_docs)]
706pub struct MonochromeSprite {
707 pub order: DrawOrder,
708 pub pad: u32,
709 pub bounds: Bounds<ScaledPixels>,
710 pub content_mask: ContentMask<ScaledPixels>,
711 pub color: Hsla,
712 pub tile: AtlasTile,
713 pub transformation: TransformationMatrix,
714}
715
716impl From<MonochromeSprite> for Primitive {
717 fn from(sprite: MonochromeSprite) -> Self {
718 Primitive::MonochromeSprite(sprite)
719 }
720}
721
722#[derive(Copy, Clone, Debug)]
723#[repr(C)]
724#[expect(missing_docs)]
725pub struct SubpixelSprite {
726 pub order: DrawOrder,
727 pub pad: u32, pub bounds: Bounds<ScaledPixels>,
729 pub content_mask: ContentMask<ScaledPixels>,
730 pub color: Hsla,
731 pub tile: AtlasTile,
732 pub transformation: TransformationMatrix,
733}
734
735impl From<SubpixelSprite> for Primitive {
736 fn from(sprite: SubpixelSprite) -> Self {
737 Primitive::SubpixelSprite(sprite)
738 }
739}
740
741#[derive(Copy, Clone, Debug)]
742#[repr(C)]
743#[expect(missing_docs)]
744pub struct PolychromeSprite {
745 pub order: DrawOrder,
746 pub pad: u32,
747 pub grayscale: PaddedBool32,
748 pub opacity: f32,
749 pub bounds: Bounds<ScaledPixels>,
750 pub content_mask: ContentMask<ScaledPixels>,
751 pub corner_radii: Corners<ScaledPixels>,
752 pub tile: AtlasTile,
753}
754
755impl From<PolychromeSprite> for Primitive {
756 fn from(sprite: PolychromeSprite) -> Self {
757 Primitive::PolychromeSprite(sprite)
758 }
759}
760
761#[derive(Clone, Debug)]
762#[allow(missing_docs)]
763pub struct PaintSurface {
764 pub order: DrawOrder,
765 pub bounds: Bounds<ScaledPixels>,
766 pub content_mask: ContentMask<ScaledPixels>,
767 #[cfg(any(target_os = "macos", target_os = "ios"))]
768 pub image_buffer: objc2_core_foundation::CFRetained<objc2_core_video::CVPixelBuffer>,
769}
770
771impl From<PaintSurface> for Primitive {
772 fn from(surface: PaintSurface) -> Self {
773 Primitive::Surface(surface)
774 }
775}
776
777#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
778#[expect(missing_docs)]
779pub struct PathId(pub usize);
780
781#[derive(Clone, Debug)]
783#[expect(missing_docs)]
784pub struct Path<P: Clone + Debug + Default + PartialEq> {
785 pub id: PathId,
786 pub order: DrawOrder,
787 pub bounds: Bounds<P>,
788 pub content_mask: ContentMask<P>,
789 pub vertices: Vec<PathVertex<P>>,
790 pub color: Background,
791 start: Point<P>,
792 current: Point<P>,
793 contour_count: usize,
794}
795
796impl Path<Pixels> {
797 pub fn new(start: Point<Pixels>) -> Self {
799 Self {
800 id: PathId(0),
801 order: DrawOrder::default(),
802 vertices: Vec::new(),
803 start,
804 current: start,
805 bounds: Bounds {
806 origin: start,
807 size: Default::default(),
808 },
809 content_mask: Default::default(),
810 color: Default::default(),
811 contour_count: 0,
812 }
813 }
814
815 pub fn scale(&self, factor: f32) -> Path<ScaledPixels> {
817 Path {
818 id: self.id,
819 order: self.order,
820 bounds: self.bounds.scale(factor),
821 content_mask: self.content_mask.scale(factor),
822 vertices: self
823 .vertices
824 .iter()
825 .map(|vertex| vertex.scale(factor))
826 .collect(),
827 start: self.start.map(|start| start.scale(factor)),
828 current: self.current.scale(factor),
829 contour_count: self.contour_count,
830 color: self.color,
831 }
832 }
833
834 pub fn move_to(&mut self, to: Point<Pixels>) {
836 self.contour_count += 1;
837 self.start = to;
838 self.current = to;
839 }
840
841 pub fn line_to(&mut self, to: Point<Pixels>) {
843 self.contour_count += 1;
844 if self.contour_count > 1 {
845 self.push_triangle(
846 (self.start, self.current, to),
847 (point(0., 1.), point(0., 1.), point(0., 1.)),
848 );
849 }
850 self.current = to;
851 }
852
853 pub fn curve_to(&mut self, to: Point<Pixels>, ctrl: Point<Pixels>) {
855 self.contour_count += 1;
856 if self.contour_count > 1 {
857 self.push_triangle(
858 (self.start, self.current, to),
859 (point(0., 1.), point(0., 1.), point(0., 1.)),
860 );
861 }
862
863 self.push_triangle(
864 (self.current, ctrl, to),
865 (point(0., 0.), point(0.5, 0.), point(1., 1.)),
866 );
867 self.current = to;
868 }
869
870 pub fn push_triangle(
872 &mut self,
873 xy: (Point<Pixels>, Point<Pixels>, Point<Pixels>),
874 st: (Point<f32>, Point<f32>, Point<f32>),
875 ) {
876 self.bounds = self
877 .bounds
878 .union(&Bounds {
879 origin: xy.0,
880 size: Default::default(),
881 })
882 .union(&Bounds {
883 origin: xy.1,
884 size: Default::default(),
885 })
886 .union(&Bounds {
887 origin: xy.2,
888 size: Default::default(),
889 });
890
891 self.vertices.push(PathVertex {
892 xy_position: xy.0,
893 st_position: st.0,
894 content_mask: Default::default(),
895 });
896 self.vertices.push(PathVertex {
897 xy_position: xy.1,
898 st_position: st.1,
899 content_mask: Default::default(),
900 });
901 self.vertices.push(PathVertex {
902 xy_position: xy.2,
903 st_position: st.2,
904 content_mask: Default::default(),
905 });
906 }
907}
908
909impl<T> Path<T>
910where
911 T: Clone + Debug + Default + PartialEq + PartialOrd + Add<T, Output = T> + Sub<Output = T>,
912{
913 #[allow(unused)]
914 #[expect(missing_docs)]
915 pub fn clipped_bounds(&self) -> Bounds<T> {
916 self.bounds.intersect(&self.content_mask.bounds)
917 }
918}
919
920impl From<Path<ScaledPixels>> for Primitive {
921 fn from(path: Path<ScaledPixels>) -> Self {
922 Primitive::Path(path)
923 }
924}
925
926#[derive(Clone, Debug)]
927#[repr(C)]
928#[expect(missing_docs)]
929pub struct PathVertex<P: Clone + Debug + Default + PartialEq> {
930 pub xy_position: Point<P>,
931 pub st_position: Point<f32>,
932 pub content_mask: ContentMask<P>,
933}
934
935#[expect(missing_docs)]
936impl PathVertex<Pixels> {
937 pub fn scale(&self, factor: f32) -> PathVertex<ScaledPixels> {
938 PathVertex {
939 xy_position: self.xy_position.scale(factor),
940 st_position: self.st_position,
941 content_mask: self.content_mask.scale(factor),
942 }
943 }
944}