glyphr 0.6.0

A no_std, lightweight and simple font rasterizing library
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! # api.rs
//!
//! Public API for text rendering.

use crate::renderer;
use glyphr_types::{AlignH, AlignV, BitmapFormat, Font};

/// Callback-backed render target.
pub struct Callbacks<P>
where
    P: FnMut(u16, u16, u32) -> bool,
{
    /// Width of the render target in pixels.
    width: u16,
    /// Height of the render target in pixels.
    height: u16,
    /// Callback to write a single pixel: (x, y, color) -> success.
    write_pixel: P,
}

impl<P> Callbacks<P>
where
    P: FnMut(u16, u16, u32) -> bool,
{
    /// Create a new callback render target.
    pub fn new(width: u16, height: u16, write_pixel: P) -> Self {
        Self {
            width,
            height,
            write_pixel,
        }
    }

    /// Get the dimensions of the render target.
    pub fn dimensions(&self) -> (u16, u16) {
        (self.width, self.height)
    }

    /// Write a pixel to the render target using the callback.
    pub fn write_pixel(&mut self, x: u16, y: u16, color: u32) -> bool {
        (self.write_pixel)(x, y, color)
    }
}

/// Callback-backed render target for per-glyph bulk writes.
pub struct BulkCallbacks<'a, P>
where
    P: FnMut(i32, i32, u16, u16, &[u32]) -> bool,
{
    /// Width of the render target in pixels.
    width: u16,
    /// Height of the render target in pixels.
    height: u16,
    /// Scratch buffer for glyph rendering (must be large enough to hold the largest glyph).
    scratch: &'a mut [u32],
    /// Callback to write a glyph: (x, y, width, height, glyph) -> success.
    write_glyph: P,
}

impl<'a, P> BulkCallbacks<'a, P>
where
    P: FnMut(i32, i32, u16, u16, &[u32]) -> bool,
{
    /// Create a new bulk callback render target.
    pub fn new(width: u16, height: u16, scratch: &'a mut [u32], write_glyph: P) -> Self {
        Self {
            width,
            height,
            scratch,
            write_glyph,
        }
    }

    /// Get the dimensions of the render target.
    pub fn dimensions(&self) -> (u16, u16) {
        (self.width, self.height)
    }

    /// Get the capacity of the scratch buffer in pixels.
    pub fn scratch_capacity(&self) -> usize {
        self.scratch.len()
    }

    /// Get a mutable reference to the scratch buffer for glyph rendering.
    pub fn scratch_mut(&mut self) -> &mut [u32] {
        self.scratch
    }

    /// Write a glyph to the render target using the callback.
    pub fn write_glyph(&mut self, x: i32, y: i32, width: u16, height: u16, pixels: &[u32]) -> bool {
        (self.write_glyph)(x, y, width, height, pixels)
    }

    /// Internal helper to write from the scratch buffer, ensuring we don't exceed its capacity.
    pub(crate) fn emit_from_scratch(
        &mut self,
        x: i32,
        y: i32,
        width: u16,
        height: u16,
        used_pixels: usize,
    ) -> bool {
        let pixels = &self.scratch[..used_pixels];
        (self.write_glyph)(x, y, width, height, pixels)
    }
}

/// Configuration for text rendering.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RenderConfig {
    /// Color to render the text.
    pub color: u32,
    /// SDF-specific configuration (ignored for bitmap fonts).
    pub sdf: SdfConfig,
}

impl RenderConfig {
    /// Create a new render config with the specified color.
    pub const fn with_color(mut self, color: u32) -> Self {
        self.color = color;
        self
    }

    /// Create a new render config with the specified SDF settings.
    pub const fn with_sdf(mut self, sdf: SdfConfig) -> Self {
        self.sdf = sdf;
        self
    }
}

impl Default for RenderConfig {
    fn default() -> Self {
        Self {
            color: 0xffffff,
            sdf: SdfConfig::default(),
        }
    }
}

/// Configuration for SDF rendering (only used with SDF fonts).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SdfConfig {
    /// Font size in pixels (only affects SDF fonts).
    pub size: u16,
    /// Mid-value for SDF (usually 0.5).
    pub mid_value: f32,
    /// Smoothing factor for anti-aliasing.
    pub smoothing: f32,
}

