Skip to content

Commit 8972026

Browse files
committed
Added mock font measuring for Safari 10 with fallback bug
1 parent 117e48d commit 8972026

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/core/fontwatchrunner.js

100644100755
+39-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ webfont.FontWatchRunner = function(activeCallback, inactiveCallback, domHelper,
3939
*/
4040
webfont.FontWatchRunner.LastResortFonts = {
4141
SERIF: 'serif',
42-
SANS_SERIF: 'sans-serif'
42+
SANS_SERIF: 'sans-serif',
43+
MOCK_FONT: 'FontWatchRunnerMockFont'
4344
};
4445

4546
/**
@@ -70,7 +71,7 @@ goog.scope(function () {
7071

7172
/**
7273
* Returns true if this browser is WebKit and it has the fallback bug
73-
* which is present in WebKit 536.11 and earlier.
74+
* which is present in WebKit 602.*, 603.* ,and 536.11 and earlier
7475
*
7576
* @return {boolean}
7677
*/
@@ -81,7 +82,9 @@ goog.scope(function () {
8182
FontWatchRunner.HAS_WEBKIT_FALLBACK_BUG = !!match &&
8283
(parseInt(match[1], 10) < 536 ||
8384
(parseInt(match[1], 10) === 536 &&
84-
parseInt(match[2], 10) <= 11));
85+
parseInt(match[2], 10) <= 11) ||
86+
parseInt(match[1], 10) === 602 ||
87+
parseInt(match[1], 10) === 603);
8588
}
8689
return FontWatchRunner.HAS_WEBKIT_FALLBACK_BUG;
8790
};
@@ -110,6 +113,10 @@ goog.scope(function () {
110113
this.lastResortWidths_[FontWatchRunner.LastResortFonts.SERIF] = this.lastResortRulerA_.getWidth();
111114
this.lastResortWidths_[FontWatchRunner.LastResortFonts.SANS_SERIF] = this.lastResortRulerB_.getWidth();
112115

116+
if (FontWatchRunner.hasWebKitFallbackBug()) {
117+
this.lastResortWidths_[FontWatchRunner.LastResortFonts.MOCK_FONT] = this.getMockFontWidth_();
118+
}
119+
113120
this.started_ = goog.now();
114121

115122
this.check_();
@@ -193,6 +200,35 @@ goog.scope(function () {
193200
return this.metricCompatibleFonts_ === null || this.metricCompatibleFonts_.hasOwnProperty(this.font_.getName());
194201
};
195202

203+
204+
/**
205+
* Calculates the width of a mock font with the current font's variation css style
206+
* before it has finished loading.
207+
*
208+
* @private
209+
* @return {number}
210+
*/
211+
FontWatchRunner.prototype.getMockFontWidth_ = function () {
212+
213+
var mockFontStyle = "@font-face\{font-family: '" + FontWatchRunner.LastResortFonts.MOCK_FONT + "';" +
214+
this.font_.getCssVariation() +
215+
"src: url(some.woff2) format('woff2');\}"
216+
217+
var mockFontCss = this.domHelper_.createStyle(mockFontStyle);
218+
this.domHelper_.insertInto('head', mockFontCss)
219+
220+
var mockFontRuler = new FontRuler(this.domHelper_, this.fontTestString_);
221+
mockFontRuler.setFont(new Font(FontWatchRunner.LastResortFonts.MOCK_FONT, this.font_.getVariation()));
222+
mockFontRuler.insert();
223+
224+
var mockFontWidth = mockFontRuler.getWidth();
225+
226+
mockFontRuler.remove()
227+
this.domHelper_.removeElement(mockFontCss)
228+
229+
return mockFontWidth
230+
}
231+
196232
/**
197233
* Checks the width of the two spans against their original widths during each
198234
* async loop. If the width of one of the spans is different than the original

0 commit comments

Comments
 (0)