138 lines
3.9 KiB
Rust
138 lines
3.9 KiB
Rust
use unicode_script::Script;
|
|
|
|
use super::Font;
|
|
|
|
use self::platform::*;
|
|
|
|
#[cfg(not(any(
|
|
target_os = "linux",
|
|
target_os = "macos",
|
|
target_os = "windows",
|
|
)))]
|
|
#[path = "other.rs"]
|
|
mod platform;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
#[path = "macos.rs"]
|
|
mod platform;
|
|
|
|
#[cfg(target_os = "linux")]
|
|
#[path = "unix.rs"]
|
|
mod platform;
|
|
|
|
#[cfg(target_os = "windows")]
|
|
#[path = "windows.rs"]
|
|
mod platform;
|
|
|
|
pub struct FontFallbackIter<'a> {
|
|
fonts: &'a [Font<'a>],
|
|
default_family_opt: Option<&'a str>,
|
|
scripts: Vec<Script>,
|
|
locale: &'a str,
|
|
script_i: (usize, usize),
|
|
common_i: usize,
|
|
other_i: usize,
|
|
end: bool,
|
|
}
|
|
|
|
impl<'a> FontFallbackIter<'a> {
|
|
pub fn new(fonts: &'a [Font<'a>], default_family_opt: Option<&'a str>, scripts: Vec<Script>, locale: &'a str) -> Self {
|
|
Self {
|
|
fonts,
|
|
default_family_opt,
|
|
scripts,
|
|
locale,
|
|
script_i: (0, 0),
|
|
common_i: 0,
|
|
other_i: 0,
|
|
end: false,
|
|
}
|
|
}
|
|
|
|
pub fn check_missing(&self, word: &str) {
|
|
if self.end {
|
|
log::warn!(
|
|
"Failed to find any fallback for {:?} locale '{}': '{}'",
|
|
self.scripts,
|
|
self.locale,
|
|
word
|
|
);
|
|
} else if self.other_i > 0 {
|
|
let font = &self.fonts[self.other_i - 1];
|
|
log::warn!(
|
|
"Failed to find preset fallback for {:?} locale '{}', used '{}': '{}'",
|
|
self.scripts,
|
|
self.locale,
|
|
font.info.family,
|
|
word
|
|
);
|
|
} else if ! self.scripts.is_empty() && self.common_i > 0 {
|
|
let family = common_fallback()[self.common_i - 1];
|
|
log::debug!(
|
|
"Failed to find script fallback for {:?} locale '{}', used '{}': '{}'",
|
|
self.scripts,
|
|
self.locale,
|
|
family,
|
|
word
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> Iterator for FontFallbackIter<'a> {
|
|
type Item = &'a Font<'a>;
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
if let Some(default_family) = self.default_family_opt.take() {
|
|
for font in self.fonts.iter() {
|
|
if font.info.family == default_family {
|
|
return Some(font);
|
|
}
|
|
}
|
|
}
|
|
|
|
while self.script_i.0 < self.scripts.len() {
|
|
let script = self.scripts[self.script_i.0];
|
|
|
|
let script_families = script_fallback(&script, self.locale);
|
|
while self.script_i.1 < script_families.len() {
|
|
let script_family = script_families[self.script_i.1];
|
|
self.script_i.1 += 1;
|
|
for font in self.fonts.iter() {
|
|
if font.info.family == script_family {
|
|
return Some(font);
|
|
}
|
|
}
|
|
log::warn!("failed to find family '{}' for script {:?} and locale '{}'", script_family, script, self.locale);
|
|
}
|
|
|
|
self.script_i.0 += 1;
|
|
self.script_i.1 = 0;
|
|
}
|
|
|
|
let common_families = common_fallback();
|
|
while self.common_i < common_families.len() {
|
|
let common_family = common_families[self.common_i];
|
|
self.common_i += 1;
|
|
for font in self.fonts.iter() {
|
|
if font.info.family == common_family {
|
|
return Some(font);
|
|
}
|
|
}
|
|
log::warn!("failed to find family '{}'", common_family)
|
|
}
|
|
|
|
//TODO: do we need to do this?
|
|
//TODO: do not evaluate fonts more than once!
|
|
let forbidden_families = forbidden_fallback();
|
|
while self.other_i < self.fonts.len() {
|
|
let font = &self.fonts[self.other_i];
|
|
self.other_i += 1;
|
|
if ! forbidden_families.contains(&font.info.family.as_str()) {
|
|
return Some(font);
|
|
}
|
|
}
|
|
|
|
self.end = true;
|
|
None
|
|
}
|
|
}
|