Skip to content

Commit bed0e68

Browse files
committed
Return validated font for MemSource::add_font.
Since we already did the work to validate it, returning the validated font could improve perf for the user of the crate.
1 parent b43fa3e commit bed0e68

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/sources/mem.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ impl MemSource {
4848

4949
/// Add an existing font handle to a `MemSource`.
5050
///
51+
/// Returns the font that was just added.
52+
///
5153
/// Note that adding fonts to an existing `MemSource` is slower than creating a new one from a
5254
/// `Handle` iterator, since this method sorts after every addition, rather than once at the
5355
/// end.
54-
pub fn add_font(&mut self, handle: Handle) -> Result<(), FontLoadingError> {
55-
add_font(handle, &mut self.families)?;
56+
pub fn add_font(&mut self, handle: Handle) -> Result<Font, FontLoadingError> {
57+
let font = add_font(handle, &mut self.families)?;
5658
self.families
5759
.sort_by(|a, b| a.family_name.cmp(&b.family_name));
58-
Ok(())
60+
Ok(font)
5961
}
6062

6163
/// Add a number of existing font handles to a `MemSource`.
@@ -183,8 +185,8 @@ impl Source for MemSource {
183185
}
184186
}
185187

186-
/// Adds a font, but doesn't sort
187-
fn add_font(handle: Handle, families: &mut Vec<FamilyEntry>) -> Result<(), FontLoadingError> {
188+
/// Adds a font, but doesn't sort. Returns the font that was created to check for validity.
189+
fn add_font(handle: Handle, families: &mut Vec<FamilyEntry>) -> Result<Font, FontLoadingError> {
188190
let font = Font::from_handle(&handle)?;
189191
if let Some(postscript_name) = font.postscript_name() {
190192
families.push(FamilyEntry {
@@ -193,7 +195,7 @@ fn add_font(handle: Handle, families: &mut Vec<FamilyEntry>) -> Result<(), FontL
193195
font: handle,
194196
})
195197
}
196-
Ok(())
198+
Ok(font)
197199
}
198200

199201
struct FamilyEntry {

0 commit comments

Comments
 (0)