resolve-shape-geometry.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "resolve-shape-geometry.h"
  2. #ifdef MSDFGEN_USE_SKIA
  3. #include <core/SkPath.h>
  4. #include <pathops/SkPathOps.h>
  5. #include "../core/Vector2.h"
  6. #include "../core/edge-segments.h"
  7. #include "../core/Contour.h"
  8. namespace msdfgen {
  9. SkPoint pointToSkiaPoint(Point2 p) {
  10. return SkPoint::Make((SkScalar) p.x, (SkScalar) p.y);
  11. }
  12. Point2 pointFromSkiaPoint(const SkPoint p) {
  13. return Point2((double) p.x(), (double) p.y());
  14. }
  15. void shapeToSkiaPath(SkPath &skPath, const Shape &shape) {
  16. for (std::vector<Contour>::const_iterator contour = shape.contours.begin(); contour != shape.contours.end(); ++contour) {
  17. if (!contour->edges.empty()) {
  18. skPath.moveTo(pointToSkiaPoint(contour->edges.front()->point(0)));
  19. for (std::vector<EdgeHolder>::const_iterator edge = contour->edges.begin(); edge != contour->edges.end(); ++edge) {
  20. {
  21. const LinearSegment *linearSegment = dynamic_cast<const LinearSegment *>(&**edge);
  22. if (linearSegment)
  23. skPath.lineTo(pointToSkiaPoint(linearSegment->p[1]));
  24. } {
  25. const QuadraticSegment *quadraticSegment = dynamic_cast<const QuadraticSegment *>(&**edge);
  26. if (quadraticSegment)
  27. skPath.quadTo(pointToSkiaPoint(quadraticSegment->p[1]), pointToSkiaPoint(quadraticSegment->p[2]));
  28. } {
  29. const CubicSegment *cubicSegment = dynamic_cast<const CubicSegment *>(&**edge);
  30. if (cubicSegment)
  31. skPath.cubicTo(pointToSkiaPoint(cubicSegment->p[1]), pointToSkiaPoint(cubicSegment->p[2]), pointToSkiaPoint(cubicSegment->p[3]));
  32. }
  33. }
  34. }
  35. }
  36. }
  37. void shapeFromSkiaPath(Shape &shape, const SkPath &skPath) {
  38. shape.contours.clear();
  39. Contour *contour = &shape.addContour();
  40. SkPath::Iter pathIterator(skPath, true);
  41. SkPoint edgePoints[4];
  42. for (SkPath::Verb op; (op = pathIterator.next(edgePoints)) != SkPath::kDone_Verb;) {
  43. switch (op) {
  44. case SkPath::kMove_Verb:
  45. if (!contour->edges.empty())
  46. contour = &shape.addContour();
  47. break;
  48. case SkPath::kLine_Verb:
  49. contour->addEdge(new LinearSegment(pointFromSkiaPoint(edgePoints[0]), pointFromSkiaPoint(edgePoints[1])));
  50. break;
  51. case SkPath::kQuad_Verb:
  52. contour->addEdge(new QuadraticSegment(pointFromSkiaPoint(edgePoints[0]), pointFromSkiaPoint(edgePoints[1]), pointFromSkiaPoint(edgePoints[2])));
  53. break;
  54. case SkPath::kCubic_Verb:
  55. contour->addEdge(new CubicSegment(pointFromSkiaPoint(edgePoints[0]), pointFromSkiaPoint(edgePoints[1]), pointFromSkiaPoint(edgePoints[2]), pointFromSkiaPoint(edgePoints[3])));
  56. break;
  57. default:;
  58. }
  59. }
  60. if (contour->edges.empty())
  61. shape.contours.pop_back();
  62. }
  63. bool resolveShapeGeometry(Shape &shape) {
  64. SkPath skPath;
  65. shapeToSkiaPath(skPath, shape);
  66. if (!Simplify(skPath, &skPath))
  67. return false;
  68. // Skia's AsWinding doesn't seem to work for unknown reasons
  69. shapeFromSkiaPath(shape, skPath);
  70. shape.orientContours();
  71. return true;
  72. }
  73. }
  74. #endif