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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Mask generator.

use super::geometry::{Origin, Placement, Transform, Vector};
use super::path_data::{apply, PathData};
use super::scratch::Scratch;
use super::style::{Fill, Style};

use crate::lib::Vec;
use core::cell::RefCell;

/// The desired output image format for rendering.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Format {
    /// 8-bit alpha mask.
    Alpha,
    /// 32-bit RGBA subpixel mask with 1/3 pixel offsets for the red and
    /// blue channels.
    Subpixel,
    /// 32-bit RGBA subpixel mask with custom offsets.
    CustomSubpixel([f32; 3]),
}

impl Format {
    /// Creates a format for BGRA subpixel rendering.
    pub fn subpixel_bgra() -> Self {
        Self::CustomSubpixel([0.3, 0., -0.3])
    }

    /// Returns the necessary buffer size to hold an image of the specified
    /// width and height with this format.
    pub fn buffer_size(self, width: u32, height: u32) -> usize {
        (width
            * height
            * match self {
                Self::Alpha => 1,
                _ => 4,
            }) as usize
    }
}

impl Default for Format {
    fn default() -> Self {
        Self::Alpha
    }
}

/// Builder for configuring and rendering a mask.
pub struct Mask<'a, 's, D> {
    data: D,
    style: Style<'a>,
    transform: Option<Transform>,
    format: Format,
    origin: Origin,
    offset: Vector,
    render_offset: Vector,
    width: u32,
    height: u32,
    explicit_size: bool,
    has_size: bool,
    bounds_offset: Vector,
    scratch: RefCell<Option<&'s mut Scratch>>,
}

