ferroui-base 0.3.0

The base library of FerroUI: the property system, layout, input, styling, media, text, animation, data binding and the compositor
Documentation
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
use super::{DrawingLog, MockDrawingContextImpl, MockDrawingContextLayerImpl, MockRenderTargetBitmapImpl};
use crate::media::imaging::BitmapInterpolationMode;
use crate::media::{FillRule, GeometryCombineMode, IPen, IntersectionResult, SweepDirection};
use crate::platform::surfaces::IPlatformRenderSurface;
use crate::platform::{
    AlphaFormat, IBitmapImpl, IDrawingContextImpl, IDrawingContextLayerImpl, IGeometryContext, IGeometryImpl,
    IOptionalFeatureProvider, IPlatformGraphicsContext, IPlatformRenderInterface, IPlatformRenderInterfaceContext,
    IPlatformRenderInterfaceRegion, IRenderTarget, IRenderTargetBitmapImpl, IStreamGeometryBuilder,
    ITransformedGeometryImpl, IWriteableBitmapImpl, LtrbPixelRect, LtrbRect, PixelFormat,
    PixelFormats, RenderTargetDrawingContextProperties, RenderTargetProperties, RenderTargetSceneInfo,
};
use crate::reactive::IDisposable;
use crate::{FerroLocator, Matrix, PixelSize, Point, Rect, Size, Vector};
use std::any::{Any, TypeId};
use std::cell::{Cell, RefCell};
use std::io::Read;
use std::rc::Rc;
use std::sync::Arc;

/// A platform geometry that behaves like its bounding rectangle.
///
/// As the headless geometries of the reference, rectangle and line
/// geometries also know their corner points (`HeadlessGeometryWithEdgesStub`),
/// and a transformed geometry knows the transformed points of its source
/// (`HeadlessTransformedGeometryStub`): fill intersections between such
/// geometries and with stream geometries use a separating axis test on them.
pub struct MockGeometryImpl {
    bounds: Rect,
    source: Option<(Arc<MockGeometryImpl>, Matrix)>,
    edges: Option<Vec<Point>>,
}

impl MockGeometryImpl {
    pub fn new(bounds: Rect) -> Arc<MockGeometryImpl> {
        Arc::new(MockGeometryImpl { bounds: bounds, source: None, edges: None })
    }

    /// A geometry with the given corner points
    /// (`HeadlessGeometryWithEdgesStub`).
    fn with_edges(bounds: Rect, points: Vec<Point>) -> Arc<MockGeometryImpl> {
        Arc::new(MockGeometryImpl { bounds: bounds, source: None, edges: Some(points) })
    }

    /// `HeadlessRectangleGeometryContextStub`.
    pub fn rectangle(bounds: Rect) -> Arc<MockGeometryImpl> {
        Self::with_edges(bounds, vec![bounds.top_left(), bounds.top_right(), bounds.bottom_left(), bounds.bottom_right()])
    }

    /// `HeadlessLineGeometryContextStub`.
    pub fn line(p1: Point, p2: Point) -> Arc<MockGeometryImpl> {
        let bounds = Rect::from_points(
            Point::new(p1.x.min(p2.x), p1.y.min(p2.y)),
            Point::new(p1.x.max(p2.x), p1.y.max(p2.y)),
        );
        Self::with_edges(bounds, vec![p1, p2])
    }

    /// The points of the geometry when it is a geometry with edges
    /// (`IHeadlessGeometryWithEdges.Points`): its own points, or, for a
    /// transformed geometry, the transformed points of its source (none
    /// when the source has no edges).
    fn edge_points(&self) -> Option<Vec<Point>> {
        match &self.source {
            Some((source, transform)) => Some(
                source.edges.as_ref().map_or_else(Vec::new, |points| {
                    points.iter().map(|p| p.transform(*transform)).collect()
                }),
            ),
            None => self.edges.clone(),
        }
    }
}

/// The points of a geometry with edges, if `geometry` is one of the mock
/// geometries that has them.
fn edge_points_of(geometry: &dyn IGeometryImpl) -> Option<Vec<Point>> {
    geometry.as_any().downcast_ref::<MockGeometryImpl>().and_then(MockGeometryImpl::edge_points)
}

