Skip to content

Commit

Permalink
Add workaround for retrieving font metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
BloCamLimb committed Nov 2, 2024
1 parent f4105a6 commit f6fb88f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions core/src/main/java/icyllis/modernui/graphics/text/OutlineFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,23 @@ public int getMetrics(@NonNull FontPaint paint, @Nullable FontMetricsInt fm) {
throw new IllegalArgumentException();
}
var font = chooseFont(paint.getFontSize());
var metrics = getGraphics(paint).getFontMetrics(font);
int ascent = metrics.getAscent(); // positive
int descent = metrics.getDescent(); // positive
int leading = metrics.getLeading(); // positive
var g = getGraphics(paint);
int ascent; // positive
int descent; // positive
int leading; // positive
try {
var metrics = g.getFontMetrics(font);
ascent = metrics.getAscent();
descent = metrics.getDescent();
leading = metrics.getLeading();
} catch (HeadlessException e) {
// this is used in some scenarios, the results of the two methods are the same
// at least, in OpenJDK 21
var metrics = font.getLineMetrics("M", g.getFontRenderContext());
ascent = (int) (0.95f + metrics.getAscent());
descent = (int) (0.95f + metrics.getDescent());
leading = (int) (0.95f + metrics.getDescent() + metrics.getLeading()) - descent;
}
if (fm != null) {
fm.extendBy(-ascent, descent, leading);
}
Expand Down

0 comments on commit f6fb88f

Please sign in to comment.