lichtung_plot/output/
format.rs1use super::{ExportRegion, PixelOptions, PngCompression, PngOptions, PngSizing};
4use crate::style::OpaqueColor;
5use crate::{Error, Result};
6mod recipe;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub(crate) enum Encoding {
10 Svg,
11 Png,
12}
13
14#[derive(Debug, Clone, PartialEq)]
17#[must_use]
18pub struct Format {
19 pub(crate) encoding: Encoding,
20 preserve_text: Option<bool>,
21 sizing: Option<PngSizing>,
22 compression: Option<PngCompression>,
23 pub(crate) region: ExportRegion,
24 pub(crate) transparent: bool,
25 pub(crate) matte: Option<OpaqueColor>,
26}
27
28impl Format {
29 pub fn from_path(path: impl AsRef<std::path::Path>) -> Result<Self> {
32 let extension = path.as_ref().extension().and_then(std::ffi::OsStr::to_str);
33 match extension {
34 Some(extension) if extension.eq_ignore_ascii_case("svg") => Ok(Self::svg()),
35 Some(extension) if extension.eq_ignore_ascii_case("png") => Ok(Self::png()),
36 _ => Err(Error::Data(
37 "image output requires an explicit device or an SVG/PNG filename".into(),
38 )),
39 }
40 }
41 pub const fn svg() -> Self {
43 Self {
44 encoding: Encoding::Svg,
45 preserve_text: None,
46 sizing: None,
47 compression: None,
48 region: ExportRegion::full(),
49 transparent: false,
50 matte: None,
51 }
52 }
53
54 pub const fn png() -> Self {
56 let mut format = Self::svg();
57 format.encoding = Encoding::Png;
58 format
59 }
60
61 pub const fn preserve_text(mut self, preserve: bool) -> Self {
63 self.preserve_text = Some(preserve);
64 self
65 }
66
67 pub const fn sizing(mut self, sizing: PngSizing) -> Self {
69 self.sizing = Some(sizing);
70 self
71 }
72
73 pub const fn compression(mut self, compression: PngCompression) -> Self {
75 self.compression = Some(compression);
76 self
77 }
78
79 pub fn region(mut self, region: ExportRegion) -> Self {
81 self.region = region;
82 self
83 }
84
85 pub const fn transparent(mut self, transparent: bool) -> Self {
87 self.transparent = transparent;
88 self
89 }
90
91 pub const fn matte(mut self, matte: OpaqueColor) -> Self {
93 self.matte = Some(matte);
94 self
95 }
96
97 pub(crate) fn pixel_options(&self, default: PngSizing) -> PixelOptions {
98 let mut options = PixelOptions::default()
99 .sizing(self.sizing.unwrap_or(default))
100 .region(self.region.clone())
101 .transparent(self.transparent);
102 if let Some(matte) = self.matte {
103 options = options.matte(matte);
104 }
105 options
106 }
107
108 pub const fn suffix(&self) -> &'static str {
110 match self.encoding {
111 Encoding::Svg => ".svg",
112 Encoding::Png => ".png",
113 }
114 }
115
116 pub const fn mime_type(&self) -> &'static str {
118 match self.encoding {
119 Encoding::Svg => "image/svg+xml",
120 Encoding::Png => "image/png",
121 }
122 }
123
124 pub fn validate(&self) -> Result<()> {
126 match self.encoding {
127 Encoding::Svg if self.sizing.is_some() || self.compression.is_some() => Err(
128 Error::Data("PNG sizing and compression require Format::png()".into()),
129 ),
130 Encoding::Png if self.preserve_text.is_some() => {
131 Err(Error::Data("preserve_text requires Format::svg()".into()))
132 }
133 _ => Ok(()),
134 }
135 }
136
137 pub(crate) fn retains_text(&self) -> bool {
138 self.preserve_text.unwrap_or(false)
139 }
140
141 pub(crate) fn png_options(&self, default: PngSizing) -> PngOptions {
142 PngOptions {
143 sizing: self.sizing.unwrap_or(default),
144 compression: self.compression.unwrap_or_default(),
145 }
146 }
147}
148
149impl From<PngOptions> for Format {
150 fn from(options: PngOptions) -> Self {
151 Self::png()
152 .sizing(options.sizing)
153 .compression(options.compression)
154 }
155}