Skip to content

Commit 6487f62

Browse files
committed
add getFontMetrics() override
1 parent b36ff01 commit 6487f62

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

core/src/processing/core/PGraphics.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.awt.Color;
2929
import java.awt.Font;
30+
import java.awt.FontMetrics;
3031
import java.awt.Image;
3132
import java.awt.Toolkit;
3233
import java.awt.font.FontRenderContext;
@@ -4801,11 +4802,21 @@ protected void textCharScreenImpl(PImage glyph,
48014802

48024803

48034804
/**
4804-
* Convenience method to jump through some Java2D hoops and get an FRC.
4805+
* Convenience method to get a legit FontMetrics object. Where possible,
4806+
* override this any renderer subclass so that you're not using what's
4807+
* returned by getDefaultToolkit() to get your metrics.
48054808
*/
48064809
@SuppressWarnings("deprecation")
4810+
public FontMetrics getFontMetrics(Font font) { // ignore
4811+
return Toolkit.getDefaultToolkit().getFontMetrics(font);
4812+
}
4813+
4814+
4815+
/**
4816+
* Convenience method to jump through some Java2D hoops and get an FRC.
4817+
*/
48074818
public FontRenderContext getFontRenderContext(Font font) { // ignore
4808-
return Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
4819+
return getFontMetrics(font).getFontRenderContext();
48094820
}
48104821

48114822

core/src/processing/core/PGraphicsJava2D.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,9 +1746,7 @@ public float textAscent() {
17461746

17471747
Font font = (Font) textFont.getNative();
17481748
if (font != null) {
1749-
@SuppressWarnings("deprecation")
1750-
FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
1751-
return metrics.getAscent();
1749+
return getFontMetrics(font).getAscent();
17521750
}
17531751
return super.textAscent();
17541752
}
@@ -1761,9 +1759,7 @@ public float textDescent() {
17611759
}
17621760
Font font = (Font) textFont.getNative();
17631761
if (font != null) {
1764-
@SuppressWarnings("deprecation")
1765-
FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
1766-
return metrics.getDescent();
1762+
return getFontMetrics(font).getDescent();
17671763
}
17681764
return super.textDescent();
17691765
}
@@ -1852,7 +1848,7 @@ protected float textWidthImpl(char buffer[], int start, int stop) {
18521848
if (font != null) {
18531849
// maybe should use one of the newer/fancier functions for this?
18541850
int length = stop - start;
1855-
FontMetrics metrics = g2.getFontMetrics(font);
1851+
FontMetrics metrics = getFontMetrics(font);
18561852
// Using fractional metrics makes the measurement worse, not better,
18571853
// at least on OS X 10.6 (November, 2010).
18581854
// TextLayout returns the same value as charsWidth().
@@ -1964,6 +1960,11 @@ protected void textLineImpl(char buffer[], int start, int stop,
19641960
}
19651961

19661962

1963+
@Override
1964+
public FontMetrics getFontMetrics(Font font) {
1965+
return (g2 != null) ? g2.getFontMetrics(font) : super.getFontMetrics(font);
1966+
}
1967+
19671968

19681969
//////////////////////////////////////////////////////////////
19691970

0 commit comments

Comments
 (0)