macroquad-text 0.2.0

A Simple way to draw text in macroquad with support of using glyphs from multiple fonts in a single draw_text call, also known as fallback fonts
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
//! A lot of stuff was based on macroquads font system,
//! just removed dpi_scaling and font_scaling and added
//! fallback font support
//!
//! **Example**
//!
//! From [examples/render_text.rs](https://github.com/Ricky12Awesome/macroquad-text/blob/main/examples/render_text.rs)
//!
//! ```rs
//! // Include Fonts
//! const NOTO_SANS: &[u8] = include_bytes!("../assets/fonts/NotoSans-Regular.ttf");
//! const NOTO_SANS_JP: &[u8] = include_bytes!("../assets/fonts/NotoSansJP-Regular.otf");
//!
//! // Window Config for macroquad
//! fn window_conf() -> Conf { ... }
//!
//! #[macroquad::main(window_conf)]
//! async fn main() {
//!   // Start by creating a fonts instance to handle all your fonts
//!   let mut fonts = Fonts::default();
//!
//!   // Load fonts, the order you load fonts is the order it uses for lookups
//!   fonts.load_font_from_bytes("Noto Sans", NOTO_SANS).unwrap();
//!   fonts.load_font_from_bytes("Noto Sans JP", NOTO_SANS_JP).unwrap();
//!
//!   loop {
//!     // Draw text
//!     fonts.draw_text("Nice", 20.0, 0.0, 69, Color::from([1.0; 4]));
//!     fonts.draw_text("良い", 20.0, 89.0, 69, Color::from([1.0; 4]));
//!     fonts.draw_text("Nice 良い", 20.0, 178.0, 69, Color::from([1.0; 4]));
//!
//!     next_frame().await;
//!   }
//! }
//! ```
//!
//! ![img.png](https://raw.githubusercontent.com/Ricky12Awesome/macroquad-text/main/examples/render_text_window.png)
//!

#![deny(unsafe_code)]

use std::{cell::RefCell, collections::HashMap, ops::Deref, path::Path};

use fontdue::{FontResult, FontSettings};
use macroquad::prelude::{
  draw_texture_ex, vec2, Color, DrawTextureParams, FilterMode, Image, TextDimensions,
};

use crate::{
  atlas::Atlas,
  misc::{read_file, IoError, IoErrorKind, IoResult},
};

pub(crate) mod atlas;
pub(crate) mod misc;

pub type ScalingMode = FilterMode;
pub type FontdueFont = fontdue::Font;

/// Where to draw from on the screen
///
/// **Default** [DrawFrom::TopLeft]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum DrawFrom {
  /// Starts drawing from the bottom left corner
  BottomLeft,
  /// Starts drawing from the top left corner
  ///
  /// this is the default
  TopLeft,
}

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

#[derive(Default, Debug, Copy, Clone, PartialEq, PartialOrd)]
pub(crate) struct CharacterInfo {
  pub id: u64,
  pub offset_x: f32,
  pub offset_y: f32,
  pub advance: f32,
}

/// Text parameters for [Fonts::draw_text_ex]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct TextParams<'a> {
  /// Text to draw to the screen
  pub text: &'a str,
  /// x-coordinate of the text
  pub x: f32,
  /// y-coordinate of the text
  pub y: f32,
  /// The size of the text in pixels
  pub size: u16,
  /// The color of the text
  pub color: Color,
  /// Where to draw from
  pub draw: DrawFrom,
}

impl<'a> Default for TextParams<'a> {
  fn default() -> Self {
    Self {
      text: "",
      x: 0.0,
      y: 0.0,
      size: 22,
      color: Color::from_rgba(255, 255, 255, 255),
      draw: DrawFrom::TopLeft,
    }
  }
}

/// Stores font data, also stores caches for much faster rendering times
#[derive(Debug)]
pub struct Font<'a> {
  pub name: &'a str,
  font: FontdueFont,
  atlas: RefCell<Atlas>,
  chars: RefCell<HashMap<(char, u16), CharacterInfo>>,
}

impl<'a> Deref for Font<'a> {
  type Target = FontdueFont;

  fn deref(&self) -> &Self::Target {
    &self.font
  }
}

impl<'a> Font<'a> {
  /// Creates a new font with a given name, [fontdue::Font], and [ScalingMode]
  fn new(name: &'a str, font: FontdueFont, mode: ScalingMode) -> Self {
    Self {
      name,
      font,
      atlas: RefCell::new(Atlas::new(mode)),
      chars: RefCell::default(),
    }
  }

  /// Checks if this font contains a given character
  pub fn contains(&self, c: char) -> bool {
    self.lookup_glyph_index(c) != 0
  }

  fn _cache_glyph(&self, c: char, size: u16) -> CharacterInfo {
    let (matrix, bitmap) = self.rasterize(c, size as f32);
    let (width, height) = (matrix.width as u16, matrix.height as u16);

    let id = self.atlas.borrow_mut().new_unique_id();
    let bytes = bitmap
      .iter()
      .flat_map(|coverage| vec![255, 255, 255, *coverage])
      .collect::<Vec<_>>();

    self.atlas.borrow_mut().cache_sprite(
      id,
      Image {
        width,
        height,
        bytes,
      },
    );

    CharacterInfo {
      id,
      offset_x: matrix.xmin as f32,
      offset_y: matrix.ymin as f32,
      advance: matrix.advance_width,
    }
  }

  /// Caches a glyph for a given character with a given font size
  ///
  /// You don't really need to call this function since caching happens automatically
  pub fn cache_glyph(&self, c: char, size: u16) {
    if !self.chars.borrow().contains_key(&(c, size)) {
      let info = self._cache_glyph(c, size);

      self.chars.borrow_mut().insert((c, size), info);
    }
  }

  /// Recaches all cached glyphs, this is expensive to call
  ///
  /// normally you wouldn't need to call this
  pub fn recache_glyphs(&self) {
    for ((c, size), info) in self.chars.borrow_mut().iter_mut() {
      *info = self._cache_glyph(*c, *size);
    }
  }
}

#[derive(Debug)]
pub struct Fonts<'a> {
  fonts: Vec<Font<'a>>,
  index_by_name: HashMap<&'a str, usize>,
  default_sm: ScalingMode,
}

impl<'a> Default for Fonts<'a> {
  /// Creates a new [Fonts] instance to handle all your font
  ///
  /// Same as calling [Fonts::new(ScalingMode::Linear)]
  fn default() -> Self {
    Self::new(ScalingMode::Linear)
  }
}

impl<'a> Fonts<'a> {
  /// Creates a new [Fonts] instance to handle all your fonts with a given [ScalingMode]
  ///
  /// You can also call [Fonts::default] which defaults to [ScalingMode::Linear]
  ///
  /// **Examples**
  ///
  /// With nearest mode
  /// ```rs
  /// let mut fonts = Fonts::new(ScalingMode::Nearest);
  /// ```
  /// With linear mode
  /// ```rs
  /// let mut fonts = Fonts::new(ScalingMode::Linear);
  /// ```
  pub fn new(default_sm: ScalingMode) -> Self {
    Self {
      fonts: Vec::default(),
      index_by_name: HashMap::default(),
      default_sm,
    }
  }

  /// Returns an immutable reference to the
  /// list of fonts that are currently loaded
  pub fn fonts(&self) -> &Vec<Font> {
    &self.fonts
  }

  /// Caches a glyph for a given character with a given font size
  ///
  /// You don't really need to call this function since caching happens automatically
  pub fn cache_glyph(&self, c: char, size: u16) {
    for font in self.fonts.iter() {
      font.cache_glyph(c, size);
    }
  }

  /// Loads font from bytes with a given name and scale
  ///
  ///
  /// What Scale does
  /// ---------------
  /// (copied from [FontSettings::scale](FontSettings))
  ///
  /// The scale in px the font geometry is optimized for. Fonts rendered at
  /// the scale defined here will be the most optimal in terms of looks and performance. Glyphs
  /// rendered smaller than this scale will look the same but perform slightly worse, while
  /// glyphs rendered larger than this will looks worse but perform slightly better. The units of
  /// the scale are pixels per Em unit.
  pub fn load_font_from_bytes_with_scale(
    &mut self,
    name: &'a str,
    bytes: &[u8],
    scale: f32,
  ) -> FontResult<()> {
    let settings = FontSettings {
      collection_index: 0,
      scale,
    };
    let font = FontdueFont::from_bytes(bytes, settings)?;

    self.index_by_name.insert(name, self.fonts.len());
    self.fonts.push(Font::new(name, font, self.default_sm));

    Ok(())
  }

  /// Loads font from bytes with a given name and a default scale of 100.0
  ///
  /// **See** [Self::load_font_from_bytes_with_scale]
  pub fn load_font_from_bytes(&mut self, name: &'a str, bytes: &[u8]) -> FontResult<()> {
    self.load_font_from_bytes_with_scale(name, bytes, 100.0)
  }

  /// Loads font from a file with a given name and path and a default scale of 100.0
  ///
  /// **See** [Self::load_font_from_bytes_with_scale]
  pub fn load_font_from_file(&mut self, name: &'a str, path: impl AsRef<Path>) -> IoResult<()> {
    self.load_font_from_file_with_scale(name, path, 100.0)
  }

  /// Loads font from a file with a given name, path and scale
  ///
  /// **See** [Self::load_font_from_bytes_with_scale]
  pub fn load_font_from_file_with_scale(
    &mut self,
    name: &'a str,
    path: impl AsRef<Path>,
    scale: f32,
  ) -> IoResult<()> {
    let bytes = read_file(path)?;

    self
      .load_font_from_bytes_with_scale(name, &bytes, scale)
      .map_err(|err| IoError::new(IoErrorKind::InvalidData, err))
  }

  /// Unloads a currently loaded font by its index
  ///
  /// This will also re-index all the currently loaded fonts
  pub fn unload_font_by_index(&mut self, index: usize) {
    if self.fonts.len() <= index {
      return;
    }

    self.fonts.remove(index);
    self.index_by_name.clear();

    for (index, font) in self.fonts.iter().enumerate() {
      self.index_by_name.insert(font.name, index);
    }
  }

  /// Unloads a currently loaded font by it name
  ///
  /// This will also re-index all the currently loaded fonts
  pub fn unload_font_by_name(&mut self, name: &str) {
    self.unload_font_by_index(self.get_index_by_name(name).unwrap_or(self.fonts.len()));
  }

  /// Gets a currently loaded font by its index
  pub fn get_font_by_index(&self, index: usize) -> Option<&Font> {
    self.fonts.get(index)
  }

  /// Gets the first currently loaded font if it contains this character
  pub fn get_index_by_char(&self, c: char) -> Option<usize> {
    self.fonts.iter().position(|it| it.contains(c))
  }

  /// Gets a currently loaded font index by its name
  pub fn get_index_by_name(&self, name: &str) -> Option<usize> {
    self.index_by_name.get(name).copied()
  }

  /// Gets a currently loaded font by its name
  pub fn get_font_by_name(&self, name: &str) -> Option<&Font> {
    self.get_font_by_index(self.get_index_by_name(name)?)
  }

  /// Gets the first currently loaded font if it contains this character
  pub fn get_font_by_char(&self, c: char) -> Option<&Font> {
    self.get_font_by_index(self.get_index_by_char(c)?)
  }

  /// Gets the first currently loaded font if it contains this character,
  /// if no font that contains this character is found, it will return the first loaded font,
  /// **if no fonts are loaded then it will panic**
  pub fn get_font_by_char_or_panic(&self, c: char) -> &Font {
    self
      .get_font_by_char(c)
      .or_else(|| self.fonts.first())
      .expect("There is no font currently loaded")
  }

