nannou_draw 0.20.0

A simple and expressive API for drawing 2D and 3D graphics, built on Bevy for nannou - the creative-coding framework.
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use core::hash::BuildHasher;

use crate::draw::drawing::DrawingContext;
use crate::draw::mesh::MeshExt;
use crate::draw::primitive::Primitive;
use crate::draw::properties::spatial::{self, dimension, orientation, position};
use crate::draw::properties::{SetColor, SetDimensions, SetOrientation, SetPosition};
use crate::draw::{self, Drawing};
use crate::text::{self, Align, FontSize, Justify, Layout, Scalar, Wrap};
use bevy::platform::hash::FixedHasher;
use bevy::prelude::*;
use bevy::text::{
    FontAtlasKey, FontAtlasSet, FontHinting, FontSmoothing, GlyphCacheKey, ScaleCx,
    add_glyph_to_atlas, get_glyph_atlas_info,
};
use swash::FontRef;

/// Properties related to drawing the **Text** primitive.
#[derive(Clone, Debug)]
pub struct Text {
    spatial: spatial::Properties,
    style: Style,
    // The byte range into the `Draw` context's text buffer.
    text: std::ops::Range<usize>,
}

/// Styling properties for the **Text** primitive.
#[derive(Clone, Debug, Default)]
pub struct Style {
    pub color: Option<Color>,
    pub glyph_colors: Vec<Color>, // Overrides `color` if non-empty.
    pub layout: text::layout::Builder,
}

/// The drawing context for the **Text** primitive.
pub type DrawingText<'a> = Drawing<'a, Text>;

impl Text {
    /// Begin drawing some text.
    pub fn new(ctxt: DrawingContext, text: &str) -> Self {
        let start = ctxt.text_buffer.len();
        ctxt.text_buffer.push_str(text);
        let end = ctxt.text_buffer.len();
        let text = start..end;
        let spatial = Default::default();
        let style = Default::default();
        Text {
            spatial,
            style,
            text,
        }
    }

    // Apply the given function to the inner text layout.
    fn map_layout<F>(mut self, map: F) -> Self
    where
        F: FnOnce(text::layout::Builder) -> text::layout::Builder,
    {
        self.style.layout = map(self.style.layout);
        self
    }

    /// The font size to use for the text.
    pub fn font_size(self, size: FontSize) -> Self {
        self.map_layout(|l| l.font_size(size))
    }

    /// Specify whether or not text should be wrapped around some width and how to do so.
    pub fn line_wrap(self, line_wrap: Option<Wrap>) -> Self {
        self.map_layout(|l| l.line_wrap(line_wrap))
    }

    /// Specify that the **Text** should not wrap lines around the width.
    pub fn no_line_wrap(self) -> Self {
        self.map_layout(|l| l.no_line_wrap())
    }

    /// Line wrap the **Text** at the beginning of the first word that exceeds the width.
    pub fn wrap_by_word(self) -> Self {
        self.map_layout(|l| l.wrap_by_word())
    }

    /// Line wrap the **Text** at the beginning of the first character that exceeds the width.
    pub fn wrap_by_character(self) -> Self {
        self.map_layout(|l| l.wrap_by_character())
    }

    /// A method for specifying the font family used for displaying the `Text`.
    pub fn font(self, family: impl Into<String>) -> Self {
        self.map_layout(|l| l.font_family(family))
    }

    /// Describe the end along the *x* axis to which the text should be aligned.
    pub fn justify(self, justify: Justify) -> Self {
        self.map_layout(|l| l.justify(justify))
    }

    /// Align the text to the left of its bounding **Rect**'s *x* axis range.
    pub fn left_justify(self) -> Self {
        self.map_layout(|l| l.left_justify())
    }

    /// Align the text to the middle of its bounding **Rect**'s *x* axis range.
    pub fn center_justify(self) -> Self {
        self.map_layout(|l| l.center_justify())
    }

    /// Align the text to the right of its bounding **Rect**'s *x* axis range.
    pub fn right_justify(self) -> Self {
        self.map_layout(|l| l.right_justify())
    }

    /// Specify how much vertical space should separate each line of text.
    pub fn line_spacing(self, spacing: Scalar) -> Self {
        self.map_layout(|l| l.line_spacing(spacing))
    }

    /// Specify how the whole text should be aligned along the y axis of its bounding rectangle
    pub fn y_align(self, align: Align) -> Self {
        self.map_layout(|l| l.y_align(align))
    }

