Skip to content

Commit

Permalink
Return validated font for MemSource::add_font.
Browse files Browse the repository at this point in the history
Since we already did the work to validate it, returning the validated
font could improve perf for the user of the crate.
  • Loading branch information
richard-uk1 committed Dec 4, 2020
1 parent b43fa3e commit bed0e68
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/sources/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ impl MemSource {

/// Add an existing font handle to a `MemSource`.
///
/// Returns the font that was just added.
///
/// Note that adding fonts to an existing `MemSource` is slower than creating a new one from a
/// `Handle` iterator, since this method sorts after every addition, rather than once at the
/// end.
pub fn add_font(&mut self, handle: Handle) -> Result<(), FontLoadingError> {
add_font(handle, &mut self.families)?;
pub fn add_font(&mut self, handle: Handle) -> Result<Font, FontLoadingError> {
let font = add_font(handle, &mut self.families)?;
self.families
.sort_by(|a, b| a.family_name.cmp(&b.family_name));
Ok(())
Ok(font)
}

/// Add a number of existing font handles to a `MemSource`.
Expand Down Expand Up @@ -183,8 +185,8 @@ impl Source for MemSource {
}
}

/// Adds a font, but doesn't sort
fn add_font(handle: Handle, families: &mut Vec<FamilyEntry>) -> Result<(), FontLoadingError> {
/// Adds a font, but doesn't sort. Returns the font that was created to check for validity.
fn add_font(handle: Handle, families: &mut Vec<FamilyEntry>) -> Result<Font, FontLoadingError> {
let font = Font::from_handle(&handle)?;
if let Some(postscript_name) = font.postscript_name() {
families.push(FamilyEntry {
Expand All @@ -193,7 +195,7 @@ fn add_font(handle: Handle, families: &mut Vec<FamilyEntry>) -> Result<(), FontL
font: handle,
})
}
Ok(())
Ok(font)
}

struct FamilyEntry {
Expand Down

0 comments on commit bed0e68

Please sign in to comment.