ori_core/canvas/
canvas.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::{
    collections::BTreeMap,
    hash::{Hash, Hasher},
    mem,
    sync::Arc,
};

use crate::{
    image::Image,
    layout::{Affine, Point, Rect, Vector},
    text::Paragraph,
    view::ViewId,
};

use super::{Color, Curve, Stroke};

/// A pattern that can be used to fill a shape.
#[derive(Clone, Debug, PartialEq)]
pub struct Pattern {
    /// The image of the pattern.
    pub image: Image,

    /// The transformation of the pattern.
    pub transform: Affine,

    /// The color of the pattern.
    pub color: Color,
}

impl Hash for Pattern {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.image.hash(state);
        self.transform.hash(state);
        self.color.hash(state);
    }
}

impl From<Image> for Pattern {
    fn from(value: Image) -> Self {
        Self {
            image: value,
            transform: Affine::IDENTITY,
            color: Color::WHITE,
        }
    }
}

/// Ways to fill a shape.
#[derive(Clone, Debug, PartialEq, Hash)]
pub enum Shader {
    /// A solid color.
    Solid(Color),

    /// A pattern.
    Pattern(Pattern),
}

/// Ways to blend two colors.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BlendMode {
    /// Replaces the destination with zero.
    Clear,

    /// Replaces the destination with the source.
    Source,

    /// Preserves the destination.
    Destination,

    /// Source over destination.
    SourceOver,

    /// Destination over source.
    DestinationOver,
}

/// Ways to anti-alias a shape.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AntiAlias {
    /// No anti-aliasing.
    None,

    /// Fast anti-aliasing.
    Fast,

    /// Anti-aliasing.
    Full,
}

/// A paint that can be used to fill or stroke a shape.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct Paint {
    /// The shader of the paint.
    pub shader: Shader,

    /// The blend mode of the paint.
    pub blend: BlendMode,

    /// Whether the paint should be anti-aliased.
    pub anti_alias: AntiAlias,
}

impl Default for Paint {
    fn default() -> Self {
        Self {
            shader: Shader::Solid(Color::BLACK),
            blend: BlendMode::SourceOver,
            anti_alias: AntiAlias::Fast,
        }
    }
}

impl From<Color> for Paint {
    fn from(value: Color) -> Self {
        Self {
            shader: Shader::Solid(value),
            ..Default::default()
        }
    }
}

impl From<Image> for Paint {
    fn from(value: Image) -> Self {
        Self {
            shader: Shader::Pattern(Pattern::from(value)),
            ..Default::default()
        }
    }
}

impl From<Pattern> for Paint {
    fn from(value: Pattern) -> Self {
        Self {
            shader: Shader::Pattern(value),
            ..Default::default()
        }
    }
}

/// Rule determining if a point is inside a shape.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FillRule {
    /// A point is inside the shape if a ray from the point crosses a non-zero sum of signed edge
    /// crossings.
    NonZero,

    /// A point is inside the shape if a ray from the point crosses an odd number of edges.
    EvenOdd,
}

/// A mask that can be used to clip a layer.
#[derive(Clone, Debug, PartialEq)]
pub struct Mask {
    /// The curve of the mask.
    pub curve: Arc<Curve>,

    /// The fill rule of the mask.
    pub fill: FillRule,
}

impl Mask {
    /// Create a new mask.
    pub fn new(curve: impl Into<Arc<Curve>>, fill: FillRule) -> Self {
        Self {
            curve: curve.into(),
            fill,
        }
    }
}

impl From<Rect> for Mask {
    fn from(value: Rect) -> Self {
        Self::new(Curve::rect(value), FillRule::NonZero)
    }
}

/// A primitive that can be drawn on a canvas.
#[derive(Clone, Debug, PartialEq)]
pub enum Primitive {
    /// A filled curve.
    Fill {
        /// The curve to draw.
        curve: Arc<Curve>,

        /// The fill rule of the curve.
        fill: FillRule,

        /// The paint to fill the curve with.
        paint: Paint,
    },

    /// A stroked curve.
    Stroke {
        /// The curve to draw.
        curve: Arc<Curve>,

        /// The stroke properties of the curve.
        stroke: Stroke,

        /// The paint to stroke the curve with.
        paint: Paint,
    },

