Make renderer configurable and add warnings to it

This commit is contained in:
Minecon724 2024-10-04 20:18:56 +02:00
parent cc99bf7f1d
commit 0452e1f809
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 33 additions and 14 deletions

View file

@ -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));
}
}

View file

@ -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<word.length(); i++) {
g2.draw(new Rectangle(x * TILE_SIZE, (y + i) * TILE_SIZE, TILE_SIZE, TILE_SIZE));
g2.draw(new Rectangle(x * tileSize, (y + i) * tileSize, tileSize, tileSize));
}
} else {
drawStringFit(g2, (x - 1) * TILE_SIZE, y * TILE_SIZE, hint);
//g2.drawString(word.word().hint(), (x) * 10, y * 10);
drawStringFit(g2, (x - 1) * tileSize, y * tileSize, hint);
for (int i=0; i<word.length(); i++) {
g2.draw(new Rectangle((x + i) * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE));
g2.draw(new Rectangle((x + i) * tileSize, y * tileSize, tileSize, tileSize));
}
}
}
@ -38,7 +42,7 @@ public class Renderer {
}
// TODO beautify
private static void drawStringFit(SVGGraphics2D graphics2D, int x, int y, String text) {
private void drawStringFit(SVGGraphics2D graphics2D, int x, int y, String text) {
FontRenderContext frc = graphics2D.getFontRenderContext();
Font font = graphics2D.getFont();
@ -46,10 +50,16 @@ public class Renderer {
int textWidth = (int) bounds.getWidth();
int textHeight = (int) bounds.getHeight();
y += textHeight;
String[] lines;
if (textWidth > 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<lines.length; i++) {
String line = lines[i];
bounds = font.getStringBounds(line, frc);
double padding = (TILE_SIZE - bounds.getWidth()) / 2;
double padding = (tileSize - bounds.getWidth()) / 2;
graphics2D.drawString(line, x + (int)padding, y + (textHeight * i));
}
}