|
32 | 32 | import java.awt.geom.Point2D; |
33 | 33 | import java.awt.geom.Rectangle2D; |
34 | 34 | import java.util.ArrayList; |
| 35 | +import sun.java2d.Disposer; |
| 36 | +import sun.java2d.DisposerRecord; |
35 | 37 |
|
36 | 38 | // Right now this class is final to avoid a problem with native code. |
37 | 39 | // For some reason the JNI IsInstanceOf was not working correctly |
@@ -98,6 +100,7 @@ private static native long createNativeFont(final String nativeFontName, |
98 | 100 | private boolean isFakeItalic; |
99 | 101 | private String nativeFontName; |
100 | 102 | private long nativeFontPtr; |
| 103 | + private final Object disposerReferent = new Object(); |
101 | 104 |
|
102 | 105 | private native float getWidthNative(final long nativeFontPtr); |
103 | 106 | private native float getWeightNative(final long nativeFontPtr); |
@@ -194,6 +197,7 @@ public CFont createItalicVariant() { |
194 | 197 | protected synchronized long getNativeFontPtr() { |
195 | 198 | if (nativeFontPtr == 0L) { |
196 | 199 | nativeFontPtr = createNativeFont(nativeFontName, style); |
| 200 | + Disposer.addRecord(disposerReferent, new CFontDisposerRecord(nativeFontPtr)); |
197 | 201 | } |
198 | 202 | return nativeFontPtr; |
199 | 203 | } |
@@ -256,13 +260,17 @@ public CompositeFont getCompositeFont2D() { |
256 | 260 | return compFont; |
257 | 261 | } |
258 | 262 |
|
259 | | - @Override |
260 | | - @SuppressWarnings("removal") |
261 | | - protected synchronized void finalize() { |
262 | | - if (nativeFontPtr != 0) { |
| 263 | + private static class CFontDisposerRecord implements DisposerRecord { |
| 264 | + |
| 265 | + private final long nativeFontPtr; |
| 266 | + |
| 267 | + CFontDisposerRecord(long ptr) { |
| 268 | + nativeFontPtr = ptr; |
| 269 | + } |
| 270 | + |
| 271 | + public void dispose() { |
263 | 272 | disposeNativeFont(nativeFontPtr); |
264 | 273 | } |
265 | | - nativeFontPtr = 0; |
266 | 274 | } |
267 | 275 |
|
268 | 276 | @Override |
|
0 commit comments