render_shape.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* render_shape.cpp - SvgRenderer shape renderer
  2. * Copyright (C) 2017-2018 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 <core/std/string.h>
  18. #include <fmt/format.h>
  19. #include <core/shading.h>
  20. #include "svg_module.h"
  21. #include "shape.h"
  22. using namespace fmt::literals;
  23. namespace rainynite::core::renderers {
  24. const string styled_svg_element = R"(<{shape} style="fill:{fill_color};fill-opacity:{fill_opacity};stroke:{line_color};stroke-opacity:{line_opacity};stroke-width:{line_width};{svg_style}" {svg_defs} />)";
  25. class ShapeSvgRenderer : SVG_RENDERER_MODULE_CLASS(ShapeSvgRenderer) {
  26. SVG_RENDERER_MODULE_NAME("RenderShape");
  27. public:
  28. string operator()(AbstractNode const& node, shared_ptr<Context> ctx, SvgRendererSettings const& settings) const override {
  29. auto shape = node.get_property("shape")->get_any(ctx);
  30. auto shading_node = node.get_property_as<Shading>("shading");
  31. auto shading = shading_node->value(ctx);
  32. auto extra_style = get_extra_svg(shading_node, ctx, settings, "style");
  33. auto extra_defs = get_extra_svg(shading_node, ctx, settings, "defs");
  34. return render_svg_shape(shape, shading, extra_style, extra_defs);
  35. }
  36. };
  37. string DefaultSvgShapeRenderer::operator()(any const& shape, Shading const& shading, string extra_style, string extra_defs) const {
  38. return fmt::format(
  39. styled_svg_element,
  40. "shape"_a=get_main_shape(shape),
  41. "fill_color"_a=colors::to_hex24(shading.fill_color),
  42. "fill_opacity"_a=shading.fill_color.alpha(),
  43. "line_color"_a=colors::to_hex24(shading.line_color),
  44. "line_opacity"_a=shading.line_color.alpha(),
  45. "line_width"_a=shading.line_width,
  46. "svg_style"_a=extra_style,
  47. "svg_defs"_a=extra_defs
  48. );
  49. }
  50. } // namespace rainynite::core::renderers