summaryrefslogtreecommitdiff
path: root/src/font.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-18 14:23:48 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-18 14:25:49 +0200
commit011865ab5c8943abcb64c7b545e265d1a65db32a (patch)
treea6c45d99909e9800d825eb3bb1edc278d2a48e6b /src/font.rs
parent594809e35b9e768f1a50926cf5e7a9df41ba7d16 (diff)
Memory loader
Diffstat (limited to 'src/font.rs')
-rw-r--r--src/font.rs41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/font.rs b/src/font.rs
index 300774bb..978fb601 100644
--- a/src/font.rs
+++ b/src/font.rs
@@ -3,11 +3,12 @@
use std::collections::{hash_map::Entry, HashMap};
use std::fmt::{self, Debug, Display, Formatter};
use std::ops::Add;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use std::rc::Rc;
use decorum::N64;
use serde::{Deserialize, Serialize};
+use ttf_parser::name_id;
use crate::geom::Length;
use crate::loading::{FileHash, Loader};
@@ -376,6 +377,44 @@ pub struct FaceInfo {
pub variant: FontVariant,
}
+impl FaceInfo {
+ /// Determine metadata about all faces that are found in the given data.
+ pub fn parse<'a>(
+ path: &'a Path,
+ data: &'a [u8],
+ ) -> impl Iterator<Item = FaceInfo> + 'a {
+ let count = ttf_parser::fonts_in_collection(data).unwrap_or(1);
+ (0 .. count).filter_map(move |index| {
+ fn find_name(face: &ttf_parser::Face, name_id: u16) -> Option<String> {
+ face.names().find_map(|entry| {
+ (entry.name_id() == name_id).then(|| entry.to_string()).flatten()
+ })
+ }
+
+ let face = ttf_parser::Face::from_slice(data, index).ok()?;
+ let family = find_name(&face, name_id::TYPOGRAPHIC_FAMILY)
+ .or_else(|| find_name(&face, name_id::FAMILY))?;
+
+ let variant = FontVariant {
+ style: match (face.is_italic(), face.is_oblique()) {
+ (false, false) => FontStyle::Normal,
+ (true, _) => FontStyle::Italic,
+ (_, true) => FontStyle::Oblique,
+ },
+ weight: FontWeight::from_number(face.weight().to_number()),
+ stretch: FontStretch::from_number(face.width().to_number()),
+ };
+
+ Some(FaceInfo {
+ path: path.to_owned(),
+ index,
+ family,
+ variant,
+ })
+ })
+ }
+}
+
/// Properties that distinguish a face from other faces in the same family.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
#[derive(Serialize, Deserialize)]