shapes.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* shapes.cpp - SvgRenderer shape renderer
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <fmt/format.h>
  18. #include "svg_module.h"
  19. #include "shape.h"
  20. #include <geom_helpers/null_shape.h>
  21. #include <geom_helpers/rectangle.h>
  22. #include <geom_helpers/circle.h>
  23. #include <geom_helpers/knots.h>
  24. using namespace fmt::literals;
  25. namespace rainynite::core::renderers {
  26. const string svg_path = R"(path d="{path}")";
  27. const string svg_rectangle = R"(rect x="{x}" y="{y}" width="{width}" height="{height}")";
  28. const string svg_circle = R"(circle cx="{x}" cy="{y}" r="{radius}")";
  29. class NullShapeSvgSubRenderer : SVG_SHAPE_RENDERER(NullShapeSvgSubRenderer, Geom::NullShape) {
  30. public:
  31. string operator()(any const& /*shape*/) const override {
  32. return "";
  33. }
  34. };
  35. class PathShapeSvgSubRenderer : SVG_SHAPE_RENDERER(PathShapeSvgSubRenderer, Geom::BezierKnots) {
  36. public:
  37. string operator()(any const& shape) const override {
  38. auto path = any_cast<Geom::BezierKnots>(shape);
  39. return fmt::format(svg_path, "path"_a=Geom::knots_to_svg(path));
  40. }
  41. };
  42. class RectangleShapeSvgSubRenderer : SVG_SHAPE_RENDERER(RectangleShapeSvgSubRenderer, Geom::Rectangle) {
  43. public:
  44. string operator()(any const& shape) const override {
  45. auto rect = any_cast<Geom::Rectangle>(shape);
  46. return fmt::format(
  47. svg_rectangle,
  48. "x"_a=rect.pos.x(),
  49. "y"_a=rect.pos.y(),
  50. "width"_a=rect.size.x(),
  51. "height"_a=rect.size.y()
  52. );
  53. }
  54. };
  55. class CircleShapeSvgSubRenderer : SVG_SHAPE_RENDERER(CircleShapeSvgSubRenderer, Geom::Circle) {
  56. public:
  57. string operator()(any const& shape) const override {
  58. auto circle = any_cast<Geom::Circle>(shape);
  59. return fmt::format(
  60. svg_circle,
  61. "x"_a=circle.pos.x(),
  62. "y"_a=circle.pos.y(),
  63. "radius"_a=circle.radius
  64. );
  65. }
  66. };
  67. } // namespace rainynite::core::renderers