#[derive(Debug, Clone)]
pub struct FontData {
pub font_name: String,
pub flags: i64,
pub font_bbox: (i64, i64, i64, i64),
pub italic_angle: i64,
pub ascent: i64,
pub descent: i64,
pub cap_height: i64,
pub stem_v: i64,
pub encoding: String,
font: Vec<u8>,
}
impl FontData {
pub fn new(font_file: &[u8], font_name: String) -> Self {
let font = ttf_parser::Face::parse(font_file, 0).expect("Failed to parse font file");
let font_bbox = font.global_bounding_box();
let ascent = font.ascender();
let descent = font.descender();
let cap_height = font.capital_height().unwrap_or(ascent);
let italic_angle = font.italic_angle();
let flags = 1;
let stem_v = (font_bbox.width() as f64 * 0.13).round() as i64;
Self {
font_name,
flags,
font_bbox: (
font_bbox.x_min as i64,
font_bbox.y_min as i64,
font_bbox.x_max as i64,
font_bbox.y_max as i64,
),
italic_angle: italic_angle.round() as i64,
ascent: ascent as i64,
descent: descent as i64,
cap_height: cap_height as i64,
stem_v,
encoding: "WinAnsiEncoding".to_string(), font: font_file.to_vec(),
}
}
pub fn set_flags(&mut self, flags: i64) -> &mut Self {
self.flags = flags;
self
}
pub fn set_font_bbox(&mut self, font_bbox: (i64, i64, i64, i64)) -> &mut Self {
self.font_bbox = font_bbox;
self
}
pub fn set_italic_angle(&mut self, italic_angle: i64) -> &mut Self {
self.italic_angle = italic_angle;
self
}
pub fn set_ascent(&mut self, ascent: i64) -> &mut Self {
self.ascent = ascent;
self
}
pub fn set_descent(&mut self, descent: i64) -> &mut Self {
self.descent = descent;
self
}
pub fn set_cap_height(&mut self, cap_height: i64) -> &mut Self {
self.cap_height = cap_height;
self
}
pub fn set_stem_v(&mut self, stem_v: i64) -> &mut Self {
self.stem_v = stem_v;
self
}
pub fn set_encoding(&mut self, encoding: String) -> &mut Self {
self.encoding = encoding;
self
}
pub fn bytes(&self) -> Vec<u8> {
self.font.clone()
}
}