FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

FX - clean up native lookup, style and comments · charneykaye/processing@86891c8 · GitHub

Repository navigation

Commit 86891c8

Browse files
committed
FX - clean up native lookup, style and comments
1 parent 4561994 commit 86891c8

1 file changed

Lines changed: 66 additions & 46 deletions

File tree

‎core/src/processing/javafx/PGraphicsFX2D.java‎

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,13 +1289,7 @@ protected PFont createFont(String name, float size,
12891289
boolean smooth, char[] charset) {
12901290
PFont font = super.createFont(name, size, smooth, charset);
12911291
if (font.isStream()) {
1292-
Font fxFont = Font.loadFont(parent.createInput(name), size);
1293-
if (fxFont != null) {
1294-
String fontName = font.getName();
1295-
fontCache.nameToFilename.put(fontName, name);
1296-
FontInfo fontInfo = fontCache.createFontInfo(fxFont);
1297-
fontCache.put(fontName, size, fontInfo);
1298-
}
1292+
fontCache.nameToFilename.put(font.getName(), name);
12991293
}
13001294
return font;
13011295
}
@@ -1319,7 +1313,7 @@ public float textAscent() {
13191313
if (textFont == null) {
13201314
defaultFontOrDeath("textAscent");
13211315
}
1322-
if (textFontInfo == FontInfo.NOT_FOUND) {
1316+
if (textFontInfo == FontInfo.NON_NATIVE) {
13231317
return super.textAscent();
13241318
}
13251319
return textFontInfo.ascent;
@@ -1331,18 +1325,20 @@ public float textDescent() {
13311325
if (textFont == null) {
13321326
defaultFontOrDeath("textDescent");
13331327
}
1334-
if (textFontInfo == FontInfo.NOT_FOUND) {
1328+
if (textFontInfo == FontInfo.NON_NATIVE) {
13351329
return super.textDescent();
13361330
}
13371331
return textFontInfo.descent;
13381332
}
13391333

13401334

1341-
protected static final class FontInfo {
1342-
// TODO: maybe make this dependent on font size?
1343-
protected static final int TINT_CACHE_SIZE = 1 << 10;
1335+
static final class FontInfo {
1336+
1337+
// TODO: this should be based on memory consumption
1338+
// this should be enough e.g. for all grays and alpha combos
1339+
static final int MAX_CACHED_COLORS_PER_FONT = 1 << 16;
13441340

1345-
static final FontInfo NOT_FOUND = new FontInfo();
1341+
static final FontInfo NON_NATIVE = new FontInfo();
13461342

13471343
Font font;
13481344
float ascent;
@@ -1351,42 +1347,52 @@ protected static final class FontInfo {
13511347
}
13521348

13531349

1354-
protected static final class FontCache {
1350+
static final class FontCache {
13551351

1356-
private static final int CACHE_SIZE = 512;
1352+
static final int MAX_CACHE_SIZE = 512;
13571353

1358-
protected Map<String, String> nameToFilename = new HashMap<>();
1354+
// keeps track of filenames of fonts loaded from ttf and otf files
1355+
Map<String, String> nameToFilename = new HashMap<>();
13591356

1360-
private final HashSet<String> namesNotFound = new HashSet<>();
1361-
1362-
private final LinkedHashMap<Key, FontInfo> cache = new LinkedHashMap<Key, FontInfo>(16, 0.75f, true) {
1357+
// keeps track of fonts which should be rendered as pictures
1358+
// so we don't go through native font search process every time
1359+
final HashSet<String> nonNativeNames = new HashSet<>();
13631360

1361+
// keeps all created fonts for reuse up to MAX_CACHE_SIZE limit
1362+
// when the limit is reached, the least recently used font is removed
1363+
// TODO: this should be based on memory consumtion
1364+
final LinkedHashMap<Key, FontInfo> cache =
1365+
new LinkedHashMap<Key, FontInfo>(16, 0.75f, true) {
13641366
@Override
13651367
protected boolean removeEldestEntry(Map.Entry<Key, FontInfo> eldest) {
1366-
return size() > CACHE_SIZE;
1368+
return size() > MAX_CACHE_SIZE;
13671369
}
1368-
13691370
};
1370-
private final Key retrievingKey = new Key();
1371-
private final Text measuringText = new Text();
13721371

1373-
private FontInfo get(String name, float size) {
1374-
if (namesNotFound.contains(name)) {
1375-
return FontInfo.NOT_FOUND;
1372+
// key for retrieving fonts from cache; don't use for insertion,
1373+
// every font has to have its own new Key instance
1374+
final Key retrievingKey = new Key();
1375+
1376+
// text node used for measuring sizes of text
1377+
final Text measuringText = new Text();
1378+
1379+
FontInfo get(String name, float size) {
1380+
if (nonNativeNames.contains(name)) {
1381+
return FontInfo.NON_NATIVE;
13761382
}
13771383
retrievingKey.name = name;
13781384
retrievingKey.size = size;
13791385
return cache.get(retrievingKey);
13801386
}
13811387

1382-
private void put(String name, float size, FontInfo fontInfo) {
1388+
void put(String name, float size, FontInfo fontInfo) {
13831389
Key key = new Key();
13841390
key.name = name;
13851391
key.size = size;
13861392
cache.put(key, fontInfo);
13871393
}
13881394

1389-
private FontInfo createFontInfo(Font font) {
1395+
FontInfo createFontInfo(Font font) {
13901396
FontInfo result = new FontInfo();
13911397
result.font = font;
13921398
{ // measure ascent and descent
@@ -1399,7 +1405,7 @@ private FontInfo createFontInfo(Font font) {
13991405
return result;
14001406
}
14011407

1402-
private static final class Key {
1408+
static final class Key {
14031409
String name;
14041410
float size;
14051411

@@ -1458,28 +1464,42 @@ protected void setTextFont(PFont which, float size) {
14581464
textFont = which;
14591465

14601466
String fontName = which.getName();
1467+
String fontPsName = which.getPostScriptName();
14611468

14621469
textFontInfo = fontCache.get(fontName, size);
14631470
if (textFontInfo == null) {
1464-
Font font;
1465-
String filename = fontCache.nameToFilename.get(fontName);
1466-
if (filename != null) {
1471+
Font font = null;
1472+
1473+
if (which.isStream()) {
1474+
// Load from ttf or otf file
1475+
String filename = fontCache.nameToFilename.get(fontName);
14671476
font = Font.loadFont(parent.createInput(filename), size);
1468-
} else {
1477+
}
1478+
1479+
if (font == null) {
1480+
// Look up font name
14691481
font = new Font(fontName, size);
1482+
if (!fontName.equalsIgnoreCase(font.getName())) {
1483+
// Look up font postscript name
1484+
font = new Font(fontPsName, size);
1485+
if (!fontPsName.equalsIgnoreCase(font.getName())) {
1486+
font = null; // Done with it
1487+
}
1488+
}
1489+
}
1490+
1491+
if (font == null && which.getNative() != null) {
1492+
// Ain't got nothing, but AWT has something, so glyph images are not
1493+
// going to be used for this font; go with the default font then
1494+
font = new Font(size);
14701495
}
14711496

1472-
// Loading from file is guaranteed to succeed, font was already
1473-
// successfully loaded in createFont().
1474-
// Loading system font may return fallback font if the font
1475-
// does not exist; this can be detected by comparing font names.
1476-
// Please note that some font names can differ in FX vs. AWT.
1477-
boolean isNotFound = filename == null &&
1478-
!fontName.equalsIgnoreCase(font.getName());
1479-
if (isNotFound) {
1480-
fontCache.namesNotFound.add(fontName);
1481-
textFontInfo = FontInfo.NOT_FOUND;
1497+
if (font == null) {
1498+
// Ain't got nothing, let's use glyph images for this one
1499+
textFontInfo = FontInfo.NON_NATIVE;
1500+
fontCache.nonNativeNames.add(fontName);
14821501
} else {
1502+
// Native font found, save it in the cache and use it
14831503
textFontInfo = fontCache.createFontInfo(font);
14841504
fontCache.put(fontName, size, textFontInfo);
14851505
}
@@ -1491,7 +1511,7 @@ protected void setTextFont(PFont which, float size) {
14911511

14921512
@Override
14931513
protected void textLineImpl(char[] buffer, int start, int stop, float x, float y) {
1494-
if (textFontInfo == FontInfo.NOT_FOUND) {
1514+
if (textFontInfo == FontInfo.NON_NATIVE) {
14951515
super.textLineImpl(buffer, start, stop, x, y);
14961516
} else {
14971517
context.fillText(new String(buffer, start, stop - start), x, y);
@@ -1504,7 +1524,7 @@ protected PImage getTintedGlyphImage(PFont.Glyph glyph, int tintColor) {
15041524
textFontInfo.tintCache = new LinkedHashMap<Integer, PImage[]>(16, 0.75f, true) {
15051525
@Override
15061526
protected boolean removeEldestEntry(Map.Entry<Integer, PImage[]> eldest) {
1507-
return size() > FontInfo.TINT_CACHE_SIZE;
1527+
return size() > FontInfo.MAX_CACHED_COLORS_PER_FONT;
15081528
}
15091529
};
15101530
}
@@ -1562,7 +1582,7 @@ protected float textWidthImpl(char[] buffer, int start, int stop) {
15621582
defaultFontOrDeath("textWidth");
15631583
}
15641584

1565-
if (textFontInfo == FontInfo.NOT_FOUND) {
1585+
if (textFontInfo == FontInfo.NON_NATIVE) {
15661586
return super.textWidthImpl(buffer, start, stop);
15671587
}
15681588

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL