diff --git a/src/main/java/eu/m724/Crossword.java b/src/main/java/eu/m724/Crossword.java index b37405c..1997454 100644 --- a/src/main/java/eu/m724/Crossword.java +++ b/src/main/java/eu/m724/Crossword.java @@ -14,12 +14,21 @@ public record Crossword( String solution, PlacedWord[] words ) { + /** + * Render as SVG + * + * @return SVG file content + */ + public String render(Renderer renderer) { + return renderer.render(this); + } + /** * Render as SVG * * @return SVG file content */ public String render() { - return Renderer.render(this); + return render(new Renderer(100)); } } diff --git a/src/main/java/eu/m724/Renderer.java b/src/main/java/eu/m724/Renderer.java index 059bb5b..5290154 100644 --- a/src/main/java/eu/m724/Renderer.java +++ b/src/main/java/eu/m724/Renderer.java @@ -7,10 +7,14 @@ import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; public class Renderer { - private static final int TILE_SIZE = 50; + private final int tileSize; - public static String render(Crossword crossword) { - SVGGraphics2D g2 = new SVGGraphics2D(crossword.width() * TILE_SIZE, crossword.height() * TILE_SIZE); + public Renderer(int tileSize) { + this.tileSize = tileSize; + } + + public String render(Crossword crossword) { + SVGGraphics2D g2 = new SVGGraphics2D(crossword.width() * tileSize, crossword.height() * tileSize); g2.setPaint(Color.BLACK); for (PlacedWord word : crossword.words()) { @@ -20,16 +24,16 @@ public class Renderer { String hint = word.word().hint(); if (word.vertical()) { - drawStringFit(g2, x * TILE_SIZE, (y - 1) * TILE_SIZE, hint); - //g2.drawString(word.word().hint(), x * TILE_SIZE, (y) * TILE_SIZE); + drawStringFit(g2, x * tileSize, (y - 1) * tileSize, hint); + for (int i=0; i TILE_SIZE) { // TODO ideally this should be splitting by word - int linesAmount = (int) Math.ceil((double) textWidth / TILE_SIZE); + if (textWidth > tileSize) { // TODO ideally this should be splitting by word + int linesAmount = (int) Math.ceil((double) textWidth / tileSize); + + if (linesAmount * textHeight > tileSize) { + int removeChars = (int) (text.length() - (text.length() / ((double) textWidth / tileSize)) * (tileSize / textHeight)); + System.out.printf("Warning: text can't fit in a tile (%d > %d). Remove %d characters. \"%s\"\n", linesAmount * textHeight, tileSize, removeChars, text); + } lines = new String[linesAmount]; int partLength = text.length() / linesAmount; @@ -69,7 +79,7 @@ public class Renderer { for (int i=0; i