ril 0.10.3

Rust Imaging Library: A performant and high-level image processing crate for Rust
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//! Implements the font/text rasterizing and layout interface.

#![allow(clippy::cast_precision_loss, clippy::too_many_arguments)]

use crate::{Draw, Error::FontError, Image, OverlayMode, Pixel};

use fontdue::{
    layout::{CoordinateSystem, Layout, LayoutSettings, TextStyle},
    FontSettings,
};
use std::{fs::File, io::Read, ops::DerefMut, path::Path};

/// Represents a single font along with its alternatives used to render text.
/// Currently, this supports TrueType and OpenType fonts.
#[allow(clippy::doc_markdown)]
#[derive(Clone)]
pub struct Font {
    inner: fontdue::Font,
    settings: FontSettings,
}

impl Font {
    /// Opens the font from the given path.
    ///
    /// The optimal size is not the fixed size of the font - rather it is the size to optimize
    /// rasterizing the font for.
    ///
    /// Lower sizes will look worse but perform faster, while higher sizes will
    /// look better but perform slower. It is best to set this to the size that will likely be
    /// the most used.
    ///
    /// # Errors
    /// * Failed to load the font.
    pub fn open<P: AsRef<Path>>(path: P, optimal_size: f32) -> crate::Result<Self> {
        Self::from_reader(File::open(path)?, optimal_size)
    }

    /// Loads the font from the given byte slice. Useful for the `include_bytes!` macro.
    ///
    /// The optimal size is not the fixed size of the font - rather it is the size to optimize
    /// rasterizing the font for.
    ///
    /// Lower sizes will look worse but perform faster, while higher sizes will
    /// look better but perform slower. It is best to set this to the size that will likely be
    /// the most used.
    ///
    /// # Errors
    /// * Failed to load the font.
    pub fn from_bytes(bytes: &[u8], optimal_size: f32) -> crate::Result<Self> {
        let settings = FontSettings {
            scale: optimal_size,
            collection_index: 0,
        };
        let inner = fontdue::Font::from_bytes(bytes, settings).map_err(FontError)?;

        Ok(Self { inner, settings })
    }

    /// Loads the font from the given byte reader. See [`from_bytes`] if you already have a byte
    /// slice - that is much more performant.
    ///
    /// The optimal size is not the fixed size of the font - rather it is the size to optimize
    /// rasterizing the font for.
    ///
    /// Lower sizes will look worse but perform faster, while higher sizes will
    /// look better but perform slower. It is best to set this to the size that will likely be
    /// the most used.
    ///
    /// # Errors
    /// * Failed to load the font.
    pub fn from_reader<R: Read>(mut buffer: R, optimal_size: f32) -> crate::Result<Self> {
        let settings = FontSettings {
            scale: optimal_size,
            collection_index: 0,
        };
        let mut out = Vec::new();
        buffer.read_to_end(&mut out)?;

        let inner = fontdue::Font::from_bytes(out, settings).map_err(FontError)?;

        Ok(Self { inner, settings })
    }

    /// Returns a reference the [`fontdue::Font`] object associated with the font.
    /// It contains technical information about the font.
    #[must_use]
    pub const fn inner(&self) -> &fontdue::Font {
        &self.inner
    }

    /// Consumes this font and returns the [`fontdue::Font`] object associated with the font.
    /// It contains technical information about the font.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)] // no destructors
    pub fn into_inner(self) -> fontdue::Font {
        self.inner
    }

    /// Returns the optimal size, in pixels, of this font.
    ///
    /// The optimal size is not the fixed size of the font - rather it is the size to optimize
    /// rasterizing the font for.
    ///
    /// Lower sizes will look worse but perform faster, while higher sizes will
    /// look better but perform slower. It is best to set this to the size that will likely be
    /// the most used.
    #[must_use]
    pub const fn optimal_size(&self) -> f32 {
        self.settings.scale
    }
}

