Scanline.h 1.5 KB

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