impl<'a, 's, D> Mask<'a, 's, D>
where
    D: PathData,
{
    /// Creates a new mask builder for the specified path data.
    pub fn new(data: D) -> Self {
        Self {
            data,
            style: Style::Fill(Fill::NonZero),
            transform: None,
            format: Format::Alpha,
            origin: Origin::TopLeft,
            offset: Vector::ZERO,
            render_offset: Vector::ZERO,
            width: 0,
            height: 0,
            explicit_size: false,
            has_size: false,
            bounds_offset: Vector::ZERO,
            scratch: RefCell::new(None),
        }
    }

    /// Creates a new mask builder for the specified path data and scratch memory.
    pub fn with_scratch(data: D, scratch: &'s mut Scratch) -> Self {
        Self {
            data,
            style: Style::Fill(Fill::NonZero),
            transform: None,
            format: Format::Alpha,
            origin: Origin::TopLeft,
            offset: Vector::ZERO,
            render_offset: Vector::ZERO,
            width: 0,
            height: 0,
            explicit_size: false,
            has_size: false,
            bounds_offset: Vector::ZERO,
            scratch: RefCell::new(Some(scratch)),
        }
    }

    /// Sets the style of the path. The default is a non-zero fill.
    pub fn style(&mut self, style: impl Into<Style<'a>>) -> &mut Self {
        self.style = style.into();
        self
    }

    /// Sets the transformation matrix of the path.
    pub fn transform(&mut self, transform: Option<Transform>) -> &mut Self {
        self.transform = transform;
        self
    }

    /// Sets the desired format of the mask. The default value is an 8-bit
    /// alpha format.
    pub fn format(&mut self, format: Format) -> &mut Self {
        self.format = format;
        self
    }

    /// Sets the origin that defines the coordinate system for the mask.
    pub fn origin(&mut self, origin: Origin) -> &mut Self {
        self.origin = origin;
        self
    }

    /// Sets the offset for the path.
    pub fn offset(&mut self, offset: impl Into<Vector>) -> &mut Self {
        self.offset = offset.into();
        self
    }

    /// Sets an explicit size for the mask. If left unspecified, the size will
    /// be computed from the bounding box of the path after applying any
    /// relevant style, offset and transform.
    pub fn size(&mut self, width: u32, height: u32) -> &mut Self {
        self.width = width;
        self.height = height;
        self.explicit_size = true;
        self.has_size = true;
        self
    }

    /// Sets an additional rendering offset for the mask. This offset does not
    /// affect bounds or size computations and is only applied during
    /// rendering.
    pub fn render_offset(&mut self, offset: impl Into<Vector>) -> &mut Self {
        self.render_offset = offset.into();
        self
    }

    /// Invokes a closure with the format, width and height of the mask provided
    /// as arguments. This is primarily useful for preparing a target buffer without
    /// interrupting the call chain.
    pub fn inspect(&mut self, mut f: impl FnMut(Format, u32, u32)) -> &mut Self {
        self.ensure_size();
        f(self.format, self.width, self.height);
        self
    }

    /// Renders the mask into a byte buffer. If specified, the pitch describes
    /// the number of bytes between subsequent rows of the target buffer. This
    /// is primarily useful for rendering into tiled images such as texture
    /// atlases. If left unspecified, the buffer is assumed to be linear and
    /// tightly packed.
    pub fn render_into(&self, buffer: &mut [u8], pitch: Option<usize>) -> Placement {
        let (offset, placement) = self.placement();
        let pitch = match pitch {
            Some(pitch) => pitch,
            _ => {
                placement.width as usize
                    * match self.format {
                        Format::Alpha => 1,
                        _ => 4,
                    }
            }
        };
        render(self, offset, &placement, buffer, pitch);
        placement
    }

    /// Renders the mask to a newly allocated buffer.
    pub fn render(&self) -> (Vec<u8>, Placement) {
        let mut buf = Vec::new();
        let (offset, placement) = self.placement();
        buf.resize(
            self.format.buffer_size(placement.width, placement.height),
            0,
        );
        let pitch = placement.width as usize
            * match self.format {
                Format::Alpha => 1,
                _ => 4,
            };
        render(self, offset, &placement, &mut buf, pitch);
        (buf, placement)
    }

    fn ensure_size(&mut self) {
        if self.has_size {
            return;
        }
        let (offset, placement) = self.placement();
        self.bounds_offset = offset;
        self.width = placement.width;
        self.height = placement.height;
        self.explicit_size = false;
        self.has_size = true;
    }

    fn placement(&self) -> (Vector, Placement) {
        let mut placement = Placement {
            left: 0,
            top: 0,
            width: self.width,
            height: self.height,
        };
        let mut offset = self.offset;
        if self.explicit_size {
            return (offset, placement);
        } else if !self.has_size {
            let mut scratch = self.scratch.borrow_mut();
            let mut bounds = if let Some(scratch) = scratch.as_mut() {
                scratch.bounds(&self.data, self.style, self.transform)
            } else {
                super::bounds(&self.data, self.style, self.transform)
            };
            bounds.min = (bounds.min + self.offset).floor();
            bounds.max = (bounds.max + self.offset).ceil();
            offset = Vector::new(-bounds.min.x + 1., -bounds.min.y);
            placement.width = bounds.width() as u32 + 2;
            placement.height = bounds.height() as u32;
        } else {
            offset = self.bounds_offset;
        }
        placement.left = -offset.x as i32;
        placement.top = if self.origin == Origin::BottomLeft {
            (-offset.y).floor() + self.height as f32
        } else {
            -offset.y
        } as i32;
        (offset, placement)
    }
}

