Skip to content

Commit

Permalink
Fix clippy warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Sep 28, 2022
1 parent 642af90 commit ec39fc1
Show file tree
Hide file tree
Showing 24 changed files with 154 additions and 66 deletions.
6 changes: 6 additions & 0 deletions src/ggg/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ impl<'a> LookupSubtables<'a> {
self.offsets.len()
}

/// Checks if there are any items.
pub fn is_empty(&self) -> bool {
self.offsets.is_empty()
}

/// Parses a subtable at index.
///
/// Accepts either
Expand All @@ -86,6 +91,7 @@ impl<'a> LookupSubtables<'a> {
/// Creates an iterator over subtables.
///
/// We cannot use `IntoIterator` here, because we have to use user-provided base type.
#[allow(clippy::should_implement_trait)]
pub fn into_iter<T: LookupSubtable<'a>>(self) -> LookupSubtablesIter<'a, T> {
LookupSubtablesIter {
data: self,
Expand Down
20 changes: 13 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Font parsing starts with a [`Face`].
#![warn(missing_docs)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![allow(clippy::get_first)] // we use it for readability
#![allow(clippy::identity_op)] // we use it for readability
#![allow(clippy::too_many_arguments)]
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::field_reassign_with_default)]
#![allow(clippy::upper_case_acronyms)]

#[cfg(feature = "std")]
#[macro_use]
Expand Down Expand Up @@ -144,7 +150,7 @@ impl FromData for Magic {
///
/// The number is stored as f2.16
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Default, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
pub struct NormalizedCoordinate(i16);

impl From<i16> for NormalizedCoordinate {
Expand Down Expand Up @@ -305,7 +311,7 @@ impl FromData for Tag {
///
/// Used for underline and strikeout.
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct LineMetrics {
/// Line position.
pub position: i16,
Expand All @@ -319,7 +325,7 @@ pub struct LineMetrics {
/// Doesn't guarantee that `x_min` <= `x_max` and/or `y_min` <= `y_max`.
#[repr(C)]
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Rect {
pub x_min: i16,
pub y_min: i16,
Expand Down Expand Up @@ -377,7 +383,7 @@ impl BBox {
}

#[inline]
fn to_rect(&self) -> Option<Rect> {
fn to_rect(self) -> Option<Rect> {
Some(Rect {
x_min: i16::try_num_from(self.x_min)?,
y_min: i16::try_num_from(self.y_min)?,
Expand Down Expand Up @@ -420,15 +426,15 @@ impl OutlineBuilder for DummyOutline {

/// A glyph raster image format.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum RasterImageFormat {
PNG,
}

/// A glyph's raster image.
///
/// Note, that glyph metrics are in pixels and not in font units.
#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct RasterGlyphImage<'a> {
/// Horizontal offset.
pub x: i16,
Expand Down Expand Up @@ -507,7 +513,7 @@ impl VarCoords {
}

/// A list of font face parsing errors.
#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum FaceParsingError {
/// An attempt to read out of bounds detected.
///
Expand Down
16 changes: 11 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub struct F2DOT14(pub i16);
impl F2DOT14 {
/// Converts i16 to f32.
#[inline]
pub fn to_f32(&self) -> f32 {
pub fn to_f32(self) -> f32 {
f32::from(self.0) / 16384.0
}
}
Expand Down Expand Up @@ -213,6 +213,7 @@ impl TryNumFrom<f32> for u16 {
}
}

#[allow(clippy::manual_range_contains)]
impl TryNumFrom<f32> for i32 {
#[inline]
fn try_num_from(v: f32) -> Option<Self> {
Expand Down Expand Up @@ -403,7 +404,7 @@ impl<'a, T: FromData> Iterator for LazyArrayIter16<'a, T> {

#[inline]
fn count(self) -> usize {
usize::from(self.data.len().checked_sub(self.index).unwrap_or(0))
usize::from(self.data.len().saturating_sub(self.index))
}
}

Expand Down Expand Up @@ -454,6 +455,11 @@ impl<'a, T: FromData> LazyArray32<'a, T> {
(self.data.len() / T::SIZE) as u32
}

/// Checks if the array is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Performs a binary search by specified `key`.
#[inline]
pub fn binary_search(&self, key: &T) -> Option<(u32, T)>
Expand Down Expand Up @@ -538,7 +544,7 @@ impl<'a, T: FromData> Iterator for LazyArrayIter32<'a, T> {

#[inline]
fn count(self) -> usize {
usize::num_from(self.data.len().checked_sub(self.index).unwrap_or(0))
usize::num_from(self.data.len().saturating_sub(self.index))
}
}

Expand Down Expand Up @@ -641,12 +647,12 @@ impl<'a, T: FromSlice<'a>> Iterator for LazyOffsetArrayIter16<'a, T> {

#[inline]
fn count(self) -> usize {
usize::from(self.array.len().checked_sub(self.index).unwrap_or(0))
usize::from(self.array.len().saturating_sub(self.index))
}
}

/// A streaming binary parser.
#[derive(Clone, Copy, Default, Debug)]
#[derive(Clone, Default, Debug)]
pub struct Stream<'a> {
data: &'a [u8],
offset: usize,
Expand Down
2 changes: 1 addition & 1 deletion src/tables/ankr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::GlyphId;

/// An anchor point.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Default, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
pub struct Point {
pub x: i16,
pub y: i16,
Expand Down
9 changes: 7 additions & 2 deletions src/tables/avar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl<'a> SegmentMaps<'a> {
pub fn len(&self) -> u16 {
self.count
}

/// Checks if there are any segments.
pub fn is_empty(&self) -> bool {
self.count == 0
}
}

impl core::fmt::Debug for SegmentMaps<'_> {
Expand Down Expand Up @@ -76,7 +81,7 @@ impl<'a> Iterator for SegmentMapsIter<'a> {

fn next(&mut self) -> Option<Self::Item> {
let count = self.stream.read::<u16>()?;
return self.stream.read_array16::<AxisValueMap>(count);
self.stream.read_array16::<AxisValueMap>(count)
}
}

Expand Down Expand Up @@ -126,7 +131,7 @@ impl<'a> Table<'a> {
fn map_value(map: &LazyArray16<AxisValueMap>, value: i16) -> Option<i16> {
// This code is based on harfbuzz implementation.

if map.len() == 0 {
if map.is_empty() {
return Some(value);
} else if map.len() == 1 {
let record = map.get(0)?;
Expand Down
19 changes: 8 additions & 11 deletions src/tables/cff/cff1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ impl FDSelect<'_> {
fn font_dict_index(&self, glyph_id: GlyphId) -> Option<u8> {
match self {
FDSelect::Format0(ref array) => array.get(glyph_id.0),
FDSelect::Format3(ref data) => {
FDSelect::Format3(data) => {
let mut s = Stream::new(data);
let number_of_ranges = s.read::<u16>()?;
if number_of_ranges == 0 {
Expand Down Expand Up @@ -842,20 +842,17 @@ fn parse_sid_metadata<'a>(
metadata.default_width = private_dict.default_width.unwrap_or(0.0);
metadata.nominal_width = private_dict.nominal_width.unwrap_or(0.0);

match (
if let (Some(private_dict_range), Some(subroutines_offset)) = (
top_dict.private_dict_range,
private_dict.local_subroutines_offset,
) {
(Some(private_dict_range), Some(subroutines_offset)) => {
// 'The local subroutines offset is relative to the beginning
// of the Private DICT data.'
if let Some(start) = private_dict_range.start.checked_add(subroutines_offset) {
let data = data.get(start..data.len())?;
let mut s = Stream::new(data);
metadata.local_subrs = parse_index::<u16>(&mut s)?;
}
// 'The local subroutines offset is relative to the beginning
// of the Private DICT data.'
if let Some(start) = private_dict_range.start.checked_add(subroutines_offset) {
let data = data.get(start..data.len())?;
let mut s = Stream::new(data);
metadata.local_subrs = parse_index::<u16>(&mut s)?;
}
_ => {}
}

Some(FontKind::SID(metadata))
Expand Down
8 changes: 4 additions & 4 deletions src/tables/cff/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn parse_index_impl<'a>(count: u32, s: &mut Stream<'a>) -> Option<Index<'a>> {
let offset_size = s.read::<OffsetSize>()?;
let offsets_len = (count + 1).checked_mul(offset_size.to_u32())?;
let offsets = VarOffsets {
data: &s.read_bytes(usize::num_from(offsets_len))?,
data: s.read_bytes(usize::num_from(offsets_len))?,
offset_size,
};

Expand Down Expand Up @@ -60,7 +60,7 @@ fn skip_index_impl(count: u32, s: &mut Stream) -> Option<()> {
let offset_size = s.read::<OffsetSize>()?;
let offsets_len = (count + 1).checked_mul(offset_size.to_u32())?;
let offsets = VarOffsets {
data: &s.read_bytes(usize::num_from(offsets_len))?,
data: s.read_bytes(usize::num_from(offsets_len))?,
offset_size,
};

Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'a> Index<'a> {
#[inline]
pub fn len(&self) -> u32 {
// Last offset points to the byte after the `Object data`. We should skip it.
self.offsets.len().checked_sub(1).unwrap_or(0)
self.offsets.len().saturating_sub(1)
}

pub fn get(&self, index: u32) -> Option<&'a [u8]> {
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<'a> Iterator for IndexIter<'a> {
}
}

#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum OffsetSize {
Size1 = 1,
Size2 = 2,
Expand Down
4 changes: 2 additions & 2 deletions src/tables/cff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{BBox, OutlineBuilder};

/// A list of errors that can occur during a CFF glyph outlining.
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum CFFError {
NoGlyph,
ReadOutOfBounds,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<'a> Builder<'a> {
}

/// A type-safe wrapper for string ID.
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Debug)]
pub struct StringId(u16);

impl FromData for StringId {
Expand Down
2 changes: 1 addition & 1 deletion src/tables/cmap/format12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a> Subtable12<'a> {
.start_glyph_id
.checked_add(code_point)?
.checked_sub(group.start_char_code)?;
return u16::try_from(id).ok().map(GlyphId);
u16::try_from(id).ok().map(GlyphId)
}

/// Calls `f` for each codepoint defined in this table.
Expand Down
2 changes: 1 addition & 1 deletion src/tables/cmap/format14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl FromData for UnicodeRangeRecord {
}

/// A result of a variation glyph mapping.
#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum GlyphVariationResult {
/// Glyph was found in the variation encoding table.
Found(GlyphId),
Expand Down
7 changes: 6 additions & 1 deletion src/tables/cmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,16 @@ impl<'a> Subtables<'a> {
})
}

/// Returns the number
/// Returns the number of subtables.
#[inline]
pub fn len(&self) -> u16 {
self.records.len()
}

/// Checks if there are any subtables.
pub fn is_empty(&self) -> bool {
self.records.is_empty()
}
}

impl<'a> IntoIterator for Subtables<'a> {
Expand Down
5 changes: 5 additions & 0 deletions src/tables/feat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ impl<'a> FeatureNames<'a> {
pub fn len(&self) -> u16 {
self.records.len()
}

/// Checks if there are any feature names.
pub fn is_empty(&self) -> bool {
self.records.is_empty()
}
}

impl<'a> core::fmt::Debug for FeatureNames<'a> {
Expand Down
5 changes: 3 additions & 2 deletions src/tables/glyf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ impl<'a> EndpointsIter<'a> {
let prev = self.endpoints.get(self.index - 1).unwrap_or(0);
// Malformed font can have endpoints not in increasing order,
// so we have to use checked_sub.
self.left = end.checked_sub(prev).unwrap_or(0);
self.left = self.left.checked_sub(1).unwrap_or(0);
self.left = end.saturating_sub(prev);
self.left = self.left.saturating_sub(1);
}

// Always advance the index, so we can check the current contour number.
Expand Down Expand Up @@ -508,6 +508,7 @@ impl CompositeGlyphFlags {
// It's not defined in the spec, so we are using our own value.
pub(crate) const MAX_COMPONENTS: u8 = 32;

#[allow(clippy::comparison_chain)]
#[inline]
fn outline_impl(
loca_table: loca::Table,
Expand Down
Loading

0 comments on commit ec39fc1

Please sign in to comment.