    /// A paragraph on rich text.
    Paragraph {
        /// The paragraph to draw.
        paragraph: Paragraph,

        /// The bounding rectangle of the paragraph.
        bounds: Rect,

        /// The rectangle to draw the paragraph in.
        rect: Rect,
    },

    /// A layer that can be transformed and masked.
    Layer {
        /// The primitives of the layer.
        primitives: Arc<Vec<Primitive>>,

        /// The transformation of the layer.
        transform: Affine,

        /// The mask of the layer.
        mask: Option<Mask>,

        /// The view of the layer.
        view: Option<ViewId>,
    },
}

impl Primitive {
    /// Count the number of primitives.
    pub fn count(&self) -> usize {
        match self {
            Primitive::Fill { .. } | Primitive::Stroke { .. } | Primitive::Paragraph { .. } => 1,
            Primitive::Layer { primitives, .. } => primitives.iter().map(Self::count).sum(),
        }
    }
}

/// A canvas that can be drawn on.
#[derive(Clone, Debug, PartialEq)]
pub struct Canvas {
    overlays: BTreeMap<i32, Arc<Vec<Primitive>>>,
    primitives: Arc<Vec<Primitive>>,
}

impl Default for Canvas {
    fn default() -> Self {
        Self::new()
    }
}

impl Canvas {
    /// Create a new canvas.
    pub fn new() -> Self {
        Self {
            overlays: BTreeMap::new(),
            primitives: Arc::new(Vec::new()),
        }
    }