pub fn render<'a, 'c, D>(
    mask: &'a Mask<'a, 'c, D>,
    offset: Vector,
    placement: &Placement,
    buf: &mut [u8],
    pitch: usize,
) where
    D: PathData,
{
    let y_up = mask.origin == Origin::BottomLeft;
    let (is_subpx, subpx) = match mask.format {
        Format::Alpha => (false, [Vector::ZERO; 3]),
        Format::Subpixel => (
            true,
            [Vector::new(-0.3, 0.), Vector::ZERO, Vector::new(0.3, 0.)],
        ),
        Format::CustomSubpixel(subpx) => (
            true,
            [
                Vector::new(subpx[0], 0.),
                Vector::new(subpx[1], 0.),
                Vector::new(subpx[2], 0.),
            ],
        ),
    };
    let fill = match mask.style {
        Style::Fill(fill) => fill,
        _ => Fill::NonZero,
    };
    let w = placement.width;
    let h = placement.height;
    let shift = offset + mask.render_offset;
    let data = &mask.data;
    let style = mask.style;
    let transform = mask.transform;
    let mut scratch = mask.scratch.borrow_mut();
    use super::raster::{AdaptiveStorage, Rasterizer};
    if let Some(scratch) = scratch.as_mut() {
        let mut ras = Rasterizer::new(&mut scratch.render);
        let inner = &mut scratch.inner;
        if is_subpx {
            ras.rasterize_write(
                shift + subpx[0],
                w,
                h,
                &mut |r| {
                    inner.apply(data.clone(), &style, transform, r);
                },
                fill,
                pitch,
                y_up,
                &mut |row_offset, x, count, coverage| {
                    let buf = &mut buf[row_offset..];
                    let mut i = 0;
                    let mut j = x * 4;
                    while i < count {
                        buf[j] = coverage;
                        i += 1;
                        j += 4;
                    }
                },
            );
            ras.rasterize_write(
                shift + subpx[1],
                w,
                h,
                &mut |r| {
                    inner.apply(data, &style, transform, r);
                },
                fill,
                pitch,
                y_up,
                &mut |row_offset, x, count, coverage| {
                    let buf = &mut buf[row_offset..];
                    let mut i = 0;
                    let mut j = x * 4 + 1;
                    while i < count {
                        buf[j] = coverage;
                        i += 1;
                        j += 4;
                    }
                },
            );
            ras.rasterize_write(
                shift + subpx[2],
                w,
                h,
                &mut |r| {
                    inner.apply(data, &style, transform, r);
                },
                fill,
                pitch,
                y_up,
                &mut |row_offset, x, count, coverage| {
                    let buf = &mut buf[row_offset..];
                    let mut i = 0;
                    let mut j = x * 4 + 2;
                    while i < count {
                        buf[j] = coverage;
                        i += 1;
                        j += 4;
                    }
                },
            );
        } else {
            ras.rasterize(
                shift,
                w,
                h,
                &mut |r| {
                    inner.apply(data, &style, transform, r);
                },
                fill,
                buf,
                pitch,
                y_up,
            );
        }
    } else {
        let mut storage = AdaptiveStorage::new();
        let mut ras = Rasterizer::new(&mut storage);
        if is_subpx {
            ras.rasterize_write(
                shift + subpx[0],
                w,
                h,
                &mut |r| {
                    apply(data, style, transform, r);
                },
                fill,
                pitch,
                y_up,
                &mut |row_offset, x, count, coverage| {
                    let buf = &mut buf[row_offset..];
                    let mut i = 0;
                    let mut j = x * 4;
                    while i < count {
                        buf[j] = coverage;
                        i += 1;
                        j += 4;
                    }
                },
            );
            ras.rasterize_write(
                shift + subpx[1],
                w,
                h,
                &mut |r| {
                    apply(data, style, transform, r);
                },
                fill,
                pitch,
                y_up,
                &mut |row_offset, x, count, coverage| {
                    let buf = &mut buf[row_offset..];
                    let mut i = 0;
                    let mut j = x * 4 + 1;
                    while i < count {
                        buf[j] = coverage;
                        i += 1;
                        j += 4;
                    }
                },
            );
            ras.rasterize_write(
                shift + subpx[2],
                w,
                h,
                &mut |r| {
                    apply(data, style, transform, r);
                },
                fill,
                pitch,
                y_up,
                &mut |row_offset, x, count, coverage| {
                    let buf = &mut buf[row_offset..];
                    let mut i = 0;
                    let mut j = x * 4 + 2;
                    while i < count {
                        buf[j] = coverage;
                        i += 1;
                        j += 4;
                    }
                },
            );
        } else {
            ras.rasterize(
                shift,
                w,
                h,
                &mut |r| {
                    apply(data, style, transform, r);
                },
                fill,
                buf,
                pitch,
                y_up,
            );
        }
    }
}