Skip to content

Commit 2835668

Browse files
author
Alan Hogan
committed
Make font-guesser more robust for keywords
1 parent 6fe78eb commit 2835668

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

docs/font-guesser.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
* Usage: d = new Detector();
2323
* d.detect('font name');
2424
*/
25+
26+
function quotedFontIfNecessary(fontName) {
27+
if(/\s/.test(fontName)) { return '\'' + fontName + '\''; }
28+
else return fontName;
29+
}
30+
2531
var Detector = function() {
2632
// a font will be compared against all the three default fonts.
2733
// and if it doesn't match all 3 then that font is not available.
@@ -50,11 +56,11 @@ var Detector = function() {
5056
defaultHeight[baseFonts[index]] = s.offsetHeight; //height for the defualt font
5157
h.removeChild(s);
5258
}
53-
59+
5460
function detect(font) {
5561
var detected = false;
5662
for (var index in baseFonts) {
57-
s.style.fontFamily = font + ',' + baseFonts[index]; // name of the font along with the base font for fallback.
63+
s.style.fontFamily = quotedFontIfNecessary(font) + ', ' + baseFonts[index]; // name of the font along with the base font for fallback.
5864
h.appendChild(s);
5965
var matched = (s.offsetWidth != defaultWidth[baseFonts[index]] || s.offsetHeight != defaultHeight[baseFonts[index]]);
6066
h.removeChild(s);
@@ -72,7 +78,7 @@ var d = new Detector();
7278
function testAndShow(fontName) {
7379
var result = d.detect(fontName);
7480
var tbody = document.getElementById('font-table');
75-
tbody.innerHTML = tbody.innerHTML + '<tr><td><span class="' + (result ? 'name-match' : 'name-no-match') + '" style="font-family: \'' + fontName.replace(/"'/g,'') + '\', monospace">' + fontName.replace(/</g,'&lt;') + '</span></td><td>' + (result ? '<span class="yes">Yes</span>' : '<span class="no">No</span>') + '</td>';
81+
tbody.innerHTML = tbody.innerHTML + '<tr><td><span class="' + (result ? 'name-match' : 'name-no-match') + '" style="font-family: ' + quotedFontIfNecessary(fontName.replace(/"'/g,'')) + ', monospace">' + fontName.replace(/</g,'&lt;') + '</span></td><td>' + (result ? '<span class="yes">Yes</span>' : '<span class="no">No</span>') + '</td>';
7682
}
7783
'use strict';
7884

0 commit comments

Comments
 (0)