• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excaliburjs / Excalibur / 11871428976

16 Nov 2024 03:58PM UTC coverage: 90.059% (-0.04%) from 90.094%
11871428976

push

github

eonarheim
docs: Update animation + fix doc build

5860 of 7464 branches covered (78.51%)

12837 of 14254 relevant lines covered (90.06%)

25223.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.13
/src/engine/Graphics/FontCache.ts
1
import { BoundingBox } from '../Collision/BoundingBox';
2
import { Color } from '../Color';
3
import { Logger } from '../Util/Log';
4
import { Font } from './Font';
5
import { FontTextInstance } from './FontTextInstance';
6

7
export class FontCache {
8
  public static FONT_TIMEOUT = 500;
1✔
9
  private static _LOGGER = Logger.getInstance();
1✔
10
  private static _TEXT_USAGE = new Map<FontTextInstance, number>();
1✔
11
  private static _TEXT_CACHE = new Map<string, FontTextInstance>();
1✔
12
  private static _MEASURE_CACHE = new Map<string, BoundingBox>();
1✔
13

14
  static measureText(text: string, font: Font, maxWidth?: number): BoundingBox {
15
    const hash = FontTextInstance.getHashCode(font, text);
1,080✔
16
    if (FontCache._MEASURE_CACHE.has(hash)) {
1,080✔
17
      return FontCache._MEASURE_CACHE.get(hash)!;
53✔
18
    }
19
    FontCache._LOGGER.debug('Font text measurement cache miss');
1,027✔
20
    const measurement = font.measureTextWithoutCache(text, maxWidth);
1,027✔
21
    FontCache._MEASURE_CACHE.set(hash, measurement);
1,027✔
22
    return measurement;
1,027✔
23
  }
24

25
  static getTextInstance(text: string, font: Font, color: Color): FontTextInstance {
26
    const hash = FontTextInstance.getHashCode(font, text, color);
25✔
27
    let textInstance = FontCache._TEXT_CACHE.get(hash);
25✔
28
    if (!textInstance) {
25✔
29
      textInstance = new FontTextInstance(font, text, color);
20✔
30
      FontCache._TEXT_CACHE.set(hash, textInstance);
20✔
31
      FontCache._LOGGER.debug('Font text instance cache miss');
20✔
32
    }
33

34
    // Cache the bitmap for certain amount of time
35
    FontCache._TEXT_USAGE.set(textInstance, performance.now());
25✔
36

37
    return textInstance;
25✔
38
  }
39

40
  static checkAndClearCache() {
41
    const deferred: FontTextInstance[] = [];
854✔
42
    const currentHashCodes = new Set<string>();
854✔
43
    for (const [textInstance, time] of FontCache._TEXT_USAGE.entries()) {
854✔
44
      // if bitmap hasn't been used in 100 ms clear it
45
      if (time + FontCache.FONT_TIMEOUT < performance.now()) {
9!
46
        FontCache._LOGGER.debug(`Text cache entry timed out ${textInstance.text}`);
9✔
47
        deferred.push(textInstance);
9✔
48
        textInstance.dispose();
9✔
49
      } else {
50
        const hash = textInstance.getHashCode(false);
×
51
        currentHashCodes.add(hash);
×
52
      }
53
    }
54
    // Deferred removal of text instances
55
    deferred.forEach((t) => {
854✔
56
      FontCache._TEXT_USAGE.delete(t);
9✔
57
    });
58

59
    // Regenerate text instance cache
60
    this._TEXT_CACHE.clear();
854✔
61
    for (const [textInstance] of this._TEXT_USAGE.entries()) {
854✔
62
      this._TEXT_CACHE.set(textInstance.getHashCode(), textInstance);
×
63
    }
64

65
    // Regenerated measurement cache
66
    const newTextMeasurementCache = new Map<string, BoundingBox>();
854✔
67
    for (const current of currentHashCodes) {
854✔
68
      if (FontCache._MEASURE_CACHE.has(current)) {
×
69
        newTextMeasurementCache.set(current, FontCache._MEASURE_CACHE.get(current)!);
×
70
      }
71
    }
72
    this._MEASURE_CACHE.clear();
854✔
73
    this._MEASURE_CACHE = newTextMeasurementCache;
854✔
74
  }
75

76
  public static get cacheSize() {
77
    return FontCache._TEXT_USAGE.size;
7✔
78
  }
79

80
  /**
81
   * Force clear all cached text bitmaps
82
   */
83
  public static clearCache() {
84
    for (const [textInstance] of FontCache._TEXT_USAGE.entries()) {
5✔
85
      textInstance.dispose();
11✔
86
    }
87
    FontCache._TEXT_USAGE.clear();
5✔
88
    FontCache._TEXT_CACHE.clear();
5✔
89
    FontCache._MEASURE_CACHE.clear();
5✔
90
  }
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc