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);
let c = x.cells.c();
let r = x.cells.r();
let vo = x.view_o.unwrap_or(x.cells.row);
for (col, k) in x.cells.cells
[(vo * c) as usize..(vo * c + r * c) as usize]
.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();
}