cpp_drawing/renderers/svg_renderer.cpp
2025-09-26 18:39:24 +02:00

39 lines
1.5 KiB
C++

#include "svg_renderer.hpp"
#include "math/constants.hpp"
SvgRenderer::SvgRenderer(const std::filesystem::path& path, size_t width, size_t height) : m_file(path)
{
if (m_file.bad())
throw std::runtime_error("Cannot open " + path.string() + " for writing");
m_file << "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" << width << "\" height=\"" << height
<< "\" viewBox=\"0 0 " << width << " " << height << "\" role=\"img\" aria-label=\"KIV/CPP\">" << std::endl;
}
void SvgRenderer::DrawLine(const math::Vector& p0, const math::Vector& p1)
{
m_file << " <line x1=\"" << p0.x << "\" y1=\"" << p0.y << "\" x2=\"" << p1.x << "\" y2=\"" << p1.y << "\" "
<< "stroke=\"#000000\" stroke-width=\"2\" stroke-linecap=\"round\" />" << std::endl;
}
void SvgRenderer::DrawRectangle(const math::Vector& pos, const math::Vector& size, float angle)
{
float angleDeg = angle * math::RAD_TO_DEG;
m_file << " <rect x=\"" << pos.x << "\" y=\"" << pos.y << "\" width=\"" << size.x << "\" height=\"" << size.y
<< "\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\" stroke-linejoin=\"round\" transform=\"rotate("
<< angleDeg << ", " << pos.x << ", " << pos.y << ")\" />" << std::endl;
}
void SvgRenderer::DrawCircle(const math::Vector& center, float radius)
{
m_file << " <circle cx=\"" << center.x << "\" cy=\"" << center.y << "\" r=\"" << radius
<< "\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\" />" << std::endl;
}
SvgRenderer::~SvgRenderer()
{
m_file << "</svg>" << std::endl;
}