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
// SPDX-License-Identifier: MIT

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use num_traits::float::FloatCore;

#[cfg(all(feature = "std", not(target_os = "redox")))]
pub use font_loader::{
    self,
    system_fonts::{self, FontProperty, FontPropertyBuilder},
};

#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use orbclient::{Color, Renderer};

#[derive(Clone)]
pub struct Font {
    inner: rusttype::Font<'static>
}

impl Font {
    /// Find a font from an optional type, family, and style, such as "Mono", "Fira", "Regular"
    #[cfg(target_os = "redox")]
    pub fn find(typeface: Option<&str>, family: Option<&str>, style: Option<&str>) -> Result<Font, String> {
        Font::from_path(&format!("/ui/fonts/{}/{}/{}.ttf", typeface.unwrap_or("Mono"), family.unwrap_or("Fira"), style.unwrap_or("Regular")))
    }

    // A funciton to automate the process of building a font property  from "typeface, family, style"
    #[cfg(all(feature = "std", not(target_os = "redox")))]
    fn build_fontproperty (typeface: Option<&str>, family: Option<&str>, style: Option<&str>) -> FontProperty {
        let mut font = FontPropertyBuilder::new();
        if let Some(style) = style {
            let style_caps = &style.to_uppercase();
            let italic = style_caps.contains("ITALIC");
            let oblique = style_caps.contains("OBLIQUE");
            let bold = style_caps.contains("BOLD");
            if italic {
                font = font.italic();
            }
            if oblique {
                font = font.oblique();
            }
            if bold {
                font = font.bold();
            }
        }
        if let Some(typeface) = typeface {
            // FontProperty has no support for differentiating Sans and Serif.
            let typeface_caps = &typeface.to_uppercase();
            if typeface_caps.contains("MONO") {
                font = font.monospace();
            }
        }
        if let Some(family) = family {
            if let Some(typeface) = typeface {
                let typeface_caps = &typeface.to_uppercase();
                // manually adding Serif and Sans
                if typeface_caps.contains("SERIF") {
                    font = font.family(&[family, "Serif"].concat());
                } else if typeface_caps.contains("SANS") {
                    font = font.family(&[family, "Sans"].concat());
                }
            } else {
                font = font.family(family);
            }
        }
        font.build()
    }

    #[cfg(all(feature = "std", not(target_os = "redox")))]
    pub fn find(typeface: Option<&str>, family: Option<&str>, style: Option<&str>) -> Result<Font, String> {
        // This funciton attempts to use the rust-font-loader library, a frontend
        // to the ubiquitous C library fontconfig, to find and load the specified
        // font.
        let mut font = Font::build_fontproperty(typeface, family, style);
        // font_loader::query specific returns an empty vector if there are no matches
        // and does not tag the result with associated data like "italic", merely returns
        // the name of the font if it exists.
        let fonts = system_fonts::query_specific(&mut font); // Returns an empty vector if there are no matches.
        // Confirm that a font matched:
        if !fonts.is_empty() {
            // get the matched font straight from the data:
            let font_data = system_fonts::get(&font); // Getting font data from properties
            match font_data {
                Some((data, _)) => {
                    if let Some(font) = rusttype::Font::try_from_vec(data) {
                        Ok(Font { inner: font })
                    } else {
                        Err("error constructing a Font from bytes".to_string())
                    }
                }
                None => Err(format!("Could not get font {} from data", &fonts[0]))
            }
        } else {
            // If no font matched, try again with no family, as concatenating "Sans" or "Serif" may rule out legitimate fonts
            let mut font = Font::build_fontproperty(None, family, style);
            let fonts = system_fonts::query_specific(&mut font);
            if !fonts.is_empty() {
                let font_data = system_fonts::get(&font);
                match font_data {
                    Some((data, _)) => {
                        if let Some(font) = rusttype::Font::try_from_vec(data) {
                            Ok(Font { inner: font })
                        } else {
                            Err("error constructing a Font from bytes".to_string())
                        }
                    }
                    None => Err(format!("Could not get font {} from data", &fonts[0]))
                }
            }  else {
                // If no font matched, try to load the default font manually
                Font::from_path("/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf")
            }
        }
    }

    /// Load a font from file path
    #[cfg(feature = "std")]
    pub fn from_path<P: AsRef<std::path::Path>>(path: P) -> Result<Font, String> {
        let data = std::fs::read(path).map_err(|err| format!("failed to read font: {}", err))?;
        if let Some(font) = rusttype::Font::try_from_vec(data) {
            Ok(Font { inner: font })
        } else {
            Err("error constructing a Font from bytes".to_string())
        }
    }

    /// Load a font from a slice
    pub fn from_data(data: &'static [u8]) -> Result<Font, String> {
        if let Some(font) = rusttype::Font::try_from_bytes(data) {
            Ok(Font { inner: font })
        } else {
            Err("error constructing a Font from bytes".to_string())
        }
    }

    /// Render provided text using the font
    pub fn render<'a>(&'a self, text: &str, height: f32) -> Text<'a> {
        let scale = rusttype::Scale::uniform(height);

        // The origin of a line of text is at the baseline (roughly where non-descending letters sit).
        // We don't want to clip the text, so we shift it down with an offset when laying it out.
        // v_metrics.ascent is the distance between the baseline and the highest edge of any glyph in
        // the font. That's enough to guarantee that there's no clipping.
        let v_metrics = self.inner.v_metrics(scale);
        let offset = rusttype::point(0.0, v_metrics.ascent);

        // Glyphs to draw for "RustType". Feel free to try other strings.
        let glyphs: Vec<rusttype::PositionedGlyph> = self.inner.layout(text, scale, offset).collect();

        // Find the most visually pleasing width to display
        let width = glyphs.iter().rev()
            .find_map(|g| g.pixel_bounding_box()
                        .map(|b| b.min.x as f32 + g.unpositioned().h_metrics().advance_width))
            .unwrap_or(0.0);

        Text {
            w: width.ceil() as u32,
            h: height.ceil() as u32,
            glyphs
        }
    }
}

pub struct Text<'a> {
    w: u32,
    h: u32,
    glyphs: Vec<rusttype::PositionedGlyph<'a>>
}

impl<'a> Text<'a> {
    /// Return width of the text
    pub fn width(&self) -> u32 {
        self.w
    }

    /// Return height of the text
    pub fn height(&self) -> u32 {
        self.h
    }

    /// Draw the text onto a window and clipp the text to the given bounds
    pub fn draw_clipped<R: Renderer + ?Sized>(&self, renderer: &mut R, x: i32, y: i32, bounds_x: i32, bounds_width: u32, color: Color) {
        for g in &self.glyphs {
            if let Some(bb) = g.pixel_bounding_box() {
                g.draw(|off_x, off_y, v| {
                    let off_x = off_x as i32 + bb.min.x;
                    let off_y = off_y as i32 + bb.min.y;
                    // There's still a possibility that the glyph clips the boundaries of the bitmap
                    if off_x >= 0 && off_x < self.w as i32 && off_y >= 0 && off_y < self.h as i32
                    && x + off_x >= bounds_x && x + off_x <= bounds_x + bounds_width as i32 {
                        let c = (v * 255.0) as u32;
                        renderer.pixel(x + off_x, y + off_y, Color{
                            data: c << 24 | (color.data & 0x00FF_FFFF)
                        });
                    }
                });
            }
        }
    }

    /// Draw the text onto a window
    pub fn draw<R: Renderer + ?Sized>(&self, renderer: &mut R, x: i32, y: i32, color: Color) {
       self.draw_clipped(renderer, x, y, x, self.w, color);
    }
}