use std::sync::{Mutex, OnceLock};
use arc_motion::{PathPoint, PolylinePath};
const QUAD_STEPS: usize = 16;
const CUBIC_STEPS: usize = 24;
#[derive(Default)]
pub(crate) struct Outline {
pub contours: Vec<PolylinePath>,
current: Vec<PathPoint>,
cursor: (f32, f32),
}
impl Outline {
fn finish(&mut self) {
if self.current.len() > 1 {
self.contours
.push(PolylinePath::new(std::mem::take(&mut self.current)));
} else {
self.current.clear();
}
}
fn add(&mut self, x: f32, y: f32) {
self.current.push(PathPoint::new(x, -y));
self.cursor = (x, y);
}
pub fn glyph(face: &ttf_parser::Face<'_>, id: ttf_parser::GlyphId) -> Self {
let mut outline = Self::default();
face.outline_glyph(id, &mut outline);
outline.finish();
outline
}
}
impl ttf_parser::OutlineBuilder for Outline {
fn move_to(&mut self, x: f32, y: f32) {
self.finish();
self.add(x, y);
}
fn line_to(&mut self, x: f32, y: f32) {
self.add(x, y);
}
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
let (x0, y0) = self.cursor;
for step in 1..=QUAD_STEPS {
let t = step as f32 / QUAD_STEPS as f32;
let u = 1.0 - t;
self.add(
u * u * x0 + 2.0 * u * t * x1 + t * t * x,
u * u * y0 + 2.0 * u * t * y1 + t * t * y,
);
}
}
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
let (x0, y0) = self.cursor;
for step in 1..=CUBIC_STEPS {
let t = step as f32 / CUBIC_STEPS as f32;
let u = 1.0 - t;
self.add(
u.powi(3) * x0 + 3.0 * u * u * t * x1 + 3.0 * u * t * t * x2 + t.powi(3) * x,
u.powi(3) * y0 + 3.0 * u * u * t * y1 + 3.0 * u * t * t * y2 + t.powi(3) * y,
);
}
}
fn close(&mut self) {
if let Some(first) = self.current.first().copied() {
self.current.push(first);
}
self.finish();
}
}
static FONTS: OnceLock<Mutex<fontdb::Database>> = OnceLock::new();
pub(crate) fn install_fonts() {
FONTS.get_or_init(|| {
let mut db = fontdb::Database::new();
db.load_system_fonts();
Mutex::new(db)
});
}
#[cfg(test)]
pub(crate) fn svg_font_database() -> Result<std::sync::Arc<fontdb::Database>, String> {
static SHARED: OnceLock<std::sync::Arc<fontdb::Database>> = OnceLock::new();
if let Some(db) = SHARED.get() {
return Ok(db.clone());
}
install_fonts();
let db = FONTS
.get()
.ok_or("字体数据库不可用")?
.lock()
.map_err(|_| "字体数据库不可用".to_string())?;
Ok(SHARED
.get_or_init(|| std::sync::Arc::new(db.clone()))
.clone())
}
pub(crate) fn with_face<R>(
family: &str,
weight: u16,
body: impl FnOnce(&mut ttf_parser::Face<'_>) -> Result<R, String>,
) -> Result<R, String> {
install_fonts();
let db = FONTS.get().ok_or("字体数据库不可用")?;
let db = db.lock().map_err(|_| "字体数据库不可用".to_string())?;
let families = [
fontdb::Family::Name(family),
fontdb::Family::Name("Segoe UI"),
fontdb::Family::Name("Arial"),
fontdb::Family::Name("DejaVu Sans"),
fontdb::Family::SansSerif,
];
let id = db
.query(&fontdb::Query {
families: &families,
weight: fontdb::Weight(weight.clamp(100, 900)),
..Default::default()
})
.ok_or("没有可用的轮廓字体")?;
db.with_face_data(id, |bytes, index| {
let mut face =
ttf_parser::Face::parse(bytes, index).map_err(|e| format!("无法解析字体:{e:?}"))?;
face.set_variation(ttf_parser::Tag::from_bytes(b"wght"), weight as f32);
body(&mut face)
})
.ok_or_else(|| "无法读取字体数据".to_string())?
}