49 lines
1.7 KiB
C++
49 lines
1.7 KiB
C++
#include "svg_renderer.hpp"
|
|
|
|
#include "math/constants.hpp"
|
|
#include <fstream>
|
|
|
|
SvgRenderer::SvgRenderer(size_t width, size_t height) : m_width{width}, m_height{height}
|
|
{
|
|
// white bg
|
|
m_out << " <rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\" />" << std::endl;
|
|
}
|
|
|
|
void SvgRenderer::DrawLine(const math::Vector& p0, const math::Vector& p1)
|
|
{
|
|
m_out << " <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_out << " <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_out << " <circle cx=\"" << center.x << "\" cy=\"" << center.y << "\" r=\"" << radius
|
|
<< "\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\" />" << std::endl;
|
|
}
|
|
|
|
void SvgRenderer::Save(const std::filesystem::path& path)
|
|
{
|
|
std::ofstream file{path};
|
|
|
|
if (!file.is_open())
|
|
{
|
|
throw std::runtime_error{"Cannot open file for writing: " + path.string()};
|
|
}
|
|
|
|
file << "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" << m_width << "\" height=\"" << m_height
|
|
<< "\" viewBox=\"0 0 " << m_width << " " << m_height << "\" role=\"img\" aria-label=\"KIV/CPP\">" << std::endl;
|
|
|
|
file << m_out.str();
|
|
|
|
file << "</svg>" << std::endl;
|
|
}
|