1 use super::{FontData, FontFamily, FontStyle, LayoutBox}; 2 3 #[derive(Debug, Clone)] 4 pub struct FontError; 5 6 impl std::fmt::Display for FontError { fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error>7 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { 8 write!(fmt, "General Error")?; 9 Ok(()) 10 } 11 } 12 13 impl std::error::Error for FontError {} 14 15 #[derive(Clone)] 16 pub struct FontDataInternal(String, String); 17 18 impl FontData for FontDataInternal { 19 type ErrorType = FontError; new(family: FontFamily, style: FontStyle) -> Result<Self, FontError>20 fn new(family: FontFamily, style: FontStyle) -> Result<Self, FontError> { 21 Ok(FontDataInternal( 22 family.as_str().into(), 23 style.as_str().into(), 24 )) 25 } 26 27 /// Note: This is only a crude estimatation, since for some backend such as SVG, we have no way to 28 /// know the real size of the text anyway. Thus using font-kit is an overkill and doesn't helps 29 /// the layout. estimate_layout(&self, size: f64, text: &str) -> Result<LayoutBox, Self::ErrorType>30 fn estimate_layout(&self, size: f64, text: &str) -> Result<LayoutBox, Self::ErrorType> { 31 let em = size / 1.24 / 1.24; 32 Ok(( 33 (0, -em.round() as i32), 34 ( 35 (em * 0.7 * text.len() as f64).round() as i32, 36 (em * 0.24).round() as i32, 37 ), 38 )) 39 } 40 } 41