Skip to content

Commit

Permalink
[rust-edition] 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
aslpavel committed Feb 21, 2025
1 parent 795671f commit 2e0adf9
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "rasterize"
version = "0.6.1"
version = "0.6.2"
authors = ["Pavel Aslanov <[email protected]>"]
description = "Simple and small 2D rendering library"
edition = "2021"
edition = "2024"
include = [
"**/*.rs",
"src/svg-colors.txt",
Expand Down
2 changes: 1 addition & 1 deletion benches/color_bench.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use criterion::{Criterion, Throughput, black_box, criterion_group, criterion_main};
use rasterize::{
linear_to_srgb,
simd::{f32x4, l2s},
Expand Down
2 changes: 1 addition & 1 deletion benches/rasterize_bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(warnings)]

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use criterion::{BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main};
use rasterize::*;
use std::{
fs::File,
Expand Down
4 changes: 2 additions & 2 deletions benches/scene_bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![deny(warnings)]
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use rasterize::{BBox, FillRule, LinColor, Path, Point, Scalar, Scene, Size, Transform, RGBA};
use criterion::{Criterion, Throughput, criterion_group, criterion_main};
use rasterize::{BBox, FillRule, LinColor, Path, Point, RGBA, Scalar, Scene, Size, Transform};
use std::sync::Arc;

fn many_cirles_benchmark(c: &mut Criterion) {
Expand Down
10 changes: 8 additions & 2 deletions examples/hatchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,14 @@ fn generate_font(
glyphs: impl IntoIterator<Item = Glyph>,
) -> Result<(), std::io::Error> {
writeln!(out, "<?xml version=\"1.0\" standalone=\"no\"?>")?;
writeln!(out, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" >")?;
writeln!(out, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\"> ")?;
writeln!(
out,
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" >"
)?;
writeln!(
out,
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\"> "
)?;
writeln!(out, "<defs>")?;
writeln!(out, " <font horiz-adv-x=\"500\">")?;
writeln!(out, " <font-face")?;
Expand Down
2 changes: 1 addition & 1 deletion examples/prebench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{sync::Arc, time::Instant};

use rasterize::{
BBox, FillRule, Image, LinColor, Path, Point, Scalar, Scene, Size, Transform, RGBA,
BBox, FillRule, Image, LinColor, Path, Point, RGBA, Scalar, Scene, Size, Transform,
};

type Error = Box<dyn std::error::Error>;
Expand Down
2 changes: 1 addition & 1 deletion examples/rasterize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
io::{BufWriter, Read},
sync::Arc,
};
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan};

type Error = Box<dyn std::error::Error>;

Expand Down
2 changes: 1 addition & 1 deletion examples/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
io::{BufReader, BufWriter},
};
use tracing::debug_span;
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan};

type Error = Box<dyn std::error::Error>;