/// Determines how text should be wrapped.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum WrapStyle {
    /// Do not wrap the text.
    None,
    /// Keep words together and do not break in the middle of words. This is usually what is
    /// desired. Breaks to a newline at the nearest word boundary.
    ///
    /// This is the default behavior.
    Word,
    /// Keep as many characters per line as possible, and allow breaks in the middle of words.
    /// Breaks to a newline at the nearest character.
    Character,
}

impl Default for WrapStyle {
    fn default() -> Self {
        Self::Word
    }
}

/// Represents a text segment that can be drawn.
///
/// See [`TextLayout`] for a more robust implementation that supports rendering text with multiple
/// styles. This type is for more simple and lightweight usages.
///
/// Additionally, accessing metrics such as the width and height of the text cannot be done here,
/// but can be done in [`TextLayout`] since it keeps a running copy of the layout.
/// Use [`TextLayout`] if you will be needing to calculate the width and height of the text.
/// Additionally, [`TextLayout`] supports text anchoring, which can be used to align text.
///
/// If you need none of these features, text segments should be used in favor of being much more
/// lightweight.
///
/// Note that [`TextLayout`] is not cloneable while text segments are, which is one advantage
/// of using this over [`TextLayout`].
#[derive(Clone)]
pub struct TextSegment<'a, P: Pixel> {
    /// The position the text will be rendered at. Ignored if this is used in a [`TextLayout`].
    pub position: (u32, u32),
    /// The width of the text box. If this is used in a [`TextLayout`], this is ignored and
    /// [`TextLayout::with_width`] is used instead. This is used for text wrapping and wrapping only.
    pub width: Option<u32>,
    /// The content of the text segment.
    pub text: String,
    /// The font to use to render the text.
    pub font: &'a Font,
    /// The fill color the text will be in.
    pub fill: P,
    /// The overlay mode of the text. Note that anti-aliasing is still a bit funky with
    /// [`OverlayMode::Replace`], so it is best to use [`OverlayMode::Merge`] for this, which is
    /// the default.
    pub overlay: OverlayMode,
    /// The size of the text in pixels.
    pub size: f32,
    /// The wrapping style of the text. Note that text will only wrap if [`width`] is set.
    /// If this is used in a [`TextLayout`], this is ignored and [`TextLayout::with_wrap`] is
    /// used instead.
    pub wrap: WrapStyle,
}

impl<'a, P: Pixel> TextSegment<'a, P> {
    /// Creates a new text segment with the given text, font, and fill color.
    /// The text can be anything that implements [`AsRef<str>`].
    ///
    /// If this is used to be directly drawn (as opposed to in a [`TextLayout`]), the position
    /// is set to ``(0, 0)`` by default. Use [`with_position`][TextSegment::with_position] to set
    /// the position.
    ///
    /// The size defaults to the font's optimal size.
    /// You can override this by using the [`with_size`][Self::with_size] method.
    #[must_use]
    pub fn new(font: &'a Font, text: impl AsRef<str>, fill: P) -> Self {
        Self {
            position: (0, 0),
            width: None,
            text: text.as_ref().to_string(),
            font,
            fill,
            overlay: OverlayMode::Merge,
            size: font.optimal_size(),
            wrap: WrapStyle::Word,
        }
    }

    /// Sets the position of the text segment. Ignored if this segment is used in a [`TextLayout`].
    #[must_use]
    pub const fn with_position(mut self, x: u32, y: u32) -> Self {
        self.position = (x, y);
        self
    }

    /// Sets the size of the text segment.
    #[must_use]
    pub const fn with_size(mut self, size: f32) -> Self {
        self.size = size;
        self
    }

    /// Sets the overlay mode of the text segment.
    #[must_use]
    pub const fn with_overlay_mode(mut self, mode: OverlayMode) -> Self {
        self.overlay = mode;
        self
    }

    /// Sets the width of the text segment, used for text wrapping.
    /// If this is used in a [`TextLayout`], this is ignored and [`TextLayout::width`] is used instead.
    #[must_use]
    pub const fn with_width(mut self, width: u32) -> Self {
        self.width = Some(width);
        self
    }

