Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
labs
netmetr-android
Commits
eb8aee93
Commit
eb8aee93
authored
Feb 01, 2018
by
Drahomír Karchňák
Browse files
Adjusted font size according so measured values always fit in the image.
parent
b2cc2ec0
Pipeline
#42432
passed with stages
in 8 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
RMBTStatisticServer/src/at/alladin/rmbt/statisticServer/export/ForumBannerGenerator.java
View file @
eb8aee93
...
...
@@ -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
(
80
f
);
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
/
2
f
,
centerY
+
height
/
2
f
);
}
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
/
2
f
);
}
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
/
2
f
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment