diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-09-29 10:48:52 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-09-29 10:48:52 +0800 |
| commit | 2e01665a238512beca31d7636f78c1134d74f352 (patch) | |
| tree | 329928ea5055539ff44f835d0abe2719b75ab6eb /src | |
| parent | f8048392526edf075a77118eceb1e8d26b6ca910 (diff) | |
image: get system text
Diffstat (limited to 'src')
| -rw-r--r-- | src/image.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/image.rs b/src/image.rs index cd76950..d642d32 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,7 +1,10 @@ +use ab_glyph::FontArc; +use fontdb::{Database, Family, Query, Source}; use image::{ColorType, DynamicImage, ImageBuffer, imageops}; use imageproc::drawing::{Canvas, draw_filled_rect_mut, draw_polygon_mut}; use imageproc::point::Point; use imageproc::rect::Rect; +use std::io; use std::path::Path; use crate::ast::DepthExpr; @@ -49,6 +52,41 @@ mod colors { ]); } +// TODO: store as singleton +fn get_font() -> Result<FontArc, io::Error> { + let mut db = Database::new(); + db.load_system_fonts(); + + let id = db + .query(&Query { + families: &[Family::SansSerif], + weight: fontdb::Weight::NORMAL, + stretch: fontdb::Stretch::Normal, + style: fontdb::Style::Normal, + }) + .or_else(|| { + db.faces() + .next() + .map(|f| f.id) + }) + .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "no system fonts"))?; + + let face = db + .face(id) + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "face missing"))?; + + match &face.source { + Source::File(path) => std::fs::read(path).and_then(|bytes| FontArc::try_from_vec(bytes).map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "parse font failed"))), + Source::Binary(data) => FontArc::try_from_vec( + data.as_ref() + .as_ref() + .to_vec(), + ) + .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "parse font failed")), + _ => Err(io::Error::new(io::ErrorKind::Unsupported, "unsupported font source")), + } +} + fn ext(img: &mut DynamicImage, (curw, curh): &mut Offset, (extw, exth): &mut Offset) { let (w, h) = img.dimensions(); let extedw = *extw + *curw; |