Expand Down
6 changes: 3 additions & 3 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{simd::f32x4, Paint, Point, Scalar, Transform, Units};
use crate::{Paint, Point, Scalar, Transform, Units, simd::f32x4};
use bytemuck::{Pod, Zeroable};
#[cfg(feature = "serde")]
use serde::{de::DeserializeSeed, Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Deserializer, Serialize, de::DeserializeSeed};
use std::{
collections::HashMap,
fmt,
Expand Down Expand Up @@ -115,7 +115,7 @@ impl RGBA {
};
let mut hex = bytes
.chunks(2)
.map(|pair| Ok(digit(pair[0])? << 4 | digit(pair[1])?));
.map(|pair| Ok((digit(pair[0])? << 4) | digit(pair[1])?));
RGBA::new(
hex.next().unwrap_or(Ok(0))?,
hex.next().unwrap_or(Ok(0))?,
Expand Down
17 changes: 12 additions & 5 deletions src/curve.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! All the things you need to handle bezier curves
use crate::{
BBox, EPSILON, EllipArc, LineCap, LineJoin, Point, Scalar, StrokeStyle, SvgParserError,
SvgPathCmd, SvgPathParser, Transform,
utils::{
clamp, cubic_solve, integrate_quadrature, quadratic_solve, ArrayIter, M3x3, M4x4,
QUADRATURE_16, QUADRATURE_32,
ArrayIter, M3x3, M4x4, QUADRATURE_16, QUADRATURE_32, clamp, cubic_solve,
integrate_quadrature, quadratic_solve,
},
BBox, EllipArc, LineCap, LineJoin, Point, Scalar, StrokeStyle, SvgParserError, SvgPathCmd,
SvgPathParser, Transform, EPSILON,
};
use std::{fmt, io::Cursor, str::FromStr};

Expand Down Expand Up @@ -834,7 +834,14 @@ impl Curve for Cubic {
fn roots(&self) -> CurveRoots {
let mut result = CurveRoots::new();
// curve(t)_y = 0
let Self([Point([_, y0]), Point([_, y1]), Point([_, y2]), Point([_, y3])]) = *self;
let Self(
[
Point([_, y0]),
Point([_, y1]),
Point([_, y2]),
Point([_, y3]),
],
) = *self;
let a = -y0 + 3.0 * y1 - 3.0 * y2 + y3;
let b = 3.0 * y0 - 6.0 * y1 + 3.0 * y2;
let c = -3.0 * y0 + 3.0 * y1;
Expand Down
2 changes: 1 addition & 1 deletion src/ellipse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BBox, Cubic, Curve, CurveFlattenIter, Line, Point, Scalar, Transform, PI};
use crate::{BBox, Cubic, Curve, CurveFlattenIter, Line, PI, Point, Scalar, Transform};
use std::fmt;

/// Elliptical Arc
Expand Down
20 changes: 11 additions & 9 deletions src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{utils::clamp, Line, Size, SvgParserError};
use crate::{Line, Size, SvgParserError, utils::clamp};
use std::{
fmt,
ops::{Add, Div, Mul, Sub},
Expand Down Expand Up @@ -72,7 +72,7 @@ impl ScalarFormatter {

pub fn round_significant(value: f64, precision: usize) -> f64 {
if value.abs() < EPSILON {
return 0.0;
0.0
} else {
let shift = precision as i32 - value.abs().log10().ceil() as i32;
let shift_factor = 10_f64.powi(shift);
Expand Down Expand Up @@ -734,7 +734,7 @@ impl<'de> Deserialize<'de> for BBox {
#[cfg(test)]
mod tests {
use super::*;
use crate::{assert_approx_eq, Curve};
use crate::{Curve, assert_approx_eq};
type Error = Box<dyn std::error::Error>;

#[test]
Expand Down Expand Up @@ -801,16 +801,18 @@ mod tests {
assert!(tr2.apply(s2.min).is_close_to(Point::new(3.0, 10.0)));

let tr3 = Transform::fit_bbox(s1, d, Align::Mid);
assert!(tr3
.apply((s1.min + s1.max) / 2.0)
.is_close_to((d.min + d.max) / 2.0));
assert!(
tr3.apply((s1.min + s1.max) / 2.0)
.is_close_to((d.min + d.max) / 2.0)
);
assert!(tr3.apply(s1.min).is_close_to(Point::new(5.5, 5.0)));
assert!(tr3.apply(s1.max).is_close_to(Point::new(10.5, 15.0)));

let tr4 = Transform::fit_bbox(s2, d, Align::Mid);
assert!(tr4
.apply((s2.min + s2.max) / 2.0)
.is_close_to((d.min + d.max) / 2.0));
assert!(
tr4.apply((s2.min + s2.max) / 2.0)
.is_close_to((d.min + d.max) / 2.0)
);
assert!(tr4.apply(s2.min).is_close_to(Point::new(3.0, 7.5)));
assert!(tr4.apply(s2.max).is_close_to(Point::new(13.0, 12.5)));

Expand Down
4 changes: 2 additions & 2 deletions src/grad.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{utils::quadratic_solve, Color, LinColor, Paint, Point, Scalar, Transform, Units};
use crate::{Color, LinColor, Paint, Point, Scalar, Transform, Units, utils::quadratic_solve};
#[cfg(feature = "serde")]
use serde::{de, Deserialize, Serialize};
use serde::{Deserialize, Serialize, de};
use std::cmp::Ordering;

/// Gradient spread logic for the parameter smaller than 0 and greater than 1
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ pub use crate::rasterize::{
};
#[cfg(feature = "serde")]
pub use color::RGBADeserializer;
pub use color::{linear_to_srgb, srgb_to_linear, Color, ColorError, LinColor, RGBA, SVG_COLORS};
pub use color::{Color, ColorError, LinColor, RGBA, SVG_COLORS, linear_to_srgb, srgb_to_linear};
pub use curve::{
Cubic, Curve, CurveExtremities, CurveFlattenIter, CurveRoots, Line, Quad, Segment,
};
pub use ellipse::EllipArc;
pub use geometry::{
Align, BBox, Point, Scalar, ScalarFormat, ScalarFormatter, Transform, EPSILON, EPSILON_SQRT, PI,
Align, BBox, EPSILON, EPSILON_SQRT, PI, Point, Scalar, ScalarFormat, ScalarFormatter, Transform,
};
pub use grad::{GradLinear, GradRadial, GradSpread, GradStop, GradStops};
pub use image::{
Image, ImageIter, ImageMut, ImageMutIter, ImageMutRef, ImageOwned, ImageRef, Shape,
};
pub use path::{
FillRule, LineCap, LineJoin, Path, PathBuilder, StrokeStyle, SubPath, DEFAULT_FLATNESS,
DEFAULT_FLATNESS, FillRule, LineCap, LineJoin, Path, PathBuilder, StrokeStyle, SubPath,
};
pub use scene::{Layer, Scene};
pub use svg::{SvgParserError, SvgPathCmd, SvgPathParser};
8 changes: 4 additions & 4 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
curve::line_offset, rasterize::Rasterizer, utils::clamp, BBox, Cubic, Curve, EllipArc,
ImageMut, LinColor, Line, Paint, Point, Quad, Scalar, ScalarFormatter, Segment, Size,
SvgParserError, SvgPathParser, Transform, EPSILON,
BBox, Cubic, Curve, EPSILON, EllipArc, ImageMut, LinColor, Line, Paint, Point, Quad, Scalar,
ScalarFormatter, Segment, Size, SvgParserError, SvgPathParser, Transform, curve::line_offset,
rasterize::Rasterizer, utils::clamp,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -1092,7 +1092,7 @@ impl<'de> Deserialize<'de> for Path {
#[cfg(test)]
mod tests {
use super::*;
use crate::{assert_approx_eq, PI};
use crate::{PI, assert_approx_eq};

fn assert_path_eq(p0: &Path, p1: &Path) {
assert_eq!(format!("{:#}", p0), format!("{:#}", p1));
Expand Down
6 changes: 3 additions & 3 deletions src/rasterize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
//! - this method is slower
//! - but requires less memory
use crate::{
Color, Curve, FillRule, ImageMut, ImageOwned, LinColor, Line, Path, Point, Scalar, Transform,
DEFAULT_FLATNESS, EPSILON, EPSILON_SQRT,
Color, Curve, DEFAULT_FLATNESS, EPSILON, EPSILON_SQRT, FillRule, ImageMut, ImageOwned,
LinColor, Line, Path, Point, Scalar, Transform,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -939,7 +939,7 @@ fn split_at_zero_x(line: Line) -> (Line, Option<Line>) {
#[cfg(test)]
mod tests {
use super::*;
use crate::{assert_approx_eq, Image};
use crate::{Image, assert_approx_eq};
type Error = Box<dyn std::error::Error>;

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
utils::clamp, ArcPaint, BBox, Color, FillRule, Image, ImageMut, ImageOwned, LinColor, Paint,
Path, Rasterizer, Scalar, Shape, Size, StrokeStyle, Transform, Units,
ArcPaint, BBox, Color, FillRule, Image, ImageMut, ImageOwned, LinColor, Paint, Path,
Rasterizer, Scalar, Shape, Size, StrokeStyle, Transform, Units, utils::clamp,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -588,7 +588,7 @@ mod serde_paint {
use crate::{GradLinear, GradRadial, LinColor};

use super::{Arc, ArcPaint};
use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de, ser};
use serde_json::Value;

pub fn serialize<S>(paint: &ArcPaint, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Utility functions and types used across the library
use crate::{Scalar, EPSILON, PI};
use crate::{EPSILON, PI, Scalar};
use std::{fmt, iter::FusedIterator, ops::Mul};

/// Restrict value to a certain interval
Expand Down Expand Up @@ -400,7 +400,7 @@ pub(crate) fn is_default<T: Default + PartialEq>(val: &T) -> bool {
/// by adding `#[serde(with = "serde_from_str")]`
#[cfg(feature = "serde")]
pub(crate) mod serde_from_str {
use serde::{de, Deserialize, Deserializer, Serializer};
use serde::{Deserialize, Deserializer, Serializer, de};
use std::{borrow::Cow, fmt::Display, str::FromStr};

pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
Expand Down

0 comments on commit 2e0adf9

Please sign in to comment.