TU-Programmieren_2/exercise6/task3.Root.hpp
2025-04-09 10:22:44 +02:00

56 lines
1.7 KiB
C++

/// @file
/// @brief Task2: SVG-Root element https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg
#pragma once
#include "task2.Element.hpp" // task2::Element
#include "task2.aliases.hpp" // task2::BBox
#include <sstream> // std::stringstream
#include <ostream> // std::endl
#include <string> // std::string
namespace task2 {
/// @brief Root SVG Element (combined a <svg> and a <g> element)
/// @note The group element is used to enable a common transform function for all contained elements to enable a bottom-left origin
/// (the svg-default is top-left)
/// open: <svg preserveAspectRatio='xMidYMid meet' viewbox='0 0 {{w}} {{h}}' height='{{w}}' width='{{h}}'>
/// <g transform='matrix(1 0 0 -1 {{-xmin}} {{ymax}})'>
/// text: empty string
/// close: </g>
/// </svg>
struct RootElement : Element {
RootElement(BBox bbox) : bbox(bbox) {}
std::string open() const override final {
auto [min, max] = bbox;
double xorg = min[0];
double yorg = min[1];
double w = max[0] - min[0];
double h = max[1] - min[1];
std::stringstream ss;
ss << "<svg ";
ss << "xmlns='http://www.w3.org/2000/svg' ";
ss << "version='1.1' ";
ss << "preserveAspectRatio='xMidYMid meet' ";
ss << "viewbox='" << xorg << " " << yorg << " " << w << " " << h << "' ";
ss << "width='" << w << "' ";
ss << "height='" << h << "' ";
ss << ">" << std::endl;
ss << "<g transform='matrix(1 0 0 -1 " << -min[0] << " " << max[1] << ")' >";
return ss.str();
}
std::string close() const override final {
std::stringstream ss;
ss << "</g>" << std::endl;
ss << "</svg>";
return ss.str();
};
std::string text() const override final { return std::string(); }
private:
BBox bbox;
};
} // namespace task2