use crate::encoding::Encoding;
use crate::fontmetrics::FontMetrics;
use std::fmt;
use std::sync::Arc;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct FontRef {
n: usize,
encoding: Encoding,
metrics: Arc<FontMetrics>,
}
impl FontRef {
pub(crate) fn new(
n: usize,
encoding: Encoding,
metrics: Arc<FontMetrics>,
) -> Self {
FontRef {
n,
encoding,
metrics,
}
}
pub fn get_encoding(&self) -> &Encoding {
&self.encoding
}
pub fn get_width(&self, size: f32, text: &str) -> f32 {
size * self.get_width_raw(text) as f32 / 1000.0
}
pub fn get_width_raw(&self, text: &str) -> u32 {
text.chars().fold(0, |result, char| {
result
+ u32::from(
self.encoding
.encode_char(char)
.and_then(|ch| self.metrics.get_width(ch))
.unwrap_or(100),
)
})
}
}
impl fmt::Display for FontRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "/F{}", self.n)
}
}