webfontloader 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
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,61 +1,70 @@
1
+ goog.provide('webfont.FontRuler');
2
+
3
+ goog.require('webfont.CssFontFamilyName');
4
+ goog.require('webfont.FontVariationDescription');
5
+ goog.require('webfont.Size');
6
+
1
7
  /**
2
8
  * An element that can be used to measure the metrics
3
9
  * of a given font and string.
4
10
  * @constructor
5
11
  * @param {webfont.DomHelper} domHelper
6
- * @param {Object.<string, function(Object): webfont.Size>} fontSizer
7
12
  * @param {string} fontTestString
8
13
  */
9
- webfont.FontRuler = function(domHelper, fontSizer, fontTestString) {
14
+ webfont.FontRuler = function(domHelper, fontTestString) {
10
15
  this.domHelper_ = domHelper;
11
- this.fontSizer_ = fontSizer;
12
16
  this.fontTestString_ = fontTestString;
13
17
  this.nameHelper_ = new webfont.CssFontFamilyName();
14
18
  this.fvd_ = new webfont.FontVariationDescription();
15
19
  this.el_ = this.domHelper_.createElement('span', {}, this.fontTestString_);
16
20
  };
17
21
 
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
- };
22
+ goog.scope(function () {
23
+ var FontRuler = webfont.FontRuler,
24
+ Size = webfont.Size;
26
25
 
27
- /**
28
- * Inserts the ruler into the DOM.
29
- */
30
- webfont.FontRuler.prototype.insert = function() {
31
- this.domHelper_.insertInto('body', this.el_);
32
- };
26
+ /**
27
+ * @param {string} fontFamily
28
+ * @param {string=} opt_fontDescription
29
+ */
30
+ FontRuler.prototype.setFont = function(fontFamily, opt_fontDescription) {
31
+ var styleString = this.computeStyleString_(fontFamily, opt_fontDescription);
32
+ this.domHelper_.setStyle(this.el_, styleString);
33
+ };
33
34
 
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;white-space:nowrap;font-family:" +
45
- this.nameHelper_.quote(fontFamily) + ";" + variationCss;
46
- return styleString;
47
- };
35
+ /**
36
+ * Inserts the ruler into the DOM.
37
+ */
38
+ FontRuler.prototype.insert = function() {
39
+ this.domHelper_.insertInto('body', this.el_);
40
+ };
48
41
 
49
- /**
50
- * @return {webfont.Size}
51
- */
52
- webfont.FontRuler.prototype.getSize = function() {
53
- return this.fontSizer_.getSize(this.el_);
54
- };
42
+ /**
43
+ * @private
44
+ * @param {string} fontFamily
45
+ * @param {string=} opt_fontDescription
46
+ * @return {string}
47
+ */
48
+ FontRuler.prototype.computeStyleString_ = function(fontFamily, opt_fontDescription) {
49
+ var variationCss = opt_fontDescription ? this.fvd_.expand(opt_fontDescription) : '';
50
+ var styleString = "position:absolute;top:-999px;left:-999px;" +
51
+ "font-size:300px;width:auto;height:auto;line-height:normal;margin:0;" +
52
+ "padding:0;font-variant:normal;white-space:nowrap;font-family:" +
53
+ this.nameHelper_.quote(fontFamily) + ";" + variationCss;
54
+ return styleString;
55
+ };
55
56
 
56
- /**
57
- * Removes the ruler element from the DOM.
58
- */
59
- webfont.FontRuler.prototype.remove = function() {
60
- this.domHelper_.removeElement(this.el_);
61
- };
57
+ /**
58
+ * @return {webfont.Size}
59
+ */
60
+ FontRuler.prototype.getSize = function() {
61
+ return new Size(this.el_.offsetWidth, this.el_.offsetHeight);
62
+ };
63
+
64
+ /**
65
+ * Removes the ruler element from the DOM.
66
+ */
67
+ FontRuler.prototype.remove = function() {
68
+ this.domHelper_.removeElement(this.el_);
69
+ };
70
+ });
@@ -1,3 +1,5 @@
1
+ goog.provide('webfont.FontVariationDescription');
2
+
1
3
  /**
2
4
  * @constructor
3
5
  */
