Shape.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "Shape.h"
  2. namespace msdfgen {
  3. Shape::Shape() : inverseYAxis(false) { }
  4. void Shape::addContour(const Contour &contour) {
  5. contours.push_back(contour);
  6. }
  7. #ifdef MSDFGEN_USE_CPP11
  8. void Shape::addContour(Contour &&contour) {
  9. contours.push_back((Contour &&) contour);
  10. }
  11. #endif
  12. Contour & Shape::addContour() {
  13. contours.resize(contours.size()+1);
  14. return contours.back();
  15. }
  16. bool Shape::validate() const {
  17. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  18. if (!contour->edges.empty()) {
  19. Point2 corner = contour->edges.back()->point(1);
  20. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  21. if (!*edge)
  22. return false;
  23. if ((*edge)->point(0) != corner)
  24. return false;
  25. corner = (*edge)->point(1);
  26. }
  27. }
  28. }
  29. return true;
  30. }
  31. void Shape::normalize() {
  32. for (std::vector<Contour>::iterator contour = contours.begin(); contour != contours.end(); ++contour)
  33. if (contour->edges.size() == 1) {
  34. EdgeSegment *parts[3] = { };
  35. contour->edges[0]->splitInThirds(parts[0], parts[1], parts[2]);
  36. contour->edges.clear();
  37. contour->edges.push_back(EdgeHolder(parts[0]));
  38. contour->edges.push_back(EdgeHolder(parts[1]));
  39. contour->edges.push_back(EdgeHolder(parts[2]));
  40. }
  41. }
  42. void Shape::bounds(double &l, double &b, double &r, double &t) const {
  43. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  44. contour->bounds(l, b, r, t);
  45. }
  46. void Shape::miterBounds(double &l, double &b, double &r, double &t, double border, double miterLimit) const {
  47. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour)
  48. contour->miterBounds(l, b, r, t, border, miterLimit);
  49. }
  50. void Shape::scanline(Scanline &line, double y) const {
  51. std::vector<Scanline::Intersection> intersections;
  52. double x[3];
  53. int dy[3];
  54. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  55. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  56. int n = (*edge)->scanlineIntersections(x, dy, y);
  57. for (int i = 0; i < n; ++i) {
  58. Scanline::Intersection intersection = { x[i], dy[i] };
  59. intersections.push_back(intersection);
  60. }
  61. }
  62. }
  63. #ifdef MSDFGEN_USE_CPP11
  64. line.setIntersections((std::vector<Scanline::Intersection> &&) intersections);
  65. #else
  66. line.setIntersections(intersections);
  67. #endif
  68. }
  69. }