Skip to main content

mln_engine/text/
atlas.rs

1//! Where images and glyphs are in an atlas.
2
3use mln_style::image::{ImageContent, ImageStretches, Impl, TextFit};
4
5/// A rectangle of an atlas, in pixels.
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, mln_wire::Wire)]
7pub struct RectU16 {
8    pub x: u16,
9    pub y: u16,
10    pub w: u16,
11    pub h: u16,
12}
13
14/// An image's place in an atlas.
15#[derive(Clone, Debug, PartialEq, mln_wire::Wire)]
16pub struct ImagePosition {
17    pub padded_rect: RectU16,
18    pub pixel_ratio: f32,
19    pub sdf: bool,
20    pub stretch_x: ImageStretches,
21    pub stretch_y: ImageStretches,
22    pub content: Option<ImageContent>,
23    pub(crate) text_fit_width: Option<TextFit>,
24    pub(crate) text_fit_height: Option<TextFit>,
25}
26
27impl ImagePosition {
28    /// The texels of padding around an image in its atlas.
29    pub const PADDING: u16 = 1;
30
31    /// The position of `image` at `rect` of its atlas, padding included.
32    #[must_use]
33    pub fn new(rect: RectU16, image: &Impl) -> Self {
34        ImagePosition {
35            padded_rect: rect,
36            pixel_ratio: image.pixel_ratio,
37            sdf: image.sdf,
38            stretch_x: image.stretch_x.clone(),
39            stretch_y: image.stretch_y.clone(),
40            content: image.content,
41            text_fit_width: image.text_fit_width,
42            text_fit_height: image.text_fit_height,
43        }
44    }
45
46    /// The top-left corner inside the padding; the sums are computed in `i32`
47    /// and narrowed to `u16`, as in C++.
48    #[must_use]
49    pub fn tl(&self) -> [u16; 2] {
50        [
51            (i32::from(self.padded_rect.x) + i32::from(Self::PADDING)) as u16,
52            (i32::from(self.padded_rect.y) + i32::from(Self::PADDING)) as u16,
53        ]
54    }
55
56    /// The bottom-right corner inside the padding.
57    #[must_use]
58    pub fn br(&self) -> [u16; 2] {
59        [
60            (i32::from(self.padded_rect.x) + i32::from(self.padded_rect.w) - i32::from(Self::PADDING)) as u16,
61            (i32::from(self.padded_rect.y) + i32::from(self.padded_rect.h) - i32::from(Self::PADDING)) as u16,
62        ]
63    }
64
65    /// The top-left and the bottom-right corners inside the padding.
66    #[must_use]
67    pub fn tlbr(&self) -> [u16; 4] {
68        let tl = self.tl();
69        let br = self.br();
70        [tl[0], tl[1], br[0], br[1]]
71    }
72
73    /// The image's size in pixels: its texels inside the padding, over its
74    /// pixel ratio.
75    #[must_use]
76    pub fn display_size(&self) -> [f32; 2] {
77        [
78            (i32::from(self.padded_rect.w) - i32::from(Self::PADDING) * 2) as f32 / self.pixel_ratio,
79            (i32::from(self.padded_rect.h) - i32::from(Self::PADDING) * 2) as f32 / self.pixel_ratio,
80        ]
81    }
82}
83
84/// The places of the images of a layout, by id.
85pub type ImagePositions = mln_style::hash::HashMap<String, ImagePosition>;