webfontloader 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/CHANGELOG +5 -0
  2. data/Rakefile +2 -1
  3. data/lib/webfontloader.rb +1 -1
  4. data/spec/core/font_spec.js +9 -4
  5. data/spec/core/fontruler_spec.js +3 -10
  6. data/spec/core/fontwatcher_spec.js +22 -23
  7. data/spec/core/fontwatchrunner_spec.js +206 -231
  8. data/spec/deps.js +27 -0
  9. data/spec/google/lastresortwebkitfontwatchrunner_spec.js +47 -58
  10. data/spec/index.html +14 -25
  11. data/src/ascender/ascender_script.js +52 -45
  12. data/src/core/browserinfo.js +54 -47
  13. data/src/core/cssclassname.js +27 -22
  14. data/src/core/cssfontfamilyname.js +23 -17
  15. data/src/core/domhelper.js +209 -203
  16. data/src/core/eventdispatcher.js +111 -103
  17. data/src/core/font.js +110 -68
  18. data/src/core/fontmoduleloader.js +56 -13
  19. data/src/core/fontruler.js +52 -43
  20. data/src/core/fontvariationdescription.js +82 -76
  21. data/src/core/fontwatcher.js +93 -88
  22. data/src/core/fontwatchrunner.js +161 -161
  23. data/src/core/initialize.js +22 -15
  24. data/src/core/namespace.js +0 -29
  25. data/src/core/size.js +31 -25
  26. data/src/core/useragent.js +63 -48
  27. data/src/core/useragentparser.js +317 -306
  28. data/src/custom/customcss.js +31 -24
  29. data/src/fontdeck/fontdeck_script.js +46 -37
  30. data/src/google/fontapiparser.js +105 -97
  31. data/src/google/fontapiurlbuilder.js +46 -41
  32. data/src/google/googlefontapi.js +48 -32
  33. data/src/google/lastresortwebkitfontwatchrunner.js +80 -67
  34. data/src/modules.yml +6 -6
  35. data/src/monotype/monotype_script.js +47 -40
  36. data/src/typekit/typekit_script.js +41 -35
  37. data/tools/compiler/base.js +1548 -0
  38. data/tools/compiler/compiler.jar +0 -0
  39. data/webfontloader.gemspec +4 -2
  40. metadata +18 -16
@@ -1,3 +1,5 @@
1
+ goog.provide('webfont.CustomCss');
2
+
1
3
  /**
2
4
  *
3
5
  * WebFont.load({
@@ -7,6 +9,7 @@
7
9
  * });
8
10
  *
9
11
  * @constructor
12
+ * @implements {webfont.FontModule}
10
13
  */
