Skip to content

Commit

Permalink
refactor: use option methods instead of macro
Browse files Browse the repository at this point in the history
The `try_opt_or!` macro slightly shortened some
`map(...).unwrap_or(...)` logic in `./lib.rs`, and
some longer `if _.is_none() { return }` logic in
`./tables/gvar.rs`. I had to do some code searching
to figure out what `try_opt_or!` did, so I refactored
it for my own comprension.
  • Loading branch information
SKalt committed Feb 7, 2024
1 parent f892c83 commit b29093d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
22 changes: 8 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ Font parsing starts with a [`Face`].
#[macro_use]
extern crate std;

macro_rules! try_opt_or {
($value:expr, $ret:expr) => {
match $value {
Some(v) => v,
None => return $ret,
}
};
}

#[cfg(feature = "apple-layout")]
mod aat;
#[cfg(feature = "opentype-layout")]
Expand Down Expand Up @@ -1342,7 +1333,7 @@ impl<'a> Face<'a> {
/// Returns `false` when OS/2 table is not present.
#[inline]
pub fn is_bold(&self) -> bool {
try_opt_or!(self.tables.os2, false).is_bold()
self.tables.os2.map(|os2| os2.is_bold()).unwrap_or(false)
}

/// Checks that face is marked as *Oblique*.
Expand All @@ -1359,15 +1350,18 @@ impl<'a> Face<'a> {
/// Returns face style.
#[inline]
pub fn style(&self) -> Style {
try_opt_or!(self.tables.os2, Style::Normal).style()
self.tables.os2.map(|os2| os2.style()).unwrap_or_default()
}

/// Checks that face is marked as *Monospaced*.
///
/// Returns `false` when `post` table is not present.
#[inline]
pub fn is_monospaced(&self) -> bool {
try_opt_or!(self.tables.post, false).is_monospaced
self.tables
.post
.map(|post| post.is_monospaced)
.unwrap_or(false)
}

/// Checks that face is variable.
Expand All @@ -1392,15 +1386,15 @@ impl<'a> Face<'a> {
/// Returns `Weight::Normal` when OS/2 table is not present.
#[inline]
pub fn weight(&self) -> Weight {
try_opt_or!(self.tables.os2, Weight::default()).weight()
self.tables.os2.map(|os2| os2.weight()).unwrap_or_default()
}

/// Returns face's width.
///
/// Returns `Width::Normal` when OS/2 table is not present or when value is invalid.
#[inline]
pub fn width(&self) -> Width {
try_opt_or!(self.tables.os2, Width::default()).width()
self.tables.os2.map(|os2| os2.width()).unwrap_or_default()
}

/// Returns face's italic angle.
Expand Down
9 changes: 7 additions & 2 deletions src/tables/gvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1626,8 +1626,13 @@ fn infer_delta(
//
// 'Target point delta is derived from the adjacent point deltas
// using linear interpolation.'
let d = f32::from(try_opt_or!(target_point.checked_sub(prev_point), 0.0))
/ f32::from(try_opt_or!(next_point.checked_sub(prev_point), 0.0));
let target_sub = target_point.checked_sub(prev_point);
let next_sub = next_point.checked_sub(prev_point);
let d = if let (Some(target_sub), Some(next_sub)) = (target_sub, next_sub) {
f32::from(target_sub) / f32::from(next_sub)
} else {
return 0.0;
};
(1.0 - d) * prev_delta + d * next_delta
}
}
Expand Down

0 comments on commit b29093d

Please sign in to comment.