small software-rendered rust tty
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
use std::sync::LazyLock;

use atools::Join;
use fimg::BlendingOverlayAt;
use swash::scale::{Render, ScaleContext, Source};
use swash::zeno::Format;
use swash::{FontRef, Instance};

use crate::colors;

#[implicit_fn::implicit_fn]
pub fn render(
    x: &mut super::Terminal,
    (w, h): (usize, usize),
    ppem: f32,
) -> Image<Box<[u8]>, 3> {
    let m = FONT.metrics(&[]);
    let sz = ppem * (m.max_width / m.units_per_em as f32);
    let mut i = Image::build(w as _, h as _).fill(colors::BACKGROUND);
    if w < 60 || h < 60 {
        return i;
    }
    let c = x.cells.c() as usize;
    let r = x.cells.r() as usize;
    let vo = x.view_o.unwrap_or(x.cells.row);
    for (col, k) in x.cells.cells[vo * c..vo * c + r * c]
        .chunks_exact(c as _)
        .zip(1..)
    {
        for (&(mut cell), j) in col.iter().zip(0..) {
            if cell.style.flags & crate::term::INVERT != 0 {
                std::mem::swap(&mut cell.style.bg, &mut cell.style.color);
            }
            if cell.style.bg != colors::BACKGROUND {
                let cell = Image::<_, 4>::build(
                    sz.ceil() as u32,
                    (ppem * 1.25).ceil() as u32,
                )
                .fill(cell.style.bg.join(255));
                unsafe {
                    i.as_mut().overlay_at(
                        &cell,
                        4 + (j as f32 * sz) as u32,
                        (k as f32 * (ppem * 1.25)) as u32 - 20,
                    )
                };
            }
            let mut color = cell.style.color;
            if (cell.style.flags & crate::term::DIM) != 0 {
                color = color.map(|x| x / 2);
            }
            if (cell.style.flags & crate::term::UNDERLINE) != 0 {
                unsafe {
                    i.as_mut().overlay_at(
                        &Image::<_, 4>::build(sz.ceil() as u32, 2)
                            .fill(color.join(255)),
                        4 + (j as f32 * sz) as u32,
                        (k as f32 * (ppem * 1.25)) as u32 + 5,
                    )
                };
            }
            if (cell.style.flags & crate::term::STRIKETHROUGH) != 0 {
                unsafe {
                    i.as_mut().overlay_at(
                        &Image::<_, 4>::build(sz.ceil() as u32, 2)
                            .fill(color.join(255)),
                        4 + (j as f32 * sz) as u32,
                        (k as f32 * (ppem * 1.25)) as u32 - 5,
                    )
                };
            }
            if let Some(l) = cell.letter {
                let f = if (cell.style.flags & crate::term::ITALIC) != 0 {
                    *IFONT
                } else {
                    *FONT
                };
                let id = f.charmap().map(l);
                let mut scbd = ScaleContext::new();
                let mut scbd = scbd.builder(f);
                scbd = scbd.size(ppem);
                if (cell.style.flags & crate::term::BOLD) != 0 {
                    scbd = scbd.variations(
                        if (cell.style.flags & crate::term::ITALIC) != 0 {
                            *BIFONT
                        } else {
                            *BFONT
                        }
                        .values()
                        .zip(f.variations())
                        .map(|x| (x.1.tag(), x.0)),
                    );
                }
                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,
                        4 + ((j as f32 * sz) + x.placement.left as f32)
                            as u32,
                        ((k as f32 * (ppem * 1.25)) as u32)
                            .saturating_sub(x.placement.top 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) -> (f32, f32) {
    let m = font.metrics(&[]);
    (ppem * (m.max_width / m.units_per_em as f32), ppem * 1.25)
}

pub static FONT: LazyLock<FontRef<'static>> = LazyLock::new(|| {
    FontRef::from_index(&include_bytes!("../CascadiaCodeNF.ttf")[..], 0)
        .unwrap()
});

pub static IFONT: LazyLock<FontRef<'static>> = LazyLock::new(|| {
    FontRef::from_index(
        &include_bytes!("../CascadiaCodeNFItalic.ttf")[..],
        0,
    )
    .unwrap()
});

pub static BIFONT: LazyLock<Instance<'static>> = LazyLock::new(|| {
    IFONT.instances().find_by_name("Bold Italic").unwrap()
});

pub static BFONT: LazyLock<Instance<'static>> =
    LazyLock::new(|| FONT.instances().find_by_name("Bold").unwrap());

use fimg::{Image, OverlayAt};
// let x = b"echo -e \"\x1b(0lqqqk\nx   \x1b(Bx\nmqqqj";
// let x = String::from_utf8_lossy(&x);
// println!("{}", x);
#[test]
fn t() {
    IFONT
        .instances()
        .for_each(|f| drop(dbg!(f.name(None).unwrap().to_string())));
    let f =
        FontRef::from_index(&include_bytes!("../cjk.ttc")[..], 0).unwrap();

    dbg!(f.attributes());
    // let m = f.metrics(&[f.charmap().map('行') as _]);

    let ppem = 30.0;
    let d = dims(&FONT, ppem);
    let sz = d.0;
    let mut grid = Image::<_, 4>::alloc(2000, 500);
    grid.chunked_mut().for_each(|x| *x = [0, 0, 0, 255]);
    for (letter, i) in "素早い茶色のキツネは怠け者の犬を飛び越えた"
        .chars()
        .zip(0..)
    {
        // grid.as_ref().show();
        let id = f.charmap().map(letter);
        let x = Render::new(&[Source::Outline])
            .format(Format::Alpha)
            .render(
                &mut ScaleContext::new().builder(f).size(ppem).build(),
                id,
            )
            .unwrap();
        unsafe {
            if x.placement.width == 0 {
                continue;
            }
            grid.as_mut().overlay_at(
                &Image::<Box<[u8]>, 4>::from(
                    Image::<_, 1>::build(
                        x.placement.width,
                        x.placement.height,
                    )
                    .buf(&*x.data),
                )
                .as_ref(),
                ((i as f32 * sz * 2.0) as i32 + x.placement.left) as _,
                ppem as u32 - x.placement.top as u32,
            );
        }
    }
    for (letter, i) in "#".repeat(21 * 2).chars().zip(0..) {
        // grid.as_ref().show();
        let id = FONT.charmap().map(letter);
        let x = Render::new(&[Source::Outline])
            .format(Format::Alpha)
            .render(
                &mut ScaleContext::new().builder(*FONT).size(ppem).build(),
                id,
            )
            .unwrap();
        unsafe {
            if x.placement.width == 0 {
                continue;
            }
            grid.as_mut().overlay_at(
                &Image::<Box<[u8]>, 4>::from(
                    Image::<_, 1>::build(
                        x.placement.width,
                        x.placement.height,
                    )
                    .buf(&*x.data),
                )
                .as_ref(),
                ((i as f32 * sz) as i32 + x.placement.left) as _,
                30 + ppem as u32 - x.placement.top as u32,
            );
        }
    }
    grid.show();
}