Commit 4df9708d authored by Piotr Gawron's avatar Piotr Gawron
Browse files

export to svg contains viewBox information

parent a5d9331e
minerva (12.2.0~beta.2) unstable; urgency=medium
* Bug fix: export to svg contains viewBox info (#716)
* Bug fix: import from sbml with layout could crash when two elements
occupied the exact same position (#717)
* Bug fix: overlays added via API couldn't be visualized after refresh (#718)
......
package lcsb.mapviewer.converter.graphics;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.exception.NotImplementedException;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.exception.NotImplementedException;
/**
* This class implements SVG generator for the image object.This class uses
......@@ -21,6 +24,11 @@ import org.w3c.dom.Document;
*
*/
public class SvgImageGenerator extends AbstractImageGenerator {
/**
* Root dom element. We need it to be able to provide viewBox.
*/
private Element root;
@Override
protected void createImageObject(final double width, final double height) {
......@@ -32,14 +40,17 @@ public class SvgImageGenerator extends AbstractImageGenerator {
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
// Create an instance of the SVG Generator.
setGraphics(new SVGGraphics2D(document));
// Create an instance of the SVG Generator.
SVGGraphics2D graphics =new SVGGraphics2D(document);
root = graphics.getRoot();
root.setAttributeNS(null, "viewBox", "0 0 "+(int)width+" "+(int)height);
setGraphics(graphics);
}
@Override
public void saveToFileImplementation(final String fileName) throws SVGGraphics2DIOException {
((SVGGraphics2D) getGraphics()).stream(fileName);
public void saveToFileImplementation(final String fileName) throws IOException {
saveToOutputStreamImplementation(new FileOutputStream(fileName));
}
/**
......@@ -63,8 +74,9 @@ public class SvgImageGenerator extends AbstractImageGenerator {
@Override
public void saveToOutputStreamImplementation(OutputStream os) throws IOException {
((SVGGraphics2D) getGraphics()).stream(new OutputStreamWriter(os));
//for some reason if you remove this line either viewbox is not define or content is not there
((SVGGraphics2D) getGraphics()).getRoot(root);
((SVGGraphics2D) getGraphics()).stream(root, new OutputStreamWriter(os));
}
@Override
......
......@@ -19,6 +19,7 @@ import lcsb.mapviewer.converter.graphics.placefinder.AllPlaceFinderTest;
MapGeneratorTest.class,
NormalImageGeneratorTest.class,
PdfImageGeneratorTest.class,
SvgImageGeneratorTest.class
})
public class AllGraphicsTests {
......
package lcsb.mapviewer.converter.graphics;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.map.species.Complex;
public class SvgImageGeneratorTest extends GraphicsTestFunctions {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSaveToFile() throws Exception {
try {
Model model = createCompartmentModel();
SvgImageGenerator sig = new SvgImageGenerator(new AbstractImageGenerator.Params().model(model));
ByteArrayOutputStream output = new ByteArrayOutputStream();
sig.saveToOutputStream(output);
assertTrue(output.toString().contains("viewBox"));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
private Model createCompartmentModel() {
Model model = new ModelFullIndexed(null);
model.setWidth(526);
model.setHeight(346);
Complex alias = new Complex("1");
alias.setName("a");
alias.setX(300);
alias.setY(90);
alias.setWidth(100);
alias.setHeight(50);
model.addElement(alias);
return model;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment