now using floating point values for `SVG` rendering

so we can generate really small svgs
development
Michael Ochmann 2 years ago
parent 7da490f3fa
commit 94b17c6fd8
  1. 16
      src/SVGRenderer.hpp

@ -2,14 +2,20 @@
#include <sstream>
#include <fstream>
#include <math.h>
#include "Renderer.hpp"
namespace massivedynamic {
class SVGRenderer : public Renderer {
private:
constexpr static float BORDER_WIDTH = 0.1f;
public:
SVGRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : Renderer(pixels, sourceSize, targetSize) {}
SVGRenderer(const std::vector<bool>& pixels, size_t sourceSize, size_t targetSize) : Renderer(pixels, sourceSize, targetSize) {
if (this->targetSize == 0)
this->targetSize = sourceSize * (2 * SVGRenderer::BORDER_WIDTH);
}
virtual void render(const std::string& filename) override {
std::stringstream file;
@ -20,15 +26,15 @@ namespace massivedynamic {
file << base;
file << "viewBox=\"0 0 " << this->targetSize << " " << this->targetSize << "\" xml:space=\"preserve\">" << '\n';
file << " <rect x=\"0\" y=\"0\" width=\"" << this->targetSize << "\" height=\"" << this->targetSize << "\" style=\"fill: white;\" />" << '\n';
size_t pixelSize = this->targetSize / (this->sourceSize + 2);
float pixelSize = static_cast<float>(this->targetSize) / static_cast<float>(this->sourceSize + 2);
for (size_t y = 0; y < this->sourceSize; y++) {
for (size_t x = 0; x < this->sourceSize; x++) {
if (!this->pixels.at(y * this->sourceSize + x))
continue;
size_t rectX = x * pixelSize + pixelSize;
size_t rectY = y * pixelSize + pixelSize;
file << " <rect x=\"" << rectX << "\" y=\"" << rectY << "\" width=\"" << pixelSize << "\" height=\"" << pixelSize << "\" style=\"fill: black; stroke: black; stroke-width: 3;\" />" << '\n';
float rectX = x * pixelSize + pixelSize;
float rectY = y * pixelSize + pixelSize;
file << " <rect x=\"" << rectX << "\" y=\"" << rectY << "\" width=\"" << pixelSize << "\" height=\"" << pixelSize << "\" style=\"fill: black; stroke: black; stroke-width: " << SVGRenderer::BORDER_WIDTH << "%;\" />" << '\n';
}
}

Loading…
Cancel
Save