use super::{BrushShading, OPEN_LEFT_BIT, OPEN_RIGHT_BIT, ShapeRecorder, rect_row};
use crate::{
BlendMode, LineFrame, LineGeometry, Rect, ShapeRecordBody, ShapeRecordCurve, Trapezoid,
};
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 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]
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,
],
}
}