    /// Get the primitives of the canvas.
    pub fn primitives(&self) -> impl Iterator<Item = &Primitive> + '_ {
        let overlays = self.overlays.values().flat_map(|p| p.iter());
        self.primitives.iter().chain(overlays)
    }

    /// Clear the canvas.
    pub fn clear(&mut self) {
        self.overlays.clear();
        Arc::make_mut(&mut self.primitives).clear();
    }

    /// Draw a rectangle.
    pub fn rect(&mut self, rect: Rect, paint: impl Into<Paint>) {
        let curve = Curve::rect(rect);
        self.fill(curve.clone(), FillRule::NonZero, paint);
    }

    /// Draw a trigger rectangle.
    pub fn trigger(&mut self, rect: Rect, view: ViewId) {
        self.hoverable(view, |canvas| {
            let curve = Curve::rect(rect);
            canvas.fill(
                curve,
                FillRule::NonZero,
                Paint {
                    shader: Shader::Solid(Color::TRANSPARENT),
                    blend: BlendMode::Destination,
                    anti_alias: AntiAlias::None,
                },
            );
        });
    }

    /// Fill a curve.
    pub fn fill(&mut self, curve: impl Into<Arc<Curve>>, fill: FillRule, paint: impl Into<Paint>) {
        let primitives = Arc::make_mut(&mut self.primitives);
        primitives.push(Primitive::Fill {
            curve: curve.into(),
            fill,
            paint: paint.into(),
        });
    }

    /// Stroke a curve.
    pub fn stroke(
        &mut self,
        curve: impl Into<Arc<Curve>>,
        stroke: impl Into<Stroke>,
        paint: impl Into<Paint>,
    ) {
        let primitives = Arc::make_mut(&mut self.primitives);
        primitives.push(Primitive::Stroke {
            curve: curve.into(),
            stroke: stroke.into(),
            paint: paint.into(),
        });
    }

    /// Draw a paragraph.
    pub fn paragraph(&mut self, paragraph: Paragraph, rect: Rect, bounds: Rect) {
        let primitives = Arc::make_mut(&mut self.primitives);

        primitives.push(Primitive::Paragraph {
            paragraph,
            bounds,
            rect,
        });
    }

    /// Draw a canvas.
    pub fn draw_canvas(&mut self, canvas: Canvas) {
        self.layer(Affine::IDENTITY, None, None, |ca| *ca = canvas);
    }

    /// Draw an overlay.
    pub fn overlay<T>(&mut self, index: i32, f: impl FnOnce(&mut Self) -> T) -> T {
        let mut overlay = Canvas::new();

        let result = f(&mut overlay);

        for (i, mut others) in overlay.overlays {
            let others = mem::take(Arc::make_mut(&mut others));
            let primitives = Arc::make_mut(self.overlays.entry(i).or_default());
            primitives.extend(others);
        }

        let other = mem::take(Arc::make_mut(&mut overlay.primitives));
        let primitives = Arc::make_mut(self.overlays.entry(index).or_default());
        primitives.extend(other);

        result
    }

    /// Draw a layer.
    pub fn layer<T>(
        &mut self,
        transform: Affine,
        mask: Option<Mask>,
        view: Option<ViewId>,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        let mut layer = Canvas::new();

        let result = f(&mut layer);

        for (i, mut other) in layer.overlays {
            let other = mem::take(Arc::make_mut(&mut other));
            let primitives = Arc::make_mut(self.overlays.entry(i).or_default());
            primitives.extend(other);
        }

        let primitives = Arc::make_mut(&mut self.primitives);
        primitives.push(Primitive::Layer {
            primitives: layer.primitives,
            transform,
            mask,
            view,
        });

        result
    }

    /// Draw a layer with a transformation.
    pub fn transformed<T>(&mut self, transform: Affine, f: impl FnOnce(&mut Self) -> T) -> T {
        self.layer(transform, None, None, f)
    }

    /// Draw a layer with a translation.
    pub fn translated<T>(&mut self, translation: Vector, f: impl FnOnce(&mut Self) -> T) -> T {
        self.transformed(Affine::translate(translation), f)
    }

    /// Draw a layer with a rotation.
    pub fn rotated<T>(&mut self, angle: f32, f: impl FnOnce(&mut Self) -> T) -> T {
        self.transformed(Affine::rotate(angle), f)
    }

    /// Draw a layer with a scale.
    pub fn scaled<T>(&mut self, scale: Vector, f: impl FnOnce(&mut Self) -> T) -> T {
        self.transformed(Affine::scale(scale), f)
    }

    /// Draw a layer with a mask.
    pub fn masked<T>(&mut self, mask: Mask, f: impl FnOnce(&mut Self) -> T) -> T {
        self.layer(Affine::IDENTITY, Some(mask), None, f)
    }

    /// Draw a layer with a view.
    pub fn hoverable<T>(&mut self, view: ViewId, f: impl FnOnce(&mut Self) -> T) -> T {
        self.layer(Affine::IDENTITY, None, Some(view), f)
    }

    /// Get the view at a point.
    pub fn view_at(&self, point: Point) -> Option<ViewId> {
        fn recurse(primitives: &[Primitive], view: Option<ViewId>, point: Point) -> Option<ViewId> {
            for primitive in primitives.iter().rev() {
                match primitive {
                    Primitive::Fill { curve, fill, .. } => {
                        if view.is_none() {
                            continue;
                        }

                        if curve.contains(point, *fill) {
                            return view;
                        }
                    }
                    Primitive::Stroke { curve, stroke, .. } => {
                        if view.is_none() {
                            continue;
                        }

                        if !curve.bounds().expand(stroke.width).contains(point) {
                            continue;
                        }

                        let mut stroked = Curve::new();
                        stroked.stroke_curve(curve, *stroke);

                        if stroked.contains(point, FillRule::NonZero) {
                            return view;
                        }
                    }
                    Primitive::Paragraph { bounds, .. } => {
                        if view.is_none() {
                            continue;
                        }

                        if bounds.contains(point) {
                            return view;
                        }
                    }
                    Primitive::Layer {
                        primitives,
                        transform,
                        mask,
                        view: layer_view,
                    } => {
                        let point = transform.inverse() * point;

                        if let Some(mask) = mask {
                            if !mask.curve.contains(point, mask.fill) {
                                continue;
                            }
                        }

                        let view = match layer_view {
                            Some(view) => recurse(primitives, Some(*view), point),
                            None => recurse(primitives, view, point),
                        };

                        if view.is_some() {
                            return view;
                        }
                    }
                }
            }

            None
        }

        for primitives in self.overlays.values().rev() {
            if let Some(view) = recurse(primitives, None, point) {
                return Some(view);
            }
        }

        recurse(&self.primitives, None, point)
    }
}