| 1 | |
| 2 | #pragma once |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | namespace msdfgen { |
| 7 | |
| 8 | /// Fill rule dictates how intersection total is interpreted during rasterization. |
| 9 | enum FillRule { |
| 10 | FILL_NONZERO, |
| 11 | FILL_ODD, // "even-odd" |
| 12 | FILL_POSITIVE, |
| 13 | FILL_NEGATIVE |
| 14 | }; |
| 15 | |
| 16 | /// Resolves the number of intersection into a binary fill value based on fill rule. |
| 17 | bool interpretFillRule(int intersections, FillRule fillRule); |
| 18 | |
| 19 | /// Represents a horizontal scanline intersecting a shape. |
| 20 | class Scanline { |
| 21 | |
| 22 | public: |
| 23 | /// An intersection with the scanline. |
| 24 | struct Intersection { |
| 25 | /// X coordinate. |
| 26 | double x; |
| 27 | /// Normalized Y direction of the oriented edge at the point of intersection. |
| 28 | int direction; |
| 29 | }; |
| 30 | |
| 31 | static double overlap(const Scanline &a, const Scanline &b, double xFrom, double xTo, FillRule fillRule); |
| 32 | |
| 33 | Scanline(); |
| 34 | /// Populates the intersection list. |
| 35 | void setIntersections(const std::vector<Intersection> &intersections); |
| 36 | #ifdef MSDFGEN_USE_CPP11 |
| 37 | void setIntersections(std::vector<Intersection> &&intersections); |
| 38 | #endif |
| 39 | /// Returns the number of intersections left of x. |
| 40 | int countIntersections(double x) const; |
| 41 | /// Returns the total sign of intersections left of x. |
| 42 | int sumIntersections(double x) const; |
| 43 | /// Decides whether the scanline is filled at x based on fill rule. |
| 44 | bool filled(double x, FillRule fillRule) const; |
| 45 | |
| 46 | private: |
| 47 | std::vector<Intersection> intersections; |
| 48 | mutable int lastIndex; |
| 49 | |
| 50 | void preprocess(); |
| 51 | int moveTo(double x) const; |
| 52 | |
| 53 | }; |
| 54 | |
| 55 | } |
| 56 | |