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
// sign.rs
//
// Copyright (C) 2018-2023  Minnesota Department of Transportation
//
use crate::dms::config::{MultiCfg, SignCfg, VmsCfg};
use crate::dms::font::FontTable;
use crate::dms::graphic::GraphicTable;
use crate::dms::multi::{ColorCtx, ColorScheme};
use crate::dms::render::{Page, Pages};
use crate::dms::Result;

/// Builder for DMS
#[derive(Clone, Default)]
pub struct DmsBuilder {
    sign_cfg: SignCfg,
    vms_cfg: VmsCfg,
    font_definition: FontTable,
    multi_cfg: MultiCfg,
    graphic_definition: GraphicTable,
}

/// Dynamic message sign
///
/// This is the root node of the 1203 Message Information Base (MIB)
#[derive(Clone)]
pub struct Dms {
    /// Configuration common to all signs — `dmsSignCfg`
    sign_cfg: SignCfg,
    /// Configuration for variable message signs — `vmsCfg`
    vms_cfg: VmsCfg,
    /// Font definition — `fontDefinition`
    font_definition: FontTable,
    /// MULTI configuration — `multiCfg`
    multi_cfg: MultiCfg,
    /// Graphic definition — `graphicDefinition`
    graphic_definition: GraphicTable,
}

impl DmsBuilder {
    /// Set sign configuration
    pub fn with_sign_cfg(mut self, cfg: SignCfg) -> Self {
        self.sign_cfg = cfg;
        self
    }

    /// Set VMS configuration
    pub fn with_vms_cfg(mut self, cfg: VmsCfg) -> Self {
        self.vms_cfg = cfg;
        self
    }

    /// Set font definition
    pub fn with_font_definition(mut self, fonts: FontTable) -> Self {
        self.font_definition = fonts;
        self
    }

    /// Set MULTI configuration
    pub fn with_multi_cfg(mut self, cfg: MultiCfg) -> Self {
        self.multi_cfg = cfg;
        self
    }

    /// Set graphic definition
    pub fn with_graphic_definition(mut self, graphics: GraphicTable) -> Self {
        self.graphic_definition = graphics;
        self
    }

    /// Build the DMS with validation
    pub fn build(self) -> Dms {
        // FIXME: validate
        Dms {
            sign_cfg: self.sign_cfg,
            vms_cfg: self.vms_cfg,
            font_definition: self.font_definition,
            multi_cfg: self.multi_cfg,
            graphic_definition: self.graphic_definition,
        }
    }
}

impl Dms {
    /// Create a DMS builder
    pub fn builder() -> DmsBuilder {
        DmsBuilder::default()
    }

    /// Convert back into builder
    pub fn into_builder(self) -> DmsBuilder {
        DmsBuilder {
            sign_cfg: self.sign_cfg,
            vms_cfg: self.vms_cfg,
            font_definition: self.font_definition,
            multi_cfg: self.multi_cfg,
            graphic_definition: self.graphic_definition,
        }
    }

    /// Get font definition
    pub fn font_definition(&self) -> &FontTable {
        &self.font_definition
    }

    /// Get graphic definition
    pub fn graphic_definition(&self) -> &GraphicTable {
        &self.graphic_definition
    }

    /// Get default font
    fn default_font(&self) -> u8 {
        self.multi_cfg.default_font
    }

    /// Get the face width (mm)
    pub fn face_width_mm(&self) -> f32 {
        self.sign_cfg.sign_width as f32
    }

    /// Get the width of the sign (pixels)
    fn pixel_width(&self) -> u16 {
        self.vms_cfg.sign_width_pixels
    }

    /// Get the horizontal border (mm)
    ///
    /// Sanity checked in case values are stupid.
    fn border_horiz_mm(&self) -> f32 {
        let pix = self.pixel_width() + self.gap_char_count();
        let min_width = (pix as f32) * self.pitch_horiz_mm();
        let extra = (self.face_width_mm() - min_width).max(0.0);
        if self.gap_char_count() > 0 {
            let border = self.sign_cfg.horizontal_border as f32;
            border.min(extra / 2.0)
        } else {
            // Ignore border_horiz if there are no character gaps
            extra / 2.0
        }
    }

