use std::collections::HashMap;
use std::ops::RangeInclusive;
use crate::TextStyle;
use super::Position;
use super::cell::validate_cell_glyph;
use super::cell_primitives::{clip_line, line_points};
use crate::view::geometry::Size;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(super) struct LineConnections(u8);
impl LineConnections {
const UP: Self = Self(1);
const RIGHT: Self = Self(2);
const DOWN: Self = Self(4);
const LEFT: Self = Self(8);
pub(super) const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct LineContinuations(u8);
impl LineContinuations {
pub const NONE: Self = Self(0);
pub const START: Self = Self(1);
pub const END: Self = Self(2);
pub const BOTH: Self = Self(Self::START.0 | Self::END.0);
const fn includes_start(self) -> bool {
self.0 & Self::START.0 != 0
}
const fn includes_end(self) -> bool {
self.0 & Self::END.0 != 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LineGlyphs {
pub isolated: char,
pub end_up: char,
pub end_right: char,
pub end_down: char,
pub end_left: char,
pub vertical: char,
pub horizontal: char,
pub corner_down_right: char,
pub corner_down_left: char,
pub corner_up_right: char,
pub corner_up_left: char,
pub tee_right: char,
pub tee_down: char,
pub tee_left: char,
pub tee_up: char,
pub cross: char,
}
impl LineGlyphs {
pub const NORMAL: Self = Self {
isolated: '•',
end_up: '│',
end_right: '─',
end_down: '│',
end_left: '─',
vertical: '│',
horizontal: '─',
corner_down_right: '┌',
corner_down_left: '┐',
corner_up_right: '└',
corner_up_left: '┘',
tee_right: '├',
tee_down: '┬',
tee_left: '┤',
tee_up: '┴',
cross: '┼',
};
pub const ROUNDED: Self = Self {
corner_down_right: '╭',
corner_down_left: '╮',
corner_up_right: '╰',
corner_up_left: '╯',
..Self::NORMAL
};
pub const ASCII: Self = Self {
isolated: '*',
end_up: '|',
end_right: '-',
end_down: '|',
end_left: '-',
vertical: '|',
horizontal: '-',
corner_down_right: '+',
corner_down_left: '+',
corner_up_right: '+',
corner_up_left: '+',
tee_right: '+',
tee_down: '+',
tee_left: '+',
tee_up: '+',
cross: '+',
};
pub const THICK: Self = Self {
isolated: '•',
end_up: '┃',
end_right: '━',
end_down: '┃',
end_left: '━',
vertical: '┃',
horizontal: '━',
corner_down_right: '┏',
corner_down_left: '┓',
corner_up_right: '┗',
corner_up_left: '┛',
tee_right: '┣',
tee_down: '┳',
tee_left: '┫',
tee_up: '┻',
cross: '╋',
};
pub const DOUBLE: Self = Self {
isolated: '•',
end_up: '║',
end_right: '═',
end_down: '║',
end_left: '═',
vertical: '║',
horizontal: '═',
corner_down_right: '╔',
corner_down_left: '╗',
corner_up_right: '╚',
corner_up_left: '╝',
tee_right: '╠',
tee_down: '╦',
tee_left: '╣',
tee_up: '╩',
cross: '╬',
};
pub const HIDDEN: Self = Self {
isolated: ' ',
end_up: ' ',
end_right: ' ',
end_down: ' ',
end_left: ' ',
vertical: ' ',
horizontal: ' ',
corner_down_right: ' ',
corner_down_left: ' ',
corner_up_right: ' ',
corner_up_left: ' ',
tee_right: ' ',
tee_down: ' ',
tee_left: ' ',
tee_up: ' ',
cross: ' ',
};
pub(super) fn glyph(self, connections: LineConnections) -> char {
match connections.0 {
0 => self.isolated,
1 => self.end_up,
2 => self.end_right,
4 => self.end_down,
8 => self.end_left,
5 => self.vertical,
10 => self.horizontal,
6 => self.corner_down_right,
12 => self.corner_down_left,
3 => self.corner_up_right,
9 => self.corner_up_left,
7 => self.tee_right,
14 => self.tee_down,
13 => self.tee_left,
11 => self.tee_up,
_ => self.cross,
}
}
fn validate(self) {
for glyph in [
self.isolated,
self.end_up,
self.end_right,
self.end_down,
self.end_left,
self.vertical,
self.horizontal,
self.corner_down_right,
self.corner_down_left,
self.corner_up_right,
self.corner_up_left,
self.tee_right,
self.tee_down,
self.tee_left,
self.tee_up,
self.cross,
] {
let mut encoded = [0; 4];
let glyph = glyph.encode_utf8(&mut encoded);
validate_cell_glyph(glyph);
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Segment {
from: Position,
to: Position,
axis: SegmentAxis,
continuations: LineContinuations,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SegmentAxis {
Horizontal,
Vertical,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LineNetwork {
segments: Vec<Segment>,
glyphs: LineGlyphs,
style: TextStyle,
}
impl LineNetwork {
pub fn new(glyphs: LineGlyphs, style: TextStyle) -> Self {
glyphs.validate();
Self {
segments: Vec::new(),
glyphs,
style,
}
}
pub fn horizontal(&mut self, y: i64, columns: RangeInclusive<i64>) -> &mut Self {
self.horizontal_with(y, columns, LineContinuations::NONE)
}
pub fn horizontal_with(
&mut self,
y: i64,
columns: RangeInclusive<i64>,
continuations: LineContinuations,
) -> &mut Self {
if columns.is_empty() {
return self;
}
self.segments.push(Segment {
from: Position::new(*columns.start(), y),
to: Position::new(*columns.end(), y),
axis: SegmentAxis::Horizontal,
continuations,
});
self
}
pub fn vertical(&mut self, x: i64, rows: RangeInclusive<i64>) -> &mut Self {
self.vertical_with(x, rows, LineContinuations::NONE)
}
pub fn vertical_with(
&mut self,
x: i64,
rows: RangeInclusive<i64>,
continuations: LineContinuations,
) -> &mut Self {
if rows.is_empty() {
return self;
}
self.segments.push(Segment {
from: Position::new(x, *rows.start()),
to: Position::new(x, *rows.end()),
axis: SegmentAxis::Vertical,
continuations,
});
self
}
pub(super) fn rasterize(&self, size: Size) -> Vec<NetworkCell> {
let mut cells = Vec::<NetworkCell>::new();
let mut indexes = HashMap::<Position, usize>::new();
for segment in &self.segments {
if let Some((from, to)) = clip_line(segment.from, segment.to, size) {
for position in line_points(from, to) {
let connections = segment_connections(position, *segment);
if let Some(index) = indexes.get(&position).copied() {
cells[index].connections = cells[index].connections.union(connections);
} else {
indexes.insert(position, cells.len());
cells.push(NetworkCell {
position,
connections,
});
}
}
}
}
cells
}
pub(super) const fn glyphs(&self) -> LineGlyphs {
self.glyphs
}
pub(super) const fn style(&self) -> &TextStyle {
&self.style
}
}
pub(super) struct NetworkCell {
pub(super) position: Position,
pub(super) connections: LineConnections,
}
fn segment_connections(position: Position, segment: Segment) -> LineConnections {
let (toward_start, toward_end) = match segment.axis {
SegmentAxis::Horizontal => (LineConnections::LEFT, LineConnections::RIGHT),
SegmentAxis::Vertical => (LineConnections::UP, LineConnections::DOWN),
};
let mut connections = LineConnections::default();
if position != segment.from || segment.continuations.includes_start() {
connections = connections.union(toward_start);
}
if position != segment.to || segment.continuations.includes_end() {
connections = connections.union(toward_end);
}
connections
}