cranpose-ui-graphics 0.30.0

Geometry, units, paths and render data for Cranpose UI
Documentation
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},
};

// The rows of one run, a stroke's lines or a fill's slices, which share a
// brush, a blend, a shading and a fragment kind with no interior, and so a
// segment key. The first row takes `push_shaded`, which opens or joins a
// segment. A later row only extends that segment while it holds no band
// class; in an arc's segment it takes the full check, which can cut a new
// segment, after which every row extends that one. The run counts the rows
// it appends and adds them to the segment once, when it closes.
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,
        }
    }

    // Records the row `body` and `curve` make. They make it in its columns
    // when it extends the segment.
    #[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;
        }
    }
}

// The body of the segment `line`, whose ends' box is `ends`, painted by the
// interned `brush`: the ends ride in the arc columns. A line reads back
// nothing else, so it keeps no source arguments, which an arc keeps for its
// radius and angles.
#[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],
    }
}

// The curve of a segment whose frame is `frame`: the unit direction and
// half length the vertex stage orients its quad by.
#[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],
    }
}

// Bounds of `trapezoid`, widening `min` and `max` to enclose it.
#[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,
    }
}

// The body of `trapezoid`, whose box is `bounds`, painted by the interned
// `brush`; see `ShapeRecorder::push_trapezoids`.
#[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],
        ],
    }
}

// The curve of `trapezoid` against the brush rect `radii` holds.
#[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,
        ],
    }
}