embedded-graphics-profont 0.1.0

Bitmap font rendering for embedded-graphics with anchor and tracking support
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
use embedded_graphics::{
    draw_target::DrawTarget,
    geometry::Point,
    pixelcolor::PixelColor,
    Drawable,
};

use crate::renderer;

/// Text anchor point for positioning.
///
/// Defines which point of the text bounding box should be placed at the given position.
/// This allows precise control over text alignment without calculating dimensions manually.
///
/// # Examples
///
/// - `TopLeft`: Position is at top-left corner (default)
/// - `MiddleCenter`: Position is at the center of the text
/// - `BottomRight`: Position is at bottom-right corner
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Anchor {
    /// Position text so the given point is at its top-left corner.
    TopLeft,
    /// Position text so the given point is at its top-center.
    TopCenter,
    /// Position text so the given point is at its top-right corner.
    TopRight,
    /// Position text so the given point is at its middle-left.
    MiddleLeft,
    /// Position text so the given point is at its center.
    MiddleCenter,
    /// Position text so the given point is at its middle-right.
    MiddleRight,
    /// Position text so the given point is at its bottom-left corner.
    BottomLeft,
    /// Position text so the given point is at its bottom-center.
    BottomCenter,
    /// Position text so the given point is at its bottom-right corner.
    BottomRight,
}

impl Default for Anchor {
    fn default() -> Self {
        Anchor::TopLeft
    }
}

/// Trait for types that support anchoring.
///
/// Allows configuring the anchor point for text and character positioning.
///
/// # Examples
///
/// ```ignore
/// use embedded_graphics_profont::{Text, Anchor, WithAnchor};
/// use embedded_graphics::geometry::Point;
///
/// let text = Text::new("Hello", Point::new(100, 100), &font, color)
///     .with_anchor(Anchor::MiddleCenter);
/// ```
pub trait WithAnchor: Sized {
    /// Set the anchor point for positioning this object.
    fn with_anchor(self, anchor: Anchor) -> Self;
}

/// A bitmap font definition.
///
/// Contains all data needed to render text: glyph lookup table, bitmap data,
/// and metadata about the font.
///
/// # Fields
///
/// - `lookup_table`: Array of glyph entries, one per character in the supported range
/// - `data`: Raw bitmap data for all glyphs, packed as bits
/// - `ascii_begin`: First character code in the supported range (e.g., 32 for space)
/// - `ascii_end`: Last character code in the supported range (e.g., 126 for tilde)
/// - `max_height`: Height of all glyphs in pixels
/// - `proportional_width`: Whether glyphs have variable widths (vs fixed-width)
///
/// # Examples
///
/// ```ignore
/// let font = Font::new(
///     &LOOKUP_TABLE,    // &[GlyphEntry]
///     &FONT_DATA,       // &[u8]
///     32,               // ASCII start
///     126,              // ASCII end
///     8,                // Height
///     true,             // Proportional
/// );
/// ```
pub struct Font {
    /// Lookup table mapping characters to glyph metadata.
    pub lookup_table: &'static [GlyphEntry],
    /// Packed bitmap data for all glyphs.
    pub data: &'static [u8],
    /// First character code supported by this font.
    pub ascii_begin: u32,
    /// Last character code supported by this font.
    pub ascii_end: u32,
    /// Maximum height of glyphs in this font (in pixels).
    pub max_height: u8,
    /// Whether this font uses proportional widths (vs monospace).
    #[allow(dead_code)]
    pub proportional_width: bool,
}

/// Metadata for a single glyph in a font.
///
/// Used as entries in the font's lookup table to locate and describe each character's bitmap.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlyphEntry {
    /// Width of this glyph in pixels.
    pub width: u8,
    /// Byte offset in font data where this glyph's bitmap starts.
    pub offset: u16,
}

/// A drawable text string with configurable positioning and styling.
///
/// Implements the `Drawable` trait from `embedded-graphics`, allowing it to be
/// rendered on any `DrawTarget`.
///
/// # Fields (Internal)
///
/// - `text`: String to render
/// - `position`: Position for the anchor point
/// - `font`: Font to use for rendering
/// - `color`: Pixel color for the text
/// - `anchor`: Which point of the text bounds the position refers to
/// - `tracking`: Extra pixel spacing between characters
///
/// # Examples
///
/// ```ignore
/// use embedded_graphics::geometry::Point;
/// use embedded_graphics_profont::{Text, Anchor, WithAnchor};
///
/// // Simple left-aligned text
/// let text = Text::new("Hello", Point::new(0, 0), &font, Rgb565::WHITE);
/// target.draw(&text)?;
///
/// // Centered text with letter spacing
/// let text = Text::new("Spaced", Point::new(64, 32), &font, color)
///     .with_anchor(Anchor::MiddleCenter)
///     .with_tracking(2);
/// target.draw(&text)?;
/// ```
pub struct Text<'a, C> {
    text: &'a str,
    position: Point,
    font: &'a Font,
    color: C,
    anchor: Anchor,
    tracking: i32,
}

