| 1 |
|
| 2 |
use std::default::Default; |
| 3 |
|
| 4 |
use bezierflattener::CBezierFlattener; |
| 5 |
use tri_rasterize::rasterize_to_mask; |
| 6 |
|
| 7 |
use crate::{bezierflattener::{CFlatteningSink, GpPointR, HRESULT, S_OK, CBezier}}; |
| 8 |
|
| 9 |
mod bezierflattener; |
| 10 |
pub mod tri_rasterize; |
| 11 |
#[cfg(feature = "c_bindings")] |
| 12 |
pub mod c_bindings; |
| 13 |
|
| 14 |
#[derive(Clone, Copy, PartialEq, Debug)] |
| 15 |
pub enum Winding { |
| 16 |
EvenOdd, |
| 17 |
NonZero, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Clone, Copy, Debug)] |
| 21 |
pub enum PathOp { |
| 22 |
MoveTo(Point), |
| 23 |
LineTo(Point), |
| 24 |
QuadTo(Point, Point), |
| 25 |
CubicTo(Point, Point, Point), |
| 26 |
Close, |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
/// Represents a complete path usable for filling or stroking. |
| 31 |
#[derive(Clone, Debug)] |
| 32 |
pub struct Path { |
| 33 |
pub ops: Vec<PathOp>, |
| 34 |
pub winding: Winding, |
| 35 |
} |
| 36 |
|
| 37 |
pub type Point = euclid::default::Point2D<f32>; |
| 38 |
pub type Transform = euclid::default::Transform2D<f32>; |
| 39 |
pub type Vector = euclid::default::Vector2D<f32>; |
| 40 |
|
| 41 |
|
| 42 |
#[derive(Clone, Copy, PartialEq, Debug)] |
| 43 |
#[repr(C)] |
| 44 |
pub enum LineCap { |
| 45 |
Round, |
| 46 |
Square, |
| 47 |
Butt, |
| 48 |
} |
| 49 |
|
| 50 |
#[derive(Clone, Copy, PartialEq, Debug)] |
| 51 |
#[repr(C)] |
| 52 |
pub enum LineJoin { |
| 53 |
Round, |
| 54 |
Miter, |
| 55 |
Bevel, |
| 56 |
} |
| 57 |
|
| 58 |
#[derive(Clone, PartialEq, Debug)] |
| 59 |
#[repr(C)] |
| 60 |
pub struct StrokeStyle { |
| 61 |
pub width: f32, |
| 62 |
pub cap: LineCap, |
| 63 |
pub join: LineJoin, |
| 64 |
pub miter_limit: f32, |
| 65 |
} |
| 66 |
|
| 67 |
impl Default for StrokeStyle { |
| 68 |
fn default() -> Self { |
| 69 |
StrokeStyle { |
| 70 |
width: 1., |
| 71 |
cap: LineCap::Butt, |
| 72 |
join: LineJoin::Miter, |
| 73 |
miter_limit: 10., |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
#[derive(Debug)] |
| 78 |
pub struct Vertex { |
| 79 |
pub x: f32, |
| 80 |
pub y: f32, |
| 81 |
pub coverage: f32 |
| 82 |
} |
| 83 |
|
| 84 |
/// A helper struct used for constructing a `Path`. |
| 85 |
pub struct PathBuilder<'z> { |
| 86 |
output_buffer: Option<&'z mut [Vertex]>, |
| 87 |
output_buffer_offset: usize, |
| 88 |
vertices: Vec<Vertex>, |
| 89 |
coverage: f32, |
| 90 |
aa: bool |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
impl<'z> PathBuilder<'z> { |
| 96 |
pub fn new(coverage: f32) -> PathBuilder<'z> { |
| 97 |
PathBuilder { |
| 98 |
output_buffer: None, |
| 99 |
output_buffer_offset: 0, |
| 100 |
vertices: Vec::new(), |
| 101 |
coverage, |
| 102 |
aa: true |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
pub fn set_output_buffer(&mut self, output_buffer: &'z mut [Vertex]) { |
| 107 |
assert!(self.output_buffer.is_none()); |
| 108 |
self.output_buffer = Some(output_buffer); |
| 109 |
} |
| 110 |
|
| 111 |
pub fn push_tri_with_coverage(&mut self, x1: f32, y1: f32, c1: f32, x2: f32, y2: f32, c2: f32, x3: f32, y3: f32, c3: f32) { |
| 112 |
let v1 = Vertex { x: x1, y: y1, coverage: c1 }; |
| 113 |
let v2 = Vertex { x: x2, y: y2, coverage: c2 }; |
| 114 |
let v3 = Vertex { x: x3, y: y3, coverage: c3 }; |
| 115 |
if let Some(output_buffer) = &mut self.output_buffer { |
| 116 |
let offset = self.output_buffer_offset; |
| 117 |
if offset + 3 <= output_buffer.len() { |
| 118 |
output_buffer[offset] = v1; |
| 119 |
output_buffer[offset + 1] = v2; |
| 120 |
output_buffer[offset + 2] = v3; |
| 121 |
} |
| 122 |
self.output_buffer_offset = offset + 3; |
| 123 |
} else { |
| 124 |
self.vertices.push(v1); |
| 125 |
self.vertices.push(v2); |
| 126 |
self.vertices.push(v3); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
pub fn push_tri(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) { |
| 131 |
self.push_tri_with_coverage(x1, y1, self.coverage, x2, y2, self.coverage, x3, y3, self.coverage); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
// x3, y3 is the full coverage vert |
| 136 |
pub fn tri_ramp(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) { |
| 137 |
self.push_tri_with_coverage(x1, y1, 0., x2, y2, 0., x3, y3, self.coverage); |
| 138 |
} |
| 139 |
|
| 140 |
pub fn quad(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, x4: f32, y4: f32) { |
| 141 |
self.push_tri(x1, y1, x2, y2, x3, y3); |
| 142 |
self.push_tri(x3, y3, x4, y4, x1, y1); |
| 143 |
} |
| 144 |
|
| 145 |
pub fn ramp(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, x4: f32, y4: f32) { |
| 146 |
self.push_tri_with_coverage(x1, y1, self.coverage, x2, y2, 0., x3, y3, 0.); |
| 147 |
self.push_tri_with_coverage(x3, y3, 0., x4, y4, self.coverage, x1, y1, self.coverage); |
| 148 |
} |
| 149 |
|
| 150 |
// first edge is outside |
| 151 |
pub fn tri(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32) { |
| 152 |
self.push_tri(x1, y1, x2, y2, x3, y3); |
| 153 |
} |
| 154 |
|
| 155 |
pub fn arc_wedge(&mut self, c: Point, radius: f32, a: Vector, b: Vector) { |
| 156 |
arc(self, c.x, c.y, radius, a, b); |
| 157 |
} |
| 158 |
|
| 159 |
/// Completes the current path |
| 160 |
pub fn finish(self) -> Box<[Vertex]> { |
| 161 |
self.vertices.into_boxed_slice() |
| 162 |
} |
| 163 |
|
| 164 |
pub fn get_output_buffer_size(&self) -> Option<usize> { |
| 165 |
if self.output_buffer.is_some() { |
| 166 |
Some(self.output_buffer_offset) |
| 167 |
} else { |
| 168 |
None |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
fn compute_normal(p0: Point, p1: Point) -> Option<Vector> { |
| 176 |
let ux = p1.x - p0.x; |
| 177 |
let uy = p1.y - p0.y; |
| 178 |
|
| 179 |
// this could overflow f32. Skia checks for this and |
| 180 |
// uses a double in that situation |
| 181 |
let ulen = ux.hypot(uy); |
| 182 |
if ulen == 0. { |
| 183 |
return None; |
| 184 |
} |
| 185 |
// the normal is perpendicular to the *unit* vector |
| 186 |
Some(Vector::new(-uy / ulen, ux / ulen)) |
| 187 |
} |
| 188 |
|
| 189 |
fn flip(v: Vector) -> Vector { |
| 190 |
Vector::new(-v.x, -v.y) |
| 191 |
} |
| 192 |
|
| 193 |
/* Compute a spline approximation of the arc |
| 194 |
centered at xc, yc from the angle a to the angle b |
| 195 |
|
| 196 |
The angle between a and b should not be more than a |
| 197 |
quarter circle (pi/2) |
| 198 |
|
| 199 |
The approximation is similar to an approximation given in: |
| 200 |
"Approximation of a cubic bezier curve by circular arcs and vice versa" |
| 201 |
by Alekas Riškus. However that approximation becomes unstable when the |
| 202 |
angle of the arc approaches 0. |
| 203 |
|
| 204 |
This approximation is inspired by a discusion with Boris Zbarsky |
| 205 |
and essentially just computes: |
| 206 |
|
| 207 |
h = 4.0/3.0 * tan ((angle_B - angle_A) / 4.0); |
| 208 |
|
| 209 |
without converting to polar coordinates. |
| 210 |
|
| 211 |
A different way to do this is covered in "Approximation of a cubic bezier |
| 212 |
curve by circular arcs and vice versa" by Alekas Riškus. However, the method |
| 213 |
presented there doesn't handle arcs with angles close to 0 because it |
| 214 |
divides by the perp dot product of the two angle vectors. |
| 215 |
*/ |
| 216 |
|
| 217 |
fn arc_segment_tri(path: &mut PathBuilder, xc: f32, yc: f32, radius: f32, a: Vector, b: Vector) { |
| 218 |
let r_sin_a = radius * a.y; |
| 219 |
let r_cos_a = radius * a.x; |
| 220 |
let r_sin_b = radius * b.y; |
| 221 |
let r_cos_b = radius * b.x; |
| 222 |
|
| 223 |
|
| 224 |
/* bisect the angle between 'a' and 'b' with 'mid' */ |
| 225 |
let mut mid = a + b; |
| 226 |
mid /= mid.length(); |
| 227 |
|
| 228 |
/* bisect the angle between 'a' and 'mid' with 'mid2' this is parallel to a |
| 229 |
* line with angle (B - A)/4 */ |
| 230 |
let mid2 = a + mid; |
| 231 |
|
| 232 |
let h = (4. / 3.) * dot(perp(a), mid2) / dot(a, mid2); |
| 233 |
|
| 234 |
let last_point = GpPointR { x: (xc + r_cos_a), y: (yc + r_sin_a) }; |
| 235 |
let initial_normal = GpPointR { x: a.x, y: a.y }; |
| 236 |
|
| 237 |
|
| 238 |
struct Target<'a, 'b> { last_point: GpPointR, last_normal: GpPointR, xc: f32, yc: f32, path: &'a mut PathBuilder<'b> } |
| 239 |
impl<'a, 'b> CFlatteningSink for Target<'a, 'b> { |
| 240 |
fn AcceptPointAndTangent(&mut self, |
| 241 |
pt: &GpPointR, |
| 242 |
// The point |
| 243 |
vec: &GpPointR, |
| 244 |
// The tangent there |
| 245 |
_last: bool |
| 246 |
// Is this the last point on the curve? |
| 247 |
) -> HRESULT { |
| 248 |
if self.path.aa { |
| 249 |
let len = vec.Norm(); |
| 250 |
let normal = *vec/len; |
| 251 |
let normal = GpPointR { x: -normal.y, y: normal.x }; |
| 252 |
// FIXME: we probably need more width here because |
| 253 |
// the normals are not perpendicular with the edge |
| 254 |
let width = 0.5; |
| 255 |
|
| 256 |
self.path.ramp( |
| 257 |
pt.x - normal.x * width, |
| 258 |
pt.y - normal.y * width, |
| 259 |
pt.x + normal.x * width, |
| 260 |
pt.y + normal.y * width, |
| 261 |
self.last_point.x + self.last_normal.x * width, |
| 262 |
self.last_point.y + self.last_normal.y * width, |
| 263 |
self.last_point.x - self.last_normal.x * width, |
| 264 |
self.last_point.y - self.last_normal.y * width, ); |
| 265 |
self.path.push_tri( |
| 266 |
self.last_point.x - self.last_normal.x * 0.5, |
| 267 |
self.last_point.y - self.last_normal.y * 0.5, |
| 268 |
pt.x - normal.x * 0.5, |
| 269 |
pt.y - normal.y * 0.5, |
| 270 |
self.xc, self.yc); |
| 271 |
self.last_normal = normal; |
| 272 |
|
| 273 |
} else { |
| 274 |
self.path.push_tri(self.last_point.x, self.last_point.y, pt.x, pt.y, self.xc, self.yc); |
| 275 |
} |
| 276 |
self.last_point = pt.clone(); |
| 277 |
return S_OK; |
| 278 |
} |
| 279 |
|
| 280 |
fn AcceptPoint(&mut self, |
| 281 |
pt: &GpPointR, |
| 282 |
// The point |
| 283 |
_t: f32, |
| 284 |
// Parameter we're at |
| 285 |
_aborted: &mut bool, |
| 286 |
_last_point: bool) -> HRESULT { |
| 287 |
self.path.push_tri(self.last_point.x, self.last_point.y, pt.x, pt.y, self.xc, self.yc); |
| 288 |
self.last_point = pt.clone(); |
| 289 |
return S_OK; |
| 290 |
} |
| 291 |
fn FirstTangent(&mut self, _: Option<GpPointR>) { } |
| 292 |
} |
| 293 |
let bezier = CBezier::new([GpPointR { x: (xc + r_cos_a), y: (yc + r_sin_a), }, |
| 294 |
GpPointR { x: (xc + r_cos_a - h * r_sin_a), y: (yc + r_sin_a + h * r_cos_a), }, |
| 295 |
GpPointR { x: (xc + r_cos_b + h * r_sin_b), y: (yc + r_sin_b - h * r_cos_b), }, |
| 296 |
GpPointR { x: (xc + r_cos_b), y: (yc + r_sin_b), }]); |
| 297 |
if bezier.is_degenerate() { |
| 298 |
return; |
| 299 |
} |
| 300 |
let mut t = Target{ last_point, last_normal: initial_normal, xc, yc, path }; |
| 301 |
let mut f = CBezierFlattener::new(&bezier, &mut t, 0.25); |
| 302 |
if f.GetFirstTangent().is_none() { |
| 303 |
// the curve is not completely degenerate but degenerate enough that we can't flatten it |
| 304 |
|
| 305 |
// draw a single triangle for the curve |
| 306 |
let width = 0.5; |
| 307 |
let next_point = GpPointR { x: (xc + r_cos_b), y: (yc + r_sin_b) }; |
| 308 |
if path.aa { |
| 309 |
// XXX: This code will potentially run into trouble if the radius is very small |
| 310 |
// but the general case suffers the same problem. |
| 311 |
path.ramp( |
| 312 |
next_point.x - b.x * width, |
| 313 |
next_point.y - b.y * width, |
| 314 |
next_point.x + b.x * width, |
| 315 |
next_point.y + b.y * width, |
| 316 |
last_point.x + a.x * width, |
| 317 |
last_point.y + a.y * width, |
| 318 |
last_point.x - a.x * width, |
| 319 |
last_point.y - a.y * width); |
| 320 |
path.push_tri( |
| 321 |
last_point.x - a.x * 0.5, |
| 322 |
last_point.y - a.y * 0.5, |
| 323 |
next_point.x - b.x * 0.5, |
| 324 |
next_point.y - b.y * 0.5, |
| 325 |
xc, yc); |
| 326 |
} else { |
| 327 |
path.push_tri(last_point.x, last_point.y, next_point.x, next_point.y, xc, yc); |
| 328 |
} |
| 329 |
} else { |
| 330 |
f.Flatten(true); |
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
/* The angle between the vectors must be <= pi */ |
| 336 |
fn bisect(a: Vector, b: Vector) -> Vector { |
| 337 |
let mut mid; |
| 338 |
if dot(a, b) >= 0. { |
| 339 |
/* if the angle between a and b is accute, then we can |
| 340 |
* just add the vectors and normalize */ |
| 341 |
mid = a + b; |
| 342 |
} else { |
| 343 |
/* otherwise, we can flip a, add it |
| 344 |
* and then use the perpendicular of the result */ |
| 345 |
mid = flip(a) + b; |
| 346 |
mid = perp(mid); |
| 347 |
} |
| 348 |
|
| 349 |
/* normalize */ |
| 350 |
/* because we assume that 'a' and 'b' are normalized, we can use |
| 351 |
* sqrt instead of hypot because the range of mid is limited */ |
| 352 |
let mid_len = mid.x * mid.x + mid.y * mid.y; |
| 353 |
let len = mid_len.sqrt(); |
| 354 |
return mid / len; |
| 355 |
} |
| 356 |
|
| 357 |
/* The angle between the vectors must be <= 180 degrees */ |
| 358 |
fn arc(path: &mut PathBuilder, xc: f32, yc: f32, radius: f32, a: Vector, b: Vector) { |
| 359 |
// Depending on the magnitude of the angle use 0, 1 or 2 arc segments. |
| 360 |
if dot(a, b) == 1.0 { |
| 361 |
// the angle is 0 degrees, do nothing |
| 362 |
} else if dot(a, b) >= 0. { |
| 363 |
// the angle is less than 90 degrees |
| 364 |
arc_segment_tri(path, xc, yc, radius, a, b); |
| 365 |
} else { |
| 366 |
/* find a vector that bisects the angle between a and b */ |
| 367 |
let mid_v = bisect(a, b); |
| 368 |
|
| 369 |
/* construct the arc using two curve segments */ |
| 370 |
arc_segment_tri(path, xc, yc, radius, a, mid_v); |
| 371 |
arc_segment_tri(path, xc, yc, radius, mid_v, b); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
fn join_round(path: &mut PathBuilder, center: Point, a: Vector, b: Vector, radius: f32) { |
| 377 |
/* |
| 378 |
int ccw = dot (perp (b), a) >= 0; // XXX: is this always true? |
| 379 |
yes, otherwise we have an interior angle. |
| 380 |
assert (ccw); |
| 381 |
*/ |
| 382 |
arc(path, center.x, center.y, radius, a, b); |
| 383 |
}*/ |
| 384 |
|
| 385 |
fn cap_line(dest: &mut PathBuilder, style: &StrokeStyle, pt: Point, normal: Vector) { |
| 386 |
let offset = style.width / 2.; |
| 387 |
match style.cap { |
| 388 |
LineCap::Butt => { |
| 389 |
if dest.aa { |
| 390 |
let half_width = offset; |
| 391 |
let end = pt; |
| 392 |
let v = Vector::new(normal.y, -normal.x); |
| 393 |
// end |
| 394 |
dest.ramp( |
| 395 |
end.x - normal.x * (half_width - 0.5), |
| 396 |
end.y - normal.y * (half_width - 0.5), |
| 397 |
end.x + v.x - normal.x * (half_width - 0.5), |
| 398 |
end.y + v.y - normal.y * (half_width - 0.5), |
| 399 |
end.x + v.x + normal.x * (half_width - 0.5), |
| 400 |
end.y + v.y + normal.y * (half_width - 0.5), |
| 401 |
end.x + normal.x * (half_width - 0.5), |
| 402 |
end.y + normal.y * (half_width - 0.5), |
| 403 |
); |
| 404 |
dest.tri_ramp( |
| 405 |
end.x + v.x - normal.x * (half_width - 0.5), |
| 406 |
end.y + v.y - normal.y * (half_width - 0.5), |
| 407 |
end.x - normal.x * (half_width + 0.5), |
| 408 |
end.y - normal.y * (half_width + 0.5), |
| 409 |
end.x - normal.x * (half_width - 0.5), |
| 410 |
end.y - normal.y * (half_width - 0.5)); |
| 411 |
dest.tri_ramp( |
| 412 |
end.x + v.x + normal.x * (half_width - 0.5), |
| 413 |
end.y + v.y + normal.y * (half_width - 0.5), |
| 414 |
end.x + normal.x * (half_width + 0.5), |
| 415 |
end.y + normal.y * (half_width + 0.5), |
| 416 |
end.x + normal.x * (half_width - 0.5), |
| 417 |
end.y + normal.y * (half_width - 0.5)); |
| 418 |
} |
| 419 |
} |
| 420 |
LineCap::Round => { |
| 421 |
dest.arc_wedge(pt, offset, normal, flip(normal)); |
| 422 |
} |
| 423 |
LineCap::Square => { |
| 424 |
// parallel vector |
| 425 |
let v = Vector::new(normal.y, -normal.x); |
| 426 |
let end = pt + v * offset; |
| 427 |
if dest.aa { |
| 428 |
let half_width = offset; |
| 429 |
let offset = offset - 0.5; |
| 430 |
dest.ramp( |
| 431 |
end.x + normal.x * (half_width - 0.5), |
| 432 |
end.y + normal.y * (half_width - 0.5), |
| 433 |
end.x + normal.x * (half_width + 0.5), |
| 434 |
end.y + normal.y * (half_width + 0.5), |
| 435 |
pt.x + normal.x * (half_width + 0.5), |
| 436 |
pt.y + normal.y * (half_width + 0.5), |
| 437 |
pt.x + normal.x * (half_width - 0.5), |
| 438 |
pt.y + normal.y * (half_width - 0.5), |
| 439 |
); |
| 440 |
dest.quad(pt.x + normal.x * offset, pt.y + normal.y * offset, |
| 441 |
end.x + normal.x * offset, end.y + normal.y * offset, |
| 442 |
end.x + -normal.x * offset, end.y + -normal.y * offset, |
| 443 |
pt.x - normal.x * offset, pt.y - normal.y * offset); |
| 444 |
|
| 445 |
dest.ramp( |
| 446 |
pt.x - normal.x * (half_width - 0.5), |
| 447 |
pt.y - normal.y * (half_width - 0.5), |
| 448 |
pt.x - normal.x * (half_width + 0.5), |
| 449 |
pt.y - normal.y * (half_width + 0.5), |
| 450 |
end.x - normal.x * (half_width + 0.5), |
| 451 |
end.y - normal.y * (half_width + 0.5), |
| 452 |
end.x - normal.x * (half_width - 0.5), |
| 453 |
end.y - normal.y * (half_width - 0.5)); |
| 454 |
|
| 455 |
// end |
| 456 |
dest.ramp( |
| 457 |
end.x - normal.x * (half_width - 0.5), |
| 458 |
end.y - normal.y * (half_width - 0.5), |
| 459 |
end.x + v.x - normal.x * (half_width - 0.5), |
| 460 |
end.y + v.y - normal.y * (half_width - 0.5), |
| 461 |
end.x + v.x + normal.x * (half_width - 0.5), |
| 462 |
end.y + v.y + normal.y * (half_width - 0.5), |
| 463 |
end.x + normal.x * (half_width - 0.5), |
| 464 |
end.y + normal.y * (half_width - 0.5), |
| 465 |
); |
| 466 |
|
| 467 |
// corners |
| 468 |
dest.tri_ramp( |
| 469 |
end.x + v.x - normal.x * (half_width - 0.5), |
| 470 |
end.y + v.y - normal.y * (half_width - 0.5), |
| 471 |
end.x - normal.x * (half_width + 0.5), |
| 472 |
end.y - normal.y * (half_width + 0.5), |
| 473 |
end.x - normal.x * (half_width - 0.5), |
| 474 |
end.y - normal.y * (half_width - 0.5)); |
| 475 |
dest.tri_ramp( |
| 476 |
end.x + v.x + normal.x * (half_width - 0.5), |
| 477 |
end.y + v.y + normal.y * (half_width - 0.5), |
| 478 |
end.x + normal.x * (half_width + 0.5), |
| 479 |
end.y + normal.y * (half_width + 0.5), |
| 480 |
end.x + normal.x * (half_width - 0.5), |
| 481 |
end.y + normal.y * (half_width - 0.5)); |
| 482 |
} else { |
| 483 |
dest.quad(pt.x + normal.x * offset, pt.y + normal.y * offset, |
| 484 |
end.x + normal.x * offset, end.y + normal.y * offset, |
| 485 |
end.x + -normal.x * offset, end.y + -normal.y * offset, |
| 486 |
pt.x - normal.x * offset, pt.y - normal.y * offset); |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
fn bevel( |
| 493 |
dest: &mut PathBuilder, |
| 494 |
style: &StrokeStyle, |
| 495 |
pt: Point, |
| 496 |
s1_normal: Vector, |
| 497 |
s2_normal: Vector, |
| 498 |
) { |
| 499 |
let offset = style.width / 2.; |
| 500 |
if dest.aa { |
| 501 |
let width = 1.; |
| 502 |
let offset = offset - width / 2.; |
| 503 |
//XXX: we should be able to just bisect the two norms to get this |
| 504 |
let diff = match (s2_normal - s1_normal).try_normalize() { |
| 505 |
Some(diff) => diff, |
| 506 |
None => return, |
| 507 |
}; |
| 508 |
let edge_normal = perp(diff); |
| 509 |
|
| 510 |
dest.tri(pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset, |
| 511 |
pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset, |
| 512 |
pt.x, pt.y); |
| 513 |
|
| 514 |
dest.tri_ramp(pt.x + s1_normal.x * (offset + width), pt.y + s1_normal.y * (offset + width), |
| 515 |
pt.x + s1_normal.x * offset + edge_normal.x, pt.y + s1_normal.y * offset + edge_normal.y, |
| 516 |
pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset); |
| 517 |
dest.ramp( |
| 518 |
pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset, |
| 519 |
pt.x + s2_normal.x * offset + edge_normal.x, pt.y + s2_normal.y * offset + edge_normal.y, |
| 520 |
pt.x + s1_normal.x * offset + edge_normal.x, pt.y + s1_normal.y * offset + edge_normal.y, |
| 521 |
pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset, |
| 522 |
); |
| 523 |
dest.tri_ramp(pt.x + s2_normal.x * (offset + width), pt.y + s2_normal.y * (offset + width), |
| 524 |
pt.x + s2_normal.x * offset + edge_normal.x, pt.y + s2_normal.y * offset + edge_normal.y, |
| 525 |
pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset); |
| 526 |
} else { |
| 527 |
dest.tri(pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset, |
| 528 |
pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset, |
| 529 |
pt.x, pt.y); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
/* given a normal rotate the vector 90 degrees to the right clockwise |
| 534 |
* This function has a period of 4. e.g. swap(swap(swap(swap(x) == x */ |
| 535 |
fn swap(a: Vector) -> Vector { |
| 536 |
/* one of these needs to be negative. We choose a.x so that we rotate to the right instead of negating */ |
| 537 |
Vector::new(a.y, -a.x) |
| 538 |
} |
| 539 |
|
| 540 |
fn unperp(a: Vector) -> Vector { |
| 541 |
swap(a) |
| 542 |
} |
| 543 |
|
| 544 |
/* rotate a vector 90 degrees to the left */ |
| 545 |
fn perp(v: Vector) -> Vector { |
| 546 |
Vector::new(-v.y, v.x) |
| 547 |
} |
| 548 |
|
| 549 |
fn dot(a: Vector, b: Vector) -> f32 { |
| 550 |
a.x * b.x + a.y * b.y |
| 551 |
} |
| 552 |
|
| 553 |
fn cross(a: Vector, b: Vector) -> f32 { |
| 554 |
return a.x * b.y - a.y * b.x; |
| 555 |
} |
| 556 |
|
| 557 |
/* Finds the intersection of two lines each defined by a point and a normal. |
| 558 |
From "Example 2: Find the intersection of two lines" of |
| 559 |
"The Pleasures of "Perp Dot" Products" |
| 560 |
F. S. Hill, Jr. */ |
| 561 |
fn line_intersection(a: Point, a_perp: Vector, b: Point, b_perp: Vector) -> Option<Point> { |
| 562 |
let a_parallel = unperp(a_perp); |
| 563 |
let c = b - a; |
| 564 |
let denom = dot(b_perp, a_parallel); |
| 565 |
if denom == 0.0 { |
| 566 |
return None; |
| 567 |
} |
| 568 |
|
| 569 |
let t = dot(b_perp, c) / denom; |
| 570 |
|
| 571 |
let intersection = Point::new(a.x + t * (a_parallel.x), a.y + t * (a_parallel.y)); |
| 572 |
|
| 573 |
Some(intersection) |
| 574 |
} |
| 575 |
|
| 576 |
fn is_interior_angle(a: Vector, b: Vector) -> bool { |
| 577 |
/* angles of 180 and 0 degress will evaluate to 0, however |
| 578 |
* we to treat 0 as an interior angle and 180 as an exterior angle */ |
| 579 |
dot(perp(a), b) > 0. || a == b /* 0 degrees is interior */ |
| 580 |
} |
| 581 |
|
| 582 |
fn join_line( |
| 583 |
dest: &mut PathBuilder, |
| 584 |
style: &StrokeStyle, |
| 585 |
pt: Point, |
| 586 |
mut s1_normal: Vector, |
| 587 |
mut s2_normal: Vector, |
| 588 |
) { |
| 589 |
if is_interior_angle(s1_normal, s2_normal) { |
| 590 |
s2_normal = flip(s2_normal); |
| 591 |
s1_normal = flip(s1_normal); |
| 592 |
std::mem::swap(&mut s1_normal, &mut s2_normal); |
| 593 |
} |
| 594 |
|
| 595 |
// XXX: joining uses `pt` which can cause seams because it lies halfway on a line and the |
| 596 |
// rasterizer may not find exactly the same spot |
| 597 |
let mut offset = style.width / 2.; |
| 598 |
|
| 599 |
match style.join { |
| 600 |
LineJoin::Round => { |
| 601 |
dest.arc_wedge(pt, offset, s1_normal, s2_normal); |
| 602 |
} |
| 603 |
LineJoin::Miter => { |
| 604 |
if dest.aa { |
| 605 |
offset -= 0.5; |
| 606 |
} |
| 607 |
let in_dot_out = -s1_normal.x * s2_normal.x + -s1_normal.y * s2_normal.y; |
| 608 |
if 2. <= style.miter_limit * style.miter_limit * (1. - in_dot_out) { |
| 609 |
let start = pt + s1_normal * offset; |
| 610 |
let end = pt + s2_normal * offset; |
| 611 |
if let Some(intersection) = line_intersection(start, s1_normal, end, s2_normal) { |
| 612 |
// We won't have an intersection if the segments are parallel |
| 613 |
if dest.aa { |
| 614 |
let ramp_start = pt + s1_normal * (offset + 1.); |
| 615 |
let ramp_end = pt + s2_normal * (offset + 1.); |
| 616 |
|
| 617 |
// The following diagram is inspired by the DoLimitedMiter code |
| 618 |
// from WpfGfx. Their math doesn't make sense to me so |
| 619 |
// there's an original derivation from jgilbert below: |
| 620 |
// |
| 621 |
// offset point |
| 622 |
// --*---------------------- offset line |
| 623 |
// | * |
| 624 |
// |* |
| 625 |
// * clip point |
| 626 |
// *|s a spine |
| 627 |
// *-- offset ................ |
| 628 |
// q| point . |
| 629 |
// | . |
| 630 |
// | . |
| 631 |
// | . |
| 632 |
// | - r - . ------- |
| 633 |
// | . | |
| 634 |
// | . | |
| 635 |
// |
| 636 |
// b = a/2 |
| 637 |
// r/(q+r) = cos b => q + r = r/cos b => q = r/cos b - r |
| 638 |
// q/s = sin b/cos b => s = q cos b/sin b |
| 639 |
// s = (r/cos b - r) * cos b/sin b = (r - r cos b)/sin b |
| 640 |
// sub in r = 1 |
| 641 |
// s = (1 - cos b)/sin b |
| 642 |
// |
| 643 |
// rearrange so that we don't have denominator of 0 when b = 0 (parallel lines) |
| 644 |
// this prevents numerical instability in that case |
| 645 |
// |
| 646 |
// (1 - cos b)/sin b => (1 - cos b)(1 + cos b)/(sin b * (1 + cos b)) |
| 647 |
// => (1 - cos^2 b) / (sin b * (1 + cos b) |
| 648 |
// => sin^2 b / sin b * (1 + cos b) |
| 649 |
// => sin b / (1 + cos b) |
| 650 |
|
| 651 |
let mid = bisect(s1_normal, s2_normal); |
| 652 |
|
| 653 |
// cross = sin, dot = cos |
| 654 |
let cos = dot(s1_normal, mid); |
| 655 |
let s = cross(mid, s1_normal)/(1. + cos); |
| 656 |
|
| 657 |
// compute the intersection in a more stable way |
| 658 |
let intersection = pt + mid * (offset / cos); |
| 659 |
|
| 660 |
let ramp_s1 = intersection + s1_normal * 1. + unperp(s1_normal) * s; |
| 661 |
let ramp_s2 = intersection + s2_normal * 1. + perp(s2_normal) * s; |
| 662 |
|
| 663 |
dest.ramp(intersection.x, intersection.y, |
| 664 |
ramp_s1.x, ramp_s1.y, |
| 665 |
ramp_start.x, ramp_start.y, |
| 666 |
pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset, |
| 667 |
); |
| 668 |
dest.ramp(pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset, |
| 669 |
ramp_end.x, ramp_end.y, |
| 670 |
ramp_s2.x, ramp_s2.y, |
| 671 |
intersection.x, intersection.y); |
| 672 |
|
| 673 |
// put a flat cap on the end |
| 674 |
dest.tri_ramp(ramp_s1.x, ramp_s1.y, ramp_s2.x, ramp_s2.y, intersection.x, intersection.y); |
| 675 |
|
| 676 |
dest.quad(pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset, |
| 677 |
intersection.x, intersection.y, |
| 678 |
pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset, |
| 679 |
pt.x, pt.y); |
| 680 |
} else { |
| 681 |
dest.quad(pt.x + s1_normal.x * offset, pt.y + s1_normal.y * offset, |
| 682 |
intersection.x, intersection.y, |
| 683 |
pt.x + s2_normal.x * offset, pt.y + s2_normal.y * offset, |
| 684 |
pt.x, pt.y); |
| 685 |
} |
| 686 |
} |
| 687 |
} else { |
| 688 |
bevel(dest, style, pt, s1_normal, s2_normal); |
| 689 |
} |
| 690 |
} |
| 691 |
LineJoin::Bevel => { |
| 692 |
bevel(dest, style, pt, s1_normal, s2_normal); |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
pub struct Stroker<'z> { |
| 698 |
stroked_path: PathBuilder<'z>, |
| 699 |
cur_pt: Option<Point>, |
| 700 |
last_normal: Vector, |
| 701 |
half_width: f32, |
| 702 |
start_point: Option<(Point, Vector)>, |
| 703 |
style: StrokeStyle, |
| 704 |
closed_subpath: bool |
| 705 |
} |
| 706 |
|
| 707 |
impl<'z> Stroker<'z> { |
| 708 |
pub fn new(style: &StrokeStyle) -> Self { |
| 709 |
let mut style = style.clone(); |
| 710 |
let mut coverage = 1.; |
| 711 |
if style.width < 1. { |
| 712 |
coverage = style.width; |
| 713 |
style.width = 1.; |
| 714 |
} |
| 715 |
Stroker { |
| 716 |
stroked_path: PathBuilder::new(coverage), |
| 717 |
cur_pt: None, |
| 718 |
last_normal: Vector::zero(), |
| 719 |
half_width: style.width / 2., |
| 720 |
start_point: None, |
| 721 |
style, |
| 722 |
closed_subpath: false, |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
pub fn set_output_buffer(&mut self, output_buffer: &'z mut [Vertex]) { |
| 727 |
self.stroked_path.set_output_buffer(output_buffer); |
| 728 |
} |
| 729 |
|
| 730 |
pub fn line_to_capped(&mut self, pt: Point) { |
| 731 |
if let Some(cur_pt) = self.cur_pt { |
| 732 |
let normal = compute_normal(cur_pt, pt).unwrap_or(self.last_normal); |
| 733 |
// if we have a butt cap end the line half a pixel early so we have room to put the cap. |
| 734 |
// XXX: this will probably mess things up if the line is shorter than 1/2 pixel long |
| 735 |
self.line_to(if self.stroked_path.aa && self.style.cap == LineCap::Butt { pt + perp(normal) * 0.5} else { pt }); |
| 736 |
if let (Some(cur_pt), Some((_point, _normal))) = (self.cur_pt, self.start_point) { |
| 737 |
// cap end |
| 738 |
cap_line(&mut self.stroked_path, &self.style, cur_pt, self.last_normal); |
| 739 |
} |
| 740 |
} |
| 741 |
self.start_point = None; |
| 742 |
} |
| 743 |
|
| 744 |
pub fn move_to(&mut self, pt: Point, closed_subpath: bool) { |
| 745 |
//eprintln!("stroker.move_to(Point::new({}, {}), {});", pt.x, pt.y, closed_subpath); |
| 746 |
|
| 747 |
self.start_point = None; |
| 748 |
self.cur_pt = Some(pt); |
| 749 |
self.closed_subpath = closed_subpath; |
| 750 |
} |
| 751 |
|
| 752 |
pub fn line_to(&mut self, pt: Point) { |
| 753 |
//eprintln!("stroker.line_to(Point::new({}, {}));", pt.x, pt.y); |
| 754 |
|
| 755 |
let cur_pt = self.cur_pt; |
| 756 |
let stroked_path = &mut self.stroked_path; |
| 757 |
let half_width = self.half_width; |
| 758 |
|
| 759 |
if cur_pt.is_none() { |
| 760 |
self.start_point = None; |
| 761 |
} else if let Some(cur_pt) = cur_pt { |
| 762 |
if let Some(normal) = compute_normal(cur_pt, pt) { |
| 763 |
if self.start_point.is_none() { |
| 764 |
if !self.closed_subpath { |
| 765 |
// cap beginning |
| 766 |
let mut cur_pt = cur_pt; |
| 767 |
if stroked_path.aa && self.style.cap == LineCap::Butt { |
| 768 |
// adjust the starting point to make room for the cap |
| 769 |
// XXX: this will probably mess things up if the line is shorter than 1/2 pixel long |
| 770 |
cur_pt += perp(flip(normal)) * 0.5; |
| 771 |
} |
| 772 |
cap_line(stroked_path, &self.style, cur_pt, flip(normal)); |
| 773 |
} |
| 774 |
self.start_point = Some((cur_pt, normal)); |
| 775 |
} else { |
| 776 |
join_line(stroked_path, &self.style, cur_pt, self.last_normal, normal); |
| 777 |
} |
| 778 |
if stroked_path.aa { |
| 779 |
stroked_path.ramp( |
| 780 |
pt.x + normal.x * (half_width - 0.5), |
| 781 |
pt.y + normal.y * (half_width - 0.5), |
| 782 |
pt.x + normal.x * (half_width + 0.5), |
| 783 |
pt.y + normal.y * (half_width + 0.5), |
| 784 |
cur_pt.x + normal.x * (half_width + 0.5), |
| 785 |
cur_pt.y + normal.y * (half_width + 0.5), |
| 786 |
cur_pt.x + normal.x * (half_width - 0.5), |
| 787 |
cur_pt.y + normal.y * (half_width - 0.5), |
| 788 |
); |
| 789 |
stroked_path.quad( |
| 790 |
cur_pt.x + normal.x * (half_width - 0.5), |
| 791 |
cur_pt.y + normal.y * (half_width - 0.5), |
| 792 |
pt.x + normal.x * (half_width - 0.5), pt.y + normal.y * (half_width - 0.5), |
| 793 |
pt.x + -normal.x * (half_width - 0.5), pt.y + -normal.y * (half_width - 0.5), |
| 794 |
cur_pt.x - normal.x * (half_width - 0.5), |
| 795 |
cur_pt.y - normal.y * (half_width - 0.5), |
| 796 |
); |
| 797 |
stroked_path.ramp( |
| 798 |
cur_pt.x - normal.x * (half_width - 0.5), |
| 799 |
cur_pt.y - normal.y * (half_width - 0.5), |
| 800 |
cur_pt.x - normal.x * (half_width + 0.5), |
| 801 |
cur_pt.y - normal.y * (half_width + 0.5), |
| 802 |
pt.x - normal.x * (half_width + 0.5), |
| 803 |
pt.y - normal.y * (half_width + 0.5), |
| 804 |
pt.x - normal.x * (half_width - 0.5), |
| 805 |
pt.y - normal.y * (half_width - 0.5), |
| 806 |
); |
| 807 |
} else { |
| 808 |
stroked_path.quad( |
| 809 |
cur_pt.x + normal.x * half_width, |
| 810 |
cur_pt.y + normal.y * half_width, |
| 811 |
pt.x + normal.x * half_width, pt.y + normal.y * half_width, |
| 812 |
pt.x + -normal.x * half_width, pt.y + -normal.y * half_width, |
| 813 |
cur_pt.x - normal.x * half_width, |
| 814 |
cur_pt.y - normal.y * half_width, |
| 815 |
); |
| 816 |
} |
| 817 |
|
| 818 |
self.last_normal = normal; |
| 819 |
|
| 820 |
} |
| 821 |
} |
| 822 |
self.cur_pt = Some(pt); |
| 823 |
} |
| 824 |
|
| 825 |
pub fn curve_to(&mut self, cx1: Point, cx2: Point, pt: Point) { |
| 826 |
//eprintln!("stroker.curve_to(Point::new({}, {}), Point::new({}, {}), Point::new({}, {}));", cx1.x, cx1.y, cx2.x, cx2.y, pt.x, pt.y); |
| 827 |
self.curve_to_direct(cx1, cx2, pt, false); |
| 828 |
} |
| 829 |
|
| 830 |
pub fn curve_to_capped(&mut self, cx1: Point, cx2: Point, pt: Point) { |
| 831 |
self.curve_to_direct(cx1, cx2, pt, true); |
| 832 |
} |
| 833 |
|
| 834 |
pub fn curve_to_internal(&mut self, cx1: Point, cx2: Point, pt: Point, end: bool) { |
| 835 |
struct Target<'a, 'b> { stroker: &'a mut Stroker<'b>, end: bool } |
| 836 |
impl<'a, 'b> CFlatteningSink for Target<'a, 'b> { |
| 837 |
fn AcceptPointAndTangent(&mut self, _: &GpPointR, _: &GpPointR, _: bool ) -> HRESULT { |
| 838 |
panic!() |
| 839 |
} |
| 840 |
fn FirstTangent(&mut self, _tangent: Option<GpPointR>) { } |
| 841 |
|
| 842 |
fn AcceptPoint(&mut self, |
| 843 |
pt: &GpPointR, |
| 844 |
// The point |
| 845 |
_t: f32, |
| 846 |
// Parameter we're at |
| 847 |
_aborted: &mut bool, |
| 848 |
last_point: bool) -> HRESULT { |
| 849 |
if last_point && self.end { |
| 850 |
self.stroker.line_to_capped(Point::new(pt.x, pt.y)); |
| 851 |
} else { |
| 852 |
self.stroker.line_to(Point::new(pt.x, pt.y)); |
| 853 |
} |
| 854 |
return S_OK; |
| 855 |
} |
| 856 |
} |
| 857 |
let cur_pt = self.cur_pt.unwrap_or(cx1); |
| 858 |
let bezier = CBezier::new([GpPointR { x: cur_pt.x, y: cur_pt.y, }, |
| 859 |
GpPointR { x: cx1.x, y: cx1.y, }, |
| 860 |
GpPointR { x: cx2.x, y: cx2.y, }, |
| 861 |
GpPointR { x: pt.x, y: pt.y, }]); |
| 862 |
let mut t = Target{ stroker: self, end }; |
| 863 |
let mut f = CBezierFlattener::new(&bezier, &mut t, 0.25); |
| 864 |
f.Flatten(false); |
| 865 |
} |
| 866 |
|
| 867 |
// Stroke a curve by flattening to trapezoids insteads instead of rectangles. |
| 868 |
// This lets us make sure that the normal of the stroked path matches the normal |
| 869 |
// of the curve and we also avoid having to join the segments |
| 870 |
pub fn curve_to_direct(&mut self, cx1: Point, cx2: Point, pt: Point, end: bool) { |
| 871 |
struct Target<'a, 'b> { stroker: &'a mut Stroker<'b>, _end: bool, last_normal: Vector, last_point: GpPointR} |
| 872 |
impl<'a, 'b> CFlatteningSink for Target<'a, 'b> { |
| 873 |
fn FirstTangent(&mut self, tangent: Option<GpPointR>) { |
| 874 |
let tangent = tangent.unwrap(); |
| 875 |
let last_normal = flip(Vector::new(tangent.y, -tangent.x).normalize()); |
| 876 |
let stroked_path = &mut self.stroker.stroked_path; |
| 877 |
if self.stroker.start_point.is_some() { |
| 878 |
if let Some(cur_pt) = self.stroker.cur_pt { |
| 879 |
join_line(stroked_path, &self.stroker.style, cur_pt, self.stroker.last_normal, last_normal); |
| 880 |
} |
| 881 |
} |
| 882 |
self.last_normal = last_normal; |
| 883 |
} |
| 884 |
fn AcceptPointAndTangent(&mut self, pt: &GpPointR, tangent: &GpPointR, _last: bool) -> HRESULT { |
| 885 |
let normal = flip(Vector::new(tangent.y, -tangent.x).normalize()); |
| 886 |
let last_normal = self.last_normal; |
| 887 |
let cur_pt = Point::new(self.last_point.x, self.last_point.y); |
| 888 |
let half_width = self.stroker.half_width; |
| 889 |
let stroked_path = &mut self.stroker.stroked_path; |
| 890 |
|
| 891 |
if self.stroker.start_point.is_none() { |
| 892 |
if !self.stroker.closed_subpath { |
| 893 |
// cap beginning |
| 894 |
let mut cur_pt = cur_pt; |
| 895 |
if stroked_path.aa && self.stroker.style.cap == LineCap::Butt { |
| 896 |
// adjust the starting point to make room for the cap |
| 897 |
// XXX: this will probably mess things up if the line is shorter than 1/2 pixel long |
| 898 |
cur_pt += perp(flip(last_normal)) * 0.5; |
| 899 |
} |
| 900 |
cap_line(stroked_path, &self.stroker.style, cur_pt, flip(last_normal)); |
| 901 |
} |
| 902 |
// last_normal will have been set by FirstTangent |
| 903 |
self.stroker.start_point = Some((cur_pt, last_normal)); |
| 904 |
} else { |
| 905 |
// we don't need to join the segments because the quads are sharing normals |
| 906 |
} |
| 907 |
if stroked_path.aa { |
| 908 |
stroked_path.ramp( |
| 909 |
pt.x + normal.x * (half_width - 0.5), |
| 910 |
pt.y + normal.y * (half_width - 0.5), |
| 911 |
pt.x + normal.x * (half_width + 0.5), |
| 912 |
pt.y + normal.y * (half_width + 0.5), |
| 913 |
cur_pt.x + last_normal.x * (half_width + 0.5), |
| 914 |
cur_pt.y + last_normal.y * (half_width + 0.5), |
| 915 |
cur_pt.x + last_normal.x * (half_width - 0.5), |
| 916 |
cur_pt.y + last_normal.y * (half_width - 0.5), |
| 917 |
); |
| 918 |
stroked_path.quad( |
| 919 |
cur_pt.x + last_normal.x * (half_width - 0.5), |
| 920 |
cur_pt.y + last_normal.y * (half_width - 0.5), |
| 921 |
pt.x + normal.x * (half_width - 0.5), pt.y + normal.y * (half_width - 0.5), |
| 922 |
pt.x + -normal.x * (half_width - 0.5), pt.y + -normal.y * (half_width - 0.5), |
| 923 |
cur_pt.x - last_normal.x * (half_width - 0.5), |
| 924 |
cur_pt.y - last_normal.y * (half_width - 0.5), |
| 925 |
); |
| 926 |
stroked_path.ramp( |
| 927 |
cur_pt.x - last_normal.x * (half_width - 0.5), |
| 928 |
cur_pt.y - last_normal.y * (half_width - 0.5), |
| 929 |
cur_pt.x - last_normal.x * (half_width + 0.5), |
| 930 |
cur_pt.y - last_normal.y * (half_width + 0.5), |
| 931 |
pt.x - normal.x * (half_width + 0.5), |
| 932 |
pt.y - normal.y * (half_width + 0.5), |
| 933 |
pt.x - normal.x * (half_width - 0.5), |
| 934 |
pt.y - normal.y * (half_width - 0.5), |
| 935 |
); |
| 936 |
} else { |
| 937 |
self.stroker.stroked_path.quad( |
| 938 |
cur_pt.x + last_normal.x * half_width, |
| 939 |
cur_pt.y + last_normal.y * half_width, |
| 940 |
pt.x + normal.x * half_width, pt.y + normal.y * half_width, |
| 941 |
pt.x + -normal.x * half_width, pt.y + -normal.y * half_width, |
| 942 |
cur_pt.x - last_normal.x * half_width, |
| 943 |
cur_pt.y - last_normal.y * half_width, |
| 944 |
); |
| 945 |
} |
| 946 |
self.last_normal = normal; |
| 947 |
self.last_point = *pt; |
| 948 |
return S_OK; |
| 949 |
} |
| 950 |
|
| 951 |
fn AcceptPoint(&mut self, _p: &GpPointR, |
| 952 |
_: f32, _: &mut bool, _: bool) -> HRESULT { |
| 953 |
panic!(); |
| 954 |
} |
| 955 |
} |
| 956 |
let cur_pt = self.cur_pt.unwrap_or(cx1); |
| 957 |
let bezier = CBezier::new([GpPointR { x: cur_pt.x, y: cur_pt.y, }, |
| 958 |
GpPointR { x: cx1.x, y: cx1.y, }, |
| 959 |
GpPointR { x: cx2.x, y: cx2.y, }, |
| 960 |
GpPointR { x: pt.x, y: pt.y, }]); |
| 961 |
|
| 962 |
let mut t = Target{ stroker: self, _end: end, |
| 963 |
last_normal: Vector::new(0., 0.), |
| 964 |
last_point: GpPointR { x: cur_pt.x, y: cur_pt.y } |
| 965 |
}; |
| 966 |
let mut f = CBezierFlattener::new(&bezier, &mut t, 0.25); |
| 967 |
if f.GetFirstTangent().is_none() { |
| 968 |
// the bezier is degnerate |
| 969 |
return if end { self.line_to_capped(pt) } else { self.line_to(pt) }; |
| 970 |
} |
| 971 |
f.Flatten(true); |
| 972 |
|
| 973 |
self.last_normal = t.last_normal; |
| 974 |
self.cur_pt = Some(pt); |
| 975 |
} |
| 976 |
|
| 977 |
pub fn close(&mut self) { |
| 978 |
let stroked_path = &mut self.stroked_path; |
| 979 |
let half_width = self.half_width; |
| 980 |
if let (Some(cur_pt), Some((end_point, start_normal))) = (self.cur_pt, self.start_point) { |
| 981 |
if let Some(normal) = compute_normal(cur_pt, end_point) { |
| 982 |
join_line(stroked_path, &self.style, cur_pt, self.last_normal, normal); |
| 983 |
if stroked_path.aa { |
| 984 |
stroked_path.ramp( |
| 985 |
end_point.x + normal.x * (half_width - 0.5), |
| 986 |
end_point.y + normal.y * (half_width - 0.5), |
| 987 |
end_point.x + normal.x * (half_width + 0.5), |
| 988 |
end_point.y + normal.y * (half_width + 0.5), |
| 989 |
cur_pt.x + normal.x * (half_width + 0.5), |
| 990 |
cur_pt.y + normal.y * (half_width + 0.5), |
| 991 |
cur_pt.x + normal.x * (half_width - 0.5), |
| 992 |
cur_pt.y + normal.y * (half_width - 0.5), |
| 993 |
); |
| 994 |
stroked_path.quad( |
| 995 |
cur_pt.x + normal.x * (half_width - 0.5), |
| 996 |
cur_pt.y + normal.y * (half_width - 0.5), |
| 997 |
end_point.x + normal.x * (half_width - 0.5), end_point.y + normal.y * (half_width - 0.5), |
| 998 |
end_point.x + -normal.x * (half_width - 0.5), end_point.y + -normal.y * (half_width - 0.5), |
| 999 |
cur_pt.x - normal.x * (half_width - 0.5), |
| 1000 |
cur_pt.y - normal.y * (half_width - 0.5), |
| 1001 |
); |
| 1002 |
stroked_path.ramp( |
| 1003 |
cur_pt.x - normal.x * (half_width - 0.5), |
| 1004 |
cur_pt.y - normal.y * (half_width - 0.5), |
| 1005 |
cur_pt.x - normal.x * (half_width + 0.5), |
| 1006 |
cur_pt.y - normal.y * (half_width + 0.5), |
| 1007 |
end_point.x - normal.x * (half_width + 0.5), |
| 1008 |
end_point.y - normal.y * (half_width + 0.5), |
| 1009 |
end_point.x - normal.x * (half_width - 0.5), |
| 1010 |
end_point.y - normal.y * (half_width - 0.5), |
| 1011 |
); |
| 1012 |
} else { |
| 1013 |
stroked_path.quad( |
| 1014 |
cur_pt.x + normal.x * half_width, |
| 1015 |
cur_pt.y + normal.y * half_width, |
| 1016 |
end_point.x + normal.x * half_width, end_point.y + normal.y * half_width, |
| 1017 |
end_point.x + -normal.x * half_width, end_point.y + -normal.y * half_width, |
| 1018 |
cur_pt.x - normal.x * half_width, |
| 1019 |
cur_pt.y - normal.y * half_width, |
| 1020 |
); |
| 1021 |
} |
| 1022 |
join_line(stroked_path, &self.style, end_point, normal, start_normal); |
| 1023 |
} else { |
| 1024 |
join_line(stroked_path, &self.style, end_point, self.last_normal, start_normal); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
self.cur_pt = self.start_point.map(|x| x.0); |
| 1028 |
self.start_point = None; |
| 1029 |
} |
| 1030 |
|
| 1031 |
pub fn get_stroked_path(&mut self) -> PathBuilder<'z> { |
| 1032 |
let mut stroked_path = std::mem::replace(&mut self.stroked_path, PathBuilder::new(1.)); |
| 1033 |
|
| 1034 |
if let (Some(cur_pt), Some((point, normal))) = (self.cur_pt, self.start_point) { |
| 1035 |
// cap end |
| 1036 |
cap_line(&mut stroked_path, &self.style, cur_pt, self.last_normal); |
| 1037 |
// cap beginning |
| 1038 |
cap_line(&mut stroked_path, &self.style, point, flip(normal)); |
| 1039 |
} |
| 1040 |
|
| 1041 |
stroked_path |
| 1042 |
} |
| 1043 |
|
| 1044 |
pub fn finish(&mut self) -> Box<[Vertex]> { |
| 1045 |
self.get_stroked_path().finish() |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
fn filled_circle_with_path_builder(mut path_builder: &mut PathBuilder, center: Point, radius: f32) { |
| 1050 |
arc(&mut path_builder, center.x, center.y, radius, Vector::new(1., 0.), Vector::new(-1., 0.)); |
| 1051 |
arc(&mut path_builder, center.x, center.y, radius, Vector::new(-1., 0.), Vector::new(1., 0.)); |
| 1052 |
} |
| 1053 |
|
| 1054 |
/// Returns an anti-aliased triangle mesh for a filled circle. |
| 1055 |
pub fn filled_circle(center: Point, radius: f32) -> Box<[Vertex]> { |
| 1056 |
let mut path_builder = PathBuilder::new(1.); |
| 1057 |
filled_circle_with_path_builder(&mut path_builder, center, radius); |
| 1058 |
path_builder.finish() |
| 1059 |
} |
| 1060 |
|
| 1061 |
#[test] |
| 1062 |
fn filled_circle_test() { |
| 1063 |
let center = Point::new(100., 100.); |
| 1064 |
let radius = 33.; |
| 1065 |
let result = filled_circle(center, radius); |
| 1066 |
let min_x = result.iter().map(|v: &Vertex| v.x).reduce(|a, b| a.min(b)).unwrap(); |
| 1067 |
let max_x = result.iter().map(|v: &Vertex| v.x).reduce(|a, b| a.max(b)).unwrap(); |
| 1068 |
let min_y = result.iter().map(|v: &Vertex| v.y).reduce(|a, b| a.min(b)).unwrap(); |
| 1069 |
let max_y = result.iter().map(|v: &Vertex| v.y).reduce(|a, b| a.max(b)).unwrap(); |
| 1070 |
assert_eq!(min_x, center.x - (radius + 0.5)); |
| 1071 |
assert_eq!(max_x, center.x + (radius + 0.5)); |
| 1072 |
assert_eq!(min_y, center.y - (radius + 0.5)); |
| 1073 |
assert_eq!(max_y, center.y + (radius + 0.5)); |
| 1074 |
} |
| 1075 |
|
| 1076 |
#[test] |
| 1077 |
fn simple() { |
| 1078 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1079 |
cap: LineCap::Round, |
| 1080 |
join: LineJoin::Bevel, |
| 1081 |
width: 20., |
| 1082 |
..Default::default()}); |
| 1083 |
stroker.move_to(Point::new(20., 20.), false); |
| 1084 |
stroker.line_to(Point::new(100., 100.)); |
| 1085 |
stroker.line_to_capped(Point::new(110., 20.)); |
| 1086 |
|
| 1087 |
stroker.move_to(Point::new(120., 20.), true); |
| 1088 |
stroker.line_to(Point::new(120., 50.)); |
| 1089 |
stroker.line_to(Point::new(140., 50.)); |
| 1090 |
stroker.close(); |
| 1091 |
|
| 1092 |
let stroked = stroker.finish(); |
| 1093 |
assert_eq!(stroked.len(), 330); |
| 1094 |
} |
| 1095 |
|
| 1096 |
#[test] |
| 1097 |
fn curve() { |
| 1098 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1099 |
cap: LineCap::Round, |
| 1100 |
join: LineJoin::Bevel, |
| 1101 |
width: 20., |
| 1102 |
..Default::default()}); |
| 1103 |
stroker.move_to(Point::new(20., 160.), true); |
| 1104 |
stroker.curve_to(Point::new(100., 160.), Point::new(100., 180.), Point::new(20., 180.)); |
| 1105 |
stroker.close(); |
| 1106 |
let stroked = stroker.finish(); |
| 1107 |
assert_eq!(stroked.len(), 624); |
| 1108 |
} |
| 1109 |
|
| 1110 |
#[test] |
| 1111 |
fn butt_cap() { |
| 1112 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1113 |
cap: LineCap::Butt, |
| 1114 |
join: LineJoin::Bevel, |
| 1115 |
width: 1., |
| 1116 |
..Default::default()}); |
| 1117 |
stroker.move_to(Point::new(20., 20.5), false); |
| 1118 |
stroker.line_to_capped(Point::new(40., 20.5)); |
| 1119 |
let result = stroker.finish(); |
| 1120 |
for v in result.iter() { |
| 1121 |
assert!(v.y == 20.5 || v.y == 19.5 || v.y == 21.5); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
#[test] |
| 1126 |
fn width_one_radius_arc() { |
| 1127 |
// previously this caused us to try to flatten an arc with radius 0 |
| 1128 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1129 |
cap: LineCap::Round, |
| 1130 |
join: LineJoin::Round, |
| 1131 |
width: 1., |
| 1132 |
..Default::default()}); |
| 1133 |
stroker.move_to(Point::new(20., 20.), false); |
| 1134 |
stroker.line_to(Point::new(30., 160.)); |
| 1135 |
stroker.line_to_capped(Point::new(40., 20.)); |
| 1136 |
stroker.finish(); |
| 1137 |
} |
| 1138 |
|
| 1139 |
#[test] |
| 1140 |
fn round_join_less_than_90deg() { |
| 1141 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1142 |
cap: LineCap::Round, |
| 1143 |
join: LineJoin::Round, |
| 1144 |
width: 1., |
| 1145 |
..Default::default()}); |
| 1146 |
stroker.move_to(Point::new(20., 20.), false); |
| 1147 |
stroker.line_to(Point::new(20., 40.)); |
| 1148 |
stroker.line_to_capped(Point::new(30., 50.)); |
| 1149 |
assert_eq!(stroker.finish().len(), 81); |
| 1150 |
} |
| 1151 |
|
| 1152 |
#[test] |
| 1153 |
fn small_arc() { |
| 1154 |
let mut path = PathBuilder::new(1.); |
| 1155 |
let start = 0_f32; |
| 1156 |
let end = 0.001_f32; |
| 1157 |
arc(&mut path, 0., 0., 0.5, Vector::new(start.cos(), start.sin()), Vector::new(end.cos(), end.sin())); |
| 1158 |
} |
| 1159 |
|
| 1160 |
#[test] |
| 1161 |
fn parallel_line_join() { |
| 1162 |
// ensure line joins of almost parallel lines don't cause math to fail |
| 1163 |
for join in [LineJoin::Bevel, LineJoin::Round, LineJoin::Miter] { |
| 1164 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1165 |
cap: LineCap::Butt, |
| 1166 |
join, |
| 1167 |
width: 1.0, |
| 1168 |
..Default::default()}); |
| 1169 |
stroker.move_to(Point::new(19.812500, 71.625000), true); |
| 1170 |
stroker.line_to(Point::new(19.250000, 72.000000)); |
| 1171 |
stroker.line_to(Point::new(19.062500, 72.125000)); |
| 1172 |
stroker.close(); |
| 1173 |
stroker.finish(); |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
#[test] |
| 1178 |
fn degenerate_miter_join() { |
| 1179 |
// from https://bugzilla.mozilla.org/show_bug.cgi?id=1841020 |
| 1180 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1181 |
cap: LineCap::Square, |
| 1182 |
join: LineJoin::Miter, |
| 1183 |
width: 1.0, |
| 1184 |
..Default::default()}); |
| 1185 |
|
| 1186 |
stroker.move_to(Point::new(-204.48355, 528.4429), false); |
| 1187 |
stroker.line_to(Point::new(-203.89037, 529.0532)); |
| 1188 |
stroker.line_to(Point::new(-202.58539, 530.396,)); |
| 1189 |
stroker.line_to(Point::new(-201.2804, 531.73883,)); |
| 1190 |
stroker.line_to(Point::new(-200.68721, 532.3492,)); |
| 1191 |
|
| 1192 |
let result = stroker.finish(); |
| 1193 |
// make sure none of the verticies are wildly out of place |
| 1194 |
for v in result.iter() { |
| 1195 |
assert!(v.y >= 527.); |
| 1196 |
} |
| 1197 |
|
| 1198 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1199 |
cap: LineCap::Square, |
| 1200 |
join: LineJoin::Miter, |
| 1201 |
width: 40.0, |
| 1202 |
..Default::default()}); |
| 1203 |
|
| 1204 |
fn distance_from_line(p1: Point, p2: Point, x: Point) -> f32 { |
| 1205 |
((p2.x - p1.x)*(p1.y - x.y) - (p1.x - x.x)*(p2.y - p1.y)).abs() / |
| 1206 |
((p2.x - p1.x).powi(2) + (p2.y - p1.y).powi(2)).sqrt() |
| 1207 |
} |
| 1208 |
let start = Point::new(512., 599.); |
| 1209 |
let end = Point::new(513.51666, 597.47736); |
| 1210 |
stroker.move_to(start, false); |
| 1211 |
stroker.line_to(Point::new(512.3874, 598.6111)); |
| 1212 |
stroker.line_to_capped(end); |
| 1213 |
let result = stroker.finish(); |
| 1214 |
for v in result.iter() { |
| 1215 |
assert!(distance_from_line(start, end, Point::new(v.x, v.y)) <= 21.); |
| 1216 |
} |
| 1217 |
|
| 1218 |
// from https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment |
| 1219 |
fn minimum_distance(v: Point, w: Point, p: Point) -> f32 { |
| 1220 |
// Return minimum distance between line segment vw and point p |
| 1221 |
let l2 = (v-w).length().powi(2); // i.e. |w-v|^2 - avoid a sqrt |
| 1222 |
if l2 == 0.0 { return (p - v).length(); } // v == w case |
| 1223 |
// Consider the line extending the segment, parameterized as v + t (w - v). |
| 1224 |
// We find projection of point p onto the line. |
| 1225 |
// It falls where t = [(p-v) . (w-v)] / |w-v|^2 |
| 1226 |
// We clamp t from [0,1] to handle points outside the segment vw. |
| 1227 |
let t = 0_f32.max(1_f32.min(dot(p - v, w - v) / l2)); |
| 1228 |
let projection = v + (w - v) * t; // Projection falls on the segment |
| 1229 |
(p - projection).length() |
| 1230 |
} |
| 1231 |
|
| 1232 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1233 |
cap: LineCap::Square, |
| 1234 |
join: LineJoin::Miter, |
| 1235 |
width: 40.0, |
| 1236 |
miter_limit: 10.0, |
| 1237 |
..Default::default()}); |
| 1238 |
let start = Point::new(689.3504, 434.5446); |
| 1239 |
let end = Point::new(671.83203, 422.61914); |
| 1240 |
stroker.move_to(Point::new(689.3504, 434.5446), false); |
| 1241 |
stroker.line_to(Point::new(681.04254, 428.8891)); |
| 1242 |
stroker.line_to_capped(Point::new(671.83203, 422.61914)); |
| 1243 |
|
| 1244 |
let result = stroker.finish(); |
| 1245 |
let max_distance = (21_f32.powi(2) * 2.).sqrt(); |
| 1246 |
for v in result.iter() { |
| 1247 |
assert!(minimum_distance(start, end, Point::new(v.x, v.y)) <= max_distance); |
| 1248 |
} |
| 1249 |
|
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
#[test] |
| 1254 |
fn nearly_degenerate_bezier() { |
| 1255 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1256 |
cap: LineCap::Square, |
| 1257 |
join: LineJoin::Miter, |
| 1258 |
width: 1.0, |
| 1259 |
..Default::default()}); |
| 1260 |
|
| 1261 |
stroker.move_to(Point::new(0., 0.0005), false); |
| 1262 |
stroker.curve_to(Point::new(0., 0.0005), Point::new(0., 0.), Point::new(0., 0.)); |
| 1263 |
stroker.finish(); |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
#[test] |
| 1268 |
fn joins_between_curves() { |
| 1269 |
let mut stroker = Stroker::new(&StrokeStyle{ |
| 1270 |
cap: LineCap::Square, |
| 1271 |
join: LineJoin::Miter, |
| 1272 |
width: 40.0, |
| 1273 |
..Default::default()}); |
| 1274 |
|
| 1275 |
stroker.move_to(Point::new(-80., 20.), false); |
| 1276 |
stroker.curve_to(Point::new(-80., 20.), Point::new(-5., 20.), Point::new(-5., 20.)); |
| 1277 |
stroker.curve_to(Point::new(-5., 20.), Point::new(-5., 90.), Point::new(-5., 90.)); |
| 1278 |
let vertices = stroker.finish(); |
| 1279 |
let result = rasterize_to_mask(&vertices, 1, 1); |
| 1280 |
assert_eq!(result[0], 255); |
| 1281 |
|
| 1282 |
} |