Skip to main content

i_slint_compiler/
embedded_resources.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4#[cfg(feature = "software-renderer")]
5pub use resvg::tiny_skia::IntRect as Rect;
6
7#[derive(Debug, Clone, Copy, Default)]
8pub struct Size {
9    pub width: u32,
10    pub height: u32,
11}
12
13#[derive(Clone, Copy, Debug, strum::Display)]
14pub enum PixelFormat {
15    // 24 bit RGB
16    Rgb,
17    // 32 bit RGBA
18    Rgba,
19    // 32 bit RGBA, but the RGB values are pre-multiplied by the alpha
20    RgbaPremultiplied,
21    // 8bit alpha map with a given color
22    AlphaMap([u8; 3]),
23}
24
25#[cfg(feature = "software-renderer")]
26#[derive(Debug, Clone)]
27pub struct Texture {
28    pub total_size: Size,
29    pub original_size: Size,
30    pub rect: Rect,
31    pub data: Vec<u8>,
32    pub format: PixelFormat,
33}
34
35#[cfg(feature = "software-renderer")]
36impl Texture {
37    pub fn new_empty() -> Self {
38        Self {
39            total_size: Size::default(),
40            original_size: Size::default(),
41            rect: Rect::from_xywh(0, 0, 1, 1).unwrap(),
42            data: vec![0, 0, 0, 0],
43            format: PixelFormat::Rgba,
44        }
45    }
46}
47
48#[cfg(feature = "software-renderer")]
49#[derive(Debug, Clone, Default)]
50pub struct BitmapGlyph {
51    pub x: i16,
52    pub y: i16,
53    pub width: i16,
54    pub height: i16,
55    pub x_advance: i16,
56    /// 8bit alpha map or SDF if `BitMapGlyphs`'s `sdf` is `true`.
57    pub data: Vec<u8>,
58}
59
60#[cfg(feature = "software-renderer")]
61#[derive(Debug, Clone)]
62pub struct BitmapGlyphs {
63    pub pixel_size: i16,
64    pub glyph_data: Vec<BitmapGlyph>,
65}
66
67#[cfg(feature = "software-renderer")]
68#[derive(Debug, Clone)]
69pub struct CharacterMapEntry {
70    pub code_point: char,
71    pub glyph_index: u16,
72}
73
74#[cfg(feature = "software-renderer")]
75#[derive(Debug, Clone)]
76pub struct BitmapFont {
77    pub family_name: String,
78    /// map of available glyphs, sorted by char
79    pub character_map: Vec<CharacterMapEntry>,
80    pub units_per_em: f32,
81    pub ascent: f32,
82    pub descent: f32,
83    pub x_height: f32,
84    pub cap_height: f32,
85    pub glyphs: Vec<BitmapGlyphs>,
86    pub weight: u16,
87    pub italic: bool,
88    /// true when the font is represented as a signed distance field
89    pub sdf: bool,
90}
91
92#[derive(Debug, Clone)]
93pub enum EmbeddedResourcesKind {
94    /// Only List the resource, do not actually embed it
95    ListOnly,
96    /// Just put the file content as a resource
97    FileData,
98    /// Encoded payload from a data URI (bytes, extension)
99    DataUriPayload(Vec<u8>, String),
100    /// The data has been processed in a texture
101    #[cfg(feature = "software-renderer")]
102    TextureData(Texture),
103    /// A set of pre-rendered glyphs of a TrueType font
104    #[cfg(feature = "software-renderer")]
105    BitmapFontData(BitmapFont),
106}
107
108#[derive(Debug, Clone)]
109pub struct EmbeddedResources {
110    /// Path on disk of the resource, or `None` for in-memory payloads such as data URIs.
111    pub path: Option<smol_str::SmolStr>,
112
113    pub kind: EmbeddedResourcesKind,
114}
115
116/// Index of an [`EmbeddedResources`] entry in [`crate::object_tree::Document::embedded_file_resources`].
117#[derive(
118    Debug,
119    Clone,
120    Copy,
121    Hash,
122    PartialEq,
123    Eq,
124    derive_more::Into,
125    derive_more::From,
126    derive_more::Display,
127)]
128pub struct EmbeddedResourcesIdx(pub usize);