Skip to content

Commit

Permalink
Merge pull request #67 from waywardmonkeys/clippy-fixes
Browse files Browse the repository at this point in the history
Clippy fixes
  • Loading branch information
dfrg authored Oct 7, 2024
2 parents 1f3e244 + b6db71e commit d1695ed
Show file tree
Hide file tree
Showing 26 changed files with 81 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Don't warn about these identifiers when using clippy::doc_markdown.
doc-valid-idents = ["ClearType", "HarfBuzz", "OpenType", "PostScript", ".."]
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ default = ["scale", "render"]
scale = ["dep:yazi", "dep:zeno"]
render = ["scale", "zeno/eval"]

[lints]
clippy.doc_markdown = "warn"
clippy.semicolon_if_nothing_returned = "warn"

[dependencies]
yazi = { version = "0.1.6", optional = true }
zeno = { version = "0.2.2", optional = true, default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub struct ObliqueAngle(pub(crate) u8);
impl ObliqueAngle {
/// Creates a new oblique angle from degrees.
pub fn from_degrees(degrees: f32) -> Self {
let a = degrees.min(90.).max(-90.) + 90.;
let a = degrees.clamp(-90., 90.) + 90.;
Self(a as u8)
}

Expand Down Expand Up @@ -455,7 +455,7 @@ impl Weight {
Some(match s {
"normal" => Self::NORMAL,
"bold" => Self::BOLD,
_ => Self(s.parse::<u32>().ok()?.min(1000).max(1) as u16),
_ => Self(s.parse::<u32>().ok()?.clamp(1, 1000) as u16),
})
}
}
Expand Down Expand Up @@ -508,7 +508,7 @@ impl Stretch {
/// clamped at half percentage increments between 50% and 200%,
/// inclusive.
pub fn from_percentage(percentage: f32) -> Self {
let value = ((percentage.min(200.).max(50.) - 50.) * 2.) as u16;
let value = ((percentage.clamp(50., 200.) - 50.) * 2.) as u16;
Self(value)
}

Expand Down
2 changes: 1 addition & 1 deletion src/feature/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn desc_from_at(tag: RawTag) -> Option<(usize, &'static str)> {
/// Returns a feature tag and description from an AAT feature
/// and selector.
pub fn desc_from_aat(feature: u16, selector: u16) -> Option<(usize, RawTag, &'static str)> {
let key = ((feature << 8) | selector) as u16;
let key = (feature << 8) | selector;
Some(match AAT_TO_AT.binary_search_by(|pair| pair.0.cmp(&key)) {
Ok(index) => {
let (tag, desc) = FEATURES[AAT_TO_AT[index].1 as usize];
Expand Down
4 changes: 2 additions & 2 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<'a> Iterator for Fonts<'a> {
type Item = FontRef<'a>;

fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = (self.data.len - self.pos) as usize;
let remaining = self.data.len - self.pos;
(remaining, Some(remaining))
}

Expand All @@ -232,7 +232,7 @@ impl<'a> Iterator for Fonts<'a> {

impl<'a> ExactSizeIterator for Fonts<'a> {
fn len(&self) -> usize {
(self.data.len - self.pos) as usize
self.data.len - self.pos
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/aat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub const KERX: RawTag = raw_tag(b"kerx");
pub const ANKR: RawTag = raw_tag(b"ankr");
pub const KERN: RawTag = raw_tag(b"kern");

/// Maximum number of times we allow consecutive DONT_ADVANCE states.
/// Maximum number of times we allow consecutive `DONT_ADVANCE` states.
const MAX_CYCLES: u16 = 16;

/// Gets a value from a lookup table.
Expand Down
2 changes: 1 addition & 1 deletion src/internal/at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ pub fn lookup_data(
_ => return None,
}
};
let ignored = ((f as u8) & 0b1110) | 1 << 5;
let ignored = (f & 0b1110) | 1 << 5;
Some(LookupData {
index,
stage,
Expand Down
2 changes: 1 addition & 1 deletion src/internal/cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn map(data: &[u8], subtable: u32, format: u8, codepoint: u32) -> Option<u16
let diff = (c - start) as usize * 2;
let id = b.read::<u16>(range_base + diff).unwrap_or(0);
return if id != 0 {
Some((id as i32 + delta as i32) as u16)
Some((id as i32 + delta) as u16)
} else {
Some(0)
};
Expand Down
7 changes: 1 addition & 6 deletions src/internal/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,7 @@ impl<'a> Os2<'a> {

/// Returns a four character font vendor identifier.
pub fn vendor_id(&self) -> &'a str {
core::str::from_utf8(
self.0
.read_bytes(58, 4)
.unwrap_or(&[b'n', b'o', b'n', b'e']),
)
.unwrap_or("none")
core::str::from_utf8(self.0.read_bytes(58, 4).unwrap_or(b"none")).unwrap_or("none")
}

/// Returns the font selection bit flags.
Expand Down
2 changes: 1 addition & 1 deletion src/internal/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<'a> Stream<'a> {
}

/// An array wrapping a byte buffer over a sequence of values that implement
/// FromBeData.
/// [`FromBeData`].
#[derive(Copy, Clone)]
pub struct Array<'a, T: FromBeData> {
data: &'a [u8],
Expand Down
7 changes: 3 additions & 4 deletions src/internal/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ impl VarAxis {
};
value = value.min(Fixed::ONE).max(-Fixed::ONE);
value = avar
.map(|(data, avar)| adjust_axis(data, avar, self.index, value))
.flatten()
.and_then(|(data, avar)| adjust_axis(data, avar, self.index, value))
.unwrap_or(value);
value.to_f2dot14()
}
Expand Down Expand Up @@ -313,9 +312,9 @@ pub fn item_delta(
} else if coord == peak {
continue;
} else if coord < peak {
scalar = scalar * (coord - start) / (peak - start)
scalar = scalar * (coord - start) / (peak - start);
} else {
scalar = scalar * (end - coord) / (end - peak)
scalar = scalar * (end - coord) / (end - peak);
};
}
let val = if idx >= short_count {
Expand Down
2 changes: 1 addition & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl<'a> GlyphMetrics<'a> {
pub fn lsb(&self, glyph_id: GlyphId) -> f32 {
let mut v = xmtx::sb(self.data, self.hmtx, self.hmtx_count, glyph_id) as f32;
if self.hvar != 0 {
v += var::sb_delta(self.data, self.hvar, glyph_id, self.coords)
v += var::sb_delta(self.data, self.hvar, glyph_id, self.coords);
}
v * self.scale
}
Expand Down
9 changes: 4 additions & 5 deletions src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'a> ColorPalettes<'a> {
let version = b.read::<u16>(0)?;
let num_entries = b.read::<u16>(2)?;
let offset = b.read::<u32>(8)? as usize;
let first = b.read::<u16>(12 + index as usize * 2)? as usize;
let first = b.read::<u16>(12 + index * 2)? as usize;
let offset = offset + first * 4;
Some(ColorPalette {
font: self.font,
Expand Down Expand Up @@ -94,16 +94,15 @@ impl<'a> ColorPalette<'a> {
return None;
}
Some(StringId::Other(
d.read::<u16>(labels_offset + self.index as usize * 2)?,
d.read::<u16>(labels_offset + self.index * 2)?,
))
}

/// Returns the name for the palette, optionally for a particular
/// language.
pub fn name(&self, language: Option<&str>) -> Option<LocalizedString<'a>> {
self.name_id()
.map(|id| self.font.localized_strings().find_by_id(id, language))
.flatten()
.and_then(|id| self.font.localized_strings().find_by_id(id, language))
}

/// Returns the theme usability of the palette, if available.
Expand Down Expand Up @@ -152,7 +151,7 @@ impl<'a> ColorPalette<'a> {
if types_offset == 0 {
return None;
}
d.read::<u32>(types_offset + self.index as usize * 4)
d.read::<u32>(types_offset + self.index * 4)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/scale/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn resize(
target_height,
scratch,
0.,
&|x| nearest(x),
&nearest,
),
Bilinear => resample(
image,
Expand All @@ -137,7 +137,7 @@ pub fn resize(
target_height,
scratch,
1.,
&|x| bilinear(x),
&bilinear,
),
Bicubic => resample(
image,
Expand All @@ -149,7 +149,7 @@ pub fn resize(
target_height,
scratch,
2.,
&|x| bicubic(x),
&bicubic,
),
Mitchell => resample(
image,
Expand All @@ -161,7 +161,7 @@ pub fn resize(
target_height,
scratch,
2.,
&|x| mitchell(x),
&mitchell,
),
Lanczos3 => resample(
image,
Expand All @@ -173,7 +173,7 @@ pub fn resize(
target_height,
scratch,
3.,
&|x| lanczos3(x),
&lanczos3,
),
Gaussian => resample(
image,
Expand Down
8 changes: 4 additions & 4 deletions src/scale/bitmap/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ fn decode_data<E: Emit>(
bwidth,
)?;
if depth == 8 {
E::emit(&state, line, target, start, y, w, inc, cols)?;
E::emit(state, line, target, start, y, w, inc, cols)?;
} else {
normalize(line, out_line, depth, has_palette, cols, trunc_16)?;
E::emit(&state, out_line, target, start, y, w, inc, cols)?;
E::emit(state, out_line, target, start, y, w, inc, cols)?;
}
std::mem::swap(&mut prev_line, &mut line);
y += row_inc;
Expand All @@ -347,7 +347,7 @@ fn decode_data<E: Emit>(
let source = decomp.get(offset..end)?;
let ty = *source.get(0)?;
defilter(ty, source.get(1..)?, line, prev_line, bwidth)?;
E::emit(&state, line, target, 0, y, w, 1, w)?;
E::emit(state, line, target, 0, y, w, 1, w)?;
std::mem::swap(&mut prev_line, &mut line);
}
} else {
Expand All @@ -358,7 +358,7 @@ fn decode_data<E: Emit>(
let ty = *source.get(0)?;
defilter(ty, source.get(1..)?, line, prev_line, bwidth)?;
normalize(line, out_line, depth, has_palette, w, trunc_16)?;
E::emit(&state, out_line, target, 0, y, w, 1, w)?;
E::emit(state, out_line, target, 0, y, w, 1, w)?;
std::mem::swap(&mut prev_line, &mut line);
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/scale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl ScaleContext {
/// Creates a new scaling context with the specified maximum number of
/// cache entries.
pub fn with_max_entries(max_entries: usize) -> Self {
let max_entries = max_entries.min(64).max(1);
let max_entries = max_entries.clamp(1, 64);
Self {
fonts: FontCache::new(max_entries),
state: State {
Expand Down Expand Up @@ -349,9 +349,7 @@ pub struct ScalerBuilder<'a> {
impl<'a> ScalerBuilder<'a> {
fn new(context: &'a mut ScaleContext, font: impl Into<FontRef<'a>>) -> Self {
let font = font.into();
let (id, proxy) = context
.fonts
.get(&font, None, |font| ScalerProxy::from_font(font));
let (id, proxy) = context.fonts.get(&font, None, ScalerProxy::from_font);
let skrifa_font = if font.offset == 0 {
skrifa::FontRef::new(font.data).ok()
} else {
Expand Down Expand Up @@ -441,7 +439,7 @@ impl<'a> ScalerBuilder<'a> {
id: self.id,
outlines,
size: skrifa_size,
coords: &self.coords,
coords: self.coords,
};
self.hinting_cache.get(&key)
}
Expand Down Expand Up @@ -556,7 +554,7 @@ impl<'a> Scaler<'a> {
} else {
(
self.skrifa_size,
skrifa::instance::LocationRef::new(&self.coords),
skrifa::instance::LocationRef::new(self.coords),
)
.into()
};
Expand Down Expand Up @@ -919,8 +917,7 @@ impl<'a> Render<'a> {
.render_into(&mut scratch[..], None);
let color = layer
.color_index()
.map(|i| palette.map(|p| p.get(i)))
.flatten()
.and_then(|i| palette.map(|p| p.get(i)))
.unwrap_or(self.foreground);
bitmap::blit(
&scratch[..],
Expand Down
4 changes: 2 additions & 2 deletions src/scale/outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub struct LayerMut<'a> {
impl<'a> LayerMut<'a> {
/// Returns the sequence of points for the layer.
pub fn points(&'a self) -> &'a [Point] {
&self.points[..]
self.points
}

/// Returns a mutable reference the sequence of points for the layer.
Expand Down Expand Up @@ -296,7 +296,7 @@ impl Outline {
points: (0, points_end),
verbs: (0, verbs_end),
color_index: None,
})
});
}
}
}
Expand Down
Loading

0 comments on commit d1695ed

Please sign in to comment.