knots.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * knots.cpp - Geom paths <-> bezier knots conversion
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <geom_helpers/knots_io.h>
  19. #include <2geom/path-sink.h>
  20. #include <2geom/svg-path-parser.h>
  21. #include <2geom/svg-path-writer.h>
  22. namespace Geom {
  23. BezierKnots path_to_knots(Geom::Path const& path) {
  24. BezierKnots result;
  25. result.closed = path.closed();
  26. if (path.size_default() < 1)
  27. return result;
  28. Geom::Point old_tg;
  29. Geom::Point next_point;
  30. for (auto const& segment : path) {
  31. auto maybe_bezier = dynamic_cast<Geom::CubicBezier const*>(&segment);
  32. if (maybe_bezier == nullptr) {
  33. std::cerr << "not curve" << std::endl;
  34. // throw
  35. continue;
  36. }
  37. auto const& bezier = *maybe_bezier;
  38. auto controls = bezier.controlPoints();
  39. result.knots.emplace_back(controls[0], old_tg, controls[1]);
  40. old_tg = controls[2];
  41. next_point = controls[3];
  42. }
  43. if (!path.closed()) {
  44. result.knots.emplace_back(next_point, old_tg, Geom::Point());
  45. } else {
  46. result.knots[0].tg1 = old_tg;
  47. }
  48. return result;
  49. }
  50. Geom::Path knots_to_path(BezierKnots const& knots) {
  51. if (knots.size() < 1) return Geom::Path();
  52. auto builder = Geom::PathBuilder();
  53. Geom::Knot first_knot;
  54. Geom::Point old_tg;
  55. bool first = true;
  56. for (auto const& knot : knots.knots) {
  57. if (first) {
  58. first = false;
  59. first_knot = knot;
  60. builder.moveTo(knot.pos);
  61. } else {
  62. builder.curveTo(old_tg, knot.tg1, knot.pos);
  63. }
  64. old_tg = knot.tg2;
  65. }
  66. if (knots.closed) {
  67. builder.curveTo(old_tg, first_knot.tg1, first_knot.pos);
  68. builder.closePath();
  69. }
  70. builder.flush();
  71. return builder.peek().at(0);
  72. }
  73. BezierKnots svg_to_knots(std::string const& str) {
  74. return svg_to_knots(str.c_str());
  75. }
  76. BezierKnots svg_to_knots(char const* str) {
  77. return path_to_knots(Geom::parse_svg_path(str).at(0));
  78. }
  79. std::string knots_to_svg(BezierKnots const& knots) {
  80. return Geom::write_svg_path({knots_to_path(knots)});
  81. }
  82. Geom::BezierKnots parse_named_knots(std::string const& str) {
  83. std::istringstream stream(str);
  84. std::string svg_path, keys;
  85. std::getline(stream, svg_path, '@');
  86. std::getline(stream, keys, '@');
  87. auto path = Geom::svg_to_knots(svg_path);
  88. unsigned index = 0;
  89. std::istringstream keystream(keys);
  90. while (keystream.good() && index < path.size()) {
  91. std::string key;
  92. std::getline(keystream, key, ',');
  93. path.knots[index].uid = key;
  94. ++index;
  95. }
  96. return path;
  97. }
  98. std::string knot_names_to_str(Geom::BezierKnots const& knots) {
  99. std::ostringstream stream;
  100. for (auto const& knot : knots.knots) {
  101. stream << knot.uid << ",";
  102. }
  103. return stream.str();
  104. }
  105. std::string named_knots_to_str(Geom::BezierKnots const& knots) {
  106. return knots_to_svg(knots)+" @"+knot_names_to_str(knots)+";";
  107. }
  108. } // namespace Geom