    /// Get the pixel width (mm)
    fn pixel_width_mm(&self) -> f32 {
        self.face_width_mm() - self.border_horiz_mm() * 2.0
    }

    /// Get the horizontal pitch
    fn pitch_horiz(&self) -> u8 {
        self.vms_cfg.horizontal_pitch
    }

    /// Get the horizontal pitch (mm)
    ///
    /// Sanity checked in case values are stupid.
    fn pitch_horiz_mm(&self) -> f32 {
        let pitch = self.pitch_horiz() as f32;
        let pix = self.pixel_width() + self.gap_char_count();
        if pix > 0 {
            pitch.min(self.face_width_mm() / pix as f32)
        } else {
            pitch
        }
    }

    /// Get the number of gaps between characters
    fn gap_char_count(&self) -> u16 {
        let cw = self.char_width().into();
        if cw > 1 && self.pixel_width() > cw {
            (self.pixel_width() - 1) / cw
        } else {
            0
        }
    }

    /// Get the X-position of a pixel on the sign
    pub fn pixel_x(&self, x: u32, width: u32) -> f32 {
        let border = self.border_horiz_mm();
        let offset = self.char_offset_mm(x);
        let x = x as f32 + 0.5; // shift to center of pixel
        let pos = border + offset + x * self.pitch_horiz_mm();
        width as f32 * pos / self.face_width_mm()
    }

    /// Get the horizontal character offset (mm)
    fn char_offset_mm(&self, x: u32) -> f32 {
        let cw = u32::from(self.char_width());
        if cw > 1 {
            let char_num = (x / cw) as f32;
            char_num * self.gap_char_mm()
        } else {
            0.0
        }
    }

    /// Get the character gap (mm)
    fn gap_char_mm(&self) -> f32 {
        let gaps = self.gap_char_count();
        if gaps > 0 {
            self.excess_char_mm() / gaps as f32
        } else {
            0.0
        }
    }

    /// Get excess width for character gaps (mm)
    fn excess_char_mm(&self) -> f32 {
        let pix_mm = self.pitch_horiz_mm() * self.pixel_width() as f32;
        (self.pixel_width_mm() - pix_mm).max(0.0)
    }

    /// Get the face height (mm)
    pub fn face_height_mm(&self) -> f32 {
        self.sign_cfg.sign_height as f32
    }

    /// Get the height of the sign (pixels)
    fn pixel_height(&self) -> u16 {
        self.vms_cfg.sign_height_pixels
    }

    /// Get the vertical border (mm)
    ///
    /// Sanity checked in case values are stupid.
    fn border_vert_mm(&self) -> f32 {
        let pix = self.pixel_height() + self.gap_line_count();
        let min_height = (pix as f32) * self.pitch_vert_mm();
        let extra = (self.face_height_mm() - min_height).max(0.0);
        if self.gap_line_count() > 0 {
            let border = self.sign_cfg.vertical_border as f32;
            border.min(extra / 2.0)
        } else {
            // Ignore border_vert if there are no line gaps
            extra / 2.0
        }
    }

    /// Get the pixel height (mm)
    fn pixel_height_mm(&self) -> f32 {
        self.face_height_mm() - self.border_vert_mm() * 2.0
    }

    /// Get the vertical pitch
    fn pitch_vert(&self) -> u8 {
        self.vms_cfg.vertical_pitch
    }

    /// Get the vertical pitch (mm)
    ///
    /// Sanity checked in case values are stupid.
    fn pitch_vert_mm(&self) -> f32 {
        let pitch = self.pitch_vert() as f32;
        let pix = self.pixel_height() + self.gap_line_count();
        if pix > 0 {
            pitch.min(self.face_height_mm() / pix as f32)
        } else {
            pitch
        }
    }

