feature/serialize #1
					 5 changed files with 38 additions and 49 deletions
				
			
		|  | @ -36,16 +36,16 @@ class Generator { | |||
|         final PDDocumentInformation info = new PDDocumentInformation(); | ||||
|         info.setTitle("Labyrinth " + this.labyrinth.getWidth() + "x" + this.labyrinth.getHeight() + ", ID " + this.labyrinth.getRandomSeed()); | ||||
|         pdDocument.setDocumentInformation(info); | ||||
|         final PDPage page = new PDPage(new PDRectangle(pageWidth, pageHeight)); | ||||
|         final PDPage solution = new PDPage(new PDRectangle(pageWidth, pageHeight)); | ||||
|         pdDocument.addPage(page); | ||||
|         pdDocument.addPage(solution); | ||||
|         try (final PDPageContentStream labyrinthPageContentStream = new PDPageContentStream(pdDocument, page); | ||||
|              final PDPageContentStream solutionPageContentStream = new PDPageContentStream(pdDocument, solution)) { | ||||
|             setUpPageContentStream(labyrinthPageContentStream); | ||||
|         final PDPage puzzlePage = new PDPage(new PDRectangle(pageWidth, pageHeight)); | ||||
|         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)) { | ||||
|             setUpPageContentStream(puzzlePageContentStream); | ||||
|             setUpPageContentStream(solutionPageContentStream); | ||||
|             this.drawHorizonzalLines(labyrinthPageContentStream, solutionPageContentStream); | ||||
|             this.drawVerticalLines(labyrinthPageContentStream, solutionPageContentStream); | ||||
|             this.drawHorizontalLines(puzzlePageContentStream, solutionPageContentStream); | ||||
|             this.drawVerticalLines(puzzlePageContentStream, solutionPageContentStream); | ||||
|             this.drawSolution(solutionPageContentStream); | ||||
|         } catch (IOException e) { | ||||
|             e.printStackTrace(); | ||||
|  | @ -68,9 +68,10 @@ class Generator { | |||
|         pageContentStream.setNonStrokingColor(Color.BLACK); | ||||
|     } | ||||
| 
 | ||||
|     private void drawHorizonzalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException { | ||||
|     private void drawHorizontalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException { | ||||
|         // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required. | ||||
|         Coordinate coordinate = new Coordinate(0f, 0f); | ||||
|         // Draw the TOP borders of all tiles. | ||||
|         for (int y = 0; y < this.labyrinth.getHeight(); y++) { | ||||
|             boolean isPainting = false; | ||||
|             coordinate = coordinate.withY(y); | ||||
|  | @ -102,6 +103,7 @@ class Generator { | |||
|                 } | ||||
|             } | ||||
|         } | ||||
|         // Draw the BOTTOM border of the last row of tiles. | ||||
|         boolean isPainting = false; | ||||
|         int y = this.labyrinth.getHeight(); | ||||
|         coordinate = coordinate.withY(this.labyrinth.getHeight()); | ||||
|  | @ -137,6 +139,7 @@ class Generator { | |||
|     private void drawVerticalLines(@NonNull final PDPageContentStream... contentStreams) throws IOException { | ||||
|         // PDF has the origin in the lower left corner. We want it in the upper left corner, hence some magic is required. | ||||
|         Coordinate coordinate = new Coordinate(0f, 0f); | ||||
|         // Draw the LEFT borders of all tiles. | ||||
|         for (int x = 0; x < this.labyrinth.getWidth(); x++) { | ||||
|             boolean isPainting = false; | ||||
|             coordinate = coordinate.withX(x); | ||||
|  | @ -168,6 +171,7 @@ class Generator { | |||
|                 } | ||||
|             } | ||||
|         } | ||||
|         // Draw the RIGHT border of the last column of tiles. | ||||
|         boolean isPainting = false; | ||||
|         int x = this.labyrinth.getWidth(); | ||||
|         coordinate = coordinate.withX(this.labyrinth.getWidth()); | ||||
|  |  | |||
|  | @ -9,7 +9,6 @@ public class PDFRenderer implements Renderer<byte[]> { | |||
|     static final float SCALE = 10; | ||||
| 
 | ||||
|     private PDFRenderer() { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|  | @ -23,6 +22,4 @@ public class PDFRenderer implements Renderer<byte[]> { | |||
|         final Generator generator = new Generator(labyrinth); | ||||
|         return generator.generate(); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -3,24 +3,30 @@ package ch.fritteli.labyrinth.generator.renderer.pdffile; | |||
| import ch.fritteli.labyrinth.generator.model.Labyrinth; | ||||
| 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 java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Path; | ||||
| import java.util.NoSuchElementException; | ||||
| 
 | ||||
| public class PDFFileRenderer implements Renderer<Path> { | ||||
|     @NonNull | ||||
|     private static final PDFRenderer PDF_RENDERER = PDFRenderer.newInstance(); | ||||
|     private Path targetFile; | ||||
|     @NonNull | ||||
|     private Option<Path> targetFile; | ||||
| 
 | ||||
|     private PDFFileRenderer() { | ||||
|         try { | ||||
|             this.targetFile = Files.createTempFile("labyrinth_", ".pdf"); | ||||
|         } catch (IOException e) { | ||||
|             System.err.println("Unable to set default target file."); | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         this.targetFile = Try | ||||
|                 .of(() -> Files.createTempFile("labyrinth_", ".pdf")) | ||||
|                 .onFailure(ex -> { | ||||
|                     System.err.println("Unable to set default target file."); | ||||
|                     ex.printStackTrace(); | ||||
|                 }) | ||||
|                 .toOption(); | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|  | @ -29,12 +35,15 @@ public class PDFFileRenderer implements Renderer<Path> { | |||
|     } | ||||
| 
 | ||||
|     public boolean isTargetFileDefinedAndWritable() { | ||||
|         return this.targetFile != null && this.targetFile.toFile().canWrite(); | ||||
|         return this.targetFile | ||||
|                 .map(Path::toFile) | ||||
|                 .map(File::canWrite) | ||||
|                 .getOrElse(false); | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     public PDFFileRenderer setTargetFile(@NonNull final Path targetFile) { | ||||
|         this.targetFile = targetFile; | ||||
|         this.targetFile = Option.of(targetFile); | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|  | @ -42,18 +51,19 @@ public class PDFFileRenderer implements Renderer<Path> { | |||
|     public Path render(@NonNull final Labyrinth labyrinth) { | ||||
|         if (!this.isTargetFileDefinedAndWritable()) { | ||||
|             try { | ||||
|                 Files.createFile(this.targetFile); | ||||
|             } catch (IOException e) { | ||||
|                 Files.createFile(this.targetFile.get()); | ||||
|             } catch (IOException | NoSuchElementException e) { | ||||
|                 throw new IllegalArgumentException("Cannot write to target file.", e); | ||||
|             } | ||||
|         } | ||||
|         final byte[] bytes = PDF_RENDERER.render(labyrinth); | ||||
|         final Path targetFile = this.targetFile.get(); | ||||
|         try { | ||||
|             Files.write(this.targetFile, bytes); | ||||
|             Files.write(targetFile, bytes); | ||||
|         } catch (IOException e) { | ||||
|             System.err.println("Failed writing to file " + this.targetFile.normalize().toString()); | ||||
|             System.err.println("Failed writing to file " + targetFile.normalize().toString()); | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         return this.targetFile; | ||||
|         return targetFile; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,11 +0,0 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <module type="JAVA_MODULE" version="4"> | ||||
|   <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||||
|     <exclude-output /> | ||||
|     <content url="file://$MODULE_DIR$"> | ||||
|       <sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" /> | ||||
|     </content> | ||||
|     <orderEntry type="inheritedJdk" /> | ||||
|     <orderEntry type="sourceFolder" forTests="false" /> | ||||
|   </component> | ||||
| </module> | ||||
|  | @ -1,11 +0,0 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <module type="JAVA_MODULE" version="4"> | ||||
|   <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||||
|     <exclude-output /> | ||||
|     <content url="file://$MODULE_DIR$"> | ||||
|       <sourceFolder url="file://$MODULE_DIR$/java" isTestSource="true" /> | ||||
|     </content> | ||||
|     <orderEntry type="inheritedJdk" /> | ||||
|     <orderEntry type="sourceFolder" forTests="false" /> | ||||
|   </component> | ||||
| </module> | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue