fix(renderer): enhance text rendering in CustomLabel to support multi-line captions

This commit is contained in:
extio-de
2026-01-03 20:47:57 +01:00
parent 5c68dd8504
commit 4034770d2a

View File

@@ -177,9 +177,9 @@ public class CustomLabel extends Component {
} }
if (!this.enabled) { if (!this.enabled) {
h = theme.getTextDisabled().getHue(); // h = theme.getTextDisabled().getHue();
s = theme.getTextDisabled().getSaturation(); // s = theme.getTextDisabled().getSaturation();
b = theme.getTextDisabled().getBrightness(); // b = theme.getTextDisabled().getBrightness();
} }
else if ((this.state & STATE_PRESSED) != 0) { else if ((this.state & STATE_PRESSED) != 0) {
h = theme.getSelectionPrimary().getHue(); h = theme.getSelectionPrimary().getHue();
@@ -204,42 +204,63 @@ public class CustomLabel extends Component {
g2d.setColor(Color.getHSBColor(h, s, b)); g2d.setColor(Color.getHSBColor(h, s, b));
final var alignment = this.textAlignment == null ? HorizontalAlignment.CENTER : this.textAlignment; final var alignment = this.textAlignment == null ? HorizontalAlignment.CENTER : this.textAlignment;
final var textDim = G2DDrawFont.getTextDimensions(this.caption, g2d, this.fontSize, this.scaleFactor);
switch (alignment) { final var lines = this.caption != null && this.caption.contains("\n") ? this.caption.split("\n", -1) : new String[] { this.caption == null ? "" : this.caption };
case LEFT: { final var lineHeight = G2DDrawFont.getTextDimensions("M", g2d, this.fontSize, 1.0).getY() + (int) (Math.max(G2DDrawFont.FONT_SIZE_MIN, this.fontSize) * 0.5);
G2DDrawFont.renderText(g2d, final CoordI2[] lineDims = new CoordI2[lines.length];
this.scaleFactor, int maxLineWidth = 0;
1, for (int i = 0; i < lines.length; i++) {
(int) ((this.getHeight() - (textDim.getY())) / 2 / this.scaleFactor), lineDims[i] = G2DDrawFont.getTextDimensions(lines[i], g2d, this.fontSize, 1.0);
this.fontSize, if (lineDims[i].getX() > maxLineWidth) {
this.caption); maxLineWidth = lineDims[i].getX();
break;
} }
}
final int totalTextHeight = lineHeight * lines.length;
final int startY = ((int) (this.getHeight() / this.scaleFactor) - totalTextHeight) / 2;
for (int i = 0; i < lines.length; i++) {
final var line = lines[i];
final var yOffset = startY + i * lineHeight;
final var textDimScaled = ImmutableCoordI2.create((int) (lineDims[i].getX() * this.scaleFactor), (int) (lineDims[i].getY() * this.scaleFactor));
case CENTER: { switch (alignment) {
G2DDrawFont.renderText(g2d, case LEFT: {
textDim, G2DDrawFont.renderText(g2d,
this.scaleFactor, textDimScaled,
(int) ((this.getWidth() - (textDim.getX())) / 2 / this.scaleFactor), this.scaleFactor,
(int) ((this.getHeight() - (textDim.getY())) / 2 / this.scaleFactor), 1,
this.fontSize, yOffset,
this.caption); this.fontSize,
break; line);
break;
}
case CENTER: {
G2DDrawFont.renderText(g2d,
textDimScaled,
this.scaleFactor,
((int) (this.getWidth() / this.scaleFactor) - lineDims[i].getX()) / 2,
yOffset,
this.fontSize,
line);
break;
}
case RIGHT: {
G2DDrawFont.renderText(g2d,
textDimScaled,
this.scaleFactor,
(int) (this.getWidth() / this.scaleFactor) - lineDims[i].getX(),
yOffset,
this.fontSize,
line);
break;
}
default:
break;
} }
case RIGHT: {
G2DDrawFont.renderText(g2d,
textDim,
this.scaleFactor,
(int) ((this.getWidth() - textDim.getX()) / this.scaleFactor),
(int) ((this.getHeight() - (textDim.getY())) / 2 / this.scaleFactor),
this.fontSize,
this.caption);
break;
}
default:
break;
} }
} }