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
use std::{ffi::CString, ptr::NonNull};

use crate::{
    color::Rgba,
    surface::{RawSurface, Surface},
    Result, Sdl, SdlError,
};

use super::Font;
use crate::bind;

pub mod pen;

/// A render mode for the font glyphs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RenderMode {
    /// A quick and dirty mode.
    Solid {
        /// A text color.
        foreground: Rgba,
    },
    /// A slow and nice, but with a solid box mode.
    Shaded {
        /// A text color.
        foreground: Rgba,
        /// A background color, around of a text.
        background: Rgba,
    },
    /// A too slow, but ultra nice over others.
    Blended {
        /// A text color.
        foreground: Rgba,
    },
}

/// An extension for rendering the text.
pub trait RenderExt {
    /// Renders the text into the [`TtfSurface`], or `Err` on failure.
    fn render(&self, text: &str, mode: RenderMode) -> Result<TtfSurface>;
    /// Renders the wrapped text into the [`TtfSurface`], or `Err` on failure.
    fn render_wrapped(&self, text: &str, wrap_length: u32, mode: RenderMode) -> Result<TtfSurface>;
    /// Renders the character into the [`TtfSurface`], or `Err` on failure.
    fn render_glyph(&self, ch: char, mode: RenderMode) -> Result<TtfSurface>;
}

impl RenderExt for Font<'_> {
    fn render(&self, text: &str, mode: RenderMode) -> Result<TtfSurface> {
        let cstr = CString::new(text).unwrap_or_default();
        let raw_color = |color: Rgba| bind::SDL_Color {
            r: color.r,
            g: color.g,
            b: color.b,
            a: color.a,
        };
        let ptr = match mode {
            RenderMode::Solid { foreground } => unsafe {
                bind::TTF_RenderUTF8_Solid(self.ptr.as_ptr(), cstr.as_ptr(), raw_color(foreground))
            },
            RenderMode::Shaded {
                foreground,
                background,
            } => unsafe {
                bind::TTF_RenderUTF8_Shaded(
                    self.ptr.as_ptr(),
                    cstr.as_ptr(),
                    raw_color(foreground),
                    raw_color(background),
                )
            },
            RenderMode::Blended { foreground } => unsafe {
                bind::TTF_RenderUTF8_Blended(
                    self.ptr.as_ptr(),
                    cstr.as_ptr(),
                    raw_color(foreground),
                )
            },
        };
        if ptr.is_null() {
            Err(SdlError::Others { msg: Sdl::error() })
        } else {
            Ok(TtfSurface {
                ptr: NonNull::new(ptr.cast()).unwrap(),
            })
        }
    }

    fn render_wrapped(&self, text: &str, wrap_length: u32, mode: RenderMode) -> Result<TtfSurface> {
        let cstr = CString::new(text).unwrap_or_default();
        let raw_color = |color: Rgba| bind::SDL_Color {
            r: color.r,
            g: color.g,
            b: color.b,
            a: color.a,
        };
        let ptr = match mode {
            RenderMode::Solid { foreground } => unsafe {
                bind::TTF_RenderUTF8_Solid_Wrapped(
                    self.ptr.as_ptr(),
                    cstr.as_ptr(),
                    raw_color(foreground),
                    wrap_length,
                )
            },
            RenderMode::Shaded {
                foreground,
                background,
            } => unsafe {
                bind::TTF_RenderUTF8_Shaded_Wrapped(
                    self.ptr.as_ptr(),
                    cstr.as_ptr(),
                    raw_color(foreground),
                    raw_color(background),
                    wrap_length,
                )
            },
            RenderMode::Blended { foreground } => unsafe {
                bind::TTF_RenderUTF8_Blended_Wrapped(
                    self.ptr.as_ptr(),
                    cstr.as_ptr(),
                    raw_color(foreground),
                    wrap_length,
                )
            },
        };
        if ptr.is_null() {
            Err(SdlError::Others { msg: Sdl::error() })
        } else {
            Ok(TtfSurface {
                ptr: NonNull::new(ptr.cast()).unwrap(),
            })
        }
    }

    fn render_glyph(&self, ch: char, mode: RenderMode) -> Result<TtfSurface> {
        let raw_color = |color: Rgba| bind::SDL_Color {
            r: color.r,
            g: color.g,
            b: color.b,
            a: color.a,
        };
        let ptr = match mode {
            RenderMode::Solid { foreground } => unsafe {
                bind::TTF_RenderGlyph32_Solid(self.ptr.as_ptr(), ch as u32, raw_color(foreground))
            },
            RenderMode::Shaded {
                foreground,
                background,
            } => unsafe {
                bind::TTF_RenderGlyph32_Shaded(
                    self.ptr.as_ptr(),
                    ch as u32,
                    raw_color(foreground),
                    raw_color(background),
                )
            },
            RenderMode::Blended { foreground } => unsafe {
                bind::TTF_RenderGlyph32_Blended(self.ptr.as_ptr(), ch as u32, raw_color(foreground))
            },
        };
        if ptr.is_null() {
            Err(SdlError::Others { msg: Sdl::error() })
        } else {
            Ok(TtfSurface {
                ptr: NonNull::new(ptr.cast()).unwrap(),
            })
        }
    }
}

/// A surface of TrueType Font.
pub struct TtfSurface {
    ptr: NonNull<RawSurface>,
}

impl Surface for TtfSurface {
    fn as_ptr(&self) -> NonNull<RawSurface> {
        self.ptr
    }
}

impl Drop for TtfSurface {
    fn drop(&mut self) {
        unsafe { bind::SDL_FreeSurface(self.ptr.as_ptr().cast()) }
    }
}