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

excaliburjs / Excalibur / 14804036802

02 May 2025 09:58PM UTC coverage: 5.927% (-83.4%) from 89.28%
14804036802

Pull #3404

github

web-flow
Merge 5c103d7f8 into 0f2ccaeb2
Pull Request #3404: feat: added Graph module to Math

234 of 8383 branches covered (2.79%)

229 of 246 new or added lines in 1 file covered. (93.09%)

13145 existing lines in 208 files now uncovered.

934 of 15759 relevant lines covered (5.93%)

4.72 hits per line

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

10.87
/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 {
UNCOV
15
    const hash = FontTextInstance.getHashCode(font, text);
×
UNCOV
16
    if (FontCache._MEASURE_CACHE.has(hash)) {
×
UNCOV
17
      return FontCache._MEASURE_CACHE.get(hash)!;
×
18
    }
UNCOV
19
    FontCache._LOGGER.debug('Font text measurement cache miss');
×
UNCOV
20
    const measurement = font.measureTextWithoutCache(text, maxWidth);
×
UNCOV
21
    FontCache._MEASURE_CACHE.set(hash, measurement);
×
UNCOV
22
    return measurement;
×
23
  }
24

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

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

UNCOV
37
    return textInstance;
×
38
  }
39

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

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

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

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

80
  /**
81
   * Force clear all cached text bitmaps
82
   */
83
  public static clearCache() {
UNCOV
84
    for (const [textInstance] of FontCache._TEXT_USAGE.entries()) {
×
UNCOV
85
      textInstance.dispose();
×
86
    }
UNCOV
87
    FontCache._TEXT_USAGE.clear();
×
UNCOV
88
    FontCache._TEXT_CACHE.clear();
×
UNCOV
89
    FontCache._MEASURE_CACHE.clear();
×
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

© 2026 Coveralls, Inc