Skip to content
Snippets Groups Projects
Commit dd078b5a authored by Georg Graßnick's avatar Georg Graßnick :thinking:
Browse files

Support multi page canvas

parent 4504d2de
No related branches found
No related tags found
1 merge request!17Feat/bool mat svg 15
......@@ -175,11 +175,9 @@ public final class App {
mLogger.debug("Render preview:\n" + mat.toBoolString());
// SVG exporting
SvgExporter<MatrixData<Boolean>> svgExporter = new BoolMatrixDataSvgExporter(canvas);
svgExporter.renderSvg();
FileOutputStream fs = new FileOutputStream("boolMat.svg");
svgExporter.dump(fs);
fs.close();
SvgExporter<RasterCanvas> svgExporter = new BoolMatrixDataSvgExporter(canvas);
svgExporter.render();
svgExporter.dump("boolMat");
// Check if some SpoolerService/Printservice exists
if (!PrintDirector.isPrintServiceOn()) {
......
......@@ -8,46 +8,64 @@ import org.jfree.graphics2d.svg.SVGUnits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
/**
* SVG exporter class that supports {@link MatrixData}{@literal <}{@literal Boolean}{@literal >}.
* SVG exporter class that supports {@link RasterCanvas} as input.
*/
public class BoolMatrixDataSvgExporter implements SvgExporter<MatrixData<Boolean>> {
public class BoolMatrixDataSvgExporter implements SvgExporter<RasterCanvas> {
private final RasterCanvas mRasterCanvas;
private SVGGraphics2D mSvg;
private List<SVGGraphics2D> mSvgs;
private final Logger mLogger = LoggerFactory.getLogger(BoolMatrixDataSvgExporter.class);
public BoolMatrixDataSvgExporter(final RasterCanvas rasterCanvas) {
mRasterCanvas = rasterCanvas;
mSvgs = new ArrayList<>(mRasterCanvas.getPageCount());
}
@Override
@SuppressWarnings("unchecked")
public void renderSvg() {
setupSvg();
public void render() {
ListIterator<PrintableData> matIt = mRasterCanvas.getPageIterator();
int idx = 0;
while (matIt.hasNext()) {
renderPage(((MatrixData<Boolean>) matIt.next()));
mSvgs.add(new SVGGraphics2D((int) mRasterCanvas.getPrintableWidth(), (int) mRasterCanvas.getPrintableHeight(), SVGUnits.MM));
renderPage(((MatrixData<Boolean>) matIt.next()), idx++);
}
}
@Override
public void dump(final OutputStream os) throws IOException {
mLogger.trace("Start dunping file to stream ...");
os.write(mSvg.getSVGDocument().getBytes());
mLogger.trace("Finished dunping file to stream ...");
public void dump(final OutputStream os, final int dataIndex) throws IOException {
Objects.requireNonNull(os);
mLogger.trace("Start dumping file to stream ...");
os.write(mSvgs.get(dataIndex).getSVGDocument().getBytes());
mLogger.trace("Finished dumping file to stream");
}
private void setupSvg() {
mSvg = new SVGGraphics2D((int) mRasterCanvas.getPrintableWidth(), (int) mRasterCanvas.getPrintableHeight(), SVGUnits.MM);
@Override
public void dump(final String filePath, final int dataIndex) throws IOException {
Objects.requireNonNull(filePath);
try (FileOutputStream fs = new FileOutputStream(filePath)) {
dump(fs, dataIndex);
}
}
@Override
public void dump(final String baseFileName) throws IOException {
Objects.requireNonNull(baseFileName);
for (int i = 0; i < mSvgs.size(); i++) {
dump(baseFileName + String.format("_%03d.svg", i), i);
}
}
private void renderPage(final MatrixData<Boolean> mat) {
private void renderPage(final MatrixData<Boolean> mat, final int dataIndex) {
List<Double> xPositions = mRasterCanvas.getXPositions();
List<Double> yPositions = mRasterCanvas.getYPositions();
int dotDiameter = (int) mRasterCanvas.getDotDiameter();
......@@ -58,7 +76,7 @@ public class BoolMatrixDataSvgExporter implements SvgExporter<MatrixData<Boolean
if (mat.getValue(y, x)) {
int xPos = (int) (double) xPositions.get(x);
int yPos = (int) (double) yPositions.get(y);
mSvg.drawOval(xPos * mult, yPos * mult, dotDiameter * mult, dotDiameter * mult);
mSvgs.get(dataIndex).drawOval(xPos * mult, yPos * mult, dotDiameter * mult, dotDiameter * mult);
}
}
}
......
package de.tudresden.inf.mci.brailleplot.svgexporter;
import de.tudresden.inf.mci.brailleplot.printabledata.PrintableData;
import de.tudresden.inf.mci.brailleplot.rendering.AbstractCanvas;
import java.io.IOException;
import java.io.OutputStream;
......@@ -9,9 +9,33 @@ import java.io.OutputStream;
* Interface for all classes that support exporting PrintableData to SVG files.
* @param <T> The type of the supported PrintableData.
*/
public interface SvgExporter<T extends PrintableData> {
public interface SvgExporter<T extends AbstractCanvas> {
void renderSvg();
/**
* Process the actual rendering of the data to SVG.
*/
void render();
void dump(OutputStream os) throws IOException;
/**
* Dump a specific rendered SVG to an output stream.
* @param os The stream to dump the SVG to.
* @param dataIndex The index of the {@link de.tudresden.inf.mci.brailleplot.printabledata.PrintableData} in {@link AbstractCanvas#getPageIterator()}
* @throws IOException on any IO related issues.
*/
void dump(OutputStream os, int dataIndex) throws IOException;
/**
Dump a specific rendered SVG to a file.
* @param filePath The path to dump the file at.
* @param dataIndex The index of the {@link de.tudresden.inf.mci.brailleplot.printabledata.PrintableData} in {@link AbstractCanvas#getPageIterator()}
* @throws IOException on any IO related issues.
*/
void dump(String filePath, int dataIndex) throws IOException;
/**
* Dump all rendered SVGs to separate files.
* @param baseFileName The basename of all files. Output filename format: ${basename}_${Index}.svg
* @throws IOException on any IO related issues.
*/
void dump(String baseFileName) throws IOException;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment