diff --git a/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/Generator.java b/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/Generator.java index d683241..8465203 100644 --- a/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/Generator.java +++ b/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/Generator.java @@ -5,6 +5,10 @@ import ch.fritteli.labyrinth.generator.model.Labyrinth; import ch.fritteli.labyrinth.generator.model.Position; import ch.fritteli.labyrinth.generator.model.Tile; import io.vavr.control.Option; +import java.awt.BasicStroke; +import java.awt.Color; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.Value; @@ -16,18 +20,15 @@ import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.jetbrains.annotations.Nullable; -import java.awt.*; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - @RequiredArgsConstructor @Slf4j class Generator { + @NonNull private final Labyrinth labyrinth; @NonNull - public byte[] generate() { + public ByteArrayOutputStream generate() { final float pageWidth = this.labyrinth.getWidth() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN; final float pageHeight = this.labyrinth.getHeight() * PDFRenderer.SCALE + 2 * PDFRenderer.MARGIN; @@ -39,7 +40,8 @@ class Generator { final PDPage solutionPage = new PDPage(new PDRectangle(pageWidth, pageHeight)); pdDocument.addPage(puzzlePage); pdDocument.addPage(solutionPage); - try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage); final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) { + try (final PDPageContentStream puzzlePageContentStream = new PDPageContentStream(pdDocument, puzzlePage); + final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solutionPage)) { setUpPageContentStream(puzzlePageContentStream); setUpPageContentStream(solutionPageContentStream); this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream); @@ -55,7 +57,7 @@ class Generator { } catch (IOException e) { log.error("Error while writing PDF data", e); } - return output.toByteArray(); + return output; } private void setUpPageContentStream(@NonNull final PDPageContentStream pageContentStream) throws IOException { @@ -241,6 +243,7 @@ class Generator { @Value private class Coordinate { + float x; float y; @@ -268,6 +271,7 @@ class Generator { @Value private class SolutionCoordinate { + float x; float y; diff --git a/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/PDFRenderer.java b/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/PDFRenderer.java index c9ff88a..087db76 100644 --- a/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/PDFRenderer.java +++ b/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdf/PDFRenderer.java @@ -2,9 +2,10 @@ package ch.fritteli.labyrinth.generator.renderer.pdf; import ch.fritteli.labyrinth.generator.model.Labyrinth; import ch.fritteli.labyrinth.generator.renderer.Renderer; +import java.io.ByteArrayOutputStream; import lombok.NonNull; -public class PDFRenderer implements Renderer { +public class PDFRenderer implements Renderer { static final float MARGIN = 10; static final float SCALE = 10; @@ -18,7 +19,7 @@ public class PDFRenderer implements Renderer { @NonNull @Override - public byte[] render(@NonNull final Labyrinth labyrinth) { + public ByteArrayOutputStream render(@NonNull final Labyrinth labyrinth) { final Generator generator = new Generator(labyrinth); return generator.generate(); } diff --git a/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdffile/PDFFileRenderer.java b/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdffile/PDFFileRenderer.java index a6753e0..7b73d05 100644 --- a/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdffile/PDFFileRenderer.java +++ b/src/main/java/ch/fritteli/labyrinth/generator/renderer/pdffile/PDFFileRenderer.java @@ -5,17 +5,18 @@ import ch.fritteli.labyrinth.generator.renderer.Renderer; import ch.fritteli.labyrinth.generator.renderer.pdf.PDFRenderer; import io.vavr.control.Option; import io.vavr.control.Try; -import lombok.NonNull; -import lombok.extern.slf4j.Slf4j; - +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.NoSuchElementException; +import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; @Slf4j public class PDFFileRenderer implements Renderer { + @NonNull private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance(); @NonNull @@ -55,13 +56,13 @@ public class PDFFileRenderer implements Renderer { throw new IllegalArgumentException("Cannot write to target file.", e); } } - final byte[] bytes = PDF_RENDERER.render(labyrinth); - final Path targetFile = this.targetFile.get(); + final ByteArrayOutputStream bytes = PDF_RENDERER.render(labyrinth); + final Path outputFile = this.targetFile.get(); try { - Files.write(targetFile, bytes); + bytes.writeTo(Files.newOutputStream(outputFile)); } catch (IOException e) { - log.error("Failed writing to file " + targetFile.normalize(), e); + log.error("Failed writing to file " + outputFile.normalize(), e); } - return targetFile; + return outputFile; } }