pride_overlay/flags/
svg.rs

1#[cfg(target_arch = "wasm32")]
2use wasm_bindgen::prelude::*;
3
4/// How an SVG image should be scaled to fit a given area.
5#[derive(Clone, Copy)]
6#[cfg_attr(target_arch = "wasm32", derive(Serialize, Deserialize))]
7#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
8pub enum SvgScaleMode {
9    /// Fit the entire SVG within the destination bounds while preserving
10    /// its aspect ratio.  
11    ///  
12    /// This mode may leave empty space (letterboxing) if the aspect ratios differ.
13    Contain,
14
15    /// Fill the entire destination area while preserving the aspect ratio,
16    /// cropping parts of the SVG if necessary.  
17    Cover,
18
19    /// Stretch the SVG to exactly fill the destination, ignoring its
20    /// intrinsic aspect ratio.  
21    ///  
22    /// This can cause distortion, but ensures there is no empty space.
23    Stretch,
24
25    /// Render the SVG at its intrinsic size without applying any scaling.
26    None,
27}
28
29/// Get information about an SVG.
30pub trait SvgData {
31    fn data(&self) -> &[u8];
32    fn scale(&self) -> &SvgScaleMode;
33}
34
35/// An SVG image.
36#[derive(bon::Builder, Clone, Copy)]
37#[builder(
38    const,
39    builder_type(doc {
40        /// Builder for the [Svg] struct.
41    })
42)]
43pub struct Svg<'a> {
44    /// The raw SVG data.
45    #[builder(start_fn)]
46    data: &'a [u8],
47    /// How the SVG should be scaled when rendered.
48    #[builder(default = SvgScaleMode::Contain)]
49    scale: SvgScaleMode,
50}
51
52impl<'a> super::SvgData for Svg<'a> {
53    fn data(&self) -> &[u8] {
54        self.data
55    }
56
57    fn scale(&self) -> &SvgScaleMode {
58        &self.scale
59    }
60}