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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use serde::{Serialize, Deserialize};
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum TextAlignment {
    Left,
    Center,
    Right,
    Middle,
    TopLeft,
    TopCenter,
    TopRight,
    MiddleLeft,
    MiddleCenter,
    MiddleRight,
    BottomLeft,
    BottomCenter,
    BottomRight,
    Aligned,
    MiddleOfField,
}

impl Default for TextAlignment {
    fn default() -> Self {
        TextAlignment::Left
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum TextVerticalAlignment {
    Top,
    Middle,
    Bottom,
}

impl Default for TextVerticalAlignment {
    fn default() -> Self {
        TextVerticalAlignment::Bottom
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextStyle {
    pub name: String,
    pub font_name: String,
    pub height: f64,
    pub width_factor: f64,
    pub oblique_angle: f64,
    pub is_backwards: bool,
    pub is_upside_down: bool,
    pub is_vertical: bool,
    pub color: (u8, u8, u8),
    pub layer: Option<String>,
}

impl Default for TextStyle {
    fn default() -> Self {
        Self {
            name: "Standard".to_string(),
            font_name: "Arial".to_string(),
            height: 2.5,
            width_factor: 1.0,
            oblique_angle: 0.0,
            is_backwards: false,
            is_upside_down: false,
            is_vertical: false,
            color: (0, 0, 0),
            layer: None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Text {
    pub content: String,
    pub position: crate::geometry::Point,
    pub height: f64,
    pub rotation: f64,
    pub width_factor: f64,
    pub oblique_angle: f64,
    pub style: TextStyle,
    pub horizontal_alignment: TextAlignment,
    pub vertical_alignment: TextVerticalAlignment,
    pub color: (u8, u8, u8),
    pub layer: Option<String>,
    pub visibility: crate::data_structure::Visibility,
}

impl Default for Text {
    fn default() -> Self {
        Self {
            content: String::new(),
            position: crate::geometry::Point::new(0.0, 0.0, 0.0),
            height: 2.5,
            rotation: 0.0,
            width_factor: 1.0,
            oblique_angle: 0.0,
            style: TextStyle::default(),
            horizontal_alignment: TextAlignment::Left,
            vertical_alignment: TextVerticalAlignment::Bottom,
            color: (0, 0, 0),
            layer: None,
            visibility: crate::data_structure::Visibility::Visible,
        }
    }
}

impl Text {
    pub fn new(content: String, position: crate::geometry::Point, height: f64) -> Self {
        Self {
            content,
            position,
            height,
            ..Default::default()
        }
    }

    pub fn with_style(content: String, position: crate::geometry::Point, height: f64, style: TextStyle) -> Self {
        Self {
            content,
            position,
            height,
            style,
            ..Default::default()
        }
    }

    pub fn set_alignment(&mut self, horizontal: TextAlignment, vertical: TextVerticalAlignment) {
        self.horizontal_alignment = horizontal;
        self.vertical_alignment = vertical;
    }

    pub fn set_rotation(&mut self, angle: f64) {
        self.rotation = angle;
    }

    pub fn get_bounding_box(&self) -> (crate::geometry::Point, crate::geometry::Point) {
        let width = self.content.len() as f64 * self.height * self.width_factor * 0.6;
        let half_height = self.height / 2.0;

        let min = crate::geometry::Point::new(
            self.position.x - width / 2.0,
            self.position.y - half_height,
            0.0,
        );
        let max = crate::geometry::Point::new(
            self.position.x + width / 2.0,
            self.position.y + half_height,
            0.0,
        );

        (min, max)
    }
}

impl fmt::Display for Text {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Text(content=\"{}\", position={}, height={})",
            self.content, self.position, self.height
        )
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TextJustification {
    Left,
    Center,
    Right,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextFormatting {
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub color: Option<(u8, u8, u8)>,
    pub font_size: Option<f64>,
    pub font_name: Option<String>,
}

impl Default for TextFormatting {
    fn default() -> Self {
        Self {
            bold: false,
            italic: false,
            underline: false,
            color: None,
            font_size: None,
            font_name: None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormattedText {
    pub text: String,
    pub formatting: TextFormatting,
}

impl FormattedText {
    pub fn new(text: String) -> Self {
        Self {
            text,
            formatting: TextFormatting::default(),
        }
    }

    pub fn bold(mut self) -> Self {
        self.formatting.bold = true;
        self
    }

    pub fn italic(mut self) -> Self {
        self.formatting.italic = true;
        self
    }

    pub fn with_color(mut self, color: (u8, u8, u8)) -> Self {
        self.formatting.color = Some(color);
        self
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MText {
    pub contents: Vec<FormattedText>,
    pub position: crate::geometry::Point,
    pub height: f64,
    pub rotation: f64,
    pub style: TextStyle,
    pub line_spacing: f64,
    pub width: Option<f64>,
    pub color: (u8, u8, u8),
    pub layer: Option<String>,
    pub visibility: crate::data_structure::Visibility,
}

impl Default for MText {
    fn default() -> Self {
        Self {
            contents: Vec::new(),
            position: crate::geometry::Point::new(0.0, 0.0, 0.0),
            height: 2.5,
            rotation: 0.0,
            style: TextStyle::default(),
            line_spacing: 1.0,
            width: None,
            color: (0, 0, 0),
            layer: None,
            visibility: crate::data_structure::Visibility::Visible,
        }
    }
}

impl MText {
    pub fn new(contents: Vec<FormattedText>, position: crate::geometry::Point, height: f64) -> Self {
        Self {
            contents,
            position,
            height,
            ..Default::default()
        }
    }

    pub fn add_text(&mut self, text: FormattedText) {
        self.contents.push(text);
    }

    pub fn add_plain_text(&mut self, text: &str) {
        self.contents.push(FormattedText::new(text.to_string()));
    }

    pub fn set_width(&mut self, width: f64) {
        self.width = Some(width);
    }

    pub fn set_line_spacing(&mut self, spacing: f64) {
        self.line_spacing = spacing;
    }

    pub fn to_plain_text(&self) -> String {
        self.contents
            .iter()
            .map(|ft| ft.text.clone())
            .collect()
    }
}

impl fmt::Display for MText {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "MText(position={}, height={}, lines={})",
            self.position,
            self.height,
            self.contents.len()
        )
    }
}

pub struct TextBuilder {
    content: String,
    position: crate::geometry::Point,
    height: f64,
    rotation: f64,
    style: TextStyle,
    horizontal_alignment: TextAlignment,
    vertical_alignment: TextVerticalAlignment,
    width_factor: f64,
    oblique_angle: f64,
}

impl Default for TextBuilder {
    fn default() -> Self {
        Self {
            content: String::new(),
            position: crate::geometry::Point::new(0.0, 0.0, 0.0),
            height: 2.5,
            rotation: 0.0,
            style: TextStyle::default(),
            horizontal_alignment: TextAlignment::Left,
            vertical_alignment: TextVerticalAlignment::Bottom,
            width_factor: 1.0,
            oblique_angle: 0.0,
        }
    }
}

impl TextBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn content(mut self, content: impl Into<String>) -> Self {
        self.content = content.into();
        self
    }

    pub fn position(mut self, x: f64, y: f64) -> Self {
        self.position = crate::geometry::Point::new(x, y, 0.0);
        self
    }

    pub fn position_point(mut self, point: crate::geometry::Point) -> Self {
        self.position = point;
        self
    }

    pub fn height(mut self, height: f64) -> Self {
        self.height = height;
        self
    }

    pub fn rotation(mut self, angle: f64) -> Self {
        self.rotation = angle;
        self
    }

    pub fn style(mut self, style: TextStyle) -> Self {
        self.style = style;
        self
    }

    pub fn alignment(mut self, horizontal: TextAlignment, vertical: TextVerticalAlignment) -> Self {
        self.horizontal_alignment = horizontal;
        self.vertical_alignment = vertical;
        self
    }

    pub fn width_factor(mut self, factor: f64) -> Self {
        self.width_factor = factor;
        self
    }

    pub fn oblique_angle(mut self, angle: f64) -> Self {
        self.oblique_angle = angle;
        self
    }

    pub fn build(self) -> Text {
        Text {
            content: self.content,
            position: self.position,
            height: self.height,
            rotation: self.rotation,
            width_factor: self.width_factor,
            oblique_angle: self.oblique_angle,
            style: self.style,
            horizontal_alignment: self.horizontal_alignment,
            vertical_alignment: self.vertical_alignment,
            color: (0, 0, 0),
            layer: None,
            visibility: crate::data_structure::Visibility::Visible,
        }
    }
}

impl fmt::Display for TextBuilder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "TextBuilder(content=\"{}\", position={}, height={})",
            self.content, self.position, self.height
        )
    }
}