transform.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * transform.cpp - SvgRenderer transform 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 <core/std/string.h>
  19. #include <fmt/format.h>
  20. #include "svg_module.h"
  21. using namespace fmt::literals;
  22. namespace rainynite::core {
  23. namespace renderers {
  24. const string svg_translate = R"svg(<g transform="translate({}, {})">{}</g>)svg";
  25. const string svg_scale = R"svg(<g transform="scale({x}, {y})">{source}</g>)svg";
  26. const string svg_rotate = R"svg(<g transform="rotate({angle}, {x}, {y})">{source}</g>)svg";
  27. class TranslateSvgRenderer : SVG_RENDERER_MODULE_CLASS(TranslateSvgRenderer) {
  28. SVG_RENDERER_MODULE_NAME("Translate");
  29. public:
  30. string operator()(AbstractNode const& node, shared_ptr<Context> ctx, SvgRendererSettings const& settings) const override {
  31. auto source = node.get_property("source");
  32. auto offset = node.get_property_as<Geom::Point>("offset")->get(ctx);
  33. return fmt::format(svg_translate, offset.x(), offset.y(), node_to_svg({source, ctx}, settings));
  34. }
  35. };
  36. class ScaleSvgRenderer : SVG_RENDERER_MODULE_CLASS(ScaleSvgRenderer) {
  37. SVG_RENDERER_MODULE_NAME("Scale");
  38. public:
  39. string operator()(AbstractNode const& node, shared_ptr<Context> ctx, SvgRendererSettings const& settings) const override {
  40. auto source = node.get_property("source");
  41. auto scale = node.get_property_as<Geom::Point>("scale")->get(ctx);
  42. return fmt::format(
  43. svg_scale,
  44. "x"_a=scale.x(),
  45. "y"_a=scale.y(),
  46. "source"_a=node_to_svg({source, ctx}, settings)
  47. );
  48. }
  49. };
  50. class RotateSvgRenderer : SVG_RENDERER_MODULE_CLASS(RotateSvgRenderer) {
  51. SVG_RENDERER_MODULE_NAME("Rotate");
  52. public:
  53. string operator()(AbstractNode const& node, shared_ptr<Context> ctx, SvgRendererSettings const& settings) const override {
  54. auto source = node.get_property("source");
  55. auto angle = node.get_property_as<double>("angle")->get(ctx);
  56. auto origin = node.get_property_as<Geom::Point>("origin")->get(ctx);
  57. return fmt::format(
  58. svg_rotate,
  59. "angle"_a=angle*360,
  60. "x"_a=origin.x(),
  61. "y"_a=origin.y(),
  62. "source"_a=node_to_svg({source, ctx}, settings)
  63. );
  64. }
  65. };
  66. } // namespace renderers
  67. } // namespace rainynite::core