/// `IHeadlessGeometryWithEdges.ProjectionOnAxis`.
fn projection_on_axis(points: &[Point], axis: Vector) -> (f64, f64) {
    let mut min = f64::INFINITY;
    let mut max = f64::NEG_INFINITY;
    for point in points {
        let p = axis.x * point.x + axis.y * point.y;
        if p < min {
            min = p;
        }
        if p > max {
            max = p;
        }
    }
    (min, max)
}

/// `IHeadlessGeometryWithEdges.GetAxes`, with the reference's choice of the
/// other point of an edge (`(i + i) % Count`).
fn axes(points: &[Point]) -> Vec<Vector> {
    let count = points.len();
    (0..count)
        .map(|i| {
            let point = points[i];
            let other_point = points[(i + i) % count];
            let edge = Vector::new(point.x - other_point.x, point.y - other_point.y);
            Vector::new(-edge.y, edge.x)
        })
        .collect()
}

/// Whether a separating axis of the two point sets exists.
fn separated(a: &[Point], b: &[Point]) -> bool {
    let mut all = axes(a);
    all.extend(axes(b));
    all.into_iter().any(|axis| {
        let (min, max) = projection_on_axis(a, axis);
        let (min2, max2) = projection_on_axis(b, axis);
        max < min2 || max2 < min
    })
}

/// The intersection of the geometry with bounds `a` with the geometry with
/// bounds `b`, from the point of view of `a`: as the headless and Skia
/// geometries of the reference, `a` containing `b` means `b` is fully
/// inside `a`.
fn rect_intersection(a: Rect, b: Rect) -> IntersectionResult {
    if a.width <= 0.0 || a.height <= 0.0 || b.width <= 0.0 || b.height <= 0.0 || !a.intersects(b) {
        IntersectionResult::Empty
    } else if a.contains_rect(b) {
        IntersectionResult::FullyInside
    } else if b.contains_rect(a) {
        IntersectionResult::FullyContains
    } else {
        IntersectionResult::Intersects
    }
}

