#! Contains the stroker, which makes a stroke of an outline the polygons it #! covers: a quadrilateral along each line, the joins between them, and the #! caps at the ends of polylines, dashed first. import std::math; module image::svg; # What strokes an outline: half its width, and how its ends, corners, and dashes # are drawn. struct pen { var half: f64; var cap: line_cap; var join: line_join; var miter_limit: f64; var dashes: f64[16]; var dash_count: u64; var dash_offset: f64; var within: f64; #! How far round ends and corners may stray from a circle. } # The polygons a stroke of an outline covers: a quadrilateral along each line, # the corners between them, and the ends of polylines, dashed first. def stroke(shape: &outline, with: &pen): outline { var made := outline::new(); var piece := vector::::new(); for held in shape.contours { val points := shape.points.view()[held.start:held.count]; if with.dash_count == 0 { stroke_polyline(points, held.closed, with, &mut made); continue; } dash(points, held.closed, with, &mut piece, &mut made); } return made; } # Strokes the dashes of a polyline, the pattern begun again at each, at its # offset. def dash(points: span, closed: bool, with: &pen, piece: &mut vector, made: &mut outline): void { var total: f64 = 0; for index in 0..with.dash_count { total += with.dashes[index]; } # Where in the pattern the polyline begins. var which: u64 = 0; var offset := with.dash_offset % total; if offset < 0 { offset += total; } while offset >= with.dashes[which] { offset -= with.dashes[which]; which = (which + 1) % with.dash_count; } var left := with.dashes[which] - offset; piece.clear(); if which % 2 == 0 { piece.append(points[0]); } val segments := closed ? points.length : points.length - 1; for index in 0..segments { val a := points[index]; val b := points[(index + 1) % points.length]; val length := math::hypot(b.x - a.x, b.y - a.y); var along: f64 = 0; while length - along > left { along += left; val at := point { x = a.x + (b.x - a.x) * along / length, y = a.y + (b.y - a.y) * along / length }; if which % 2 == 0 { piece.append(at); stroke_polyline(piece.view(), false, with, made); piece.clear(); } else { piece.append(at); } which = (which + 1) % with.dash_count; left = with.dashes[which]; } left -= length - along; if which % 2 == 0 { piece.append(b); } } if which % 2 == 0 && !piece.is_empty { stroke_polyline(piece.view(), false, with, made); } piece.clear(); } # Strokes a polyline, or a polygon. def stroke_polyline(given: span, closed_given: bool, with: &pen, made: &mut outline): void { # Points the same as the one before add nothing. var points := vector::::new(); for at in given { if points.is_empty || !same_point(at, points.last()) { points.append(at); } } var closed := closed_given; if closed && points.length > 1 && same_point(points[0], points.last()) { points.truncate(); } if points.length == 0 { return; } if points.length == 1 { # A path of no length is drawn by its caps alone. if with.cap == line_cap::round { circle(points[0], with.half, with, made); } else if with.cap == line_cap::square { val at := points[0]; val h := with.half; made.polygon([point { x = at.x - h, y = at.y - h }, point { x = at.x + h, y = at.y - h }, point { x = at.x + h, y = at.y + h }, point { x = at.x - h, y = at.y + h }]); } return; } if points.length == 2 { closed = false; } val count := points.length; val segments := closed ? count : count - 1; for index in 0..segments { val a := points[index]; val b := points[(index + 1) % count]; val across := normal(a, b, with.half); made.polygon([offset(a, &across), offset(b, &across), point { x = b.x - across.x, y = b.y - across.y }, point { x = a.x - across.x, y = a.y - across.y }]); } val first_corner: u64 = closed ? 0 : 1; val last_corner := closed ? count : count - 1; for index in first_corner..last_corner { val before := points[(index + count - 1) % count]; val here := points[index]; val after := points[(index + 1) % count]; corner(before, here, after, with, made); } if !closed { end_cap(points[1], points[0], with, made); end_cap(points[count - 2], points[count - 1], with, made); } } # The corner where a line from one point to another turns to a third: a miter, # where it's no longer than the limit, a bevel, or a round, on its outer side. def corner(before: point, here: point, after: point, with: &pen, made: &mut outline): void { val in_length := math::hypot(here.x - before.x, here.y - before.y); val out_length := math::hypot(after.x - here.x, after.y - here.y); val in_x := (here.x - before.x) / in_length; val in_y := (here.y - before.y) / in_length; val out_x := (after.x - here.x) / out_length; val out_y := (after.y - here.y) / out_length; val cross := in_x * out_y - in_y * out_x; val dot := in_x * out_x + in_y * out_y; if math::abs(cross) < 1e-12 && dot > 0 { return; } # The outer side is the one the line turns from. val side := cross > 0 ? -with.half : with.half; val first := point { x = -in_y * side, y = in_x * side }; val second := point { x = -out_y * side, y = out_x * side }; val a := offset(here, &first); val b := offset(here, &second); if with.join == line_join::round { var fan := vector::::new(); fan.append(here); arc(here, first, second, with, &mut fan); made.polygon(fan.view()); return; } if with.join == line_join::miter && dot > -0.999999 && 1 / math::sqrt((1 + dot) / 2) <= with.miter_limit { val tip := point { x = here.x + (first.x + second.x) / (1 + dot), y = here.y + (first.y + second.y) / (1 + dot) }; made.polygon([here, a, tip, b]); return; } made.polygon([here, a, b]); } # The cap at the end of a polyline, from the point before it. def end_cap(before: point, end: point, with: &pen, made: &mut outline): void { if with.cap == line_cap::round { circle(end, with.half, with, made); return; } if with.cap == line_cap::square { val across := normal(before, end, with.half); val ahead := point { x = across.y, y = -across.x }; made.polygon([offset(end, &across), point { x = end.x + across.x + ahead.x, y = end.y + across.y + ahead.y }, point { x = end.x - across.x + ahead.x, y = end.y - across.y + ahead.y }, point { x = end.x - across.x, y = end.y - across.y }]); } } # The normal of a line, at a length, to its left as it goes. def normal(from: point, to: point, length: f64): point { val size := math::hypot(to.x - from.x, to.y - from.y); return point { x = -(to.y - from.y) / size * length, y = (to.x - from.x) / size * length }; } def same_point(a: point, b: point): bool { return math::abs(a.x - b.x) < 1e-9 && math::abs(a.y - b.y) < 1e-9; } # How many steps a whole circle of a radius takes to stay within the pen's # distance of it. def circle_steps(radius: f64, with: &pen): f64 { if radius <= with.within { return 8; } val steps := math::ceil(math::pi / math::acos(1 - with.within / radius)); return steps < 8 ? 8 : (steps > 360 ? 360 : steps); } def circle(center: point, radius: f64, with: &pen, made: &mut outline): void { val steps := circle_steps(radius, with); var round := vector::::new(); for step in 0..steps as u64 { val angle := 2 * math::pi * step as f64 / steps; round.append(point { x = center.x + radius * math::cos(angle), y = center.y + radius * math::sin(angle) }); } made.polygon(round.view()); } # Appends the points of the shorter arc from one offset of a center to another. def arc(center: point, from: point, to: point, with: &pen, into: &mut vector): void { val radius := math::hypot(from.x, from.y); val begin := math::atan2(from.y, from.x); var turn := math::atan2(to.y, to.x) - begin; if turn > math::pi { turn -= 2 * math::pi; } else if turn < -math::pi { turn += 2 * math::pi; } var steps := math::ceil(math::abs(turn) / (2 * math::pi) * circle_steps(radius, with)); steps = steps < 1 ? 1 : steps; for step in 0..=steps as u64 { val angle := begin + turn * step as f64 / steps; into.append(point { x = center.x + radius * math::cos(angle), y = center.y + radius * math::sin(angle) }); } }