|
|
@ -46,36 +46,45 @@ pub struct FontLoader {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
|
|
|
|
pub enum FontKey {
|
|
|
|
pub struct FontKey {
|
|
|
|
|
|
|
|
// TODO(smolck): Could make these private and add constructor method(s)?
|
|
|
|
|
|
|
|
// Would theoretically make things safer I guess, but not sure . . .
|
|
|
|
|
|
|
|
pub bold: bool,
|
|
|
|
|
|
|
|
pub italic: bool,
|
|
|
|
|
|
|
|
pub font_selection: FontSelection,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
|
|
|
|
|
|
|
|
pub enum FontSelection {
|
|
|
|
Name(String),
|
|
|
|
Name(String),
|
|
|
|
Character(char),
|
|
|
|
Character(char),
|
|
|
|
Default,
|
|
|
|
Default,
|
|
|
|
LastResort,
|
|
|
|
LastResort,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<&str> for FontKey {
|
|
|
|
impl From<&str> for FontSelection {
|
|
|
|
fn from(string: &str) -> FontKey {
|
|
|
|
fn from(string: &str) -> FontSelection {
|
|
|
|
let string = string.to_string();
|
|
|
|
let string = string.to_string();
|
|
|
|
FontKey::Name(string)
|
|
|
|
FontSelection::Name(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<&String> for FontKey {
|
|
|
|
impl From<&String> for FontSelection {
|
|
|
|
fn from(string: &String) -> FontKey {
|
|
|
|
fn from(string: &String) -> FontSelection {
|
|
|
|
let string = string.to_owned();
|
|
|
|
let string = string.to_owned();
|
|
|
|
FontKey::Name(string)
|
|
|
|
FontSelection::Name(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<String> for FontKey {
|
|
|
|
impl From<String> for FontSelection {
|
|
|
|
fn from(string: String) -> FontKey {
|
|
|
|
fn from(string: String) -> FontSelection {
|
|
|
|
FontKey::Name(string)
|
|
|
|
FontSelection::Name(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<char> for FontKey {
|
|
|
|
impl From<char> for FontSelection {
|
|
|
|
fn from(character: char) -> FontKey {
|
|
|
|
fn from(character: char) -> FontSelection {
|
|
|
|
FontKey::Character(character)
|
|
|
|
FontSelection::Character(character)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -89,14 +98,19 @@ impl FontLoader {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn load(&mut self, font_key: FontKey) -> Option<FontPair> {
|
|
|
|
fn load(&mut self, font_key: FontKey) -> Option<FontPair> {
|
|
|
|
match font_key {
|
|
|
|
let font_style = match (font_key.bold, font_key.italic) {
|
|
|
|
FontKey::Name(name) => {
|
|
|
|
(true, true) => FontStyle::bold_italic(),
|
|
|
|
let font_style = FontStyle::normal();
|
|
|
|
(false, true) => FontStyle::italic(),
|
|
|
|
|
|
|
|
(true, false) => FontStyle::bold(),
|
|
|
|
|
|
|
|
(false, false) => FontStyle::normal(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match font_key.font_selection {
|
|
|
|
|
|
|
|
FontSelection::Name(name) => {
|
|
|
|
let typeface = self.font_mgr.match_family_style(name, font_style)?;
|
|
|
|
let typeface = self.font_mgr.match_family_style(name, font_style)?;
|
|
|
|
FontPair::new(Font::from_typeface(typeface, self.font_size))
|
|
|
|
FontPair::new(Font::from_typeface(typeface, self.font_size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FontKey::Character(character) => {
|
|
|
|
FontSelection::Character(character) => {
|
|
|
|
let font_style = FontStyle::normal();
|
|
|
|
|
|
|
|
let typeface = self.font_mgr.match_family_style_character(
|
|
|
|
let typeface = self.font_mgr.match_family_style_character(
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
font_style,
|
|
|
|
font_style,
|
|
|
@ -105,13 +119,13 @@ impl FontLoader {
|
|
|
|
)?;
|
|
|
|
)?;
|
|
|
|
FontPair::new(Font::from_typeface(typeface, self.font_size))
|
|
|
|
FontPair::new(Font::from_typeface(typeface, self.font_size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FontKey::Default => {
|
|
|
|
FontSelection::Default => {
|
|
|
|
let default_font_data = Asset::get(DEFAULT_FONT).unwrap();
|
|
|
|
let default_font_data = Asset::get(DEFAULT_FONT).unwrap();
|
|
|
|
let data = Data::new_copy(&default_font_data);
|
|
|
|
let data = Data::new_copy(&default_font_data);
|
|
|
|
let typeface = Typeface::from_data(data, 0).unwrap();
|
|
|
|
let typeface = Typeface::from_data(data, 0).unwrap();
|
|
|
|
FontPair::new(Font::from_typeface(typeface, self.font_size))
|
|
|
|
FontPair::new(Font::from_typeface(typeface, self.font_size))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FontKey::LastResort => {
|
|
|
|
FontSelection::LastResort => {
|
|
|
|
let default_font_data = Asset::get(LAST_RESORT_FONT).unwrap();
|
|
|
|
let default_font_data = Asset::get(LAST_RESORT_FONT).unwrap();
|
|
|
|
let data = Data::new_copy(&default_font_data);
|
|
|
|
let data = Data::new_copy(&default_font_data);
|
|
|
|
let typeface = Typeface::from_data(data, 0).unwrap();
|
|
|
|
let typeface = Typeface::from_data(data, 0).unwrap();
|
|
|
|