impl IGeometryImpl for MockGeometryImpl {
    fn bounds(&self) -> Rect {
        self.bounds
    }
    fn contour_length(&self) -> f64 {
        let bounds = self.bounds;
        2.0 * (bounds.width + bounds.height)
    }
    fn get_render_bounds(&self, pen: Option<&dyn IPen>) -> Rect {
        self.bounds.inflate(pen.map_or(0.0, |p| p.thickness()) / 2.0)
    }
    fn get_widened_geometry(&self, pen: &dyn IPen) -> Arc<dyn IGeometryImpl> {
        MockGeometryImpl::new(self.bounds.inflate(pen.thickness() / 2.0))
    }
    fn fill_contains(&self, point: Point) -> bool {
        self.bounds.contains(point)
    }
    fn get_fill_intersection_result(&self, geometry: &dyn IGeometryImpl) -> IntersectionResult {
        // A geometry with edges of its own (not a transformed one, which
        // uses the bounds as the base class of the reference does) tests
        // the edges of another geometry with edges.
        if let (None, Some(points)) = (&self.source, &self.edges) {
            if let Some(other) = edge_points_of(geometry) {
                if separated(points, &other) {
                    return IntersectionResult::Empty;
                }
                let bounds = self.get_render_bounds(None);
                let other_bounds = geometry.get_render_bounds(None);
                if bounds.contains_rect(other_bounds) {
                    return IntersectionResult::FullyInside;
                }
                if other_bounds.contains_rect(bounds) {
                    return IntersectionResult::FullyContains;
                }
                return IntersectionResult::Intersects;
            }
        }
        rect_intersection(self.bounds, geometry.bounds())
    }
    fn intersect(&self, geometry: &dyn IGeometryImpl) -> Option<Arc<dyn IGeometryImpl>> {
        let intersection = self.bounds.intersect(geometry.bounds());
        if intersection.width <= 0.0 || intersection.height <= 0.0 {
            None
        } else {
            Some(MockGeometryImpl::new(intersection))
        }
    }
    fn stroke_contains(&self, pen: Option<&dyn IPen>, point: Point) -> bool {
        let half = pen.map_or(0.0, |p| p.thickness()) / 2.0;
        let bounds = self.bounds;
        bounds.inflate(half).contains(point) && !bounds.deflate(half).contains_exclusive(point)
    }
    fn with_transform(&self, transform: Matrix) -> Arc<dyn ITransformedGeometryImpl> {
        // As the reference, the transform of a transformed geometry is
        // combined with the new one over the same source.
        let (source, transform) = match &self.source {
            Some((source, own)) => (source.clone(), *own * transform),
            None => (
                Arc::new(MockGeometryImpl {
                    bounds: self.bounds,
                    source: None,
                    edges: self.edges.clone(),
                }),
                transform,
            ),
        };
        Arc::new(MockGeometryImpl {
            bounds: source.bounds.transform_to_aabb(transform),
            source: Some((source, transform)),
            edges: None,
        })
    }
    fn try_get_point_at_distance(&self, _distance: f64) -> Option<Point> {
        None
    }
    fn try_get_point_and_tangent_at_distance(&self, _distance: f64) -> Option<(Point, Point)> {
        None
    }
    fn try_get_segment(&self, _start: f64, _stop: f64, _begin: bool) -> Option<Arc<dyn IGeometryImpl>> {
        None
    }
    fn as_transformed_geometry(&self) -> Option<&dyn ITransformedGeometryImpl> {
        self.source.as_ref().map(|_| self as &dyn ITransformedGeometryImpl)
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl ITransformedGeometryImpl for MockGeometryImpl {
    fn source_geometry(&self) -> Arc<dyn IGeometryImpl> {
        match &self.source {
            Some((source, _)) => source.clone() as Arc<dyn IGeometryImpl>,
            None => MockGeometryImpl::new(self.bounds),
        }
    }
    fn transform(&self) -> Matrix {
        self.source.as_ref().map_or(Matrix::IDENTITY, |(_, transform)| *transform)
    }
}

/// A stream geometry whose bounds are the bounding box of the points
/// written to it; a point is in its fill when it is in one of the triangles
/// of consecutive points. It is immutable: [`MockStreamGeometryBuilder`]
/// builds it.
pub struct MockStreamGeometryImpl {
    points: Vec<Point>,
}

impl MockStreamGeometryImpl {
    pub fn new() -> Arc<MockStreamGeometryImpl> {
        Self::with_points(Vec::new())
    }

    fn with_points(points: Vec<Point>) -> Arc<MockStreamGeometryImpl> {
        Arc::new(MockStreamGeometryImpl { points })
    }

    fn as_rect(&self) -> Arc<MockGeometryImpl> {
        let points = &self.points;
        let mut bounds: Option<Rect> = None;
        for p in points.iter() {
            let r = Rect::new(p.x, p.y, 0.0, 0.0);
            bounds = Some(match bounds {
                Some(b) => {
                    let left = b.x.min(r.x);
                    let top = b.y.min(r.y);
                    Rect::new(left, top, b.right().max(r.x) - left, b.bottom().max(r.y) - top)
                }
                None => r,
            });
        }
        MockGeometryImpl::new(bounds.unwrap_or_default())
    }
}

impl IGeometryImpl for MockStreamGeometryImpl {
    fn bounds(&self) -> Rect {
        self.as_rect().bounds()
    }
    fn contour_length(&self) -> f64 {
        self.as_rect().contour_length()
    }
    fn get_render_bounds(&self, pen: Option<&dyn IPen>) -> Rect {
        self.as_rect().get_render_bounds(pen)
    }
    fn get_widened_geometry(&self, pen: &dyn IPen) -> Arc<dyn IGeometryImpl> {
        self.as_rect().get_widened_geometry(pen)
    }
    fn fill_contains(&self, point: Point) -> bool {
        // As the headless stream geometry of the reference: the point is
        // tested against the triangles of consecutive points (the geometry
        // is assumed to be convex), with the algorithm from
        // https://www.blackpawn.com/texts/pointinpoly/default.html.
        let points = &self.points;
        let count = points.len();
        for i in 0..count {
            let a = points[i];
            let b = points[(i + 1) % count];
            let c = points[(i + 2) % count];

            let v0 = c - a;
            let v1 = b - a;
            let v2 = point - a;

            let dot00 = v0.x * v0.x + v0.y * v0.y;
            let dot01 = v0.x * v1.x + v0.y * v1.y;
            let dot02 = v0.x * v2.x + v0.y * v2.y;
            let dot11 = v1.x * v1.x + v1.y * v1.y;
            let dot12 = v1.x * v2.x + v1.y * v2.y;

            let inv_denom = 1.0 / (dot00 * dot11 - dot01 * dot01);
            let u = (dot11 * dot02 - dot01 * dot12) * inv_denom;
            let v = (dot00 * dot12 - dot01 * dot02) * inv_denom;
            if u >= 0.0 && v >= 0.0 && u + v < 1.0 {
                return true;
            }
        }
        false
    }
    fn get_fill_intersection_result(&self, geometry: &dyn IGeometryImpl) -> IntersectionResult {
        // As the headless stream geometry of the reference: against a
        // geometry with edges, a separating axis test on the points.
        if let Some(other) = edge_points_of(geometry) {
            return if separated(&self.points, &other) { IntersectionResult::Empty } else { IntersectionResult::Intersects };
        }
        self.as_rect().get_fill_intersection_result(geometry)
    }
    fn intersect(&self, geometry: &dyn IGeometryImpl) -> Option<Arc<dyn IGeometryImpl>> {
        self.as_rect().intersect(geometry)
    }
    fn stroke_contains(&self, pen: Option<&dyn IPen>, point: Point) -> bool {
        self.as_rect().stroke_contains(pen, point)
    }
    fn with_transform(&self, transform: Matrix) -> Arc<dyn ITransformedGeometryImpl> {
        self.as_rect().with_transform(transform)
    }
    fn try_get_point_at_distance(&self, _distance: f64) -> Option<Point> {
        None
    }
    fn try_get_point_and_tangent_at_distance(&self, _distance: f64) -> Option<(Point, Point)> {
        None
    }
    fn try_get_segment(&self, _start: f64, _stop: f64, _begin: bool) -> Option<Arc<dyn IGeometryImpl>> {
        None
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Builds a [`MockStreamGeometryImpl`] from the points of its source
/// geometry and the points written to it, as the headless stream geometry
/// builder of the reference.
pub struct MockStreamGeometryBuilder {
    points: Vec<Point>,
    built: bool,
}

impl MockStreamGeometryBuilder {
    /// Creates a builder that starts with the points of `source`: a stream
    /// geometry or a geometry with edges of this render interface.
    pub fn new(source: Option<&dyn IGeometryImpl>) -> MockStreamGeometryBuilder {
        let points = match source {
            None => Vec::new(),
            Some(source) => match source.as_any().downcast_ref::<MockStreamGeometryImpl>() {
                Some(stream) => stream.points.clone(),
                None => edge_points_of(source).expect("the source isn't a geometry of the mock render interface"),
            },
        };

        MockStreamGeometryBuilder { points, built: false }
    }
}

impl IGeometryContext for MockStreamGeometryBuilder {
    fn arc_to(&mut self, point: Point, _: Size, _: f64, _: bool, _: SweepDirection, _: bool) {
        self.points.push(point);
    }
    fn begin_figure(&mut self, start_point: Point, _is_filled: bool) {
        self.points.push(start_point);
    }
    fn cubic_bezier_to(&mut self, p1: Point, p2: Point, p3: Point, _is_stroked: bool) {
        self.points.extend([p1, p2, p3]);
    }
    fn quadratic_bezier_to(&mut self, p1: Point, p2: Point, _is_stroked: bool) {
        self.points.extend([p1, p2]);
    }
    fn line_to(&mut self, point: Point, _is_stroked: bool) {
        self.points.push(point);
    }
    fn end_figure(&mut self, _is_closed: bool) {}
    fn set_fill_rule(&mut self, _fill_rule: FillRule) {}
    fn dispose(&mut self) {}
}

impl IStreamGeometryBuilder for MockStreamGeometryBuilder {
    fn to_geometry(&mut self) -> Arc<dyn IGeometryImpl> {
        assert!(!self.built, "The geometry has already been built.");
        self.built = true;
        MockStreamGeometryImpl::with_points(std::mem::take(&mut self.points))
    }
}

/// A region that keeps its rectangles in a list.
#[derive(Default)]
pub struct MockRegion {
    rects: RefCell<Vec<LtrbPixelRect>>,
}

impl IPlatformRenderInterfaceRegion for MockRegion {
    fn add_rect(&self, rect: LtrbPixelRect) {
        self.rects.borrow_mut().push(rect);
    }
    fn reset(&self) {
        self.rects.borrow_mut().clear();
    }
    fn is_empty(&self) -> bool {
        self.rects.borrow().is_empty()
    }
    fn bounds(&self) -> LtrbPixelRect {
        let rects = self.rects.borrow();
        let mut iter = rects.iter();
        let Some(first) = iter.next() else { return LtrbPixelRect::default() };
        iter.fold(*first, |acc, r| acc.union(*r))
    }
    fn rects(&self) -> Vec<LtrbPixelRect> {
        self.rects.borrow().clone()
    }
    fn intersects(&self, rect: LtrbRect) -> bool {
        self.rects.borrow().iter().any(|r| {
            LtrbRect::from_rect(Rect::new(
                f64::from(r.left),
                f64::from(r.top),
                f64::from(r.width()),
                f64::from(r.height()),
            ))
            .intersects(rect)
        })
    }
    fn contains(&self, pt: Point) -> bool {
        self.rects.borrow().iter().any(|r| {
            pt.x >= f64::from(r.left) && pt.x < f64::from(r.right) && pt.y >= f64::from(r.top) && pt.y < f64::from(r.bottom)
        })
    }
    fn dispose(&self) {}
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// A render target whose drawing contexts record to a [`DrawingLog`].
pub struct MockRenderTarget {
    log: DrawingLog,
    pub properties: Cell<RenderTargetProperties>,
    pub disposed: Cell<bool>,
}

impl MockRenderTarget {
    pub fn new(log: DrawingLog) -> Rc<MockRenderTarget> {
        Rc::new(MockRenderTarget { log, properties: Cell::default(), disposed: Cell::new(false) })
    }
}

impl IRenderTarget for MockRenderTarget {
    fn properties(&self) -> RenderTargetProperties {
        self.properties.get()
    }
    fn create_drawing_context(
        &self,
        _scene_info: &RenderTargetSceneInfo,
    ) -> (Box<dyn IDrawingContextImpl>, RenderTargetDrawingContextProperties) {
        (Box::new(MockDrawingContextImpl::new(self.log.clone())), RenderTargetDrawingContextProperties::default())
    }
    fn dispose(&self) {
        self.disposed.set(true);
    }
}

/// A backend context that creates mock render targets.
pub struct MockPlatformRenderInterfaceContext {
    log: DrawingLog,
    pub is_lost: Cell<bool>,
}

impl IOptionalFeatureProvider for MockPlatformRenderInterfaceContext {
    fn try_get_feature(&self, _feature_type: TypeId) -> Option<Rc<dyn Any>> {
        None
    }
}

impl IPlatformRenderInterfaceContext for MockPlatformRenderInterfaceContext {
    fn create_render_target(&self, _surfaces: &[std::sync::Arc<dyn IPlatformRenderSurface>]) -> Rc<dyn IRenderTarget> {
        MockRenderTarget::new(self.log.clone())
    }
    fn create_offscreen_render_target(
        &self,
        pixel_size: PixelSize,
        _scaling: Vector,
        _enable_text_antialiasing: bool,
    ) -> Rc<dyn IDrawingContextLayerImpl> {
        Rc::new(MockDrawingContextLayerImpl::new(self.log.clone(), pixel_size))
    }
    fn is_lost(&self) -> bool {
        self.is_lost.get()
    }
    fn max_offscreen_render_target_pixel_size(&self) -> Option<PixelSize> {
        None
    }
    fn dispose(&self) {}
}

/// Creates the backend context of a [`MockPlatformRenderInterface`] in place
/// of the mock one: see
/// [`set_backend_context_factory`](MockPlatformRenderInterface::set_backend_context_factory).
type BackendContextFactory = Box<
    dyn Fn(&DrawingLog, Option<Rc<dyn IPlatformGraphicsContext>>) -> Rc<dyn IPlatformRenderInterfaceContext>
        + Send
        + Sync,
>;

/// A render interface for tests: rectangle-like geometries, drawing
/// contexts that record to a [`DrawingLog`], no bitmap decoding.
pub struct MockPlatformRenderInterface {
    log: DrawingLog,
    backend_context_factory: std::sync::OnceLock<BackendContextFactory>,
}

impl MockPlatformRenderInterface {
    pub fn new(log: DrawingLog) -> Rc<MockPlatformRenderInterface> {
        Rc::new(MockPlatformRenderInterface { log, backend_context_factory: std::sync::OnceLock::new() })
    }

    /// Makes `factory` create the backend contexts, for a test that watches
    /// what is done with them. It is called by the thread that creates the
    /// context, with the log of this interface and the graphics context.
    ///
    /// # Panics
    ///
    /// When a factory was set before.
    pub fn set_backend_context_factory(
        &self,
        factory: impl Fn(&DrawingLog, Option<Rc<dyn IPlatformGraphicsContext>>) -> Rc<dyn IPlatformRenderInterfaceContext>
            + Send
            + Sync
            + 'static,
    ) {
        if self.backend_context_factory.set(Box::new(factory)).is_err() {
            panic!("the backend context factory of the mock render interface is set once");
        }
    }

    /// The log every drawing context created through this interface records
    /// to.
    pub fn log(&self) -> &DrawingLog {
        &self.log
    }

    /// Registers a new mock render interface in a fresh locator scope.
    /// Dispose the returned scope to unregister it.
    pub fn install() -> (Rc<dyn IDisposable>, Rc<MockPlatformRenderInterface>) {
        let scope = FerroLocator::enter_scope();
        let render_interface = MockPlatformRenderInterface::new(DrawingLog::new());
        FerroLocator::current_mutable().bind::<dyn IPlatformRenderInterface>().to_constant(render_interface.clone());
        (scope, render_interface)
    }
}

fn unsupported<T>() -> std::io::Result<T> {
    Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "the mock render interface does not decode bitmaps"))
}

/// A platform glyph run that only knows its bounds: as wide as the glyph
/// advances, from the font's ascent to its descent around the baseline.
pub struct MockGlyphRunImpl {
    font_rendering_em_size: f64,
    baseline_origin: Point,
    bounds: Rect,
}

impl MockGlyphRunImpl {
    /// A platform glyph run with the given bounds, an em size of 12 and its
    /// baseline origin at the top left of the bounds.
    pub fn new(bounds: Rect) -> Arc<MockGlyphRunImpl> {
        Arc::new(MockGlyphRunImpl { font_rendering_em_size: 12.0, baseline_origin: bounds.position(), bounds })
    }
}

impl crate::platform::IGlyphRunImpl for MockGlyphRunImpl {
    fn font_rendering_em_size(&self) -> f64 {
        self.font_rendering_em_size
    }
    fn baseline_origin(&self) -> Point {
        self.baseline_origin
    }
    fn bounds(&self) -> Rect {
        self.bounds
    }
    fn get_intersections(&self, _lower_limit: f32, _upper_limit: f32) -> Vec<f32> {
        Vec::new()
    }
    fn dispose(&self) {}
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl IPlatformRenderInterface for MockPlatformRenderInterface {
    fn build_glyph_run_geometry(&self, glyph_run: &crate::media::GlyphRun) -> Arc<dyn IGeometryImpl> {
        MockGeometryImpl::new(glyph_run.bounds())
    }
    fn create_glyph_run(
        &self,
        glyph_typeface: &Rc<crate::media::GlyphTypeface>,
        font_rendering_em_size: f64,
        glyph_infos: &[crate::media::text_formatting::GlyphInfo],
        baseline_origin: Point,
    ) -> std::sync::Arc<dyn crate::platform::IGlyphRunImpl> {
        let metrics = glyph_typeface.metrics();
        let scale = font_rendering_em_size / metrics.design_em_height as f64;
        let width: f64 = glyph_infos.iter().map(|glyph| glyph.glyph_advance).sum();
        std::sync::Arc::new(MockGlyphRunImpl {
            font_rendering_em_size,
            baseline_origin,
            bounds: Rect::new(
                baseline_origin.x,
                baseline_origin.y + metrics.ascent as f64 * scale,
                width,
                (metrics.descent - metrics.ascent) as f64 * scale,
            ),
        })
    }
    fn create_ellipse_geometry(&self, rect: Rect) -> Arc<dyn IGeometryImpl> {
        MockGeometryImpl::new(rect)
    }
    fn create_line_geometry(&self, p1: Point, p2: Point) -> Arc<dyn IGeometryImpl> {
        MockGeometryImpl::line(p1, p2)
    }
    fn create_rectangle_geometry(&self, rect: Rect) -> Arc<dyn IGeometryImpl> {
        MockGeometryImpl::rectangle(rect)
    }
    fn create_stream_geometry_builder(&self, source: Option<&dyn IGeometryImpl>) -> Box<dyn IStreamGeometryBuilder> {
        Box::new(MockStreamGeometryBuilder::new(source))
    }
    fn create_geometry_group(&self, _fill_rule: FillRule, children: &[Arc<dyn IGeometryImpl>]) -> Arc<dyn IGeometryImpl> {
        let bounds = children.iter().fold(None, |acc, g| Rect::union_optional(acc, Some(g.bounds())));
        MockGeometryImpl::new(bounds.unwrap_or_default())
    }
    fn create_combined_geometry(
        &self,
        _combine_mode: GeometryCombineMode,
        g1: Arc<dyn IGeometryImpl>,
        g2: Arc<dyn IGeometryImpl>,
    ) -> Arc<dyn IGeometryImpl> {
        MockGeometryImpl::new(g1.bounds().union(g2.bounds()))
    }
    fn create_render_target_bitmap(&self, size: PixelSize, dpi: Vector) -> std::sync::Arc<dyn IRenderTargetBitmapImpl> {
        std::sync::Arc::new(MockRenderTargetBitmapImpl::new(self.log.clone(), size, dpi))
    }
    fn create_writeable_bitmap(
        &self,
        _size: PixelSize,
        _dpi: Vector,
        _format: PixelFormat,
        _alpha_format: AlphaFormat,
    ) -> std::sync::Arc<dyn IWriteableBitmapImpl> {
        panic!("the mock render interface does not create writeable bitmaps")
    }
    fn load_bitmap_from_file(&self, _file_name: &str) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>> {
        unsupported()
    }
    fn load_bitmap(&self, _stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>> {
        unsupported()
    }
    fn load_writeable_bitmap_to_width(
        &self,
        _stream: &mut dyn Read,
        _width: i32,
        _interpolation_mode: BitmapInterpolationMode,
    ) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>> {
        unsupported()
    }
    fn load_writeable_bitmap_to_height(
        &self,
        _stream: &mut dyn Read,
        _height: i32,
        _interpolation_mode: BitmapInterpolationMode,
    ) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>> {
        unsupported()
    }
    fn load_writeable_bitmap_from_file(&self, _file_name: &str) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>> {
        unsupported()
    }
    fn load_writeable_bitmap(&self, _stream: &mut dyn Read) -> std::io::Result<std::sync::Arc<dyn IWriteableBitmapImpl>> {
        unsupported()
    }
    fn load_bitmap_to_width(
        &self,
        _stream: &mut dyn Read,
        _width: i32,
        _interpolation_mode: BitmapInterpolationMode,
    ) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>> {
        unsupported()
    }
    fn load_bitmap_to_height(
        &self,
        _stream: &mut dyn Read,
        _height: i32,
        _interpolation_mode: BitmapInterpolationMode,
    ) -> std::io::Result<std::sync::Arc<crate::platform::SharedBitmapImpl>> {
        unsupported()
    }
    fn resize_bitmap(
        &self,
        _bitmap_impl: &dyn IBitmapImpl,
        destination_size: PixelSize,
        _interpolation_mode: BitmapInterpolationMode,
    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl> {
        std::sync::Arc::new(MockDrawingContextLayerImpl::new(self.log.clone(), destination_size))
    }
    fn load_bitmap_from_pixels(
        &self,
        _format: PixelFormat,
        _alpha_format: AlphaFormat,
        _data: &[u8],
        size: PixelSize,
        _dpi: Vector,
        _stride: i32,
    ) -> std::sync::Arc<crate::platform::SharedBitmapImpl> {
        std::sync::Arc::new(MockDrawingContextLayerImpl::new(self.log.clone(), size))
    }
    fn create_backend_context(
        &self,
        graphics_api_context: Option<Rc<dyn IPlatformGraphicsContext>>,
    ) -> Rc<dyn IPlatformRenderInterfaceContext> {
        if let Some(factory) = self.backend_context_factory.get() {
            return factory(&self.log, graphics_api_context);
        }
        Rc::new(MockPlatformRenderInterfaceContext { log: self.log.clone(), is_lost: Cell::new(false) })
    }
    fn supports_individual_round_rects(&self) -> bool {
        false
    }
    fn default_alpha_format(&self) -> AlphaFormat {
        AlphaFormat::Premul
    }
    fn default_pixel_format(&self) -> PixelFormat {
        PixelFormats::RGBA8888
    }
    fn is_supported_bitmap_pixel_format(&self, _format: PixelFormat) -> bool {
        true
    }
    fn supports_regions(&self) -> bool {
        true
    }
    fn create_region(&self) -> Rc<dyn IPlatformRenderInterfaceRegion> {
        Rc::new(MockRegion::default())
    }
}