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

excaliburjs / Excalibur / 11647275945

03 Nov 2024 01:14AM UTC coverage: 90.198% (-0.2%) from 90.374%
11647275945

push

github

eonarheim
docs: fix version header

5861 of 7457 branches covered (78.6%)

12837 of 14232 relevant lines covered (90.2%)

25251.21 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)!;
51✔
18
    }
19
    FontCache._LOGGER.debug('Font text measurement cache miss');
1,029✔
20
    const measurement = font.measureTextWithoutCache(text, maxWidth);
1,029✔
21
    FontCache._MEASURE_CACHE.set(hash, measurement);
1,029✔
22
    return measurement;
1,029✔
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);
21✔
30
      FontCache._TEXT_CACHE.set(hash, textInstance);
21✔
31
      FontCache._LOGGER.debug('Font text instance cache miss');
21✔
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[] = [];
853✔
42
    const currentHashCodes = new Set<string>();
853✔
43
    for (const [textInstance, time] of FontCache._TEXT_USAGE.entries()) {
853✔
44
      // if bitmap hasn't been used in 100 ms clear it
45
      if (time + FontCache.FONT_TIMEOUT < performance.now()) {
4!
46
        FontCache._LOGGER.debug(`Text cache entry timed out ${textInstance.text}`);
4✔
47
        deferred.push(textInstance);
4✔
48
        textInstance.dispose();
4✔
49
      } else {
50
        const hash = textInstance.getHashCode(false);
×
51
        currentHashCodes.add(hash);
×
52
      }
53
    }
54
    // Deferred removal of text instances
55
    deferred.forEach((t) => {
853✔
56
      FontCache._TEXT_USAGE.delete(t);
4✔
57
    });
58

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

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