| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4561994 commit 86891c8
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1289,13 +1289,7 @@ protected PFont createFont(String name, float size, | |||
| 1289 | 1289 | boolean smooth, char[] charset) { | |
| 1290 | 1290 | PFont font = super.createFont(name, size, smooth, charset); | |
| 1291 | 1291 | 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); | ||
| 1299 | 1293 | } | |
| 1300 | 1294 | return font; | |
| 1301 | 1295 | } | |
@@ -1319,7 +1313,7 @@ public float textAscent() { | |||
| 1319 | 1313 | if (textFont == null) { | |
| 1320 | 1314 | defaultFontOrDeath("textAscent"); | |
| 1321 | 1315 | } | |
| 1322 | - if (textFontInfo == FontInfo.NOT_FOUND) { | ||
| 1316 | + if (textFontInfo == FontInfo.NON_NATIVE) { | ||
| 1323 | 1317 | return super.textAscent(); | |
| 1324 | 1318 | } | |
| 1325 | 1319 | return textFontInfo.ascent; | |
@@ -1331,18 +1325,20 @@ public float textDescent() { | |||
| 1331 | 1325 | if (textFont == null) { | |
| 1332 | 1326 | defaultFontOrDeath("textDescent"); | |
| 1333 | 1327 | } | |
| 1334 | - if (textFontInfo == FontInfo.NOT_FOUND) { | ||
| 1328 | + if (textFontInfo == FontInfo.NON_NATIVE) { | ||
| 1335 | 1329 | return super.textDescent(); | |
| 1336 | 1330 | } | |
| 1337 | 1331 | return textFontInfo.descent; | |
| 1338 | 1332 | } | |
| 1339 | 1333 | ||
| 1340 | 1334 | ||
| 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; | ||
| 1344 | 1340 | ||
| 1345 | - static final FontInfo NOT_FOUND = new FontInfo(); | ||
| 1341 | + static final FontInfo NON_NATIVE = new FontInfo(); | ||
| 1346 | 1342 | ||
| 1347 | 1343 | Font font; | |
| 1348 | 1344 | float ascent; | |
@@ -1351,42 +1347,52 @@ protected static final class FontInfo { | |||
| 1351 | 1347 | } | |
| 1352 | 1348 | ||
| 1353 | 1349 | ||
| 1354 | - protected static final class FontCache { | ||
| 1350 | + static final class FontCache { | ||
| 1355 | 1351 | ||
| 1356 | - private static final int CACHE_SIZE = 512; | ||
| 1352 | + static final int MAX_CACHE_SIZE = 512; | ||
| 1357 | 1353 | ||
| 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<>(); | ||
| 1359 | 1356 | ||
| 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<>(); | ||
| 1363 | 1360 | ||
| 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) { | ||
| 1364 | 1366 | @Override | |
| 1365 | 1367 | protected boolean removeEldestEntry(Map.Entry<Key, FontInfo> eldest) { | |
| 1366 | - return size() > CACHE_SIZE; | ||
| 1368 | + return size() > MAX_CACHE_SIZE; | ||
| 1367 | 1369 | } | |
| 1368 | - | ||
| 1369 | 1370 | }; | |
| 1370 | - private final Key retrievingKey = new Key(); | ||
| 1371 | - private final Text measuringText = new Text(); | ||
| 1372 | 1371 | ||
| 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; | ||
| 1376 | 1382 | } | |
| 1377 | 1383 | retrievingKey.name = name; | |
| 1378 | 1384 | retrievingKey.size = size; | |
| 1379 | 1385 | return cache.get(retrievingKey); | |
| 1380 | 1386 | } | |
| 1381 | 1387 | ||
| 1382 | - private void put(String name, float size, FontInfo fontInfo) { | ||
| 1388 | + void put(String name, float size, FontInfo fontInfo) { | ||
| 1383 | 1389 | Key key = new Key(); | |
| 1384 | 1390 | key.name = name; | |
| 1385 | 1391 | key.size = size; | |
| 1386 | 1392 | cache.put(key, fontInfo); | |
| 1387 | 1393 | } | |
| 1388 | 1394 | ||
| 1389 | - private FontInfo createFontInfo(Font font) { | ||
| 1395 | + FontInfo createFontInfo(Font font) { | ||
| 1390 | 1396 | FontInfo result = new FontInfo(); | |
| 1391 | 1397 | result.font = font; | |
| 1392 | 1398 | { // measure ascent and descent | |
@@ -1399,7 +1405,7 @@ private FontInfo createFontInfo(Font font) { | |||
| 1399 | 1405 | return result; | |
| 1400 | 1406 | } | |
| 1401 | 1407 | ||
| 1402 | - private static final class Key { | ||
| 1408 | + static final class Key { | ||
| 1403 | 1409 | String name; | |
| 1404 | 1410 | float size; | |
| 1405 | 1411 | ||
@@ -1458,28 +1464,42 @@ protected void setTextFont(PFont which, float size) { | |||
| 1458 | 1464 | textFont = which; | |
| 1459 | 1465 | ||
| 1460 | 1466 | String fontName = which.getName(); | |
| 1467 | + String fontPsName = which.getPostScriptName(); | ||
| 1461 | 1468 | ||
| 1462 | 1469 | textFontInfo = fontCache.get(fontName, size); | |
| 1463 | 1470 | 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); | ||
| 1467 | 1476 | font = Font.loadFont(parent.createInput(filename), size); | |
| 1468 | - } else { | ||
| 1477 | + } | ||
| 1478 | + | ||
| 1479 | + if (font == null) { | ||
| 1480 | + // Look up font name | ||
| 1469 | 1481 | 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); | ||
| 1470 | 1495 | } | |
| 1471 | 1496 | ||
| 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); | ||
| 1482 | 1501 | } else { | |
| 1502 | + // Native font found, save it in the cache and use it | ||
| 1483 | 1503 | textFontInfo = fontCache.createFontInfo(font); | |
| 1484 | 1504 | fontCache.put(fontName, size, textFontInfo); | |
| 1485 | 1505 | } | |
@@ -1491,7 +1511,7 @@ protected void setTextFont(PFont which, float size) { | |||
| 1491 | 1511 | ||
| 1492 | 1512 | @Override | |
| 1493 | 1513 | 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) { | ||
| 1495 | 1515 | super.textLineImpl(buffer, start, stop, x, y); | |
| 1496 | 1516 | } else { | |
| 1497 | 1517 | context.fillText(new String(buffer, start, stop - start), x, y); | |
@@ -1504,7 +1524,7 @@ protected PImage getTintedGlyphImage(PFont.Glyph glyph, int tintColor) { | |||
| 1504 | 1524 | textFontInfo.tintCache = new LinkedHashMap<Integer, PImage[]>(16, 0.75f, true) { | |
| 1505 | 1525 | @Override | |
| 1506 | 1526 | protected boolean removeEldestEntry(Map.Entry<Integer, PImage[]> eldest) { | |
| 1507 | - return size() > FontInfo.TINT_CACHE_SIZE; | ||
| 1527 | + return size() > FontInfo.MAX_CACHED_COLORS_PER_FONT; | ||
| 1508 | 1528 | } | |
| 1509 | 1529 | }; | |
| 1510 | 1530 | } | |
@@ -1562,7 +1582,7 @@ protected float textWidthImpl(char[] buffer, int start, int stop) { | |||
| 1562 | 1582 | defaultFontOrDeath("textWidth"); | |
| 1563 | 1583 | } | |
| 1564 | 1584 | ||
| 1565 | - if (textFontInfo == FontInfo.NOT_FOUND) { | ||
| 1585 | + if (textFontInfo == FontInfo.NON_NATIVE) { | ||
| 1566 | 1586 | return super.textWidthImpl(buffer, start, stop); | |
| 1567 | 1587 | } | |
| 1568 | 1588 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments