1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::path_builder::{Path, PathOp, PathBuilder};
use euclid::{Point2D, Vector2D};

type Point = Point2D<f32>;
type Vector = Vector2D<f32>;

pub struct StrokeStyle {
    pub width: f32,
    pub cap: LineCap,
    pub join: LineJoin,
    pub mitre_limit: f32,
    pub dash_array: Vec<f32>,
    pub dash_offset: f32,
}

pub enum LineCap {
    Round,
    Square,
    Butt
}

pub enum LineJoin {
    Round,
    Mitre,
    Bevel,
}

fn compute_normal(p0: Point, p1: Point) -> Vector {
    let ux = p1.x - p0.x;
    let uy = p1.y - p0.y;

    // this could overflow f32. Skia checks for this and
    // uses a double in that situation
    let ulen = ux.hypot(uy);
    assert!(ulen != 0.);
    // the normal is perpendicular to the *unit* vector
    Vector::new(-uy/ulen, ux/ulen)
}

fn flip(v: Vector) -> Vector {
    Vector::new(-v.x, -v.y)
}

fn cap_line(dest: &mut PathBuilder, style: &StrokeStyle, pt: Point, normal: Vector) {
    let offset = style.width / 2.;
    match style.cap {
        LineCap::Butt => { /* nothing to do */ },
        LineCap::Round => { unimplemented!() },
        LineCap::Square => {
            // parallel vector
            let v = Vector::new(normal.y, -normal.x);
            let end = pt + v * offset;
            dest.move_to(pt.x + normal.x * offset, pt.y + normal.y * offset);
            dest.line_to(end.x + normal.x * offset, end.y + normal.y * offset);
            dest.line_to(end.x + -normal.x * offset, end.y + -normal.y * offset);
            dest.line_to(pt.x - normal.x * offset, pt.y - normal.y * offset);
            dest.close();
        },
    }
}

fn bevel(dest: &mut PathBuilder, style: &StrokeStyle, pt: Point, s1_normal: Vector, s2_normal: Vector) {
    let offset = style.width / 2.;
    dest.move_to(pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset);
    dest.line_to(pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset);
    dest.line_to(pt.x, pt.y);
    dest.close();
}

/* given a normal rotate the vector 90 degrees to the right clockwise
 * This function has a period of 4. e.g. swap(swap(swap(swap(x) == x */
fn swap(a: Vector) -> Vector
{
    /* one of these needs to be negative. We choose a.x so that we rotate to the right instead of negating */
    return Vector::new(a.y, -a.x);
}

fn unperp(a: Vector) -> Vector
{
    swap(a)
}

/* rotate a vector 90 degrees to the left */
fn perp(v: Vector) -> Vector
{
    Vector::new(-v.y, v.x)
}

fn dot(a: Vector, b: Vector) -> f32
{
    a.x * b.x + a.y * b.y
}

/* Finds the intersection of two lines each defined by a point and a normal.
   From "Example 2: Find the intersection of two lines" of
   "The Pleasures of "Perp Dot" Products"
   F. S. Hill, Jr. */
fn line_intersection(A: Point, a_perp: Vector, B: Point, b_perp: Vector) -> Point
{
    let a = unperp(a_perp);
    let c = B - A;
    let denom = dot(b_perp, a);
    if denom == 0.0 {
        panic!("trouble")
    }

    let t = dot(b_perp, c) / denom;

    let intersection = Point::new(A.x + t * (a.x),
                                  A.y + t * (a.y));

    intersection
}

fn is_interior_angle(a: Vector, b: Vector) -> bool {
    /* angles of 180 and 0 degress will evaluate to 0, however
     * we to treat 180 as an interior angle and 180 as an exterior angle */
    dot(perp(a), b) > 0. || a == b /* 0 degrees is interior */
}

fn join_line(dest: &mut PathBuilder, style: &StrokeStyle, pt: Point, mut s1_normal: Vector, mut s2_normal: Vector) {

    if is_interior_angle(s1_normal, s2_normal) {
        s2_normal = flip(s2_normal);
        s1_normal = flip(s1_normal);
        std::mem::swap(&mut s1_normal, &mut s2_normal);
    }

    let offset = style.width / 2.;
    match style.join {
        LineJoin::Round => { unimplemented!() },
        LineJoin::Mitre => {
            let in_dot_out = -s1_normal.x * s2_normal.x + -s1_normal.y * s2_normal.y;
            if 2. <= style.mitre_limit*style.mitre_limit * (1. - in_dot_out) {
                let start = pt + s1_normal * offset;
                let end = pt + s2_normal * offset;
                let intersection = line_intersection(start, s1_normal, end, s2_normal);
                dest.move_to(pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset);
                dest.line_to(intersection.x, intersection.y);
                dest.line_to(pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset);
                dest.line_to(pt.x, pt.y);
                dest.close();
            } else {
                bevel(dest, style, pt, s1_normal, s2_normal);
            }
        },
        LineJoin::Bevel => {
            bevel(dest, style, pt, s1_normal, s2_normal);
        },
    }
}


pub fn stroke_to_path(path: &Path, style: &StrokeStyle) -> Path {
    let mut cur_x = 0.;
    let mut cur_y = 0.;
    let mut stroked_path = PathBuilder::new();
    let mut last_normal = Vector::zero();
    let half_width = style.width / 2.;
    let mut start_point = None;
    for op in &path.ops {
        match *op {
            PathOp::MoveTo(x, y) => {
                if let Some((point, normal)) = start_point {
                    // cap end
                    cap_line(&mut stroked_path, style, Point::new(cur_x, cur_y), last_normal);
                    // cap beginning
                    cap_line(&mut stroked_path, style, point, flip(normal));
                }
                start_point = None;
                cur_x = x;
                cur_y = y;
            }
            PathOp::LineTo(x, y) => {
                let normal = compute_normal(Point2D::new(cur_x, cur_y), Point2D::new(x, y));
                if start_point.is_none() {
                    start_point = Some((Point::new(cur_x, cur_y), normal));
                } else {
                    join_line(&mut stroked_path, style, Point::new(cur_x, cur_y), last_normal, normal);
                }

                stroked_path.move_to(cur_x + normal.x * half_width, cur_y + normal.y * half_width);
                stroked_path.line_to(x + normal.x * half_width, y + normal.y * half_width);
                stroked_path.line_to(x + -normal.x * half_width, y + -normal.y * half_width);
                stroked_path.line_to(cur_x - normal.x * half_width, cur_y - normal.y * half_width);
                stroked_path.close();
                last_normal = normal;

                cur_x = x;
                cur_y = y;

            }
            PathOp::Close => {
                if let Some((point, normal)) = start_point {
                    let last_normal = compute_normal(Point2D::new(cur_x, cur_y), Point2D::new(point.x, point.y));

                    stroked_path.move_to(cur_x + normal.x * half_width, cur_y + normal.y * half_width);
                    stroked_path.line_to(point.x + normal.x * half_width, point.y + normal.y * half_width);
                    stroked_path.line_to(point.x + -normal.x * half_width, point.y + -normal.y * half_width);
                    stroked_path.line_to(cur_x - normal.x * half_width, cur_y - normal.y * half_width);
                    stroked_path.close();

                    join_line(&mut stroked_path, style, point, last_normal, normal);
                }
            },
            PathOp::QuadTo(..) => {
                panic!("Only flat paths handled")
            }
            PathOp::CubicTo(..) => {
                panic!("Only flat paths handled")
            }
        }
    }
    stroked_path.finish()
}