Add svg conversion.
This commit is contained in:
parent
1f85954e98
commit
dfbc87cb09
33 changed files with 602 additions and 79 deletions
90
src/publishing/svg/SvgNode.cpp
Normal file
90
src/publishing/svg/SvgNode.cpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
#include "SvgNode.h"
|
||||
|
||||
#include "CircleNode.h"
|
||||
|
||||
#include "SvgShapeElements.h"
|
||||
|
||||
SvgNode::SvgNode(const Point& location)
|
||||
: AbstractVisualNode(location)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SvgNode::setContent(std::unique_ptr<SvgDocument> doc)
|
||||
{
|
||||
mContent = std::move(doc);
|
||||
mContentDirty = true;
|
||||
|
||||
mChildren.clear();
|
||||
mManagedChildren.clear();
|
||||
}
|
||||
|
||||
void SvgNode::updateTransform()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SvgNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
|
||||
{
|
||||
if (!mContent->getRoot())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto viewbox = mContent->getViewBox();
|
||||
|
||||
for (const auto& svg_element : mContent->getRoot()->getChildren())
|
||||
{
|
||||
std::unique_ptr<AbstractVisualNode> node;
|
||||
|
||||
if (svg_element->getTagName() == "circle")
|
||||
{
|
||||
auto svg_circle = dynamic_cast<SvgCircle*>(svg_element.get());
|
||||
auto loc = svg_circle->getLocation();
|
||||
auto radius = svg_circle->getRadius();
|
||||
auto minor_radius = radius;
|
||||
|
||||
if (svg_circle->getType() == SvgCircle::Type::ELLIPSE)
|
||||
{
|
||||
minor_radius = svg_circle->getMinorRadius();
|
||||
}
|
||||
|
||||
if (svg_element->hasAttribute("transform"))
|
||||
{
|
||||
const auto transform = svg_circle->getTransform();
|
||||
loc.move(transform.getLocation().getX(), transform.getLocation().getY());
|
||||
radius *= transform.getScaleX();
|
||||
minor_radius *= transform.getScaleY();
|
||||
}
|
||||
auto circle_node = std::make_unique<CircleNode>(loc, radius);
|
||||
circle_node->setMinorRadius(minor_radius);
|
||||
node = std::move(circle_node);
|
||||
}
|
||||
|
||||
if (!node)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto raw_node = node.get();
|
||||
mManagedChildren.push_back(std::move(node));
|
||||
|
||||
addChild(raw_node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SvgNode::update(SceneInfo* sceneInfo)
|
||||
{
|
||||
if (mContent && mContentDirty)
|
||||
{
|
||||
createOrUpdateGeometry(sceneInfo);
|
||||
mContentDirty = false;
|
||||
}
|
||||
|
||||
if (mTransformIsDirty)
|
||||
{
|
||||
updateTransform();
|
||||
mTransformIsDirty = false;
|
||||
}
|
||||
}
|
25
src/publishing/svg/SvgNode.h
Normal file
25
src/publishing/svg/SvgNode.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include "AbstractVisualNode.h"
|
||||
#include "SvgDocument.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class SvgNode : public AbstractVisualNode
|
||||
{
|
||||
public:
|
||||
SvgNode(const Point& location);
|
||||
|
||||
void setContent(std::unique_ptr<SvgDocument> doc);
|
||||
|
||||
void update(SceneInfo* sceneInfo);
|
||||
private:
|
||||
void createOrUpdateGeometry(SceneInfo* sceneInfo);
|
||||
void updateTransform();
|
||||
|
||||
bool mContentDirty{ true };
|
||||
|
||||
std::vector<std::unique_ptr<AbstractVisualNode> > mManagedChildren;
|
||||
std::unique_ptr<SvgDocument> mContent;
|
||||
};
|
157
src/publishing/svg/SvgPainter.cpp
Normal file
157
src/publishing/svg/SvgPainter.cpp
Normal file
|
@ -0,0 +1,157 @@
|
|||
#include "SvgPainter.h"
|
||||
|
||||
#include "SvgDocument.h"
|
||||
#include "Scene.h"
|
||||
|
||||
#include "SceneItem.h"
|
||||
#include "SceneModel.h"
|
||||
#include "SceneText.h"
|
||||
|
||||
#include "AbstractGeometricItem.h"
|
||||
#include "AbstractMesh.h"
|
||||
#include "TriMesh.h"
|
||||
#include "AbstractFace.h"
|
||||
|
||||
#include "Circle.h"
|
||||
|
||||
#include "SvgShapeElements.h"
|
||||
#include "XmlAttribute.h"
|
||||
|
||||
std::unique_ptr<SvgDocument> SvgPainter::paint(Scene* scene, double width, double height) const
|
||||
{
|
||||
auto doc = std::make_unique<SvgDocument>();
|
||||
doc->setViewBox(0.0, 0.0, width, height);
|
||||
|
||||
scene->update();
|
||||
|
||||
for (auto item : scene->getItems())
|
||||
{
|
||||
if (item->getType() == SceneItem::Type::MODEL)
|
||||
{
|
||||
auto model = dynamic_cast<SceneModel*>(item);
|
||||
if (model->getGeometry())
|
||||
{
|
||||
paintPrimitive(doc.get(), model);
|
||||
}
|
||||
else
|
||||
{
|
||||
paintMesh(doc.get(), model, scene->shouldShowMeshOutline());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto text = dynamic_cast<SceneText*>(item);
|
||||
paintText(doc.get(), text);
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(doc);
|
||||
}
|
||||
|
||||
void SvgPainter::paintMesh(SvgDocument* document, SceneModel* model, bool showOutline) const
|
||||
{
|
||||
auto mesh = model->getMesh();
|
||||
auto transform = model->getTransform();
|
||||
|
||||
if (mesh->getType() == AbstractMesh::MeshType::TRI)
|
||||
{
|
||||
for (const auto& face : dynamic_cast<TriMesh*>(mesh)->getFaces())
|
||||
{
|
||||
auto svg_tri = std::make_unique<SvgPolygon>();
|
||||
|
||||
std::vector<Point> points(3);
|
||||
unsigned count = 0;
|
||||
|
||||
auto face_locs = face->getNodeLocations();
|
||||
for (const auto& loc : face_locs)
|
||||
{
|
||||
const auto x = loc.getX() * transform.getScaleX() + transform.getLocation().getX();
|
||||
const auto y = loc.getY() * transform.getScaleY() + transform.getLocation().getY();
|
||||
points[count] = { x, y };
|
||||
count++;
|
||||
}
|
||||
svg_tri->setPoints(points);
|
||||
svg_tri->setFill(model->getFillColor());
|
||||
document->getRoot()->addChild(std::move(svg_tri));
|
||||
|
||||
if (showOutline)
|
||||
{
|
||||
auto mesh_outline = std::make_unique<SvgPolyline>();
|
||||
points.push_back(points[0]);
|
||||
mesh_outline->setPoints(points);
|
||||
mesh_outline->setStrokeColor({ 0, 0, 0 });
|
||||
mesh_outline->setStrokeWidth(0.1);
|
||||
document->getRoot()->addChild(std::move(mesh_outline));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SvgPainter::setStyle(SceneModel* model, SvgShapeElement* element) const
|
||||
{
|
||||
auto transform = model->getTransform();
|
||||
|
||||
if (model->hasFillColor())
|
||||
{
|
||||
element->setFill(model->getFillColor());
|
||||
}
|
||||
else
|
||||
{
|
||||
element->setNoFill();
|
||||
}
|
||||
if (model->hasOutlineColor())
|
||||
{
|
||||
element->setStrokeColor(model->getOutlineColor());
|
||||
element->setStrokeWidth(model->getOutlineThickness() / transform.getScaleX());
|
||||
}
|
||||
else
|
||||
{
|
||||
element->setNoStroke();
|
||||
}
|
||||
element->addAttribute(std::move(toTransform(transform)));
|
||||
}
|
||||
|
||||
void SvgPainter::paintPrimitive(SvgDocument* document, SceneModel* model) const
|
||||
{
|
||||
if (model->getGeometry()->getType() == AbstractGeometricItem::Type::RECTANGLE)
|
||||
{
|
||||
auto rect = std::make_unique<SvgRectangle>();
|
||||
rect->setWidth(1.0);
|
||||
rect->setHeight(1.0);
|
||||
|
||||
setStyle(model, rect.get());
|
||||
document->getRoot()->addChild(std::move(rect));
|
||||
}
|
||||
else if (model->getGeometry()->getType() == AbstractGeometricItem::Type::CIRCLE)
|
||||
{
|
||||
auto model_circle = dynamic_cast<Circle*>(model->getGeometry());
|
||||
|
||||
auto is_ellipse = model_circle->getMinorRadius() != model_circle->getRadius();
|
||||
auto circle = std::make_unique<SvgCircle>(model_circle->isEllipse() ? SvgCircle::Type::ELLIPSE : SvgCircle::Type::REGULAR);
|
||||
circle->setRadius(model_circle->getRadius());
|
||||
if (model_circle->isEllipse())
|
||||
{
|
||||
circle->setMinorRadius(model_circle->getMinorRadius());
|
||||
}
|
||||
|
||||
setStyle(model, circle.get());
|
||||
document->getRoot()->addChild(std::move(circle));
|
||||
}
|
||||
}
|
||||
|
||||
void SvgPainter::paintText(SvgDocument* document, SceneText* model) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<XmlAttribute> SvgPainter::toTransform(const Transform& transform) const
|
||||
{
|
||||
auto svg_transform = std::make_unique<XmlAttribute>("transform");
|
||||
|
||||
std::string ops;
|
||||
ops += "translate(" + std::to_string(transform.getLocation().getX()) + " " + std::to_string(transform.getLocation().getY()) + ") ";
|
||||
ops += "scale(" + std::to_string(transform.getScaleX()) + " " + std::to_string(transform.getScaleY()) + ") ";
|
||||
svg_transform->setValue(ops);
|
||||
|
||||
return std::move(svg_transform);
|
||||
}
|
32
src/publishing/svg/SvgPainter.h
Normal file
32
src/publishing/svg/SvgPainter.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#pragma once
|
||||
|
||||
#include "AbstractPainter.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
class SvgDocument;
|
||||
class SvgShapeElement;
|
||||
class XmlAttribute;
|
||||
|
||||
class Scene;
|
||||
class SceneModel;
|
||||
class SceneText;
|
||||
|
||||
class Transform;
|
||||
|
||||
class SvgPainter
|
||||
{
|
||||
public:
|
||||
std::unique_ptr<SvgDocument> paint(Scene* scene, double width = 800.0, double height = 800.0) const;
|
||||
|
||||
private:
|
||||
void paintMesh(SvgDocument* document, SceneModel* model, bool showOutline = false) const;
|
||||
|
||||
void paintPrimitive(SvgDocument* document, SceneModel* model) const;
|
||||
|
||||
void paintText(SvgDocument* document, SceneText* model) const;
|
||||
|
||||
void setStyle(SceneModel* model, SvgShapeElement* element) const;
|
||||
|
||||
std::unique_ptr<XmlAttribute> toTransform(const Transform& transform) const;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue