libcosmic-yoda/examples/text/src/font/system.rs

64 lines
2 KiB
Rust
Raw Normal View History

2022-10-05 09:16:51 -06:00
use super::{Font, FontMatches};
pub struct FontSystem<'a> {
fonts: Vec<Font<'a>>,
}
impl<'a> FontSystem<'a> {
pub fn new() -> Self {
Self {
fonts: Vec::new(),
}
}
pub fn add(&mut self, font: Font<'a>) {
self.fonts.push(font);
}
pub fn matches(&'a self, patterns: &[&str]) -> Option<FontMatches<'a>> {
let mut fonts = Vec::new();
for font in self.fonts.iter() {
for rec in font.rustybuzz.names() {
if rec.name_id == 4 && rec.is_unicode() {
let mut words: Vec<u16> = Vec::new();
let mut i = 0;
while i + 1 < rec.name.len() {
words.push(
(rec.name[i + 1] as u16) |
((rec.name[i] as u16) << 8)
);
i += 2;
}
match String::from_utf16(&words) {
Ok(name) => {
let mut matched = false;
for pattern in patterns.iter() {
println!("Matching font name '{}' with pattern '{}'", name, pattern);
if name.contains(pattern) {
matched = true;
}
}
if matched {
println!("Matched font name '{}'", name);
fonts.push(font);
} else {
println!("Did not match font name '{}'", name);
}
},
Err(_) => ()
}
}
}
}
if ! fonts.is_empty() {
2022-10-07 09:41:36 -06:00
Some(FontMatches {
fonts
})
2022-10-05 09:16:51 -06:00
} else {
None
}
}
}