Blame |
Last modification |
View Log
| RSS feed
/*
* Copyright (C) 2014 Steve Ratcliffe
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 or
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package test.svg;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* Holds the SVG document.
*
* Various elements are saved as fields, so that information can be added later.
*
* @author Steve Ratcliffe
*/
public class SvgDoc
{
private static final String SODIPODI =
"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd";
private Document doc
; // The whole document
private Element svg
; // The root element.
private Element defs
; // Definitions, eg markers.
// Layers for drawing to
private Element routing
; // The layer used to draw the route network
private Element roads
; // For roads
private Element info
; // For information boxes
private Element spokes
; // Lines from node to its center.
// Offsets to translate the whole diagram by, so that it is all on the page.
private int topOffset
;
private int leftOffset
;
/**
* Create the document outline. Various elements are saved and added to by the
* rest of the program.
*/
public void createDoc
() {
doc = DocumentHelper.
createDocument();
svg = doc.
addElement("svg",
"http://www.w3.org/2000/svg")
.
addAttribute("xmlns",
"http://www.w3.org/2000/svg")
.
addAttribute("xmlns:inkscape",
"http://www.inkscape.org/namespaces/inkscape")
.
addAttribute("xmlns:sodipodi",
"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd")
.
addAttribute("version",
"1.1")
.
addAttribute("id",
"svg2");
// Definitions for the whole document
defs = svg.
addElement("defs").
addAttribute("id",
"defs1");
addMarkers
();
// Information for inkscape.
svg.
addElement("sodipodi:namedview", SODIPODI
)
.
addAttribute("id",
"base")
.
addAttribute("pagecolor",
"#ffffff")
.
addAttribute("inkscape:window-width",
"1036")
.
addAttribute("inkscape:window-height",
"730")
;
// Add the layers
spokes = svg.
addElement("g")
.
addAttribute("inkscape:label",
"center spokes")
.
addAttribute("style",
"display:none")
.
addAttribute("inkscape:groupmode",
"layer");
roads = svg.
addElement("g")
.
addAttribute("inkscape:label",
"roads")
.
addAttribute("inkscape:groupmode",
"layer");
routing = svg.
addElement("g")
.
addAttribute("inkscape:label",
"routing")
.
addAttribute("inkscape:groupmode",
"layer");
info = svg.
addElement("g")
.
addAttribute("inkscape:label",
"info")
.
addAttribute("inkscape:groupmode",
"layer");
}
private void addMarkers
() {
defs.
addElement("marker")
.
addAttribute("inkscape:stockid",
"Arrow2Send")
.
addAttribute("orient",
"auto")
.
addAttribute("refX",
"0.0")
.
addAttribute("refY",
"0.0")
.
addAttribute("id",
"Arrow2Send")
.
addAttribute("style",
"overflow:visible;")
.
addElement("path")
.
addAttribute("style",
"fill-rule:evenodd;stroke-width:0.62500000;stroke-linejoin:round;")
.
addAttribute("d",
"M 8.7185878,4.0337352 L -2.2072895,0.016013256 L 8.7185884,-4.0017078 C 6.9730900,-1.6296469 6.9831476,1.6157441 8.7185878,4.0337352 z ")
.
addAttribute("transform",
"scale(0.3) rotate(180) translate(-2.3,0)")
;
}
public Document getDoc
() {
return doc
;
}
public Element getRouting
() {
return routing
;
}
public Element getRoads
() {
return roads
;
}
public Element getInfo
() {
return info
;
}
public Element getSpokes
() {
return spokes
;
}
public void setTransform
(int leftOffset,
int topOffset
) {
int MARGIN =
30;
String translate =
String.
format("translate(%d,%d)", MARGIN - leftOffset, MARGIN - topOffset
);
spokes.
addAttribute("transform", translate
);
routing.
addAttribute("transform", translate
);
roads.
addAttribute("transform", translate
);
info.
addAttribute("transform", translate
);
}
public int getLeftOffset
() {
return leftOffset
;
}
public int getTopOffset
() {
return topOffset
;
}
public XY coord
(int lat,
int lon
) {
XY co =
new XY
(lat, lon
);
if (co.
getXi() < leftOffset
)
leftOffset = co.
getXi();
if (co.
getYi() < topOffset
)
topOffset = co.
getYi();
return co
;
}
}