use std::collections::HashMap;
#[derive(Debug, Clone, Copy)]
pub struct TypographyOptions {
pub ligatures: bool,
pub kerning: bool,
}
impl Default for TypographyOptions {
fn default() -> Self {
Self {
ligatures: true,
kerning: true,
}
}
}
pub type KerningValue = i16;
#[derive(Debug, Clone, Default)]
pub struct KerningTable {
map: HashMap<(char, char), KerningValue>,
}
impl KerningTable {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, left: char, right: char, value: KerningValue) {
self.map.insert((left, right), value);
}
pub fn get(&self, left: char, right: char) -> Option<KerningValue> {
self.map.get(&(left, right)).copied()
}
pub fn dejavu_sans() -> Self {
let mut t = Self::new();
t.insert('A', 'V', -50); t.insert('V', 'A', -50);
t.insert('A', 'W', -40); t.insert('W', 'A', -40);
t.insert('A', 'Y', -50); t.insert('Y', 'A', -50);
t.insert('L', 'T', -40);
t.insert('T', 'o', -30); t.insert('T', 'a', -30);
t.insert('T', 'e', -30); t.insert('T', 'r', -30);
t.insert('V', 'o', -30); t.insert('V', 'a', -30);
t.insert('W', 'o', -20); t.insert('W', 'a', -20);
t.insert('Y', 'o', -40); t.insert('Y', 'a', -40);
t.insert('P', 'a', -20); t.insert('P', 'e', -20);
t.insert('F', 'a', -30); t.insert('F', 'e', -30);
t.insert('L', 'V', -30); t.insert('L', 'W', -20);
t.insert('D', 'A', -20);
t.insert('O', 'X', -20);
t.insert('C', 'A', -10);
t.insert('K', 'o', -20); t.insert('K', 'e', -20);
t.insert('X', 'o', -20); t.insert('X', 'a', -20);
t.insert('r', 't', -10);
t.insert('a', 'v', -10);
t.insert('e', 'x', -10);
t.insert('L', ' ', -20);
t.insert('T', ' ', -15);
t.insert(' ', 'T', -10);
t.insert(' ', 'V', -10);
t.insert('f', 'f', -30);
t.insert('f', 'i', -30);
t.insert('f', 'l', -30);
t
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextSegment {
pub text: String,
pub adjustment: KerningValue,
}
#[derive(Debug, Clone)]
pub struct TypographyEngine {
pub options: TypographyOptions,
pub kerning: KerningTable,
}
impl Default for TypographyEngine {
fn default() -> Self {
Self {
options: TypographyOptions::default(),
kerning: KerningTable::dejavu_sans(),
}
}
}
impl TypographyEngine {
pub fn new(options: TypographyOptions) -> Self {
Self {
options,
kerning: KerningTable::dejavu_sans(),
}
}
pub fn apply_ligatures(text: &str) -> String {
let mut s = text.to_string();
s = s.replace("ffi", "\u{FB03}");
s = s.replace("ffl", "\u{FB04}");
s = s.replace("ff", "\u{FB00}");
s = s.replace("fi", "\u{FB01}");
s = s.replace("fl", "\u{FB02}");
s
}
pub fn segment(&self, text: &str) -> Vec<TextSegment> {
if text.is_empty() {
return Vec::new();
}
if !self.options.kerning {
return vec![TextSegment {
text: text.to_string(),
adjustment: 0,
}];
}
let mut segments = Vec::new();
let mut current = String::new();
let chars: Vec<char> = text.chars().collect();
for i in 0..chars.len() {
current.push(chars[i]);
if i + 1 < chars.len()
&& let Some(adj) = self.kerning.get(chars[i], chars[i + 1])
{
segments.push(TextSegment {
text: current,
adjustment: adj,
});
current = String::new();
}
}
if !current.is_empty() {
segments.push(TextSegment {
text: current,
adjustment: 0,
});
}
segments
}
pub fn process(&self, text: &str) -> Vec<TextSegment> {
let text = if self.options.ligatures {
Self::apply_ligatures(text)
} else {
text.to_string()
};
self.segment(&text)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_apply_ligatures() {
assert_eq!(
TypographyEngine::apply_ligatures("fi fl ff ffi ffl"),
"\u{FB01} \u{FB02} \u{FB00} \u{FB03} \u{FB04}"
);
}
#[test]
fn test_apply_ligatures_office() {
assert_eq!(
TypographyEngine::apply_ligatures("office"),
"o\u{FB03}ce"
);
}
#[test]
fn test_kerning_table() {
let table = KerningTable::dejavu_sans();
assert_eq!(table.get('A', 'V'), Some(-50));
assert_eq!(table.get('X', 'Y'), None);
}
#[test]
fn test_segment_no_kerning() {
let engine = TypographyEngine::new(TypographyOptions {
ligatures: false,
kerning: false,
});
let segs = engine.segment("hello");
assert_eq!(segs.len(), 1);
assert_eq!(segs[0].text, "hello");
assert_eq!(segs[0].adjustment, 0);
}
#[test]
fn test_segment_with_kerning() {
let engine = TypographyEngine::default();
let segs = engine.segment("AV");
assert_eq!(segs.len(), 2);
assert_eq!(segs[0].text, "A");
assert_eq!(segs[0].adjustment, -50);
assert_eq!(segs[1].text, "V");
assert_eq!(segs[1].adjustment, 0);
}
#[test]
fn test_process_ligatures_and_kerning() {
let engine = TypographyEngine::default();
let segs = engine.process("ffi");
assert_eq!(segs.len(), 1);
assert_eq!(segs[0].text, "\u{FB03}");
}
}