use super::{BrushShading, OPEN_LEFT_BIT, OPEN_RIGHT_BIT, ShapeRecorder, rect_row};
use crate::{
BlendMode, LineFrame, LineGeometry, Rect, ShapeRecordBody, ShapeRecordCurve, Trapezoid,
float::{at_least, at_most},
};
pub(super) struct RowRun {
blend: BlendMode,
shading: BrushShading,
plain: bool,
appended: u32,
}
impl RowRun {
pub(super) fn new(blend: BlendMode, shading: BrushShading) -> Self {
Self {
blend,
shading,
plain: false,
appended: 0,
}
}
#[inline]
pub(super) fn push(
&mut self,
recorder: &mut ShapeRecorder,
body: impl FnOnce() -> ShapeRecordBody,
curve: impl FnOnce() -> ShapeRecordCurve,
) {
if self.plain {
let flag = self.shading.flag();
recorder.tables.shapes.push_row_with(
|| {
let mut body = body();
body.flags |= flag;
body
},
curve,
);
self.appended += 1;
return;
}
recorder.push_shaded(body(), curve(), [0.0; 4], (self.blend, self.shading), None);
self.plain = recorder.extends_plainly();
}
pub(super) fn push_trapezoids(
mut self,
recorder: &mut ShapeRecorder,
trapezoids: &[Trapezoid],
brush: (u32, [f32; 4]),
flags: u32,
radii: [f32; 4],
) {
let Some((first, rest)) = trapezoids.split_first() else {
return;
};
let mut min = [f32::INFINITY; 2];
let mut max = [f32::NEG_INFINITY; 2];
let first_bounds = include_slice_bounds(first, &mut min, &mut max);
self.push(
recorder,
|| slice_body(first, first_bounds, brush, flags),
|| slice_curve(first, radii),
);
if self.plain {
let shaded_flags = flags | self.shading.flag();
let bodies = rest.iter().map(|trapezoid| {
let bounds = include_slice_bounds(trapezoid, &mut min, &mut max);
self.appended += 1;
slice_body(trapezoid, bounds, brush, shaded_flags)
});
let curves = rest.iter().map(|trapezoid| slice_curve(trapezoid, radii));
recorder.tables.shapes.extend_rows(bodies, curves);
} else {
for trapezoid in rest {
let bounds = include_slice_bounds(trapezoid, &mut min, &mut max);
self.push(
recorder,
|| slice_body(trapezoid, bounds, brush, flags),
|| slice_curve(trapezoid, radii),
);
}
}
recorder.include_bounds(Rect {
x: min[0],
y: min[1],
width: max[0] - min[0],
height: max[1] - min[1],
});
self.close(recorder);
}
pub(super) fn close(self, recorder: &mut ShapeRecorder) {
if self.appended == 0 {
return;
}
if let Some(segment) = recorder.tables_mut().segments.last_mut() {
segment.count += self.appended;
}
}
}
#[inline]
pub(super) fn line_body(
line: &LineGeometry,
ends: Rect,
(brush, color): (u32, [f32; 4]),
stroke_width: f32,
flags: u32,
) -> ShapeRecordBody {
ShapeRecordBody {
rect: rect_row(ends),
color,
stroke_width,
flags,
brush,
placement: 0,
arc_geometry: [line.start.x, line.start.y, line.end.x, line.end.y],
}
}
#[inline]
pub(super) fn line_curve(frame: &LineFrame) -> ShapeRecordCurve {
ShapeRecordCurve {
radii: [0.0; 4],
arc_normalized: [frame.direction.x, frame.direction.y, frame.half_length, 0.0],
}
}
#[inline]
fn include_slice_bounds(trapezoid: &Trapezoid, min: &mut [f32; 2], max: &mut [f32; 2]) -> Rect {
let top = at_most(trapezoid.top[0], trapezoid.top[1]);
let bottom = at_least(trapezoid.bottom[0], trapezoid.bottom[1]);
min[0] = at_most(min[0], trapezoid.left);
min[1] = at_most(min[1], top);
max[0] = at_least(max[0], trapezoid.right);
max[1] = at_least(max[1], bottom);
Rect {
x: trapezoid.left,
y: top,
width: trapezoid.right - trapezoid.left,
height: bottom - top,
}
}
#[inline]
pub(super) fn slice_body(
trapezoid: &Trapezoid,
bounds: Rect,
(brush, color): (u32, [f32; 4]),
flags: u32,
) -> ShapeRecordBody {
let open = (u32::from(trapezoid.open_left) * OPEN_LEFT_BIT)
| (u32::from(trapezoid.open_right) * OPEN_RIGHT_BIT);
ShapeRecordBody {
rect: rect_row(bounds),
color,
stroke_width: 0.0,
flags: flags | open,
brush,
placement: 0,
arc_geometry: [
trapezoid.top[0],
trapezoid.top[1],
trapezoid.bottom[0],
trapezoid.bottom[1],
],
}
}
#[inline]
pub(super) fn slice_curve(trapezoid: &Trapezoid, radii: [f32; 4]) -> ShapeRecordCurve {
ShapeRecordCurve {
radii,
arc_normalized: [
trapezoid.left,
trapezoid.right,
trapezoid.right - trapezoid.left,
0.0,
],
}
}