/
fontwatcher.js
100 lines (89 loc) · 3.05 KB
/
fontwatcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @constructor
* @param {webfont.DomHelper} domHelper
* @param {webfont.EventDispatcher} eventDispatcher
* @param {Object.<string, function(Object): number>} fontSizer
* @param {function(function(), number=)} asyncCall
* @param {function(): number} getTime
*/
webfont.FontWatcher = function(domHelper, eventDispatcher, fontSizer,
asyncCall, getTime) {
this.domHelper_ = domHelper;
this.eventDispatcher_ = eventDispatcher;
this.fontSizer_ = fontSizer;
this.asyncCall_ = asyncCall;
this.getTime_ = getTime;
this.currentlyWatched_ = 0;
this.last_ = false;
this.success_ = false;
};
/**
* @type {string}
* @const
*/
webfont.FontWatcher.DEFAULT_VARIATION = 'n4';
/**
* Watches a set of font families.
* @param {Array.<string>} fontFamilies The font family names to watch.
* @param {Object.<string, Array.<string>>} fontDescriptions The font variations
* of each family to watch. Described with FVD.
* @param {Object.<string, string>} fontTestStrings The font test strings for
* each family.
* @param {boolean} last True if this is the last set of families to watch.
*/
webfont.FontWatcher.prototype.watch = function(fontFamilies, fontDescriptions,
fontTestStrings, last) {
var length = fontFamilies.length;
for (var i = 0; i < length; i++) {
var fontFamily = fontFamilies[i];
if (!fontDescriptions[fontFamily]) {
fontDescriptions[fontFamily] = [webfont.FontWatcher.DEFAULT_VARIATION];
}
this.currentlyWatched_ += fontDescriptions[fontFamily].length;
}
if (last) {
this.last_ = last;
}
for (var i = 0; i < length; i++) {
var fontFamily = fontFamilies[i];
var descriptions = fontDescriptions[fontFamily];
var fontTestString = fontTestStrings[fontFamily];
for (var j = 0, len = descriptions.length; j < len; j++) {
var fontDescription = descriptions[j];
this.eventDispatcher_.dispatchFontLoading(fontFamily, fontDescription);
new webfont.FontWatchRunner(this, this.domHelper_, this.fontSizer_,
this.asyncCall_, this.getTime_, fontFamily, fontDescription, fontTestString);
}
}
};
/**
* Called by a FontWatchRunner when a font has been detected as active.
* @param {string} fontFamily
* @param {string} fontDescription
*/
webfont.FontWatcher.prototype.fontActive = function(fontFamily, fontDescription) {
this.eventDispatcher_.dispatchFontActive(fontFamily, fontDescription);
this.success_ = true;
this.decreaseCurrentlyWatched_();
};
/**
* Called by a FontWatchRunner when a font has been detected as inactive.
* @param {string} fontFamily
* @param {string} fontDescription
*/
webfont.FontWatcher.prototype.fontInactive = function(fontFamily, fontDescription) {
this.eventDispatcher_.dispatchFontInactive(fontFamily, fontDescription);
this.decreaseCurrentlyWatched_();
};
/**
* @private
*/
webfont.FontWatcher.prototype.decreaseCurrentlyWatched_ = function() {
if (--this.currentlyWatched_ == 0 && this.last_) {
if (this.success_) {
this.eventDispatcher_.dispatchActive();
} else {
this.eventDispatcher_.dispatchInactive();
}
}
};