anyrender 0.15.0

2D Canvas abstraction
Documentation
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
//! Pattern paints: a recorded [`Scene`] repeated as a tile.

use std::sync::{
    Arc,
    atomic::{AtomicU64, Ordering},
};

use kurbo::{Affine, Rect, Shape, Stroke, StrokeOpts, Vec2};
use peniko::{BlendMode, Color, Compose, Fill, FontData, Mix, StyleRef};

use crate::{AntiAliasing, Glyph, NormalizedCoord, PaintScene, Scene};

static PATTERN_ID_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Patterns covering more tiles than this are not drawn by [`fill_pattern_tiles`] and
/// [`stroke_pattern_tiles`].
/// The fallback draws nothing when it would draw more than this many commands (tiles times the
/// commands in the pattern's scene).
const MAX_PATTERN_COMMANDS: f64 = 4_000_000.0;

/// Rasterized tiles are downscaled to at most this many pixels, and this many on each side.
const MAX_TILE_PIXELS: f64 = 4096.0 * 4096.0;
const MAX_TILE_SIDE: f64 = 16384.0;

/// The pixel size at which backends rasterize one tile of a pattern drawn with `transform`
/// (mapping pattern space to pixels), or `None` if the tile is empty.
///
/// This is intended for use by backends.
#[doc(hidden)]
pub fn pattern_raster_size(tile: Rect, transform: Affine) -> Option<(u32, u32)> {
    let [a, b, c, d, _, _] = transform.as_coeffs();
    let mut width = (tile.width() * a.hypot(b)).round().max(1.0);
    let mut height = (tile.height() * c.hypot(d)).round().max(1.0);
    if !(width.is_finite() && height.is_finite() && tile.width() > 0.0 && tile.height() > 0.0) {
        return None;
    }
    let scale =
        (MAX_TILE_SIDE / width.max(height)).min((MAX_TILE_PIXELS / (width * height)).sqrt());
    if scale < 1.0 {
        width = (width * scale).floor().max(1.0);
        height = (height * scale).floor().max(1.0);
    }
    Some((width as u32, height as u32))
}

/// A paint that repeats the content of a [`Scene`] in a grid of tiles.
///
/// The scene is drawn in pattern space, where one tile covers `tile`, and the tile is repeated
/// with a period of `tile.width()` by `tile.height()` in both directions. Content outside of
/// `tile` is clipped. Pattern space is mapped to the space of the shape being painted by the
/// brush transform of the draw call.
///
/// A pattern is immutable and has an [`id`](Self::id) that is unique to it and kept by its
/// clones, so backends can cache the rasterized tile across draws and frames.
#[derive(Clone, Debug)]
pub struct Pattern {
    id: u64,
    scene: Arc<Scene>,
    tile: Rect,
}

impl Pattern {
    /// Creates a pattern that repeats the area `tile` of `scene`.
    pub fn new(scene: impl Into<Arc<Scene>>, tile: Rect) -> Self {
        Self {
            id: PATTERN_ID_COUNTER.fetch_add(1, Ordering::Relaxed),
            scene: scene.into(),
            tile,
        }
    }

    /// An id unique to this pattern (and its clones).
    pub fn id(&self) -> u64 {
        self.id
    }

    /// The content of the pattern, in pattern space.
    pub fn scene(&self) -> &Scene {
        &self.scene
    }

    /// The tile, in pattern space.
    pub fn tile(&self) -> Rect {
        self.tile
    }
}

impl PartialEq for Pattern {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

/// Fills `shape` with `pattern` by drawing a clipped copy of the pattern's scene for every tile
/// that intersects the shape. This is a fallback for backends that can't paint with a
/// pattern directly. Takes the same arguments as [`PaintScene::fill`], plus `cull_rect`: the
/// area of the painter's space that can be drawn to (such as the viewport), if known. Tiles
/// outside it are skipped.
///
/// This is intended for use by backends.
#[doc(hidden)]
pub fn fill_pattern_tiles<S: PaintScene>(
    painter: &mut S,
    pattern: &Pattern,
    fill: Fill,
    transform: Affine,
    brush_transform: Option<Affine>,
    shape: &impl Shape,
    cull_rect: Option<Rect>,
) {
    fill_pattern_tiles_with(
        painter,
        pattern,
        fill,
        transform,
        brush_transform,
        shape,
        cull_rect,
        |painter, tile_transform| pattern.scene().replay(painter, tile_transform),
    );
}

/// Strokes `shape` with `pattern`, as [`fill_pattern_tiles`] fills it. Takes the same arguments
/// as [`PaintScene::stroke`].
///
/// This is intended for use by backends.
#[doc(hidden)]
pub fn stroke_pattern_tiles<S: PaintScene>(
    painter: &mut S,
    pattern: &Pattern,
    style: &Stroke,
    transform: Affine,
    brush_transform: Option<Affine>,
    shape: &impl Shape,
    cull_rect: Option<Rect>,
) {
    stroke_pattern_tiles_with(
        painter,
        pattern,
        style,
        transform,
        brush_transform,
        shape,
        cull_rect,
        |painter, tile_transform| pattern.scene().replay(painter, tile_transform),
    );
}

/// Like [`fill_pattern_tiles`], but calls `draw_tile` to draw the content of each tile, with
/// the transform mapping pattern space to the painter's space for that tile. Backends use this
/// to draw a tile they converted to their own format once.
///
/// This is intended for use by backends.
#[doc(hidden)]
#[allow(clippy::too_many_arguments)]
pub fn fill_pattern_tiles_with<S: PaintScene>(
    painter: &mut S,
    pattern: &Pattern,
    fill: Fill,
    transform: Affine,
    brush_transform: Option<Affine>,
    shape: &impl Shape,
    cull_rect: Option<Rect>,
    draw_tile: impl FnMut(&mut S, Affine),
) {
    let brush_transform = brush_transform.unwrap_or(Affine::IDENTITY);
    if brush_transform.determinant() == 0.0 {
        return;
    }
    let bounds = brush_transform
        .inverse()
        .transform_rect_bbox(shape.bounding_box());
    painter.push_clip_layer(fill, transform, shape);
    draw_tiles(
        painter,
        pattern,
        transform * brush_transform,
        bounds,
        cull_rect,
        draw_tile,
    );
    painter.pop_layer();
}

/// Like [`stroke_pattern_tiles`], but calls `draw_tile` to draw the content of each tile, as
/// [`fill_pattern_tiles_with`] does.
///
/// This is intended for use by backends.
#[doc(hidden)]
#[allow(clippy::too_many_arguments)]
pub fn stroke_pattern_tiles_with<S: PaintScene>(
    painter: &mut S,
    pattern: &Pattern,
    style: &Stroke,
    transform: Affine,
    brush_transform: Option<Affine>,
    shape: &impl Shape,
    cull_rect: Option<Rect>,
    draw_tile: impl FnMut(&mut S, Affine),
) {
    if style.width <= 0.0 {
        return;
    }
    // Flatten to 0.1px in the painter's space.
    let [a, b, c, d, _, _] = (transform * brush_transform.unwrap_or(Affine::IDENTITY)).as_coeffs();
    let scale = a.hypot(b).max(c.hypot(d));
    let tolerance = if scale > 0.0 { 0.1 / scale } else { 0.1 };
    let outline = kurbo::stroke(
        shape.path_elements(tolerance),
        style,
        &StrokeOpts::default(),
        tolerance,
    );
    fill_pattern_tiles_with(
        painter,
        pattern,
        Fill::NonZero,
        transform,
        brush_transform,
        &outline,
        cull_rect,
        draw_tile,
    );
}

/// Draws a glyph run filled or stroked with `pattern`, by drawing the pattern's tiles into a
/// layer composited onto the glyphs with [`Compose::SrcIn`]. This is a fallback for backends
/// that can't paint glyphs with a pattern directly. Takes the same arguments as
/// [`PaintScene::draw_glyphs`], with pattern space being the glyph run's space.
///
/// This is intended for use by backends.
#[doc(hidden)]
#[allow(clippy::too_many_arguments)]
pub fn draw_glyphs_pattern_tiles<'a, S: PaintScene>(
    painter: &mut S,
    pattern: &Pattern,
    font: &'a FontData,
    font_size: f32,
    hint: bool,
    normalized_coords: &'a [NormalizedCoord],
    embolden: Vec2,
    style: impl Into<StyleRef<'a>>,
    brush_alpha: f32,
    transform: Affine,
    glyph_transform: Option<Affine>,
    glyphs: impl Iterator<Item = Glyph> + Clone,
    cull_rect: Option<Rect>,
) {
    draw_glyphs_pattern_tiles_with(
        painter,
        pattern,
        font,
        font_size,
        hint,
        normalized_coords,
        embolden,
        style,
        brush_alpha,
        transform,
        glyph_transform,
        glyphs,
        cull_rect,
        |painter, tile_transform| pattern.scene().replay(painter, tile_transform),
    );
}

/// Like [`draw_glyphs_pattern_tiles`], but calls `draw_tile` to draw the content of each tile,
/// as [`fill_pattern_tiles_with`] does.
///
/// This is intended for use by backends.
#[doc(hidden)]
#[allow(clippy::too_many_arguments)]
pub fn draw_glyphs_pattern_tiles_with<'a, S: PaintScene>(
    painter: &mut S,
    pattern: &Pattern,
    font: &'a FontData,
    font_size: f32,
    hint: bool,
    normalized_coords: &'a [NormalizedCoord],
    embolden: Vec2,
    style: impl Into<StyleRef<'a>>,
    brush_alpha: f32,
    transform: Affine,
    glyph_transform: Option<Affine>,
    glyphs: impl Iterator<Item = Glyph> + Clone,
    cull_rect: Option<Rect>,
    draw_tile: impl FnMut(&mut S, Affine),
) {
    let style = style.into();
    let Some(bounds) = glyph_run_bounds(
        font,
        font_size,
        embolden,
        &style,
        glyph_transform,
        glyphs.clone(),
    ) else {
        return;
    };
    painter.push_layer(
        Fill::NonZero,
        Mix::Normal,
        brush_alpha,
        transform,
        &bounds,
        None,
        None,
    );
    painter.draw_glyphs(
        font,
        font_size,
        hint,
        normalized_coords,
        embolden,
        style,
        Color::BLACK,
        1.0,
        transform,
        glyph_transform,
        glyphs,
        AntiAliasing::Enabled,
    );
    painter.push_layer(
        Fill::NonZero,
        BlendMode::new(Mix::Normal, Compose::SrcIn),
        1.0,
        transform,
        &bounds,
        None,
        None,
    );
    draw_tiles(painter, pattern, transform, bounds, cull_rect, draw_tile);
    painter.pop_layer();
    painter.pop_layer();
}

/// A conservative bounding box of a glyph run, in the run's space: each glyph is assumed to fit
/// within the font's bounding box (or two ems of its origin, if the font has none).
fn glyph_run_bounds(
    font: &FontData,
    font_size: f32,
    embolden: Vec2,
    style: &StyleRef<'_>,
    glyph_transform: Option<Affine>,
    glyphs: impl Iterator<Item = Glyph>,
) -> Option<Rect> {
    let em_box = font_bbox(font)
        .map(|bbox| bbox.inflate(0.1, 0.1))
        .unwrap_or(Rect::new(-2.0, -2.0, 2.0, 2.0));
    let em = f64::from(font_size);
    let glyph_box = Rect::new(
        em_box.x0 * em,
        em_box.y0 * em,
        em_box.x1 * em,
        em_box.y1 * em,
    );
    let mut glyph_box = glyph_transform
        .unwrap_or(Affine::IDENTITY)
        .transform_rect_bbox(glyph_box)
        .inflate(embolden.x.abs(), embolden.y.abs());
    if let StyleRef::Stroke(stroke) = style {
        glyph_box = glyph_box.inflate(stroke.width, stroke.width);
    }
    glyphs
        .map(|glyph| glyph_box + Vec2::new(f64::from(glyph.x), f64::from(glyph.y)))
        .reduce(|a, b| a.union(b))
}

