@@ -1282,19 +1282,37 @@ public PShape loadShape(String filename, String options) {
12821282 // and mirrors PGraphics.textFont field
12831283 protected FontInfo textFontInfo ;
12841284
1285- protected Text measuringText = new Text ();
1285+
1286+ @ Override
1287+ protected PFont createFont (String name , float size ,
1288+ boolean smooth , char [] charset ) {
1289+ PFont font = super .createFont (name , size , smooth , charset );
1290+ if (font .isStream ()) {
1291+ Font fxFont = Font .loadFont (parent .createInput (name ), size );
1292+ if (fxFont != null ) {
1293+ String fontName = font .getName ();
1294+ fontCache .nameToFilename .put (fontName , name );
1295+ FontInfo fontInfo = fontCache .createFontInfo (fxFont , false );
1296+ fontCache .put (fontName , size , fontInfo );
1297+ }
1298+ }
1299+ return font ;
1300+ }
1301+
12861302
12871303 @ Override
12881304 protected void defaultFontOrDeath (String method , float size ) {
12891305 super .defaultFontOrDeath (method , size );
12901306 textFontInfo = fontCache .get (textFont .getName (), textFont .getSize ());
12911307 }
12921308
1309+
12931310 @ Override
12941311 protected boolean textModeCheck (int mode ) {
12951312 return mode == MODEL ;
12961313 }
12971314
1315+
12981316 @ Override
12991317 public void textSize (float size ) {
13001318 if (size <= 0 ) {
@@ -1311,6 +1329,7 @@ public void textSize(float size) {
13111329 textFontImpl (textFont , size );
13121330 }
13131331
1332+
13141333 @ Override
13151334 public void textFont (PFont which ) {
13161335 if (which != null ) {
@@ -1320,6 +1339,7 @@ public void textFont(PFont which) {
13201339 }
13211340 }
13221341
1342+
13231343 @ Override
13241344 public void textFont (PFont which , float size ) {
13251345 if (which != null ) {
@@ -1337,9 +1357,10 @@ public void textFont(PFont which, float size) {
13371357 }
13381358 }
13391359
1360+
13401361 /**
1341- * Sets the font and size without doing any checks.
1342- * Check the validity of args before calling.
1362+ * Sets the font and the size. Check the validity of args and
1363+ * print possible errors to the user before calling this .
13431364 *
13441365 * @param which font to set, not null
13451366 * @param size size to set, greater than zero
@@ -1348,9 +1369,31 @@ protected void textFontImpl(PFont which, float size) {
13481369 textFont = which ;
13491370 textSize = size ;
13501371
1351- textFontInfo = fontCache .get (which .getName (), size );
1372+ String fontName = which .getName ();
1373+
1374+ textFontInfo = fontCache .get (fontName , size );
1375+ if (textFontInfo == null ) {
1376+ Font font ;
1377+ String filename = fontCache .nameToFilename .get (fontName );
1378+ if (filename != null ) {
1379+ font = Font .loadFont (parent .createInput (filename ), size );
1380+ } else {
1381+ font = new Font (fontName , size );
1382+ }
1383+
1384+ // Loading from file is guaranteed to succeed, font was already
1385+ // successfully loaded in createFont().
1386+ // Loading system font may return fallback font if the font
1387+ // does not exist; this can be detected by comparing font names.
1388+ // Please note that some font names can differ in FX vs. AWT.
1389+ boolean isFallbackFont = filename == null &&
1390+ !fontName .equalsIgnoreCase (font .getName ());
1391+ textFontInfo = fontCache .createFontInfo (font , isFallbackFont );
1392+ fontCache .put (fontName , size , textFontInfo );
1393+ }
1394+
13521395 context .setFont (textFontInfo .font );
1353- measuringText .setFont (textFontInfo .font );
1396+ fontCache . measuringText .setFont (textFontInfo .font );
13541397 if (textFontInfo .isFallbackFont ) {
13551398 textLeading = (textFont .ascent () + textFont .descent ()) * 1.275f ;
13561399 } else {
@@ -1399,6 +1442,8 @@ protected static final class FontCache {
13991442
14001443 private static final int CACHE_SIZE = 512 ;
14011444
1445+ protected Map <String , String > nameToFilename = new HashMap <>();
1446+
14021447 private final LinkedHashMap <Key , FontInfo > cache = new LinkedHashMap <Key , FontInfo >(16 , 0.75f , true ) {
14031448
14041449 @ Override
@@ -1413,27 +1458,28 @@ protected boolean removeEldestEntry(Map.Entry<Key, FontInfo> eldest) {
14131458 private FontInfo get (String name , float size ) {
14141459 retrievingKey .name = name ;
14151460 retrievingKey .size = size ;
1416- FontInfo fontInfo = cache .get (retrievingKey );
1417- if (fontInfo == null ) {
1418- fontInfo = new FontInfo ();
1419- fontInfo .font = new Font (name , size );
1420- fontInfo .isFallbackFont = !name .equalsIgnoreCase (fontInfo .font .getName ());
1421- { // measure ascent and descent
1422- measuringText .setFont (fontInfo .font );
1423- measuringText .setText (" " );
1424- float lineHeight = (float ) measuringText .getLayoutBounds ().getHeight ();
1425- fontInfo .ascent = (float ) measuringText .getBaselineOffset ();
1426- fontInfo .descent = lineHeight - fontInfo .ascent ;
1427- }
1461+ return cache .get (retrievingKey );
1462+ }
14281463
1429- { // create new key and add the info to the cache
1430- Key key = new Key ();
1431- key .name = name ;
1432- key .size = size ;
1433- cache .put (key , fontInfo );
1434- }
1464+ private void put (String name , float size , FontInfo fontInfo ) {
1465+ Key key = new Key ();
1466+ key .name = name ;
1467+ key .size = size ;
1468+ cache .put (key , fontInfo );
1469+ }
1470+
1471+ private FontInfo createFontInfo (Font font , boolean isFallbackFont ) {
1472+ FontInfo result = new FontInfo ();
1473+ result .isFallbackFont = isFallbackFont ;
1474+ result .font = font ;
1475+ { // measure ascent and descent
1476+ measuringText .setFont (result .font );
1477+ measuringText .setText (" " );
1478+ float lineHeight = (float ) measuringText .getLayoutBounds ().getHeight ();
1479+ result .ascent = (float ) measuringText .getBaselineOffset ();
1480+ result .descent = lineHeight - result .ascent ;
14351481 }
1436- return fontInfo ;
1482+ return result ;
14371483 }
14381484
14391485 private static final class Key {
@@ -1545,8 +1591,8 @@ protected float textWidthImpl(char[] buffer, int start, int stop) {
15451591 return super .textWidthImpl (buffer , start , stop );
15461592 }
15471593
1548- measuringText .setText (new String (buffer , start , stop - start ));
1549- return (float ) measuringText .getLayoutBounds ().getWidth ();
1594+ fontCache . measuringText .setText (new String (buffer , start , stop - start ));
1595+ return (float ) fontCache . measuringText .getLayoutBounds ().getWidth ();
15501596 }
15511597
15521598
0 commit comments