Skip to content

Commit

Permalink
refactor(font-enumeration)!: simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcur committed Jul 25, 2024
1 parent 544678c commit 1774c79
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 42 deletions.
6 changes: 3 additions & 3 deletions font-enumeration/src/core_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core_text::{
font_descriptor::{CTFontTraits, SymbolicTraitAccessors, TraitAccessors},
};

use crate::{Error, OwnedFont, Stretch, Style, Weight};
use crate::{Error, Font, Stretch, Style, Weight};

fn roughly_eq(a: f32, b: f32) -> bool {
const EPSILON: f32 = 0.00001;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Stretch {
}
}

pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {
pub fn all_fonts() -> Result<Box<[Font]>, Error> {
let collection = font_collection::create_for_all_families();

let fonts = collection
Expand All @@ -104,7 +104,7 @@ pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {
.iter()
.filter_map(|font| {
let traits = font.traits();
Some(OwnedFont {
Some(Font {
family_name: font.family_name(),
font_name: font.font_name(),
path: font.font_path()?,
Expand Down
6 changes: 3 additions & 3 deletions font-enumeration/src/direct_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dwrote::{
FontWeight as DWriteWeight,
};

use crate::{Error, OwnedFont, Stretch, Style, Weight};
use crate::{Error, Font, Stretch, Style, Weight};

impl Style {
fn from_direct_write(style: DWriteStyle) -> Self {
Expand Down Expand Up @@ -55,7 +55,7 @@ impl Stretch {
}
}

pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {
pub fn all_fonts() -> Result<Box<[Font]>, Error> {
let collection = FontCollection::system();

let mut fonts = Vec::new();
Expand All @@ -67,7 +67,7 @@ pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {

// check: are there ever multiple files in a face?
let path = face.get_files()[0].get_font_file_path().unwrap();
fonts.push(OwnedFont {
fonts.push(Font {
family_name: font.family_name(),
font_name: font.face_name(),
path,
Expand Down
6 changes: 3 additions & 3 deletions font-enumeration/src/fontconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use fontconfig::{Fontconfig, ObjectSet, Pattern};

use crate::{Error, OwnedFont, Stretch, Style, Weight};
use crate::{Error, Font, Stretch, Style, Weight};

impl Stretch {
fn from_fc(width: i32) -> Self {
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Weight {
}
}

pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {
pub fn all_fonts() -> Result<Box<[Font]>, Error> {
let fc = Fontconfig::new().ok_or(Error::SystemCollection)?;

let pattern = Pattern::new(&fc);
Expand All @@ -100,7 +100,7 @@ pub fn all_fonts() -> Result<Box<[OwnedFont]>, Error> {
let weight = font.weight().unwrap_or(fontconfig::FC_WEIGHT_REGULAR);
let width = font.width().unwrap_or(fontconfig::FC_WIDTH_NORMAL);

Some(OwnedFont {
Some(Font {
family_name: family.to_owned(),
font_name: name.to_owned(),
path: PathBuf::from(path),
Expand Down
45 changes: 14 additions & 31 deletions font-enumeration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use thiserror::Error;

Expand All @@ -24,7 +24,7 @@ pub enum Error {

pub struct Collection {
// Using a boxed slice rather than Vec saves [Collection] from having to store a capacity
all_fonts: Box<[OwnedFont]>,
all_fonts: Box<[Font]>,
}

impl Collection {
Expand All @@ -34,26 +34,19 @@ impl Collection {
Ok(Self { all_fonts })
}

pub fn all<'c>(&'c self) -> impl Iterator<Item = Font<'c>> {
self.all_fonts.iter().map(move |font| Font {
family_name: &font.family_name,
font_name: &font.font_name,
path: &font.path,
style: font.style,
weight: font.weight,
stretch: font.stretch,
})
pub fn all(&self) -> impl Iterator<Item = &'_ Font> {
self.all_fonts.iter()
}

pub fn by_family<'c, 'f>(&'c self, family_name: &'f str) -> impl Iterator<Item = Font<'c>> + 'f
pub fn by_family<'c, 'f>(&'c self, family_name: &'f str) -> impl Iterator<Item = &'c Font> + 'f
where
'c: 'f,
{
self.all()
.filter(|font| utils::case_insensitive_match(font.family_name, family_name))
.filter(|font| utils::case_insensitive_match(&font.family_name, family_name))
}

pub fn take(self) -> Vec<OwnedFont> {
pub fn take(self) -> Vec<Font> {
self.all_fonts.into_vec()
}
}
Expand Down Expand Up @@ -163,24 +156,14 @@ impl Stretch {
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Font<'c> {
pub family_name: &'c str,
pub font_name: &'c str,
pub path: &'c Path,
style: Style,
weight: Weight,
stretch: Stretch,
}

#[derive(Clone, Debug, PartialEq)]
pub struct OwnedFont {
family_name: String,
font_name: String,
path: PathBuf,
style: Style,
weight: Weight,
stretch: Stretch,
pub struct Font {
pub family_name: String,
pub font_name: String,
pub path: PathBuf,
pub style: Style,
pub weight: Weight,
pub stretch: Stretch,
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ fn main_(cli: Cli, mut out: impl Out) -> anyhow::Result<()> {

let mut font_files: Vec<&'_ Path> = Vec::new();
for font in font_collection.by_family(&family_name) {
if !font_files.contains(&font.path) {
font_files.push(font.path);
if !font_files.contains(&font.path.as_ref()) {
font_files.push(font.path.as_ref());
}
}

Expand Down

0 comments on commit 1774c79

Please sign in to comment.