carnelian/render/generic/forma/
path.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::render::generic::forma::Forma;
6use crate::render::generic::PathBuilder;
7use crate::Point;
8
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct FormaPath {
11    pub(crate) path: forma::Path,
12}
13
14fn to_forma_point(point: Point) -> forma::Point {
15    forma::Point::new(point.x, point.y)
16}
17
18#[derive(Debug)]
19pub struct FormaPathBuilder {
20    builder: forma::PathBuilder,
21}
22
23impl FormaPathBuilder {
24    pub(crate) fn new() -> Self {
25        Self { builder: forma::PathBuilder::new() }
26    }
27}
28
29impl PathBuilder<Forma> for FormaPathBuilder {
30    fn move_to(&mut self, point: Point) -> &mut Self {
31        self.builder.move_to(to_forma_point(point));
32        self
33    }
34
35    fn line_to(&mut self, point: Point) -> &mut Self {
36        self.builder.line_to(to_forma_point(point));
37        self
38    }
39
40    fn quad_to(&mut self, p1: Point, p2: Point) -> &mut Self {
41        self.builder.quad_to(to_forma_point(p1), to_forma_point(p2));
42        self
43    }
44
45    fn cubic_to(&mut self, p1: Point, p2: Point, p3: Point) -> &mut Self {
46        self.builder.cubic_to(to_forma_point(p1), to_forma_point(p2), to_forma_point(p3));
47        self
48    }
49
50    fn rat_quad_to(&mut self, p1: Point, p2: Point, w: f32) -> &mut Self {
51        self.builder.rat_quad_to(to_forma_point(p1), to_forma_point(p2), w);
52        self
53    }
54
55    fn rat_cubic_to(&mut self, p1: Point, p2: Point, p3: Point, w1: f32, w2: f32) -> &mut Self {
56        self.builder.rat_cubic_to(
57            to_forma_point(p1),
58            to_forma_point(p2),
59            to_forma_point(p3),
60            w1,
61            w2,
62        );
63        self
64    }
65
66    fn build(mut self) -> FormaPath {
67        FormaPath { path: self.builder.build() }
68    }
69}