/// A single drawable character with configurable positioning.
///
/// Like `Text`, this implements `Drawable` for rendering on `DrawTarget`.
///
/// Useful for rendering individual characters with positioning and anchor support.
///
/// # Examples
///
/// ```ignore
/// use embedded_graphics_profont::{Character, Anchor, WithAnchor};
/// use embedded_graphics::geometry::Point;
///
/// let ch = Character::new('A', Point::new(10, 10), &font, color)
///     .with_anchor(Anchor::MiddleCenter);
/// target.draw(&ch)?;
/// ```
pub struct Character<'a, C> {
    ch: char,
    position: Point,
    font: &'a Font,
    color: C,
    anchor: Anchor,
}

impl Font {
    /// Create a new font definition.
    ///
    /// # Arguments
    ///
    /// * `lookup_table` - Array of glyph entries, one per supported character
    /// * `data` - Raw bitmap data for all glyphs
    /// * `ascii_begin` - First supported character code
    /// * `ascii_end` - Last supported character code (inclusive)
    /// * `max_height` - Height of all glyphs in pixels
    /// * `proportional_width` - Whether glyphs have variable widths
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let font = Font::new(
    ///     &GLYPHS,      // 95 entries for ASCII 32-126
    ///     &FONT_DATA,   // Binary bitmap data
    ///     32,           // Space
    ///     126,          // Tilde
    ///     8,            // 8-pixel height
    ///     false,        // Monospace
    /// );
    /// ```
    pub const fn new(
        lookup_table: &'static [GlyphEntry],
        data: &'static [u8],
        ascii_begin: u32,
        ascii_end: u32,
        max_height: u8,
        proportional_width: bool,
    ) -> Self {
        Self {
            lookup_table,
            data,
            ascii_begin,
            ascii_end,
            max_height,
            proportional_width,
        }
    }

    /// Get the glyph metadata for a character.
    ///
    /// Returns `None` if the character is not supported by this font.
    #[inline]
    pub fn get_glyph(&self, c: char) -> Option<&GlyphEntry> {
        let code = c as u32;
        if code >= self.ascii_begin && code <= self.ascii_end {
            let index = (code - self.ascii_begin) as usize;
            self.lookup_table.get(index)
        } else {
            None
        }
    }

    /// Get the bitmap data for a glyph.
    ///
    /// Data is stored as packed bits: 8 bits per byte, where each set bit
    /// represents a pixel that should be drawn.
    pub fn glyph_data(&self, entry: &GlyphEntry) -> &[u8] {
        let start = entry.offset as usize;
        let bytes_per_row = (entry.width as usize + 7) / 8;
        let end = start + (bytes_per_row * self.max_height as usize);
        &self.data[start..end]
    }

    /// Measure the pixel width of a text string.
    ///
    /// Returns the total width including any unsupported characters (which contribute 0 width)
    /// and the specified tracking value.
    ///
    /// # Arguments
    ///
    /// * `text` - The string to measure
    /// * `tracking` - Extra spacing to add between characters in pixels
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let width = font.measure_str("Hello", 0);      // No extra spacing
    /// let width = font.measure_str("Hello", 2);      // 2 pixels between chars
    /// ```
    pub fn measure_str(&self, text: &str, tracking: i32) -> u32 {
        let mut total_width: i32 = 0;
        let mut count = 0;

        for c in text.chars() {
            if let Some(entry) = self.get_glyph(c) {
                total_width += entry.width as i32 + tracking;
                count += 1;
            }
        }

        if count > 0 && tracking > 0 {
            total_width -= tracking;
        }

        total_width.max(0) as u32
    }
}

impl<'a, C> Text<'a, C>
where
    C: PixelColor,
{
    /// Create a new text drawable.
    ///
    /// The text will be positioned at the given point using the default
    /// `TopLeft` anchor. Use `with_anchor()` to change the anchor point.
    ///
    /// # Arguments
    ///
    /// * `text` - String to render
    /// * `position` - Position of the anchor point
    /// * `font` - Font to use
    /// * `color` - Pixel color for the text
    pub fn new(text: &'a str, position: Point, font: &'a Font, color: C) -> Self {
        Self {
            text,
            position,
            font,
            color,
            anchor: Anchor::TopLeft,
            tracking: 0,
        }
    }

    /// Set the spacing between characters in pixels.
    ///
    /// # Arguments
    ///
    /// * `tracking` - Extra pixels to add between characters (can be negative)
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let text = Text::new("Hello", Point::new(0, 0), &font, color)
    ///     .with_tracking(2);  // 2 pixels between each character
    /// ```
    pub fn with_tracking(mut self, tracking: i32) -> Self {
        self.tracking = tracking;
        self
    }
}

impl<'a, C: PixelColor> WithAnchor for Text<'a, C> {
    fn with_anchor(mut self, anchor: Anchor) -> Self {
        self.anchor = anchor;
        self
    }
}

impl<'a, C> Character<'a, C>
where
    C: PixelColor,
{
    /// Create a new character drawable.
    ///
    /// The character will be positioned at the given point using the default
    /// `TopLeft` anchor. Use `with_anchor()` to change the anchor point.
    ///
    /// If the character is not supported by the font, it will simply not render.
    ///
    /// # Arguments
    ///
    /// * `ch` - Character to render
    /// * `position` - Position of the anchor point
    /// * `font` - Font to use
    /// * `color` - Pixel color for the character
    pub fn new(ch: char, position: Point, font: &'a Font, color: C) -> Self {
        Self {
            ch,
            position,
            font,
            color,
            anchor: Anchor::TopLeft,
        }
    }
}

impl<'a, C: PixelColor> WithAnchor for Character<'a, C> {
    fn with_anchor(mut self, anchor: Anchor) -> Self {
        self.anchor = anchor;
        self
    }
}

impl<'a, C> Drawable for Text<'a, C>
where
    C: PixelColor,
{
    type Color = C;
    type Output = ();

    fn draw<D>(&self, target: &mut D) -> Result<Self::Output, D::Error>
    where
        D: DrawTarget<Color = Self::Color>,
    {
        let text_width = self.font.measure_str(self.text, self.tracking) as i32;
        let text_height = self.font.max_height as i32;

        let (x, y) = match self.anchor {
            Anchor::TopLeft => (
                self.position.x, 
                self.position.y
            ),
            Anchor::TopCenter => (
                self.position.x - (text_width / 2), 
                self.position.y
            ),
            Anchor::TopRight => (
                self.position.x - text_width, 
                self.position.y
            ),
            Anchor::MiddleLeft => (
                self.position.x, 
                self.position.y - (text_height / 2)
            ),
            Anchor::MiddleCenter => (
                self.position.x - (text_width / 2),
                self.position.y - (text_height / 2),
            ),
            Anchor::MiddleRight => (
                self.position.x - text_width,
                self.position.y - (text_height / 2),
            ),
            Anchor::BottomLeft => (
                self.position.x, 
                self.position.y - text_height
            ),
            Anchor::BottomCenter => (
                self.position.x - (text_width / 2), 
                self.position.y - text_height
            ),
            Anchor::BottomRight => (
                self.position.x - text_width, 
                self.position.y - text_height
            ),
        };

        renderer::draw_str(
            target,
            self.text,
            Point::new(x, y),
            self.font,
            self.color,
            self.tracking,
        )?;

        Ok(())
    }
}

impl<'a, C> Drawable for Character<'a, C>
where
    C: PixelColor,
{
    type Color = C;
    type Output = ();

    fn draw<D>(&self, target: &mut D) -> Result<Self::Output, D::Error>
    where
        D: DrawTarget<Color = Self::Color>,
    {
        let char_width = match self.font.get_glyph(self.ch) {
            Some(entry) => entry.width as i32,
            None => return Ok(()),
        };
        let char_height = self.font.max_height as i32;

        let (x, y) = match self.anchor {
            Anchor::TopLeft => (
                self.position.x, 
                self.position.y
            ),
            Anchor::TopCenter => (
                self.position.x - (char_width / 2), 
                self.position.y
            ),
            Anchor::TopRight => (
                self.position.x - char_width, 
                self.position.y
            ),
            Anchor::MiddleLeft => (
                self.position.x, 
                self.position.y - (char_height / 2)
            ),
            Anchor::MiddleCenter => (
                self.position.x - (char_width / 2),
                self.position.y - (char_height / 2),
            ),
            Anchor::MiddleRight => (
                self.position.x - char_width,
                self.position.y - (char_height / 2),
            ),
            Anchor::BottomLeft => (
                self.position.x, 
                self.position.y - char_height
            ),
            Anchor::BottomCenter => (
                self.position.x - (char_width / 2),
                self.position.y - char_height,
            ),
            Anchor::BottomRight => (
                self.position.x - char_width, 
                self.position.y - char_height
            ),
        };

        renderer::draw_char(target, self.ch, Point::new(x, y), self.font, self.color)?;

        Ok(())
    }
}