use super::MonoBitmap;
use super::profiles::{
DROPOUT, FLOW_UP, OVERSHOOT_BOTTOM, OVERSHOOT_TOP, Precision, Profile, Profiles,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Pass {
Vertical,
Horizontal,
}
impl Pass {
const fn pixel(self, e: i64, y: i64) -> (i64, i64) {
match self {
Self::Vertical => (e, y),
Self::Horizontal => (y, e),
}
}
}
pub(super) fn draw_sweep(
profiles: &mut Profiles,
precision: Precision,
pass: Pass,
bitmap: &mut MonoBitmap,
) {
let (Some(&min_y), Some(&top)) = (profiles.turns.first(), profiles.turns.last()) else {
return;
};
let max_y = top - 1;
let mut waiting: Vec<usize> = (0..profiles.list.len()).collect();
let (mut left, mut right) = (Vec::new(), Vec::new());
let turns = profiles.turns.clone();
let mut turns = turns[1..].iter().copied();
let mut y = min_y;
while y <= max_y {
waiting.retain(|&index| {
let profile = &profiles.list[index];
if profile.start != y {
return true;
}
let list = if profile.flags & FLOW_UP != 0 {
&mut left
} else {
&mut right
};
insert_sorted(list, &profiles.list, index);
false
});
let Some(y_turn) = turns.next() else {
return;
};
loop {
let dropouts = trace(profiles, precision, &left, &right, |x1, x2| {
span(bitmap, precision, pass, y, x1, x2);
});
if dropouts > 0 {
for (&l, &r) in left.iter().zip(&right) {
if profiles.list[l].flags & DROPOUT != 0 {
let (x1, x2) = (profiles.list[l].x, profiles.list[r].x);
drop_out(bitmap, precision, pass, y, x1, x2);
profiles.list[l].flags &= !DROPOUT;
}
}
}
increment(profiles, &mut left, 1);
increment(profiles, &mut right, -1);
y += 1;
if y >= y_turn {
break;
}
}
}
}
fn trace(
profiles: &mut Profiles,
precision: Precision,
left: &[usize],
right: &[usize],
mut fill: impl FnMut(i64, i64),
) -> usize {
let mut dropouts = 0;
for (&l, &r) in left.iter().zip(right) {
let (mut x1, mut x2) = (profiles.list[l].x, profiles.list[r].x);
if x1 > x2 {
(x1, x2) = (x2, x1);
}
if precision.ceiling(x1) <= precision.floor(x2) {
fill(x1, x2);
continue;
}
let (left_profile, right_profile) = (profiles.list[l], profiles.list[r]);
let Some((x1, x2)) =
dropout_pixels(precision, &left_profile, &right_profile, (l, r), x1, x2)
else {
continue;
};
profiles.list[l].x = x2;
profiles.list[r].x = x1;
profiles.list[l].flags |= DROPOUT;
dropouts += 1;
}
dropouts
}
fn dropout_pixels(
precision: Precision,
left: &Profile,
right: &Profile,
(l, r): (usize, usize),
x1: i64,
x2: i64,
) -> Option<(i64, i64)> {
let control = left.flags & 7;
if control & 2 != 0 {
return None;
}
if control & 1 != 0 {
let wide = x2 - x1 >= precision.half();
let upper_stub = left.height == 1 && left.next == r;
if upper_stub && !(left.flags & OVERSHOOT_TOP != 0 && wide) {
return None;
}
let lower_stub = left.offset == 0 && right.next == l;
if lower_stub && !(left.flags & OVERSHOOT_BOTTOM != 0 && wide) {
return None;
}
}
Some(if control & 4 != 0 {
let x2 = precision.smart(x1, x2);
let other = if x1 > x2 {
x2 + precision.one()
} else {
x2 - precision.one()
};
(other, x2)
} else {
(precision.ceiling(x1), precision.floor(x2))
})
}
fn insert_sorted(list: &mut Vec<usize>, profiles: &[Profile], index: usize) {
let x = profiles[index].x;
let at = list
.iter()
.position(|&other| profiles[other].x >= x)
.unwrap_or(list.len());
list.insert(at, index);
}
fn increment(profiles: &mut Profiles, list: &mut Vec<usize>, flow: i64) {
list.retain(|&index| {
let profile = profiles.list[index];
if profile.height <= 1 {
profiles.list[index].height = 0;
return false;
}
let offset = profile.offset + flow;
let x = profiles.crossing(&profile, offset);
let profile = &mut profiles.list[index];
profile.height -= 1;
profile.offset = offset;
profile.x = x;
true
});
list.sort_by_key(|&index| profiles.list[index].x);
}
fn span(bitmap: &mut MonoBitmap, p: Precision, pass: Pass, y: i64, x1: i64, x2: i64) {
match pass {
Pass::Vertical => {
let e1 = p.trunc(p.ceiling(x1)).max(0);
let e2 = p.trunc(p.floor(x2)).min(bitmap.right());
for x in e1..=e2 {
bitmap.set(x, y);
}
}
Pass::Horizontal => {
for (edge, aligned) in [(x1, p.ceiling(x1)), (x2, p.floor(x2))] {
let row = p.trunc(aligned);
if edge == aligned && (0..=bitmap.top()).contains(&row) {
bitmap.set(y, row);
}
}
}
}
}
fn drop_out(bitmap: &mut MonoBitmap, p: Precision, pass: Pass, y: i64, x1: i64, x2: i64) {
let (e1, e2) = (p.trunc(x1), p.trunc(x2));
let limit = match pass {
Pass::Vertical => bitmap.right(),
Pass::Horizontal => bitmap.top(),
};
let inside = |e: i64| (0..=limit).contains(&e);
let pixel = if inside(e1) {
let (x, row) = pass.pixel(e2, y);
if inside(e2) && bitmap.get(x, row) {
return;
}
e1
} else {
e2
};
if inside(pixel) {
let (x, row) = pass.pixel(pixel, y);
bitmap.set(x, row);
}
}