og_image_writer/
style.rs

1pub use image::{Rgb, Rgba as ImageRgba};
2use std::marker::Copy;
3#[cfg(all(target_arch = "wasm32", feature = "web"))]
4use wasm_bindgen::prelude::*;
5
6#[cfg(all(target_arch = "wasm32", feature = "web"))]
7#[wasm_bindgen]
8#[derive(Debug, Copy, Clone)]
9pub enum KernSetting {
10    Normal,
11    Metrics,
12    Optical,
13}
14
15#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
16#[derive(Debug, Copy, Clone)]
17pub enum KernSetting {
18    Normal,
19    Metrics,
20    Optical,
21}
22
23#[derive(Debug, Copy, Clone)]
24pub struct Rgba(pub [u8; 4]);
25
26impl Rgba {
27    pub(super) fn as_image_rgba(&self) -> ImageRgba<u8> {
28        ImageRgba(self.0)
29    }
30}
31
32#[cfg(all(target_arch = "wasm32", feature = "web"))]
33#[wasm_bindgen]
34#[derive(Debug, Copy, Clone)]
35pub enum WordBreak {
36    Normal,
37    BreakAll,
38}
39
40#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
41#[derive(Debug, Copy, Clone)]
42pub enum WordBreak {
43    Normal,
44    BreakAll,
45}
46
47#[cfg(all(target_arch = "wasm32", feature = "web"))]
48#[wasm_bindgen]
49#[derive(Debug, Copy, Clone)]
50pub enum WhiteSpace {
51    Normal,
52    PreLine,
53}
54
55#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
56#[derive(Debug, Copy, Clone)]
57pub enum WhiteSpace {
58    Normal,
59    PreLine,
60}
61
62impl WhiteSpace {
63    pub(crate) fn is_pre(&self) -> bool {
64        match self {
65            Self::Normal => false,
66            Self::PreLine => true,
67        }
68    }
69}
70
71#[derive(Debug, Default, Clone, Copy)]
72pub struct Margin(pub i32, pub i32, pub i32, pub i32);
73
74#[derive(Debug, Default, Clone, Copy)]
75pub struct BorderRadius(pub u32, pub u32, pub u32, pub u32);
76
77/// Adjust the horizontal position.
78#[cfg(all(target_arch = "wasm32", feature = "web"))]
79#[wasm_bindgen]
80#[derive(Debug, Copy, Clone)]
81pub enum AlignItems {
82    Start,
83    Center,
84    End,
85}
86
87/// Adjust the horizontal position.
88#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
89#[derive(Debug, Copy, Clone)]
90pub enum AlignItems {
91    Start,
92    Center,
93    End,
94}
95
96/// Adjust the vertical position.
97#[cfg(all(target_arch = "wasm32", feature = "web"))]
98#[wasm_bindgen]
99#[derive(Debug, Copy, Clone)]
100pub enum JustifyContent {
101    Start,
102    Center,
103    End,
104}
105
106#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
107/// Adjust the vertical position.
108#[derive(Debug, Copy, Clone)]
109pub enum JustifyContent {
110    Start,
111    Center,
112    End,
113}
114
115/// Adjust the text horizontal position.
116#[cfg(all(target_arch = "wasm32", feature = "web"))]
117#[wasm_bindgen]
118#[derive(Debug, Copy, Clone)]
119pub enum TextAlign {
120    Start,
121    Center,
122    End,
123}
124
125#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
126#[derive(Debug, Copy, Clone)]
127pub enum TextAlign {
128    Start,
129    Center,
130    End,
131}
132
133#[derive(Debug)]
134pub enum TextOverflow {
135    Clip,
136    Ellipsis,
137    Content(String),
138}
139
140#[cfg(all(target_arch = "wasm32", feature = "web"))]
141#[wasm_bindgen]
142#[derive(Debug, Copy, Clone)]
143pub enum Position {
144    Static,
145    Absolute,
146}
147
148#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
149#[derive(Debug, Copy, Clone)]
150pub enum Position {
151    Static,
152    Absolute,
153}
154
155#[cfg(all(target_arch = "wasm32", feature = "web"))]
156#[wasm_bindgen]
157#[derive(Debug, Copy, Clone)]
158pub enum FlexDirection {
159    Column,
160    Row,
161}
162
163#[cfg(not(all(target_arch = "wasm32", feature = "web")))]
164#[derive(Debug, Copy, Clone)]
165pub enum FlexDirection {
166    Column,
167    Row,
168}
169
170/// Style is used by `text` or `img` element.
171/// Text element is `inline-block`, so you can adjust text position by using `text_align`.
172#[derive(Debug)]
173pub struct Style {
174    pub margin: Margin,
175    /// For Text element
176    pub line_height: f32,
177    /// For Text element
178    pub font_size: f32,
179    /// For Text element
180    pub letter_spacing: i32,
181    /// For Text element
182    pub kern_setting: KernSetting,
183    /// For Text element
184    pub word_break: WordBreak,
185    /// For Text element
186    pub white_space: WhiteSpace,
187    /// For Text element
188    pub color: Rgba,
189    /// For Text element
190    pub text_align: TextAlign,
191    /// For Text element
192    pub max_height: Option<u32>,
193    /// For Text element
194    pub max_width: Option<u32>,
195    /// For Text element
196    /// This property support multiline.
197    pub text_overflow: TextOverflow,
198    pub position: Position,
199    pub top: Option<i32>,
200    pub right: Option<i32>,
201    pub bottom: Option<i32>,
202    pub left: Option<i32>,
203    /// For Img element
204    pub border_radius: BorderRadius,
205}
206
207impl Default for Style {
208    fn default() -> Self {
209        Style {
210            margin: Margin::default(),
211            line_height: 1.5,
212            font_size: 30.,
213            letter_spacing: 0,
214            kern_setting: KernSetting::Normal,
215            word_break: WordBreak::Normal,
216            white_space: WhiteSpace::Normal,
217            color: Rgba([0, 0, 0, 255]),
218            text_align: TextAlign::Start,
219            max_height: None,
220            max_width: None,
221            text_overflow: TextOverflow::Clip,
222            position: Position::Static,
223            top: None,
224            right: None,
225            bottom: None,
226            left: None,
227            border_radius: BorderRadius::default(),
228        }
229    }
230}
231
232pub enum LogicalFlexRowPosition {
233    Start,
234    Center,
235    End,
236}
237
238/// Window is act like flexbox. And default direction is `column`.
239/// You can adjust position with `align_item` and `justify_content`.
240/// You must pass `background_image` or `background_color` for constructing surface.
241#[derive(Debug)]
242pub struct WindowStyle {
243    pub height: u32,
244    pub width: u32,
245    pub background_color: Option<Rgba>,
246    pub align_items: AlignItems,
247    pub justify_content: JustifyContent,
248    /// This controls the direction in which the children of a node are laid out.
249    pub flex_direction: FlexDirection,
250}
251
252impl WindowStyle {
253    pub fn logical_flex_row_position(&self) -> LogicalFlexRowPosition {
254        match &self.flex_direction {
255            FlexDirection::Column => match self.align_items {
256                AlignItems::Start => LogicalFlexRowPosition::Start,
257                AlignItems::Center => LogicalFlexRowPosition::Center,
258                AlignItems::End => LogicalFlexRowPosition::End,
259            },
260            FlexDirection::Row => match self.justify_content {
261                JustifyContent::Start => LogicalFlexRowPosition::Start,
262                JustifyContent::Center => LogicalFlexRowPosition::Center,
263                JustifyContent::End => LogicalFlexRowPosition::End,
264            },
265        }
266    }
267}
268
269impl Default for WindowStyle {
270    fn default() -> Self {
271        WindowStyle {
272            height: 0,
273            width: 0,
274            background_color: None,
275            align_items: AlignItems::Start,
276            justify_content: JustifyContent::Start,
277            flex_direction: FlexDirection::Column,
278        }
279    }
280}