Initial svg support.

This commit is contained in:
James Grogan 2022-12-07 20:58:45 +00:00
parent 101bfb4207
commit 65ac927929
22 changed files with 284 additions and 30 deletions

View file

@ -0,0 +1,28 @@
#include "SvgDocument.h"
#include "XmlAttribute.h"
#include <sstream>
SvgDocument::SvgDocument()
: XmlDocument()
{
auto root = XmlElement::Create("svg");
auto xmlns = std::make_unique<XmlAttribute>("xmlns");
xmlns->setValue("http://www.w3.org/2000/svg");
root->addAttribute(std::move(xmlns));
setRoot(std::move(root));
}
void SvgDocument::setViewBox(unsigned x, unsigned y, unsigned w, unsigned h)
{
auto viewbox = std::make_unique<XmlAttribute>("viewBox");
std::stringstream sstr;
sstr << x << " " << y << " " << w << " " << h;
viewbox->setValue(sstr.str());
getRoot()->addAttribute(std::move(viewbox));
}

View file

@ -0,0 +1,10 @@
#pragma once
#include "XmlDocument.h"
class SvgDocument : public XmlDocument
{
public:
SvgDocument();
void setViewBox(unsigned x, unsigned y, unsigned w, unsigned h);
};

View file

@ -0,0 +1,7 @@
#include "SvgElement.h"
SvgElement::SvgElement(const std::string& tagName)
: XmlElement(tagName)
{
}

9
src/web/svg/SvgElement.h Normal file
View file

@ -0,0 +1,9 @@
#pragma once
#include "XmlElement.h"
class SvgElement : public XmlElement
{
public:
SvgElement(const std::string& tagName);
};

View file

@ -0,0 +1,41 @@
#include "SvgShapeElement.h"
#include "XmlAttribute.h"
#include <sstream>
SvgShapeElement::SvgShapeElement(const std::string& tagName)
: SvgElement(tagName)
{
}
void SvgShapeElement::setFill(const Color& fill)
{
auto attr = std::make_unique<XmlAttribute>("fill");
std::stringstream sstr;
sstr << "rgb(" << fill.getR() << "," << fill.getG() << "," << fill.getB() << ")";
attr->setValue(sstr.str());
addAttribute(std::move(attr));
}
void SvgShapeElement::setStrokeWidth(unsigned width)
{
auto attr = std::make_unique<XmlAttribute>("stroke-width");
attr->setValue(std::to_string(width));
addAttribute(std::move(attr));
}
void SvgShapeElement::setStrokeColor(const Color& stroke)
{
auto attr = std::make_unique<XmlAttribute>("stroke");
std::stringstream sstr;
sstr << "rgb(" << stroke.getR() << "," << stroke.getG() << "," << stroke.getB() << ")";
attr->setValue(sstr.str());
addAttribute(std::move(attr));
}

View file

@ -0,0 +1,17 @@
#pragma once
#include "SvgElement.h"
#include "Color.h"
class SvgShapeElement : public SvgElement
{
public:
SvgShapeElement(const std::string& tagName);
void setFill(const Color& fill);
void setStrokeWidth(unsigned width);
void setStrokeColor(const Color& stroke);
};

View file

@ -0,0 +1,13 @@
#include "SvgWriter.h"
#include "SvgDocument.h"
std::string SvgWriter::toString(SvgDocument* document)
{
std::string content = "";
if (auto root = document->getRoot())
{
content += root->toString();
}
return content;
}

View file

@ -0,0 +1,11 @@
#pragma once
#include <string>
class SvgDocument;
class SvgWriter
{
public:
std::string toString(SvgDocument* document);
};

View file

@ -0,0 +1,62 @@
#include "SvgShapeElements.h"
SvgCircle::SvgCircle()
: SvgShapeElement("circle")
{
}
void SvgCircle::setLocation(const DiscretePoint& loc)
{
auto cx = std::make_unique<XmlAttribute>("cx");
auto cy = std::make_unique<XmlAttribute>("cy");
cx->setValue(std::to_string(loc.GetX()));
cy->setValue(std::to_string(loc.GetY()));
addAttribute(std::move(cx));
addAttribute(std::move(cy));
}
void SvgCircle::setRadius(unsigned rad)
{
auto r = std::make_unique<XmlAttribute>("r");
r->setValue(std::to_string(rad));
addAttribute(std::move(r));
}
SvgRectangle::SvgRectangle()
: SvgShapeElement("rect")
{
}
void SvgRectangle::setLocation(const DiscretePoint& loc)
{
auto x = std::make_unique<XmlAttribute>("x");
auto y = std::make_unique<XmlAttribute>("y");
x->setValue(std::to_string(loc.GetX()));
y->setValue(std::to_string(loc.GetY()));
addAttribute(std::move(x));
addAttribute(std::move(y));
}
void SvgRectangle::setWidth(unsigned w)
{
auto width = std::make_unique<XmlAttribute>("width");
width->setValue(std::to_string(w));
addAttribute(std::move(width));
}
void SvgRectangle::setHeight(unsigned h)
{
auto height = std::make_unique<XmlAttribute>("height");
height->setValue(std::to_string(h));
addAttribute(std::move(height));
}

View file

@ -0,0 +1,28 @@
#pragma once
#include "SvgShapeElement.h"
#include "DiscretePoint.h"
#include "XmlAttribute.h"
class SvgCircle : public SvgShapeElement
{
public:
SvgCircle();
void setLocation(const DiscretePoint& loc);
void setRadius(unsigned rad);
};
class SvgRectangle : public SvgShapeElement
{
public:
SvgRectangle();
void setLocation(const DiscretePoint& loc);
void setWidth(unsigned width);
void setHeight(unsigned height);
};