    /// Align the top edge of the text with the top edge of its bounding rectangle.
    pub fn align_top(self) -> Self {
        self.map_layout(|l| l.align_top())
    }

    /// Align the middle of the text with the middle of the bounding rect along the y axis.
    pub fn align_middle_y(self) -> Self {
        self.map_layout(|l| l.align_middle_y())
    }

    /// Align the bottom edge of the text with the bottom edge of its bounding rectangle.
    pub fn align_bottom(self) -> Self {
        self.map_layout(|l| l.align_bottom())
    }

    /// Set all the parameters via an existing `Layout`
    pub fn layout(self, layout: &Layout) -> Self {
        self.map_layout(|l| l.layout(layout))
    }

    /// Specify the entire styling for the **Text**.
    pub fn with_style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set a color for each glyph.
    pub fn glyph_colors(mut self, colors: Vec<Color>) -> Self {
        self.style.glyph_colors = colors;
        self
    }
}

impl<'a> DrawingText<'a> {
    /// The font size to use for the text.
    pub fn font_size(self, size: text::FontSize) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.font_size(size));
        self
    }

    /// Specify that the **Text** should not wrap lines around the width.
    pub fn no_line_wrap(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.no_line_wrap());
        self
    }

    /// Line wrap the **Text** at the beginning of the first word that exceeds the width.
    pub fn wrap_by_word(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.wrap_by_word());
        self
    }

    /// Line wrap the **Text** at the beginning of the first character that exceeds the width.
    pub fn wrap_by_character(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.wrap_by_character());
        self
    }

    /// A method for specifying the font family used for displaying the `Text`.
    pub fn font(self, family: impl Into<String>) -> Self {
        set_font(&self.draw, self.index, family.into());
        self
    }

    /// Build the **Text** with the given **Style**.
    pub fn with_style(self, style: Style) -> Self {
        update_text(&self.draw, self.index, |text| text.style = style);
        self
    }

    /// Describe the end along the *x* axis to which the text should be aligned.
    pub fn justify(self, justify: text::Justify) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.justify(justify));
        self
    }

    /// Align the text to the left of its bounding **Rect**'s *x* axis range.
    pub fn left_justify(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.left_justify());
        self
    }

    /// Align the text to the middle of its bounding **Rect**'s *x* axis range.
    pub fn center_justify(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.center_justify());
        self
    }

    /// Align the text to the right of its bounding **Rect**'s *x* axis range.
    pub fn right_justify(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.right_justify());
        self
    }

    /// Specify how much vertical space should separate each line of text.
    pub fn line_spacing(self, spacing: text::Scalar) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.line_spacing(spacing));
        self
    }

    /// Specify how the whole text should be aligned along the y axis of its bounding rectangle
    pub fn y_align_text(self, align: Align) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.y_align(align));
        self
    }

    /// Align the top edge of the text with the top edge of its bounding rectangle.
    pub fn align_text_top(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.align_top());
        self
    }

    /// Align the middle of the text with the middle of the bounding rect along the y axis.
    pub fn align_text_middle_y(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.align_middle_y());
        self
    }

    /// Align the bottom edge of the text with the bottom edge of its bounding rectangle.
    pub fn align_text_bottom(self) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.align_bottom());
        self
    }

    /// Set all the parameters via an existing `Layout`
    pub fn layout(self, layout: &Layout) -> Self {
        update_text_layout(&self.draw, self.index, |l| l.layout(layout));
        self
    }

    /// Set a color for each glyph.
    pub fn glyph_colors<I, C>(self, glyph_colors: I) -> Self
    where
        I: IntoIterator<Item = C>,
        C: Into<Color>,
    {
        let glyph_colors: Vec<Color> = glyph_colors.into_iter().map(|c| c.into()).collect();
        set_glyph_colors(&self.draw, self.index, glyph_colors);
        self
    }
}

// Update the inner `Text` of the primitive being drawn at `index`.
fn update_text(draw: &crate::draw::Draw, index: usize, f: impl FnOnce(&mut Text)) {
    crate::draw::drawing::with_primitive(draw, index, |prim| match prim {
        Primitive::Text(text) => f(text),
        _ => bevy::log::warn_once!("expected a `Text` primitive"),
    })
}

// Update the layout builder of the `Text` primitive being drawn at `index`.
fn update_text_layout(
    draw: &crate::draw::Draw,
    index: usize,
    f: impl FnOnce(text::layout::Builder) -> text::layout::Builder,
) {
    update_text(draw, index, |text| {
        let layout = std::mem::take(&mut text.style.layout);
        text.style.layout = f(layout);
    })
}

