Initial svg support.
This commit is contained in:
parent
101bfb4207
commit
65ac927929
22 changed files with 284 additions and 30 deletions
|
@ -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));
|
||||
}
|
|
@ -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);
|
||||
};
|
7
src/web/svg/SvgElement.cpp
Normal file
7
src/web/svg/SvgElement.cpp
Normal file
|
@ -0,0 +1,7 @@
|
|||
#include "SvgElement.h"
|
||||
|
||||
SvgElement::SvgElement(const std::string& tagName)
|
||||
: XmlElement(tagName)
|
||||
{
|
||||
|
||||
}
|
9
src/web/svg/SvgElement.h
Normal file
9
src/web/svg/SvgElement.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "XmlElement.h"
|
||||
|
||||
class SvgElement : public XmlElement
|
||||
{
|
||||
public:
|
||||
SvgElement(const std::string& tagName);
|
||||
};
|
41
src/web/svg/SvgShapeElement.cpp
Normal file
41
src/web/svg/SvgShapeElement.cpp
Normal 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));
|
||||
}
|
17
src/web/svg/SvgShapeElement.h
Normal file
17
src/web/svg/SvgShapeElement.h
Normal 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);
|
||||
|
||||
};
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class SvgDocument;
|
||||
|
||||
class SvgWriter
|
||||
{
|
||||
public:
|
||||
std::string toString(SvgDocument* document);
|
||||
};
|
62
src/web/svg/elements/SvgShapeElements.cpp
Normal file
62
src/web/svg/elements/SvgShapeElements.cpp
Normal 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));
|
||||
}
|
28
src/web/svg/elements/SvgShapeElements.h
Normal file
28
src/web/svg/elements/SvgShapeElements.h
Normal 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);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue