| 1 |
/*! |
| 2 |
Converts a 2D path into a set of vertices of a triangle strip mesh that represents the antialiased fill of that path. |
| 3 |
|
| 4 |
```rust |
| 5 |
use wpf_gpu_raster::PathBuilder; |
| 6 |
let mut p = PathBuilder::new(); |
| 7 |
p.move_to(10., 10.); |
| 8 |
p.line_to(40., 10.); |
| 9 |
p.line_to(40., 40.); |
| 10 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 11 |
``` |
| 12 |
|
| 13 |
*/ |
| 14 |
#![allow(unused_parens)] |
| 15 |
#![allow(overflowing_literals)] |
| 16 |
#![allow(non_snake_case)] |
| 17 |
#![allow(non_camel_case_types)] |
| 18 |
#![allow(non_upper_case_globals)] |
| 19 |
#![allow(dead_code)] |
| 20 |
#![allow(unused_macros)] |
| 21 |
|
| 22 |
#[macro_use] |
| 23 |
mod fix; |
| 24 |
#[macro_use] |
| 25 |
mod helpers; |
| 26 |
#[macro_use] |
| 27 |
mod real; |
| 28 |
mod bezier; |
| 29 |
#[macro_use] |
| 30 |
mod aarasterizer; |
| 31 |
mod hwrasterizer; |
| 32 |
mod aacoverage; |
| 33 |
mod hwvertexbuffer; |
| 34 |
|
| 35 |
mod types; |
| 36 |
mod geometry_sink; |
| 37 |
mod matrix; |
| 38 |
|
| 39 |
mod nullable_ref; |
| 40 |
|
| 41 |
#[cfg(feature = "c_bindings")] |
| 42 |
pub mod c_bindings; |
| 43 |
|
| 44 |
#[cfg(test)] |
| 45 |
mod tri_rasterize; |
| 46 |
|
| 47 |
use aarasterizer::CheckValidRange28_4; |
| 48 |
use hwrasterizer::CHwRasterizer; |
| 49 |
use hwvertexbuffer::{CHwVertexBuffer, CHwVertexBufferBuilder}; |
| 50 |
use real::CFloatFPU; |
| 51 |
use types::{MilFillMode, PathPointTypeStart, MilPoint2F, MilPointAndSizeL, PathPointTypeLine, MilVertexFormat, MilVertexFormatAttribute, DynArray, BYTE, PathPointTypeBezier, PathPointTypeCloseSubpath, CMILSurfaceRect, POINT}; |
| 52 |
|
| 53 |
#[repr(C)] |
| 54 |
#[derive(Clone, Debug, Default)] |
| 55 |
pub struct OutputVertex { |
| 56 |
pub x: f32, |
| 57 |
pub y: f32, |
| 58 |
pub coverage: f32 |
| 59 |
} |
| 60 |
|
| 61 |
#[repr(C)] |
| 62 |
#[derive(Copy, Clone)] |
| 63 |
pub enum FillMode { |
| 64 |
EvenOdd = 0, |
| 65 |
Winding = 1, |
| 66 |
} |
| 67 |
|
| 68 |
impl Default for FillMode { |
| 69 |
fn default() -> Self { |
| 70 |
FillMode::EvenOdd |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#[derive(Clone, Default)] |
| 75 |
pub struct OutputPath { |
| 76 |
fill_mode: FillMode, |
| 77 |
points: Box<[POINT]>, |
| 78 |
types: Box<[BYTE]>, |
| 79 |
} |
| 80 |
|
| 81 |
impl std::hash::Hash for OutputVertex { |
| 82 |
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 83 |
self.x.to_bits().hash(state); |
| 84 |
self.y.to_bits().hash(state); |
| 85 |
self.coverage.to_bits().hash(state); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
pub struct PathBuilder { |
| 90 |
points: DynArray<POINT>, |
| 91 |
types: DynArray<BYTE>, |
| 92 |
initial_point: Option<MilPoint2F>, |
| 93 |
current_point: Option<MilPoint2F>, |
| 94 |
in_shape: bool, |
| 95 |
fill_mode: FillMode, |
| 96 |
outside_bounds: Option<CMILSurfaceRect>, |
| 97 |
need_inside: bool, |
| 98 |
valid_range: bool, |
| 99 |
rasterization_truncates: bool, |
| 100 |
} |
| 101 |
|
| 102 |
impl PathBuilder { |
| 103 |
pub fn new() -> Self { |
| 104 |
Self { |
| 105 |
points: Vec::new(), |
| 106 |
types: Vec::new(), |
| 107 |
initial_point: None, |
| 108 |
current_point: None, |
| 109 |
in_shape: false, |
| 110 |
fill_mode: FillMode::EvenOdd, |
| 111 |
outside_bounds: None, |
| 112 |
need_inside: true, |
| 113 |
valid_range: true, |
| 114 |
rasterization_truncates: false, |
| 115 |
} |
| 116 |
} |
| 117 |
fn reset(&mut self) { |
| 118 |
*self = Self { |
| 119 |
points: std::mem::take(&mut self.points), |
| 120 |
types: std::mem::take(&mut self.types), |
| 121 |
..Self::new() |
| 122 |
}; |
| 123 |
self.points.clear(); |
| 124 |
self.types.clear(); |
| 125 |
} |
| 126 |
fn add_point(&mut self, x: f32, y: f32) { |
| 127 |
self.current_point = Some(MilPoint2F{X: x, Y: y}); |
| 128 |
// Transform from pixel corner at 0.0 to pixel center at 0.0. Scale into 28.4 range. |
| 129 |
// Validate that the point before rounding is within expected bounds for the rasterizer. |
| 130 |
let (x, y) = ((x - 0.5) * 16.0, (y - 0.5) * 16.0); |
| 131 |
self.valid_range = self.valid_range && CheckValidRange28_4(x, y); |
| 132 |
self.points.push(POINT { |
| 133 |
x: CFloatFPU::Round(x), |
| 134 |
y: CFloatFPU::Round(y), |
| 135 |
}); |
| 136 |
} |
| 137 |
pub fn line_to(&mut self, x: f32, y: f32) { |
| 138 |
if let Some(initial_point) = self.initial_point { |
| 139 |
if !self.in_shape { |
| 140 |
self.types.push(PathPointTypeStart); |
| 141 |
self.add_point(initial_point.X, initial_point.Y); |
| 142 |
self.in_shape = true; |
| 143 |
} |
| 144 |
self.types.push(PathPointTypeLine); |
| 145 |
self.add_point(x, y); |
| 146 |
} else { |
| 147 |
self.initial_point = Some(MilPoint2F{X: x, Y: y}) |
| 148 |
} |
| 149 |
} |
| 150 |
pub fn move_to(&mut self, x: f32, y: f32) { |
| 151 |
self.in_shape = false; |
| 152 |
self.initial_point = Some(MilPoint2F{X: x, Y: y}); |
| 153 |
self.current_point = self.initial_point; |
| 154 |
} |
| 155 |
pub fn curve_to(&mut self, c1x: f32, c1y: f32, c2x: f32, c2y: f32, x: f32, y: f32) { |
| 156 |
let initial_point = match self.initial_point { |
| 157 |
Some(initial_point) => initial_point, |
| 158 |
None => MilPoint2F{X:c1x, Y:c1y} |
| 159 |
}; |
| 160 |
if !self.in_shape { |
| 161 |
self.types.push(PathPointTypeStart); |
| 162 |
self.add_point(initial_point.X, initial_point.Y); |
| 163 |
self.initial_point = Some(initial_point); |
| 164 |
self.in_shape = true; |
| 165 |
} |
| 166 |
self.types.push(PathPointTypeBezier); |
| 167 |
self.add_point(c1x, c1y); |
| 168 |
self.add_point(c2x, c2y); |
| 169 |
self.add_point(x, y); |
| 170 |
} |
| 171 |
pub fn quad_to(&mut self, cx: f32, cy: f32, x: f32, y: f32) { |
| 172 |
// For now we just implement quad_to on top of curve_to. |
| 173 |
// Long term we probably want to support quad curves |
| 174 |
// directly. |
| 175 |
let c0 = match self.current_point { |
| 176 |
Some(current_point) => current_point, |
| 177 |
None => MilPoint2F{X:cx, Y:cy} |
| 178 |
}; |
| 179 |
|
| 180 |
let c1x = c0.X + (2./3.) * (cx - c0.X); |
| 181 |
let c1y = c0.Y + (2./3.) * (cy - c0.Y); |
| 182 |
|
| 183 |
let c2x = x + (2./3.) * (cx - x); |
| 184 |
let c2y = y + (2./3.) * (cy - y); |
| 185 |
|
| 186 |
self.curve_to(c1x, c1y, c2x, c2y, x, y); |
| 187 |
} |
| 188 |
pub fn close(&mut self) { |
| 189 |
if self.in_shape { |
| 190 |
// Only close the path if we are inside a shape. Otherwise, the point |
| 191 |
// should be safe to elide. |
| 192 |
if let Some(last) = self.types.last_mut() { |
| 193 |
*last |= PathPointTypeCloseSubpath; |
| 194 |
} |
| 195 |
self.in_shape = false; |
| 196 |
} |
| 197 |
// Close must install a new initial point that is the same as the |
| 198 |
// initial point of the just-closed sub-path. Thus, just leave the |
| 199 |
// initial point unchanged. |
| 200 |
self.current_point = self.initial_point; |
| 201 |
} |
| 202 |
pub fn set_fill_mode(&mut self, fill_mode: FillMode) { |
| 203 |
self.fill_mode = fill_mode; |
| 204 |
} |
| 205 |
/// Enables rendering geometry for areas outside the shape but |
| 206 |
/// within the bounds. These areas will be created with |
| 207 |
/// zero alpha. |
| 208 |
/// |
| 209 |
/// This is useful for creating geometry for other blend modes. |
| 210 |
/// For example: |
| 211 |
/// - `IN(dest, geometry)` can be done with `outside_bounds` and `need_inside = false` |
| 212 |
/// - `IN(dest, geometry, alpha)` can be done with `outside_bounds` and `need_inside = true` |
| 213 |
/// |
| 214 |
/// Note: trapezoidal areas won't be clipped to outside_bounds |
| 215 |
pub fn set_outside_bounds(&mut self, outside_bounds: Option<(i32, i32, i32, i32)>, need_inside: bool) { |
| 216 |
self.outside_bounds = outside_bounds.map(|r| CMILSurfaceRect { left: r.0, top: r.1, right: r.2, bottom: r.3 }); |
| 217 |
self.need_inside = need_inside; |
| 218 |
} |
| 219 |
|
| 220 |
/// Set this to true if post vertex shader coordinates are converted to fixed point |
| 221 |
/// via truncation. This has been observed with OpenGL on AMD GPUs on macOS. |
| 222 |
pub fn set_rasterization_truncates(&mut self, rasterization_truncates: bool) { |
| 223 |
self.rasterization_truncates = rasterization_truncates; |
| 224 |
} |
| 225 |
|
| 226 |
/// Note: trapezoidal areas won't necessarily be clipped to the clip rect |
| 227 |
pub fn rasterize_to_tri_list(&self, clip_x: i32, clip_y: i32, clip_width: i32, clip_height: i32) -> Box<[OutputVertex]> { |
| 228 |
if !self.valid_range { |
| 229 |
// If any of the points are outside of valid 28.4 range, then just return an empty triangle list. |
| 230 |
return Box::new([]); |
| 231 |
} |
| 232 |
let (x, y, width, height, need_outside) = if let Some(CMILSurfaceRect { left, top, right, bottom }) = self.outside_bounds { |
| 233 |
let x0 = clip_x.max(left); |
| 234 |
let y0 = clip_y.max(top); |
| 235 |
let x1 = (clip_x + clip_width).min(right); |
| 236 |
let y1 = (clip_y + clip_height).min(bottom); |
| 237 |
(x0, y0, x1 - x0, y1 - y0, true) |
| 238 |
} else { |
| 239 |
(clip_x, clip_y, clip_width, clip_height, false) |
| 240 |
}; |
| 241 |
rasterize_to_tri_list(self.fill_mode, &self.types, &self.points, x, y, width, height, self.need_inside, need_outside, self.rasterization_truncates, None) |
| 242 |
.flush_output() |
| 243 |
} |
| 244 |
|
| 245 |
pub fn get_path(&mut self) -> Option<OutputPath> { |
| 246 |
if self.valid_range && !self.points.is_empty() && !self.types.is_empty() { |
| 247 |
Some(OutputPath { |
| 248 |
fill_mode: self.fill_mode, |
| 249 |
points: Box::from(self.points.as_slice()), |
| 250 |
types: Box::from(self.types.as_slice()), |
| 251 |
}) |
| 252 |
} else { |
| 253 |
None |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// Converts a path that is specified as an array of edge types, each associated with a fixed number |
| 259 |
// of points that are serialized to the points array. Edge types are specified via PathPointType |
| 260 |
// masks, whereas points must be supplied in 28.4 signed fixed-point format. By default, users can |
| 261 |
// fill the inside of the path excluding the outside. It may alternatively be desirable to fill the |
| 262 |
// outside the path out to the clip boundary, optionally keeping the inside. PathBuilder may be |
| 263 |
// used instead as a simpler interface to this function that handles building the path arrays. |
| 264 |
pub fn rasterize_to_tri_list<'a>( |
| 265 |
fill_mode: FillMode, |
| 266 |
types: &[BYTE], |
| 267 |
points: &[POINT], |
| 268 |
clip_x: i32, |
| 269 |
clip_y: i32, |
| 270 |
clip_width: i32, |
| 271 |
clip_height: i32, |
| 272 |
need_inside: bool, |
| 273 |
need_outside: bool, |
| 274 |
rasterization_truncates: bool, |
| 275 |
output_buffer: Option<&'a mut [OutputVertex]>, |
| 276 |
) -> CHwVertexBuffer<'a> { |
| 277 |
let clipRect = MilPointAndSizeL { |
| 278 |
X: clip_x, |
| 279 |
Y: clip_y, |
| 280 |
Width: clip_width, |
| 281 |
Height: clip_height, |
| 282 |
}; |
| 283 |
|
| 284 |
let mil_fill_mode = match fill_mode { |
| 285 |
FillMode::EvenOdd => MilFillMode::Alternate, |
| 286 |
FillMode::Winding => MilFillMode::Winding, |
| 287 |
}; |
| 288 |
|
| 289 |
let m_mvfIn: MilVertexFormat = MilVertexFormatAttribute::MILVFAttrXY as MilVertexFormat; |
| 290 |
let m_mvfGenerated: MilVertexFormat = MilVertexFormatAttribute::MILVFAttrNone as MilVertexFormat; |
| 291 |
//let mvfaAALocation = MILVFAttrNone; |
| 292 |
const HWPIPELINE_ANTIALIAS_LOCATION: MilVertexFormatAttribute = MilVertexFormatAttribute::MILVFAttrDiffuse; |
| 293 |
let mvfaAALocation = HWPIPELINE_ANTIALIAS_LOCATION; |
| 294 |
|
| 295 |
let outside_bounds = if need_outside { |
| 296 |
Some(CMILSurfaceRect { |
| 297 |
left: clip_x, |
| 298 |
top: clip_y, |
| 299 |
right: clip_x + clip_width, |
| 300 |
bottom: clip_y + clip_height, |
| 301 |
}) |
| 302 |
} else { |
| 303 |
None |
| 304 |
}; |
| 305 |
|
| 306 |
let mut vertexBuffer = CHwVertexBuffer::new(rasterization_truncates, output_buffer); |
| 307 |
{ |
| 308 |
let mut vertexBuilder = CHwVertexBufferBuilder::Create( |
| 309 |
m_mvfIn, m_mvfIn | m_mvfGenerated, mvfaAALocation, &mut vertexBuffer); |
| 310 |
vertexBuilder.SetOutsideBounds(outside_bounds.as_ref(), need_inside); |
| 311 |
vertexBuilder.BeginBuilding(); |
| 312 |
{ |
| 313 |
let mut rasterizer = CHwRasterizer::new( |
| 314 |
&mut vertexBuilder, mil_fill_mode, None, clipRect); |
| 315 |
rasterizer.SendGeometry(points, types); |
| 316 |
} |
| 317 |
vertexBuilder.EndBuilding(); |
| 318 |
} |
| 319 |
|
| 320 |
vertexBuffer |
| 321 |
} |
| 322 |
|
| 323 |
#[cfg(test)] |
| 324 |
mod tests { |
| 325 |
use std::{hash::{Hash, Hasher}, collections::hash_map::DefaultHasher}; |
| 326 |
use crate::{*, tri_rasterize::rasterize_to_mask}; |
| 327 |
fn calculate_hash<T: Hash>(t: &T) -> u64 { |
| 328 |
let mut s = DefaultHasher::new(); |
| 329 |
t.hash(&mut s); |
| 330 |
s.finish() |
| 331 |
} |
| 332 |
#[test] |
| 333 |
fn basic() { |
| 334 |
let mut p = PathBuilder::new(); |
| 335 |
p.move_to(10., 10.); |
| 336 |
p.line_to(10., 30.); |
| 337 |
p.line_to(30., 30.); |
| 338 |
p.line_to(30., 10.); |
| 339 |
p.close(); |
| 340 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 341 |
assert_eq!(result.len(), 18); |
| 342 |
//assert_eq!(dbg!(calculate_hash(&result)), 0x5851570566450135); |
| 343 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0xfbb7c3932059e240); |
| 344 |
} |
| 345 |
|
| 346 |
#[test] |
| 347 |
fn simple() { |
| 348 |
let mut p = PathBuilder::new(); |
| 349 |
p.move_to(10., 10.); |
| 350 |
p.line_to(40., 10.); |
| 351 |
p.line_to(40., 40.); |
| 352 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 353 |
//assert_eq!(dbg!(calculate_hash(&result)), 0x81a9af7769f88e68); |
| 354 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x6d1595533d40ef92); |
| 355 |
} |
| 356 |
|
| 357 |
#[test] |
| 358 |
fn rust() { |
| 359 |
let mut p = PathBuilder::new(); |
| 360 |
p.move_to(10., 10.); |
| 361 |
p.line_to(40., 10.); |
| 362 |
p.line_to(40., 40.); |
| 363 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 364 |
//assert_eq!(dbg!(calculate_hash(&result)), 0x81a9af7769f88e68); |
| 365 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x6d1595533d40ef92); |
| 366 |
} |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn fill_mode() { |
| 370 |
let mut p = PathBuilder::new(); |
| 371 |
p.move_to(10., 10.); |
| 372 |
p.line_to(40., 10.); |
| 373 |
p.line_to(40., 40.); |
| 374 |
p.line_to(10., 40.); |
| 375 |
p.close(); |
| 376 |
p.move_to(15., 15.); |
| 377 |
p.line_to(35., 15.); |
| 378 |
p.line_to(35., 35.); |
| 379 |
p.line_to(15., 35.); |
| 380 |
p.close(); |
| 381 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 382 |
//assert_eq!(dbg!(calculate_hash(&result)), 0xb34344234f2f75a8); |
| 383 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0xc7bf999c56ccfc34); |
| 384 |
|
| 385 |
let mut p = PathBuilder::new(); |
| 386 |
p.move_to(10., 10.); |
| 387 |
p.line_to(40., 10.); |
| 388 |
p.line_to(40., 40.); |
| 389 |
p.line_to(10., 40.); |
| 390 |
p.close(); |
| 391 |
p.move_to(15., 15.); |
| 392 |
p.line_to(35., 15.); |
| 393 |
p.line_to(35., 35.); |
| 394 |
p.line_to(15., 35.); |
| 395 |
p.close(); |
| 396 |
p.set_fill_mode(FillMode::Winding); |
| 397 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 398 |
//assert_eq!(dbg!(calculate_hash(&result)), 0xee4ecd8a738fc42c); |
| 399 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0xfafad659db9a2efd); |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
#[test] |
| 404 |
fn range() { |
| 405 |
// test for a start point out of range |
| 406 |
let mut p = PathBuilder::new(); |
| 407 |
p.curve_to(8.872974e16, 0., 0., 0., 0., 0.); |
| 408 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 409 |
assert_eq!(result.len(), 0); |
| 410 |
|
| 411 |
// test for a subsequent point out of range |
| 412 |
let mut p = PathBuilder::new(); |
| 413 |
p.curve_to(0., 0., 8.872974e16, 0., 0., 0.); |
| 414 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 415 |
assert_eq!(result.len(), 0); |
| 416 |
} |
| 417 |
|
| 418 |
#[test] |
| 419 |
fn multiple_starts() { |
| 420 |
let mut p = PathBuilder::new(); |
| 421 |
p.line_to(10., 10.); |
| 422 |
p.move_to(0., 0.); |
| 423 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 424 |
assert_eq!(result.len(), 0); |
| 425 |
} |
| 426 |
|
| 427 |
#[test] |
| 428 |
fn path_closing() { |
| 429 |
let mut p = PathBuilder::new(); |
| 430 |
p.curve_to(0., 0., 0., 0., 0., 32.0); |
| 431 |
p.close(); |
| 432 |
p.curve_to(0., 0., 0., 0., 0., 32.0); |
| 433 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 434 |
assert_eq!(result.len(), 0); |
| 435 |
} |
| 436 |
|
| 437 |
#[test] |
| 438 |
fn curve() { |
| 439 |
let mut p = PathBuilder::new(); |
| 440 |
p.move_to(10., 10.); |
| 441 |
p.curve_to(40., 10., 40., 10., 40., 40.); |
| 442 |
p.close(); |
| 443 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 444 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0xa92aae8dba7b8cd4); |
| 445 |
assert_eq!(dbg!(calculate_hash(&result)), 0x8dbc4d23f9bba38d); |
| 446 |
} |
| 447 |
|
| 448 |
#[test] |
| 449 |
fn partial_coverage_last_line() { |
| 450 |
let mut p = PathBuilder::new(); |
| 451 |
|
| 452 |
p.move_to(10., 10.); |
| 453 |
p.line_to(40., 10.); |
| 454 |
p.line_to(40., 39.6); |
| 455 |
p.line_to(10., 39.6); |
| 456 |
|
| 457 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 458 |
assert_eq!(result.len(), 21); |
| 459 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0xfa200c3bae144952); |
| 460 |
assert_eq!(dbg!(calculate_hash(&result)), 0xf90cb6afaadfb559); |
| 461 |
} |
| 462 |
|
| 463 |
#[test] |
| 464 |
fn delta_upper_bound() { |
| 465 |
let mut p = PathBuilder::new(); |
| 466 |
p.move_to(-122.3 + 200.,84.285); |
| 467 |
p.curve_to(-122.3 + 200., 84.285, -122.2 + 200.,86.179, -123.03 + 200., 86.16); |
| 468 |
p.curve_to(-123.85 + 200., 86.141, -140.3 + 200., 38.066, -160.83 + 200., 40.309); |
| 469 |
p.curve_to(-160.83 + 200., 40.309, -143.05 + 200., 32.956, -122.3 + 200., 84.285); |
| 470 |
p.close(); |
| 471 |
|
| 472 |
let result = p.rasterize_to_tri_list(0, 0, 400, 400); |
| 473 |
assert_eq!(result.len(), 429); |
| 474 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x5e82d98fdb47a796); |
| 475 |
assert_eq!(dbg!(calculate_hash(&result)), 0x52d52992e249587a); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
#[test] |
| 480 |
fn self_intersect() { |
| 481 |
let mut p = PathBuilder::new(); |
| 482 |
p.move_to(10., 10.); |
| 483 |
p.line_to(40., 10.); |
| 484 |
p.line_to(10., 40.); |
| 485 |
p.line_to(40., 40.); |
| 486 |
p.close(); |
| 487 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 488 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x49ecc769e1d4ec01); |
| 489 |
assert_eq!(dbg!(calculate_hash(&result)), 0xf10babef5c619d19); |
| 490 |
} |
| 491 |
|
| 492 |
#[test] |
| 493 |
fn grid() { |
| 494 |
let mut p = PathBuilder::new(); |
| 495 |
|
| 496 |
for i in 0..200 { |
| 497 |
let offset = i as f32 * 1.3; |
| 498 |
p.move_to(0. + offset, -8.); |
| 499 |
p.line_to(0.5 + offset, -8.); |
| 500 |
p.line_to(0.5 + offset, 40.); |
| 501 |
p.line_to(0. + offset, 40.); |
| 502 |
p.close(); |
| 503 |
} |
| 504 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 505 |
assert_eq!(result.len(), 12000); |
| 506 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x5a7df39d9e9292f0); |
| 507 |
} |
| 508 |
|
| 509 |
#[test] |
| 510 |
fn outside() { |
| 511 |
let mut p = PathBuilder::new(); |
| 512 |
p.move_to(10., 10.); |
| 513 |
p.line_to(40., 10.); |
| 514 |
p.line_to(10., 40.); |
| 515 |
p.line_to(40., 40.); |
| 516 |
p.close(); |
| 517 |
p.set_outside_bounds(Some((0, 0, 50, 50)), false); |
| 518 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 519 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x59403ddbb7e1d09a); |
| 520 |
assert_eq!(dbg!(calculate_hash(&result)), 0x805fd385e47e6f2); |
| 521 |
|
| 522 |
// ensure that adjusting the outside bounds changes the results |
| 523 |
p.set_outside_bounds(Some((5, 5, 50, 50)), false); |
| 524 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 525 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x59403ddbb7e1d09a); |
| 526 |
assert_eq!(dbg!(calculate_hash(&result)), 0xcec2ed688999c966); |
| 527 |
} |
| 528 |
|
| 529 |
#[test] |
| 530 |
fn outside_inside() { |
| 531 |
let mut p = PathBuilder::new(); |
| 532 |
p.move_to(10., 10.); |
| 533 |
p.line_to(40., 10.); |
| 534 |
p.line_to(10., 40.); |
| 535 |
p.line_to(40., 40.); |
| 536 |
p.close(); |
| 537 |
p.set_outside_bounds(Some((0, 0, 50, 50)), true); |
| 538 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 539 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x49ecc769e1d4ec01); |
| 540 |
assert_eq!(dbg!(calculate_hash(&result)), 0xaf76b42a5244d1ec); |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn outside_clipped() { |
| 545 |
let mut p = PathBuilder::new(); |
| 546 |
p.move_to(10., 10.); |
| 547 |
p.line_to(10., 40.); |
| 548 |
p.line_to(90., 40.); |
| 549 |
p.line_to(40., 10.); |
| 550 |
p.close(); |
| 551 |
p.set_outside_bounds(Some((0, 0, 50, 50)), false); |
| 552 |
let result = p.rasterize_to_tri_list(0, 0, 50, 50); |
| 553 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x3d2a08f5d0bac999); |
| 554 |
assert_eq!(dbg!(calculate_hash(&result)), 0xbd42b934ab52be39); |
| 555 |
} |
| 556 |
|
| 557 |
#[test] |
| 558 |
fn clip_edge() { |
| 559 |
let mut p = PathBuilder::new(); |
| 560 |
// tests the bigNumerator < 0 case of aarasterizer::ClipEdge |
| 561 |
p.curve_to(-24., -10., -300., 119., 0.0, 0.0); |
| 562 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 563 |
// The edge merging only happens between points inside the enumerate buffer. This means |
| 564 |
// that the vertex output can depend on the size of the enumerate buffer because there |
| 565 |
// the number of edges and positions of vertices will change depending on edge merging. |
| 566 |
if ENUMERATE_BUFFER_NUMBER!() == 32 { |
| 567 |
assert_eq!(result.len(), 111); |
| 568 |
} else { |
| 569 |
assert_eq!(result.len(), 171); |
| 570 |
} |
| 571 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x50b887b09a4c16e); |
| 572 |
} |
| 573 |
|
| 574 |
#[test] |
| 575 |
fn enum_buffer_num() { |
| 576 |
let mut p = PathBuilder::new(); |
| 577 |
p.curve_to(0.0, 0.0, 0.0, 12.0, 0.0, 44.919434); |
| 578 |
p.line_to(64.0, 36.0 ); |
| 579 |
p.line_to(0.0, 80.0,); |
| 580 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 581 |
assert_eq!(result.len(), 300); |
| 582 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x659cc742f16b42f2); |
| 583 |
} |
| 584 |
|
| 585 |
#[test] |
| 586 |
fn fill_alternating_empty_interior_pairs() { |
| 587 |
let mut p = PathBuilder::new(); |
| 588 |
p.line_to( 0., 2. ); |
| 589 |
p.curve_to(0.0, 0.0,1., 6., 0.0, 0.0); |
| 590 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 591 |
assert_eq!(result.len(), 9); |
| 592 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x726606a662fe46a0); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn fill_winding_empty_interior_pairs() { |
| 597 |
let mut p = PathBuilder::new(); |
| 598 |
p.curve_to(45., 61., 0.09, 0., 0., 0.); |
| 599 |
p.curve_to(45., 61., 0.09, 0., 0., 0.); |
| 600 |
p.curve_to(0., 0., 0., 38., 0.09, 15.); |
| 601 |
p.set_fill_mode(FillMode::Winding); |
| 602 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 603 |
assert_eq!(result.len(), 462); |
| 604 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x651ea4ade5543087); |
| 605 |
} |
| 606 |
|
| 607 |
#[test] |
| 608 |
fn empty_fill() { |
| 609 |
let mut p = PathBuilder::new(); |
| 610 |
p.move_to(0., 0.); |
| 611 |
p.line_to(10., 100.); |
| 612 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 613 |
assert_eq!(result.len(), 0); |
| 614 |
} |
| 615 |
|
| 616 |
#[test] |
| 617 |
fn rasterize_line() { |
| 618 |
let mut p = PathBuilder::new(); |
| 619 |
p.move_to(1., 1.); |
| 620 |
p.line_to(2., 1.); |
| 621 |
p.line_to(2., 2.); |
| 622 |
p.line_to(1., 2.); |
| 623 |
p.close(); |
| 624 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 625 |
let mask = rasterize_to_mask(&result, 3, 3); |
| 626 |
assert_eq!(&mask[..], &[0, 0, 0, |
| 627 |
0, 255, 0, |
| 628 |
0, 0, 0][..]); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn triangle() { |
| 633 |
let mut p = PathBuilder::new(); |
| 634 |
p.move_to(1., 10.); |
| 635 |
p.line_to(100., 13.); |
| 636 |
p.line_to(1., 16.); |
| 637 |
p.close(); |
| 638 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 639 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x4757b0c5a19b02f0); |
| 640 |
} |
| 641 |
|
| 642 |
#[test] |
| 643 |
fn single_pixel() { |
| 644 |
let mut p = PathBuilder::new(); |
| 645 |
p.move_to(1.5, 1.5); |
| 646 |
p.line_to(2., 1.5); |
| 647 |
p.line_to(2., 2.); |
| 648 |
p.line_to(1.5, 2.); |
| 649 |
p.close(); |
| 650 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 651 |
assert_eq!(result.len(), 3); |
| 652 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 4, 4)), 0x9f481fe5588e341c); |
| 653 |
} |
| 654 |
|
| 655 |
#[test] |
| 656 |
fn traps_outside_bounds() { |
| 657 |
let mut p = PathBuilder::new(); |
| 658 |
p.move_to(10., 10.0); |
| 659 |
p.line_to(30., 10.); |
| 660 |
p.line_to(50., 20.); |
| 661 |
p.line_to(30., 30.); |
| 662 |
p.line_to(10., 30.); |
| 663 |
p.close(); |
| 664 |
// The generated trapezoids are not necessarily clipped to the outside bounds rect |
| 665 |
// and in this case the outside bounds geometry ends up drawing on top of the |
| 666 |
// edge geometry which could be considered a bug. |
| 667 |
p.set_outside_bounds(Some((0, 0, 50, 30)), true); |
| 668 |
let result = p.rasterize_to_tri_list(0, 0, 100, 100); |
| 669 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 100, 100)), 0x6514e3d79d641f09); |
| 670 |
|
| 671 |
} |
| 672 |
|
| 673 |
#[test] |
| 674 |
fn quad_to() { |
| 675 |
let mut p = PathBuilder::new(); |
| 676 |
p.move_to(10., 10.0); |
| 677 |
p.quad_to(30., 10., 30., 30.); |
| 678 |
p.quad_to(10., 30., 30., 30.); |
| 679 |
p.quad_to(60., 30., 60., 10.); |
| 680 |
p.close(); |
| 681 |
let result = p.rasterize_to_tri_list(0, 0, 70, 40); |
| 682 |
assert_eq!(result.len(), 279); |
| 683 |
assert_eq!(calculate_hash(&rasterize_to_mask(&result, 70, 40)), 0xbd2eec3cfe9bd30b); |
| 684 |
} |
| 685 |
|
| 686 |
#[test] |
| 687 |
fn close_after_move_to() { |
| 688 |
let mut p = PathBuilder::new(); |
| 689 |
p.move_to(10., 0.); |
| 690 |
p.close(); |
| 691 |
p.move_to(0., 0.); |
| 692 |
p.line_to(0., 10.); |
| 693 |
p.line_to(10., 10.); |
| 694 |
p.move_to(10., 0.); |
| 695 |
p.close(); |
| 696 |
let result = p.rasterize_to_tri_list(0, 0, 20, 20); |
| 697 |
assert_eq!(result.len(), 27); |
| 698 |
assert_eq!(dbg!(calculate_hash(&result)), 0xecfdf5bdfa25a1dd); |
| 699 |
} |
| 700 |
} |