-
Notifications
You must be signed in to change notification settings - Fork 6
/
jfont-checker.js
85 lines (71 loc) · 2.01 KB
/
jfont-checker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
*
* JFont Checker
* Derek Leung
* Original Date: 2010.8.23
* Current: Feb 2016
*
* This piece of code checks for the existence of a specified font.
* It ultilizes the font fallback mechanism in CSS for font checking.
*
* Compatibility:
* Tested on Chrome, Firefox, IE9+
* Requires CSS and JS
*
**/
(function(){
var containerA, containerB, html = document.getElementsByTagName("html")[0],
filler = "random_words_#_!@#$^&*()_+mdvejreu_RANDOM_WORDS";
function createContainers(){
containerA = document.createElement("span");
containerB = document.createElement("span");
containerA.textContent = filler;
containerB.textContent = filler;
var styles = {
margin: "0",
padding: "0",
fontSize: "32px",
position: "absolute",
zIndex: "-1"
};
for(var key in styles){
if(styles.hasOwnProperty(key)){
containerA.style[key] = styles[key];
containerB.style[key] = styles[key];
}
}
return function(){
//clean up
containerA.outerHTML = "";
containerB.outerHTML = "";
};
}
function checkDimension(){
return containerA.offsetWidth === containerB.offsetWidth &&
containerA.offsetHeight === containerB.offsetHeight;
}
function checkfont(font, DOM){
var rootEle = html;
if(DOM && DOM.children && DOM.children.length) rootEle = DOM.children[0];
var result = null,
reg = /[\,\.\/\;\'\[\]\`\<\>\\\?\:\"\{\}\|\~\!\@\#\$\%\^\&\*\(\)\-\=\_\+]/g,
cleanUp = createContainers();
font = font.replace(reg, "");
rootEle.appendChild(containerA);
rootEle.appendChild(containerB);
//First Check
containerA.style.fontFamily = font + ",monospace";
containerB.style.fontFamily = "monospace";
if(checkDimension()){
//Assume Arial exists, Second Check
containerA.style.fontFamily = font + ",Arial";
containerB.style.fontFamily = "Arial";
result = !checkDimension();
}else{
result = true;
}
cleanUp();
return result
}
this.checkfont = checkfont;
})();