Skip to main content

sequoia_draw/
lib.rs

1#![forbid(unsafe_code)]
2
3use sequoia_core::{Color, Quad, Rect};
4
5mod cache;
6mod flatten;
7mod gradient;
8mod path;
9mod raster;
10mod stroke;
11mod svg_path;
12mod transform;
13mod waveform;
14
15pub use cache::{DEFAULT_CAPACITY, Shape, ShapeCache, ShapeStats};
16pub use flatten::{Contour, flatten};
17pub use gradient::{Gradient, GradientStop};
18pub use path::{FillRule, Path, PathError, PathSeg, Radius};
19pub use raster::{FillStyle, RasterOptions, fill, fill_contours, fill_styled};
20pub use stroke::{Dash, LineCap, LineJoin, StrokeStyle, stroke, stroke_path};
21pub use transform::Transform;
22pub use waveform::{
23    MinMax, Peaks, WaveformCache, WaveformOpts, bake_strip, layout_bars, waveform_line_points,
24    white_noise,
25};
26
27pub const TOLERANCE: f32 = 0.25;
28
29pub const DEFAULT_OPTIONS: RasterOptions = RasterOptions {
30    tolerance: TOLERANCE,
31    row_height: 1.0,
32    merge_tol: 0.35,
33    aa_samples: 4,
34    analytic: true,
35};
36
37#[derive(Clone, Copy, Debug, PartialEq)]
38pub struct Span {
39    pub rect: Rect,
40
41    pub radius: f32,
42
43    pub alpha: f32,
44}
45
46impl Span {
47    pub fn rect(rect: Rect) -> Self {
48        Self {
49            rect,
50            radius: 0.0,
51            alpha: 1.0,
52        }
53    }
54
55    pub fn rounded(rect: Rect, radius: f32) -> Self {
56        Self {
57            rect,
58            radius,
59            alpha: 1.0,
60        }
61    }
62
63    pub fn solid(rect: Rect) -> Self {
64        Self::rect(rect)
65    }
66
67    pub fn edge(rect: Rect, coverage: f32) -> Self {
68        Self::rect(rect).with_alpha(coverage)
69    }
70
71    #[must_use]
72    pub fn with_alpha(mut self, alpha: f32) -> Self {
73        self.alpha = alpha.clamp(0.0, 1.0);
74        self
75    }
76
77    pub fn quad(self, color: Color) -> Quad {
78        Quad {
79            rect: self.rect,
80            color: Color {
81                a: color.a * self.alpha,
82                ..color
83            },
84            radius: self.radius,
85            border_color: Color::TRANSPARENT,
86            border_width: 0.0,
87        }
88    }
89
90    pub fn is_visible(&self) -> bool {
91        !self.rect.is_empty() && self.alpha > 0.0
92    }
93}
94
95pub(crate) fn union_rect(a: Rect, b: Rect) -> Rect {
96    if a.is_empty() {
97        return b;
98    }
99    if b.is_empty() {
100        return a;
101    }
102    let x0 = a.left().min(b.left());
103    let y0 = a.top().min(b.top());
104    let x1 = a.right().max(b.right());
105    let y1 = a.bottom().max(b.bottom());
106    Rect::from_xywh(x0, y0, x1 - x0, y1 - y0)
107}