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,37 +204,57 @@ 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);
final var lines = this.caption != null && this.caption.contains("\n") ? this.caption.split("\n", -1) : new String[] { this.caption == null ? "" : this.caption };
final var lineHeight = G2DDrawFont.getTextDimensions("M", g2d, this.fontSize, 1.0).getY() + (int) (Math.max(G2DDrawFont.FONT_SIZE_MIN, this.fontSize) * 0.5);
final CoordI2[] lineDims = new CoordI2[lines.length];
int maxLineWidth = 0;
for (int i = 0; i < lines.length; i++) {
lineDims[i] = G2DDrawFont.getTextDimensions(lines[i], g2d, this.fontSize, 1.0);
if (lineDims[i].getX() > maxLineWidth) {
maxLineWidth = lineDims[i].getX();
}
}
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));
switch (alignment) { switch (alignment) {
case LEFT: { case LEFT: {
G2DDrawFont.renderText(g2d, G2DDrawFont.renderText(g2d,
textDimScaled,
this.scaleFactor, this.scaleFactor,
1, 1,
(int) ((this.getHeight() - (textDim.getY())) / 2 / this.scaleFactor), yOffset,
this.fontSize, this.fontSize,
this.caption); line);
break; break;
} }
case CENTER: { case CENTER: {
G2DDrawFont.renderText(g2d, G2DDrawFont.renderText(g2d,
textDim, textDimScaled,
this.scaleFactor, this.scaleFactor,
(int) ((this.getWidth() - (textDim.getX())) / 2 / this.scaleFactor), ((int) (this.getWidth() / this.scaleFactor) - lineDims[i].getX()) / 2,
(int) ((this.getHeight() - (textDim.getY())) / 2 / this.scaleFactor), yOffset,
this.fontSize, this.fontSize,
this.caption); line);
break; break;
} }
case RIGHT: { case RIGHT: {
G2DDrawFont.renderText(g2d, G2DDrawFont.renderText(g2d,
textDim, textDimScaled,
this.scaleFactor, this.scaleFactor,
(int) ((this.getWidth() - textDim.getX()) / this.scaleFactor), (int) (this.getWidth() / this.scaleFactor) - lineDims[i].getX(),
(int) ((this.getHeight() - (textDim.getY())) / 2 / this.scaleFactor), yOffset,
this.fontSize, this.fontSize,
this.caption); line);
break; break;
} }
@@ -242,5 +262,6 @@ public class CustomLabel extends Component {
break; break;
} }
} }
}
} }