Scanline.h 1.5 KB

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