refactor(renderer): enhance font handling by introducing FONT_LEADING and optimizing font caching
This commit is contained in:
@@ -27,6 +27,10 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
|||||||
|
|
||||||
public final static int FONT_SIZE_DEFAULT = 18;
|
public final static int FONT_SIZE_DEFAULT = 18;
|
||||||
|
|
||||||
|
public final static double FONT_LEADING = 1.6;
|
||||||
|
|
||||||
|
private static final AtomicReference<Font> cachedFontRef = new AtomicReference<>();
|
||||||
|
|
||||||
private static Font baseFont;
|
private static Font baseFont;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@@ -85,7 +89,7 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
|||||||
graphics.setColor(this.color == null ? this.rendererData.getThemeManager().getCurrentTheme().getTextNormal().toColor() : this.color.toAwtColor());
|
graphics.setColor(this.color == null ? this.rendererData.getThemeManager().getCurrentTheme().getTextNormal().toColor() : this.color.toAwtColor());
|
||||||
|
|
||||||
final var lines = this.text.contains("\n") ? this.text.split("\n", -1) : new String[] { this.text };
|
final var lines = this.text.contains("\n") ? this.text.split("\n", -1) : new String[] { this.text };
|
||||||
final var lineHeight = getTextDimensions("M", graphics, this.size, 1.0).getY() + (int) (Math.max(FONT_SIZE_MIN, this.size) * 0.5);
|
final var lineHeight = (int) (getTextDimensions("M", graphics, this.size, 1.0).getY() * FONT_LEADING); //+ (int) (Math.max(FONT_SIZE_MIN, this.size) * 0.5);
|
||||||
final CoordI2[] lineDims = new CoordI2[lines.length];
|
final CoordI2[] lineDims = new CoordI2[lines.length];
|
||||||
int maxLineWidth = 0;
|
int maxLineWidth = 0;
|
||||||
for (int i = 0; i < lines.length; i++) {
|
for (int i = 0; i < lines.length; i++) {
|
||||||
@@ -166,8 +170,13 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
|||||||
public static Font getFont(final double scaleFactor, final int size_) {
|
public static Font getFont(final double scaleFactor, final int size_) {
|
||||||
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));
|
||||||
|
return cachedFontRef.updateAndGet(existingFont -> {
|
||||||
|
if (existingFont == null || existingFont.getSize2D() != sizeScaled) {
|
||||||
return baseFont.deriveFont(sizeScaled);
|
return baseFont.deriveFont(sizeScaled);
|
||||||
}
|
}
|
||||||
|
return existingFont;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static void renderText(final Graphics graphics, final double scaleFactor, final int x, final int y, final int size_, final String text) {
|
public static void renderText(final Graphics graphics, final double scaleFactor, final int x, final int y, final int size_, final String text) {
|
||||||
renderText(graphics, null, scaleFactor, x, y, size_, text);
|
renderText(graphics, null, scaleFactor, x, y, size_, text);
|
||||||
@@ -180,7 +189,7 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
|||||||
|
|
||||||
if (text.contains("\n")) {
|
if (text.contains("\n")) {
|
||||||
final var lines = text.split("\n", -1);
|
final var lines = text.split("\n", -1);
|
||||||
final var lineHeight = getTextDimensions("M", graphics, size_, scaleFactor).getY();
|
final var lineHeight = (int) (getTextDimensions("M", graphics, size_, scaleFactor).getY() * FONT_LEADING);
|
||||||
|
|
||||||
for (int i = 0; i < lines.length; i++) {
|
for (int i = 0; i < lines.length; i++) {
|
||||||
renderText(graphics, null, scaleFactor, x, y + i * lineHeight, size_, lines[i]);
|
renderText(graphics, null, scaleFactor, x, y + i * lineHeight, size_, lines[i]);
|
||||||
@@ -195,9 +204,7 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final var size = size_ == 0 ? FONT_SIZE_DEFAULT : size_;
|
final var font = getFont(scaleFactor, size_);
|
||||||
final var sizeScaled = Math.max(FONT_SIZE_MIN, (float) (size * scaleFactor));
|
|
||||||
final var font = baseFont.deriveFont(sizeScaled);
|
|
||||||
CoordI2 textDim;
|
CoordI2 textDim;
|
||||||
if (textDim_ == null) {
|
if (textDim_ == null) {
|
||||||
textDim = getTextDimensions(text, graphics, size_, scaleFactor);
|
textDim = getTextDimensions(text, graphics, size_, scaleFactor);
|
||||||
@@ -209,21 +216,12 @@ public class G2DDrawFont extends G2DAbstractRenderingBo implements DrawFontRende
|
|||||||
graphics.drawString(text, (int) (x * scaleFactor), (int) (y * scaleFactor) + textDim.getY());
|
graphics.drawString(text, (int) (x * scaleFactor), (int) (y * scaleFactor) + textDim.getY());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final AtomicReference<Font> cachedFontRef = new AtomicReference<>();
|
|
||||||
|
|
||||||
public static CoordI2 getTextDimensions(final String text, final Graphics graphics, final int size_, final double scaleFactor) {
|
public static CoordI2 getTextDimensions(final String text, final Graphics graphics, final int size_, final double scaleFactor) {
|
||||||
if (text == null) {
|
if (text == null) {
|
||||||
return ImmutableCoordI2.one();
|
return ImmutableCoordI2.one();
|
||||||
}
|
}
|
||||||
|
|
||||||
final var size = size_ == 0 ? FONT_SIZE_DEFAULT : size_;
|
final var font = getFont(scaleFactor, size_);
|
||||||
final var sizeScaled = Math.max(FONT_SIZE_MIN, (float) (size * scaleFactor));
|
|
||||||
final var font = cachedFontRef.updateAndGet(existingFont -> {
|
|
||||||
if (existingFont == null || existingFont.getSize2D() != sizeScaled) {
|
|
||||||
return baseFont.deriveFont(sizeScaled);
|
|
||||||
}
|
|
||||||
return existingFont;
|
|
||||||
});
|
|
||||||
final var gv = font.layoutGlyphVector(((Graphics2D) graphics).getFontRenderContext(), text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
|
final var gv = font.layoutGlyphVector(((Graphics2D) graphics).getFontRenderContext(), text.toCharArray(), 0, text.length(), Font.LAYOUT_LEFT_TO_RIGHT);
|
||||||
final var pixBounds = gv.getPixelBounds(((Graphics2D) graphics).getFontRenderContext(), 0, 0);
|
final var pixBounds = gv.getPixelBounds(((Graphics2D) graphics).getFontRenderContext(), 0, 0);
|
||||||
return MutableCoordI2.create(pixBounds.x + pixBounds.width, pixBounds.height - (int) pixBounds.getMaxY());
|
return MutableCoordI2.create(pixBounds.x + pixBounds.width, pixBounds.height - (int) pixBounds.getMaxY());
|
||||||
|
|||||||
Reference in New Issue
Block a user