impl SdfConfig {
    /// Create a new SDF config with the specified size.
    pub const fn with_size(mut self, size: u16) -> Self {
        self.size = size;
        self
    }

    /// Create a new SDF config with the specified mid-value.
    pub const fn with_mid_value(mut self, mid_value: f32) -> Self {
        self.mid_value = mid_value;
        self
    }

    /// Create a new SDF config with the specified smoothing factor.
    pub const fn with_smoothing(mut self, smoothing: f32) -> Self {
        self.smoothing = smoothing;
        self
    }
}

impl Default for SdfConfig {
    fn default() -> Self {
        Self {
            size: 16,
            mid_value: 0.5,
            smoothing: 0.1,
        }
    }
}

/// Text alignment options.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TextAlign {
    /// Horizontal alignment.
    pub horizontal: AlignH,
    /// Vertical alignment.
    pub vertical: AlignV,
}

impl TextAlign {
    /// Create a new TextAlign with the specified horizontal and vertical alignment.
    pub const fn new(horizontal: AlignH, vertical: AlignV) -> Self {
        Self {
            horizontal,
            vertical,
        }
    }
}

impl Default for TextAlign {
    fn default() -> Self {
        Self {
            horizontal: AlignH::Left,
            vertical: AlignV::Top,
        }
    }
}

/// Main renderer struct.
pub struct Glyphr {
    render_config: RenderConfig,
}

impl Default for Glyphr {
    /// Create a new text renderer with default configuration.
    fn default() -> Self {
        Self::new()
    }
}

impl Glyphr {
    /// Create a new text renderer with default configuration.
    pub fn new() -> Self {
        Self {
            render_config: RenderConfig::default(),
        }
    }

    /// Create a new text renderer with custom configuration.
    pub fn with_config(render_config: RenderConfig) -> Self {
        Self { render_config }
    }

    /// Update the render configuration.
    pub fn set_config(&mut self, config: RenderConfig) {
        self.render_config = config;
    }

    /// Get the current render configuration.
    pub fn config(&self) -> &RenderConfig {
        &self.render_config
    }

    /// Renders text using the configured per-pixel callback.
    pub fn draw_text<P>(
        &self,
        target: &mut Callbacks<P>,
        text: &str,
        font: Font,
        mut x: i32,
        y: i32,
        align: TextAlign,
    ) -> Result<(), GlyphrError>
    where
        P: FnMut(u16, u16, u32) -> bool,
    {
        let scale = match font.format {
            BitmapFormat::SDF => self.render_config.sdf.size as f32 / font.size as f32,
            BitmapFormat::Bitmap => 1.0,
        };
        let ascent = i32::from(font.ascent);
        let descent = i32::from(font.descent);

        let x_offset = match align.horizontal {
            AlignH::Center => self.measure_text(text, font) / 2,
            AlignH::Right => self.measure_text(text, font),
            AlignH::Left => 0,
        };

        let y_offset = match align.vertical {
            AlignV::Top => (descent as f32 * scale) as i32,
            AlignV::Center => {
                let total_height = (ascent - descent) as f32 * scale;
                -(total_height / 2.0) as i32
            }
            AlignV::Baseline => -(ascent as f32 * scale) as i32,
        };

        for c in text.chars() {
            let glyph = font.find_glyph(c).ok_or(GlyphrError::InvalidGlyph(c))?;
            let glyph_y = y
                + y_offset
                + ((ascent - i32::from(glyph.ymin) - i32::from(glyph.height)) as f32 * scale)
                    as i32;
            renderer::render_glyph(x - x_offset, glyph_y, c, font, self, scale, target)?;
            x += (glyph.advance_width as f32 * scale) as i32;
        }

        Ok(())
    }

