feat(renderer): Multi-line text rendering in G2DDrawFont class

This commit is contained in:
extio-de
2025-12-26 18:58:10 +01:00
parent eb1fc240ba
commit 22d99e75b9

View File

@@ -87,25 +87,57 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
graphics.setColor(this.color == null ? Color.WHITE : this.color.toAwtColor()); graphics.setColor(this.color == null ? Color.WHITE : this.color.toAwtColor());
switch (this.alignment) { if (this.text.contains("\n")) {
case LEFT: final var lines = this.text.split("\n", -1);
renderText(graphics, scaleFactor, this.x, this.y, this.size, this.text); final var lineHeight = getTextDimensions("M", graphics, this.size, 1.0).getY();
break;
case CENTER: { for (int i = 0; i < lines.length; i++) {
final var textDim = G2DDrawFont.getTextDimensions(this.text, graphics, this.size, 1.0); final var line = lines[i];
renderText(graphics, textDim, scaleFactor, (this.width - textDim.getX()) / 2 + this.x, this.y, this.size, this.text); final var yOffset = this.y + i * lineHeight;
break;
switch (this.alignment) {
case LEFT:
renderSingleLine(graphics, null, scaleFactor, this.x, yOffset, this.size, line);
break;
case CENTER: {
final var textDim = G2DDrawFont.getTextDimensions(line, graphics, this.size, 1.0);
renderSingleLine(graphics, textDim, scaleFactor, (this.width - textDim.getX()) / 2 + this.x, yOffset, this.size, line);
break;
}
case RIGHT: {
final var textDim = G2DDrawFont.getTextDimensions(line, graphics, this.size, 1.0);
renderSingleLine(graphics, textDim, scaleFactor, this.width - textDim.getX() + this.x, yOffset, this.size, line);
break;
}
default:
break;
}
} }
}
case RIGHT: { else {
final var textDim = G2DDrawFont.getTextDimensions(this.text, graphics, this.size, 1.0); switch (this.alignment) {
renderText(graphics, textDim, scaleFactor, this.width - textDim.getX() + this.x, this.y, this.size, this.text); case LEFT:
break; renderSingleLine(graphics, null, scaleFactor, this.x, this.y, this.size, this.text);
break;
case CENTER: {
final var textDim = G2DDrawFont.getTextDimensions(this.text, graphics, this.size, 1.0);
renderSingleLine(graphics, textDim, scaleFactor, (this.width - textDim.getX()) / 2 + this.x, this.y, this.size, this.text);
break;
}
case RIGHT: {
final var textDim = G2DDrawFont.getTextDimensions(this.text, graphics, this.size, 1.0);
renderSingleLine(graphics, textDim, scaleFactor, this.width - textDim.getX() + this.x, this.y, this.size, this.text);
break;
}
default:
break;
} }
default:
break;
} }
} }
@@ -143,6 +175,24 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
return; return;
} }
if (text.contains("\n")) {
final var lines = text.split("\n", -1);
final var lineHeight = getTextDimensions("M", graphics, size_, scaleFactor).getY();
for (int i = 0; i < lines.length; i++) {
renderText(graphics, null, scaleFactor, x, y + i * lineHeight, size_, lines[i]);
}
return;
}
renderSingleLine(graphics, textDim_, scaleFactor, x, y, size_, text);
}
private static void renderSingleLine(final Graphics graphics, final CoordI2 textDim_, final double scaleFactor, final int x, final int y, final int size_, final String text) {
if (text == null) {
return;
}
final var size = size_ == 0 ? FONT_SIZE_DEFAULT : size_; final var size = size_ == 0 ? FONT_SIZE_DEFAULT : size_;
final var sizeScaled = Math.max(FONT_SIZE_MIN, (float) (size * scaleFactor)); final var sizeScaled = Math.max(FONT_SIZE_MIN, (float) (size * scaleFactor));
final var font = baseFont.deriveFont(sizeScaled); final var font = baseFont.deriveFont(sizeScaled);