use std::sync::Arc;
mod path;
pub use path::Path2D;
mod code {
pub const FILL_STYLE: u8 = 1;
pub const STROKE_STYLE: u8 = 2;
pub const GLOBAL_ALPHA: u8 = 3;
pub const LINE_WIDTH: u8 = 4;
pub const LINE_WIDTH_PX: u8 = 5;
pub const LINE_CAP: u8 = 6;
pub const FILL_RECT: u8 = 7;
pub const FILL_CIRCLE: u8 = 8;
pub const FILL_CIRCLE_PX: u8 = 9;
pub const STROKE_CIRCLE: u8 = 10;
pub const POLYLINE: u8 = 11;
pub const LINE_JOIN: u8 = 12;
pub const FILL_POLYGON: u8 = 13;
pub const STROKE_POLYGON: u8 = 14;
pub const DRAW_IMAGE: u8 = 15;
pub const PATTERN: u8 = 16;
pub const LINEAR_GRADIENT: u8 = 17;
pub const APPEND: u8 = 18;
pub const APPEND_END: u8 = 19;
pub const SAVE: u8 = 20;
pub const RESTORE: u8 = 21;
pub const TRANSLATE: u8 = 22;
pub const ROTATE: u8 = 23;
pub const SCALE: u8 = 24;
pub const TRANSFORM: u8 = 25;
pub const RESET_TRANSFORM: u8 = 26;
pub const FILL_PATH: u8 = 27;
pub const STROKE_PATH: u8 = 28;
pub const CLIP: u8 = 29;
pub const STROKE_RECT: u8 = 30;
pub const CLEAR_RECT: u8 = 31;
pub const FONT: u8 = 32;
pub const FONT_PX: u8 = 33;
pub const TEXT_ALIGN: u8 = 34;
pub const TEXT_BASELINE: u8 = 35;
pub const TEXT: u8 = 36;
pub const RADIAL_GRADIENT: u8 = 37;
pub const CONIC_GRADIENT: u8 = 38;
pub const LINE_DASH: u8 = 39;
pub const MITER_LIMIT: u8 = 40;
pub const COMPOSITE: u8 = 41;
pub const SHADOW: u8 = 42;
pub const FILTER: u8 = 43;
pub const IMAGE_SMOOTHING: u8 = 44;
pub const DRAW_PIXELS: u8 = 45;
}
const UNITS_PIXELS: u8 = 0;
const UNITS_VIEW_BOX: u8 = 1;
const FILL: u8 = 0;
const STROKE: u8 = 1;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ViewBox {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl ViewBox {
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width,
height,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Rect {
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width,
height,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LineCap {
Butt,
Round,
Square,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LineJoin {
Miter,
Round,
Bevel,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FillRule {
#[default]
NonZero,
EvenOdd,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextAlign {
#[default]
Start,
End,
Left,
Right,
Center,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextBaseline {
Top,
Hanging,
Middle,
#[default]
Alphabetic,
Ideographic,
Bottom,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
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 {
fn keyword(self) -> &'static str {
match self {
Self::SourceOver => "source-over",
Self::SourceIn => "source-in",
Self::SourceOut => "source-out",
Self::SourceAtop => "source-atop",
Self::DestinationOver => "destination-over",
Self::DestinationIn => "destination-in",
Self::DestinationOut => "destination-out",
Self::DestinationAtop => "destination-atop",
Self::Lighter => "lighter",
Self::Copy => "copy",
Self::Xor => "xor",
Self::Multiply => "multiply",
Self::Screen => "screen",
Self::Overlay => "overlay",
Self::Darken => "darken",
Self::Lighten => "lighten",
Self::ColorDodge => "color-dodge",
Self::ColorBurn => "color-burn",
Self::HardLight => "hard-light",
Self::SoftLight => "soft-light",
Self::Difference => "difference",
Self::Exclusion => "exclusion",
Self::Hue => "hue",
Self::Saturation => "saturation",
Self::Color => "color",
Self::Luminosity => "luminosity",
}
}
}
impl TextAlign {
fn keyword(self) -> &'static str {
match self {
Self::Start => "start",
Self::End => "end",
Self::Left => "left",
Self::Right => "right",
Self::Center => "center",
}
}
}
impl TextBaseline {
fn keyword(self) -> &'static str {
match self {
Self::Top => "top",
Self::Hanging => "hanging",
Self::Middle => "middle",
Self::Alphabetic => "alphabetic",
Self::Ideographic => "ideographic",
Self::Bottom => "bottom",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ImageSmoothing {
Off,
#[default]
Low,
Medium,
High,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Drawing {
bytes: Arc<Vec<u8>>,
saves: u32,
}
impl Drawing {
pub fn new(view_box: ViewBox) -> Self {
let mut bytes = vec![UNITS_VIEW_BOX];
for value in [view_box.x, view_box.y, view_box.width, view_box.height] {
bytes.extend_from_slice(&value.to_le_bytes());
}
Self {
bytes: Arc::new(bytes),
saves: 0,
}
}
pub fn pixels() -> Self {
Self {
bytes: Arc::new(vec![UNITS_PIXELS]),
saves: 0,
}
}
pub fn fill_style(&mut self, color: &str) -> &mut Self {
self.text(code::FILL_STYLE, color)
}
pub fn stroke_style(&mut self, color: &str) -> &mut Self {
self.text(code::STROKE_STYLE, color)
}
pub fn global_alpha(&mut self, alpha: f32) -> &mut Self {
self.op(code::GLOBAL_ALPHA, &[alpha])
}
pub fn line_width(&mut self, width: f32) -> &mut Self {
self.op(code::LINE_WIDTH, &[width])
}
pub fn line_width_px(&mut self, width: f32) -> &mut Self {
self.op(code::LINE_WIDTH_PX, &[width])
}
pub fn line_cap(&mut self, cap: LineCap) -> &mut Self {
let value = match cap {
LineCap::Butt => 0,
LineCap::Round => 1,
LineCap::Square => 2,
};
let bytes = self.bytes_mut();
bytes.push(code::LINE_CAP);
bytes.push(value);
self
}
pub fn line_join(&mut self, join: LineJoin) -> &mut Self {
let value = match join {
LineJoin::Miter => 0,
LineJoin::Round => 1,
LineJoin::Bevel => 2,
};
let bytes = self.bytes_mut();
bytes.push(code::LINE_JOIN);
bytes.push(value);
self
}
pub fn miter_limit(&mut self, limit: f32) -> &mut Self {
self.op(code::MITER_LIMIT, &[limit])
}
pub fn line_dash(&mut self, segments: &[f32], offset: f32) -> &mut Self {
self.dash(false, segments, offset)
}
pub fn line_dash_px(&mut self, segments: &[f32], offset: f32) -> &mut Self {
self.dash(true, segments, offset)
}
pub fn fill_rect(&mut self, x: f32, y: f32, width: f32, height: f32) -> &mut Self {
self.op(code::FILL_RECT, &[x, y, width, height])
}
pub fn stroke_rect(&mut self, x: f32, y: f32, width: f32, height: f32) -> &mut Self {
self.op(code::STROKE_RECT, &[x, y, width, height])
}
pub fn clear_rect(&mut self, x: f32, y: f32, width: f32, height: f32) -> &mut Self {
self.op(code::CLEAR_RECT, &[x, y, width, height])
}
pub fn fill_circle(&mut self, x: f32, y: f32, radius: f32) -> &mut Self {
self.op(code::FILL_CIRCLE, &[x, y, radius])
}
pub fn fill_circle_px(&mut self, x: f32, y: f32, radius: f32) -> &mut Self {
self.op(code::FILL_CIRCLE_PX, &[x, y, radius])
}
pub fn stroke_circle(&mut self, x: f32, y: f32, radius: f32) -> &mut Self {
self.op(code::STROKE_CIRCLE, &[x, y, radius])
}
pub fn polyline(&mut self, points: impl IntoIterator<Item = (f32, f32)>) -> &mut Self {
self.points(code::POLYLINE, points)
}
pub fn fill_polygon(&mut self, points: impl IntoIterator<Item = (f32, f32)>) -> &mut Self {
self.points(code::FILL_POLYGON, points)
}
pub fn stroke_polygon(&mut self, points: impl IntoIterator<Item = (f32, f32)>) -> &mut Self {
self.points(code::STROKE_POLYGON, points)
}
pub fn fill_path(&mut self, path: &Path2D, rule: FillRule) -> &mut Self {
let bytes = self.bytes_mut();
bytes.extend_from_slice(&[code::FILL_PATH, rule as u8]);
path.write(bytes);
self
}
pub fn stroke_path(&mut self, path: &Path2D) -> &mut Self {
let bytes = self.bytes_mut();
bytes.push(code::STROKE_PATH);
path.write(bytes);
self
}
pub fn clip(&mut self, path: &Path2D, rule: FillRule) -> &mut Self {
let bytes = self.bytes_mut();
bytes.extend_from_slice(&[code::CLIP, rule as u8]);
path.write(bytes);
self
}
pub fn font(&mut self, font: &str) -> &mut Self {
self.text(code::FONT, font)
}
pub fn font_px(&mut self, font: &str) -> &mut Self {
self.text(code::FONT_PX, font)
}
pub fn text_align(&mut self, align: TextAlign) -> &mut Self {
self.text(code::TEXT_ALIGN, align.keyword())
}
pub fn text_baseline(&mut self, baseline: TextBaseline) -> &mut Self {
self.text(code::TEXT_BASELINE, baseline.keyword())
}
pub fn fill_text(&mut self, text: &str, x: f32, y: f32) -> &mut Self {
self.write(FILL, text, x, y)
}
pub fn stroke_text(&mut self, text: &str, x: f32, y: f32) -> &mut Self {
self.write(STROKE, text, x, y)
}
pub fn draw_image(&mut self, url: &str, part: Rect, target: Rect) -> &mut Self {
self.text(code::DRAW_IMAGE, url);
let bytes = self.bytes_mut();
for rect in [part, target] {
for value in [rect.x, rect.y, rect.width, rect.height] {
bytes.extend_from_slice(&value.to_le_bytes());
}
}
self
}
pub fn draw_pixels(
&mut self,
pixels: &[u8],
width: u32,
height: u32,
target: Rect,
) -> &mut Self {
let length = (width as usize)
.checked_mul(height as usize)
.and_then(|count| count.checked_mul(4));
if length == Some(pixels.len()) && !pixels.is_empty() {
let bytes = self.bytes_mut();
bytes.push(code::DRAW_PIXELS);
bytes.extend_from_slice(&width.to_le_bytes());
bytes.extend_from_slice(&height.to_le_bytes());
for value in [target.x, target.y, target.width, target.height] {
bytes.extend_from_slice(&value.to_le_bytes());
}
bytes.extend_from_slice(pixels);
}
self
}
pub fn fill_pattern(&mut self, url: &str, scale: f32) -> &mut Self {
self.pattern(FILL, url, scale)
}
pub fn stroke_pattern(&mut self, url: &str, scale: f32) -> &mut Self {
self.pattern(STROKE, url, scale)
}
pub fn fill_linear_gradient(
&mut self,
start: (f32, f32),
end: (f32, f32),
stops: &[(f32, &str)],
) -> &mut Self {
self.linear_gradient(FILL, start, end, stops)
}
pub fn stroke_linear_gradient(
&mut self,
start: (f32, f32),
end: (f32, f32),
stops: &[(f32, &str)],
) -> &mut Self {
self.linear_gradient(STROKE, start, end, stops)
}
pub fn fill_radial_gradient(
&mut self,
start: (f32, f32),
start_radius: f32,
end: (f32, f32),
end_radius: f32,
stops: &[(f32, &str)],
) -> &mut Self {
let geometry = [start.0, start.1, start_radius, end.0, end.1, end_radius];
self.gradient(code::RADIAL_GRADIENT, FILL, &geometry, stops)
}
pub fn stroke_radial_gradient(
&mut self,
start: (f32, f32),
start_radius: f32,
end: (f32, f32),
end_radius: f32,
stops: &[(f32, &str)],
) -> &mut Self {
let geometry = [start.0, start.1, start_radius, end.0, end.1, end_radius];
self.gradient(code::RADIAL_GRADIENT, STROKE, &geometry, stops)
}
pub fn fill_conic_gradient(
&mut self,
center: (f32, f32),
angle: f32,
stops: &[(f32, &str)],
) -> &mut Self {
let geometry = [angle, center.0, center.1];
self.gradient(code::CONIC_GRADIENT, FILL, &geometry, stops)
}
pub fn stroke_conic_gradient(
&mut self,
center: (f32, f32),
angle: f32,
stops: &[(f32, &str)],
) -> &mut Self {
let geometry = [angle, center.0, center.1];
self.gradient(code::CONIC_GRADIENT, STROKE, &geometry, stops)
}
pub fn composite(&mut self, composite: Composite) -> &mut Self {
self.text(code::COMPOSITE, composite.keyword())
}
pub fn shadow(&mut self, color: &str, blur: f32, offset_x: f32, offset_y: f32) -> &mut Self {
self.text(code::SHADOW, color);
let bytes = self.bytes_mut();
for value in [blur, offset_x, offset_y] {
bytes.extend_from_slice(&value.to_le_bytes());
}
self
}
pub fn filter(&mut self, filter: &str) -> &mut Self {
self.text(code::FILTER, filter)
}
pub fn image_smoothing(&mut self, smoothing: ImageSmoothing) -> &mut Self {
let bytes = self.bytes_mut();
bytes.extend_from_slice(&[code::IMAGE_SMOOTHING, smoothing as u8]);
self
}
pub fn save(&mut self) -> &mut Self {
self.saves += 1;
self.bytes_mut().push(code::SAVE);
self
}
pub fn restore(&mut self) -> &mut Self {
if self.saves > 0 {
self.saves -= 1;
self.bytes_mut().push(code::RESTORE);
}
self
}
pub fn translate(&mut self, x: f32, y: f32) -> &mut Self {
self.op(code::TRANSLATE, &[x, y])
}
pub fn rotate(&mut self, angle: f32) -> &mut Self {
self.op(code::ROTATE, &[angle])
}
pub fn scale(&mut self, x: f32, y: f32) -> &mut Self {
self.op(code::SCALE, &[x, y])
}
pub fn transform(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) -> &mut Self {
self.op(code::TRANSFORM, &[a, b, c, d, e, f])
}
pub fn reset_transform(&mut self) -> &mut Self {
self.bytes_mut().push(code::RESET_TRANSFORM);
self
}
pub fn append(&mut self, other: &Drawing) -> &mut Self {
let ops = other.ops();
if !ops.is_empty() {
let bytes = self.bytes_mut();
bytes.push(code::APPEND);
bytes.extend_from_slice(ops);
bytes.extend(std::iter::repeat_n(code::RESTORE, other.saves as usize));
bytes.push(code::APPEND_END);
}
self
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub(crate) fn into_shared_bytes(self) -> Arc<Vec<u8>> {
self.bytes
}
fn ops(&self) -> &[u8] {
let units = if self.bytes.first() == Some(&UNITS_VIEW_BOX) {
1 + 4 * 4
} else {
1
};
&self.bytes[units..]
}
fn pattern(&mut self, target: u8, url: &str, scale: f32) -> &mut Self {
self.bytes_mut().extend_from_slice(&[code::PATTERN, target]);
self.text_bytes(url);
self.bytes_mut().extend_from_slice(&scale.to_le_bytes());
self
}
fn linear_gradient(
&mut self,
target: u8,
start: (f32, f32),
end: (f32, f32),
stops: &[(f32, &str)],
) -> &mut Self {
let geometry = [start.0, start.1, end.0, end.1];
self.gradient(code::LINEAR_GRADIENT, target, &geometry, stops)
}
fn gradient(
&mut self,
code: u8,
target: u8,
geometry: &[f32],
stops: &[(f32, &str)],
) -> &mut Self {
let bytes = self.bytes_mut();
bytes.extend_from_slice(&[code, target]);
for value in geometry {
bytes.extend_from_slice(&value.to_le_bytes());
}
self.stops(stops)
}
fn dash(&mut self, px: bool, segments: &[f32], offset: f32) -> &mut Self {
let segments = &segments[..segments.len().min(u16::MAX as usize)];
let bytes = self.bytes_mut();
bytes.extend_from_slice(&[code::LINE_DASH, u8::from(px)]);
bytes.extend_from_slice(&(segments.len() as u16).to_le_bytes());
for value in segments.iter().chain([&offset]) {
bytes.extend_from_slice(&value.to_le_bytes());
}
self
}
fn write(&mut self, target: u8, text: &str, x: f32, y: f32) -> &mut Self {
self.bytes_mut().extend_from_slice(&[code::TEXT, target]);
self.text_bytes(text);
let bytes = self.bytes_mut();
for value in [x, y] {
bytes.extend_from_slice(&value.to_le_bytes());
}
self
}
fn stops(&mut self, stops: &[(f32, &str)]) -> &mut Self {
let stops = &stops[..stops.len().min(u16::MAX as usize)];
self.bytes_mut()
.extend_from_slice(&(stops.len() as u16).to_le_bytes());
for (offset, color) in stops {
self.bytes_mut().extend_from_slice(&offset.to_le_bytes());
self.text_bytes(color);
}
self
}
fn points(&mut self, code: u8, points: impl IntoIterator<Item = (f32, f32)>) -> &mut Self {
let bytes = self.bytes_mut();
bytes.push(code);
let count_at = bytes.len();
bytes.extend_from_slice(&0u32.to_le_bytes());
let mut count: u32 = 0;
for (x, y) in points {
bytes.extend_from_slice(&x.to_le_bytes());
bytes.extend_from_slice(&y.to_le_bytes());
count += 1;
}
bytes[count_at..count_at + 4].copy_from_slice(&count.to_le_bytes());
self
}
fn op(&mut self, code: u8, args: &[f32]) -> &mut Self {
let bytes = self.bytes_mut();
bytes.push(code);
for arg in args {
bytes.extend_from_slice(&arg.to_le_bytes());
}
self
}
fn text(&mut self, code: u8, value: &str) -> &mut Self {
self.bytes_mut().push(code);
self.text_bytes(value);
self
}
fn text_bytes(&mut self, value: &str) {
let mut end = value.len().min(u16::MAX as usize);
while !value.is_char_boundary(end) {
end -= 1;
}
let bytes = self.bytes_mut();
bytes.extend_from_slice(&(end as u16).to_le_bytes());
bytes.extend_from_slice(&value.as_bytes()[..end]);
}
fn bytes_mut(&mut self) -> &mut Vec<u8> {
Arc::make_mut(&mut self.bytes)
}
}
#[cfg(test)]
mod tests {
use super::*;
pub(crate) fn fixture() -> Drawing {
let mut appended = Drawing::pixels();
appended.save().fill_rect(1.0, 1.0, 1.0, 1.0);
let mut path = Path2D::svg("M0 0 H1");
path.move_to(0.0, 0.0)
.line_to(1.0, 0.0)
.quad_to(1.0, 1.0, 0.0, 1.0)
.cubic_to(0.0, 2.0, 1.0, 2.0, 1.0, 3.0)
.arc_to(2.0, 3.0, 2.0, 4.0, 0.5)
.ellipse((1.0, 1.0), (2.0, 1.0), 0.5, 0.0, 1.0, true)
.close();
let mut drawing = Drawing::new(ViewBox::new(-8.0, -5.0, 16.0, 10.0));
drawing
.fill_style("#ffd27f")
.stroke_style("żółć")
.global_alpha(0.5)
.line_width(2.0)
.line_width_px(1.5)
.line_cap(LineCap::Round)
.line_join(LineJoin::Bevel)
.fill_rect(-8.0, -5.0, 16.0, 10.0)
.fill_circle(1.0, 2.0, 3.0)
.fill_circle_px(-1.0, -2.0, 4.0)
.stroke_circle(0.0, 0.0, 6.0)
.polyline([(0.0, 0.0), (1.0, 1.0), (2.0, 0.0)])
.fill_polygon([(0.0, -1.0), (2.0, 0.0), (0.0, 1.0), (-2.0, 0.0)])
.stroke_polygon([(0.0, -1.0), (2.0, 0.0), (0.0, 1.0)])
.draw_image(
"cat.png",
Rect::new(1.0, 2.0, 3.0, 4.0),
Rect::new(-1.0, -2.0, 2.0, 1.0),
)
.fill_pattern("cat.png", 0.5)
.stroke_pattern("cat.png", 2.0)
.fill_rect(0.0, 0.0, 2.0, 2.0)
.fill_linear_gradient((0.0, -1.0), (2.0, 1.0), &[(0.0, "red"), (1.0, "#00f")])
.stroke_linear_gradient((0.0, 0.0), (0.0, 1.0), &[(0.5, "lime")])
.fill_rect(0.0, 0.0, 2.0, 2.0)
.append(&appended)
.save()
.translate(1.0, 2.0)
.rotate(0.5)
.scale(2.0, 2.0)
.transform(1.0, 0.0, 0.0, 1.0, 3.0, 4.0)
.line_width_px(1.0)
.reset_transform()
.restore()
.restore()
.fill_path(&path, FillRule::EvenOdd)
.stroke_path(&path)
.clip(&path, FillRule::NonZero)
.stroke_rect(0.0, 0.0, 1.0, 1.0)
.clear_rect(0.0, 0.0, 1.0, 1.0)
.font("bold 2px serif")
.font_px("12px serif")
.text_align(TextAlign::Center)
.text_baseline(TextBaseline::Middle)
.fill_text("Hi", 1.0, 2.0)
.stroke_text("Hi", 1.0, 2.0)
.fill_radial_gradient((0.0, 0.0), 0.0, (0.0, 0.0), 1.0, &[(0.0, "red")])
.stroke_conic_gradient((1.0, 1.0), 0.5, &[(0.0, "red"), (1.0, "blue")])
.line_dash(&[1.0, 2.0], 0.5)
.line_dash_px(&[4.0], 2.0)
.miter_limit(4.0)
.composite(Composite::Multiply)
.shadow("black", 2.0, 1.0, -1.0)
.filter("blur(2px)")
.image_smoothing(ImageSmoothing::Off)
.draw_pixels(&[255, 0, 0, 255], 1, 1, Rect::new(0.0, 0.0, 1.0, 1.0))
.save();
drawing
}
const FIXTURE: &[u8] = &[
1, 0, 0, 0, 193, 0, 0, 160, 192, 0, 0, 128, 65, 0, 0, 32, 65, 1, 7, 0, 35, 102, 102, 100,
50, 55, 102, 2, 8, 0, 197, 188, 195, 179, 197, 130, 196, 135, 3, 0, 0, 0, 63, 4, 0, 0, 0,
64, 5, 0, 0, 192, 63, 6, 1, 12, 2, 7, 0, 0, 0, 193, 0, 0, 160, 192, 0, 0, 128, 65, 0, 0,
32, 65, 8, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 9, 0, 0, 128, 191, 0, 0, 0, 192, 0, 0,
128, 64, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 64, 11, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 0, 0, 13, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128, 191, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 192, 0, 0, 0, 0, 14,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 191, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63,
15, 7, 0, 99, 97, 116, 46, 112, 110, 103, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0,
128, 64, 0, 0, 128, 191, 0, 0, 0, 192, 0, 0, 0, 64, 0, 0, 128, 63, 16, 0, 7, 0, 99, 97,
116, 46, 112, 110, 103, 0, 0, 0, 63, 16, 1, 7, 0, 99, 97, 116, 46, 112, 110, 103, 0, 0, 0,
64, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 64, 17, 0, 0, 0, 0, 0, 0, 0, 128, 191,
0, 0, 0, 64, 0, 0, 128, 63, 2, 0, 0, 0, 0, 0, 3, 0, 114, 101, 100, 0, 0, 128, 63, 4, 0, 35,
48, 48, 102, 17, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 1, 0, 0, 0, 0, 63,
4, 0, 108, 105, 109, 101, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 64, 18, 20, 7,
0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 128, 63, 21, 19, 20, 22, 0, 0, 128, 63,
0, 0, 0, 64, 23, 0, 0, 0, 63, 24, 0, 0, 0, 64, 0, 0, 0, 64, 25, 0, 0, 128, 63, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 64, 64, 0, 0, 128, 64, 5, 0, 0, 128, 63, 26, 21, 27, 1, 7,
0, 0, 0, 77, 48, 32, 48, 32, 72, 49, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 128,
63, 0, 0, 0, 0, 2, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 128, 63, 3, 0, 0, 0, 0,
0, 0, 0, 64, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 128, 63, 0, 0, 64, 64, 4, 0, 0, 0, 64, 0, 0,
64, 64, 0, 0, 0, 64, 0, 0, 128, 64, 0, 0, 0, 63, 5, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0,
64, 0, 0, 128, 63, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 128, 63, 1, 6, 28, 7, 0, 0, 0, 77, 48,
32, 48, 32, 72, 49, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 128, 63, 0, 0, 0, 0, 2,
0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 128, 63, 3, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0,
128, 63, 0, 0, 0, 64, 0, 0, 128, 63, 0, 0, 64, 64, 4, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0,
64, 0, 0, 128, 64, 0, 0, 0, 63, 5, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 128,
63, 0, 0, 0, 63, 0, 0, 0, 0, 0, 0, 128, 63, 1, 6, 29, 0, 7, 0, 0, 0, 77, 48, 32, 48, 32,
72, 49, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 128, 63, 0, 0, 0, 0, 2, 0, 0, 128,
63, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 128, 63, 3, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 128, 63, 0,
0, 0, 64, 0, 0, 128, 63, 0, 0, 64, 64, 4, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 64, 0, 0,
128, 64, 0, 0, 0, 63, 5, 0, 0, 128, 63, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 128, 63, 0, 0, 0,
63, 0, 0, 0, 0, 0, 0, 128, 63, 1, 6, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 128,
63, 31, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 128, 63, 32, 14, 0, 98, 111, 108, 100,
32, 50, 112, 120, 32, 115, 101, 114, 105, 102, 33, 10, 0, 49, 50, 112, 120, 32, 115, 101,
114, 105, 102, 34, 6, 0, 99, 101, 110, 116, 101, 114, 35, 6, 0, 109, 105, 100, 100, 108,
101, 36, 0, 2, 0, 72, 105, 0, 0, 128, 63, 0, 0, 0, 64, 36, 1, 2, 0, 72, 105, 0, 0, 128, 63,
0, 0, 0, 64, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128,
63, 1, 0, 0, 0, 0, 0, 3, 0, 114, 101, 100, 38, 1, 0, 0, 0, 63, 0, 0, 128, 63, 0, 0, 128,
63, 2, 0, 0, 0, 0, 0, 3, 0, 114, 101, 100, 0, 0, 128, 63, 4, 0, 98, 108, 117, 101, 39, 0,
2, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 0, 63, 39, 1, 1, 0, 0, 0, 128, 64, 0, 0, 0, 64, 40,
0, 0, 128, 64, 41, 8, 0, 109, 117, 108, 116, 105, 112, 108, 121, 42, 5, 0, 98, 108, 97, 99,
107, 0, 0, 0, 64, 0, 0, 128, 63, 0, 0, 128, 191, 43, 9, 0, 98, 108, 117, 114, 40, 50, 112,
120, 41, 44, 0, 45, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0,
128, 63, 255, 0, 0, 255, 20,
];
#[test]
fn matches_the_cross_language_fixture() {
assert_eq!(
fixture().as_bytes(),
FIXTURE,
"the drawing format changed - update canvas.test.ts to match, or this is a bug"
);
}
#[test]
fn a_drawing_in_pixels_has_no_view_box() {
let mut drawing = Drawing::pixels();
drawing.fill_circle(1.0, 2.0, 3.0);
assert_eq!(drawing.as_bytes()[0], UNITS_PIXELS);
assert_eq!(drawing.as_bytes().len(), 1 + 1 + 12);
}
#[test]
fn drawings_are_equal_when_they_draw_the_same() {
let draw = || {
let mut drawing = Drawing::pixels();
drawing.fill_style("red").fill_circle(1.0, 2.0, 3.0);
drawing
};
let mut moved = draw();
moved.fill_circle(1.0, 2.0, 3.0);
assert_eq!(draw(), draw());
assert_ne!(draw(), moved);
}
#[test]
fn a_clone_draws_on_alone() {
let mut drawing = Drawing::pixels();
drawing.fill_circle(1.0, 2.0, 3.0);
let kept = drawing.clone();
drawing.fill_circle(4.0, 5.0, 6.0);
assert_eq!(kept.as_bytes().len(), 1 + 13);
assert_eq!(drawing.as_bytes().len(), 1 + 13 + 13);
}
#[test]
fn a_polyline_counts_its_points() {
let mut drawing = Drawing::pixels();
drawing.polyline((0..300).map(|step| (step as f32, 0.0)));
let bytes = drawing.as_bytes();
assert_eq!(bytes[1], code::POLYLINE);
assert_eq!(
u32::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5]]),
300
);
assert_eq!(bytes.len(), 1 + 1 + 4 + 300 * 8);
}
#[test]
fn an_image_takes_its_url_and_two_rectangles() {
let mut drawing = Drawing::pixels();
drawing.draw_image(
"atlas.webp",
Rect::new(0.0, 0.0, 32.0, 16.0),
Rect::new(10.0, 20.0, 64.0, 32.0),
);
let bytes = drawing.as_bytes();
assert_eq!(bytes[1], code::DRAW_IMAGE);
assert_eq!(u16::from_le_bytes([bytes[2], bytes[3]]), 10);
assert_eq!(&bytes[4..14], b"atlas.webp");
assert_eq!(bytes.len(), 1 + 1 + 2 + 10 + 8 * 4);
}
#[test]
fn a_pattern_takes_its_target_url_and_scale() {
let mut drawing = Drawing::pixels();
drawing.fill_pattern("grass.webp", 0.25);
let bytes = drawing.as_bytes();
assert_eq!(&bytes[1..3], [code::PATTERN, FILL]);
assert_eq!(u16::from_le_bytes([bytes[3], bytes[4]]), 10);
assert_eq!(&bytes[5..15], b"grass.webp");
assert_eq!(&bytes[15..], 0.25f32.to_le_bytes());
let mut stroked = Drawing::pixels();
stroked.stroke_pattern("grass.webp", 0.25);
assert_eq!(&stroked.as_bytes()[1..3], [code::PATTERN, STROKE]);
assert_eq!(stroked.as_bytes()[3..], bytes[3..]);
}
#[test]
fn a_gradient_takes_its_target_ends_and_stops() {
let mut drawing = Drawing::pixels();
drawing.fill_linear_gradient((1.0, 2.0), (3.0, 4.0), &[(0.0, "red"), (0.5, "#0f0")]);
let bytes = drawing.as_bytes();
assert_eq!(&bytes[1..3], [code::LINEAR_GRADIENT, FILL]);
assert_eq!(
&bytes[3..19],
[1.0f32, 2.0, 3.0, 4.0].map(f32::to_le_bytes).concat()
);
assert_eq!(u16::from_le_bytes([bytes[19], bytes[20]]), 2);
assert_eq!(&bytes[21..25], 0.0f32.to_le_bytes());
assert_eq!(&bytes[25..30], b"\x03\x00red");
assert_eq!(&bytes[30..34], 0.5f32.to_le_bytes());
assert_eq!(&bytes[34..], b"\x04\x00#0f0");
let mut stroked = Drawing::pixels();
stroked.stroke_linear_gradient((1.0, 2.0), (3.0, 4.0), &[(0.0, "red"), (0.5, "#0f0")]);
assert_eq!(&stroked.as_bytes()[1..3], [code::LINEAR_GRADIENT, STROKE]);
assert_eq!(stroked.as_bytes()[3..], bytes[3..]);
}
#[test]
fn a_gradient_of_too_many_stops_is_cut() {
let stops = vec![(0.5f32, "red"); 70_000];
let mut drawing = Drawing::pixels();
drawing.fill_linear_gradient((0.0, 0.0), (1.0, 0.0), &stops);
let bytes = drawing.as_bytes();
assert_eq!(u16::from_le_bytes([bytes[19], bytes[20]]), u16::MAX);
assert_eq!(
bytes.len(),
1 + 2 + 16 + 2 + usize::from(u16::MAX) * (4 + 2 + 3)
);
}
#[test]
fn an_appended_drawing_comes_between_a_save_and_a_restore_without_its_units() {
let mut layer = Drawing::new(ViewBox::new(0.0, 0.0, 10.0, 10.0));
layer.fill_circle(1.0, 2.0, 3.0);
let mut drawing = Drawing::pixels();
drawing.fill_style("red").append(&layer);
let bytes = drawing.as_bytes();
let circle = &layer.as_bytes()[1 + 16..];
assert_eq!(bytes[1..7], *b"\x01\x03\x00red");
assert_eq!(bytes[7], code::APPEND);
assert_eq!(&bytes[8..8 + circle.len()], circle);
assert_eq!(bytes[8 + circle.len()..], [code::APPEND_END]);
}
#[test]
fn an_appended_drawing_restores_the_saves_it_left_open() {
let mut layer = Drawing::pixels();
layer.save().save().fill_circle(1.0, 2.0, 3.0);
let mut drawing = Drawing::pixels();
drawing.append(&layer);
let bytes = drawing.as_bytes();
assert_eq!(bytes[1..4], [code::APPEND, code::SAVE, code::SAVE]);
assert_eq!(
bytes[bytes.len() - 3..],
[code::RESTORE, code::RESTORE, code::APPEND_END]
);
assert_eq!(drawing.saves, 0);
}
#[test]
fn a_restore_without_a_save_does_nothing() {
let mut drawing = Drawing::pixels();
drawing.save().restore().restore();
assert_eq!(
drawing.as_bytes(),
[UNITS_PIXELS, code::SAVE, code::RESTORE]
);
}
#[test]
fn pixels_of_another_length_than_their_size_draw_nothing() {
let target = Rect::new(0.0, 0.0, 1.0, 1.0);
let mut drawing = Drawing::pixels();
drawing
.draw_pixels(&[0; 8], 1, 1, target)
.draw_pixels(&[], 0, 0, target)
.draw_pixels(&[0; 4], u32::MAX, u32::MAX, target);
assert_eq!(drawing.as_bytes(), [UNITS_PIXELS]);
drawing.draw_pixels(&[1, 2, 3, 4, 5, 6, 7, 8], 2, 1, target);
let bytes = drawing.as_bytes();
assert_eq!(bytes[1], code::DRAW_PIXELS);
assert_eq!(&bytes[2..10], [2u32, 1].map(u32::to_le_bytes).concat());
assert_eq!(&bytes[26..], [1, 2, 3, 4, 5, 6, 7, 8]);
}
#[test]
fn keywords_travel_as_their_text() {
let mut drawing = Drawing::pixels();
drawing
.composite(Composite::DestinationOut)
.text_align(TextAlign::Right)
.text_baseline(TextBaseline::Hanging);
assert_eq!(
&drawing.as_bytes()[1..],
[
&[code::COMPOSITE, 15, 0][..],
b"destination-out",
&[code::TEXT_ALIGN, 5, 0],
b"right",
&[code::TEXT_BASELINE, 7, 0],
b"hanging",
]
.concat()
);
}
#[test]
fn dashes_say_whether_they_are_in_pixels() {
let mut drawing = Drawing::pixels();
drawing.line_dash_px(&[1.0, 2.0], 3.0);
let bytes = drawing.as_bytes();
assert_eq!(bytes[1..5], [code::LINE_DASH, 1, 2, 0]);
assert_eq!(
&bytes[5..],
[1.0f32, 2.0, 3.0].map(f32::to_le_bytes).concat()
);
}
#[test]
fn a_text_takes_its_target_and_place() {
let mut drawing = Drawing::pixels();
drawing.stroke_text("ż", 1.0, 2.0);
let bytes = drawing.as_bytes();
assert_eq!(bytes[1..5], [code::TEXT, STROKE, 2, 0]);
assert_eq!(&bytes[5..7], "ż".as_bytes());
assert_eq!(&bytes[7..], [1.0f32, 2.0].map(f32::to_le_bytes).concat());
}
#[test]
fn a_path_follows_its_rule() {
let mut path = Path2D::new();
path.rect(0.0, 0.0, 1.0, 1.0);
let mut drawing = Drawing::pixels();
drawing.fill_path(&path, FillRule::EvenOdd);
let mut written = Vec::new();
path.write(&mut written);
let bytes = drawing.as_bytes();
assert_eq!(bytes[1..3], [code::FILL_PATH, 1]);
assert_eq!(&bytes[3..], written);
}
#[test]
fn appending_a_drawing_of_nothing_adds_nothing() {
let mut drawing = Drawing::pixels();
drawing.fill_circle(1.0, 2.0, 3.0);
let before = drawing.clone();
drawing.append(&Drawing::new(ViewBox::new(0.0, 0.0, 1.0, 1.0)));
assert_eq!(drawing, before);
}
#[test]
fn a_drawing_appended_to_its_clone_stays_as_it_was() {
let mut layer = Drawing::pixels();
layer.fill_circle(1.0, 2.0, 3.0);
let mut doubled = layer.clone();
doubled.append(&layer);
assert_eq!(layer.as_bytes().len(), 1 + 13);
assert_eq!(doubled.as_bytes().len(), 1 + 13 + 1 + 13 + 1);
}
#[test]
fn a_style_too_long_is_cut_between_characters() {
let long = "ż".repeat(40_000);
let mut drawing = Drawing::pixels();
drawing.fill_style(&long);
let bytes = drawing.as_bytes();
let length = u16::from_le_bytes([bytes[2], bytes[3]]) as usize;
assert_eq!(length, 65_534);
assert!(std::str::from_utf8(&bytes[4..4 + length]).is_ok());
}
}