aboutsummaryrefslogtreecommitdiff
path: root/src/style/font/naive.rs
blob: 99530401baac85a4edf1df96d4bd104225d6db85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use super::{FontData, FontFamily, FontStyle, LayoutBox};

#[derive(Debug, Clone)]
pub struct FontError;

impl std::fmt::Display for FontError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(fmt, "General Error")?;
        Ok(())
    }
}

impl std::error::Error for FontError {}

#[derive(Clone)]
pub struct FontDataInternal(String, String);

impl FontData for FontDataInternal {
    type ErrorType = FontError;
    fn new(family: FontFamily, style: FontStyle) -> Result<Self, FontError> {
        Ok(FontDataInternal(
            family.as_str().into(),
            style.as_str().into(),
        ))
    }

    /// Note: This is only a crude estimatation, since for some backend such as SVG, we have no way to
    /// know the real size of the text anyway. Thus using font-kit is an overkill and doesn't helps
    /// the layout.
    fn estimate_layout(&self, size: f64, text: &str) -> Result<LayoutBox, Self::ErrorType> {
        let em = size / 1.24 / 1.24;
        Ok((
            (0, -em.round() as i32),
            (
                (em * 0.7 * text.len() as f64).round() as i32,
                (em * 0.24).round() as i32,
            ),
        ))
    }
}