carnelian/render/generic/forma/
path.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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::render::generic::forma::Forma;
use crate::render::generic::PathBuilder;
use crate::Point;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FormaPath {
    pub(crate) path: forma::Path,
}

fn to_forma_point(point: Point) -> forma::Point {
    forma::Point::new(point.x, point.y)
}

#[derive(Debug)]
pub struct FormaPathBuilder {
    builder: forma::PathBuilder,
}

impl FormaPathBuilder {
    pub(crate) fn new() -> Self {
        Self { builder: forma::PathBuilder::new() }
    }
}

impl PathBuilder<Forma> for FormaPathBuilder {
    fn move_to(&mut self, point: Point) -> &mut Self {
        self.builder.move_to(to_forma_point(point));
        self
    }

    fn line_to(&mut self, point: Point) -> &mut Self {
        self.builder.line_to(to_forma_point(point));
        self
    }

    fn quad_to(&mut self, p1: Point, p2: Point) -> &mut Self {
        self.builder.quad_to(to_forma_point(p1), to_forma_point(p2));
        self
    }

    fn cubic_to(&mut self, p1: Point, p2: Point, p3: Point) -> &mut Self {
        self.builder.cubic_to(to_forma_point(p1), to_forma_point(p2), to_forma_point(p3));
        self
    }

    fn rat_quad_to(&mut self, p1: Point, p2: Point, w: f32) -> &mut Self {
        self.builder.rat_quad_to(to_forma_point(p1), to_forma_point(p2), w);
        self
    }

    fn rat_cubic_to(&mut self, p1: Point, p2: Point, p3: Point, w1: f32, w2: f32) -> &mut Self {
        self.builder.rat_cubic_to(
            to_forma_point(p1),
            to_forma_point(p2),
            to_forma_point(p3),
            w1,
            w2,
        );
        self
    }

    fn build(mut self) -> FormaPath {
        FormaPath { path: self.builder.build() }
    }
}