@@ -38,97 +40,101 @@ webfont.FontVariationDescription.VALUES = {
38
40
  ]
39
41
  };
40
42
 
41
- /**
42
- * @private
43
- * @constructor
44
- */
45
- webfont.FontVariationDescription.Item = function(index, property, values) {
46
- this.index_ = index;
47
- this.property_ = property;
48
- this.values_ = values;
49
- }
50
-
51
- webfont.FontVariationDescription.Item.prototype.compact = function(output, value) {
52
- for (var i = 0; i < this.values_.length; i++) {
53
- if (value == this.values_[i][1]) {
54
- output[this.index_] = this.values_[i][0];
55
- return;
56
- }
43
+ goog.scope(function () {
44
+ var FontVariationDescription = webfont.FontVariationDescription;
45
+
46
+ /**
47
+ * @private
48
+ * @constructor
49
+ */
50
+ FontVariationDescription.Item = function(index, property, values) {
51
+ this.index_ = index;
52
+ this.property_ = property;
53
+ this.values_ = values;
57
54
  }
58
- }
59
55
 
60
- webfont.FontVariationDescription.Item.prototype.expand = function(output, value) {
61
- for (var i = 0; i < this.values_.length; i++) {
62
- if (value == this.values_[i][0]) {
63
- output[this.index_] = this.property_ + ':' + this.values_[i][1];
64
- return;
56
+ FontVariationDescription.Item.prototype.compact = function(output, value) {
57
+ for (var i = 0; i < this.values_.length; i++) {
58
+ if (value == this.values_[i][1]) {
59
+ output[this.index_] = this.values_[i][0];
60
+ return;
61
+ }
65
62
  }
66
63
  }
67
- }
68
64
 
69
- /**
70
- * Compacts CSS declarations into an FVD.
71
- * @param {string} input A string of CSS declarations such as
72
- * 'font-weight:normal;font-style:italic'.
73
- * @return {string} The equivalent FVD such as 'n4'.
74
- */
75
- webfont.FontVariationDescription.prototype.compact = function(input) {
76
- var result = ['n', '4'];
77
- var descriptors = input.split(';');
78
-
79
- for (var i = 0, len = descriptors.length; i < len; i++) {
80
- var pair = descriptors[i].replace(/\s+/g, '').split(':');
81
- if (pair.length == 2) {
82
- var property = pair[0];
83
- var value = pair[1];
84
- var item = this.getItem_(property);
85
- if (item) {
86
- item.compact(result, value);
65
+ FontVariationDescription.Item.prototype.expand = function(output, value) {
66
+ for (var i = 0; i < this.values_.length; i++) {
67
+ if (value == this.values_[i][0]) {
68
+ output[this.index_] = this.property_ + ':' + this.values_[i][1];
69
+ return;
87
70
  }
88
71
  }
89
72
  }
90
73
 
91
- return result.join('');
92
- };
74
+ /**
75
+ * Compacts CSS declarations into an FVD.
76
+ * @param {string} input A string of CSS declarations such as
77
+ * 'font-weight:normal;font-style:italic'.
78
+ * @return {string} The equivalent FVD such as 'n4'.
79
+ */
80
+ FontVariationDescription.prototype.compact = function(input) {
81
+ var result = ['n', '4'];
82
+ var descriptors = input.split(';');
93
83
 
94
- /**
95
- * Expands a FVD string into equivalent CSS declarations.
96
- * @param {string} fvd The FVD string, such as 'n4'.
97
- * @return {?string} The equivalent CSS such as
98
- * 'font-weight:normal;font-style:italic' or null if it cannot be parsed.
99
- */
100
- webfont.FontVariationDescription.prototype.expand = function(fvd) {
101
- if (fvd.length != 2) {
102
- return null;
103
- }
84
+ for (var i = 0, len = descriptors.length; i < len; i++) {
85
+ var pair = descriptors[i].replace(/\s+/g, '').split(':');
86
+ if (pair.length == 2) {
87
+ var property = pair[0];
88
+ var value = pair[1];
89
+ var item = this.getItem_(property);
90
+ if (item) {
91
+ item.compact(result, value);
92
+ }
93
+ }
94
+ }
104
95
 
105
- var result = [null, null];
96
+ return result.join('');
97
+ };
106
98
 
107
- for (var i = 0, len = this.properties_.length; i < len; i++) {
108
- var property = this.properties_[i];
109
- var key = fvd.substr(i, 1);
110
- var values = this.values_[property];
111
- var item = new webfont.FontVariationDescription.Item(i, property, values);
112
- item.expand(result, key);
113
- }
99
+ /**
100
+ * Expands a FVD string into equivalent CSS declarations.
101
+ * @param {string} fvd The FVD string, such as 'n4'.
102
+ * @return {?string} The equivalent CSS such as
103
+ * 'font-weight:normal;font-style:italic' or null if it cannot be parsed.
104
+ */
105
+ FontVariationDescription.prototype.expand = function(fvd) {
106
+ if (fvd.length != 2) {
107
+ return null;
108
+ }
114
109
 
115
- if (result[0] && result[1]) {
116
- return result.join(';') + ';';
117
- } else {
118
- return null;
119
- }
120
- }
110
+ var result = [null, null];
121
111
 
122
- /**
123
- * @private
124
- */
125
- webfont.FontVariationDescription.prototype.getItem_ = function(property) {
126
- for (var i = 0; i < this.properties_.length; i++) {
127
- if (property == this.properties_[i]) {
112
+ for (var i = 0, len = this.properties_.length; i < len; i++) {
113
+ var property = this.properties_[i];
114
+ var key = fvd.substr(i, 1);
128
115
  var values = this.values_[property];
129
- return new webfont.FontVariationDescription.Item(i, property, values);
116
+ var item = new webfont.FontVariationDescription.Item(i, property, values);
117
+ item.expand(result, key);
118
+ }
119
+
120
+ if (result[0] && result[1]) {
121
+ return result.join(';') + ';';
122
+ } else {
123
+ return null;
130
124
  }
131
125
  }
132
126
 
133
- return null;
134
- };
127
+ /**
128
+ * @private
129
+ */
130
+ FontVariationDescription.prototype.getItem_ = function(property) {
131
+ for (var i = 0; i < this.properties_.length; i++) {
132
+ if (property == this.properties_[i]) {
133
+ var values = this.values_[property];
134
+ return new webfont.FontVariationDescription.Item(i, property, values);
135
+ }
136
+ }
137
+
138
+ return null;
139
+ };
140
+ });
@@ -1,20 +1,17 @@
1
+ goog.provide('webfont.FontWatcher');
2
+
3
+ goog.require('webfont.FontWatchRunner');
4
+
1
5
  /**
2
6
  * @constructor
3
7
  * @param {webfont.UserAgent} userAgent
4
8
  * @param {webfont.DomHelper} domHelper
5
9
  * @param {webfont.EventDispatcher} eventDispatcher
6
- * @param {Object.<string, function(Object): webfont.Size>} fontSizer
7
- * @param {function(function(), number=)} asyncCall
8
- * @param {function(): number} getTime
9
10
  * @param {number=} opt_timeout
10
11
  */
11
- webfont.FontWatcher = function(userAgent, domHelper, eventDispatcher, fontSizer,
12
- asyncCall, getTime, opt_timeout) {
12
+ webfont.FontWatcher = function(userAgent, domHelper, eventDispatcher, opt_timeout) {
13
13
  this.domHelper_ = domHelper;
14
14
  this.eventDispatcher_ = eventDispatcher;
15
- this.fontSizer_ = fontSizer;
16
- this.asyncCall_ = asyncCall;
17
- this.getTime_ = getTime;
18
15
  this.currentlyWatched_ = 0;
19
16
  this.last_ = false;
20
17
  this.success_ = false;
@@ -29,95 +26,103 @@ webfont.FontWatcher = function(userAgent, domHelper, eventDispatcher, fontSizer,
29
26
  */
30
27
  webfont.FontWatcher.DEFAULT_VARIATION = 'n4';
31
28
 
32
- /**
33
- * Watches a set of font families.
34
- * @param {Array.<string>} fontFamilies The font family names to watch.
35
- * @param {Object.<string, Array.<string>>} fontDescriptions The font variations
36
- * of each family to watch. Described with FVD.
37
- * @param {Object.<string, string>} fontTestStrings The font test strings for
38
- * each family.
39
- * @param {function(new:webfont.FontWatchRunner, function(string, string),
40
- * function(string, string), webfont.DomHelper,
41
- * Object.<string, function(Object): number>,
42
- * function(function(), number=), function(): number, string, string,
43
- * webfont.BrowserInfo, number=, Object.<string,boolean>=, string=)} fontWatchRunnerCtor The font watch runner constructor.
44
- * @param {boolean} last True if this is the last set of families to watch.
45
- */
46
- webfont.FontWatcher.prototype.watch = function(fontFamilies, fontDescriptions,
47
- fontTestStrings, fontWatchRunnerCtor, last) {
48
- var length = fontFamilies.length;
49
-
50
- if (length === 0) {
51
- this.eventDispatcher_.dispatchInactive();
52
- return;
53
- }
54
-
55
- for (var i = 0; i < length; i++) {
56
- var fontFamily = fontFamilies[i];
57
- if (!fontDescriptions[fontFamily]) {
58
- fontDescriptions[fontFamily] = [webfont.FontWatcher.DEFAULT_VARIATION];
29
+ goog.scope(function () {
30
+ var FontWatcher = webfont.FontWatcher;
31
+
32
+ /**
33
+ * Watches a set of font families.
34
+ * @param {Array.<string>} fontFamilies The font family names to watch.
35
+ * @param {Object.<string, Array.<string>>} fontDescriptions The font variations
36
+ * of each family to watch. Described with FVD.
37
+ * @param {Object.<string, string>} fontTestStrings The font test strings for
38
+ * each family.
39
+ * @param {function(new:webfont.FontWatchRunner,
40
+ * function(string, string),
41
+ * function(string, string),
42
+ * webfont.DomHelper,
43
+ * string,
44
+ * string,
45
+ * webfont.BrowserInfo,
46
+ * number=,
47
+ * Object.<string, boolean>=,
48
+ * string=)} fontWatchRunnerCtor The font watch runner constructor.
49
+ * @param {boolean} last True if this is the last set of families to watch.
50
+ */
51
+ FontWatcher.prototype.watch = function(fontFamilies, fontDescriptions,
52
+ fontTestStrings, fontWatchRunnerCtor, last) {
53
+ var length = fontFamilies.length;
54
+
55
+ if (length === 0) {
56
+ this.eventDispatcher_.dispatchInactive();
57
+ return;
59
58
  }
60
- this.currentlyWatched_ += fontDescriptions[fontFamily].length;
61
- }
62
59
 
63
- if (last) {
64
- this.last_ = last;
65
- }
60
+ for (var i = 0; i < length; i++) {
61
+ var fontFamily = fontFamilies[i];
62
+ if (!fontDescriptions[fontFamily]) {
63
+ fontDescriptions[fontFamily] = [webfont.FontWatcher.DEFAULT_VARIATION];
64
+ }
65
+ this.currentlyWatched_ += fontDescriptions[fontFamily].length;
66
+ }
66
67
 
67
- for (var i = 0; i < length; i++) {
68
- var fontFamily = fontFamilies[i];
69
- var descriptions = fontDescriptions[fontFamily];
70
- var fontTestString = fontTestStrings[fontFamily];
68
+ if (last) {
69
+ this.last_ = last;
70
+ }
71
71
 
72
- for (var j = 0, len = descriptions.length; j < len; j++) {
73
- var fontDescription = descriptions[j];
72
+ for (var i = 0; i < length; i++) {
73
+ var fontFamily = fontFamilies[i];
74
+ var descriptions = fontDescriptions[fontFamily];
75
+ var fontTestString = fontTestStrings[fontFamily];
74
76
 
75
- this.eventDispatcher_.dispatchFontLoading(fontFamily, fontDescription);
77
+ for (var j = 0, len = descriptions.length; j < len; j++) {
78
+ var fontDescription = descriptions[j];
76
79
 
77
- var activeCallback = webfont.bind(this, this.fontActive_);
78
- var inactiveCallback = webfont.bind(this, this.fontInactive_)
79
- var fontWatchRunner = new fontWatchRunnerCtor(activeCallback,
80
- inactiveCallback, this.domHelper_, this.fontSizer_, this.asyncCall_,
81
- this.getTime_, fontFamily, fontDescription,
82
- this.browserInfo_, this.timeout_, null, fontTestString);
80
+ this.eventDispatcher_.dispatchFontLoading(fontFamily, fontDescription);
83
81
 
84
- fontWatchRunner.start();
82
+ var activeCallback = goog.bind(this.fontActive_, this);
83
+ var inactiveCallback = goog.bind(this.fontInactive_, this);
84
+ var fontWatchRunner = new fontWatchRunnerCtor(activeCallback,
85
+ inactiveCallback, this.domHelper_, fontFamily, fontDescription,
86
+ this.browserInfo_, this.timeout_, null, fontTestString);
87
+
88
+ fontWatchRunner.start();
89
+ }
85
90
  }
86
- }
87
- };
91
+ };
88
92
 
89
- /**
90
- * Called by a FontWatchRunner when a font has been detected as active.
91
- * @param {string} fontFamily
92
- * @param {string} fontDescription
93
- * @private
94
- */
95
- webfont.FontWatcher.prototype.fontActive_ = function(fontFamily, fontDescription) {
96
- this.eventDispatcher_.dispatchFontActive(fontFamily, fontDescription);
97
- this.success_ = true;
98
- this.decreaseCurrentlyWatched_();
99
- };
93
+ /**
94
+ * Called by a FontWatchRunner when a font has been detected as active.
95
+ * @param {string} fontFamily
96
+ * @param {string} fontDescription
97
+ * @private
98
+ */
99
+ FontWatcher.prototype.fontActive_ = function(fontFamily, fontDescription) {
100
+ this.eventDispatcher_.dispatchFontActive(fontFamily, fontDescription);
101
+ this.success_ = true;
102
+ this.decreaseCurrentlyWatched_();
103
+ };
100
104
 
101
- /**
102
- * Called by a FontWatchRunner when a font has been detected as inactive.
103
- * @param {string} fontFamily
104
- * @param {string} fontDescription
105
- * @private
106
- */
107
- webfont.FontWatcher.prototype.fontInactive_ = function(fontFamily, fontDescription) {
108
- this.eventDispatcher_.dispatchFontInactive(fontFamily, fontDescription);
109
- this.decreaseCurrentlyWatched_();
110
- };
105
+ /**
106
+ * Called by a FontWatchRunner when a font has been detected as inactive.
107
+ * @param {string} fontFamily
108
+ * @param {string} fontDescription
109
+ * @private
110
+ */
111
+ FontWatcher.prototype.fontInactive_ = function(fontFamily, fontDescription) {
112
+ this.eventDispatcher_.dispatchFontInactive(fontFamily, fontDescription);
113
+ this.decreaseCurrentlyWatched_();
114
+ };
111
115
 
112
- /**
113
- * @private
114
- */
115
- webfont.FontWatcher.prototype.decreaseCurrentlyWatched_ = function() {
116
- if (--this.currentlyWatched_ == 0 && this.last_) {
117
- if (this.success_) {
118
- this.eventDispatcher_.dispatchActive();
119
- } else {
120
- this.eventDispatcher_.dispatchInactive();
116
+ /**
117
+ * @private
118
+ */
119
+ FontWatcher.prototype.decreaseCurrentlyWatched_ = function() {
120
+ if (--this.currentlyWatched_ == 0 && this.last_) {
121
+ if (this.success_) {
122
+ this.eventDispatcher_.dispatchActive();
123
+ } else {
124
+ this.eventDispatcher_.dispatchInactive();
125
+ }
121
126
  }
122
- }
123
- };
127
+ };
128
+ });