refactor: address clippy warnings and improve code quality (#409)

- Fix string formatting with modern interpolation syntax
- Improve Debug implementation with finish_non_exhaustive()
- Fix function placement in shape.rs to avoid items_after_statements warning
- Use more idiomatic Rust patterns (map_or_else, next_back)
- Clean up conditional imports in vi.rs
- Convert multiple methods to `const` functions for optimization and consistency
- Introduce `core_maths` for enhanced no-std compatibility
- Update `Cargo.toml` for the new optional dependency and feature adjustments
This commit is contained in:
romanstingler 2025-08-11 21:58:59 +02:00 committed by GitHub
parent e80dbc3607
commit a2f1f4b2a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 282 additions and 299 deletions

View file

@ -77,7 +77,7 @@ pub trait Fallback: Send + Sync {
}
#[derive(Debug, Default)]
pub(crate) struct Fallbacks {
pub struct Fallbacks {
lists: Vec<&'static str>,
common_fallback_range: Range<usize>,
forbidden_fallback_range: Range<usize>,
@ -174,13 +174,14 @@ use log::warn as missing_warn;
// Default font gets None for both `weight_offset` and `script_non_matches`, and thus, it is
// always the first to be popped from the set.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct MonospaceFallbackInfo {
pub struct MonospaceFallbackInfo {
font_weight_diff: Option<u16>,
codepoint_non_matches: Option<usize>,
font_weight: u16,
id: fontdb::ID,
}
#[derive(Debug)]
pub struct FontFallbackIter<'a> {
font_system: &'a mut FontSystem,
font_match_keys: &'a [FontMatchKey],
@ -223,7 +224,7 @@ impl<'a> FontFallbackIter<'a> {
}
}
pub fn check_missing(&mut self, word: &str) {
pub fn check_missing(&self, word: &str) {
if self.end {
missing_warn!(
"Failed to find any fallback for {:?} locale '{}': '{}'",
@ -252,15 +253,16 @@ impl<'a> FontFallbackIter<'a> {
}
pub fn face_name(&self, id: fontdb::ID) -> &str {
if let Some(face) = self.font_system.db().face(id) {
if let Some((name, _)) = face.families.first() {
name
} else {
&face.post_script_name
}
} else {
"invalid font id"
}
self.font_system
.db()
.face(id)
.map_or("invalid font id", |face| {
if let Some((name, _)) = face.families.first() {
name
} else {
&face.post_script_name
}
})
}
pub fn shape_caches(&mut self) -> &mut ShapeBuffer {
@ -268,11 +270,10 @@ impl<'a> FontFallbackIter<'a> {
}
fn face_contains_family(&self, id: fontdb::ID, family_name: &str) -> bool {
if let Some(face) = self.font_system.db().face(id) {
face.families.iter().any(|(name, _)| name == family_name)
} else {
false
}
self.font_system
.db()
.face(id)
.is_some_and(|face| face.families.iter().any(|(name, _)| name == family_name))
}
fn default_font_match_key(&self) -> Option<&FontMatchKey> {
@ -304,7 +305,7 @@ impl<'a> FontFallbackIter<'a> {
'DEF_FAM: while self.default_i < self.default_families.len() {
self.default_i += 1;
let is_mono = self.default_families[self.default_i - 1] == &Family::Monospace;
let default_font_match_key = self.default_font_match_key().cloned();
let default_font_match_key = self.default_font_match_key().copied();
let word_chars_count = self.word.chars().count();
macro_rules! mk_mono_fallback_info {
@ -334,9 +335,8 @@ impl<'a> FontFallbackIter<'a> {
(false, Some(m_key)) => {
if let Some(font) = self.font_system.get_font(m_key.id, self.ideal_weight) {
return Some(font);
} else {
break 'DEF_FAM;
}
break 'DEF_FAM;
}
(true, None) => (),
(true, Some(m_key)) => {
@ -360,7 +360,7 @@ impl<'a> FontFallbackIter<'a> {
}
}
}
};
}
let mono_ids_for_scripts = if is_mono && !self.scripts.is_empty() {
let scripts = self.scripts.iter().filter_map(|script| {

View file

@ -27,16 +27,16 @@ impl Fallback for PlatformFallback {
}
// Fallbacks to use after any script specific fallbacks
fn common_fallback() -> &'static [&'static str] {
const fn common_fallback() -> &'static [&'static str] {
&[]
}
// Fallbacks to never use
fn forbidden_fallback() -> &'static [&'static str] {
const fn forbidden_fallback() -> &'static [&'static str] {
&[]
}
// Fallbacks to use per script
fn script_fallback(_script: Script, _locale: &str) -> &'static [&'static str] {
const fn script_fallback(_script: Script, _locale: &str) -> &'static [&'static str] {
&[]
}

View file

@ -27,7 +27,7 @@ impl Fallback for PlatformFallback {
}
// Fallbacks to use after any script specific fallbacks
fn common_fallback() -> &'static [&'static str] {
const fn common_fallback() -> &'static [&'static str] {
//TODO: abstract style (sans/serif/monospaced)
&[
/* Sans-serif fallbacks */
@ -49,7 +49,7 @@ fn common_fallback() -> &'static [&'static str] {
}
// Fallbacks to never use
fn forbidden_fallback() -> &'static [&'static str] {
const fn forbidden_fallback() -> &'static [&'static str] {
&[]
}

View file

@ -15,7 +15,7 @@ use alloc::vec::Vec;
use rustybuzz::Face as RustybuzzFace;
use self_cell::self_cell;
pub(crate) mod fallback;
pub mod fallback;
pub use fallback::{Fallback, PlatformFallback};
pub use self::system::*;
@ -58,7 +58,7 @@ impl fmt::Debug for Font {
}
impl Font {
pub fn id(&self) -> fontdb::ID {
pub const fn id(&self) -> fontdb::ID {
self.id
}
@ -119,9 +119,9 @@ impl Font {
let monospace_em_width = info
.monospaced
.then(|| {
let hor_advance = face.glyph_hor_advance(face.glyph_index(' ')?)? as f32;
let upem = face.units_per_em() as f32;
Some(hor_advance / upem)
let hor_advance = face.glyph_hor_advance(face.glyph_index(' ')?)?;
let upem = face.units_per_em();
Some(f32::from(hor_advance) / f32::from(upem))
})
.flatten();
@ -144,7 +144,7 @@ impl Font {
.cmap?
.subtables
.into_iter()
.filter(|subtable| subtable.is_unicode())
.filter(ttf_parser::cmap::Subtable::is_unicode)
.for_each(|subtable| {
unicode_codepoints.reserve(1024);
subtable.codepoints(|code_point| {
@ -194,7 +194,7 @@ impl Font {
.into_iter()
.find(|axis| axis.tag == ttf_parser::Tag::from_bytes(b"wght"))
{
let wght = (weight.0 as f32).clamp(axis.min_value, axis.max_value);
let wght = f32::from(weight.0).clamp(axis.min_value, axis.max_value);
let _ = face.set_variation(ttf_parser::Tag::from_bytes(b"wght"), wght);
}
face

View file

@ -127,7 +127,7 @@ impl fmt::Debug for FontSystem {
f.debug_struct("FontSystem")
.field("locale", &self.locale)
.field("db", &self.db)
.finish()
.finish_non_exhaustive()
}
}
@ -180,7 +180,7 @@ impl FontSystem {
HashMap::default();
if cfg!(feature = "monospace_fallback") {
monospace_font_ids.iter().for_each(|&id| {
for &id in &monospace_font_ids {
db.with_face_data(id, |font_data, face_index| {
let _ = ttf_parser::Face::parse(font_data, face_index).map(|face| {
face.tables()
@ -196,7 +196,7 @@ impl FontSystem {
})
});
});
});
}
}
let per_script_monospace_font_ids = per_script_monospace_font_ids
@ -211,9 +211,9 @@ impl FontSystem {
db,
monospace_font_ids,
per_script_monospace_font_ids,
font_cache: Default::default(),
font_matches_cache: Default::default(),
font_codepoint_support_info_cache: Default::default(),
font_cache: HashMap::default(),
font_matches_cache: HashMap::default(),
font_codepoint_support_info_cache: HashMap::default(),
monospace_fallbacks_buffer: BTreeSet::default(),
#[cfg(feature = "shape-run-cache")]
shape_run_cache: crate::ShapeRunCache::default(),
@ -234,7 +234,7 @@ impl FontSystem {
}
/// Get the database.
pub fn db(&self) -> &fontdb::Database {
pub const fn db(&self) -> &fontdb::Database {
&self.db
}
@ -258,15 +258,14 @@ impl FontSystem {
unsafe {
self.db.make_shared_face_data(id);
}
match Font::new(&self.db, id, weight) {
Some(font) => Some(Arc::new(font)),
None => {
log::warn!(
"failed to load font '{}'",
self.db.face(id)?.post_script_name
);
None
}
if let Some(font) = Font::new(&self.db, id, weight) {
Some(Arc::new(font))
} else {
log::warn!(
"failed to load font '{}'",
self.db.face(id)?.post_script_name
);
None
}
})
.clone()