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
// |jlfJKlBM
// |||||||||||||||||||||||||||||||||||||
#![allow(incomplete_features)]
#![feature(
    super_let,
    debug_closure_helpers,
    const_trait_impl,
    anonymous_lifetime_in_impl_trait,
    deref_patterns,
    generic_const_exprs,
    guard_patterns,
    impl_trait_in_bindings,
    if_let_guard,
    import_trait_associated_functions
)]
use std::iter::zip;
pub mod cell;
use atools::Join;
use swash::FontRef;
use swash::scale::{Render, ScaleContext, Source};
use swash::shape::ShapeContext;
use swash::shape::cluster::Glyph;
use swash::text::cluster::{CharCluster, Parser, Token};
use swash::text::{Codepoint, Script};
use swash::zeno::Format;

pub struct Fonts<'a, 'b, 'c, 'd> {
    regular: FontRef<'a>,
    bold: FontRef<'b>,
    italic: FontRef<'c>,
    bold_italic: FontRef<'d>,
}
impl<'a, 'b, 'c, 'd> Fonts<'a, 'b, 'c, 'd> {
    pub fn new(
        regular: impl Into<FontRef<'a>>,
        bold: impl Into<FontRef<'b>>,
        italic: impl Into<FontRef<'c>>,
        bold_italic: impl Into<FontRef<'d>>,
    ) -> Self {
        Self {
            regular: regular.into(),
            bold: bold.into(),
            italic: italic.into(),
            bold_italic: bold_italic.into(),
        }
    }
}
#[implicit_fn::implicit_fn]
pub unsafe fn render(
    cells: &[Cell],
    (c, r): (usize, usize),
    ppem: f32,
    bgcolor: [u8; 3],
    fonts: Fonts,
    line_spacing: f32,
) -> Image<Box<[u8]>, 3> {
    assert_eq!(c * r, cells.len(), "cells too short.");

    let (fw, h) = dims(&fonts.regular, ppem, line_spacing);
    let fh = h - line_spacing;
    let (w, h) = ((fw * c as f32).ceil() as u32, (h * r as f32).ceil() as u32);
    let mut i = Image::build(w as _, h as _).fill(bgcolor);
    if w < 60 || h < 60 {
        return i;
    }

    for (col, k) in cells.chunks_exact(c as _).zip(0..) {
        for (&cell, j) in zip(col, 0..) {
            if cell.style.bg != bgcolor {
                let cell =
                    Image::<_, 4>::build(fw.ceil() as u32, (fh + line_spacing).round() as u32)
                        .fill(cell.style.bg.join(255));

                unsafe {
                    i.as_mut().overlay_at(
                        &cell,
                        (j as f32 * fw).round() as u32,
                        (((k) as f32 * fh) + ((k as f32) * line_spacing as f32)).round() as u32,
                    )
                };
            }
        }
    }
    let mut characters: Vec<Glyph> = vec![Glyph::default(); c];

    for (col, k) in cells.chunks_exact(c as _).zip(0..) {
        let tokenized = col.iter().enumerate().map(|(i, cell)| {
            let ch = cell.letter.unwrap_or(' ');
            (
                Token {
                    ch,
                    offset: i as u32,
                    len: ch.len_utf8() as u8,
                    info: ch.properties().into(),
                    data: i as u32,
                },
                cell.style.flags,
            )
        });

        macro_rules! input {
            ($rule:expr, $font:expr) => {
                let mut scx = ShapeContext::new();
                let mut shaper = scx
                    .builder($font)
                    .size(ppem)
                    .script(Script::Latin)
                    .features([
                        ("ss19", 1),
                        ("ss01", 1),
                        ("ss20", 1),
                        ("liga", 1),
                        ("rlig", 1),
                    ])
                    .build();

                let mut cluster = CharCluster::new();

                let mut parser =
                    Parser::new(Script::Latin, tokenized.clone().filter($rule).map(|x| x.0));
                while parser.next(&mut cluster) {
                    cluster.map(|ch| $font.charmap().map(ch));
                    shaper.add_cluster(&cluster);
                }
                shaper.shape_with(|x| {
                    x.glyphs.into_iter().for_each(|x| {
                        characters[x.data as usize] = *x;
                    })
                });
            };
        }
        input!(|x| x.1 & Style::ITALIC == 0, fonts.regular);
        input!(|x| x.1 & Style::ITALIC != 0, fonts.bold_italic); // bifont is an instance of ifont

        for (&cell, glyph) in characters.iter().map(|x| (&col[x.data as usize], x)) {
            let j = glyph.data as usize;
            let mut color = cell.style.color;
            if (cell.style.flags & Style::DIM) != 0 {
                color = color.map(|x| x / 2);
            }
            // if (cell.style.flags & Style::UNDERLINE) != 0 {
            //     unsafe {
            //         i.as_mut().overlay_at(
            //             &Image::<_, 4>::build(fw.ceil() as u32, 2).fill(color.join(255)),
            //             4 + (j as f32 * fw as u32,
            //             (k as f32 * (ppem * 1.25)) as u32 + 5,
            //         )
            //     };
            // }
            // if (cell.style.flags & Style::STRIKETHROUGH) != 0 {
            //     unsafe {
            //         i.as_mut().overlay_at(
            //             &Image::<_, 4>::build(fw.ceil() as u32, 2).fill(color.join(255)),
            //             4 + (j as f32 * fw) as u32,
            //             (k as f32 * (ppem * 1.25)) as u32 - 5,
            //         )
            //     };
            // }
            if let Some(_) = cell.letter {
                let f = match cell.style.flags {
                    f if f & (Style::BOLD | Style::ITALIC) != 0 => fonts.bold_italic,
                    f if f & Style::BOLD != 0 => fonts.bold,
                    f if f & Style::ITALIC != 0 => fonts.italic,
                    _ => fonts.regular,
                };
                let id = glyph.id;
                let mut scbd = ScaleContext::new();
                let mut scbd = scbd.builder(f);
                scbd = scbd.size(ppem);

                let x = Render::new(&[Source::Outline])
                    .format(Format::Alpha)
                    .render(&mut scbd.build(), id)
                    .unwrap();
                unsafe {
                    if x.placement.width == 0 {
                        continue;
                    }
                    let item = Image::<_, 1>::build(x.placement.width, x.placement.height)
                        .buf_unchecked(x.data);
                    i.as_mut().blend_alpha_and_color_at(
                        &item.as_ref(),
                        color,
                        ((j as f32 * fw + glyph.x) + x.placement.left as f32).round() as u32,
                        (
                            dbg!(((k + 1) as f32 * fh) - (x.placement.top as f32))
                            // + ((k as f32) * line_spacing as f32)
                        )
                        .round() as u32,
                    );
                }
            }
        }
    }

    // if x.view_o == Some(x.cells.row) || x.view_o.is_none() {
    //     let cell =
    //         Image::<_, 4>::build(3, (ppem * 1.25).ceil() as u32).fill([0xFF, 0xCC, 0x66, 255]);
    //     unsafe {
    //         i.as_mut().overlay_at(
    //             &cell,
    //             4 + ((x.cursor.0 - 1) as f32 * sz) as u32,
    //             (x.cursor.1 as f32 * (ppem * 1.25)) as u32 - 20,
    //         )
    //     };
    // }
    i
}

pub fn dims(font: &FontRef, ppem: f32, line_spacing: f32) -> (f32, f32) {
    // ppem * (m.max_width / m.units_per_em as f32),
    // m.max_width * (ppem / m.units_per_em as f32);
    let m = font.metrics(&[]);
    let f = ppem / m.units_per_em as f32;
    (
        m.max_width * f,
        m.ascent * f + line_spacing,
        // ((ppem + line_spacing) * (m.ascent / m.units_per_em as f32)),
    )
}
pub use crate::cell::Cell;
use crate::cell::Style;
use fimg::{Image, OverlayAt};