feat(renderer): Multi-line text rendering in G2DDrawFont class
This commit is contained in:
@@ -87,20 +87,28 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
||||
|
||||
graphics.setColor(this.color == null ? Color.WHITE : this.color.toAwtColor());
|
||||
|
||||
if (this.text.contains("\n")) {
|
||||
final var lines = this.text.split("\n", -1);
|
||||
final var lineHeight = getTextDimensions("M", graphics, this.size, 1.0).getY();
|
||||
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
final var line = lines[i];
|
||||
final var yOffset = this.y + i * lineHeight;
|
||||
|
||||
switch (this.alignment) {
|
||||
case LEFT:
|
||||
renderText(graphics, scaleFactor, this.x, this.y, this.size, this.text);
|
||||
renderSingleLine(graphics, null, scaleFactor, this.x, yOffset, this.size, line);
|
||||
break;
|
||||
|
||||
case CENTER: {
|
||||
final var textDim = G2DDrawFont.getTextDimensions(this.text, graphics, this.size, 1.0);
|
||||
renderText(graphics, textDim, scaleFactor, (this.width - textDim.getX()) / 2 + this.x, this.y, this.size, this.text);
|
||||
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(this.text, graphics, this.size, 1.0);
|
||||
renderText(graphics, textDim, scaleFactor, this.width - textDim.getX() + this.x, this.y, this.size, this.text);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -108,6 +116,30 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
switch (this.alignment) {
|
||||
case LEFT:
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(final RenderingBo other) {
|
||||
@@ -143,6 +175,24 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
||||
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 sizeScaled = Math.max(FONT_SIZE_MIN, (float) (size * scaleFactor));
|
||||
final var font = baseFont.deriveFont(sizeScaled);
|
||||
|
||||
Reference in New Issue
Block a user