    /// Sets the wrapping style of the text segment.
    /// If this is used in a [`TextLayout`], this is ignored and [`TextLayout::wrap`] is used instead.
    #[must_use]
    pub const fn with_wrap(mut self, wrap: WrapStyle) -> Self {
        self.wrap = wrap;
        self
    }

    fn layout(&self) -> Layout<(P, OverlayMode)> {
        let mut layout = Layout::new(CoordinateSystem::PositiveYDown);
        layout.reset(&LayoutSettings {
            x: self.position.0 as f32,
            y: self.position.1 as f32,
            max_width: if self.wrap == WrapStyle::None {
                None
            } else {
                self.width.map(|w| w as f32)
            },
            wrap_style: match self.wrap {
                WrapStyle::None | WrapStyle::Word => fontdue::layout::WrapStyle::Word,
                WrapStyle::Character => fontdue::layout::WrapStyle::Letter,
            },
            ..LayoutSettings::default()
        });
        layout.append(
            &[self.font.inner()],
            &TextStyle::with_user_data(&self.text, self.size, 0, (self.fill, self.overlay)),
        );
        layout
    }
}

fn render_layout<P: Pixel>(
    image: &mut Image<P>,
    fonts: &[&fontdue::Font],
    layout: &Layout<(P, OverlayMode)>,
) {
    let glyphs = layout.glyphs();
    if glyphs.is_empty() {
        return;
    }

    // SAFETY: already checked before calling
    let lines = unsafe { layout.lines().unwrap_unchecked() };
    for line in lines {
        for glyph in &glyphs[line.glyph_start..=line.glyph_end] {
            let (fill, overlay) = glyph.user_data;
            let font = fonts[glyph.font_index];
            let (metrics, bitmap) = font.rasterize_config(glyph.key);

            if metrics.width == 0 || glyph.char_data.is_whitespace() || metrics.height == 0 {
                continue;
            }

            for (row, y) in bitmap.chunks_exact(metrics.width).zip(glyph.y as i32..) {
                for (value, x) in row.iter().zip(glyph.x as i32..) {
                    let (x, y) = if x < 0 || y < 0 {
                        continue;
                    } else {
                        (x as u32, y as u32)
                    };

                    let value = *value;
                    if value == 0 {
                        continue;
                    }

                    if let Some(pixel) = image.get_pixel(x, y) {
                        *image.pixel_mut(x, y) = pixel.overlay_with_alpha(fill, overlay, value);
                    }
                }
            }
        }
    }
}

fn render_layout_with_alignment<P: Pixel>(
    image: &mut Image<P>,
    fonts: &[&fontdue::Font],
    layout: &Layout<(P, OverlayMode)>,
    widths: Vec<u32>,
    max_width: u32,
    fx: f32,
    ox: f32,
    oy: f32,
) {
    let glyphs = layout.glyphs();
    if glyphs.is_empty() {
        return;
    }

    // SAFETY: this was checked before calling
    let lines = unsafe { layout.lines().unwrap_unchecked() };
    for (line, width) in lines.iter().zip(widths) {
        let ox = ((max_width - width) as f32).mul_add(fx, ox);

        for glyph in &glyphs[line.glyph_start..=line.glyph_end] {
            let (fill, overlay) = glyph.user_data;
            let font = fonts[glyph.font_index];
            let (metrics, bitmap) = font.rasterize_config(glyph.key);

            if metrics.width == 0 || glyph.char_data.is_whitespace() || metrics.height == 0 {
                continue;
            }

            let x = (glyph.x + ox) as i32;
            let y = (glyph.y + oy) as i32;

            for (row, y) in bitmap.chunks_exact(metrics.width).zip(y..) {
                for (value, x) in row.iter().zip(x..) {
                    let (x, y) = if x < 0 || y < 0 {
                        continue;
                    } else {
                        (x as u32, y as u32)
                    };

                    let value = *value;
                    if value == 0 {
                        continue;
                    }

                    if let Some(pixel) = image.get_pixel(x, y) {
                        *image.pixel_mut(x, y) = pixel.overlay_with_alpha(fill, overlay, value);
                    }
                }
            }
        }
    }
}

