shapes.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * shapes.cpp - SvgRenderer shape renderer
  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 <fmt/format.h>
  19. #include "svg_module.h"
  20. #include "shape.h"
  21. #include <geom_helpers/null_shape.h>
  22. #include <geom_helpers/rectangle.h>
  23. #include <geom_helpers/circle.h>
  24. #include <geom_helpers/knots.h>
  25. using namespace fmt::literals;
  26. namespace rainynite::core {
  27. namespace renderers {
  28. const string svg_path = R"(<path d="{path}" style="fill:{{fill_color}};fill-opacity:{{fill_opacity}};stroke:none;{{svg_style}}" />)";
  29. const string svg_rectangle = R"(<rect x="{x}" y="{y}" width="{width}" height="{height}" style="fill:{{fill_color}};fill-opacity:{{fill_opacity}};stroke:none;{{svg_style}}"/>)";
  30. const string svg_circle = R"(<circle cx="{x}" cy="{y}" r="{radius}" style="fill:{{fill_color}};fill-opacity:{{fill_opacity}};stroke:none;{{svg_style}}"/>)";
  31. class NullShapeSvgSubRenderer : SVG_SHAPE_RENDERER(NullShapeSvgSubRenderer, Geom::NullShape) {
  32. public:
  33. string operator()(any const& /*shape*/) const override {
  34. return "";
  35. }
  36. };
  37. class PathShapeSvgSubRenderer : SVG_SHAPE_RENDERER(PathShapeSvgSubRenderer, Geom::BezierKnots) {
  38. public:
  39. string operator()(any const& shape) const override {
  40. auto path = any_cast<Geom::BezierKnots>(shape);
  41. return fmt::format(svg_path, "path"_a=Geom::knots_to_svg(path));
  42. }
  43. };
  44. class RectangleShapeSvgSubRenderer : SVG_SHAPE_RENDERER(RectangleShapeSvgSubRenderer, Geom::Rectangle) {
  45. public:
  46. string operator()(any const& shape) const override {
  47. auto rect = any_cast<Geom::Rectangle>(shape);
  48. return fmt::format(
  49. svg_rectangle,
  50. "x"_a=rect.pos.x(),
  51. "y"_a=rect.pos.y(),
  52. "width"_a=rect.size.x(),
  53. "height"_a=rect.size.y()
  54. );
  55. }
  56. };
  57. class CircleShapeSvgSubRenderer : SVG_SHAPE_RENDERER(CircleShapeSvgSubRenderer, Geom::Circle) {
  58. public:
  59. string operator()(any const& shape) const override {
  60. auto circle = any_cast<Geom::Circle>(shape);
  61. return fmt::format(
  62. svg_circle,
  63. "x"_a=circle.pos.x(),
  64. "y"_a=circle.pos.y(),
  65. "radius"_a=circle.radius
  66. );
  67. }
  68. };
  69. } // namespace renderers
  70. } // namespace rainynite::core