refactor: pedantic cargo clippy, do not use Results for always-succeeding functions

This commit is contained in:
Aleksander
2025-08-16 21:27:47 +02:00
parent 1f8960033c
commit 481db7f23c
45 changed files with 320 additions and 310 deletions

View File

@@ -23,29 +23,31 @@ pub enum CustomGlyphContent {
impl CustomGlyphContent {
pub fn from_bin_svg(data: &[u8]) -> anyhow::Result<Self> {
let tree = Tree::from_data(data, &Options::default())?;
Ok(CustomGlyphContent::Svg(Box::new(tree)))
Ok(Self::Svg(Box::new(tree)))
}
pub fn from_bin_raster(data: &[u8]) -> anyhow::Result<Self> {
let image = image::load_from_memory(data)?.into_rgba8();
Ok(CustomGlyphContent::Image(image))
Ok(Self::Image(image))
}
#[allow(clippy::case_sensitive_file_extension_comparisons)]
pub fn from_assets(provider: &mut Box<dyn AssetProvider>, path: &str) -> anyhow::Result<Self> {
let data = provider.load_from_path(path)?;
if path.ends_with(".svg") || path.ends_with(".svgz") {
Ok(CustomGlyphContent::from_bin_svg(&data)?)
Ok(Self::from_bin_svg(&data)?)
} else {
Ok(CustomGlyphContent::from_bin_raster(&data)?)
Ok(Self::from_bin_raster(&data)?)
}
}
#[allow(clippy::case_sensitive_file_extension_comparisons)]
pub fn from_file(path: &str) -> anyhow::Result<Self> {
let data = std::fs::read(path)?;
if path.ends_with(".svg") || path.ends_with(".svgz") {
Ok(CustomGlyphContent::from_bin_svg(&data)?)
Ok(Self::from_bin_svg(&data)?)
} else {
Ok(CustomGlyphContent::from_bin_raster(&data)?)
Ok(Self::from_bin_raster(&data)?)
}
}
}
@@ -107,7 +109,7 @@ pub struct CustomGlyph {
}
impl CustomGlyph {
pub fn new(data: CustomGlyphData) -> Self {
pub const fn new(data: CustomGlyphData) -> Self {
Self {
data,
left: 0.0,
@@ -156,10 +158,10 @@ pub struct RasterizedCustomGlyph {
}
impl RasterizedCustomGlyph {
pub(super) fn try_from(input: &RasterizeCustomGlyphRequest) -> Option<RasterizedCustomGlyph> {
pub(super) fn try_from(input: &RasterizeCustomGlyphRequest) -> Option<Self> {
match input.data.content.as_ref() {
CustomGlyphContent::Svg(tree) => rasterize_svg(tree, input),
CustomGlyphContent::Image(data) => rasterize_image(data),
CustomGlyphContent::Image(data) => Some(rasterize_image(data)),
}
}
@@ -212,7 +214,7 @@ pub enum ContentType {
impl ContentType {
/// The number of bytes per pixel for this content type
pub fn bytes_per_pixel(&self) -> usize {
pub const fn bytes_per_pixel(&self) -> usize {
match self {
Self::Color => 4,
Self::Mask => 1,
@@ -226,10 +228,10 @@ fn rasterize_svg(
) -> Option<RasterizedCustomGlyph> {
// Calculate the scale based on the "glyph size".
let svg_size = tree.size();
let scale_x = input.width as f32 / svg_size.width();
let scale_y = input.height as f32 / svg_size.height();
let scale_x = f32::from(input.width) / svg_size.width();
let scale_y = f32::from(input.height) / svg_size.height();
let mut pixmap = resvg::tiny_skia::Pixmap::new(input.width as u32, input.height as u32)?;
let mut pixmap = resvg::tiny_skia::Pixmap::new(u32::from(input.width), u32::from(input.height))?;
let mut transform = resvg::usvg::Transform::from_scale(scale_x, scale_y);
// Offset the glyph by the subpixel amount.
@@ -249,11 +251,11 @@ fn rasterize_svg(
})
}
fn rasterize_image(image: &RgbaImage) -> Option<RasterizedCustomGlyph> {
Some(RasterizedCustomGlyph {
fn rasterize_image(image: &RgbaImage) -> RasterizedCustomGlyph {
RasterizedCustomGlyph {
data: image.to_vec(),
content_type: ContentType::Color,
width: image.width() as _,
height: image.height() as _,
})
}
}

View File

@@ -20,10 +20,10 @@ pub static FONT_SYSTEM: LazyLock<Mutex<FontSystem>> =
pub static SWASH_CACHE: LazyLock<Mutex<SwashCache>> =
LazyLock::new(|| Mutex::new(SwashCache::new()));
/// Used in case no font_size is defined
/// Used in case no `font_size` is defined
const DEFAULT_FONT_SIZE: f32 = 14.;
/// In case no line_height is defined, use font_size * DEFAULT_LINE_HEIGHT_RATIO
/// In case no `line_height` is defined, use `font_size` * `DEFAULT_LINE_HEIGHT_RATIO`
const DEFAULT_LINE_HEIGHT_RATIO: f32 = 1.43;
pub(crate) const DEFAULT_METRICS: Metrics = Metrics::new(
@@ -55,7 +55,7 @@ impl From<&TextStyle> for Metrics {
fn from(style: &TextStyle) -> Self {
let font_size = style.size.unwrap_or(DEFAULT_FONT_SIZE);
Metrics {
Self {
font_size,
line_height: style
.size
@@ -67,9 +67,9 @@ impl From<&TextStyle> for Metrics {
impl From<&TextStyle> for Wrap {
fn from(value: &TextStyle) -> Self {
if value.wrap {
Wrap::WordOrGlyph
Self::WordOrGlyph
} else {
Wrap::None
Self::None
}
}
}
@@ -84,10 +84,10 @@ pub enum FontStyle {
}
impl From<FontStyle> for Style {
fn from(value: FontStyle) -> Style {
fn from(value: FontStyle) -> Self {
match value {
FontStyle::Normal => Style::Normal,
FontStyle::Italic => Style::Italic,
FontStyle::Normal => Self::Normal,
FontStyle::Italic => Self::Italic,
}
}
}
@@ -100,10 +100,10 @@ pub enum FontWeight {
}
impl From<FontWeight> for Weight {
fn from(value: FontWeight) -> Weight {
fn from(value: FontWeight) -> Self {
match value {
FontWeight::Normal => Weight::NORMAL,
FontWeight::Bold => Weight::BOLD,
FontWeight::Normal => Self::NORMAL,
FontWeight::Bold => Self::BOLD,
}
}
}
@@ -119,20 +119,20 @@ pub enum HorizontalAlign {
}
impl From<HorizontalAlign> for Align {
fn from(value: HorizontalAlign) -> Align {
fn from(value: HorizontalAlign) -> Self {
match value {
HorizontalAlign::Left => Align::Left,
HorizontalAlign::Right => Align::Right,
HorizontalAlign::Center => Align::Center,
HorizontalAlign::Justified => Align::Justified,
HorizontalAlign::End => Align::End,
HorizontalAlign::Left => Self::Left,
HorizontalAlign::Right => Self::Right,
HorizontalAlign::Center => Self::Center,
HorizontalAlign::Justified => Self::Justified,
HorizontalAlign::End => Self::End,
}
}
}
impl From<drawing::Color> for cosmic_text::Color {
fn from(value: drawing::Color) -> cosmic_text::Color {
cosmic_text::Color::rgba(
fn from(value: drawing::Color) -> Self {
Self::rgba(
(value.r * 255.999) as _,
(value.g * 255.999) as _,
(value.b * 255.999) as _,
@@ -142,12 +142,12 @@ impl From<drawing::Color> for cosmic_text::Color {
}
impl From<cosmic_text::Color> for drawing::Color {
fn from(value: cosmic_text::Color) -> drawing::Color {
drawing::Color::new(
value.r() as f32 / 255.999,
value.g() as f32 / 255.999,
value.b() as f32 / 255.999,
value.a() as f32 / 255.999,
fn from(value: cosmic_text::Color) -> Self {
Self::new(
f32::from(value.r()) / 255.999,
f32::from(value.g()) / 255.999,
f32::from(value.b()) / 255.999,
f32::from(value.a()) / 255.999,
)
}
}

View File

@@ -21,7 +21,7 @@ use super::{
};
use crate::gfx::{BLEND_ALPHA, WGfx, pipeline::WGfxPipeline};
/// Pipeline & shaders to be reused between TextRenderer instances
/// Pipeline & shaders to be reused between `TextRenderer` instances
#[derive(Clone)]
pub struct TextPipeline {
pub(super) gfx: Arc<WGfx>,
@@ -34,8 +34,8 @@ impl TextPipeline {
let frag = frag_atlas::load(gfx.device.clone())?;
let pipeline = gfx.create_pipeline::<GlyphVertex>(
vert,
frag,
&vert,
&frag,
format,
Some(BLEND_ALPHA),
PrimitiveTopology::TriangleStrip,
@@ -133,7 +133,7 @@ impl InnerAtlas {
})
}
fn descriptor_set(kind: Kind) -> usize {
const fn descriptor_set(kind: Kind) -> usize {
match kind {
Kind::Color => 0,
Kind::Mask => 1,
@@ -176,7 +176,7 @@ impl InnerAtlas {
}
#[allow(dead_code)]
pub fn num_channels(&self) -> usize {
pub const fn num_channels(&self) -> usize {
self.kind.num_channels()
}
@@ -185,13 +185,14 @@ impl InnerAtlas {
font_system: &mut FontSystem,
cache: &mut SwashCache,
) -> anyhow::Result<bool> {
const GROWTH_FACTOR: u32 = 2;
if self.size >= self.max_texture_dimension_2d {
return Ok(false);
}
// Grow each dimension by a factor of 2. The growth factor was chosen to match the growth
// factor of `Vec`.`
const GROWTH_FACTOR: u32 = 2;
let new_size = (self.size * GROWTH_FACTOR).min(self.max_texture_dimension_2d);
log::info!("Grow {:?} atlas {} → {new_size}", self.kind, self.size);
@@ -230,11 +231,11 @@ impl InnerAtlas {
GlyphonCacheKey::Custom(cache_key) => (cache_key.width as usize, cache_key.height as usize),
};
let offset = [x as _, y as _, 0];
let offset = [x.into(), y.into(), 0];
cmd_buf.copy_image(
old_image.clone(),
&old_image.clone(),
offset,
image.clone(),
&image.clone(),
offset,
Some([width as _, height as _, 1]),
)?;
@@ -267,17 +268,17 @@ pub(super) enum Kind {
}
impl Kind {
fn num_channels(self) -> usize {
const fn num_channels(self) -> usize {
match self {
Kind::Mask => 1,
Kind::Color => 4,
Self::Mask => 1,
Self::Color => 4,
}
}
fn texture_format(self) -> Format {
const fn texture_format(self) -> Format {
match self {
Kind::Mask => Format::R8_UNORM,
Kind::Color => Format::R8G8B8A8_UNORM,
Self::Mask => Format::R8_UNORM,
Self::Color => Format::R8G8B8A8_UNORM,
}
}
}
@@ -326,7 +327,10 @@ impl TextAtlas {
Ok(did_grow)
}
pub(super) fn inner_for_content_mut(&mut self, content_type: ContentType) -> &mut InnerAtlas {
pub(super) const fn inner_for_content_mut(
&mut self,
content_type: ContentType,
) -> &mut InnerAtlas {
match content_type {
ContentType::Color => &mut self.color_atlas,
ContentType::Mask => &mut self.mask_atlas,

View File

@@ -52,6 +52,7 @@ impl TextRenderer {
}
/// Prepares all of the provided text areas for rendering.
#[allow(clippy::too_many_lines)]
pub fn prepare<'a>(
&mut self,
font_system: &mut FontSystem,
@@ -70,7 +71,7 @@ impl TextRenderer {
let bounds_max_x = text_area.bounds.right.min(resolution[0] as i32);
let bounds_max_y = text_area.bounds.bottom.min(resolution[1] as i32);
for glyph in text_area.custom_glyphs.iter() {
for glyph in text_area.custom_glyphs {
let x = text_area.left + (glyph.left * text_area.scale);
let y = text_area.top + (glyph.top * text_area.scale);
let width = (glyph.width * text_area.scale).round() as u16;
@@ -102,7 +103,7 @@ impl TextRenderer {
let color = glyph.color.unwrap_or(text_area.default_color);
if let Some(glyph_to_render) = prepare_glyph(
PrepareGlyphParams {
&mut PrepareGlyphParams {
label_pos: Vec2::new(text_area.left, text_area.top),
x,
y,
@@ -114,7 +115,7 @@ impl TextRenderer {
font_system,
model_buffer: &mut self.model_buffer,
scale_factor: text_area.scale,
glyph_scale: width as f32 / cached_width as f32,
glyph_scale: f32::from(width) / f32::from(cached_width),
bounds_min_x,
bounds_min_y,
bounds_max_x,
@@ -169,7 +170,7 @@ impl TextRenderer {
.take_while(is_run_visible);
for run in layout_runs {
for glyph in run.glyphs.iter() {
for glyph in run.glyphs {
let physical_glyph = glyph.physical((text_area.left, text_area.top), text_area.scale);
let color = match glyph.color_opt {
@@ -178,7 +179,7 @@ impl TextRenderer {
};
if let Some(glyph_to_render) = prepare_glyph(
PrepareGlyphParams {
&mut PrepareGlyphParams {
label_pos: Vec2::new(text_area.left, text_area.top),
x: physical_glyph.x,
y: physical_glyph.y,
@@ -323,9 +324,9 @@ struct PrepareGlyphParams<'a> {
depth: f32,
}
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_lines)]
fn prepare_glyph(
par: PrepareGlyphParams,
par: &mut PrepareGlyphParams,
get_glyph_image: impl FnOnce(&mut SwashCache, &mut FontSystem) -> Option<GetGlyphImageResult>,
) -> anyhow::Result<Option<GlyphVertex>> {
let gfx = par.atlas.common.gfx.clone();
@@ -347,33 +348,31 @@ fn prepare_glyph(
// Find a position in the packer
let allocation = loop {
match inner.try_allocate(image.width as usize, image.height as usize) {
Some(a) => break a,
None => {
if !par
.atlas
.grow(par.font_system, par.cache, image.content_type)?
{
anyhow::bail!(
"Atlas full. atlas: {:?} cache_key: {:?}",
image.content_type,
par.cache_key
);
}
inner = par.atlas.inner_for_content_mut(image.content_type);
}
if let Some(a) = inner.try_allocate(image.width as usize, image.height as usize) {
break a;
}
if !par
.atlas
.grow(par.font_system, par.cache, image.content_type)?
{
anyhow::bail!(
"Atlas full. atlas: {:?} cache_key: {:?}",
image.content_type,
par.cache_key
);
}
inner = par.atlas.inner_for_content_mut(image.content_type);
};
let atlas_min = allocation.rectangle.min;
let mut cmd_buf = gfx.create_xfer_command_buffer(CommandBufferUsage::OneTimeSubmit)?;
cmd_buf.update_image(
inner.image_view.image().clone(),
inner.image_view.image(),
&image.data,
[atlas_min.x as _, atlas_min.y as _, 0],
Some([image.width as _, image.height as _, 1]),
Some([image.width.into(), image.height.into(), 1]),
)?;
cmd_buf.build_and_execute_now()?; //TODO: do not wait for fence here
@@ -406,16 +405,16 @@ fn prepare_glyph(
})
};
let mut x = par.x + details.left as i32;
let mut y = (par.line_y * par.scale_factor).round() as i32 + par.y - details.top as i32;
let mut x = par.x + i32::from(details.left);
let mut y = (par.line_y * par.scale_factor).round() as i32 + par.y - i32::from(details.top);
let (mut atlas_x, mut atlas_y, content_type) = match details.gpu_cache {
GpuCacheStatus::InAtlas { x, y, content_type } => (x, y, content_type),
GpuCacheStatus::SkipRasterization => return Ok(None),
};
let mut glyph_width = details.width as i32;
let mut glyph_height = details.height as i32;
let mut glyph_width = i32::from(details.width);
let mut glyph_height = i32::from(details.height);
// Starts beyond right edge or ends beyond left edge
let max_x = x + glyph_width;
@@ -429,7 +428,7 @@ fn prepare_glyph(
return Ok(None);
}
// Clip left ege
// Clip left edge
if x < par.bounds_min_x {
let right_shift = par.bounds_min_x - x;