Make renderer configurable and add warnings to it
This commit is contained in:
parent
cc99bf7f1d
commit
0452e1f809
2 changed files with 33 additions and 14 deletions
|
@ -14,12 +14,21 @@ public record Crossword(
|
||||||
String solution,
|
String solution,
|
||||||
PlacedWord[] words
|
PlacedWord[] words
|
||||||
) {
|
) {
|
||||||
|
/**
|
||||||
|
* Render as SVG
|
||||||
|
*
|
||||||
|
* @return SVG file content
|
||||||
|
*/
|
||||||
|
public String render(Renderer renderer) {
|
||||||
|
return renderer.render(this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render as SVG
|
* Render as SVG
|
||||||
*
|
*
|
||||||
* @return SVG file content
|
* @return SVG file content
|
||||||
*/
|
*/
|
||||||
public String render() {
|
public String render() {
|
||||||
return Renderer.render(this);
|
return render(new Renderer(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,14 @@ import java.awt.font.FontRenderContext;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
|
|
||||||
public class Renderer {
|
public class Renderer {
|
||||||
private static final int TILE_SIZE = 50;
|
private final int tileSize;
|
||||||
|
|
||||||
public static String render(Crossword crossword) {
|
public Renderer(int tileSize) {
|
||||||
SVGGraphics2D g2 = new SVGGraphics2D(crossword.width() * TILE_SIZE, crossword.height() * TILE_SIZE);
|
this.tileSize = tileSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String render(Crossword crossword) {
|
||||||
|
SVGGraphics2D g2 = new SVGGraphics2D(crossword.width() * tileSize, crossword.height() * tileSize);
|
||||||
g2.setPaint(Color.BLACK);
|
g2.setPaint(Color.BLACK);
|
||||||
|
|
||||||
for (PlacedWord word : crossword.words()) {
|
for (PlacedWord word : crossword.words()) {
|
||||||
|
@ -20,16 +24,16 @@ public class Renderer {
|
||||||
String hint = word.word().hint();
|
String hint = word.word().hint();
|
||||||
|
|
||||||
if (word.vertical()) {
|
if (word.vertical()) {
|
||||||
drawStringFit(g2, x * TILE_SIZE, (y - 1) * TILE_SIZE, hint);
|
drawStringFit(g2, x * tileSize, (y - 1) * tileSize, hint);
|
||||||
//g2.drawString(word.word().hint(), x * TILE_SIZE, (y) * TILE_SIZE);
|
|
||||||
for (int i=0; i<word.length(); i++) {
|
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 {
|
} else {
|
||||||
drawStringFit(g2, (x - 1) * TILE_SIZE, y * TILE_SIZE, hint);
|
drawStringFit(g2, (x - 1) * tileSize, y * tileSize, hint);
|
||||||
//g2.drawString(word.word().hint(), (x) * 10, y * 10);
|
|
||||||
for (int i=0; i<word.length(); i++) {
|
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
|
// 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();
|
FontRenderContext frc = graphics2D.getFontRenderContext();
|
||||||
Font font = graphics2D.getFont();
|
Font font = graphics2D.getFont();
|
||||||
|
|
||||||
|
@ -46,10 +50,16 @@ public class Renderer {
|
||||||
int textWidth = (int) bounds.getWidth();
|
int textWidth = (int) bounds.getWidth();
|
||||||
int textHeight = (int) bounds.getHeight();
|
int textHeight = (int) bounds.getHeight();
|
||||||
y += textHeight;
|
y += textHeight;
|
||||||
|
|
||||||
String[] lines;
|
String[] lines;
|
||||||
|
|
||||||
if (textWidth > TILE_SIZE) { // TODO ideally this should be splitting by word
|
if (textWidth > tileSize) { // TODO ideally this should be splitting by word
|
||||||
int linesAmount = (int) Math.ceil((double) textWidth / TILE_SIZE);
|
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];
|
lines = new String[linesAmount];
|
||||||
int partLength = text.length() / linesAmount;
|
int partLength = text.length() / linesAmount;
|
||||||
|
@ -69,7 +79,7 @@ public class Renderer {
|
||||||
for (int i=0; i<lines.length; i++) {
|
for (int i=0; i<lines.length; i++) {
|
||||||
String line = lines[i];
|
String line = lines[i];
|
||||||
bounds = font.getStringBounds(line, frc);
|
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));
|
graphics2D.drawString(line, x + (int)padding, y + (textHeight * i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue