Shape.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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[contours.size()-1];
  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::scanline(Scanline &line, double y) const {
  47. std::vector<Scanline::Intersection> intersections;
  48. double x[3];
  49. int dy[3];
  50. for (std::vector<Contour>::const_iterator contour = contours.begin(); contour != contours.end(); ++contour) {
  51. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  52. int n = (*edge)->scanlineIntersections(x, dy, y);
  53. for (int i = 0; i < n; ++i) {
  54. Scanline::Intersection intersection = { x[i], dy[i] };
  55. intersections.push_back(intersection);
  56. }
  57. }
  58. }
  59. #ifdef MSDFGEN_USE_CPP11
  60. line.setIntersections((std::vector<Scanline::Intersection> &&) intersections);
  61. #else
  62. line.setIntersections(intersections);
  63. #endif
  64. }
  65. }