use pdfboss_core::geom::{Matrix, Point};
use crate::path::Subpath;
const MIN_WIDTH: f32 = 0.75;
const FAN_SEGMENTS: usize = 12;
const MAX_DASH_PIECES: usize = 65_536;
const MIN_SEGMENT: f32 = 1e-2;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub(crate) enum LineCap {
#[default]
Butt,
Round,
Square,
}
impl LineCap {
pub(crate) fn from_code(code: i32) -> LineCap {
match code {
1 => LineCap::Round,
2 => LineCap::Square,
_ => LineCap::Butt,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub(crate) enum LineJoin {
#[default]
Miter,
Round,
Bevel,
}
impl LineJoin {
pub(crate) fn from_code(code: i32) -> LineJoin {
match code {
1 => LineJoin::Round,
2 => LineJoin::Bevel,
_ => LineJoin::Miter,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct StrokeStyle {
pub(crate) width: f32,
pub(crate) cap: LineCap,
pub(crate) join: LineJoin,
pub(crate) miter_limit: f32,
}
fn lerp(a: Point, b: Point, t: f32) -> Point {
Point::new(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t)
}
fn add(a: Point, b: Point) -> Point {
Point::new(a.x + b.x, a.y + b.y)
}
fn sub(a: Point, b: Point) -> Point {
Point::new(a.x - b.x, a.y - b.y)
}
fn scale(v: Point, k: f32) -> Point {
Point::new(v.x * k, v.y * k)
}
fn unit(v: Point) -> Option<Point> {
let len = v.x.hypot(v.y);
(len > 1e-6 && len.is_finite()).then(|| scale(v, 1.0 / len))
}
fn distinct(points: &[Point]) -> Vec<Point> {
let mut out: Vec<Point> = Vec::with_capacity(points.len());
for &p in points {
let dup = out
.last()
.is_some_and(|last| (p.x - last.x).hypot(p.y - last.y) <= MIN_SEGMENT);
if !dup {
out.push(p);
}
}
out
}
fn linear(m: Matrix, v: Point) -> Point {
Point::new(m.a * v.x + m.c * v.y, m.b * v.x + m.d * v.y)
}
struct Run {
points: Vec<Point>,
along: Point,
}
fn dash_split(points: &[Point], dash: &[f32], phase: f32, inv: Matrix) -> Vec<Run> {
let first = sub(points[1], points[0]);
let pattern: Vec<f32> = dash
.iter()
.copied()
.filter(|d| d.is_finite() && *d >= 0.0)
.collect();
let total: f32 = pattern.iter().sum();
if pattern.len() != dash.len() || pattern.is_empty() || total <= 0.0 {
return vec![Run {
points: points.to_vec(),
along: first,
}];
}
let mut idx = 0usize;
let mut rem = pattern[0];
let mut ph = if phase.is_finite() && phase > 0.0 {
phase % total
} else {
0.0
};
while ph > 0.0 {
if ph >= rem {
ph -= rem;
idx = (idx + 1) % pattern.len();
rem = pattern[idx];
} else {
rem -= ph;
ph = 0.0;
}
}
let mut on = idx.is_multiple_of(2);
let mut runs: Vec<Run> = Vec::new();
let mut cur: Vec<Point> = if on { vec![points[0]] } else { Vec::new() };
let mut cur_along = first;
let mut pieces = 0usize;
for seg in points.windows(2) {
let (a, b) = (seg[0], seg[1]);
let seglen = {
let u = linear(inv, sub(b, a));
u.x.hypot(u.y)
};
let mut done = 0.0f32;
while seglen - done > rem && pieces < MAX_DASH_PIECES {
done += rem;
let p = lerp(a, b, done / seglen);
if on {
cur.push(p);
if cur.len() >= 2 {
runs.push(Run {
points: std::mem::take(&mut cur),
along: cur_along,
});
} else {
cur.clear();
}
} else {
cur = vec![p];
cur_along = sub(b, a);
}
on = !on;
idx = (idx + 1) % pattern.len();
rem = pattern[idx];
pieces += 1;
}
rem -= seglen - done;
if on {
cur.push(b);
}
}
if on && cur.len() >= 2 {
runs.push(Run {
points: cur,
along: cur_along,
});
}
runs
}
#[derive(Clone, Copy)]
struct Pen {
to_device: Matrix,
to_user: Matrix,
r: f32,
winding: f32,
uniform_r: Option<f32>,
}
fn offset(p: Point, q: Point, pen: Pen) -> Option<Point> {
let dx = q.x - p.x;
let dy = q.y - p.y;
let len = dx.hypot(dy);
if len <= 1e-6 || !len.is_finite() {
return None;
}
let mut o = if let Some(r) = pen.uniform_r {
Point::new(-dy / len * r * pen.winding, dx / len * r * pen.winding)
} else {
let u = linear(pen.to_user, Point::new(dx, dy));
let ulen = u.x.hypot(u.y);
if ulen > 0.0 && ulen.is_finite() {
linear(
pen.to_device,
Point::new(-u.y / ulen * pen.r, u.x / ulen * pen.r),
)
} else {
Point::new(0.0, 0.0)
}
};
let across = ((o.x * -dy + o.y * dx) / len).abs();
if !across.is_finite() || across <= 0.0 {
let h = MIN_WIDTH / 2.0 * pen.winding;
o = Point::new(-dy / len * h, dx / len * h);
} else if across < MIN_WIDTH / 2.0 {
let k = MIN_WIDTH / 2.0 / across;
o = Point::new(o.x * k, o.y * k);
}
Some(o)
}
fn segment_quad(p: Point, q: Point, pen: Pen) -> Option<Subpath> {
let o = offset(p, q, pen)?;
Some(Subpath {
points: vec![add(p, o), add(q, o), sub(q, o), sub(p, o)],
closed: true,
})
}
fn extension(along: Point, pen: Pen) -> Option<Point> {
if let Some(r) = pen.uniform_r {
return Some(scale(unit(along)?, r));
}
let u = unit(linear(pen.to_user, along))?;
Some(linear(pen.to_device, scale(u, pen.r)))
}
fn cap(e: Point, away: Point, pen: Pen, cap: LineCap) -> Option<Subpath> {
match cap {
LineCap::Butt => None,
LineCap::Round => Some(disc(e, pen)),
LineCap::Square => segment_quad(e, add(e, extension(away, pen)?), pen),
}
}
fn dot(c: Point, along: Option<Point>, pen: Pen, cap: LineCap) -> Option<Subpath> {
match cap {
LineCap::Butt => None,
LineCap::Round => Some(disc(c, pen)),
LineCap::Square => {
let ext = extension(along?, pen)?;
segment_quad(sub(c, ext), add(c, ext), pen)
}
}
}
fn signed_area(points: &[Point]) -> f32 {
let n = points.len();
(0..n)
.map(|i| {
let p = points[i];
let q = points[(i + 1) % n];
p.x * q.y - q.x * p.y
})
.sum()
}
fn wound(mut points: Vec<Point>, pen: Pen) -> Subpath {
if signed_area(&points) * pen.winding > 0.0 {
points.reverse();
}
Subpath {
points,
closed: true,
}
}
fn miter_fits(d1: Point, d2: Point, pen: Pen, limit: f32) -> bool {
let (Some(u1), Some(u2)) = (unit(linear(pen.to_user, d1)), unit(linear(pen.to_user, d2)))
else {
return false;
};
let denominator = 1.0 + u1.x * u2.x + u1.y * u2.y;
denominator > 1e-6 && 2.0 / denominator <= limit * limit
}
fn join(p: Point, v: Point, q: Point, pen: Pen, style: StrokeStyle) -> Option<Subpath> {
if style.join == LineJoin::Round {
return Some(disc(v, pen));
}
let d1 = unit(sub(v, p))?;
let d2 = unit(sub(q, v))?;
let turn = d1.x * d2.y - d1.y * d2.x;
if turn.abs() <= 1e-6 {
return None;
}
let o1 = offset(p, v, pen)?;
let o1 = if (d1.x * o1.y - d1.y * o1.x) * turn > 0.0 {
scale(o1, -1.0)
} else {
o1
};
let o2 = offset(v, q, pen)?;
let o2 = if (d2.x * o2.y - d2.y * o2.x) * turn > 0.0 {
scale(o2, -1.0)
} else {
o2
};
let a = add(v, o1);
let b = add(v, o2);
if style.join == LineJoin::Miter && miter_fits(d1, d2, pen, style.miter_limit) {
let ab = sub(b, a);
let t = (ab.x * d2.y - ab.y * d2.x) / turn;
let tip = add(a, scale(d1, t));
return Some(wound(vec![v, a, tip, b], pen));
}
Some(wound(vec![v, a, b], pen))
}
fn disc(c: Point, pen: Pen) -> Subpath {
let mut points = Vec::with_capacity(FAN_SEGMENTS);
for i in 0..FAN_SEGMENTS {
let theta = -(i as f32) * std::f32::consts::TAU / FAN_SEGMENTS as f32;
let (sin, cos) = theta.sin_cos();
let v = linear(pen.to_device, Point::new(pen.r * cos, pen.r * sin));
let vlen = v.x.hypot(v.y);
let v = if !vlen.is_finite() || vlen <= 0.0 {
Point::new(MIN_WIDTH / 2.0 * cos, MIN_WIDTH / 2.0 * sin * pen.winding)
} else if vlen < MIN_WIDTH / 2.0 {
let k = MIN_WIDTH / 2.0 / vlen;
Point::new(v.x * k, v.y * k)
} else {
v
};
points.push(Point::new(c.x + v.x, c.y + v.y));
}
Subpath {
points,
closed: true,
}
}
pub(crate) fn stroke_path(
subpaths: &[Subpath],
style: StrokeStyle,
ctm: Matrix,
dash: &[f32],
phase: f32,
) -> Vec<Subpath> {
let width = if style.width.is_finite() {
style.width.max(0.0)
} else {
0.0
};
let (to_device, to_user) = match ctm.invert() {
Some(inv) => (ctm, inv),
None => (Matrix::identity(), Matrix::identity()),
};
let c1 = to_device.a * to_device.a + to_device.b * to_device.b;
let c2 = to_device.c * to_device.c + to_device.d * to_device.d;
let skew = to_device.a * to_device.c + to_device.b * to_device.d;
let largest = c1.max(c2);
let uniform_r = ((c1 - c2).abs() <= largest * 1e-3 && skew.abs() <= largest * 1e-3)
.then(|| width / 2.0 * c1.sqrt());
let pen = Pen {
to_device,
to_user,
r: width / 2.0,
winding: if to_device.a * to_device.d - to_device.b * to_device.c < 0.0 {
-1.0
} else {
1.0
},
uniform_r,
};
let mut out = Vec::new();
for subpath in subpaths {
let mut pts = distinct(&subpath.points);
if subpath.closed && pts.len() >= 2 {
let (first, last) = (pts[0], pts[pts.len() - 1]);
if (first.x - last.x).hypot(first.y - last.y) <= MIN_SEGMENT {
pts.pop();
}
}
match pts.len() {
0 => continue,
1 => {
out.extend(dot(pts[0], None, pen, style.cap));
continue;
}
_ => {}
}
if subpath.closed {
pts.push(pts[0]);
}
let runs = dash_split(&pts, dash, phase, to_user);
let closed = subpath.closed && runs.len() == 1 && runs[0].points.len() == pts.len();
for run in runs {
let rp = distinct(&run.points);
let n = rp.len();
if n == 0 {
continue;
}
if n == 1 {
out.extend(dot(rp[0], Some(run.along), pen, style.cap));
continue;
}
for seg in rp.windows(2) {
out.extend(segment_quad(seg[0], seg[1], pen));
}
for w in rp.windows(3) {
out.extend(join(w[0], w[1], w[2], pen, style));
}
if closed {
out.extend(join(rp[n - 2], rp[0], rp[1], pen, style));
} else {
out.extend(cap(rp[0], sub(rp[0], rp[1]), pen, style.cap));
out.extend(cap(rp[n - 1], sub(rp[n - 1], rp[n - 2]), pen, style.cap));
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::raster::{fill_path, BlendMode, FillRule, RasterScratch};
use crate::Pixmap;
fn line(points: &[(f32, f32)]) -> Subpath {
Subpath {
points: points.iter().map(|&(x, y)| Point::new(x, y)).collect(),
closed: false,
}
}
fn alpha_at(pix: &Pixmap, x: u32, y: u32) -> u8 {
pix.data[((y * pix.width + x) * 4 + 3) as usize]
}
fn solid(width: f32) -> StrokeStyle {
StrokeStyle {
width,
cap: LineCap::Butt,
join: LineJoin::Miter,
miter_limit: 10.0,
}
}
const BLACK: [u8; 4] = [0, 0, 0, 255];
fn paint(pix: &mut Pixmap, polys: &[Subpath]) {
fill_path(
pix,
&mut RasterScratch::default(),
polys,
FillRule::NonZero,
BLACK,
1.0,
None,
BlendMode::Normal,
);
}
#[test]
fn horizontal_line_paints_band_of_expected_thickness() {
let mut pix = Pixmap::new(20, 10);
let polys = stroke_path(
&[line(&[(2.0, 5.0), (18.0, 5.0)])],
solid(4.0),
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
let thick = (0..10).filter(|&y| alpha_at(&pix, 10, y) > 127).count();
assert!((3..=5).contains(&thick), "band thickness {thick}");
assert_eq!(alpha_at(&pix, 10, 5), 255, "band core solid");
assert_eq!(alpha_at(&pix, 10, 0), 0, "above band clear");
assert_eq!(alpha_at(&pix, 10, 9), 0, "below band clear");
}
#[test]
fn round_caps_extend_past_endpoints() {
let mut pix = Pixmap::new(20, 10);
let polys = stroke_path(
&[line(&[(4.0, 5.0), (16.0, 5.0)])],
StrokeStyle {
cap: LineCap::Round,
..solid(4.0)
},
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
assert!(alpha_at(&pix, 2, 5) > 127, "left cap");
assert!(alpha_at(&pix, 17, 5) > 127, "right cap");
assert_eq!(alpha_at(&pix, 0, 5), 0);
assert!(alpha_at(&pix, 2, 3) < 200, "round cap corner");
}
#[test]
fn butt_caps_stop_at_the_endpoints() {
let mut pix = Pixmap::new(20, 10);
let polys = stroke_path(
&[line(&[(4.0, 5.0), (16.0, 5.0)])],
solid(4.0),
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
assert_eq!(alpha_at(&pix, 2, 5), 0, "past the left end");
assert_eq!(alpha_at(&pix, 17, 5), 0, "past the right end");
assert_eq!(alpha_at(&pix, 5, 5), 255, "band");
}
#[test]
fn square_caps_extend_the_band_by_half_the_width() {
let mut pix = Pixmap::new(20, 10);
let polys = stroke_path(
&[line(&[(4.0, 5.0), (16.0, 5.0)])],
StrokeStyle {
cap: LineCap::Square,
..solid(4.0)
},
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
assert_eq!(alpha_at(&pix, 2, 5), 255, "left extension");
assert_eq!(alpha_at(&pix, 2, 3), 255, "left corner");
assert_eq!(alpha_at(&pix, 17, 6), 255, "right corner");
assert_eq!(alpha_at(&pix, 1, 5), 0, "beyond the extension");
}
fn corner(join: LineJoin, miter_limit: f32, ctm: Matrix) -> Pixmap {
let mut pix = Pixmap::new(14, 14);
let polys = stroke_path(
&[line(&[(2.0, 2.0), (2.0, 10.0), (10.0, 10.0)])],
StrokeStyle {
join,
miter_limit,
..solid(4.0)
},
ctm,
&[],
0.0,
);
paint(&mut pix, &polys);
pix
}
#[test]
fn miter_joins_fill_the_outer_corner() {
let pix = corner(LineJoin::Miter, 10.0, Matrix::identity());
assert_eq!(alpha_at(&pix, 0, 11), 255, "miter corner");
assert_eq!(alpha_at(&pix, 1, 10), 255, "bevel triangle");
assert_eq!(alpha_at(&pix, 5, 5), 0, "inside the L");
let mut pix = Pixmap::new(14, 14);
let polys = stroke_path(
&[line(&[(12.0, 2.0), (12.0, 10.0), (4.0, 10.0)])],
solid(4.0),
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
assert_eq!(alpha_at(&pix, 13, 11), 255, "mirrored miter corner");
assert_eq!(alpha_at(&pix, 8, 5), 0, "inside the mirrored L");
}
#[test]
fn bevel_joins_cut_the_corner() {
let pix = corner(LineJoin::Bevel, 10.0, Matrix::identity());
assert!(alpha_at(&pix, 0, 11) < 30, "bevel leaves the corner");
assert_eq!(alpha_at(&pix, 1, 10), 255, "bevel triangle");
}
#[test]
fn round_joins_keep_the_pen_disc() {
let pix = corner(LineJoin::Round, 10.0, Matrix::identity());
assert!(
alpha_at(&pix, 0, 11) < 200,
"disc leaves most of the corner"
);
assert_eq!(alpha_at(&pix, 1, 10), 255, "disc covers the notch");
}
#[test]
fn joins_follow_a_flipped_matrix() {
let flipped = Matrix {
a: 1.0,
b: 0.0,
c: 0.0,
d: -1.0,
e: 0.0,
f: 0.0,
};
let pix = corner(LineJoin::Miter, 10.0, flipped);
assert_eq!(alpha_at(&pix, 0, 11), 255, "miter corner");
assert_eq!(alpha_at(&pix, 1, 10), 255, "bevel triangle");
assert_eq!(alpha_at(&pix, 2, 9), 255, "no hole where pieces overlap");
let pix = corner(LineJoin::Round, 10.0, flipped);
assert_eq!(alpha_at(&pix, 1, 10), 255, "disc covers the notch");
assert_eq!(alpha_at(&pix, 2, 9), 255, "no hole under the disc");
}
#[test]
fn joins_pick_the_outer_side_under_a_skewed_pen() {
let skewed = Matrix {
a: 2.0,
b: 0.0,
c: 0.0,
d: 0.5,
e: 0.0,
f: 0.0,
};
let mut pix = Pixmap::new(40, 40);
let polys = stroke_path(
&[line(&[(4.0, 4.0), (20.0, 20.0), (28.0, 27.0)])],
solid(4.0),
skewed,
&[],
0.0,
);
paint(&mut pix, &polys);
assert_eq!(alpha_at(&pix, 12, 12), 255, "band");
assert_eq!(alpha_at(&pix, 1, 1), 0, "no sliver behind the start");
assert_eq!(alpha_at(&pix, 36, 35), 0, "no sliver past the end");
}
#[test]
fn the_miter_limit_cuts_sharp_joins_to_bevels() {
let spike = |miter_limit: f32| {
let mut pix = Pixmap::new(14, 18);
let polys = stroke_path(
&[line(&[(2.0, 2.0), (7.0, 12.0), (12.0, 2.0)])],
StrokeStyle {
miter_limit,
..solid(4.0)
},
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
alpha_at(&pix, 7, 13)
};
assert_eq!(spike(10.0), 255, "within the default limit");
assert_eq!(spike(2.3), 255, "just within the limit");
assert_eq!(spike(2.0), 0, "over the limit: bevel");
}
#[test]
fn degenerate_subpaths_paint_a_dot_only_under_round_caps() {
let closed_point = Subpath {
points: vec![Point::new(5.0, 5.0)],
closed: true,
};
let coincident = Subpath {
points: vec![Point::new(14.0, 5.0), Point::new(14.0, 5.0)],
closed: false,
};
for cap in [LineCap::Butt, LineCap::Round, LineCap::Square] {
let mut pix = Pixmap::new(20, 10);
let polys = stroke_path(
&[closed_point.clone(), coincident.clone()],
StrokeStyle { cap, ..solid(4.0) },
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
let want = if cap == LineCap::Round { 255 } else { 0 };
assert_eq!(alpha_at(&pix, 5, 5), want, "closed point under {cap:?}");
assert_eq!(
alpha_at(&pix, 14, 5),
want,
"coincident points under {cap:?}"
);
}
}
#[test]
fn zero_length_dashes_take_the_cap_style() {
let stroke = |cap| {
let mut pix = Pixmap::new(20, 10);
let polys = stroke_path(
&[line(&[(1.0, 5.0), (19.0, 5.0)])],
StrokeStyle { cap, ..solid(4.0) },
Matrix::identity(),
&[0.0, 8.0],
0.0,
);
paint(&mut pix, &polys);
pix
};
let pix = stroke(LineCap::Round);
assert_eq!(alpha_at(&pix, 9, 5), 255, "round dot");
assert_eq!(alpha_at(&pix, 5, 5), 0, "gap");
assert!(alpha_at(&pix, 7, 3) < 200, "round dot corner");
let pix = stroke(LineCap::Square);
assert_eq!(alpha_at(&pix, 9, 5), 255, "square dot");
assert_eq!(alpha_at(&pix, 7, 3), 255, "square dot corner");
assert_eq!(alpha_at(&pix, 5, 5), 0, "gap");
let pix = stroke(LineCap::Butt);
assert_eq!(alpha_at(&pix, 9, 5), 0, "butt caps paint nothing");
}
#[test]
fn minimum_device_width_keeps_hairlines_visible() {
let mut pix = Pixmap::new(20, 10);
let polys = stroke_path(
&[line(&[(2.0, 5.5), (18.0, 5.5)])],
solid(0.05),
Matrix::identity(),
&[],
0.0,
);
paint(&mut pix, &polys);
let total: u32 = (0..10).map(|y| alpha_at(&pix, 10, y) as u32).sum();
assert!(total >= 150, "hairline too faint: {total}");
}
#[test]
fn dash_pattern_splits_into_runs() {
let mut pix = Pixmap::new(21, 10);
let polys = stroke_path(
&[line(&[(1.0, 5.0), (19.0, 5.0)])],
solid(2.0),
Matrix::identity(),
&[4.0, 4.0],
0.0,
);
paint(&mut pix, &polys);
let mut runs = 0;
let mut prev_on = false;
for x in 0..21 {
let on = alpha_at(&pix, x, 4) > 127;
if on && !prev_on {
runs += 1;
}
prev_on = on;
}
assert_eq!(runs, 3, "expected 3 painted runs");
}
#[test]
fn dash_split_counts_and_phase() {
let pts = [Point::new(0.0, 0.0), Point::new(20.0, 0.0)];
assert_eq!(
dash_split(&pts, &[2.0, 2.0], 0.0, Matrix::identity()).len(),
5
);
assert_eq!(
dash_split(&pts, &[2.0, 2.0], 2.0, Matrix::identity()).len(),
5
);
assert_eq!(
dash_split(&pts, &[2.0, 2.0], 1.0, Matrix::identity()).len(),
6
);
assert_eq!(dash_split(&pts, &[], 0.0, Matrix::identity()).len(), 1);
assert_eq!(
dash_split(&pts, &[0.0, 0.0], 0.0, Matrix::identity()).len(),
1
);
assert_eq!(
dash_split(&pts, &[-1.0, 2.0], 0.0, Matrix::identity()).len(),
1
);
}
#[test]
fn closed_subpath_strokes_closing_segment() {
let mut pix = Pixmap::new(12, 12);
let square = Subpath {
points: vec![
Point::new(2.0, 2.0),
Point::new(10.0, 2.0),
Point::new(10.0, 10.0),
Point::new(2.0, 10.0),
],
closed: true,
};
let polys = stroke_path(&[square], solid(2.0), Matrix::identity(), &[], 0.0);
paint(&mut pix, &polys);
assert_eq!(alpha_at(&pix, 2, 6), 255, "left edge");
assert_eq!(alpha_at(&pix, 6, 6), 0, "interior clear");
}
#[test]
fn zero_length_segments_are_skipped() {
let pen = Pen {
to_device: Matrix::identity(),
to_user: Matrix::identity(),
r: 2.0,
winding: 1.0,
uniform_r: Some(2.0),
};
assert!(segment_quad(Point::new(1.0, 1.0), Point::new(1.0, 1.0), pen).is_none());
}
#[test]
fn anisotropic_ctm_widens_the_pen_across_the_stroke() {
let ctm = Matrix {
a: 0.0,
b: 2.0629,
c: 0.4848,
d: 0.0,
e: 0.0,
f: 0.0,
};
let mut pix = Pixmap::new(40, 20);
let polys = stroke_path(
&[line(&[(2.0, 10.0), (38.0, 10.0)])],
solid(4.26),
ctm,
&[],
0.0,
);
paint(&mut pix, &polys);
let thick = (0..20).filter(|&y| alpha_at(&pix, 20, y) > 127).count();
assert!(
(8..=10).contains(&thick),
"band thickness {thick}, want ~8.8"
);
}
#[test]
fn dash_pattern_is_measured_in_user_space() {
let ctm = Matrix {
a: 0.0,
b: 2.0629,
c: 0.4848,
d: 0.0,
e: 0.0,
f: 0.0,
};
let mut pix = Pixmap::new(40, 20);
let polys = stroke_path(
&[line(&[(1.0, 10.0), (39.0, 10.0)])],
solid(1.5),
ctm,
&[4.0, 4.0],
0.0,
);
paint(&mut pix, &polys);
let mut runs = 0;
let mut prev_on = false;
for x in 0..40 {
let on = alpha_at(&pix, x, 10) > 127;
if on && !prev_on {
runs += 1;
}
prev_on = on;
}
assert!((9..=11).contains(&runs), "painted runs {runs}, want ~10");
}
}