From ba8d74b0ef4ee4fa0d78e8511e57710d39441f3c Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Mon, 29 Sep 2025 14:52:01 +0800 Subject: image(font): simplify sansserif system font lookup --- src/image.rs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'src/image.rs') diff --git a/src/image.rs b/src/image.rs index b78dc04..077bb82 100644 --- a/src/image.rs +++ b/src/image.rs @@ -74,25 +74,24 @@ fn get_font() -> Result { 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::other("face missing"))?; - - match &face.source { + match &db + .face( + 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"))?, + ) + .ok_or_else(|| io::Error::other("face missing"))? + .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() -- cgit v1.2.3