mod damage;
mod hit;
use std::sync::Arc;
pub use self::{
damage::{Damage, DamageScan},
hit::{point_in_path, point_in_stroke},
};
use crate::{
js::{canvas::*, ops::JsOps},
style::{Color, FontVariant},
ui::PropEnum,
};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Num {
Const(f32),
Slot(u16),
}
impl Num {
#[inline]
pub fn get(self, slot: impl Fn(u16) -> f32) -> f32 {
match self {
Num::Const(v) => v,
Num::Slot(s) => slot(s),
}
}
}
impl Default for Num {
fn default() -> Self {
Num::Const(0.0)
}
}
#[derive(Debug, Clone, PartialEq, JsOps)]
#[ops(cx = Drawing)]
pub enum PathOp {
#[op(code = CV_MOVE_TO, method = "moveTo", params = "x, y")]
MoveTo(Num, Num),
#[op(code = CV_LINE_TO, method = "lineTo", params = "x, y")]
LineTo(Num, Num),
#[op(code = CV_QUAD_TO, method = "quadraticCurveTo", params = "cpx, cpy, x, y")]
QuadTo([Num; 4]),
#[op(
code = CV_CUBIC_TO,
method = "bezierCurveTo",
params = "cp1x, cp1y, cp2x, cp2y, x, y"
)]
CubicTo([Num; 6]),
#[op(
code = CV_ARC,
method = "arc",
params = "x, y, radius, startAngle, endAngle, counterclockwise = false"
)]
Arc {
x: Num,
y: Num,
radius: Num,
start: Num,
end: Num,
ccw: bool,
},
#[op(code = CV_ARC_TO, method = "arcTo", params = "x1, y1, x2, y2, radius")]
ArcTo([Num; 5]),
#[op(
code = CV_ELLIPSE,
method = "ellipse",
params = "x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise = false"
)]
Ellipse {
geometry: [Num; 5],
start: Num,
end: Num,
ccw: bool,
},
#[op(code = CV_RECT, method = "rect", params = "x, y, w, h")]
Rect([Num; 4]),
#[op(code = CV_ROUND_RECT)]
RoundRect([Num; 4], [Num; 4]),
#[op(code = CV_CLOSE, method = "closePath")]
Close,
}
pub type Path = Arc<[PathOp]>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PropEnum)]
pub enum FillRule {
#[default]
#[prop(rename = "nonzero")]
NonZero,
#[prop(rename = "evenodd")]
EvenOdd,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PropEnum)]
pub enum LineCap {
#[default]
Butt,
Round,
Square,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PropEnum)]
pub enum LineJoin {
#[default]
Miter,
Round,
Bevel,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GradientShape {
Linear([f32; 4]),
Radial([f32; 6]),
Conic([f32; 3]),
}
#[derive(Debug, Clone, PartialEq)]
pub struct CanvasGradient {
pub shape: GradientShape,
pub stops: Vec<(f32, Color)>,
}
impl CanvasGradient {
fn new(shape: GradientShape) -> Self {
Self {
shape,
stops: Vec::new(),
}
}
pub fn linear(x0: f32, y0: f32, x1: f32, y1: f32) -> Self {
Self::new(GradientShape::Linear([x0, y0, x1, y1]))
}
pub fn radial(x0: f32, y0: f32, r0: f32, x1: f32, y1: f32, r1: f32) -> Self {
Self::new(GradientShape::Radial([x0, y0, r0, x1, y1, r1]))
}
pub fn conic(start_angle: f32, x: f32, y: f32) -> Self {
Self::new(GradientShape::Conic([start_angle, x, y]))
}
pub fn add_color_stop(mut self, offset: f32, color: Color) -> Self {
let offset = offset.clamp(0.0, 1.0);
let at = self.stops.partition_point(|(o, _)| *o <= offset);
self.stops.insert(at, (offset, color));
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Paint {
Color(Color),
Slot(u16),
Gradient(Arc<CanvasGradient>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[allow(missing_docs)]
pub enum Composite {
#[default]
SourceOver,
SourceIn,
SourceOut,
SourceAtop,
DestinationOver,
DestinationIn,
DestinationOut,
DestinationAtop,
Lighter,
Copy,
Xor,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorDodge,
ColorBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
Hue,
Saturation,
Color,
Luminosity,
}
impl Composite {
pub fn parse(name: &str) -> Option<Composite> {
use Composite::*;
Some(match name {
"source-over" => SourceOver,
"source-in" => SourceIn,
"source-out" => SourceOut,
"source-atop" => SourceAtop,
"destination-over" => DestinationOver,
"destination-in" => DestinationIn,
"destination-out" => DestinationOut,
"destination-atop" => DestinationAtop,
"lighter" => Lighter,
"copy" => Copy,
"xor" => Xor,
"multiply" => Multiply,
"screen" => Screen,
"overlay" => Overlay,
"darken" => Darken,
"lighten" => Lighten,
"color-dodge" => ColorDodge,
"color-burn" => ColorBurn,
"hard-light" => HardLight,
"soft-light" => SoftLight,
"difference" => Difference,
"exclusion" => Exclusion,
"hue" => Hue,
"saturation" => Saturation,
"color" => Color,
"luminosity" => Luminosity,
_ => return None,
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CanvasFont {
pub size: f32,
pub weight: u16,
pub italic: bool,
pub family: Option<Arc<str>>,
pub variant: FontVariant,
}
impl Default for CanvasFont {
fn default() -> Self {
Self {
size: 10.0,
weight: 400,
italic: false,
family: None,
variant: FontVariant::default(),
}
}
}
impl CanvasFont {
pub fn parse(s: &str) -> Option<CanvasFont> {
let mut font = CanvasFont {
size: 0.0,
..CanvasFont::default()
};
let mut rest = s.trim();
loop {
let (word, tail) = match rest.split_once(char::is_whitespace) {
Some((w, t)) => (w, t.trim_start()),
None => (rest, ""),
};
if let Some(size) = font_size(word) {
font.size = size;
rest = tail;
break;
}
match word {
"italic" | "oblique" => font.italic = true,
"normal" | "condensed" | "expanded" => {}
w if let Some(v) = FontVariant::parse(w) => font.variant.0 |= v.0,
"bold" | "bolder" => font.weight = 700,
"lighter" => font.weight = 300,
w => match w.parse::<u16>() {
Ok(n) if (1..=1000).contains(&n) => font.weight = n,
_ => return None,
},
}
if tail.is_empty() {
return None;
}
rest = tail;
}
let family = rest
.split(',')
.next()
.map(|f| f.trim().trim_matches(['"', '\'']))
.filter(|f| !f.is_empty())?;
font.family = match family {
"system-ui" | "sans-serif" | "-apple-system" | "ui-sans-serif" => None,
f => Some(f.into()),
};
Some(font)
}
}
fn font_size(word: &str) -> Option<f32> {
let word = word.split('/').next()?;
let (num, scale) = if let Some(n) = word.strip_suffix("px") {
(n, 1.0)
} else if let Some(n) = word.strip_suffix("rem").or_else(|| word.strip_suffix("em")) {
(n, 16.0)
} else {
(word.strip_suffix("pt")?, 4.0 / 3.0)
};
num.parse::<f32>()
.ok()
.filter(|v| *v > 0.0)
.map(|v| v * scale)
}
impl CanvasFont {
pub fn with_numeric(&self, numeric: FontVariant) -> CanvasFont {
let mut font = self.clone();
if numeric.0 != 0 {
font.variant = FontVariant(font.variant.0 & FontVariant::SMALL_CAPS | numeric.0);
}
font
}
pub fn text_style(&self) -> crate::text::ResolvedTextStyle {
crate::text::ResolvedTextStyle {
font_size: self.size,
font_weight: self.weight,
italic: self.italic,
font_variant: self.variant,
font_family: self.family.as_deref().map(crate::style::FontFamily::new),
..Default::default()
}
}
}
pub fn measure_line(engine: &impl crate::text::TextEngine, font: &CanvasFont, text: &str) -> f32 {
let mut attributed = crate::text::AttributedText {
no_wrap: true,
max_lines: 1,
..Default::default()
};
attributed.push(text, font.text_style());
if attributed.text.is_empty() {
return 0.0;
}
let prepared = engine.prepare(&attributed);
engine.measure(&prepared, None).width
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PropEnum)]
pub enum CanvasTextAlign {
#[default]
Start,
End,
Left,
Right,
Center,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PropEnum)]
pub enum CanvasTextBaseline {
#[default]
Alphabetic,
Top,
Hanging,
Middle,
Ideographic,
Bottom,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CanvasImage {
pub uri: Arc<str>,
}
#[derive(Debug, Clone, PartialEq, JsOps)]
#[ops(cx = Drawing)]
pub enum CanvasOp {
#[op(code = CV_SAVE)]
Save,
#[op(code = CV_RESTORE)]
Restore,
#[op(code = CV_TRANSLATE, method = "translate", params = "x, y")]
Translate(Num, Num),
#[op(code = CV_SCALE, method = "scale", params = "x, y")]
Scale(Num, Num),
#[op(code = CV_ROTATE, method = "rotate", params = "angle")]
Rotate(Num),
#[op(code = CV_TRANSFORM, method = "transform", params = "a, b, c, d, e, f")]
Transform([Num; 6]),
#[op(code = CV_SET_TRANSFORM, method = "setTransform", params = "a, b, c, d, e, f")]
SetTransform([Num; 6]),
#[op(code = CV_BEGIN_PATH, method = "beginPath")]
BeginPath,
#[op(nested)]
Path(PathOp),
#[op(code = CV_FILL)]
Fill(Option<Path>, FillRule),
#[op(code = CV_STROKE)]
Stroke(Option<Path>),
#[op(code = CV_CLIP)]
Clip(Option<Path>, FillRule),
#[op(code = CV_FILL_RECT, method = "fillRect", params = "x, y, w, h")]
FillRect([Num; 4]),
#[op(code = CV_STROKE_RECT, method = "strokeRect", params = "x, y, w, h")]
StrokeRect([Num; 4]),
#[op(code = CV_CLEAR_RECT, method = "clearRect", params = "x, y, w, h")]
ClearRect([Num; 4]),
#[op(code = CV_FILL_STYLE)]
FillStyle(Paint),
#[op(code = CV_STROKE_STYLE)]
StrokeStyle(Paint),
#[op(code = CV_LINE_WIDTH, property = "lineWidth", initial = "1")]
LineWidth(Num),
#[op(code = CV_LINE_CAP, property = "lineCap", initial = "\"butt\"")]
LineCap(LineCap),
#[op(code = CV_LINE_JOIN, property = "lineJoin", initial = "\"miter\"")]
LineJoin(LineJoin),
#[op(code = CV_MITER_LIMIT, property = "miterLimit", initial = "10", min = 1.0)]
MiterLimit(f32),
#[op(code = CV_LINE_DASH, with = crate::js::canvas::line_dash)]
LineDash(Arc<[f32]>),
#[op(code = CV_LINE_DASH_OFFSET, property = "lineDashOffset", initial = "0")]
LineDashOffset(Num),
#[op(code = CV_GLOBAL_ALPHA, property = "globalAlpha", initial = "1")]
GlobalAlpha(Num),
#[op(
code = CV_COMPOSITE,
property = "globalCompositeOperation",
initial = "\"source-over\""
)]
Composite(Composite),
#[op(code = CV_SHADOW_COLOR, property = "shadowColor", initial = "\"rgba(0,0,0,0)\"")]
ShadowColor(Color),
#[op(code = CV_SHADOW_BLUR, property = "shadowBlur", initial = "0", min = 0.0)]
ShadowBlur(f32),
#[op(code = CV_SHADOW_OFFSET)]
ShadowOffset(f32, f32),
#[op(code = CV_FONT, property = "font", initial = "\"10px sans-serif\"")]
Font(CanvasFont),
#[op(
code = CV_FONT_VARIANT_NUMERIC,
property = "fontVariantNumeric",
initial = "\"normal\""
)]
FontVariantNumeric(FontVariant),
#[op(code = CV_TEXT_ALIGN, property = "textAlign", initial = "\"start\"")]
TextAlign(CanvasTextAlign),
#[op(code = CV_TEXT_BASELINE, property = "textBaseline", initial = "\"alphabetic\"")]
TextBaseline(CanvasTextBaseline),
#[op(code = CV_FILL_TEXT)]
FillText {
text: Arc<str>,
x: Num,
y: Num,
max_width: Option<f32>,
},
#[op(code = CV_STROKE_TEXT)]
StrokeText {
text: Arc<str>,
x: Num,
y: Num,
max_width: Option<f32>,
},
#[op(code = CV_DRAW_IMAGE)]
DrawImage {
image: CanvasImage,
source: Option<[f32; 4]>,
dest: [Num; 4],
},
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Drawing {
pub ops: Vec<CanvasOp>,
pub slots: Vec<nsh_animation::NodeId>,
}
impl Drawing {
pub fn slot(&mut self, node: nsh_animation::NodeId) -> u16 {
match self.slots.iter().position(|&n| n == node) {
Some(i) => i as u16,
None => {
self.slots.push(node);
(self.slots.len() - 1) as u16
}
}
}
pub fn images(&self) -> impl Iterator<Item = &CanvasImage> {
self.ops.iter().filter_map(|op| match op {
CanvasOp::DrawImage { image, .. } => Some(image),
_ => None,
})
}
}
pub fn arc_sweep(start: f32, end: f32, ccw: bool) -> f32 {
use std::f32::consts::TAU;
if !ccw {
if end - start >= TAU {
TAU
} else {
(end - start).rem_euclid(TAU)
}
} else if start - end >= TAU {
-TAU
} else {
-(start - end).rem_euclid(TAU)
}
}
pub fn fit_radii(w: f32, h: f32, radii: [f32; 4]) -> [f32; 4] {
let r = radii.map(|r| r.max(0.0));
let (w, h) = (w.abs(), h.abs());
let sides = [r[0] + r[1], r[2] + r[3], r[0] + r[3], r[1] + r[2]];
let limits = [w, w, h, h];
let scale = sides
.iter()
.zip(limits)
.filter(|(s, _)| **s > 0.0)
.map(|(s, l)| l / s)
.fold(1.0f32, f32::min);
r.map(|r| r * scale)
}
pub fn svg_path(d: &str) -> Path {
parse_svg_path(d).into()
}
pub fn parse_svg_path(d: &str) -> Vec<PathOp> {
let mut out = Vec::new();
let mut p = PathParser {
s: d.as_bytes(),
pos: 0,
};
let (mut x, mut y) = (0f32, 0f32);
let (mut sx, mut sy) = (0f32, 0f32);
let mut last_cubic: Option<(f32, f32)> = None;
let mut last_quad: Option<(f32, f32)> = None;
let mut command = 0u8;
let c = |v: f32| Num::Const(v);
loop {
p.skip_separators();
if p.pos >= p.s.len() {
break;
}
let b = p.s[p.pos];
if b.is_ascii_alphabetic() {
command = b;
p.pos += 1;
} else if command == 0 {
break;
} else if command == b'M' {
command = b'L';
} else if command == b'm' {
command = b'l';
}
let rel = command.is_ascii_lowercase();
let (ox, oy) = if rel { (x, y) } else { (0.0, 0.0) };
let upper = command.to_ascii_uppercase();
let ok = (|| -> Option<()> {
match upper {
b'M' => {
let (nx, ny) = (p.number()? + ox, p.number()? + oy);
out.push(PathOp::MoveTo(c(nx), c(ny)));
(x, y, sx, sy) = (nx, ny, nx, ny);
last_cubic = None;
last_quad = None;
}
b'L' => {
let (nx, ny) = (p.number()? + ox, p.number()? + oy);
out.push(PathOp::LineTo(c(nx), c(ny)));
(x, y) = (nx, ny);
last_cubic = None;
last_quad = None;
}
b'H' => {
x = p.number()? + ox;
out.push(PathOp::LineTo(c(x), c(y)));
last_cubic = None;
last_quad = None;
}
b'V' => {
y = p.number()? + oy;
out.push(PathOp::LineTo(c(x), c(y)));
last_cubic = None;
last_quad = None;
}
b'C' | b'S' => {
let (x1, y1) = if upper == b'C' {
(p.number()? + ox, p.number()? + oy)
} else {
last_cubic.map_or((x, y), |(cx, cy)| (2.0 * x - cx, 2.0 * y - cy))
};
let (x2, y2) = (p.number()? + ox, p.number()? + oy);
let (nx, ny) = (p.number()? + ox, p.number()? + oy);
out.push(PathOp::CubicTo([x1, y1, x2, y2, nx, ny].map(c)));
last_cubic = Some((x2, y2));
last_quad = None;
(x, y) = (nx, ny);
}
b'Q' | b'T' => {
let (x1, y1) = if upper == b'Q' {
(p.number()? + ox, p.number()? + oy)
} else {
last_quad.map_or((x, y), |(cx, cy)| (2.0 * x - cx, 2.0 * y - cy))
};
let (nx, ny) = (p.number()? + ox, p.number()? + oy);
out.push(PathOp::QuadTo([x1, y1, nx, ny].map(c)));
last_quad = Some((x1, y1));
last_cubic = None;
(x, y) = (nx, ny);
}
b'A' => {
let rx = p.number()?.abs();
let ry = p.number()?.abs();
let rotation = p.number()?;
let large = p.flag()?;
let sweep = p.flag()?;
let (nx, ny) = (p.number()? + ox, p.number()? + oy);
arc_to_cubics(&mut out, (x, y), (rx, ry), rotation, large, sweep, (nx, ny));
(x, y) = (nx, ny);
last_cubic = None;
last_quad = None;
}
b'Z' => {
out.push(PathOp::Close);
(x, y) = (sx, sy);
last_cubic = None;
last_quad = None;
command = 0;
}
_ => return None,
}
Some(())
})();
if ok.is_none() {
break;
}
}
out
}
struct PathParser<'a> {
s: &'a [u8],
pos: usize,
}
impl PathParser<'_> {
fn skip_separators(&mut self) {
while self.pos < self.s.len()
&& (self.s[self.pos].is_ascii_whitespace() || self.s[self.pos] == b',')
{
self.pos += 1;
}
}
fn number(&mut self) -> Option<f32> {
self.skip_separators();
let start = self.pos;
let s = self.s;
let mut i = self.pos;
if i < s.len() && (s[i] == b'+' || s[i] == b'-') {
i += 1;
}
let mut digits = false;
while i < s.len() && s[i].is_ascii_digit() {
i += 1;
digits = true;
}
if i < s.len() && s[i] == b'.' {
i += 1;
while i < s.len() && s[i].is_ascii_digit() {
i += 1;
digits = true;
}
}
if !digits {
return None;
}
if i < s.len() && (s[i] == b'e' || s[i] == b'E') {
let mut j = i + 1;
if j < s.len() && (s[j] == b'+' || s[j] == b'-') {
j += 1;
}
if j < s.len() && s[j].is_ascii_digit() {
while j < s.len() && s[j].is_ascii_digit() {
j += 1;
}
i = j;
}
}
self.pos = i;
std::str::from_utf8(&s[start..i]).ok()?.parse().ok()
}
fn flag(&mut self) -> Option<bool> {
self.skip_separators();
let b = *self.s.get(self.pos)?;
self.pos += 1;
match b {
b'0' => Some(false),
b'1' => Some(true),
_ => None,
}
}
}
fn arc_to_cubics(
out: &mut Vec<PathOp>,
from: (f32, f32),
radii: (f32, f32),
rotation_deg: f32,
large: bool,
sweep: bool,
to: (f32, f32),
) {
let c = |v: f64| Num::Const(v as f32);
let (x1, y1) = (from.0 as f64, from.1 as f64);
let (x2, y2) = (to.0 as f64, to.1 as f64);
let (mut rx, mut ry) = (radii.0 as f64, radii.1 as f64);
if (x1 == x2 && y1 == y2) || rx == 0.0 || ry == 0.0 {
if x1 != x2 || y1 != y2 {
out.push(PathOp::LineTo(c(x2), c(y2)));
}
return;
}
let phi = (rotation_deg as f64).to_radians();
let (sin, cos) = phi.sin_cos();
let dx = (x1 - x2) / 2.0;
let dy = (y1 - y2) / 2.0;
let x1p = cos * dx + sin * dy;
let y1p = -sin * dx + cos * dy;
let lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
if lambda > 1.0 {
let s = lambda.sqrt();
rx *= s;
ry *= s;
}
let num = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
let den = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
let mut coef = (num / den).max(0.0).sqrt();
if large == sweep {
coef = -coef;
}
let cxp = coef * rx * y1p / ry;
let cyp = -coef * ry * x1p / rx;
let cx = cos * cxp - sin * cyp + (x1 + x2) / 2.0;
let cy = sin * cxp + cos * cyp + (y1 + y2) / 2.0;
let angle = |ux: f64, uy: f64, vx: f64, vy: f64| (ux * vy - uy * vx).atan2(ux * vx + uy * vy);
let theta1 = angle(1.0, 0.0, (x1p - cxp) / rx, (y1p - cyp) / ry);
let mut delta = angle(
(x1p - cxp) / rx,
(y1p - cyp) / ry,
(-x1p - cxp) / rx,
(-y1p - cyp) / ry,
);
if !sweep && delta > 0.0 {
delta -= std::f64::consts::TAU;
} else if sweep && delta < 0.0 {
delta += std::f64::consts::TAU;
}
let segments = (delta.abs() / std::f64::consts::FRAC_PI_2).ceil().max(1.0) as usize;
let step = delta / segments as f64;
let k = 4.0 / 3.0 * (step / 4.0).tan();
let point = |t: f64| {
let (s, co) = t.sin_cos();
(
cx + rx * co * cos - ry * s * sin,
cy + rx * co * sin + ry * s * cos,
)
};
let derivative = |t: f64| {
let (s, co) = t.sin_cos();
(-rx * s * cos - ry * co * sin, -rx * s * sin + ry * co * cos)
};
let mut t = theta1;
for i in 0..segments {
let t2 = t + step;
let (p1x, p1y) = point(t);
let (d1x, d1y) = derivative(t);
let (p2x, p2y) = if i + 1 == segments {
(x2, y2)
} else {
point(t2)
};
let (d2x, d2y) = derivative(t2);
out.push(PathOp::CubicTo([
c(p1x + k * d1x),
c(p1y + k * d1y),
c(p2x - k * d2x),
c(p2y - k * d2y),
c(p2x),
c(p2y),
]));
t = t2;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn consts(op: &PathOp) -> Vec<f32> {
let v = |n: &Num| match n {
Num::Const(v) => *v,
Num::Slot(_) => f32::NAN,
};
match op {
PathOp::MoveTo(a, b) | PathOp::LineTo(a, b) => vec![v(a), v(b)],
PathOp::QuadTo(n) => n.iter().map(v).collect(),
PathOp::CubicTo(n) => n.iter().map(v).collect(),
_ => vec![],
}
}
#[test]
fn svg_paths_parse_to_absolute_ops() {
let ops = parse_svg_path("M10 20 l5-5 H30 v10 z m1 1 2 2");
assert_eq!(ops.len(), 7);
assert_eq!(consts(&ops[1]), vec![15.0, 15.0]);
assert_eq!(consts(&ops[2]), vec![30.0, 15.0]);
assert_eq!(consts(&ops[3]), vec![30.0, 25.0]);
assert_eq!(ops[4], PathOp::Close);
assert_eq!(consts(&ops[5]), vec![11.0, 21.0]);
assert_eq!(consts(&ops[6]), vec![13.0, 23.0]);
let ops = parse_svg_path("M.5.5L1e1-2");
assert_eq!(consts(&ops[0]), vec![0.5, 0.5]);
assert_eq!(consts(&ops[1]), vec![10.0, -2.0]);
}
#[test]
fn smooth_curves_reflect_control_points() {
let ops = parse_svg_path("M0 0 C0 10 10 10 10 0 S20 -10 20 0");
assert_eq!(consts(&ops[2]), vec![10.0, -10.0, 20.0, -10.0, 20.0, 0.0]);
}
#[test]
fn arcs_become_cubics_ending_on_the_point() {
let ops = parse_svg_path("M0 0 A10 10 0 0120 0");
assert_eq!(ops.len(), 3);
let last = consts(&ops[2]);
assert!((last[4] - 20.0).abs() < 1e-4 && last[5].abs() < 1e-4);
let mid = consts(&ops[1]);
assert!((mid[4] - 10.0).abs() < 1e-3 && (mid[5] + 10.0).abs() < 1e-3);
}
#[test]
fn bad_path_data_keeps_what_parsed() {
let ops = parse_svg_path("M0 0 L10 10 L oops");
assert_eq!(ops.len(), 2);
}
#[test]
fn arc_sweeps_follow_the_canvas_spec() {
use std::f32::consts::{PI, TAU};
assert_eq!(arc_sweep(0.0, PI, false), PI);
assert!((arc_sweep(PI, 0.0, false) - PI).abs() < 1e-6);
assert!((arc_sweep(0.0, -PI / 2.0, false) - 1.5 * PI).abs() < 1e-5);
assert_eq!(arc_sweep(0.0, 3.0 * TAU, false), TAU);
assert!((arc_sweep(0.0, PI / 2.0, true) + 1.5 * PI).abs() < 1e-5);
assert_eq!(arc_sweep(TAU, 0.0, true), -TAU);
assert_eq!(
fit_radii(10.0, 100.0, [8.0, 8.0, 0.0, 0.0]),
[5.0, 5.0, 0.0, 0.0]
);
}
#[test]
fn fonts_parse_the_css_shorthand() {
let f = CanvasFont::parse("italic bold 14px \"Inter\", sans-serif").unwrap();
assert_eq!((f.size, f.weight, f.italic), (14.0, 700, true));
assert_eq!(f.family.as_deref(), Some("Inter"));
let f = CanvasFont::parse("600 12px/1.5 system-ui").unwrap();
assert_eq!((f.size, f.weight, f.family), (12.0, 600, None));
let f = CanvasFont::parse("small-caps tabular-nums 13px system-ui").unwrap();
assert_eq!(
f.variant.0,
FontVariant::SMALL_CAPS | FontVariant::TABULAR_NUMS
);
let numeric = f.with_numeric(FontVariant(FontVariant::OLDSTYLE_NUMS));
assert_eq!(
numeric.variant.0,
FontVariant::SMALL_CAPS | FontVariant::OLDSTYLE_NUMS
);
assert_eq!(f.with_numeric(FontVariant::default()), f);
assert!(CanvasFont::parse("wide 13px system-ui").is_none());
assert!(CanvasFont::parse("bold").is_none());
assert!(CanvasFont::parse("14px").is_none());
}
}