    /// Renders text calling the callback once per glyph with a full ARGB buffer.
    pub fn draw_text_bulk<P>(
        &self,
        target: &mut BulkCallbacks<'_, P>,
        text: &str,
        font: Font,
        mut x: i32,
        y: i32,
        align: TextAlign,
    ) -> Result<(), GlyphrError>
    where
        P: FnMut(i32, i32, u16, u16, &[u32]) -> bool,
    {
        let scale = match font.format {
            BitmapFormat::SDF => self.render_config.sdf.size as f32 / font.size as f32,
            BitmapFormat::Bitmap => 1.0,
        };
        let ascent = i32::from(font.ascent);
        let descent = i32::from(font.descent);

        let x_offset = match align.horizontal {
            AlignH::Center => self.measure_text(text, font) / 2,
            AlignH::Right => self.measure_text(text, font),
            AlignH::Left => 0,
        };

        let y_offset = match align.vertical {
            AlignV::Top => (descent as f32 * scale) as i32,
            AlignV::Center => {
                let total_height = (ascent - descent) as f32 * scale;
                -(total_height / 2.0) as i32
            }
            AlignV::Baseline => -(ascent as f32 * scale) as i32,
        };

        for c in text.chars() {
            let glyph = font.find_glyph(c).ok_or(GlyphrError::InvalidGlyph(c))?;
            let glyph_y = y
                + y_offset
                + ((ascent - i32::from(glyph.ymin) - i32::from(glyph.height)) as f32 * scale)
                    as i32;
            renderer::render_glyph_bulk(x - x_offset, glyph_y, c, font, self, scale, target)?;
            x += (glyph.advance_width as f32 * scale) as i32;
        }

        Ok(())
    }

    /// Returns the width of text in pixels for the current render config.
    pub fn measure_text(&self, phrase: &str, font: Font) -> i32 {
        let scale = match font.format {
            BitmapFormat::SDF => self.render_config.sdf.size as f32 / font.size as f32,
            BitmapFormat::Bitmap => 1.0,
        };
        let mut tot = 0;
        for c in phrase.chars() {
            if let Some(glyph) = font.find_glyph(c) {
                tot += (glyph.advance_width as f32 * scale) as i32;
            }
        }
        tot
    }
}

#[derive(Debug, Clone)]
pub enum GlyphrError {
    /// Rendering position is out of bounds of the target.
    OutOfBounds,
    /// The font does not contain a glyph for the specified character.
    InvalidGlyph(char),
    /// The provided scratch buffer is too small to render the largest glyph.
    BufferTooSmall { needed: usize, available: usize },
    /// The render target is invalid (e.g. callback returned false).
    InvalidTarget,
}

impl core::fmt::Display for GlyphrError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            GlyphrError::OutOfBounds => write!(f, "Rendering position is out of bounds"),
            GlyphrError::InvalidGlyph(c) => write!(f, "Glyph not found: '{c}'"),
            GlyphrError::BufferTooSmall { needed, available } => {
                write!(
                    f,
                    "Scratch buffer too small: needed {needed} pixels, available {available}"
                )
            }
            GlyphrError::InvalidTarget => write!(f, "Invalid render target"),
        }
    }
}

impl core::error::Error for GlyphrError {}

#[cfg(test)]
mod tests {
    use super::*;
    use glyphr_types::Glyph;

    fn bitmap_font<'a>(glyphs: &'a [Glyph<'a>]) -> Font<'a> {
        Font {
            glyphs,
            size: 16,
            ascent: 2,
            descent: 0,
            line_gap: 0,
            format: BitmapFormat::Bitmap,
        }
    }

    #[test]
    fn sdf_config_default_values() {
        let cfg = SdfConfig::default();
        assert_eq!(cfg.size, 16);
        assert_eq!(cfg.mid_value, 0.5);
        assert_eq!(cfg.smoothing, 0.1);
    }

    #[test]
    fn render_config_default_values() {
        let cfg = RenderConfig::default();
        assert_eq!(cfg.color, 0xffffff);
        assert_eq!(cfg.sdf.size, 16);
        assert_eq!(cfg.sdf.mid_value, 0.5);
        assert_eq!(cfg.sdf.smoothing, 0.1);
    }

    #[test]
    fn glyphr_new_initializes_correctly() {
        let glyphr = Glyphr::new();

        assert_eq!(glyphr.render_config.color, 0xffffff);
        assert_eq!(glyphr.render_config.sdf.size, 16);
        assert_eq!(glyphr.render_config.sdf.mid_value, 0.5);
        assert_eq!(glyphr.render_config.sdf.smoothing, 0.1);
    }

    #[test]
    fn callbacks_sink_works() {
        let mut writes = 0u32;
        let mut target = Callbacks::new(8, 8, |_x, _y, _color| {
            writes += 1;
            true
        });

        assert!(target.write_pixel(1, 1, 0xff00ff00));
        assert_eq!(writes, 1);
    }

    #[test]
    fn bulk_callbacks_sink_works() {
        let mut writes = 0u32;
        let mut scratch = [0u32; 16];
        let mut target = BulkCallbacks::new(8, 8, &mut scratch, |_x, _y, w, h, pixels| {
            writes += 1;
            assert_eq!(w, 2);
            assert_eq!(h, 2);
            assert_eq!(pixels.len(), 4);
            true
        });

        let tile = [0xff00ff00u32; 4];
        assert!(target.write_glyph(1, 1, 2, 2, &tile));
        assert_eq!(writes, 1);
    }

    #[test]
    fn draw_text_bulk_errors_when_scratch_too_small() {
        let glyph_bitmap = [0b1111_0000u8];
        let glyphs = [Glyph {
            character: 'A',
            bitmap: &glyph_bitmap,
            width: 2,
            height: 2,
            xmin: 0,
            ymin: 0,
            advance_width: 2,
        }];

        let font = Font {
            glyphs: &glyphs,
            size: 16,
            ascent: 2,
            descent: 0,
            line_gap: 0,
            format: BitmapFormat::Bitmap,
        };

        let glyphr = Glyphr::new();
        let mut scratch = [0u32; 3];
        let mut target = BulkCallbacks::new(16, 16, &mut scratch, |_x, _y, _w, _h, _pixels| true);

        let result = glyphr.draw_text_bulk(
            &mut target,
            "A",
            font,
            0,
            0,
            TextAlign::new(AlignH::Left, AlignV::Top),
        );

        assert!(matches!(
            result,
            Err(GlyphrError::BufferTooSmall {
                needed: 4,
                available: 3
            })
        ));
    }

    #[test]
    fn draw_text_returns_invalid_glyph_for_missing_character() {
        let glyph_bitmap = [0b1000_0000u8];
        let glyphs = [Glyph {
            character: 'A',
            bitmap: &glyph_bitmap,
            width: 1,
            height: 1,
            xmin: 0,
            ymin: 0,
            advance_width: 1,
        }];
        let font = bitmap_font(&glyphs);

        let glyphr = Glyphr::new();
        let mut target = Callbacks::new(8, 8, |_x, _y, _color| true);
        let result = glyphr.draw_text(
            &mut target,
            "AZ",
            font,
            0,
            0,
            TextAlign::new(AlignH::Left, AlignV::Top),
        );

        assert!(matches!(result, Err(GlyphrError::InvalidGlyph('Z'))));
    }

    #[test]
    fn draw_text_bulk_emits_once_per_glyph() {
        let glyph_bitmap = [0b1000_0000u8, 0b1000_0000u8];
        let glyphs = [
            Glyph {
                character: 'A',
                bitmap: &glyph_bitmap[0..1],
                width: 1,
                height: 1,
                xmin: 0,
                ymin: 0,
                advance_width: 2,
            },
            Glyph {
                character: 'B',
                bitmap: &glyph_bitmap[1..2],
                width: 1,
                height: 1,
                xmin: 0,
                ymin: 0,
                advance_width: 3,
            },
        ];
        let font = bitmap_font(&glyphs);

        let glyphr = Glyphr::new();
        let mut scratch = [0u32; 8];
        let mut calls = 0usize;
        let mut x_positions = [0i32; 2];
        let mut target = BulkCallbacks::new(32, 32, &mut scratch, |x, _y, w, h, pixels| {
            x_positions[calls] = x;
            assert_eq!(w, 1);
            assert_eq!(h, 1);
            assert_eq!(pixels.len(), 1);
            calls += 1;
            true
        });

        glyphr
            .draw_text_bulk(
                &mut target,
                "AB",
                font,
                5,
                0,
                TextAlign::new(AlignH::Left, AlignV::Top),
            )
            .unwrap();

        assert_eq!(calls, 2);
        assert_eq!(x_positions[0], 5);
        assert_eq!(x_positions[1], 7);
    }

    #[test]
    fn measure_text_ignores_missing_glyphs() {
        let glyph_bitmap = [0b1000_0000u8];
        let glyphs = [Glyph {
            character: 'A',
            bitmap: &glyph_bitmap,
            width: 1,
            height: 1,
            xmin: 0,
            ymin: 0,
            advance_width: 4,
        }];
        let font = bitmap_font(&glyphs);

        let glyphr = Glyphr::new();
        assert_eq!(glyphr.measure_text("AZA", font), 8);
    }
}