summaryrefslogtreecommitdiff
path: root/rust/crates/mask-defs/src/shapes.rs
blob: 890fc8fe0146f5d43e0ec4920e27e6942ce66bcd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use scene::RectangleMaskParams;
use tiny_skia::{Path, PathBuilder, Transform};

use crate::geometry::{box_like_geometry, rotate_point, stroke_offset};

const STAR_INNER_RADIUS_RATIO: f64 = 0.45;
const STAR_VERTEX_COUNT: usize = 10;

fn polygon_path(points: &[(f64, f64)]) -> Option<Path> {
    let mut builder = PathBuilder::new();
    let first = points.first()?;
    builder.move_to(first.0 as f32, first.1 as f32);
    for point in &points[1..] {
        builder.line_to(point.0 as f32, point.1 as f32);
    }
    builder.close();
    builder.finish()
}

fn rotated_rect_path(
    center_x: f64,
    center_y: f64,
    half_width: f64,
    half_height: f64,
    rotation_rad: f64,
) -> Option<Path> {
    let corners = [
        (center_x - half_width, center_y - half_height),
        (center_x + half_width, center_y - half_height),
        (center_x + half_width, center_y + half_height),
        (center_x - half_width, center_y + half_height),
    ];
    let rotated: Vec<(f64, f64)> = corners
        .iter()
        .map(|&(x, y)| rotate_point(x, y, center_x, center_y, rotation_rad))
        .collect();
    polygon_path(&rotated)
}

fn rotated_ellipse_path(
    center_x: f64,
    center_y: f64,
    radius_x: f64,
    radius_y: f64,
    rotation_rad: f64,
) -> Option<Path> {
    let rect = tiny_skia::Rect::from_ltrb(
        -radius_x as f32,
        -radius_y as f32,
        radius_x as f32,
        radius_y as f32,
    )?;
    let path = PathBuilder::from_oval(rect)?;
    let cos = rotation_rad.cos() as f32;
    let sin = rotation_rad.sin() as f32;
    path.transform(Transform::from_row(
        cos,
        sin,
        -sin,
        cos,
        center_x as f32,
        center_y as f32,
    ))
}

fn heart_path(
    center_x: f64,
    center_y: f64,
    half_width: f64,
    half_height: f64,
    rotation_rad: f64,
) -> Option<Path> {
    let to_point = |local_x: f64, local_y: f64| {
        rotate_point(
            center_x + local_x,
            center_y + local_y,
            center_x,
            center_y,
            rotation_rad,
        )
    };

    let start = to_point(0.0, -half_height * 0.475);
    let right_control1 = to_point(half_width, -half_height * 1.225);
    let right_control2 = to_point(half_width, -half_height * 0.125);
    let bottom = to_point(0.0, half_height * 0.725);
    let left_control1 = to_point(-half_width, -half_height * 0.125);
    let left_control2 = to_point(-half_width, -half_height * 1.225);

    let mut builder = PathBuilder::new();
    builder.move_to(start.0 as f32, start.1 as f32);
    builder.cubic_to(
        right_control1.0 as f32,
        right_control1.1 as f32,
        right_control2.0 as f32,
        right_control2.1 as f32,
        bottom.0 as f32,
        bottom.1 as f32,
    );
    builder.cubic_to(
        left_control1.0 as f32,
        left_control1.1 as f32,
        left_control2.0 as f32,
        left_control2.1 as f32,
        start.0 as f32,
        start.1 as f32,
    );
    builder.close();
    builder.finish()
}

fn diamond_path(
    center_x: f64,
    center_y: f64,
    half_width: f64,
    half_height: f64,
    rotation_rad: f64,
) -> Option<Path> {
    let points = [
        (center_x, center_y - half_height),
        (center_x + half_width, center_y),
        (center_x, center_y + half_height),
        (center_x - half_width, center_y),
    ];
    let rotated: Vec<(f64, f64)> = points
        .iter()
        .map(|&(x, y)| rotate_point(x, y, center_x, center_y, rotation_rad))
        .collect();
    polygon_path(&rotated)
}

fn star_path(
    center_x: f64,
    center_y: f64,
    half_width: f64,
    half_height: f64,
    rotation_rad: f64,
) -> Option<Path> {
    let mut points = Vec::with_capacity(STAR_VERTEX_COUNT);
    for index in 0..STAR_VERTEX_COUNT {
        let is_outer = index % 2 == 0;
        let ratio = if is_outer { 1.0 } else { STAR_INNER_RADIUS_RATIO };
        let angle = (index as f64 * std::f64::consts::PI) / 5.0 - std::f64::consts::FRAC_PI_2;
        let x = center_x + half_width * ratio * angle.cos();
        let y = center_y + half_height * ratio * angle.sin();
        points.push(rotate_point(x, y, center_x, center_y, rotation_rad));
    }
    polygon_path(&points)
}

fn box_like_body_path(
    params: &RectangleMaskParams,
    width: f64,
    height: f64,
    build: fn(f64, f64, f64, f64, f64) -> Option<Path>,
) -> Option<Path> {
    let geometry = box_like_geometry(params, width, height);
    build(
        geometry.center_x,
        geometry.center_y,
        geometry.mask_width / 2.0,
        geometry.mask_height / 2.0,
        geometry.rotation_rad,
    )
}

fn box_like_stroke_path(
    params: &RectangleMaskParams,
    width: f64,
    height: f64,
    build: fn(f64, f64, f64, f64, f64) -> Option<Path>,
) -> Option<Path> {
    let geometry = box_like_geometry(params, width, height);
    let offset = stroke_offset(params.stroke_align, params.stroke_width);
    build(
        geometry.center_x,
        geometry.center_y,
        (geometry.mask_width / 2.0 + offset).max(1.0),
        (geometry.mask_height / 2.0 + offset).max(1.0),
        geometry.rotation_rad,
    )
}

pub fn rectangle_body_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_body_path(params, width, height, rotated_rect_path)
}

pub fn rectangle_stroke_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_stroke_path(params, width, height, rotated_rect_path)
}

pub fn ellipse_body_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_body_path(params, width, height, rotated_ellipse_path)
}

pub fn ellipse_stroke_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_stroke_path(params, width, height, rotated_ellipse_path)
}

pub fn heart_body_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_body_path(params, width, height, heart_path)
}

pub fn heart_stroke_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_stroke_path(params, width, height, heart_path)
}

pub fn diamond_body_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_body_path(params, width, height, diamond_path)
}

pub fn diamond_stroke_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_stroke_path(params, width, height, diamond_path)
}

pub fn star_body_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_body_path(params, width, height, star_path)
}

pub fn star_stroke_path(params: &RectangleMaskParams, width: f64, height: f64) -> Option<Path> {
    box_like_stroke_path(params, width, height, star_path)
}

fn cinematic_bars_dimensions(params: &RectangleMaskParams, width: f64, height: f64) -> (f64, f64) {
    (
        (params.width * width).max(width),
        params.height.max(0.01) * height,
    )
}

pub fn cinematic_bars_body_path(
    params: &RectangleMaskParams,
    width: f64,
    height: f64,
) -> Option<Path> {
    let (mask_width, mask_height) = cinematic_bars_dimensions(params, width, height);
    rotated_rect_path(
        width / 2.0 + params.center_x * width,
        height / 2.0 + params.center_y * height,
        mask_width / 2.0,
        mask_height / 2.0,
        params.rotation.to_radians(),
    )
}

pub fn cinematic_bars_stroke_path(
    params: &RectangleMaskParams,
    width: f64,
    height: f64,
) -> Option<Path> {
    let (mask_width, mask_height) = cinematic_bars_dimensions(params, width, height);
    let offset = stroke_offset(params.stroke_align, params.stroke_width);
    rotated_rect_path(
        width / 2.0 + params.center_x * width,
        height / 2.0 + params.center_y * height,
        (mask_width / 2.0 + offset).max(1.0),
        (mask_height / 2.0 + offset).max(1.0),
        params.rotation.to_radians(),
    )
}