  /// Checks if any fonts supports this character
  pub fn contains(&self, c: char) -> bool {
    self.fonts.iter().any(|f| f.contains(c))
  }

  /// Measures text with a given font size
  ///
  /// **Example**
  /// ```rs
  /// let dimensions = fonts.measure_text("Some Text", 22);
  ///
  /// println!("width: {}, height: {}, offset_y: {}",
  ///   dimensions.width,
  ///   dimensions.height,
  ///   dimensions.offset_y
  /// )
  /// ```
  ///
  /// **See** [TextDimensions]
  pub fn measure_text(&self, text: &str, size: u16) -> TextDimensions {
    let mut width = 0f32;
    let mut min_y = f32::MAX;
    let mut max_y = f32::MIN;

    for c in text.chars() {
      let font = self.get_font_by_char_or_panic(c);

      font.cache_glyph(c, size);

      let info = font.chars.borrow()[&(c, size)];
      let glyph = font.atlas.borrow().get(info.id).unwrap().rect;

      width += info.advance;

      if min_y > info.offset_y {
        min_y = info.offset_y;
      }

      if max_y < glyph.h + info.offset_y {
        max_y = glyph.h + info.offset_y;
      }
    }

    TextDimensions {
      width,
      height: max_y - min_y,
      offset_y: max_y,
    }
  }

  /// Draws text with a given font size, draws from TopLeft
  ///
  /// **Examples**
  /// ```rs
  /// fonts.draw_text("Some Text", 20.0, 20.0, 22, Color::from_rgba(255, 255, 255, 255));
  /// ```
  ///
  /// **See** [Self::draw_text_ex]
  pub fn draw_text(&self, text: &str, x: f32, y: f32, size: u16, color: Color) -> TextDimensions {
    self.draw_text_ex(&TextParams {
      text,
      x,
      y,
      size,
      color,
      draw: Default::default(),
    })
  }

  /// Draws text with given [TextParams]
  ///
  /// **Example**
  /// ```rs
  /// fonts.draw_text_ex(&TextParams {
  ///   text: "Some Text",
  ///   x: 20.0,
  ///   y: 20.0,
  ///   // Default Size
  ///   size: 22,
  ///   // Default Color
  ///   color: Color::from_rgba(255, 255, 255, 255),
  ///   // Default Draw method
  ///   draw: DrawFrom::TopLeft
  /// });
  ///
  /// // Does the same as above
  /// fonts.draw_text_ex(&TextParams {
  ///   text: "Some Text",
  ///   x: 20.0,
  ///   y: 20.0,
  ///   ..Default::default()
  /// });
  /// ```
  ///
  /// **See** [Self::draw_text]
  pub fn draw_text_ex(&self, params: &TextParams) -> TextDimensions {
    let mut total_width = 0f32;

    for c in params.text.chars() {
      let font = self.get_font_by_char_or_panic(c);
      font.cache_glyph(c, params.size);
    }

    for c in params.text.chars() {
      let font = self.get_font_by_char_or_panic(c);
      let mut atlas = font.atlas.borrow_mut();
      let info = &font.chars.borrow()[&(c, params.size)];
      let glyph = atlas.get(info.id).unwrap().rect;
      let mut y = 0.0 - glyph.h - info.offset_y + params.y;

      if let DrawFrom::TopLeft = params.draw {
        y += params.size as f32;
      }

      draw_texture_ex(
        atlas.texture(),
        info.offset_x + total_width + params.x,
        y,
        params.color,
        DrawTextureParams {
          dest_size: Some(vec2(glyph.w, glyph.h)),
          source: Some(glyph),
          ..Default::default()
        },
      );

      total_width += info.advance;
    }

    self.measure_text(params.text, params.size)
  }
}