1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
use scene::params::{ParamValue, ParamValues};
pub const MIN_FONT_SIZE: f64 = 5.0;
pub const MAX_FONT_SIZE: f64 = 300.0;
pub const DEFAULT_TEXT_COLOR: &str = "#000000";
pub const FONT_SIZE_SCALE_REFERENCE: f64 = 90.0;
pub const CORNER_RADIUS_MIN: f64 = 0.0;
pub const CORNER_RADIUS_MAX: f64 = 100.0;
pub const DEFAULT_LETTER_SPACING: f64 = 0.0;
pub const DEFAULT_LINE_HEIGHT: f64 = 1.2;
pub const DEFAULT_BACKGROUND_ENABLED: bool = false;
pub const DEFAULT_BACKGROUND_COLOR: &str = "#000000";
pub const DEFAULT_BACKGROUND_CORNER_RADIUS: f64 = 0.0;
pub const DEFAULT_BACKGROUND_PADDING_X: f64 = 30.0;
pub const DEFAULT_BACKGROUND_PADDING_Y: f64 = 42.0;
pub const DEFAULT_BACKGROUND_OFFSET_X: f64 = 0.0;
pub const DEFAULT_BACKGROUND_OFFSET_Y: f64 = 0.0;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextAlign {
Left,
#[default]
Center,
Right,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FontWeight {
#[default]
Normal,
Bold,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FontStyle {
#[default]
Normal,
Italic,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextDecoration {
#[default]
None,
Underline,
LineThrough,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TextLayoutParams {
pub content: String,
pub font_size: f64,
pub font_family: String,
pub font_weight: FontWeight,
pub font_style: FontStyle,
pub text_align: TextAlign,
pub text_decoration: TextDecoration,
pub letter_spacing: f64,
pub line_height: f64,
}
impl Default for TextLayoutParams {
fn default() -> Self {
Self {
content: "Default text".to_string(),
font_size: 15.0,
font_family: "Arial".to_string(),
font_weight: FontWeight::Normal,
font_style: FontStyle::Normal,
text_align: TextAlign::Center,
text_decoration: TextDecoration::None,
letter_spacing: DEFAULT_LETTER_SPACING,
line_height: DEFAULT_LINE_HEIGHT,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TextBackground {
pub enabled: bool,
pub color: String,
pub corner_radius: f64,
pub padding_x: f64,
pub padding_y: f64,
pub offset_x: f64,
pub offset_y: f64,
}
impl Default for TextBackground {
fn default() -> Self {
Self {
enabled: DEFAULT_BACKGROUND_ENABLED,
color: DEFAULT_BACKGROUND_COLOR.to_string(),
corner_radius: DEFAULT_BACKGROUND_CORNER_RADIUS,
padding_x: DEFAULT_BACKGROUND_PADDING_X,
padding_y: DEFAULT_BACKGROUND_PADDING_Y,
offset_x: DEFAULT_BACKGROUND_OFFSET_X,
offset_y: DEFAULT_BACKGROUND_OFFSET_Y,
}
}
}
pub fn text_layout_params_from(params: &ParamValues) -> TextLayoutParams {
TextLayoutParams {
content: string_param(params, "content", "Default text"),
font_size: number_param(params, "fontSize", 15.0),
font_family: string_param(params, "fontFamily", "Arial"),
font_weight: match string_param(params, "fontWeight", "normal").as_str() {
"bold" => FontWeight::Bold,
_ => FontWeight::Normal,
},
font_style: match string_param(params, "fontStyle", "normal").as_str() {
"italic" => FontStyle::Italic,
_ => FontStyle::Normal,
},
text_align: match string_param(params, "textAlign", "center").as_str() {
"left" => TextAlign::Left,
"right" => TextAlign::Right,
_ => TextAlign::Center,
},
text_decoration: match string_param(params, "textDecoration", "none").as_str() {
"underline" => TextDecoration::Underline,
"line-through" => TextDecoration::LineThrough,
_ => TextDecoration::None,
},
letter_spacing: number_param(params, "letterSpacing", DEFAULT_LETTER_SPACING),
line_height: number_param(params, "lineHeight", DEFAULT_LINE_HEIGHT),
}
}
pub fn text_background_from(params: &ParamValues) -> TextBackground {
TextBackground {
enabled: bool_param(params, "background.enabled", DEFAULT_BACKGROUND_ENABLED),
color: string_param(params, "background.color", DEFAULT_BACKGROUND_COLOR),
corner_radius: number_param(
params,
"background.cornerRadius",
DEFAULT_BACKGROUND_CORNER_RADIUS,
),
padding_x: number_param(params, "background.paddingX", DEFAULT_BACKGROUND_PADDING_X),
padding_y: number_param(params, "background.paddingY", DEFAULT_BACKGROUND_PADDING_Y),
offset_x: number_param(params, "background.offsetX", DEFAULT_BACKGROUND_OFFSET_X),
offset_y: number_param(params, "background.offsetY", DEFAULT_BACKGROUND_OFFSET_Y),
}
}
fn string_param(params: &ParamValues, key: &str, fallback: &str) -> String {
match params.get(key) {
Some(ParamValue::String(value)) => value.clone(),
_ => fallback.to_string(),
}
}
fn number_param(params: &ParamValues, key: &str, fallback: f64) -> f64 {
match params.get(key) {
Some(ParamValue::Number(value)) => *value,
_ => fallback,
}
}
fn bool_param(params: &ParamValues, key: &str, fallback: bool) -> bool {
match params.get(key) {
Some(ParamValue::Bool(value)) => *value,
_ => fallback,
}
}
|