pub const PI: f32 = core::f32::consts::PI;
pub const TAU: f32 = core::f32::consts::TAU;
#[inline(always)]
pub fn abs_f32(x: f32) -> f32 {
f32::from_bits(x.to_bits() & 0x7FFF_FFFF)
}
#[inline]
pub fn floor_f32(x: f32) -> f32 {
{
x.floor()
}
}
#[inline]
pub fn ceil_f32(x: f32) -> f32 {
{
x.ceil()
}
}
#[inline]
pub fn round_f32(x: f32) -> f32 {
{
x.round()
}
}
#[inline]
pub fn sqrt_f32(x: f32) -> f32 {
{
if x <= 0.0 {
0.0
} else {
x.sqrt()
}
}
}
#[inline]
pub fn sin_cos_f32(rad: f32) -> (f32, f32) {
{
rad.sin_cos()
}
}
#[inline]
pub fn tan_f32(rad: f32) -> f32 {
let (s, c) = sin_cos_f32(rad);
if abs_f32(c) < 1e-6 {
if s >= 0.0 {
1e6
} else {
-1e6
}
} else {
s / c
}
}
#[inline]
pub fn atan2_f32(y: f32, x: f32) -> f32 {
{
y.atan2(x)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Transform2D {
pub a: f32,
pub b: f32,
pub c: f32,
pub d: f32,
pub e: f32,
pub f: f32,
}
impl Default for Transform2D {
#[inline(always)]
fn default() -> Self {
Self::IDENTITY
}
}
impl Transform2D {
pub const IDENTITY: Self = Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
};
#[inline(always)]
pub const fn new(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> Self {
Self { a, b, c, d, e, f }
}
#[inline(always)]
pub const fn translate(tx: f32, ty: f32) -> Self {
Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: tx,
f: ty,
}
}
#[inline(always)]
pub const fn scale(sx: f32, sy: f32) -> Self {
Self {
a: sx,
b: 0.0,
c: 0.0,
d: sy,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn rotate(deg: f32) -> Self {
let (s, c) = sin_cos_f32(deg * (PI / 180.0));
Self {
a: c,
b: s,
c: -s,
d: c,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn rotate_around(deg: f32, cx: f32, cy: f32) -> Self {
Self::translate(cx, cy)
.concat(Self::rotate(deg))
.concat(Self::translate(-cx, -cy))
}
#[inline]
pub fn skew_x(deg: f32) -> Self {
Self {
a: 1.0,
b: 0.0,
c: tan_f32(deg * (PI / 180.0)),
d: 1.0,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn skew_y(deg: f32) -> Self {
Self {
a: 1.0,
b: tan_f32(deg * (PI / 180.0)),
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
}
}
#[inline]
pub fn concat(self, rhs: Self) -> Self {
Self {
a: self.a * rhs.a + self.c * rhs.b,
b: self.b * rhs.a + self.d * rhs.b,
c: self.a * rhs.c + self.c * rhs.d,
d: self.b * rhs.c + self.d * rhs.d,
e: self.a * rhs.e + self.c * rhs.f + self.e,
f: self.b * rhs.e + self.d * rhs.f + self.f,
}
}
#[inline(always)]
pub fn apply(self, x: f32, y: f32) -> (f32, f32) {
(
self.a * x + self.c * y + self.e,
self.b * x + self.d * y + self.f,
)
}
#[inline]
pub fn approx_scale(self) -> f32 {
let sx = sqrt_f32(self.a * self.a + self.b * self.b);
let sy = sqrt_f32(self.c * self.c + self.d * self.d);
let avg = 0.5 * (sx + sy);
if avg > 1e-5 {
avg
} else {
1.0
}
}
pub fn parse_svg_attr(input: &str) -> Self {
let bytes = input.as_bytes();
let mut pos = 0usize;
let mut acc = Self::IDENTITY;
while pos < bytes.len() {
while pos < bytes.len() && is_sep(bytes[pos]) {
pos += 1;
}
if pos >= bytes.len() {
break;
}
let name_start = pos;
while pos < bytes.len() && bytes[pos].is_ascii_alphabetic() {
pos += 1;
}
let name = &input[name_start..pos];
while pos < bytes.len() && bytes[pos].is_ascii_whitespace() {
pos += 1;
}
if pos >= bytes.len() || bytes[pos] != b'(' {
pos += 1;
continue;
}
pos += 1; let args_start = pos;
while pos < bytes.len() && bytes[pos] != b')' {
pos += 1;
}
let args_str = &input[args_start..pos];
if pos < bytes.len() && bytes[pos] == b')' {
pos += 1;
}
let mut nums = [0.0f32; 6];
let count = parse_number_list(args_str, &mut nums);
let step = if name.eq_ignore_ascii_case("translate") {
if count >= 2 {
Self::translate(nums[0], nums[1])
} else if count == 1 {
Self::translate(nums[0], 0.0)
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("scale") {
if count >= 2 {
Self::scale(nums[0], nums[1])
} else if count == 1 {
Self::scale(nums[0], nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("rotate") {
if count >= 3 {
Self::rotate_around(nums[0], nums[1], nums[2])
} else if count >= 1 {
Self::rotate(nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("skewX") {
if count >= 1 {
Self::skew_x(nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("skewY") {
if count >= 1 {
Self::skew_y(nums[0])
} else {
Self::IDENTITY
}
} else if name.eq_ignore_ascii_case("matrix") && count >= 6 {
Self::new(nums[0], nums[1], nums[2], nums[3], nums[4], nums[5])
} else {
Self::IDENTITY
};
acc = acc.concat(step);
}
acc
}
}
#[inline(always)]
fn is_sep(b: u8) -> bool {
b == b' ' || b == b'\t' || b == b'\r' || b == b'\n' || b == b','
}
pub(crate) fn parse_number_list(s: &str, out: &mut [f32]) -> usize {
let mut count = 0usize;
let mut cur = s;
while count < out.len() {
let Some((val, rest)) = parse_next_f32(cur) else {
break;
};
out[count] = val;
count += 1;
cur = rest;
}
count
}
pub(crate) fn parse_next_f32(s: &str) -> Option<(f32, &str)> {
let bytes = s.as_bytes();
let mut i = 0usize;
while i < bytes.len() && is_sep(bytes[i]) {
i += 1;
}
if i >= bytes.len() {
return None;
}
let start = i;
if bytes[i] == b'+' || bytes[i] == b'-' {
i += 1;
}
let mut has_digits = false;
while i < bytes.len() && bytes[i].is_ascii_digit() {
has_digits = true;
i += 1;
}
if i < bytes.len() && bytes[i] == b'.' {
i += 1;
while i < bytes.len() && bytes[i].is_ascii_digit() {
has_digits = true;
i += 1;
}
}
if !has_digits {
return None;
}
if i < bytes.len() && (bytes[i] == b'e' || bytes[i] == b'E') {
let exp_pos = i;
i += 1;
if i < bytes.len() && (bytes[i] == b'+' || bytes[i] == b'-') {
i += 1;
}
let mut exp_digits = false;
while i < bytes.len() && bytes[i].is_ascii_digit() {
exp_digits = true;
i += 1;
}
if !exp_digits {
i = exp_pos;
}
}
let token = &s[start..i];
let val = token.parse::<f32>().ok()?;
Some((val, &s[i..]))
}
pub(crate) fn parse_svg_length(s: &str) -> Option<f32> {
let trimmed = s.trim();
if trimmed.is_empty() {
return None;
}
parse_next_f32(trimmed).map(|(v, _)| v)
}