Skip to main content

img_gen_spec/validators/layers/
icon.rs

1use super::{ColorKind, PreserveAspect};
2
3#[cfg(feature = "pyo3")]
4use pyo3::prelude::*;
5
6use serde::{Deserialize, Serialize};
7
8/// An attribute to describe a [`Layer`](struct@crate::Layer)'s [`Icon`].
9#[cfg_attr(
10    feature = "pyo3",
11    pyclass(module = "img_gen", get_all, set_all, from_py_object)
12)]
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct Icon {
15    /// A path to an image file.
16    ///
17    /// If the given image path has no file extension, then
18    /// it will be treated as an SVG image.
19    ///
20    /// This also supports built-in SVG icons from the following icon packs:
21    ///
22    /// - Material Design Icons (``material/{icon_slug}``)
23    /// - Simple Icons (``simple/{icon_slug}``)
24    /// - Octicons (``octicons/{icon_slug}``)
25    /// - FontAwesome Free (``fontawesome/<brands|solid|regular>/{icon_slug}``)
26    ///
27    /// Otherwise, the image file's path is resolved via a search through the list of
28    /// ``external_resource_paths`` provided to the ``Generator`` (in `img_gen_renderer` crate),
29    /// which defaults to the current working directory if unspecified or an empty list.
30    pub image: String,
31    /// A color used to replace the [`Icon::image`]'s original coloring.
32    #[serde(
33        alias = "linear_gradient",
34        alias = "radial_gradient",
35        alias = "conical_gradient",
36        alias = "linear-gradient",
37        alias = "radial-gradient",
38        alias = "conical-gradient"
39    )]
40    pub color: Option<ColorKind>,
41    /// This controls how the original image is rendered into the layer.
42    ///
43    /// The default is to preserve the original image's width and height
44    /// ([`PreserveAspect::On`]).
45    #[serde(default = "Icon::default_preserve_aspect")]
46    pub preserve_aspect: PreserveAspect,
47}
48
49impl Icon {
50    const fn default_preserve_aspect() -> PreserveAspect {
51        PreserveAspect::On
52    }
53}