Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
laurmaedje committed Feb 4, 2025
1 parent 172416a commit 906da09
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: dtolnay/rust-toolchain@1.83.0
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@cargo-hack
- run: cargo clippy
- run: cargo fmt --check --all
- run: cargo doc --workspace --no-deps
- run: cargo doc --workspace --no-deps
8 changes: 4 additions & 4 deletions src/cff/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn parse_index<'a, T: IndexSize>(r: &mut Reader<'a>) -> Option<Index<'a>> {
}

fn parse_index_impl<'a>(count: u32, r: &mut Reader<'a>) -> Option<Index<'a>> {
if count == 0 || count == core::u32::MAX {
if count == 0 || count == u32::MAX {
return Some(Index::default());
}

Expand All @@ -54,7 +54,7 @@ pub fn skip_index<T: IndexSize>(r: &mut Reader) -> Option<()> {
}

fn skip_index_impl(count: u32, r: &mut Reader) -> Option<()> {
if count == 0 || count == core::u32::MAX {
if count == 0 || count == u32::MAX {
return Some(());
}

Expand All @@ -78,7 +78,7 @@ pub struct VarOffsets<'a> {
pub offset_size: OffsetSize,
}

impl<'a> VarOffsets<'a> {
impl VarOffsets<'_> {
pub fn get(&self, index: u32) -> Option<u32> {
if index >= self.len() {
return None;
Expand Down Expand Up @@ -124,7 +124,7 @@ pub struct Index<'a> {
pub offsets: VarOffsets<'a>,
}

impl<'a> Default for Index<'a> {
impl Default for Index<'_> {
#[inline]
fn default() -> Self {
Index {
Expand Down
2 changes: 1 addition & 1 deletion src/cff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn subset(ctx: &mut Context<'_>) -> Result<()> {
w.extend(&index.data);
// Global Subr INDEX
// We desubroutinized, so no global subroutines and thus empty index.
w.write(&OwnedIndex::default());
w.write(OwnedIndex::default());

// Charsets
offsets.charset_offset.update_value(w.len())?;
Expand Down
10 changes: 5 additions & 5 deletions src/cff/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ mod tests {
let mut r = Reader::new(&first);
let rewritten = r.read::<U24>().unwrap();
let mut w = Writer::new();
w.write(&rewritten);
w.write(rewritten);
let second = w.finish();

assert_eq!(first, second);
Expand All @@ -453,7 +453,7 @@ mod tests {

let reparsed = IntegerNumber::parse(&mut reader).unwrap();
let mut w = Writer::new();
w.write(&reparsed);
w.write(reparsed);
let bytes = w.finish();
assert_eq!(bytes.len(), 1);
assert_eq!(reparsed.0, num);
Expand All @@ -473,7 +473,7 @@ mod tests {

let reparsed = IntegerNumber::parse(&mut reader).unwrap();
let mut w = Writer::new();
w.write(&reparsed);
w.write(reparsed);
let bytes = w.finish();
assert_eq!(bytes.len(), 2);
assert_eq!(reparsed.0, num);
Expand All @@ -493,7 +493,7 @@ mod tests {

let reparsed = IntegerNumber::parse(&mut reader).unwrap();
let mut w = Writer::new();
w.write(&reparsed);
w.write(reparsed);
let bytes = w.finish();
assert_eq!(bytes.len(), 3);
assert_eq!(reparsed.0, num);
Expand All @@ -513,7 +513,7 @@ mod tests {

let reparsed = IntegerNumber::parse(&mut reader).unwrap();
let mut w = Writer::new();
w.write(&reparsed);
w.write(reparsed);
let bytes = w.finish();
assert_eq!(bytes.len(), 5);
assert_eq!(reparsed.0, num);
Expand Down
8 changes: 4 additions & 4 deletions src/glyf.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! The `glyf` table contains the main description of the glyphs. In order to
//! subset it, there are 5 things we need to do:
//! 1. We need to form the glyph closure. Glyphs can reference other glyphs, meaning that
//! if a user for example requests the glyph 1, and this glyph references the glyph 2, then
//! we need to include both of them in our subset.
//! if a user for example requests the glyph 1, and this glyph references the glyph 2, then
//! we need to include both of them in our subset.
//! 2. We need to remove glyph descriptions that are not needed for the subset, and reorder
//! the existing glyph descriptions to match the order defined by the remapper.
//! the existing glyph descriptions to match the order defined by the remapper.
//! 3. For component glyphs, we need to rewrite their description so that they reference
//! the new glyph ID of the glyphs they reference.
//! the new glyph ID of the glyphs they reference.
//! 4. We need to calculate which format to use in the `loca` table.
//! 5. We need to update the `loca` table itself with the new offsets.
use super::*;
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ of a general-purpose subsetter in the Rust ecosystem.
A couple of important notes if you want to use this crate in combination with your own pdf writer:
- You must write your fonts as a CID font. This is because we remove the `cmap` table from the font,
so you must provide your own cmap table in the PDF.
so you must provide your own cmap table in the PDF.
- Copyright information in the font will be retained.
- When writing a CID font in PDF, CIDs must be used to address glyphs. This can be pretty tricky,
because the meaning of CID depends on the type of font you are embedding (see the PDF specification
for more information). The subsetter will convert SID-keyed fonts to CID-keyed ones and an identity
mapping from GID to CID for all fonts, regardless of the previous mapping. Because of this, you can
always use the remapped GID as the CID for a glyph, and do not need to worry about the type of font
you are embedding.
because the meaning of CID depends on the type of font you are embedding (see the PDF specification
for more information). The subsetter will convert SID-keyed fonts to CID-keyed ones and an identity
mapping from GID to CID for all fonts, regardless of the previous mapping. Because of this, you can
always use the remapped GID as the CID for a glyph, and do not need to worry about the type of font
you are embedding.
# Example
In the example below, we remove all glyphs except the ones with IDs 68, 69, 70 from Noto Sans.
Expand Down
16 changes: 6 additions & 10 deletions tests/fuzz/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ttf_parser::GlyphId;
const NUM_ITERATIONS: usize = 200;

fn main() {
let exclude_fonts = vec![
let exclude_fonts = [
// Seems to be an invalid font for some reason, fonttools can't read it either.
// Glyph 822 doesn't seem to draw properly with ttf-parser... But most likely a ttf-parser
// bug because it does work with skrifa and freetype. fonttools ttx subset matches
Expand Down Expand Up @@ -54,7 +54,7 @@ fn main() {
let is_font_file = extension == Some("ttf") || extension == Some("otf");

if is_font_file {
match run_test(&path, &mut rng, &ft_library) {
match run_test(path, &mut rng, &ft_library) {
Ok(_) => {}
Err(msg) => {
println!("Error while fuzzing {:?}: {:}", path.clone(), msg)
Expand Down Expand Up @@ -251,7 +251,7 @@ fn glyph_outlines_skrifa(
let glyph2 = new_face
.outline_glyphs()
.get(skrifa::GlyphId::new(new_glyph))
.expect(&format!("failed to find glyph {} in new face", glyph));
.unwrap_or_else(|| panic!("failed to find glyph {} in new face", glyph));
glyph2
.draw(settings, &mut sink2)
.map_err(|e| format!("failed to draw new glyph {}: {}", glyph, e))?;
Expand Down Expand Up @@ -303,7 +303,7 @@ fn glyph_outlines_freetype(
}
}

return Ok(());
Ok(())
}

fn glyph_outlines_ttf_parser(
Expand All @@ -317,19 +317,15 @@ fn glyph_outlines_ttf_parser(
let mut sink1 = Sink::default();
let mut sink2 = Sink::default();

if let Some(_) = old_face.outline_glyph(GlyphId(*glyph), &mut sink1) {
if old_face.outline_glyph(GlyphId(*glyph), &mut sink1).is_some() {
new_face.outline_glyph(GlyphId(new_glyph), &mut sink2);
if sink1 != sink2 {
return Err(*glyph);
} else {
return Ok(());
}
} else {
return Ok(());
}
}

return Ok(());
Ok(())
}

#[derive(Debug, Default, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions tests/scripts/gen-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def gen_cff_tests():

def cff_fonttools_impl(test_src, out_path, fn_name):
test_string = f"// This file was auto-generated by `{Path(__file__).name}`, do not edit manually.\n\n"
test_string += "#[allow(non_snake_case)]\n\n"
test_string += "#![allow(non_snake_case)]\n\n"
test_string += f"use crate::*;\n\n"

counters = {}
Expand Down Expand Up @@ -58,7 +58,7 @@ def cff_fonttools_impl(test_src, out_path, fn_name):

def gen_subset_tests():
test_string = f"// This file was auto-generated by `{Path(__file__).name}`, do not edit manually.\n\n"
test_string += "#[allow(non_snake_case)]\n\n"
test_string += "#![allow(non_snake_case)]\n\n"
test_string += f"use crate::*;\n\n"

counters = {}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/cff.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file was auto-generated by `gen-tests.py`, do not edit manually.

#[allow(non_snake_case)]
#![allow(non_snake_case)]

use crate::*;

Expand Down
2 changes: 1 addition & 1 deletion tests/src/font_tools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file was auto-generated by `gen-tests.py`, do not edit manually.

#[allow(non_snake_case)]
#![allow(non_snake_case)]

use crate::*;

Expand Down
4 changes: 2 additions & 2 deletions tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ fn glyph_outlines_skrifa(font_file: &str, gids: &str) {
let glyph2 = new_face
.outline_glyphs()
.get(skrifa::GlyphId::new(new_glyph))
.expect(&format!("failed to find glyph {} in new face", glyph));
.unwrap_or_else(|| panic!("failed to find glyph {} in new face", glyph));
glyph2.draw(settings, &mut sink2).unwrap();
assert_eq!(sink1, sink2, "glyph {} drawn with skrifa didn't match.", glyph);
}
Expand All @@ -308,7 +308,7 @@ fn glyph_outlines_ttf_parser(font_file: &str, gids: &str) {
let mut sink1 = Sink::default();
let mut sink2 = Sink::default();

if let Some(_) = old_face.outline_glyph(GlyphId(glyph), &mut sink1) {
if old_face.outline_glyph(GlyphId(glyph), &mut sink1).is_some() {
new_face.outline_glyph(GlyphId(new_glyph), &mut sink2);
assert_eq!(
sink1, sink2,
Expand Down
2 changes: 1 addition & 1 deletion tests/src/subsets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file was auto-generated by `gen-tests.py`, do not edit manually.

#[allow(non_snake_case)]
#![allow(non_snake_case)]

use crate::*;

Expand Down

0 comments on commit 906da09

Please sign in to comment.