use std::fmt;
#[derive(Debug, Clone, Copy)]
pub enum FontSize {
Pt(f32),
Px(f32),
}
impl Default for FontSize {
fn default() -> Self {
FontSize::Pt(12.0)
}
}
impl FontSize {
pub fn to_pt(&self) -> f32 {
match self {
FontSize::Pt(size) => size.clone(),
FontSize::Px(size) => (size * 72.0) / 96.0,
}
}
pub fn to_px(&self) -> f32 {
match self {
FontSize::Pt(size) => (size * 96.0) / 72.0,
FontSize::Px(size) => size.clone(),
}
}
#[inline]
pub fn scale(&self, scale: f32) -> ScaledFontSize {
ScaledFontSize::new(self.clone(), scale)
}
}
#[derive(Debug, Clone, Copy)]
pub struct ScaledFontSize {
size: FontSize,
scale: f32,
}
impl ScaledFontSize {
pub fn new(size: FontSize, scale: f32) -> Self {
Self {
size,
scale,
}
}
#[inline]
pub fn size(&self) -> FontSize { self.size }
#[inline]
pub fn scale(&self) -> f32 { self.scale }
#[inline]
pub fn to_px(&self) -> f32 { self.size.to_px() * self.scale }
}
pub trait FontSizer: fmt::Debug + Send + Sync + 'static {
fn height(&self, size: ScaledFontSize) -> f32;
fn width(&self, size: ScaledFontSize) -> f32;
fn line_gap(&self, size: ScaledFontSize) -> f32;
#[inline]
fn h_advance(&self, size: ScaledFontSize, _c: char) -> f32 { self.width(size) }
#[inline]
fn kern(&self, _size: ScaledFontSize, _first: char, _second: char) -> f32 { 0.0 }
#[inline]
fn v_advance(&self, size: ScaledFontSize) -> f32 { self.height(size) + self.line_gap(size) }
}