glyph serialization
This commit is contained in:
+16
-5
@@ -1,5 +1,4 @@
|
||||
use color::Rgba8;
|
||||
use kiln_core::cp437::tile_to_char;
|
||||
use kiln_core::glyph::Glyph;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::prelude::Color;
|
||||
@@ -14,15 +13,27 @@ pub fn rgba8_to_color(c: Rgba8) -> Color {
|
||||
Color::Rgb(c.r, c.g, c.b)
|
||||
}
|
||||
|
||||
/// Converts a [`Glyph`] to a single-character styled [`Span`].
|
||||
/// The character this terminal front-end draws for `glyph`.
|
||||
///
|
||||
/// The tile index is mapped to a CP437 character; fg and bg are both applied.
|
||||
/// A glyph carries its character directly, so this is almost the identity — the
|
||||
/// one case that needs handling is the transparent sentinel `'\0'`, which means
|
||||
/// "draw nothing". By the time a glyph reaches a renderer, `Board::glyph_at` has
|
||||
/// already exhausted its precedence chain, so there is nothing underneath left to
|
||||
/// reveal and the cell is simply blank. Writing the NUL through to the terminal
|
||||
/// buffer would emit an unprintable character instead.
|
||||
///
|
||||
/// How a sentinel looks on screen is a front-end decision, which is why this
|
||||
/// lives here rather than in kiln-core.
|
||||
pub fn glyph_char(glyph: Glyph) -> char {
|
||||
if glyph.is_visible() { glyph.tile } else { ' ' }
|
||||
}
|
||||
|
||||
/// Converts a [`Glyph`] to a single-character styled [`Span`], applying fg and bg.
|
||||
pub fn glyph_to_span(glyph: Glyph) -> Span<'static> {
|
||||
let ch = tile_to_char(glyph.tile).to_string();
|
||||
let style = Style::default()
|
||||
.fg(rgba8_to_color(glyph.fg))
|
||||
.bg(rgba8_to_color(glyph.bg));
|
||||
Span::styled(ch, style)
|
||||
Span::styled(glyph_char(glyph).to_string(), style)
|
||||
}
|
||||
|
||||
/// Returns true if two `Rect`s share at least one cell.
|
||||
|
||||
Reference in New Issue
Block a user