impl<'a, P: Pixel> Draw<P> for TextSegment<'a, P> {
    fn draw<I: DerefMut<Target = Image<P>>>(&self, mut image: I) {
        render_layout(&mut *image, &[self.font.inner()], &self.layout());
    }
}

/// Represents where text is anchored horizontally.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum HorizontalAnchor {
    /// The x position is the left edge of the text. This is the default.
    Left,
    /// The x position is the center of the text. This also center-aligns the text.
    Center,
    /// The x position is the right edge of the text. This also right-aligns the text.
    Right,
}

impl Default for HorizontalAnchor {
    fn default() -> Self {
        Self::Left
    }
}

/// Represents how to align text horizontally within its bounding box.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TextAlign {
    /// Aligns the text to the left.
    Left,
    /// Aligns the text to the center.
    Center,
    /// Aligns the text to the right.
    Right,
}

impl Default for TextAlign {
    fn default() -> Self {
        Self::Left
    }
}

/// Represents where text is anchored vertically.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum VerticalAnchor {
    /// The y position is the top edge of the text. This is the default.
    Top,
    /// The y position is the center of the text.
    Center,
    /// The y position is the bottom edge of the text.
    Bottom,
}

impl Default for VerticalAnchor {
    fn default() -> Self {
        Self::Top
    }
}

/// Represents a high-level text layout that can layout text segments, maybe with different fonts.
///
/// This is a high-level layout that can be used to layout text segments. It can be used to layout
/// text segments with different fonts and styles, and has many features over [`TextSegment`] such
/// as text anchoring, which can be useful for text alignment. This also keeps track of font
/// metrics, meaning that unlike [`TextSegment`], this can be used to determine the width and height
/// of text before rendering it.
///
/// This is less efficient than [`TextSegment`] and you should use [`TextSegment`] if you don't need
/// any of the features [`TextLayout`] provides.
///
/// # Note
/// This is does not implement [`Clone`] and therefore it is not cloneable! Consider using
/// [`TextSegment`] if you require cloning functionality.
pub struct TextLayout<'a, P: Pixel> {
    inner: Layout<(P, OverlayMode)>,
    fonts: Vec<&'a fontdue::Font>,
    settings: LayoutSettings,
    x_anchor: HorizontalAnchor,
    y_anchor: VerticalAnchor,
    align: TextAlign,
}

impl<'a, P: Pixel> TextLayout<'a, P> {
    /// Creates a new text layout with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Layout::new(CoordinateSystem::PositiveYDown),
            fonts: Vec::new(),
            settings: LayoutSettings::default(),
            x_anchor: HorizontalAnchor::default(),
            y_anchor: VerticalAnchor::default(),
            align: TextAlign::default(),
        }
    }

    fn set_settings(&mut self, settings: LayoutSettings) {
        self.inner.reset(&settings);
        self.settings = settings;
    }

    /// Sets the position of the text layout.
    ///
    /// **This must be set before adding any text segments!**
    #[must_use]
    pub fn with_position(mut self, x: u32, y: u32) -> Self {
        self.set_settings(LayoutSettings {
            x: x as f32,
            y: y as f32,
            ..self.settings
        });
        self
    }

    /// Sets the wrapping style of the text. Make sure to also set the wrapping width using
    /// [`with_width`] for wrapping to work.
    ///
    /// **This must be set before adding any text segments!**
    #[must_use]
    pub fn with_wrap(mut self, wrap: WrapStyle) -> Self {
        self.set_settings(LayoutSettings {
            wrap_style: match wrap {
                WrapStyle::None | WrapStyle::Word => fontdue::layout::WrapStyle::Word,
                WrapStyle::Character => fontdue::layout::WrapStyle::Letter,
            },
            max_width: Some(self.settings.max_width.unwrap_or(f32::MAX)),
            ..self.settings
        });
        self
    }

    /// Sets the wrapping width of the text. This does not impact [`Self::width`] and [`Self::dimensions`].
    ///
    /// **This must be set before adding any text segments!**
    #[must_use]
    pub fn with_width(mut self, width: u32) -> Self {
        self.set_settings(LayoutSettings {
            max_width: Some(width as f32),
            ..self.settings
        });
        self
    }

    /// Adds a text segment to the text layout.
    pub fn push_segment(&mut self, segment: &TextSegment<'a, P>) {
        self.fonts.push(segment.font.inner());
        self.inner.append(
            &self.fonts,
            &TextStyle::with_user_data(
                &segment.text,
                segment.size,
                self.fonts.len() - 1,
                (segment.fill, segment.overlay),
            ),
        );
    }

    /// Takes this text layout and returns it with the given text segment added to the text layout.
    /// Useful for method chaining.
    #[must_use]
    pub fn with_segment(mut self, segment: &TextSegment<'a, P>) -> Self {
        self.push_segment(segment);
        self
    }

    /// Adds basic text to the text layout. This is a convenience method that creates a [`TextSegment`]
    /// with the given font, text, and fill and adds it to the text layout.
    ///
    /// The size of the text is determined by the font's optimal size.
    ///
    /// # Note
    /// The overlay mode is set to [`OverlayMode::Merge`] and not the image's overlay mode, since
    /// anti-aliasing is funky with the replace overlay mode.
    pub fn push_basic_text(&mut self, font: &'a Font, text: impl AsRef<str>, fill: P) {
        self.push_segment(&TextSegment::new(font, text, fill));
    }

    /// Takes this text layout and returns it with the given basic text added to the text layout.
    /// Useful for method chaining.
    ///
    /// # Note
    /// The overlay mode is set to [`OverlayMode::Merge`] and not the image's overlay mode, since
    /// anti-aliasing is funky with the replace overlay mode.
    ///
    /// # See Also
    /// * [`push_basic_text`][TextLayout::push_basic_text]
    #[must_use]
    pub fn with_basic_text(mut self, font: &'a Font, text: impl AsRef<str>, fill: P) -> Self {
        self.push_basic_text(font, text, fill);
        self
    }

    /// Sets the horizontal anchor of the text. The horizontal anchor determines where the x
    /// position of the text is anchored.
    #[must_use]
    pub const fn with_horizontal_anchor(mut self, anchor: HorizontalAnchor) -> Self {
        self.x_anchor = anchor;
        self
    }

    /// Sets the vertical anchor of the text. The vertical anchor determines where the y position of
    /// the text is anchored.
    #[must_use]
    pub const fn with_vertical_anchor(mut self, anchor: VerticalAnchor) -> Self {
        self.y_anchor = anchor;
        self
    }

    /// Sets the horizontal text alignment. This determines how text is aligned within its bounding
    /// box (which is determined by [`Self::width`] and [`Self::height`]).
    #[must_use]
    pub const fn with_align(mut self, align: TextAlign) -> Self {
        self.align = align;
        self
    }

    /// Sets the horizontal anchor and vertial anchor of the text to be centered. This makes the
    /// position of the text be the center as opposed to the top-left corner.
    #[must_use]
    pub const fn centered(self) -> Self {
        self.with_horizontal_anchor(HorizontalAnchor::Center)
            .with_vertical_anchor(VerticalAnchor::Center)
    }

    fn line_widths(&self) -> (Vec<u32>, u32, u32) {
        let glyphs = self.inner.glyphs();
        if glyphs.is_empty() {
            return (Vec::new(), 0, 0);
        }

        let mut widths = Vec::new();
        let mut max_width = 0;

        // SAFETY: checking glyphs.is_empty() above means that glyphs is not empty.
        for line in unsafe { self.inner.lines().unwrap_unchecked() } {
            let x = self.settings.x as u32;

            let glyph = &glyphs[line.glyph_end];
            let right = glyph.x + glyph.width as f32;
            let line_width = (right - x as f32).ceil() as u32;
            widths.push(line_width);
            max_width = max_width.max(line_width);
        }

        (widths, max_width, self.inner.height() as u32)
    }

    /// Returns the width of the text. This is a slightly expensive operation and is not a simple
    /// getter.
    ///
    /// If you want both width and height, use [`dimensions`][TextLayout::dimensions].
    #[must_use]
    pub fn width(&self) -> u32 {
        let glyphs = self.inner.glyphs();
        if glyphs.is_empty() {
            return 0;
        }

        let mut width = 0;

        // SAFETY: checking glyphs.is_empty() above means that glyphs is not empty.
        for line in unsafe { self.inner.lines().unwrap_unchecked() } {
            let x = self.settings.x as u32;

            for glyph in glyphs[line.glyph_start..=line.glyph_end].iter().rev() {
                if glyph.char_data.is_whitespace() {
                    continue;
                }

                let right = glyph.x + glyph.width as f32;
                let line_width = (right - x as f32).ceil() as u32;
                width = width.max(line_width);

                break;
            }
        }

        width
    }

    /// Returns the height of the text.
    ///
    /// If you want both width and height, use [`dimensions`][TextLayout::dimensions].
    #[must_use]
    pub fn height(&self) -> u32 {
        self.inner.height() as u32
    }

    /// Returns the width and height of the text. This is a slightly expensive operation and should
    /// be used sparingly - it is not a simple getter.
    #[must_use]
    pub fn dimensions(&self) -> (u32, u32) {
        (self.width(), self.height())
    }

    /// Returns the bounding box of the text. Left and top bounds are inclusive; right and bottom
    /// bounds are exclusive.
    #[must_use]
    pub fn bounding_box(&self) -> (u32, u32, u32, u32) {
        let (width, height) = self.dimensions();

        let ox = match self.x_anchor {
            HorizontalAnchor::Left => 0.0,
            HorizontalAnchor::Center => width as f32 / -2.0,
            HorizontalAnchor::Right => -(width as f32),
        };
        let oy = match self.y_anchor {
            VerticalAnchor::Top => 0.0,
            VerticalAnchor::Center => height as f32 / -2.0,
            VerticalAnchor::Bottom => -(height as f32),
        };

        let x = (self.settings.x + ox) as u32;
        let y = (self.settings.y + oy) as u32;

        (x, y, x + width, y + height)
    }

    fn calculate_offsets(&self) -> (Vec<u32>, u32, f32, f32, f32) {
        let (widths, width, height) = self.line_widths();

        let fx = match self.align {
            TextAlign::Left => 0.0,
            TextAlign::Center => 0.5,
            TextAlign::Right => 1.0,
        };
        let ox = match self.x_anchor {
            HorizontalAnchor::Left => 0.0,
            HorizontalAnchor::Center => width as f32 / -2.0,
            HorizontalAnchor::Right => -(width as f32),
        };
        let oy = match self.y_anchor {
            VerticalAnchor::Top => 0.0,
            VerticalAnchor::Center => height as f32 / -2.0,
            VerticalAnchor::Bottom => -(height as f32),
        };

        (widths, width, fx, ox, oy)
    }
}

impl<'a, P: Pixel> Draw<P> for TextLayout<'a, P> {
    fn draw<I: DerefMut<Target = Image<P>>>(&self, mut image: I) {
        let image = &mut *image;

        // Skips the calculation of offsets
        if self.x_anchor == HorizontalAnchor::Left
            && self.y_anchor == VerticalAnchor::Top
            && self.align == TextAlign::Left
        {
            render_layout(image, &self.fonts, &self.inner);
            return;
        }

        let (widths, max_width, fx, ox, oy) = self.calculate_offsets();
        render_layout_with_alignment(
            image,
            &self.fonts,
            &self.inner,
            widths,
            max_width,
            fx,
            ox,
            oy,
        );
    }
}

impl<'a, P: Pixel> Default for TextLayout<'a, P> {
    fn default() -> Self {
        Self::new()
    }
}