/// Draws every tile of `pattern` that intersects `bounds` (in pattern space), with
/// `transform` mapping pattern space to the painter's space.
/// The bounding box of all glyphs in `font`, in ems with y pointing down, from its `head` table.
fn font_bbox(font: &FontData) -> Option<Rect> {
    let data = font.data.data();
    let u16_at = |at: usize| Some(u16::from_be_bytes(data.get(at..at + 2)?.try_into().ok()?));
    let i16_at = |at: usize| Some(f64::from(u16_at(at)? as i16));
    let u32_at = |at: usize| Some(u32::from_be_bytes(data.get(at..at + 4)?.try_into().ok()?));
    let mut font_offset = 0;
    if data.get(0..4)? == b"ttcf" {
        font_offset = u32_at(12 + 4 * font.index as usize)? as usize;
    }
    let num_tables = u16_at(font_offset + 4)? as usize;
    let head = (0..num_tables)
        .map(|i| font_offset + 12 + 16 * i)
        .find(|&record| data.get(record..record + 4) == Some(b"head"))?;
    let head = u32_at(head + 8)? as usize;
    let units_per_em = f64::from(u16_at(head + 18)?);
    if units_per_em == 0.0 {
        return None;
    }
    let [x0, y0, x1, y1] = [36, 38, 40, 42].map(|at| i16_at(head + at));
    let scale = 1.0 / units_per_em;
    Some(Rect::new(
        x0? * scale,
        -y1? * scale,
        x1? * scale,
        -y0? * scale,
    ))
}

fn draw_tiles<S: PaintScene>(
    painter: &mut S,
    pattern: &Pattern,
    transform: Affine,
    bounds: Rect,
    cull_rect: Option<Rect>,
    mut draw_tile: impl FnMut(&mut S, Affine),
) {
    let bounds = match cull_rect {
        Some(cull_rect) if transform.determinant() != 0.0 => {
            bounds.intersect(transform.inverse().transform_rect_bbox(cull_rect))
        }
        _ => bounds,
    };
    let tile = pattern.tile();
    let (width, height) = (tile.width(), tile.height());
    if !(width > 0.0 && height > 0.0 && bounds.is_finite()) || bounds.is_zero_area() {
        return;
    }
    let first_x = ((bounds.x0 - tile.x0) / width).floor();
    let last_x = ((bounds.x1 - tile.x0) / width).ceil();
    let first_y = ((bounds.y0 - tile.y0) / height).floor();
    let last_y = ((bounds.y1 - tile.y0) / height).ceil();
    let count = (last_x - first_x) * (last_y - first_y);
    let commands = pattern.scene().commands.len().max(1) as f64;
    if !(count > 0.0 && count * commands <= MAX_PATTERN_COMMANDS) {
        return;
    }
    // When pattern space is axis-aligned with the painter's space, snap tile edges to whole
    // pixels, so adjacent tiles' antialiased clips don't leave seams between them.
    let [a, b, c, d, e, f] = transform.as_coeffs();
    let snap = b == 0.0 && c == 0.0 && (a * width).abs() >= 1.0 && (d * height).abs() >= 1.0;
    for y in first_y as i64..last_y as i64 {
        for x in first_x as i64..last_x as i64 {
            let tile_transform = if snap {
                let left = (a * (tile.x0 + x as f64 * width) + e).round();
                let right = (a * (tile.x0 + (x + 1) as f64 * width) + e).round();
                let top = (d * (tile.y0 + y as f64 * height) + f).round();
                let bottom = (d * (tile.y0 + (y + 1) as f64 * height) + f).round();
                let (sx, sy) = ((right - left) / width, (bottom - top) / height);
                Affine::new([sx, 0.0, 0.0, sy, left - sx * tile.x0, top - sy * tile.y0])
            } else {
                transform * Affine::translate((x as f64 * width, y as f64 * height))
            };
            painter.push_clip_layer(Fill::NonZero, tile_transform, &tile);
            draw_tile(painter, tile_transform);
            painter.pop_layer();
        }
    }
}