webfontloader 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/README.md +13 -2
- data/docs/MODULES.md +15 -2
- data/lib/webfontloader.rb +1 -1
- data/src/ascender/ascender_script.js +1 -1
- data/src/core/browserinfo.js +23 -0
- data/src/core/font.js +4 -4
- data/src/core/fontruler.js +61 -0
- data/src/core/fontwatcher.js +7 -4
- data/src/core/fontwatchrunner.js +144 -92
- data/src/core/initialize.js +3 -1
- data/src/core/size.js +19 -0
- data/src/core/useragent.js +6 -6
- data/src/core/useragentparser.js +62 -59
- data/src/custom/customcss.js +21 -4
- data/src/google/googlefontapi.js +1 -1
- data/src/google/lastresortwebkitfontwatchrunner.js +26 -22
- data/src/modules.yml +3 -0
- data/src/monotype/monotype_script.js +1 -1
- data/src-test/core/fonttest.js +6 -4
- data/src-test/core/fontwatchertest.js +47 -11
- data/src-test/core/fontwatchrunnertest.js +289 -74
- data/src-test/core/useragenttest.js +65 -45
- data/src-test/custom/customcsstest.js +7 -2
- data/src-test/google/lastresortwebkitfontwatchrunnertest.js +23 -23
- data/src-test/monotype/monotype_script_test.js +5 -5
- data/webfontloader.gemspec +5 -2
- metadata +16 -13
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
v1.2.0 (January 30, 2013)
|
2
|
+
* Improved font watching for browsers with the WebKit web font fallback bug
|
3
|
+
* Improved font watching in general by comparing both width and height
|
4
|
+
* UserAgent user interface has changed with the introduction of a BrowserInfo object that contains information derived from the user agent string
|
5
|
+
* The custom module now supports loading font variations
|
6
|
+
|
1
7
|
v1.1.2 (January 21, 2013)
|
2
8
|
* Added parameter to Google module to do character based subsetting.
|
3
9
|
* Made WebKit useragent check less strict about capitalization for LG L160 phones that apparently use 'AppleWebkit' instead of 'AppleWebKit' in their useragent strings.
|
data/README.md
CHANGED
@@ -119,13 +119,24 @@ Please open [an issue][issues]. Sample pages are greatly appreciated.
|
|
119
119
|
Is there something else WebFont Loader should do? Did you find a bug and want
|
120
120
|
to fix it?
|
121
121
|
|
122
|
+
### Building
|
123
|
+
|
124
|
+
Run rake:
|
125
|
+
|
126
|
+
rake
|
127
|
+
|
122
128
|
### Testing
|
123
129
|
|
130
|
+
You can run uncompressed, debuggable code by starting the demo server in dev mode:
|
131
|
+
|
132
|
+
rake demodev
|
133
|
+
|
124
134
|
WebFont Loader has an extensive test suite that runs via
|
125
135
|
[jsTestDriver][jstestdriver]. Please add tests for any changes.
|
126
136
|
|
127
|
-
To run tests, first boot the test server.
|
128
|
-
|
137
|
+
To run tests, first boot the test server. Then open a browser and navigate to
|
138
|
+
the test server url listed by `rake test` to start listing for test executions.
|
139
|
+
You can register multiple browsers.
|
129
140
|
|
130
141
|
rake test:boot
|
131
142
|
|
data/docs/MODULES.md
CHANGED
@@ -14,8 +14,16 @@ Using Google's Font API, name the font families you'd like to load.
|
|
14
14
|
}
|
15
15
|
});
|
16
16
|
|
17
|
-
Learn more about the [Google Font API][gfontapi].
|
17
|
+
Learn more about the [Google Font API][gfontapi]. You can also supply the `text` parameter to perform character subsetting:
|
18
18
|
|
19
|
+
WebFont.load({
|
20
|
+
google: {
|
21
|
+
families: ['Droid Sans', 'Droid Serif'],
|
22
|
+
text: 'abcdedfghijklmopqrstuvwxyz!'
|
23
|
+
}
|
24
|
+
});
|
25
|
+
|
26
|
+
This functionality is only available for the Google module.
|
19
27
|
|
20
28
|
## Typekit
|
21
29
|
|
@@ -68,9 +76,13 @@ To load fonts from any external stylesheet, use the `custom` module. Here you'll
|
|
68
76
|
need to specify both the url of the stylesheet as well as the font families it
|
69
77
|
provides.
|
70
78
|
|
79
|
+
You can specify a specific font variation or set of variations to load and watch
|
80
|
+
by appending the variations separated by commas to the family name separated by
|
81
|
+
a colon. variations are specified using (FVD notation)[fvd].
|
82
|
+
|
71
83
|
WebFont.load({
|
72
84
|
custom: {
|
73
|
-
families: ['My Font', 'My Other Font'],
|
85
|
+
families: ['My Font', 'My Other Font:n4,i4,n7'],
|
74
86
|
urls: ['/fonts.css']
|
75
87
|
}
|
76
88
|
});
|
@@ -80,3 +92,4 @@ provides.
|
|
80
92
|
[tk]: http://typekit.com/
|
81
93
|
[fd]: http://fontdeck.com/
|
82
94
|
[mtiwfs]: http://webfonts.fonts.com/
|
95
|
+
[fvd]: https://github.com/typekit/fvd
|
data/lib/webfontloader.rb
CHANGED
@@ -28,7 +28,7 @@ webfont.AscenderScript.VARIATIONS = {
|
|
28
28
|
};
|
29
29
|
|
30
30
|
webfont.AscenderScript.prototype.supportUserAgent = function(userAgent, support) {
|
31
|
-
return support(userAgent.
|
31
|
+
return support(userAgent.getBrowserInfo().hasWebFontSupport());
|
32
32
|
};
|
33
33
|
|
34
34
|
webfont.AscenderScript.prototype.load = function(onReady) {
|
@@ -0,0 +1,23 @@
|
|
1
|
+
/**
|
2
|
+
* @constructor
|
3
|
+
* @param {boolean} webfontSupport
|
4
|
+
* @param {boolean} webKitFallbackBug
|
5
|
+
*/
|
6
|
+
webfont.BrowserInfo = function (webfontSupport, webKitFallbackBug) {
|
7
|
+
this.webfontSupport_ = webfontSupport;
|
8
|
+
this.webKitFallbackBug_ = webKitFallbackBug;
|
9
|
+
};
|
10
|
+
|
11
|
+
/**
|
12
|
+
* @return {boolean}
|
13
|
+
*/
|
14
|
+
webfont.BrowserInfo.prototype.hasWebFontSupport = function () {
|
15
|
+
return this.webfontSupport_;
|
16
|
+
};
|
17
|
+
|
18
|
+
/**
|
19
|
+
* @return {boolean}
|
20
|
+
*/
|
21
|
+
webfont.BrowserInfo.prototype.hasWebKitFallbackBug = function () {
|
22
|
+
return this.webKitFallbackBug_;
|
23
|
+
};
|
data/src/core/font.js
CHANGED
@@ -26,7 +26,7 @@ webfont.WebFont.prototype.load = function(configuration) {
|
|
26
26
|
var eventDispatcher = new webfont.EventDispatcher(
|
27
27
|
this.domHelper_, context.document.documentElement, configuration);
|
28
28
|
|
29
|
-
if (this.userAgent_.
|
29
|
+
if (this.userAgent_.getBrowserInfo().hasWebFontSupport()) {
|
30
30
|
this.load_(eventDispatcher, configuration);
|
31
31
|
} else {
|
32
32
|
eventDispatcher.dispatchInactive();
|
@@ -77,10 +77,10 @@ webfont.WebFont.prototype.load_ = function(eventDispatcher, configuration) {
|
|
77
77
|
|
78
78
|
this.moduleFailedLoading_ = this.moduleLoading_ = modules.length;
|
79
79
|
|
80
|
-
var fontWatcher = new webfont.FontWatcher(this.domHelper_,
|
80
|
+
var fontWatcher = new webfont.FontWatcher(this.userAgent_, this.domHelper_,
|
81
81
|
eventDispatcher, {
|
82
|
-
|
83
|
-
return elem.offsetWidth;
|
82
|
+
getSize: function(elem) {
|
83
|
+
return new webfont.Size(elem.offsetWidth, elem.offsetHeight);
|
84
84
|
}}, self.asyncCall_, function() {
|
85
85
|
return new Date().getTime();
|
86
86
|
});
|
@@ -0,0 +1,61 @@
|
|
1
|
+
/**
|
2
|
+
* An element that can be used to measure the metrics
|
3
|
+
* of a given font and string.
|
4
|
+
* @constructor
|
5
|
+
* @param {webfont.DomHelper} domHelper
|
6
|
+
* @param {Object.<string, function(Object): webfont.Size>} fontSizer
|
7
|
+
* @param {string} fontTestString
|
8
|
+
*/
|
9
|
+
webfont.FontRuler = function(domHelper, fontSizer, fontTestString) {
|
10
|
+
this.domHelper_ = domHelper;
|
11
|
+
this.fontSizer_ = fontSizer;
|
12
|
+
this.fontTestString_ = fontTestString;
|
13
|
+
this.nameHelper_ = new webfont.CssFontFamilyName();
|
14
|
+
this.fvd_ = new webfont.FontVariationDescription();
|
15
|
+
this.el_ = this.domHelper_.createElement('span', {}, this.fontTestString_);
|
16
|
+
};
|
17
|
+
|
18
|
+
/**
|
19
|
+
* @param {string} fontFamily
|
20
|
+
* @param {string=} opt_fontDescription
|
21
|
+
*/
|
22
|
+
webfont.FontRuler.prototype.setFont = function(fontFamily, opt_fontDescription) {
|
23
|
+
var styleString = this.computeStyleString_(fontFamily, opt_fontDescription);
|
24
|
+
this.domHelper_.setStyle(this.el_, styleString);
|
25
|
+
};
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Inserts the ruler into the DOM.
|
29
|
+
*/
|
30
|
+
webfont.FontRuler.prototype.insert = function() {
|
31
|
+
this.domHelper_.insertInto('body', this.el_);
|
32
|
+
};
|
33
|
+
|
34
|
+
/**
|
35
|
+
* @private
|
36
|
+
* @param {string} fontFamily
|
37
|
+
* @param {string=} opt_fontDescription
|
38
|
+
* @return {string}
|
39
|
+
*/
|
40
|
+
webfont.FontRuler.prototype.computeStyleString_ = function(fontFamily, opt_fontDescription) {
|
41
|
+
var variationCss = opt_fontDescription ? this.fvd_.expand(opt_fontDescription) : '';
|
42
|
+
var styleString = "position:absolute;top:-999px;left:-999px;" +
|
43
|
+
"font-size:300px;width:auto;height:auto;line-height:normal;margin:0;" +
|
44
|
+
"padding:0;font-variant:normal;font-family:" +
|
45
|
+
this.nameHelper_.quote(fontFamily) + ";" + variationCss;
|
46
|
+
return styleString;
|
47
|
+
};
|
48
|
+
|
49
|
+
/**
|
50
|
+
* @return {webfont.Size}
|
51
|
+
*/
|
52
|
+
webfont.FontRuler.prototype.getSize = function() {
|
53
|
+
return this.fontSizer_.getSize(this.el_);
|
54
|
+
};
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Removes the ruler element from the DOM.
|
58
|
+
*/
|
59
|
+
webfont.FontRuler.prototype.remove = function() {
|
60
|
+
this.domHelper_.removeElement(this.el_);
|
61
|
+
};
|
data/src/core/fontwatcher.js
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
/**
|
2
2
|
* @constructor
|
3
|
+
* @param {webfont.UserAgent} userAgent
|
3
4
|
* @param {webfont.DomHelper} domHelper
|
4
5
|
* @param {webfont.EventDispatcher} eventDispatcher
|
5
|
-
* @param {Object.<string, function(Object):
|
6
|
+
* @param {Object.<string, function(Object): webfont.Size>} fontSizer
|
6
7
|
* @param {function(function(), number=)} asyncCall
|
7
8
|
* @param {function(): number} getTime
|
8
9
|
*/
|
9
|
-
webfont.FontWatcher = function(domHelper, eventDispatcher, fontSizer,
|
10
|
+
webfont.FontWatcher = function(userAgent, domHelper, eventDispatcher, fontSizer,
|
10
11
|
asyncCall, getTime) {
|
11
12
|
this.domHelper_ = domHelper;
|
12
13
|
this.eventDispatcher_ = eventDispatcher;
|
@@ -16,6 +17,8 @@ webfont.FontWatcher = function(domHelper, eventDispatcher, fontSizer,
|
|
16
17
|
this.currentlyWatched_ = 0;
|
17
18
|
this.last_ = false;
|
18
19
|
this.success_ = false;
|
20
|
+
|
21
|
+
this.hasWebKitFallbackBug_ = userAgent.getBrowserInfo().hasWebKitFallbackBug();
|
19
22
|
};
|
20
23
|
|
21
24
|
/**
|
@@ -35,7 +38,7 @@ webfont.FontWatcher.DEFAULT_VARIATION = 'n4';
|
|
35
38
|
* function(string, string), webfont.DomHelper,
|
36
39
|
* Object.<string, function(Object): number>,
|
37
40
|
* function(function(), number=), function(): number, string, string,
|
38
|
-
* string=)} fontWatchRunnerCtor The font watch runner constructor.
|
41
|
+
* boolean, Object.<string,boolean>=, string=)} fontWatchRunnerCtor The font watch runner constructor.
|
39
42
|
* @param {boolean} last True if this is the last set of families to watch.
|
40
43
|
*/
|
41
44
|
webfont.FontWatcher.prototype.watch = function(fontFamilies, fontDescriptions,
|
@@ -68,7 +71,7 @@ webfont.FontWatcher.prototype.watch = function(fontFamilies, fontDescriptions,
|
|
68
71
|
var inactiveCallback = webfont.bind(this, this.fontInactive_)
|
69
72
|
var fontWatchRunner = new fontWatchRunnerCtor(activeCallback,
|
70
73
|
inactiveCallback, this.domHelper_, this.fontSizer_, this.asyncCall_,
|
71
|
-
this.getTime_, fontFamily, fontDescription, fontTestString);
|
74
|
+
this.getTime_, fontFamily, fontDescription, this.hasWebKitFallbackBug_, null, fontTestString);
|
72
75
|
|
73
76
|
fontWatchRunner.start();
|
74
77
|
}
|
data/src/core/fontwatchrunner.js
CHANGED
@@ -3,61 +3,57 @@
|
|
3
3
|
* @param {function(string, string)} activeCallback
|
4
4
|
* @param {function(string, string)} inactiveCallback
|
5
5
|
* @param {webfont.DomHelper} domHelper
|
6
|
-
* @param {Object.<string, function(Object):
|
6
|
+
* @param {Object.<string, function(Object): webfont.Size>} fontSizer
|
7
7
|
* @param {function(function(), number=)} asyncCall
|
8
8
|
* @param {function(): number} getTime
|
9
9
|
* @param {string} fontFamily
|
10
10
|
* @param {string} fontDescription
|
11
|
+
* @param {boolean} hasWebKitFallbackBug
|
12
|
+
* @param {Object.<string, boolean>=} opt_metricCompatibleFonts
|
11
13
|
* @param {string=} opt_fontTestString
|
12
14
|
*/
|
13
15
|
webfont.FontWatchRunner = function(activeCallback, inactiveCallback, domHelper,
|
14
|
-
fontSizer, asyncCall, getTime, fontFamily, fontDescription, opt_fontTestString) {
|
16
|
+
fontSizer, asyncCall, getTime, fontFamily, fontDescription, hasWebKitFallbackBug, opt_metricCompatibleFonts, opt_fontTestString) {
|
15
17
|
this.activeCallback_ = activeCallback;
|
16
18
|
this.inactiveCallback_ = inactiveCallback;
|
17
19
|
this.domHelper_ = domHelper;
|
18
20
|
this.fontSizer_ = fontSizer;
|
19
21
|
this.asyncCall_ = asyncCall;
|
20
22
|
this.getTime_ = getTime;
|
21
|
-
this.nameHelper_ = new webfont.CssFontFamilyName();
|
22
|
-
this.fvd_ = new webfont.FontVariationDescription();
|
23
23
|
this.fontFamily_ = fontFamily;
|
24
24
|
this.fontDescription_ = fontDescription;
|
25
25
|
this.fontTestString_ = opt_fontTestString || webfont.FontWatchRunner.DEFAULT_TEST_STRING;
|
26
|
-
this.
|
27
|
-
|
28
|
-
this.originalSizeB_ = this.getDefaultFontSize_(
|
29
|
-
webfont.FontWatchRunner.DEFAULT_FONTS_B);
|
30
|
-
this.lastObservedSizeA_ = this.originalSizeA_;
|
31
|
-
this.lastObservedSizeB_ = this.originalSizeB_;
|
32
|
-
this.requestedFontA_ = this.createHiddenElementWithFont_(
|
33
|
-
webfont.FontWatchRunner.DEFAULT_FONTS_A);
|
34
|
-
this.requestedFontB_ = this.createHiddenElementWithFont_(
|
35
|
-
webfont.FontWatchRunner.DEFAULT_FONTS_B);
|
36
|
-
};
|
26
|
+
this.hasWebKitFallbackBug_ = hasWebKitFallbackBug;
|
27
|
+
this.lastResortSizes_ = {};
|
37
28
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
webfont.FontWatchRunner.DEFAULT_FONTS_A = "arial,'URW Gothic L',sans-serif";
|
29
|
+
this.metricCompatibleFonts_ = opt_metricCompatibleFonts || null;
|
30
|
+
|
31
|
+
this.fontRulerA_ = new webfont.FontRuler(this.domHelper_, this.fontSizer_, this.fontTestString_);
|
32
|
+
this.fontRulerA_.insert();
|
33
|
+
this.fontRulerB_ = new webfont.FontRuler(this.domHelper_, this.fontSizer_, this.fontTestString_);
|
34
|
+
this.fontRulerB_.insert();
|
35
|
+
|
36
|
+
this.setupLastResortSizes_();
|
37
|
+
};
|
48
38
|
|
49
39
|
/**
|
50
|
-
*
|
51
|
-
* want each of these fonts to have a different width when rendering the test
|
52
|
-
* string than each of the fonts in DEFAULT_FONTS_A:
|
53
|
-
* Windows - Georgia - 98.98%
|
54
|
-
* Mac - Georgia - 95.60%
|
55
|
-
* Linux - Century Schoolbook L - 97.97%
|
56
|
-
* (Based on http://www.codestyle.org/css/font-family/sampler-CombinedResults.shtml)
|
57
|
-
* @type {string}
|
40
|
+
* @enum {string}
|
58
41
|
* @const
|
59
42
|
*/
|
60
|
-
webfont.FontWatchRunner.
|
43
|
+
webfont.FontWatchRunner.LastResortFonts = {
|
44
|
+
SERIF: 'serif',
|
45
|
+
SANS_SERIF: 'sans-serif',
|
46
|
+
MONOSPACE: 'monospace',
|
47
|
+
// Apple Color Emoji is the last character fallback on iOS. Since
|
48
|
+
// all iOS installations that support web fonts have this font it
|
49
|
+
// effectively means that Apple Color Emoji is the last resort
|
50
|
+
// font on iOS. The caveat is that it only has characters in the
|
51
|
+
// Emoji code range, and falls back to the real last resort font,
|
52
|
+
// which is the default sans-serif font. It however affects the
|
53
|
+
// height of the span we are monitoring, so we'll have to include
|
54
|
+
// it in our list of last resort fonts.
|
55
|
+
EMOJI: 'Apple Color Emoji'
|
56
|
+
};
|
61
57
|
|
62
58
|
/**
|
63
59
|
* Default test string. Characters are chosen so that their widths vary a lot
|
@@ -68,11 +64,110 @@ webfont.FontWatchRunner.DEFAULT_FONTS_B = "Georgia,'Century Schoolbook L',serif"
|
|
68
64
|
*/
|
69
65
|
webfont.FontWatchRunner.DEFAULT_TEST_STRING = 'BESbswy';
|
70
66
|
|
67
|
+
/**
|
68
|
+
* @private
|
69
|
+
*/
|
70
|
+
webfont.FontWatchRunner.prototype.setupLastResortSizes_ = function() {
|
71
|
+
var fontRuler = new webfont.FontRuler(this.domHelper_, this.fontSizer_, this.fontTestString_);
|
72
|
+
|
73
|
+
fontRuler.insert();
|
74
|
+
|
75
|
+
for (var font in webfont.FontWatchRunner.LastResortFonts) {
|
76
|
+
if (webfont.FontWatchRunner.LastResortFonts.hasOwnProperty(font)) {
|
77
|
+
fontRuler.setFont(webfont.FontWatchRunner.LastResortFonts[font], this.fontDescription_);
|
78
|
+
this.lastResortSizes_[webfont.FontWatchRunner.LastResortFonts[font]] = fontRuler.getSize();
|
79
|
+
}
|
80
|
+
}
|
81
|
+
fontRuler.remove();
|
82
|
+
};
|
83
|
+
|
71
84
|
webfont.FontWatchRunner.prototype.start = function() {
|
72
85
|
this.started_ = this.getTime_();
|
86
|
+
|
87
|
+
this.fontRulerA_.setFont(this.fontFamily_ + ',' + webfont.FontWatchRunner.LastResortFonts.SERIF, this.fontDescription_);
|
88
|
+
this.fontRulerB_.setFont(this.fontFamily_ + ',' + webfont.FontWatchRunner.LastResortFonts.SANS_SERIF, this.fontDescription_);
|
89
|
+
|
73
90
|
this.check_();
|
74
91
|
};
|
75
92
|
|
93
|
+
/**
|
94
|
+
* Returns true if the given size matches the generic font family size.
|
95
|
+
*
|
96
|
+
* @private
|
97
|
+
* @param {?webfont.Size} size
|
98
|
+
* @param {string} lastResortFont
|
99
|
+
* @return {boolean}
|
100
|
+
*/
|
101
|
+
webfont.FontWatchRunner.prototype.sizeMatches_ = function(size, lastResortFont) {
|
102
|
+
return size.equals(this.lastResortSizes_[lastResortFont]);
|
103
|
+
};
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Return true if the given sizes match any of the generic font family
|
107
|
+
* sizes.
|
108
|
+
*
|
109
|
+
* @private
|
110
|
+
* @param {?webfont.Size} a
|
111
|
+
* @param {?webfont.Size} b
|
112
|
+
* @return {boolean}
|
113
|
+
*/
|
114
|
+
webfont.FontWatchRunner.prototype.sizesMatchLastResortSizes_ = function(a, b) {
|
115
|
+
for (var font in webfont.FontWatchRunner.LastResortFonts) {
|
116
|
+
if (webfont.FontWatchRunner.LastResortFonts.hasOwnProperty(font)) {
|
117
|
+
if (this.sizeMatches_(a, webfont.FontWatchRunner.LastResortFonts[font]) &&
|
118
|
+
this.sizeMatches_(b, webfont.FontWatchRunner.LastResortFonts[font])) {
|
119
|
+
return true;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
return false;
|
124
|
+
};
|
125
|
+
|
126
|
+
/**
|
127
|
+
* @private
|
128
|
+
* Returns true if the loading has timed out.
|
129
|
+
* @return {boolean}
|
130
|
+
*/
|
131
|
+
webfont.FontWatchRunner.prototype.hasTimedOut_ = function() {
|
132
|
+
return this.getTime_() - this.started_ >= 5000;
|
133
|
+
};
|
134
|
+
|
135
|
+
/**
|
136
|
+
* Returns true if both fonts match the normal fallback fonts.
|
137
|
+
*
|
138
|
+
* @private
|
139
|
+
* @param {webfont.Size} sizeA
|
140
|
+
* @param {webfont.Size} sizeB
|
141
|
+
* @return {boolean}
|
142
|
+
*/
|
143
|
+
webfont.FontWatchRunner.prototype.isFallbackFont_ = function (sizeA, sizeB) {
|
144
|
+
return this.sizeMatches_(sizeA, webfont.FontWatchRunner.LastResortFonts.SERIF) &&
|
145
|
+
this.sizeMatches_(sizeB, webfont.FontWatchRunner.LastResortFonts.SANS_SERIF);
|
146
|
+
};
|
147
|
+
|
148
|
+
/**
|
149
|
+
* Returns true if the WebKit bug is present and both sizes match a last resort font.
|
150
|
+
*
|
151
|
+
* @private
|
152
|
+
* @param {webfont.Size} sizeA
|
153
|
+
* @param {webfont.Size} sizeB
|
154
|
+
* @return {boolean}
|
155
|
+
*/
|
156
|
+
webfont.FontWatchRunner.prototype.isLastResortFont_ = function (sizeA, sizeB) {
|
157
|
+
return this.hasWebKitFallbackBug_ && this.sizesMatchLastResortSizes_(sizeA, sizeB);
|
158
|
+
};
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Returns true if the current font is metric compatible. Also returns true
|
162
|
+
* if we do not have a list of metric compatible fonts.
|
163
|
+
*
|
164
|
+
* @private
|
165
|
+
* @return {boolean}
|
166
|
+
*/
|
167
|
+
webfont.FontWatchRunner.prototype.isMetricCompatibleFont_ = function () {
|
168
|
+
return this.metricCompatibleFonts_ === null || this.metricCompatibleFonts_.hasOwnProperty(this.fontFamily_);
|
169
|
+
};
|
170
|
+
|
76
171
|
/**
|
77
172
|
* Checks the size of the two spans against their original sizes during each
|
78
173
|
* async loop. If the size of one of the spans is different than the original
|
@@ -80,27 +175,24 @@ webfont.FontWatchRunner.prototype.start = function() {
|
|
80
175
|
* callback. If we wait more than 5 seconds and nothing has changed, we finish
|
81
176
|
* with the inactive callback.
|
82
177
|
*
|
83
|
-
* Because of an odd Webkit quirk, we wait to observe the new width twice
|
84
|
-
* in a row before finishing with the active callback. Sometimes, Webkit will
|
85
|
-
* render the spans with a changed width for one iteration even though the font
|
86
|
-
* is broken. This only happens for one async loop, so waiting for 2 consistent
|
87
|
-
* measurements allows us to work around the quirk.
|
88
|
-
*
|
89
178
|
* @private
|
90
179
|
*/
|
91
180
|
webfont.FontWatchRunner.prototype.check_ = function() {
|
92
|
-
var sizeA = this.
|
93
|
-
var sizeB = this.
|
181
|
+
var sizeA = this.fontRulerA_.getSize();
|
182
|
+
var sizeB = this.fontRulerB_.getSize();
|
94
183
|
|
95
|
-
if (
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
184
|
+
if (this.isFallbackFont_(sizeA, sizeB) || this.isLastResortFont_(sizeA, sizeB)) {
|
185
|
+
if (this.hasTimedOut_()) {
|
186
|
+
if (this.isLastResortFont_(sizeA, sizeB) && this.isMetricCompatibleFont_()) {
|
187
|
+
this.finish_(this.activeCallback_);
|
188
|
+
} else {
|
189
|
+
this.finish_(this.inactiveCallback_);
|
190
|
+
}
|
191
|
+
} else {
|
192
|
+
this.asyncCheck_();
|
193
|
+
}
|
100
194
|
} else {
|
101
|
-
this.
|
102
|
-
this.lastObservedSizeB_ = sizeB;
|
103
|
-
this.asyncCheck_();
|
195
|
+
this.finish_(this.activeCallback_);
|
104
196
|
}
|
105
197
|
};
|
106
198
|
|
@@ -120,47 +212,7 @@ webfont.FontWatchRunner.prototype.asyncCheck_ = function() {
|
|
120
212
|
* @param {function(string, string)} callback
|
121
213
|
*/
|
122
214
|
webfont.FontWatchRunner.prototype.finish_ = function(callback) {
|
123
|
-
this.
|
124
|
-
this.
|
215
|
+
this.fontRulerA_.remove();
|
216
|
+
this.fontRulerB_.remove();
|
125
217
|
callback(this.fontFamily_, this.fontDescription_);
|
126
218
|
};
|
127
|
-
|
128
|
-
/**
|
129
|
-
* @private
|
130
|
-
* @param {string} defaultFonts
|
131
|
-
*/
|
132
|
-
webfont.FontWatchRunner.prototype.getDefaultFontSize_ = function(defaultFonts) {
|
133
|
-
var defaultFont = this.createHiddenElementWithFont_(defaultFonts, true);
|
134
|
-
var size = this.fontSizer_.getWidth(defaultFont);
|
135
|
-
|
136
|
-
this.domHelper_.removeElement(defaultFont);
|
137
|
-
return size;
|
138
|
-
};
|
139
|
-
|
140
|
-
/**
|
141
|
-
* @private
|
142
|
-
* @param {string} defaultFonts
|
143
|
-
* @param {boolean=} opt_withoutFontFamily
|
144
|
-
*/
|
145
|
-
webfont.FontWatchRunner.prototype.createHiddenElementWithFont_ = function(
|
146
|
-
defaultFonts, opt_withoutFontFamily) {
|
147
|
-
var styleString = this.computeStyleString_(defaultFonts,
|
148
|
-
this.fontDescription_, opt_withoutFontFamily);
|
149
|
-
var span = this.domHelper_.createElement('span', { 'style': styleString },
|
150
|
-
this.fontTestString_);
|
151
|
-
|
152
|
-
this.domHelper_.insertInto('body', span);
|
153
|
-
return span;
|
154
|
-
};
|
155
|
-
|
156
|
-
webfont.FontWatchRunner.prototype.computeStyleString_ = function(defaultFonts,
|
157
|
-
fontDescription, opt_withoutFontFamily) {
|
158
|
-
var variationCss = this.fvd_.expand(fontDescription);
|
159
|
-
var styleString = "position:absolute;top:-999px;left:-999px;" +
|
160
|
-
"font-size:300px;width:auto;height:auto;line-height:normal;margin:0;" +
|
161
|
-
"padding:0;font-variant:normal;font-family:"
|
162
|
-
+ (opt_withoutFontFamily ? "" :
|
163
|
-
this.nameHelper_.quote(this.fontFamily_) + ",")
|
164
|
-
+ defaultFonts + ";" + variationCss;
|
165
|
-
return styleString;
|
166
|
-
};
|
data/src/core/initialize.js
CHANGED
@@ -23,4 +23,6 @@ webfont.UserAgent.prototype['getEngineVersion'] = webfont.UserAgent.prototype.ge
|
|
23
23
|
webfont.UserAgent.prototype['getPlatform'] = webfont.UserAgent.prototype.getPlatform;
|
24
24
|
webfont.UserAgent.prototype['getPlatformVersion'] = webfont.UserAgent.prototype.getPlatformVersion;
|
25
25
|
webfont.UserAgent.prototype['getDocumentMode'] = webfont.UserAgent.prototype.getDocumentMode;
|
26
|
-
webfont.UserAgent.prototype['
|
26
|
+
webfont.UserAgent.prototype['getBrowserInfo'] = webfont.UserAgent.prototype.getBrowserInfo;
|
27
|
+
webfont.BrowserInfo.prototype['hasWebFontSupport'] = webfont.BrowserInfo.prototype.hasWebFontSupport;
|
28
|
+
webfont.BrowserInfo.prototype['hasWebKitFallbackBug'] = webfont.BrowserInfo.prototype.hasWebKitFallbackBug;
|
data/src/core/size.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
/**
|
2
|
+
* @constructor
|
3
|
+
* @param {number} width
|
4
|
+
* @param {number} height
|
5
|
+
*/
|
6
|
+
webfont.Size = function (width, height) {
|
7
|
+
this.width = width;
|
8
|
+
this.height = height;
|
9
|
+
};
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Returns true if this size equals other.
|
13
|
+
*
|
14
|
+
* @param {webfont.Size} other
|
15
|
+
* @return {boolean}
|
16
|
+
*/
|
17
|
+
webfont.Size.prototype.equals = function (other) {
|
18
|
+
return !!other && this.width == other.width && this.height == other.height;
|
19
|
+
};
|
data/src/core/useragent.js
CHANGED
@@ -6,11 +6,11 @@
|
|
6
6
|
* @param {string} platform
|
7
7
|
* @param {string} platformVersion
|
8
8
|
* @param {number|undefined} documentMode
|
9
|
-
* @param {
|
9
|
+
* @param {!webfont.BrowserInfo} browserInfo
|
10
10
|
* @constructor
|
11
11
|
*/
|
12
12
|
webfont.UserAgent = function(name, version, engine, engineVersion, platform,
|
13
|
-
platformVersion, documentMode,
|
13
|
+
platformVersion, documentMode, browserInfo) {
|
14
14
|
this.name_ = name;
|
15
15
|
this.version_ = version;
|
16
16
|
this.engine_ = engine;
|
@@ -18,7 +18,7 @@ webfont.UserAgent = function(name, version, engine, engineVersion, platform,
|
|
18
18
|
this.platform_ = platform;
|
19
19
|
this.platformVersion_ = platformVersion;
|
20
20
|
this.documentMode_ = documentMode;
|
21
|
-
this.
|
21
|
+
this.browserInfo_ = browserInfo;
|
22
22
|
};
|
23
23
|
|
24
24
|
/**
|
@@ -71,8 +71,8 @@ webfont.UserAgent.prototype.getDocumentMode = function() {
|
|
71
71
|
};
|
72
72
|
|
73
73
|
/**
|
74
|
-
* @return {
|
74
|
+
* @return {webfont.BrowserInfo}
|
75
75
|
*/
|
76
|
-
webfont.UserAgent.prototype.
|
77
|
-
return this.
|
76
|
+
webfont.UserAgent.prototype.getBrowserInfo = function() {
|
77
|
+
return this.browserInfo_;
|
78
78
|
};
|