peniko/style.rs
1// Copyright 2022 the Peniko Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use kurbo::Stroke;
5
6/// Describes the rule that determines the interior portion of a shape.
7///
8/// This is only relevant for self-intersecting paths (e.g. a hourglass shape).
9/// For non-self-intersecting paths, both rules produce the same result.
10#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[repr(u8)]
13pub enum Fill {
14 /// Non-zero fill rule.
15 ///
16 /// All regions where the winding number of the path is not zero will be filled.
17 /// This is generally more correct, but can be more expensive to implement in renderers.
18 /// This matches the default behavior of the web canvas, and is the default value.
19 #[default]
20 NonZero = 0,
21 /// Even-odd fill rule.
22 ///
23 /// All regions where the winding number of the path is odd will be filled.
24 /// The most common use case for this rule is as an optimisation when the
25 /// paths are known to not be self-intersecting. There may also be cases where
26 /// this rendering is desired. <!-- TODO: Are there? -->
27 /// This can be implemented more efficiently than even-odd, as the winding
28 /// number state can be stored in only one bit (and so the winding numbers for
29 /// several pixels can be packed extremely efficiently).
30 EvenOdd = 1,
31 // NOTICE: If a new value is added, be sure to modify `MAX_VALUE` in the `bytemuck::Contiguous` impl.
32}
33
34/// Describes draw style-- either a [fill](Fill) or [stroke](Stroke).
35///
36/// See also [`StyleRef`] which can be used to avoid allocations.
37#[derive(Clone, Debug, PartialEq)]
38#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
39pub enum Style {
40 /// Filled draw operation.
41 Fill(Fill),
42 /// Stroked draw operation.
43 Stroke(Stroke),
44}
45
46impl From<Fill> for Style {
47 fn from(fill: Fill) -> Self {
48 Self::Fill(fill)
49 }
50}
51
52impl From<Stroke> for Style {
53 fn from(stroke: Stroke) -> Self {
54 Self::Stroke(stroke)
55 }
56}
57
58/// Reference to a [draw style](Style).
59///
60/// This is useful for methods that would like to accept draw styles by reference. Defining
61/// the type as `impl<Into<StyleRef>>` allows accepting types like `&Stroke` or `Fill`
62/// directly without cloning or allocating.
63#[derive(Debug, Copy, Clone)]
64pub enum StyleRef<'a> {
65 /// Filled draw operation.
66 Fill(Fill),
67 /// Stroked draw operation.
68 Stroke(&'a Stroke),
69}
70
71impl StyleRef<'_> {
72 /// Converts the reference to an owned draw.
73 #[must_use]
74 pub fn to_owned(&self) -> Style {
75 match self {
76 Self::Fill(fill) => Style::Fill(*fill),
77 Self::Stroke(stroke) => Style::Stroke((*stroke).clone()),
78 }
79 }
80}
81
82impl From<Fill> for StyleRef<'_> {
83 fn from(fill: Fill) -> Self {
84 Self::Fill(fill)
85 }
86}
87
88impl<'a> From<&'a Stroke> for StyleRef<'a> {
89 fn from(stroke: &'a Stroke) -> Self {
90 Self::Stroke(stroke)
91 }
92}
93
94impl<'a> From<&'a Style> for StyleRef<'a> {
95 fn from(draw: &'a Style) -> Self {
96 match draw {
97 Style::Fill(fill) => Self::Fill(*fill),
98 Style::Stroke(stroke) => Self::Stroke(stroke),
99 }
100 }
101}