// Set the font family of the `Text` primitive being drawn at `index`.
//
// Non-generic so that the body compiles here rather than in the caller's crate.
fn set_font(draw: &crate::draw::Draw, index: usize, family: String) {
    update_text_layout(draw, index, |l| l.font_family(family))
}

// Set the per-glyph colors of the `Text` primitive being drawn at `index`.
//
// Non-generic so that the body compiles here rather than in the caller's crate.
fn set_glyph_colors(draw: &crate::draw::Draw, index: usize, colors: Vec<Color>) {
    update_text(draw, index, |text| text.style.glyph_colors = colors)
}

/// A run of glyph quads sampling a single font atlas texture.
pub(crate) struct TextQuadBatch {
    pub texture: Handle<Image>,
    pub mesh: Mesh,
}

impl Text {
    /// Lay out the text and emit one textured quad per glyph, rasterising glyphs into
    /// `bevy_text`'s cached font atlases as required.
    ///
    /// Glyph positions are computed at physical-pixel resolution (`scale_factor`) so that
    /// rasterised glyphs map 1:1 to screen pixels, then converted back to logical points.
    /// A new batch is started whenever consecutive glyphs sample different atlas textures.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn render_atlas_quads(
        self,
        text_buffer: &str,
        theme: &draw::Theme,
        transform: &Mat4,
        output_attachment_size: Vec2,
        scale_factor: f32,
        text_cx: &crate::text::font::SharedTextCx,
        font_atlas_set: &mut FontAtlasSet,
        images: &mut Assets<Image>,
        scale_cx: &mut ScaleCx,
    ) -> Vec<TextQuadBatch> {
        let s = &text_buffer[self.text.clone()];
        if s.is_empty() {
            return Vec::new();
        }

        let layout_params = self.style.layout.build();
        let w = self
            .spatial
            .dimensions
            .x
            .unwrap_or(output_attachment_size.x);
        let h = self
            .spatial
            .dimensions
            .y
            .unwrap_or(output_attachment_size.y);
        let x = self.spatial.position.point.x;
        let y = self.spatial.position.point.y;
        let rect = nannou_core::geom::Rect::from_x_y_w_h(x, y, w, h);

        let mut inner = text_cx.0.lock().unwrap();
        let text_obj =
            text::Text::layout_with_inner(&mut inner, s, &layout_params, rect, scale_factor);
        drop(inner);

        let default_color = self
            .style
            .color
            .unwrap_or_else(|| theme.fill(&draw::theme::Primitive::Text));
        let glyph_colors = &self.style.glyph_colors;

        let rect_center = Vec2::new(x, y);
        let pos_offset = text_obj.position_offset_value() + rect_center;

        // Rasterise with bevy's defaults so atlas entries are shared with bevy UI text.
        let font_smoothing = FontSmoothing::AntiAliased;
        let hinting = FontHinting::default();

        let mut batches: Vec<TextQuadBatch> = Vec::new();
        // Counts every positioned glyph (rendered or not) so `glyph_colors` indices
        // are stable regardless of which glyphs make it into an atlas.
        let mut glyph_index = 0;

        for line in text_obj.parley_layout().lines() {
            for item in line.items() {
                let parley::PositionedLayoutItem::GlyphRun(glyph_run) = item else {
                    continue;
                };

                let run = glyph_run.run();
                let font = run.font();
                let font_size = run.font_size();
                let coords = run.normalized_coords();
                let font_atlas_key = FontAtlasKey {
                    id: font.data.id() as u32,
                    index: font.index,
                    font_size_bits: font_size.to_bits(),
                    variations_hash: FixedHasher.hash_one(coords),
                    hinting,
                    font_smoothing,
                };

                let Some(font_ref) = FontRef::from_index(font.data.as_ref(), font.index as usize)
                else {
                    glyph_index += glyph_run.positioned_glyphs().count();
                    continue;
                };

                let hint = hinting.is_enabled() && font_smoothing == FontSmoothing::AntiAliased;
                let mut scaler = scale_cx
                    .0
                    .builder(font_ref)
                    .size(font_size)
                    .hint(hint)
                    .normalized_coords(coords)
                    .build();

                for glyph in glyph_run.positioned_glyphs() {
                    let i = glyph_index;
                    glyph_index += 1;

                    let Ok(glyph_id) = u16::try_from(glyph.id) else {
                        continue;
                    };

                    let font_atlases = font_atlas_set.entry(font_atlas_key).or_default();
                    let atlas_info = get_glyph_atlas_info(font_atlases, GlyphCacheKey { glyph_id })
                        .map(Ok)
                        .unwrap_or_else(|| {
                            add_glyph_to_atlas(
                                font_atlases,
                                images,
                                &mut scaler,
                                font_smoothing,
                                glyph_id,
                            )
                        });
                    let Ok(atlas_info) = atlas_info else {
                        continue;
                    };

                    let size = atlas_info.rect.size();
                    if size.x <= 0.0 || size.y <= 0.0 {
                        continue;
                    }

                    let Some(atlas) = font_atlases
                        .iter()
                        .find(|atlas| atlas.texture.id() == atlas_info.texture)
                    else {
                        continue;
                    };

                    // Glyph centre in parley's y-down physical-pixel layout space,
                    // converted to nannou's y-up logical points.
                    let centre = size / 2.0 + Vec2::new(glyph.x, glyph.y) + atlas_info.offset;
                    let cx = pos_offset.x + centre.x / scale_factor;
                    let cy = pos_offset.y - centre.y / scale_factor;
                    let hw = size.x / (2.0 * scale_factor);
                    let hh = size.y / (2.0 * scale_factor);

                    // Monochrome glyphs are white-on-alpha in the atlas and tinted by
                    // vertex colour; colour glyphs (e.g. emoji) are sampled as-is.
                    let color = if atlas_info.is_alpha_mask {
                        glyph_colors.get(i).copied().unwrap_or(default_color)
                    } else {
                        Color::WHITE
                    };
                    let color_arr: [f32; 4] = LinearRgba::from(color).to_f32_array();

                    // UVs over the atlas; the image's y-down texel space performs the
                    // vertical flip relative to the y-up quad corners.
                    let atlas_size = atlas.texture_atlas.size.as_vec2();
                    let (uv_l, uv_r) = (
                        atlas_info.rect.min.x / atlas_size.x,
                        atlas_info.rect.max.x / atlas_size.x,
                    );
                    let (uv_t, uv_b) = (
                        atlas_info.rect.min.y / atlas_size.y,
                        atlas_info.rect.max.y / atlas_size.y,
                    );

                    let batch = match batches.last_mut() {
                        Some(batch) if batch.texture.id() == atlas_info.texture => batch,
                        _ => {
                            batches.push(TextQuadBatch {
                                texture: atlas.texture.clone(),
                                mesh: Mesh::init(),
                            });
                            batches.last_mut().unwrap()
                        }
                    };

                    let mesh = &mut batch.mesh;
                    let base = mesh.points().len() as u32;
                    // Corners ordered top-left, top-right, bottom-right, bottom-left.
                    let corners = [
                        (cx - hw, cy + hh, uv_l, uv_t),
                        (cx + hw, cy + hh, uv_r, uv_t),
                        (cx + hw, cy - hh, uv_r, uv_b),
                        (cx - hw, cy - hh, uv_l, uv_b),
                    ];
                    for (px, py, u, v) in corners {
                        let p = *transform * Vec4::new(px, py, 0.0, 1.0);
                        mesh.points_mut().push([p.x, p.y, p.z]);
                        mesh.colors_mut().push(color_arr);
                        mesh.tex_coords_mut().push([u, v]);
                        mesh.normals_mut().push([0.0, 0.0, 1.0]);
                    }
                    // Two triangles wound counter-clockwise in y-up space.
                    for idx in [0, 3, 2, 0, 2, 1] {
                        mesh.push_index(base + idx);
                    }
                }
            }
        }

        batches
    }
}

impl SetOrientation for Text {
    fn properties(&mut self) -> &mut orientation::Properties {
        SetOrientation::properties(&mut self.spatial)
    }
}

impl SetPosition for Text {
    fn properties(&mut self) -> &mut position::Properties {
        SetPosition::properties(&mut self.spatial)
    }
}

impl SetDimensions for Text {
    fn properties(&mut self) -> &mut dimension::Properties {
        SetDimensions::properties(&mut self.spatial)
    }
}

impl SetColor for Text {
    fn color_mut(&mut self) -> &mut Option<Color> {
        SetColor::color_mut(&mut self.style.color)
    }
}

// Primitive conversions.

impl From<Text> for Primitive {
    fn from(prim: Text) -> Self {
        Primitive::Text(prim)
    }
}