Skip to content

Commit

Permalink
replaced raw XMLNode pointers with unique_ptrs
Browse files Browse the repository at this point in the history
which helps to ensure proper ownership and simplifies deleting the corresponding objects
  • Loading branch information
mgieseki committed Oct 27, 2017
1 parent 54edd1f commit 307e5bb
Show file tree
Hide file tree
Showing 30 changed files with 439 additions and 428 deletions.
2 changes: 1 addition & 1 deletion src/DVIToSVG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void DVIToSVG::enterBeginPage (unsigned pageno, const vector<int32_t> &c) {
if (pageno != (unsigned)c[0]) // Does page number shown on page differ from physical page number?
Message::mstream(false) << " [" << c[0] << ']';
Message::mstream().indent(1);
_svg.appendToDoc(new XMLCommentNode(" This file was generated by dvisvgm " + string(PROGRAM_VERSION) + " "));
_svg.appendToDoc(util::make_unique<XMLCommentNode>(" This file was generated by dvisvgm " + string(PROGRAM_VERSION) + " "));
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/DVIToSVGActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void DVIToSVGActions::setChar (double x, double y, unsigned c, bool vertical, co
* @param[in] width length of the horizontal edges */
void DVIToSVGActions::setRule (double x, double y, double height, double width) {
// (x,y) is the lower left corner of the rectangle
XMLElementNode *rect = new XMLElementNode("rect");
auto rect = util::make_unique<XMLElementNode>("rect");
rect->addAttribute("x", x);
rect->addAttribute("y", y-height);
rect->addAttribute("height", height);
Expand All @@ -167,7 +167,7 @@ void DVIToSVGActions::setRule (double x, double y, double height, double width)
rect->addAttribute("transform", getMatrix().getSVG());
if (getColor() != Color::BLACK)
rect->addAttribute("fill", _svg.getColor().svgColorString());
_svg.appendToPage(rect);
_svg.appendToPage(std::move(rect));

// update bounding box
BoundingBox bb(x, y-height, x+width, y);
Expand Down Expand Up @@ -225,13 +225,13 @@ void DVIToSVGActions::endPage (unsigned pageno) {
_svg.transformPage(matrix);
if (_bgcolor != Color::TRANSPARENT) {
// create a rectangle filled with the background color
XMLElementNode *r = new XMLElementNode("rect");
r->addAttribute("x", _bbox.minX());
r->addAttribute("y", _bbox.minY());
r->addAttribute("width", _bbox.width());
r->addAttribute("height", _bbox.height());
r->addAttribute("fill", _bgcolor.svgColorString());
_svg.prependToPage(r);
auto rect = util::make_unique<XMLElementNode>("rect");
rect->addAttribute("x", _bbox.minX());
rect->addAttribute("y", _bbox.minY());
rect->addAttribute("width", _bbox.width());
rect->addAttribute("height", _bbox.height());
rect->addAttribute("fill", _bgcolor.svgColorString());
_svg.prependToPage(std::move(rect));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/DVIToSVGActions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class DVIToSVGActions : public DVIActions, public SpecialActions {
Color getColor () const override {return _svg.getColor();}
int getDVIStackDepth() const override {return _dvireader->stackDepth();}
unsigned getCurrentPageNumber() const override {return _dvireader->currentPageNumber();}
void appendToPage (XMLNode *node) override {_svg.appendToPage(node);}
void appendToDefs (XMLNode *node) override {_svg.appendToDefs(node);}
void prependToPage (XMLNode *node) override {_svg.prependToPage(node);}
void pushContextElement (XMLElementNode *node) override {_svg.pushContextElement(node);}
void appendToPage(std::unique_ptr<XMLNode> &&node) override {_svg.appendToPage(std::move(node));}
void appendToDefs(std::unique_ptr<XMLNode> &&node) override {_svg.appendToDefs(std::move(node));}
void prependToPage(std::unique_ptr<XMLNode> &&node) override {_svg.prependToPage(std::move(node));}
void pushContextElement (std::unique_ptr<XMLElementNode> &&node) override {_svg.pushContextElement(std::move(node));}
void popContextElement () override {_svg.popContextElement();}
void setTextOrientation(bool vertical) override {_svg.setVertical(vertical);}
void moveToX (double x) override;
Expand Down
13 changes: 7 additions & 6 deletions src/DvisvgmSpecialHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "InputReader.hpp"
#include "Length.hpp"
#include "SpecialActions.hpp"
#include "utility.hpp"
#include "XMLNode.hpp"
#include "XMLString.hpp"

Expand Down Expand Up @@ -190,7 +191,7 @@ void DvisvgmSpecialHandler::processRaw (InputReader &ir, SpecialActions &actions
string str = ir.getLine();
if (!str.empty()) {
expand_constants(str, actions);
actions.appendToPage(new XMLTextNode(str));
actions.appendToPage(util::make_unique<XMLTextNode>(str));
}
}
}
Expand All @@ -201,7 +202,7 @@ void DvisvgmSpecialHandler::processRawDef (InputReader &ir, SpecialActions &acti
string str = ir.getLine();
if (!str.empty()) {
expand_constants(str, actions);
actions.appendToDefs(new XMLTextNode(str));
actions.appendToDefs(util::make_unique<XMLTextNode>(str));
}
}
}
Expand Down Expand Up @@ -233,9 +234,9 @@ void DvisvgmSpecialHandler::processRawPut (InputReader &ir, SpecialActions &acti
if ((type == 'P' || type == 'D') && !def.empty()) {
expand_constants(def, actions);
if (type == 'P')
actions.appendToPage(new XMLTextNode(def));
actions.appendToPage(util::make_unique<XMLTextNode>(def));
else { // type == 'D'
actions.appendToDefs(new XMLTextNode(def));
actions.appendToDefs(util::make_unique<XMLTextNode>(def));
type = 'L'; // locked
}
}
Expand Down Expand Up @@ -321,15 +322,15 @@ void DvisvgmSpecialHandler::processImg (InputReader &ir, SpecialActions &actions
Length h = read_length(ir);
string f = ir.getString();
update_bbox(w, h, 0, actions);
XMLElementNode *img = new XMLElementNode("image");
auto img = util::make_unique<XMLElementNode>("image");
img->addAttribute("x", actions.getX());
img->addAttribute("y", actions.getY());
img->addAttribute("width", w.bp());
img->addAttribute("height", h.bp());
img->addAttribute("xlink:href", f);
if (!actions.getMatrix().isIdentity())
img->addAttribute("transform", actions.getMatrix().getSVG());
actions.appendToPage(img);
actions.appendToPage(std::move(img));
}
catch (const UnitException &e) {
throw SpecialException(string("dvisvgm:img: ") + e.what());
Expand Down
3 changes: 2 additions & 1 deletion src/EPSToSVG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "PsSpecialHandler.hpp"
#include "SVGOutput.hpp"
#include "System.hpp"
#include "utility.hpp"
#include "version.hpp"

using namespace std;
Expand Down Expand Up @@ -68,7 +69,7 @@ void EPSToSVG::convert () {
// output SVG file
_svg.removeRedundantElements();
_svg.setBBox(_bbox);
_svg.appendToDoc(new XMLCommentNode(" This file was generated by dvisvgm " + string(PROGRAM_VERSION) + " "));
_svg.appendToDoc(util::make_unique<XMLCommentNode>(" This file was generated by dvisvgm " + string(PROGRAM_VERSION) + " "));
bool success = _svg.write(_out.getPageStream(1, 1));
string svgfname = _out.filename(1, 1);
if (svgfname.empty())
Expand Down
12 changes: 6 additions & 6 deletions src/EPSToSVG.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
#ifndef EPSTOSVG_HPP
#define EPSTOSVG_HPP

#include <memory>
#include <string>
#include "SpecialActions.hpp"
#include "SVGTree.hpp"

struct SVGOutputBase;

class EPSToSVG : protected SpecialActions
{
class EPSToSVG : protected SpecialActions {
public:
EPSToSVG (const std::string &fname, SVGOutputBase &out) : _fname(fname), _out(out), _x(0), _y(0) {}
void convert ();
Expand All @@ -48,10 +48,10 @@ class EPSToSVG : protected SpecialActions
const Matrix& getMatrix () const override {return _svg.getMatrix();}
void getPageTransform (Matrix &matrix) const override {}
void setBgColor (const Color &color) override {}
void appendToPage (XMLNode *node) override {_svg.appendToPage(node);}
void appendToDefs (XMLNode *node) override {_svg.appendToDefs(node);}
void prependToPage (XMLNode *node) override {_svg.prependToPage(node);}
void pushContextElement (XMLElementNode *node) override {_svg.pushContextElement(node);}
void appendToPage(std::unique_ptr<XMLNode> &&node) override {_svg.appendToPage(std::move(node));}
void appendToDefs(std::unique_ptr<XMLNode> &&node) override {_svg.appendToDefs(std::move(node));}
void prependToPage(std::unique_ptr<XMLNode> &&node) override {_svg.prependToPage(std::move(node));}
void pushContextElement (std::unique_ptr<XMLElementNode> &&node) override {_svg.pushContextElement(std::move(node));}
void popContextElement () override {_svg.popContextElement();}
void embed (const BoundingBox &bbox) override {_bbox.embed(bbox);}
void embed (const DPair &p, double r=0) override {if (r==0) _bbox.embed(p); else _bbox.embed(p, r);}
Expand Down
8 changes: 4 additions & 4 deletions src/EmSpecialHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ static DPair cut_vector (char cuttype, const DPair &linedir, double linewidth) {
* @param[in] lw line width in PS point units
* @param[in] actions object providing the actions that can be performed by the SpecialHandler */
static void create_line (const DPair &p1, const DPair &p2, char c1, char c2, double lw, SpecialActions &actions) {
XMLElementNode *node=0;
unique_ptr<XMLElementNode> node;
DPair dir = p2-p1;
if (dir.x() == 0 || dir.y() == 0 || (c1 == 'p' && c2 == 'p')) {
// draw regular line
node = new XMLElementNode("line");
node = util::make_unique<XMLElementNode>("line");
node->addAttribute("x1", p1.x());
node->addAttribute("y1", p1.y());
node->addAttribute("x2", p2.x());
Expand All @@ -107,7 +107,7 @@ static void create_line (const DPair &p1, const DPair &p2, char c1, char c2, dou
<< XMLString(q12.x()) << ',' << XMLString(q12.y()) << ' '
<< XMLString(q22.x()) << ',' << XMLString(q22.y()) << ' '
<< XMLString(q21.x()) << ',' << XMLString(q21.y());
node = new XMLElementNode("polygon");
node = util::make_unique<XMLElementNode>("polygon");
node->addAttribute("points", oss.str());
if (actions.getColor() != Color::BLACK)
node->addAttribute("fill", actions.getColor().svgColorString());
Expand All @@ -117,7 +117,7 @@ static void create_line (const DPair &p1, const DPair &p2, char c1, char c2, dou
actions.embed(q21);
actions.embed(q22);
}
actions.appendToPage(node);
actions.appendToPage(std::move(node));
}


Expand Down
21 changes: 11 additions & 10 deletions src/HyperlinkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Message.hpp"
#include "SpecialActions.hpp"
#include "SVGTree.hpp"
#include "utility.hpp"
#include "XMLNode.hpp"

using namespace std;
Expand Down Expand Up @@ -105,10 +106,10 @@ void HyperlinkManager::createLink (string uri, SpecialActions &actions) {
uri = "/" + uri;
uri = _base + uri;
}
XMLElementNode *anchor = new XMLElementNode("a");
anchor->addAttribute("xlink:href", uri);
anchor->addAttribute("xlink:title", XMLString(name.empty() ? uri : name, false));
actions.pushContextElement(anchor);
auto anchorNode = util::make_unique<XMLElementNode>("a");
anchorNode->addAttribute("xlink:href", uri);
anchorNode->addAttribute("xlink:title", XMLString(name.empty() ? uri : name, false));
actions.pushContextElement(std::move(anchorNode));
actions.bbox("{anchor}", true); // start computing the bounding box of the linked area
_depthThreshold = actions.getDVIStackDepth();
_anchorType = AnchorType::HREF;
Expand Down Expand Up @@ -147,7 +148,7 @@ void HyperlinkManager::markLinkedBox (SpecialActions &actions) {
if (bbox.width() > 0 && bbox.height() > 0) { // does the bounding box extend in both dimensions?
if (MARKER_TYPE != MarkerType::NONE) {
const double linewidth = _linewidth >= 0 ? _linewidth : min(0.5, bbox.height()/15);
XMLElementNode *rect = new XMLElementNode("rect");
auto rect = util::make_unique<XMLElementNode>("rect");
double x = bbox.minX();
double y = bbox.maxY()+linewidth;
double w = bbox.width();
Expand Down Expand Up @@ -178,7 +179,7 @@ void HyperlinkManager::markLinkedBox (SpecialActions &actions) {
rect->addAttribute("y", y);
rect->addAttribute("width", w);
rect->addAttribute("height", h);
actions.prependToPage(rect);
actions.prependToPage(std::move(rect));
if (MARKER_TYPE == MarkerType::BOX || MARKER_TYPE == MarkerType::BGCOLOR) {
// slightly enlarge the boxed area
x -= linewidth;
Expand All @@ -191,14 +192,14 @@ void HyperlinkManager::markLinkedBox (SpecialActions &actions) {
// Create an invisible rectangle around the linked area so that it's easier to access.
// This is only necessary when using paths rather than real text elements together with fonts.
if (!SVGTree::USE_FONTS) {
XMLElementNode *rect = new XMLElementNode("rect");
auto rect = util::make_unique<XMLElementNode>("rect");
rect->addAttribute("x", bbox.minX());
rect->addAttribute("y", bbox.minY());
rect->addAttribute("width", bbox.width());
rect->addAttribute("height", bbox.height());
rect->addAttribute("fill", "white");
rect->addAttribute("fill-opacity", 0);
actions.appendToPage(rect);
actions.appendToPage(std::move(rect));
}
}
}
Expand All @@ -212,10 +213,10 @@ void HyperlinkManager::createViews (unsigned pageno, SpecialActions &actions) {
ostringstream oss;
oss << pagebox.minX() << ' ' << stranchorpair.second.pos << ' '
<< pagebox.width() << ' ' << pagebox.height();
XMLElementNode *view = new XMLElementNode("view");
auto view = util::make_unique<XMLElementNode>("view");
view->addAttribute("id", "loc"+XMLString(stranchorpair.second.id));
view->addAttribute("viewBox", oss.str());
actions.appendToDefs(view);
actions.appendToDefs(std::move(view));
}
}
closeAnchor(actions);
Expand Down
Loading

0 comments on commit 307e5bb

Please sign in to comment.