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
use crate::color::{Color, SetColor};
use core::{
    fmt,
    sync::atomic::{AtomicU64, Ordering},
};
use embedded_graphics::{
    geometry::Point,
    mono_font::{self, MonoFont, MonoTextStyle},
    pixelcolor::{self, RgbColor},
    text::{self, Text},
    Drawable,
};
use hal_core::framebuffer::{Draw, DrawTarget};
#[derive(Debug)]
pub struct MakeTextWriter<D> {
    mk: fn() -> D,
    settings: TextWriterBuilder,
    next_point: AtomicU64,
    line_len: u32,
    char_height: u32,
    last_line: i32,
}

#[derive(Debug, Clone, Copy)]
pub struct TextWriterBuilder {
    default_color: Color,
    start_point: Point,
}

#[derive(Clone, Debug)]
pub struct TextWriter<'mk, D> {
    target: DrawTarget<D>,
    color: Color,
    mk: &'mk MakeTextWriter<D>,
}

const fn pack_point(Point { x, y }: Point) -> u64 {
    (x as u64) << 32 | y as u64
}

const fn unpack_point(u: u64) -> Point {
    const Y_MASK: u64 = u32::MAX as u64;
    let x = (u >> 32) as i32;
    let y = (u & Y_MASK) as i32;
    Point { x, y }
}

impl<D> fmt::Write for TextWriter<'_, D>
where
    D: Draw,
{
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let curr_packed = self.mk.next_point.load(Ordering::Relaxed);
        let mut curr_point = unpack_point(curr_packed);

        // for a couple of reasons, we don't trust the `embedded-graphics` crate
        // to handle newlines for us:
        //
        // 1. it currently only actually produces a carriage return when a
        //    newline character appears in the *middle* of a string. this means
        //    that strings beginning or ending with newlines (and strings that
        //    are *just* newlines) won't advance the write position the way we'd
        //    expect them to. so, we have to do that part ourself --- it turns
        //    out that most `fmt::Debug`/`fmt::Display` implementations will
        //    write a bunch of strings that begin or end with `\n`.
        // 2. when we reach the bottom of the screen, we want to scroll the
        //    previous text up to make room for a new line of text.
        //    `embedded-graphics` doesn't implement this behavior. because we
        //    want to scroll every time we encounter a newline if we have
        //    reached the bottom of the screen, this means we have to implement
        //    *all* newline handling ourselves.
        //
        // TODO(eliza): currently, our newline handling doesn't honor
        // configurable line height. all lines are simply a single character
        // high. if we want to do something nicer about line height, we'd have
        // to implement that here...
        for mut line in s.split_inclusive('\n') {
            // does this chunk end with a newline? it might not, if:
            // (a) it's the last chunk in a string where newlines only occur in
            //     the beginning/middle.
            // (b) the string being written has no newlines (so
            //     `split_inclusive` will only yield a single chunk)
            let has_newline = line.ends_with('\n');
            if has_newline {
                // if there's a trailing newline, trim it off --- no sense
                // making the `embedded-graphics` crate draw an extra character
                // it will essentially nop for.
                line = &line[..line.len() - 1];
            }

            // if we have reached the bottom of the screen, we'll need to scroll
            // previous framebuffer contents up to make room for new line(s) of
            // text.
            if curr_point.y > self.mk.last_line {
                let ydiff = curr_point.y - self.mk.last_line;
                curr_point = Point {
                    y: self.mk.last_line,
                    x: 10,
                };
                self.target.inner_mut().scroll_vert(ydiff as isize);
            }

            let next_point = if line.is_empty() {
                // if this line is now empty, it was *just* a newline character,
                // so all we have to do is advance the write position.
                curr_point
            } else {
                // otherwise, actually draw the text.
                Text::with_alignment(s, curr_point, self.text_style(), text::Alignment::Left)
                    .draw(&mut self.target)
                    .map_err(|_| fmt::Error)?
            };

            if has_newline {
                // carriage return
                curr_point = Point {
                    y: curr_point.y + self.mk.char_height as i32,
                    x: 10,
                };
            } else {
                curr_point = next_point;
            }
        }

        match self.mk.next_point.compare_exchange(
            curr_packed,
            pack_point(curr_point),
            Ordering::Relaxed,
            Ordering::Relaxed,
        ) {
            Ok(_) => Ok(()),
            Err(actual_point) => unsafe {
                mycelium_util::unreachable_unchecked!(
                    "lock should guard this, could actually be totally unsync; curr_point={}; actual_point={}",
                    unpack_point(curr_packed),
                    unpack_point(actual_point)
                );
            },
        }
    }
}

impl<'mk, D> SetColor for TextWriter<'mk, D>
where
    D: Draw,
{
    fn set_fg_color(&mut self, color: Color) {
        let color = if color == Color::Default {
            self.mk.settings.default_color
        } else {
            color
        };
        self.color = color;
    }

    fn fg_color(&self) -> Color {
        self.color
    }

    fn set_bold(&mut self, bold: bool) {
        use Color::*;
        let next_color = if bold {
            match self.color {
                Black => BrightBlack,
                Red => BrightRed,
                Green => BrightGreen,
                Yellow => BrightYellow,
                Blue => BrightBlue,
                Magenta => BrightMagenta,
                Cyan => BrightCyan,
                White => BrightWhite,
                x => x,
            }
        } else {
            match self.color {
                BrightBlack => Black,
                BrightRed => Red,
                BrightGreen => Green,
                BrightYellow => Yellow,
                BrightBlue => Blue,
                BrightMagenta => Magenta,
                BrightCyan => Cyan,
                BrightWhite => White,
                x => x,
            }
        };
        self.set_fg_color(next_color);
    }
}

impl<'mk, D> TextWriter<'mk, D>
where
    D: Draw,
{
    fn text_style(&self) -> MonoTextStyle<'static, pixelcolor::Rgb888> {
        use pixelcolor::Rgb888;
        const COLOR_TABLE: [Rgb888; 17] = [
            Rgb888::BLACK,              // black
            Rgb888::new(128, 0, 0),     // red
            Rgb888::new(0, 128, 0),     // green
            Rgb888::new(128, 128, 0),   // yellow
            Rgb888::new(0, 0, 128),     // blue
            Rgb888::new(128, 0, 128),   // magenta
            Rgb888::new(0, 128, 128),   // cyan
            Rgb888::new(192, 192, 192), // white
            Rgb888::new(192, 192, 192), // default
            Rgb888::new(128, 128, 128), // bright black
            Rgb888::new(255, 0, 0),     // bright red
            Rgb888::new(0, 255, 0),     // bright green
            Rgb888::new(255, 255, 0),   // bright yellow
            Rgb888::new(0, 0, 255),     // bright blue
            Rgb888::new(255, 0, 255),   // bright magenta
            Rgb888::new(0, 255, 255),   // bright cyan
            Rgb888::new(255, 255, 255), // bright white
        ];
        MonoTextStyle::new(
            self.mk.settings.get_font(),
            COLOR_TABLE[self.color as usize],
        )
    }
}

// === impl MakeTextWriter ===
impl<D> MakeTextWriter<D> {
    #[must_use]
    pub const fn builder() -> TextWriterBuilder {
        TextWriterBuilder::new()
    }
}

impl<D: Draw> MakeTextWriter<D> {
    #[must_use]
    pub fn new(mk: fn() -> D) -> Self {
        Self::build(mk, TextWriterBuilder::new())
    }

    fn build(mk: fn() -> D, settings: TextWriterBuilder) -> Self {
        let (pixel_width, pixel_height) = {
            let buf = (mk)();
            (buf.width() as u32, buf.height() as u32)
        };
        let text_style = MonoTextStyle::new(settings.get_font(), pixelcolor::Rgb888::WHITE);
        let line_len = Self::line_len(pixel_width, &text_style);
        let char_height = text_style.font.character_size.height;
        let last_line = (pixel_height - char_height - 10) as i32;
        Self {
            settings,
            next_point: AtomicU64::new(pack_point(settings.start_point)),
            char_height,
            mk,
            line_len,
            last_line,
        }
    }

    fn line_len(pixel_width: u32, text_style: &MonoTextStyle<'static, pixelcolor::Rgb888>) -> u32 {
        pixel_width / text_style.font.character_size.width
    }
}

impl<'a, D> crate::writer::MakeWriter<'a> for MakeTextWriter<D>
where
    D: Draw + 'a,
{
    type Writer = TextWriter<'a, D>;

    fn make_writer(&'a self) -> Self::Writer {
        TextWriter {
            color: self.settings.default_color,
            target: (self.mk)().into_draw_target(),
            mk: self,
        }
    }

    fn line_len(&self) -> usize {
        self.line_len as usize
    }
}

impl TextWriterBuilder {
    #[must_use]
    pub const fn new() -> Self {
        Self {
            // TODO(eliza): it would be nice if this was configurable via the builder,
            // but it's not, because `MonoFont` is `!Sync` due to containing a trait
            // object without a `Sync` bound...this should be fixed upstream in
            // `embedded-graphics`.
            // font: &mono_font::ascii::FONT_6X13,
            default_color: Color::White,
            start_point: Point { x: 10, y: 10 },
        }
    }

    // #[must_use]
    // pub fn font(self, font: &'static MonoFont<'static>) -> Self {
    //     Self { font, ..self }
    // }

    #[must_use]
    pub fn default_color(self, default_color: Color) -> Self {
        Self {
            default_color,
            ..self
        }
    }

    #[must_use]
    pub fn starting_point(self, start_point: Point) -> Self {
        Self {
            start_point,
            ..self
        }
    }

    #[must_use]
    pub fn build<D: Draw>(self, mk: fn() -> D) -> MakeTextWriter<D> {
        MakeTextWriter::build(mk, self)
    }

    // TODO(eliza): it would be nice if this was configurable via the builder,
    // but it's not, because `MonoFont` is `!Sync` due to containing a trait
    // object without a `Sync` bound...this should be fixed upstream in
    // `embedded-graphics`.
    fn get_font(&self) -> &'static MonoFont<'static> {
        &mono_font::ascii::FONT_6X13
    }
}

impl Default for TextWriterBuilder {
    fn default() -> Self {
        Self::new()
    }
}