11
14
  webfont.CustomCss = function(domHelper, configuration) {
12
15
  this.domHelper_ = domHelper;
@@ -15,38 +18,42 @@ webfont.CustomCss = function(domHelper, configuration) {
15
18
 
16
19
  webfont.CustomCss.NAME = 'custom';
17
20
 
18
- webfont.CustomCss.prototype.load = function(onReady) {
19
- var i, len;
20
- var urls = this.configuration_['urls'] || [];
21
- var familiesConfiguration = this.configuration_['families'] || [];
21
+ goog.scope(function () {
22
+ var CustomCss = webfont.CustomCss;
23
+
24
+ CustomCss.prototype.load = function(onReady) {
25
+ var i, len;
26
+ var urls = this.configuration_['urls'] || [];
27
+ var familiesConfiguration = this.configuration_['families'] || [];
22
28
 
23
- for (i = 0, len = urls.length; i < len; i++) {
24
- var url = urls[i];
29
+ for (i = 0, len = urls.length; i < len; i++) {
30
+ var url = urls[i];
25
31
 
26
- this.domHelper_.insertInto('head', this.domHelper_.createCssLink(url));
27
- }
32
+ this.domHelper_.insertInto('head', this.domHelper_.createCssLink(url));
33
+ }
28
34
 
29
- var families = [];
30
- var variations = {};
31
- for (i = 0, len = familiesConfiguration.length; i < len; i++) {
32
- var components = familiesConfiguration[i].split(":");
33
- var family = components[0];
34
- var familyVariations = components[1];
35
+ var families = [];
36
+ var variations = {};
37
+ for (i = 0, len = familiesConfiguration.length; i < len; i++) {
38
+ var components = familiesConfiguration[i].split(":");
39
+ var family = components[0];
40
+ var familyVariations = components[1];
35
41
 
36
- families.push(family);
42
+ families.push(family);
37
43
 
38
- if (familyVariations) {
39
- var newVariations = familyVariations.split(",");
40
- variations[family] = (variations[family] || []).concat(newVariations);
44
+ if (familyVariations) {
45
+ var newVariations = familyVariations.split(",");
46
+ variations[family] = (variations[family] || []).concat(newVariations);
47
+ }
41
48
  }
42
- }
43
49
 
44
- onReady(families, variations);
45
- };
50
+ onReady(families, variations);
51
+ };
46
52
 
47
- webfont.CustomCss.prototype.supportUserAgent = function(userAgent, support) {
48
- return support(userAgent.getBrowserInfo().hasWebFontSupport());
49
- };
53
+ CustomCss.prototype.supportUserAgent = function(userAgent, support) {
54
+ return support(userAgent.getBrowserInfo().hasWebFontSupport());
55
+ };
56
+ });
50
57
 
51
58
  globalNamespaceObject.addModule(webfont.CustomCss.NAME, function(configuration, domHelper) {
52
59
  return new webfont.CustomCss(domHelper, configuration);
@@ -1,5 +1,10 @@
1
+ goog.provide('webfont.FontdeckScript');
2
+
3
+ goog.require('webfont.FontVariationDescription');
4
+
1
5
  /**
2
6
  * @constructor
7
+ * @implements {webfont.FontModule}
3
8
  */
4
9
  webfont.FontdeckScript = function(domHelper, configuration) {
5
10
  this.domHelper_ = domHelper;
@@ -13,50 +18,54 @@ webfont.FontdeckScript.NAME = 'fontdeck';
13
18
  webfont.FontdeckScript.HOOK = '__webfontfontdeckmodule__';
14
19
  webfont.FontdeckScript.API = '//f.fontdeck.com/s/css/js/';
15
20
 
16
- webfont.FontdeckScript.prototype.getScriptSrc = function(projectId) {
17
- var protocol = this.domHelper_.getProtocol();
18
- // For empty iframes, fall back to main window's hostname.
19
- var hostname = this.domHelper_.getLoadWindow().location.hostname ||
20
- this.domHelper_.getMainWindow().location.hostname;
21
- var api = this.configuration_['api'] || webfont.FontdeckScript.API;
22
- return protocol + api + hostname + '/' + projectId + '.js';
23
- };
21
+ goog.scope(function () {
22
+ var FontdeckScript = webfont.FontdeckScript;
24
23
 
25
- webfont.FontdeckScript.prototype.supportUserAgent = function(userAgent, support) {
26
- var projectId = this.configuration_['id'];
27
- var loadWindow = this.domHelper_.getLoadWindow();
28
- var self = this;
24
+ FontdeckScript.prototype.getScriptSrc = function(projectId) {
25
+ var protocol = this.domHelper_.getProtocol();
26
+ // For empty iframes, fall back to main window's hostname.
27
+ var hostname = this.domHelper_.getLoadWindow().location.hostname ||
28
+ this.domHelper_.getMainWindow().location.hostname;
29
+ var api = this.configuration_['api'] || webfont.FontdeckScript.API;
30
+ return protocol + api + hostname + '/' + projectId + '.js';
31
+ };
29
32
 
30
- if (projectId) {
31
- // Provide data to Fontdeck for processing.
32
- if (!loadWindow[webfont.FontdeckScript.HOOK]) {
33
- loadWindow[webfont.FontdeckScript.HOOK] = {};
34
- }
33
+ FontdeckScript.prototype.supportUserAgent = function(userAgent, support) {
34
+ var projectId = this.configuration_['id'];
35
+ var loadWindow = this.domHelper_.getLoadWindow();
36
+ var self = this;
35
37
 
36
- // Fontdeck will call this function to indicate support status
37
- // and what fonts are provided.
38
- loadWindow[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) {
39
- for (var i = 0, j = data['fonts'].length; i<j; ++i) {
40
- var font = data['fonts'][i];
41
- // Add the FVDs
42
- self.fontFamilies_.push(font['name']);
43
- self.fontVariations_[font['name']] = [self.fvd_.compact("font-weight:" + font['weight'] + ";font-style:" + font['style'])];
38
+ if (projectId) {
39
+ // Provide data to Fontdeck for processing.
40
+ if (!loadWindow[webfont.FontdeckScript.HOOK]) {
41
+ loadWindow[webfont.FontdeckScript.HOOK] = {};
44
42
  }
45
- support(fontdeckSupports);
46
- };
47
43
 
48
- // Call the Fontdeck API.
49
- var script = this.domHelper_.createScriptSrc(this.getScriptSrc(projectId));
50
- this.domHelper_.insertInto('head', script);
44
+ // Fontdeck will call this function to indicate support status
45
+ // and what fonts are provided.
46
+ loadWindow[webfont.FontdeckScript.HOOK][projectId] = function(fontdeckSupports, data) {
47
+ for (var i = 0, j = data['fonts'].length; i<j; ++i) {
48
+ var font = data['fonts'][i];
49
+ // Add the FVDs
50
+ self.fontFamilies_.push(font['name']);
51
+ self.fontVariations_[font['name']] = [self.fvd_.compact("font-weight:" + font['weight'] + ";font-style:" + font['style'])];
52
+ }
53
+ support(fontdeckSupports);
54
+ };
51
55
 
52
- } else {
53
- support(true);
54
- }
55
- };
56
+ // Call the Fontdeck API.
57
+ var script = this.domHelper_.createScriptSrc(this.getScriptSrc(projectId));
58
+ this.domHelper_.insertInto('head', script);
56
59
 
57
- webfont.FontdeckScript.prototype.load = function(onReady) {
58
- onReady(this.fontFamilies_, this.fontVariations_);
59
- };
60
+ } else {
61
+ support(true);
62
+ }
63
+ };
64
+
65
+ FontdeckScript.prototype.load = function(onReady) {
66
+ onReady(this.fontFamilies_, this.fontVariations_);
67
+ };
68
+ });
60
69
 
61
70
  globalNamespaceObject.addModule(webfont.FontdeckScript.NAME, function(configuration, domHelper) {
62
71
  return new webfont.FontdeckScript(domHelper, configuration);
@@ -1,3 +1,7 @@
1
+ goog.provide('webfont.FontApiParser');
2
+
3
+ goog.require('webfont.FontVariationDescription');
4
+
1
5
  /**
2
6
  * @constructor
3
7
  */
@@ -56,122 +60,126 @@ webfont.FontApiParser.VARIATION_MATCH =
56
60
  "(?:(?:semi|demi|extra|ultra)-?)?bold|black|heavy|l|r|b|[1-9]00)?(n|i" +
57
61
  "|normal|italic)?$");
58
62
 
59
- webfont.FontApiParser.prototype.parse = function() {
60
- var length = this.fontFamilies_.length;
61
-
62
- for (var i = 0; i < length; i++) {
63
- var elements = this.fontFamilies_[i].split(":");
64
- var fontFamily = elements[0].replace(/\+/g, " ");
65
- var variations = ['n4'];
66
-
67
- if (elements.length >= 2) {
68
- var fvds = this.parseVariations_(elements[1]);
69
-
70
- if (fvds.length > 0) {
71
- variations = fvds;
72
- }
73
- if (elements.length == 3) {
74
- var subsets = this.parseSubsets_(elements[2]);
75
- if (subsets.length > 0) {
76
- var fontTestString = webfont.FontApiParser.INT_FONTS[subsets[0]];
77
-
78
- if (fontTestString) {
79
- this.fontTestStrings_[fontFamily] = fontTestString;
80
- }
81
- }
63
+ goog.scope(function () {
64
+ var FontApiParser = webfont.FontApiParser;
65
+
66
+ FontApiParser.prototype.parse = function() {
67
+ var length = this.fontFamilies_.length;
68
+
69
+ for (var i = 0; i < length; i++) {
70
+ var elements = this.fontFamilies_[i].split(":");
71
+ var fontFamily = elements[0].replace(/\+/g, " ");
72
+ var variations = ['n4'];
73
+
74
+ if (elements.length >= 2) {
75
+ var fvds = this.parseVariations_(elements[1]);
76
+
77
+ if (fvds.length > 0) {
78
+ variations = fvds;
79
+ }
80
+ if (elements.length == 3) {
81
+ var subsets = this.parseSubsets_(elements[2]);
82
+ if (subsets.length > 0) {
83
+ var fontTestString = FontApiParser.INT_FONTS[subsets[0]];
84
+
85
+ if (fontTestString) {
86
+ this.fontTestStrings_[fontFamily] = fontTestString;
87
+ }
88
+ }
89
+ }
82
90
  }
83
- }
84
91
 
85
- // For backward compatibility
86
- if (!this.fontTestStrings_[fontFamily]) {
87
- var hanumanTestString = webfont.FontApiParser.INT_FONTS[fontFamily];
88
- if (hanumanTestString) {
89
- this.fontTestStrings_[fontFamily] = hanumanTestString;
92
+ // For backward compatibility
93
+ if (!this.fontTestStrings_[fontFamily]) {
94
+ var hanumanTestString = FontApiParser.INT_FONTS[fontFamily];
95
+ if (hanumanTestString) {
96
+ this.fontTestStrings_[fontFamily] = hanumanTestString;
97
+ }
90
98
  }
99
+ this.parsedFontFamilies_.push(fontFamily);
100
+ this.variations_[fontFamily] = variations;
91
101
  }
92
- this.parsedFontFamilies_.push(fontFamily);
93
- this.variations_[fontFamily] = variations;
94
- }
95
- };
102
+ };
96
103
 
97
- webfont.FontApiParser.prototype.generateFontVariationDescription_ = function(variation) {
98
- if (!variation.match(/^[\w]+$/)) {
99
- return '';
100
- }
101
- var normalizedVariation = variation.toLowerCase();
102
- var groups = webfont.FontApiParser.VARIATION_MATCH.exec(normalizedVariation);
103
- if (groups == null) {
104
- return '';
105
- }
106
- var styleMatch = this.normalizeStyle_(groups[2]);
107
- var weightMatch = this.normalizeWeight_(groups[1]);
108
- var css = this.fvd_.expand([styleMatch, weightMatch].join(''));
109
- return css ? this.fvd_.compact(css) : null;
110
- };
104
+ FontApiParser.prototype.generateFontVariationDescription_ = function(variation) {
105
+ if (!variation.match(/^[\w]+$/)) {
106
+ return '';
107
+ }
108
+ var normalizedVariation = variation.toLowerCase();
109
+ var groups = FontApiParser.VARIATION_MATCH.exec(normalizedVariation);
110
+ if (groups == null) {
111
+ return '';
112
+ }
113
+ var styleMatch = this.normalizeStyle_(groups[2]);
114
+ var weightMatch = this.normalizeWeight_(groups[1]);
115
+ var css = this.fvd_.expand([styleMatch, weightMatch].join(''));
116
+ return css ? this.fvd_.compact(css) : null;
117
+ };
111
118
 
112
119
 
113
- webfont.FontApiParser.prototype.normalizeStyle_ = function(parsedStyle) {
114
- if (parsedStyle == null) {
115
- return 'n';
116
- }
117
- return webfont.FontApiParser.STYLES[parsedStyle];
118
- };
120
+ FontApiParser.prototype.normalizeStyle_ = function(parsedStyle) {
121
+ if (parsedStyle == null || parsedStyle == '') {
122
+ return 'n';
123
+ }
124
+ return FontApiParser.STYLES[parsedStyle];
125
+ };
119
126
 
120
127
 
121
- webfont.FontApiParser.prototype.normalizeWeight_ = function(parsedWeight) {
122
- if (parsedWeight == null) {
123
- return '4';
124
- }
125
- var weight = webfont.FontApiParser.WEIGHTS[parsedWeight];
126
- if (weight) {
127
- return weight;
128
- }
129
- if (isNaN(parsedWeight)) {
130
- return '4';
131
- }
132
- return parsedWeight.substr(0, 1);
133
- };
128
+ FontApiParser.prototype.normalizeWeight_ = function(parsedWeight) {
129
+ if (parsedWeight == null || parsedWeight == '') {
130
+ return '4';
131
+ }
132
+ var weight = FontApiParser.WEIGHTS[parsedWeight];
133
+ if (weight) {
134
+ return weight;
135
+ }
136
+ if (isNaN(parsedWeight)) {
137
+ return '4';
138
+ }
139
+ return parsedWeight.substr(0, 1);
140
+ };
134
141
 
135
142
 
136
- webfont.FontApiParser.prototype.parseVariations_ = function(variations) {
137
- var finalVariations = [];
143
+ FontApiParser.prototype.parseVariations_ = function(variations) {
144
+ var finalVariations = [];
138
145
 
139
- if (!variations) {
140
- return finalVariations;
141
- }
142
- var providedVariations = variations.split(",");
143
- var length = providedVariations.length;
146
+ if (!variations) {
147
+ return finalVariations;
148
+ }
149
+ var providedVariations = variations.split(",");
150
+ var length = providedVariations.length;
144
151
 
145
- for (var i = 0; i < length; i++) {
146
- var variation = providedVariations[i];
147
- var fvd = this.generateFontVariationDescription_(variation);
152
+ for (var i = 0; i < length; i++) {
153
+ var variation = providedVariations[i];
154
+ var fvd = this.generateFontVariationDescription_(variation);
148
155
 
149
- if (fvd) {
150
- finalVariations.push(fvd);
156
+ if (fvd) {
157
+ finalVariations.push(fvd);
158
+ }
151
159
  }
152
- }
153
- return finalVariations;
154
- };
160
+ return finalVariations;
161
+ };
155
162
 
156
163
 
157
- webfont.FontApiParser.prototype.parseSubsets_ = function(subsets) {
158
- var finalSubsets = [];
164
+ FontApiParser.prototype.parseSubsets_ = function(subsets) {
165
+ var finalSubsets = [];
159
166
 
160
- if (!subsets) {
161
- return finalSubsets;
162
- }
163
- return subsets.split(",");
164
- };
167
+ if (!subsets) {
168
+ return finalSubsets;
169
+ }
170
+ return subsets.split(",");
171
+ };
165
172
 
166
173
 
167
- webfont.FontApiParser.prototype.getFontFamilies = function() {
168
- return this.parsedFontFamilies_;
169
- };
174
+ FontApiParser.prototype.getFontFamilies = function() {
175
+ return this.parsedFontFamilies_;
176
+ };
170
177
 
171
- webfont.FontApiParser.prototype.getVariations = function() {
172
- return this.variations_;
173
- };
178
+ FontApiParser.prototype.getVariations = function() {
179
+ return this.variations_;
180
+ };
174
181
 
175
- webfont.FontApiParser.prototype.getFontTestStrings = function() {
176
- return this.fontTestStrings_;
177
- };
182
+ FontApiParser.prototype.getFontTestStrings = function() {
183
+ return this.fontTestStrings_;
184
+ };
185
+ });
@@ -1,3 +1,5 @@
1
+ goog.provide('webfont.FontApiUrlBuilder');
2
+
1
3
  /**
2
4
  * @constructor
3
5
  */
@@ -15,58 +17,61 @@ webfont.FontApiUrlBuilder = function(apiUrl, protocol, text) {
15
17
 
16
18
  webfont.FontApiUrlBuilder.DEFAULT_API_URL = '//fonts.googleapis.com/css';
17
19
 
20
+ goog.scope(function () {
21
+ var FontApiUrlBuilder = webfont.FontApiUrlBuilder;
18
22
 
19
- webfont.FontApiUrlBuilder.prototype.setFontFamilies = function(fontFamilies) {
20
- this.parseFontFamilies_(fontFamilies);
21
- };
23
+ FontApiUrlBuilder.prototype.setFontFamilies = function(fontFamilies) {
24
+ this.parseFontFamilies_(fontFamilies);
25
+ };
22
26
 
23
27
 
24
- webfont.FontApiUrlBuilder.prototype.parseFontFamilies_ =
25
- function(fontFamilies) {
26
- var length = fontFamilies.length;
28
+ FontApiUrlBuilder.prototype.parseFontFamilies_ =
29
+ function(fontFamilies) {
30
+ var length = fontFamilies.length;
27
31
 
28
- for (var i = 0; i < length; i++) {
29
- var elements = fontFamilies[i].split(':');
32
+ for (var i = 0; i < length; i++) {
33
+ var elements = fontFamilies[i].split(':');
30
34
 
31
- if (elements.length == 3) {
32
- this.subsets_.push(elements.pop());
33
- }
34
- var joinCharacter = '';
35
- if (elements.length == 2 && elements[1] != ''){
36
- joinCharacter = ':';
35
+ if (elements.length == 3) {
36
+ this.subsets_.push(elements.pop());
37
+ }
38
+ var joinCharacter = '';
39
+ if (elements.length == 2 && elements[1] != ''){
40
+ joinCharacter = ':';
41
+ }
42
+ this.fontFamilies_.push(elements.join(joinCharacter));
37
43
  }
38
- this.fontFamilies_.push(elements.join(joinCharacter));
39
- }
40
- };
44
+ };
41
45
 
42
46
 
43
- webfont.FontApiUrlBuilder.prototype.webSafe = function(string) {
44
- return string.replace(/ /g, '+');
45
- };
47
+ FontApiUrlBuilder.prototype.webSafe = function(string) {
48
+ return string.replace(/ /g, '+');
49
+ };
46
50
 
47
51
 
48
- webfont.FontApiUrlBuilder.prototype.build = function() {
49
- if (this.fontFamilies_.length == 0) {
50
- throw new Error('No fonts to load !');
51
- }
52
- if (this.apiUrl_.indexOf("kit=") != -1) {
53
- return this.apiUrl_;
54
- }
55
- var length = this.fontFamilies_.length;
56
- var sb = [];
52
+ FontApiUrlBuilder.prototype.build = function() {
53
+ if (this.fontFamilies_.length == 0) {
54
+ throw new Error('No fonts to load !');
55
+ }
56
+ if (this.apiUrl_.indexOf("kit=") != -1) {
57
+ return this.apiUrl_;
58
+ }
59
+ var length = this.fontFamilies_.length;
60
+ var sb = [];
57
61
 
58
- for (var i = 0; i < length; i++) {
59
- sb.push(this.webSafe(this.fontFamilies_[i]));
60
- }
61
- var url = this.apiUrl_ + '?family=' + sb.join('%7C'); // '|' escaped.
62
+ for (var i = 0; i < length; i++) {
63
+ sb.push(this.webSafe(this.fontFamilies_[i]));
64
+ }
65
+ var url = this.apiUrl_ + '?family=' + sb.join('%7C'); // '|' escaped.
62
66
 
63
- if (this.subsets_.length > 0) {
64
- url += '&subset=' + this.subsets_.join(',');
65
- }
67
+ if (this.subsets_.length > 0) {
68
+ url += '&subset=' + this.subsets_.join(',');
69
+ }
66
70
 
67
- if (this.text_.length > 0) {
68
- url += '&text=' + encodeURIComponent(this.text_);
69
- }
71
+ if (this.text_.length > 0) {
72
+ url += '&text=' + encodeURIComponent(this.text_);
73
+ }
70
74
 
71
- return url;
72
- };
75
+ return url;
76
+ };
77
+ });