Skip to main content

viewport_lib/resources/
font.rs

1//! Font atlas and single-line text layout for overlay rendering.
2//!
3//! This module is the text back-end for [`LabelItem`], [`ScalarBarItem`], and
4//! [`RulerItem`].  It uses [`fontdue`] for glyph rasterization and packs glyphs
5//! into a single GPU texture atlas on demand.
6//!
7//! Public surface: [`FontHandle`] (opaque font identifier) and
8//! [`super::ViewportGpuResources::upload_font`].  Everything else is `pub(crate)`.
9
10use std::collections::HashMap;
11
12/// Default font embedded in the library binary (Inter Regular, SIL OFL 1.1).
13const DEFAULT_FONT_BYTES: &[u8] = include_bytes!("../fonts/Inter-Regular.ttf");
14
15// ---------------------------------------------------------------------------
16// FontHandle : public opaque identifier
17// ---------------------------------------------------------------------------
18
19/// Opaque handle to a font uploaded via
20/// [`ViewportGpuResources::upload_font`](super::ViewportGpuResources::upload_font).
21///
22/// Pass `None` (or omit the field) on overlay items to use the built-in default
23/// font.  Pass `Some(handle)` to use a user-supplied TTF font.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub struct FontHandle(pub(crate) usize);
26
27// ---------------------------------------------------------------------------
28// GlyphKey / GlyphEntry : atlas bookkeeping
29// ---------------------------------------------------------------------------
30
31/// Unique key for a rasterized glyph in the atlas.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33struct GlyphKey {
34    font_index: usize,
35    glyph_index: u16,
36    /// Font size in tenths of a pixel (e.g. 140 = 14.0 px).
37    /// Quantised to avoid unbounded atlas growth from fractional sizes.
38    size_tenths: u32,
39}
40
41/// Location and metrics of a single rasterized glyph in the atlas texture.
42#[derive(Debug, Clone, Copy)]
43struct GlyphEntry {
44    /// Top-left pixel coordinate in the atlas.
45    x: u32,
46    y: u32,
47    /// Rasterized bitmap dimensions.
48    width: u32,
49    height: u32,
50    /// Offset from the pen position to the top-left of the bitmap.
51    offset_x: f32,
52    offset_y: f32,
53}
54
55// ---------------------------------------------------------------------------
56// GlyphQuad / TextLayout : internal layout output
57// ---------------------------------------------------------------------------
58
59/// A positioned, textured quad for one glyph, ready for vertex generation.
60#[derive(Debug, Clone, Copy)]
61pub(crate) struct GlyphQuad {
62    /// Screen-space top-left corner (pixels from top-left of viewport).
63    pub pos: [f32; 2],
64    /// Screen-space size [w, h] in pixels.
65    pub size: [f32; 2],
66    /// UV top-left in the atlas (0..1).
67    pub uv_min: [f32; 2],
68    /// UV bottom-right in the atlas (0..1).
69    pub uv_max: [f32; 2],
70}
71
72/// Layout result for a single-line text string.
73#[derive(Debug, Clone)]
74pub(crate) struct TextLayout {
75    /// One quad per visible glyph (whitespace characters are skipped).
76    pub quads: Vec<GlyphQuad>,
77    /// Total advance width of the laid-out string in pixels.
78    pub total_width: f32,
79    /// Line height in pixels (ascent - descent + line gap at the requested size).
80    pub height: f32,
81}
82
83// ---------------------------------------------------------------------------
84// GlyphAtlas
85// ---------------------------------------------------------------------------
86
87/// A dynamically-growing glyph atlas backed by a single `Rgba8Unorm` texture.
88///
89/// Owned by [`ViewportGpuResources`]; never exposed in the public API.
90pub(crate) struct GlyphAtlas {
91    /// Parsed fontdue fonts.  Index 0 is always the built-in default.
92    fonts: Vec<fontdue::Font>,
93
94    /// Cached rasterized glyphs.
95    entries: HashMap<GlyphKey, GlyphEntry>,
96
97    /// CPU-side atlas pixel data (single-channel alpha, packed row-major).
98    /// Stored as RGBA for direct GPU upload: R=G=B=255, A=coverage.
99    pixels: Vec<[u8; 4]>,
100
101    /// Current atlas dimensions (always square, power of two).
102    size: u32,
103
104    /// Simple row-based packer state.
105    cursor_x: u32,
106    cursor_y: u32,
107    row_height: u32,
108
109    /// GPU texture (recreated when the atlas grows).
110    pub texture: wgpu::Texture,
111    /// View into the atlas texture.
112    pub view: wgpu::TextureView,
113
114    /// Set to `true` whenever new glyphs have been rasterized since the last
115    /// GPU upload.  Cleared by [`GlyphAtlas::upload_if_dirty`].
116    dirty: bool,
117}
118
119impl GlyphAtlas {
120    /// Initial atlas size in pixels (width = height).
121    const INITIAL_SIZE: u32 = 512;
122
123    /// Create a new atlas with the built-in default font pre-loaded.
124    pub fn new(device: &wgpu::Device) -> Self {
125        let default_font = fontdue::Font::from_bytes(
126            DEFAULT_FONT_BYTES,
127            fontdue::FontSettings::default(),
128        )
129        .expect("built-in default font must parse");
130
131        let size = Self::INITIAL_SIZE;
132        let pixel_count = (size * size) as usize;
133        let pixels = vec![[255, 255, 255, 0]; pixel_count];
134
135        let (texture, view) = Self::create_texture(device, size);
136
137        Self {
138            fonts: vec![default_font],
139            entries: HashMap::new(),
140            pixels,
141            size,
142            cursor_x: 0,
143            cursor_y: 0,
144            row_height: 0,
145            texture,
146            view,
147            dirty: false,
148        }
149    }
150
151    /// Register a user-supplied TTF font.  Returns a [`FontHandle`] that can be
152    /// passed to overlay items.
153    pub fn upload_font(&mut self, ttf_bytes: &[u8]) -> Result<FontHandle, FontError> {
154        let font = fontdue::Font::from_bytes(ttf_bytes, fontdue::FontSettings::default())
155            .map_err(|e| FontError::ParseFailed(e.to_string()))?;
156        let index = self.fonts.len();
157        self.fonts.push(font);
158        Ok(FontHandle(index))
159    }
160
161    /// Lay out a single-line string and return positioned glyph quads.
162    ///
163    /// Glyphs that are not yet in the atlas are rasterized and packed on the
164    /// fly.  Call [`upload_if_dirty`] after all layout calls for the frame to
165    /// push new glyphs to the GPU.
166    pub fn layout_text(
167        &mut self,
168        text: &str,
169        font_size: f32,
170        font: Option<FontHandle>,
171        device: &wgpu::Device,
172    ) -> TextLayout {
173        let font_index = font.map_or(0, |h| h.0);
174        let size_tenths = (font_size * 10.0).round() as u32;
175        let px = font_size;
176
177        let metrics = self.fonts[font_index].horizontal_line_metrics(px);
178        let height = metrics
179            .map(|m| m.ascent - m.descent + m.line_gap)
180            .unwrap_or(px * 1.2);
181
182        let mut quads = Vec::new();
183        let mut pen_x: f32 = 0.0;
184
185        let mut prev_glyph: Option<u16> = None;
186        for ch in text.chars() {
187            let glyph_index = self.fonts[font_index].lookup_glyph_index(ch);
188
189            // Kerning.
190            if let Some(prev) = prev_glyph {
191                if let Some(kern) = self.fonts[font_index]
192                    .horizontal_kern_indexed(prev, glyph_index, px)
193                {
194                    pen_x += kern;
195                }
196            }
197            prev_glyph = Some(glyph_index);
198
199            // Get metrics for advance, even for whitespace.
200            let m = self.fonts[font_index].metrics_indexed(glyph_index, px);
201
202            // Only emit a quad for glyphs with visible bitmap area.
203            if m.width > 0 && m.height > 0 {
204                let entry = self.ensure_glyph(device, font_index, glyph_index, size_tenths, px);
205                let atlas_size = self.size as f32;
206
207                quads.push(GlyphQuad {
208                    pos: [
209                        pen_x + entry.offset_x,
210                        entry.offset_y,
211                    ],
212                    size: [entry.width as f32, entry.height as f32],
213                    uv_min: [
214                        entry.x as f32 / atlas_size,
215                        entry.y as f32 / atlas_size,
216                    ],
217                    uv_max: [
218                        (entry.x + entry.width) as f32 / atlas_size,
219                        (entry.y + entry.height) as f32 / atlas_size,
220                    ],
221                });
222            }
223
224            pen_x += m.advance_width;
225        }
226
227        TextLayout {
228            quads,
229            total_width: pen_x,
230            height,
231        }
232    }
233
234    /// Lay out text with word wrapping at a maximum width.
235    ///
236    /// Words that exceed `max_width` on their own are not broken: they extend
237    /// past the boundary.  The returned `total_width` is the maximum line width
238    /// actually used.
239    pub fn layout_text_wrapped(
240        &mut self,
241        text: &str,
242        font_size: f32,
243        font: Option<FontHandle>,
244        max_width: f32,
245        device: &wgpu::Device,
246    ) -> TextLayout {
247        let font_index = font.map_or(0, |h| h.0);
248        let px = font_size;
249
250        let metrics = self.fonts[font_index].horizontal_line_metrics(px);
251        let line_height = metrics
252            .map(|m| m.ascent - m.descent + m.line_gap)
253            .unwrap_or(px * 1.2);
254
255        // Split into words and lay out greedily.
256        let words: Vec<&str> = text.split_whitespace().collect();
257        if words.is_empty() {
258            return TextLayout {
259                quads: Vec::new(),
260                total_width: 0.0,
261                height: line_height,
262            };
263        }
264
265        let size_tenths = (px * 10.0).round() as u32;
266        let space_advance = {
267            let gi = self.fonts[font_index].lookup_glyph_index(' ');
268            self.fonts[font_index].metrics_indexed(gi, px).advance_width
269        };
270
271        let mut quads = Vec::new();
272        let mut line_x: f32 = 0.0;
273        let mut line_y: f32 = 0.0;
274        let mut max_line_width: f32 = 0.0;
275        let mut first_on_line = true;
276
277        for word in &words {
278            // Measure the word width.
279            let mut word_quads: Vec<GlyphQuad> = Vec::new();
280            let mut pen_x: f32 = 0.0;
281            let mut prev_glyph: Option<u16> = None;
282
283            for ch in word.chars() {
284                let glyph_index = self.fonts[font_index].lookup_glyph_index(ch);
285                if let Some(prev) = prev_glyph {
286                    if let Some(kern) = self.fonts[font_index]
287                        .horizontal_kern_indexed(prev, glyph_index, px)
288                    {
289                        pen_x += kern;
290                    }
291                }
292                prev_glyph = Some(glyph_index);
293                let m = self.fonts[font_index].metrics_indexed(glyph_index, px);
294                if m.width > 0 && m.height > 0 {
295                    let entry = self.ensure_glyph(device, font_index, glyph_index, size_tenths, px);
296                    let atlas_size = self.size as f32;
297                    word_quads.push(GlyphQuad {
298                        pos: [pen_x + entry.offset_x, entry.offset_y],
299                        size: [entry.width as f32, entry.height as f32],
300                        uv_min: [
301                            entry.x as f32 / atlas_size,
302                            entry.y as f32 / atlas_size,
303                        ],
304                        uv_max: [
305                            (entry.x + entry.width) as f32 / atlas_size,
306                            (entry.y + entry.height) as f32 / atlas_size,
307                        ],
308                    });
309                }
310                pen_x += m.advance_width;
311            }
312            let word_width = pen_x;
313
314            // Wrap if this word doesn't fit on the current line.
315            let test_x = if first_on_line { line_x } else { line_x + space_advance };
316            if !first_on_line && test_x + word_width > max_width {
317                max_line_width = max_line_width.max(line_x);
318                line_x = 0.0;
319                line_y += line_height;
320                first_on_line = true;
321            }
322
323            let start_x = if first_on_line { line_x } else { line_x + space_advance };
324            for mut gq in word_quads {
325                gq.pos[0] += start_x;
326                gq.pos[1] += line_y;
327                quads.push(gq);
328            }
329            line_x = start_x + word_width;
330            first_on_line = false;
331        }
332
333        max_line_width = max_line_width.max(line_x);
334        let total_height = line_y + line_height;
335
336        TextLayout {
337            quads,
338            total_width: max_line_width,
339            height: total_height,
340        }
341    }
342
343    /// Return the font ascent in pixels for the given font index and size.
344    ///
345    /// The ascent is the distance from the baseline to the top of the tallest
346    /// glyph.  Used to position glyph quads relative to a text origin at the
347    /// top-left corner of the bounding box.
348    pub fn font_ascent(&self, font_index: usize, font_size: f32) -> f32 {
349        self.fonts[font_index]
350            .horizontal_line_metrics(font_size)
351            .map(|m| m.ascent)
352            .unwrap_or(font_size * 0.8)
353    }
354
355    /// Upload new glyph data to the GPU if any glyphs were rasterized since
356    /// the last upload.
357    pub fn upload_if_dirty(&mut self, queue: &wgpu::Queue) {
358        if !self.dirty {
359            return;
360        }
361        let flat: Vec<u8> = self.pixels.iter().flat_map(|p| p.iter().copied()).collect();
362        queue.write_texture(
363            wgpu::TexelCopyTextureInfo {
364                texture: &self.texture,
365                mip_level: 0,
366                origin: wgpu::Origin3d::ZERO,
367                aspect: wgpu::TextureAspect::All,
368            },
369            &flat,
370            wgpu::TexelCopyBufferLayout {
371                offset: 0,
372                bytes_per_row: Some(self.size * 4),
373                rows_per_image: Some(self.size),
374            },
375            wgpu::Extent3d {
376                width: self.size,
377                height: self.size,
378                depth_or_array_layers: 1,
379            },
380        );
381        self.dirty = false;
382    }
383
384    // ------------------------------------------------------------------
385    // Private helpers
386    // ------------------------------------------------------------------
387
388    /// Ensure a glyph is in the atlas, rasterizing and packing it if needed.
389    /// Returns the atlas entry.
390    fn ensure_glyph(
391        &mut self,
392        device: &wgpu::Device,
393        font_index: usize,
394        glyph_index: u16,
395        size_tenths: u32,
396        px: f32,
397    ) -> GlyphEntry {
398        let key = GlyphKey {
399            font_index,
400            glyph_index,
401            size_tenths,
402        };
403
404        if let Some(&entry) = self.entries.get(&key) {
405            return entry;
406        }
407
408        // Rasterize.
409        let (metrics, bitmap) = self.fonts[font_index].rasterize_indexed(glyph_index, px);
410        let w = metrics.width as u32;
411        let h = metrics.height as u32;
412
413        if w == 0 || h == 0 {
414            // Whitespace glyph: insert a zero-area entry.
415            let entry = GlyphEntry {
416                x: 0,
417                y: 0,
418                width: 0,
419                height: 0,
420                offset_x: metrics.xmin as f32,
421                offset_y: -(metrics.ymin as f32 + h as f32),
422            };
423            self.entries.insert(key, entry);
424            return entry;
425        }
426
427        // Pack into the atlas (simple row packer with 1px padding).
428        let pad = 1;
429        if self.cursor_x + w + pad > self.size {
430            // Move to next row.
431            self.cursor_y += self.row_height + pad;
432            self.cursor_x = 0;
433            self.row_height = 0;
434        }
435        if self.cursor_y + h + pad > self.size {
436            // Atlas is full : grow.
437            self.grow(device);
438        }
439
440        let x = self.cursor_x;
441        let y = self.cursor_y;
442
443        // Blit coverage into the RGBA pixel buffer.
444        for row in 0..h {
445            for col in 0..w {
446                let src = (row * w + col) as usize;
447                let dst = ((y + row) * self.size + (x + col)) as usize;
448                self.pixels[dst] = [255, 255, 255, bitmap[src]];
449            }
450        }
451        self.dirty = true;
452
453        self.cursor_x = x + w + pad;
454        self.row_height = self.row_height.max(h);
455
456        let entry = GlyphEntry {
457            x,
458            y,
459            width: w,
460            height: h,
461            offset_x: metrics.xmin as f32,
462            offset_y: -(metrics.ymin as f32 + h as f32),
463        };
464        self.entries.insert(key, entry);
465        entry
466    }
467
468    /// Double the atlas size, copying existing pixel data into the new buffer
469    /// and recreating the GPU texture.
470    fn grow(&mut self, device: &wgpu::Device) {
471        let old_size = self.size;
472        let new_size = old_size * 2;
473        tracing::info!("Growing glyph atlas from {}x{} to {}x{}", old_size, old_size, new_size, new_size);
474
475        let mut new_pixels = vec![[255, 255, 255, 0u8]; (new_size * new_size) as usize];
476        for row in 0..old_size {
477            let src_start = (row * old_size) as usize;
478            let dst_start = (row * new_size) as usize;
479            new_pixels[dst_start..dst_start + old_size as usize]
480                .copy_from_slice(&self.pixels[src_start..src_start + old_size as usize]);
481        }
482
483        self.pixels = new_pixels;
484        self.size = new_size;
485
486        let (texture, view) = Self::create_texture(device, new_size);
487        self.texture = texture;
488        self.view = view;
489        self.dirty = true; // Full re-upload needed.
490    }
491
492    fn create_texture(device: &wgpu::Device, size: u32) -> (wgpu::Texture, wgpu::TextureView) {
493        let texture = device.create_texture(&wgpu::TextureDescriptor {
494            label: Some("glyph_atlas"),
495            size: wgpu::Extent3d {
496                width: size,
497                height: size,
498                depth_or_array_layers: 1,
499            },
500            mip_level_count: 1,
501            sample_count: 1,
502            dimension: wgpu::TextureDimension::D2,
503            format: wgpu::TextureFormat::Rgba8Unorm,
504            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
505            view_formats: &[],
506        });
507        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
508        (texture, view)
509    }
510}
511
512// ---------------------------------------------------------------------------
513// FontError
514// ---------------------------------------------------------------------------
515
516/// Error returned by [`super::ViewportGpuResources::upload_font`].
517#[derive(Debug, Clone, thiserror::Error)]
518pub enum FontError {
519    /// The TTF data could not be parsed.
520    #[error("font parsing failed: {0}")]
521    ParseFailed(String),
522}
523
524// ---------------------------------------------------------------------------
525// ViewportGpuResources integration
526// ---------------------------------------------------------------------------
527
528impl super::ViewportGpuResources {
529    /// Upload a user-supplied TTF font for use with overlay items.
530    ///
531    /// Returns an opaque [`FontHandle`] that can be passed to [`LabelItem`],
532    /// [`ScalarBarItem`], or [`RulerItem`] via their `font` field.  Pass
533    /// `None` on those items to use the built-in default font instead.
534    ///
535    /// The font bytes must be a valid TrueType (`.ttf`) file.
536    pub fn upload_font(&mut self, ttf_bytes: &[u8]) -> Result<FontHandle, FontError> {
537        self.glyph_atlas.upload_font(ttf_bytes)
538    }
539}