use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub struct GlyphPointToken {
pub x: f64,
pub y: f64,
}
impl GlyphPointToken {
#[must_use]
pub const fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
impl From<(f64, f64)> for GlyphPointToken {
fn from((x, y): (f64, f64)) -> Self {
Self { x, y }
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct GlyphAGoldenMaster {
pub glyphname: String,
#[serde(alias = "units_per_em", alias = "unitsPerEm", alias = "UPM")]
pub upm: u16,
#[serde(alias = "width", alias = "advance_width", alias = "advanceWidth")]
pub width: f64,
pub ascender: f64,
#[serde(alias = "cap_height", alias = "capHeight")]
pub cap_height: f64,
#[serde(alias = "x_height", alias = "xHeight")]
pub x_height: f64,
pub descender: f64,
#[serde(alias = "outer_contour", alias = "outerContour")]
pub outer_contour: Vec<GlyphPointToken>,
#[serde(alias = "inner_counter", alias = "innerCounter")]
pub inner_counter: Vec<GlyphPointToken>,
}
impl GlyphAGoldenMaster {
#[must_use]
pub fn load() -> Self {
let json_str = include_str!("../../tokens/glyph_a_golden_master.json");
serde_json::from_str(json_str).unwrap_or_default()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GlyphArchetype {
A,
N,
O,
S,
}
impl GlyphArchetype {
#[inline]
#[must_use]
pub const fn all() -> [Self; 4] {
[Self::A, Self::N, Self::O, Self::S]
}
#[inline]
#[must_use]
pub const fn button_label(self) -> &'static str {
match self {
Self::A => "/A",
Self::N => "/n",
Self::O => "/o",
Self::S => "/s",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ArchetypeNodePreset {
pub x: f64,
pub y: f64,
pub node_type: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ArchetypeContourPreset {
pub closed: bool,
pub nodes: Vec<ArchetypeNodePreset>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct ArchetypeGlyphPreset {
pub glyph_name: String,
pub advance_width: f64,
pub upm: f64,
pub ascender: f64,
pub cap_height: f64,
pub x_height: f64,
pub baseline: f64,
pub descender: f64,
pub contours: Vec<ArchetypeContourPreset>,
}
#[derive(Debug, Clone, Default, Deserialize)]
struct CurvedArchetypesCatalog {
n: ArchetypeGlyphPreset,
o: ArchetypeGlyphPreset,
s: ArchetypeGlyphPreset,
}
#[must_use]
pub fn load_archetype_preset(archetype: GlyphArchetype) -> ArchetypeGlyphPreset {
match archetype {
GlyphArchetype::A => {
let m = GlyphAGoldenMaster::load();
ArchetypeGlyphPreset {
glyph_name: m.glyphname,
advance_width: m.width,
upm: f64::from(m.upm),
ascender: m.ascender,
cap_height: m.cap_height,
x_height: m.x_height,
baseline: 0.0,
descender: m.descender,
contours: vec![
ArchetypeContourPreset {
closed: true,
nodes: m
.outer_contour
.into_iter()
.map(|pt| ArchetypeNodePreset {
x: pt.x,
y: pt.y,
node_type: "corner".to_string(),
})
.collect(),
},
ArchetypeContourPreset {
closed: true,
nodes: m
.inner_counter
.into_iter()
.map(|pt| ArchetypeNodePreset {
x: pt.x,
y: pt.y,
node_type: "corner".to_string(),
})
.collect(),
},
],
}
}
other => {
let raw = include_str!("../../tokens/glyph_archetypes_golden.json");
let catalog: CurvedArchetypesCatalog = serde_json::from_str(raw).unwrap_or_default();
match other {
GlyphArchetype::N => catalog.n,
GlyphArchetype::O => catalog.o,
GlyphArchetype::S | GlyphArchetype::A => catalog.s,
}
}
}
}