    /// Get the number of gaps between lines
    fn gap_line_count(&self) -> u16 {
        let ch = self.char_height().into();
        if ch > 1 && self.pixel_height() > ch {
            (self.pixel_height() - 1) / ch
        } else {
            0
        }
    }

    /// Get the Y-position of a pixel on the sign
    pub fn pixel_y(&self, y: u32, height: u32) -> f32 {
        let border = self.border_vert_mm();
        let offset = self.line_offset_mm(y);
        let y = y as f32 + 0.5; // shift to center of pixel
        let pos = border + offset + y * self.pitch_vert_mm();
        height as f32 * pos / self.face_height_mm()
    }

    /// Get the vertical line offset (mm)
    fn line_offset_mm(&self, y: u32) -> f32 {
        let ch = u32::from(self.char_height());
        if ch > 1 {
            let line_num = (y / ch) as f32;
            line_num * self.gap_line_mm()
        } else {
            0.0
        }
    }

    /// Get the line gap (mm)
    fn gap_line_mm(&self) -> f32 {
        let gaps = self.gap_line_count();
        if gaps > 0 {
            self.excess_line_mm() / gaps as f32
        } else {
            0.0
        }
    }

    /// Get excess height for line gaps (mm).
    fn excess_line_mm(&self) -> f32 {
        let pix_mm = self.pitch_vert_mm() * self.pixel_height() as f32;
        (self.pixel_height_mm() - pix_mm).max(0.0)
    }

    /// Get the character width as u8
    fn char_width(&self) -> u8 {
        self.vms_cfg.char_width_pixels
    }

    /// Get the character height as u8
    fn char_height(&self) -> u8 {
        self.vms_cfg.char_height_pixels
    }

    /// Get the color scheme value
    fn color_scheme(&self) -> ColorScheme {
        self.multi_cfg.color_scheme
    }

    /// Get the default foreground color
    fn foreground_default_rgb(&self) -> (u8, u8, u8) {
        match self.color_scheme() {
            ColorScheme::ColorClassic | ColorScheme::Color24Bit => {
                self.multi_cfg.default_foreground_rgb.rgb()
            }
            _ => {
                let mono = &self.vms_cfg.monochrome_color;
                (mono[0], mono[1], mono[2])
            }
        }
    }

    /// Get the default background color
    fn background_default_rgb(&self) -> (u8, u8, u8) {
        match self.color_scheme() {
            ColorScheme::ColorClassic | ColorScheme::Color24Bit => {
                self.multi_cfg.default_background_rgb.rgb()
            }
            _ => {
                let mono = &self.vms_cfg.monochrome_color;
                (mono[3], mono[4], mono[5])
            }
        }
    }

    /// Render to a series of pages
    pub fn render_pages<'a>(
        &'a self,
        multi: &'a str,
    ) -> impl Iterator<Item = Result<Page>> + 'a {
        let width = self.pixel_width();
        let height = self.pixel_height();
        let color_scheme = self.color_scheme();
        let fg_default = self.foreground_default_rgb();
        let bg_default = self.background_default_rgb();
        let color_ctx = ColorCtx::new(color_scheme, fg_default, bg_default);
        let char_width = self.char_width();
        let char_height = self.char_height();
        let font_num = self.default_font();
        Pages::builder(width, height)
            .with_color_ctx(color_ctx)
            .with_char_size(char_width, char_height)
            .with_page_on_time_ds(self.multi_cfg.default_page_on_time)
            .with_page_off_time_ds(self.multi_cfg.default_page_off_time)
            .with_justification_page(self.multi_cfg.default_justification_page)
            .with_justification_line(self.multi_cfg.default_justification_line)
            .with_font_num(font_num)
            .with_fonts(&self.font_definition)
            .with_graphics(&self.graphic_definition)
            .build(multi)
    }
}