use std::path::Path;
use crate::error::Error;
use crate::extras::{Shape, ShapePath};
pub struct FontLoader;
impl Default for FontLoader {
fn default() -> Self {
Self::new()
}
}
impl FontLoader {
pub fn new() -> Self {
Self
}
pub fn load(&self, path: impl AsRef<Path>) -> Result<Font, Error> {
let path = path.as_ref();
let text = crate::io::read_to_string(path)?;
let json: serde_json::Value =
serde_json::from_str(&text).map_err(|source| Error::Json {
path: Some(path.to_path_buf()),
source,
})?;
Ok(self.parse(json))
}
pub fn parse(&self, json: serde_json::Value) -> Font {
Font::new(json)
}
}
#[derive(Clone, Debug)]
pub struct Font {
pub data: serde_json::Value,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextDirection {
#[default]
Ltr,
Rtl,
Tb,
}
impl Font {
pub fn new(data: serde_json::Value) -> Self {
Self { data }
}
pub fn generate_shapes(&self, text: &str, size: f64, direction: TextDirection) -> Vec<Shape> {
let mut shapes = Vec::new();
for path in create_paths(text, size, &self.data, direction) {
shapes.extend(path.to_shapes());
}
shapes
}
}
fn num(value: &serde_json::Value) -> f64 {
value.as_f64().unwrap_or(f64::NAN)
}
fn create_paths(
text: &str,
size: f64,
data: &serde_json::Value,
direction: TextDirection,
) -> Vec<ShapePath> {
let mut chars: Vec<char> = text.chars().collect();
let scale = size / num(&data["resolution"]);
let line_height = (num(&data["boundingBox"]["yMax"]) - num(&data["boundingBox"]["yMin"])
+ num(&data["underlineThickness"]))
* scale;
let mut paths = Vec::new();
let mut offset_x = 0.0;
let mut offset_y = 0.0;
if direction == TextDirection::Rtl || direction == TextDirection::Tb {
chars.reverse();
}
for ch in chars {
if ch == '\n' {
offset_x = 0.0;
offset_y -= line_height;
} else {
let Some((advance, path)) = create_path(ch, scale, offset_x, offset_y, data) else {
continue;
};
if direction == TextDirection::Tb {
offset_x = 0.0;
offset_y += num(&data["ascender"]) * scale;
} else {
offset_x += advance;
}
paths.push(path);
}
}
paths
}
fn create_path(
ch: char,
scale: f64,
offset_x: f64,
offset_y: f64,
data: &serde_json::Value,
) -> Option<(f64, ShapePath)> {
let glyphs = &data["glyphs"];
let mut key = [0u8; 4];
let glyph = glyphs
.get(&*ch.encode_utf8(&mut key))
.or_else(|| glyphs.get("?"))?;
let mut path = ShapePath::new();
if let Some(o) = glyph.get("o").and_then(|o| o.as_str()) {
if !o.is_empty() {
let outline: Vec<&str> = o.split(' ').collect();
let mut i = 0;
let next = |i: &mut usize, offset: f64| {
let v = outline
.get(*i)
.and_then(|s| s.parse::<f64>().ok())
.unwrap_or(f64::NAN);
*i += 1;
v * scale + offset
};
while i < outline.len() {
let action = outline[i];
i += 1;
match action {
"m" => {
let x = next(&mut i, offset_x);
let y = next(&mut i, offset_y);
path.move_to(x, y);
}
"l" => {
let x = next(&mut i, offset_x);
let y = next(&mut i, offset_y);
path.line_to(x, y);
}
"q" => {
let cpx = next(&mut i, offset_x);
let cpy = next(&mut i, offset_y);
let cpx1 = next(&mut i, offset_x);
let cpy1 = next(&mut i, offset_y);
path.quadratic_curve_to(cpx1, cpy1, cpx, cpy);
}
"b" => {
let cpx = next(&mut i, offset_x);
let cpy = next(&mut i, offset_y);
let cpx1 = next(&mut i, offset_x);
let cpy1 = next(&mut i, offset_y);
let cpx2 = next(&mut i, offset_x);
let cpy2 = next(&mut i, offset_y);
path.bezier_curve_to(cpx1, cpy1, cpx2, cpy2, cpx, cpy);
}
_ => {}
}
}
}
}
Some((num(&glyph["ha"]) * scale, path))
}