1use super::{FontData, FontFamily, FontStyle, LayoutBox};
23#[derive(Debug, Clone)]
4pub struct FontError;
56impl std::fmt::Display for FontError {
7fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
8write!(fmt, "General Error")?;
9Ok(())
10 }
11}
1213impl std::error::Error for FontError {}
1415#[derive(Clone)]
16pub struct FontDataInternal(String, String);
1718impl FontData for FontDataInternal {
19type ErrorType = FontError;
20fn new(family: FontFamily, style: FontStyle) -> Result<Self, FontError> {
21Ok(FontDataInternal(
22 family.as_str().into(),
23 style.as_str().into(),
24 ))
25 }
2627/// 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.
30fn estimate_layout(&self, size: f64, text: &str) -> Result<LayoutBox, Self::ErrorType> {
31let em = size / 1.24 / 1.24;
32Ok((
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}