Skip to content

Commit d96a692

Browse files
committed
FX - support for fonts from TTF and OTF lands
1 parent 4747bc5 commit d96a692

File tree

3 files changed

+109
-56
lines changed

3 files changed

+109
-56
lines changed

core/src/processing/core/PApplet.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.awt.DisplayMode;
3030
import java.awt.EventQueue;
3131
import java.awt.FileDialog;
32-
import java.awt.Font;
3332
import java.awt.Frame;
3433
import java.awt.GraphicsDevice;
3534
import java.awt.GraphicsEnvironment;
@@ -6140,39 +6139,12 @@ public PFont createFont(String name, float size, boolean smooth) {
61406139
* @param charset array containing characters to be generated
61416140
* @see PFont
61426141
* @see PGraphics#textFont(PFont, float)
6143-
* @see PGraphics#text(String, float, float, float, float, float)
6142+
* @see PGraphics#text(String, float, float, float, float)
61446143
* @see PApplet#loadFont(String)
61456144
*/
61466145
public PFont createFont(String name, float size,
61476146
boolean smooth, char[] charset) {
6148-
String lowerName = name.toLowerCase();
6149-
Font baseFont = null;
6150-
6151-
try {
6152-
InputStream stream = null;
6153-
if (lowerName.endsWith(".otf") || lowerName.endsWith(".ttf")) {
6154-
stream = createInput(name);
6155-
if (stream == null) {
6156-
System.err.println("The font \"" + name + "\" " +
6157-
"is missing or inaccessible, make sure " +
6158-
"the URL is valid or that the file has been " +
6159-
"added to your sketch and is readable.");
6160-
return null;
6161-
}
6162-
baseFont = Font.createFont(Font.TRUETYPE_FONT, createInput(name));
6163-
6164-
} else {
6165-
baseFont = PFont.findFont(name);
6166-
}
6167-
return new PFont(baseFont.deriveFont(size * pixelDensity),
6168-
smooth, charset, stream != null,
6169-
pixelDensity);
6170-
6171-
} catch (Exception e) {
6172-
System.err.println("Problem with createFont(\"" + name + "\")");
6173-
e.printStackTrace();
6174-
return null;
6175-
}
6147+
return g.createFont(name, size, smooth, charset);
61766148
}
61776149

61786150

core/src/processing/core/PGraphics.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import java.awt.Color;
3030

3131
// Used for the 'image' object that's been here forever
32+
import java.awt.Font;
3233
import java.awt.Image;
3334

35+
import java.io.InputStream;
3436
import java.util.Map;
3537
import java.util.HashMap;
3638
import java.util.WeakHashMap;
@@ -4013,6 +4015,39 @@ protected void shape(PShape shape, float x, float y, float z, float c, float d,
40134015
// TEXT/FONTS
40144016

40154017

4018+
protected PFont createFont(String name, float size,
4019+
boolean smooth, char[] charset) {
4020+
String lowerName = name.toLowerCase();
4021+
Font baseFont = null;
4022+
4023+
try {
4024+
InputStream stream = null;
4025+
if (lowerName.endsWith(".otf") || lowerName.endsWith(".ttf")) {
4026+
stream = parent.createInput(name);
4027+
if (stream == null) {
4028+
System.err.println("The font \"" + name + "\" " +
4029+
"is missing or inaccessible, make sure " +
4030+
"the URL is valid or that the file has been " +
4031+
"added to your sketch and is readable.");
4032+
return null;
4033+
}
4034+
baseFont = Font.createFont(Font.TRUETYPE_FONT, parent.createInput(name));
4035+
4036+
} else {
4037+
baseFont = PFont.findFont(name);
4038+
}
4039+
return new PFont(baseFont.deriveFont(size * parent.pixelDensity),
4040+
smooth, charset, stream != null,
4041+
parent.pixelDensity);
4042+
4043+
} catch (Exception e) {
4044+
System.err.println("Problem with createFont(\"" + name + "\")");
4045+
e.printStackTrace();
4046+
return null;
4047+
}
4048+
}
4049+
4050+
40164051
public void textAlign(int alignX) {
40174052
textAlign(alignX, BASELINE);
40184053
}

core/src/processing/javafx/PGraphicsFX2D.java

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)