Commit eb8aee93 authored by Drahomír Karchňák's avatar Drahomír Karchňák

Adjusted font size according so measured values always fit in the image.

parent b2cc2ec0
Pipeline #42432 passed with stages
in 8 seconds
......@@ -89,9 +89,29 @@ public class ForumBannerGenerator extends AbstractImageGenerator {
prepareFonts();
renderTemplate(lang, finalImageGraphics);
renderMainValue(formatNumber(download, lang), 93, 118, finalImageGraphics);
renderMainValue(formatNumber(upload, lang), 280, 118, finalImageGraphics);
renderMainValue(formatNumber(ping, lang), 469, 118, finalImageGraphics);
String downloadValue = formatNumber(download, lang);
String uploadValue = formatNumber(upload, lang);
String pingValue = formatNumber(ping, lang);
int optimalFontSize = getOptimalFontSize(
new String[] {downloadValue, uploadValue, pingValue},
new int[] {80, 74, 66, 60, 54, 48, 42, 36, 30, 24},
167,
primaryFont,
finalImageGraphics
);
if(optimalFontSize == -1) {
//At this point something went horribly wrong. Our values wont fit in the image even on the smallest size.
//We should probably give up on life at this point.
//TODO - Error handling?
System.out.println(ForumBannerGenerator.class + " Couldn't find optimal font size!");
optimalFontSize = 1; //Why not?
}
renderMainValue(downloadValue, 93, 118, optimalFontSize, finalImageGraphics);
renderMainValue(uploadValue, 280, 118, optimalFontSize, finalImageGraphics);
renderMainValue(pingValue, 469, 118, optimalFontSize, finalImageGraphics);
String unknownText = UnknownValueMapping.getUnknownValue(lang);
......@@ -120,17 +140,47 @@ public class ForumBannerGenerator extends AbstractImageGenerator {
}
}
private void renderMainValue(String value, int centerX, int centerY, Graphics2D graphics) {
/**
* Gets optimal size from possibleSizes for a font and maximumSize.
* @param values - Strings that need to fit in to maximumSize when rendered
* @param possibleSizes - Possible sizes to try
* @param maximumSize - All Strings from values array need to be smaller than this
* @param font - Font to use
* @param graphics - Graphics to use
* @return Actual optimal font size or -1 on failure.
*/
private int getOptimalFontSize(String[] values, int[] possibleSizes, int maximumSize, Font font, Graphics2D graphics) {
for(int currentSize : possibleSizes) {
Font currentFont = font.deriveFont((float)(currentSize));
FontMetrics fontMetrics = graphics.getFontMetrics(currentFont);
boolean success = true;
for(String currentValue : values) {
if(fontMetrics.stringWidth(currentValue) > maximumSize) {
success = false;
break;
}
}
if(success) return currentSize;
}
//Fallback - return -1
return -1;
}
private void renderMainValue(String value, int centerX, int centerY, float fontSize, Graphics2D graphics) {
graphics.setColor(Color.WHITE);
Font font = primaryFont.deriveFont(80f);
Font font = primaryFont.deriveFont(fontSize); //Need to be float
graphics.setFont(font);
FontMetrics fontMetrics = graphics.getFontMetrics(font);
int width = fontMetrics.stringWidth(value);
int height = fontMetrics.getHeight();
graphics.drawString(value, centerX - width/2, centerY + height/2);
graphics.drawString(value, centerX - width/2f, centerY + height/2f);
}
private void renderAdditionalValue(String value, String unknownValue, int posX, int posY, Graphics2D graphics) {
......@@ -144,7 +194,7 @@ public class ForumBannerGenerator extends AbstractImageGenerator {
FontMetrics fontMetrics = graphics.getFontMetrics(font);
int height = fontMetrics.getHeight();
graphics.drawString(value, posX, posY + height/2);
graphics.drawString(value, posX, posY + height/2f);
}
private void renderTimeDateValue(String value, String unknownValue, int posX, int posY, Graphics2D graphics) {
......@@ -159,6 +209,6 @@ public class ForumBannerGenerator extends AbstractImageGenerator {
int width = fontMetrics.stringWidth(value);
int height = fontMetrics.getHeight();
graphics.drawString(value, posX - width, posY + height/2);
graphics.drawString(value, posX - width, posY + height/2f);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment