Skip to content

Instantly share code, notes, and snippets.

@sorrycc
Created April 18, 2014 08:37
Show Gist options
  • Save sorrycc/11031875 to your computer and use it in GitHub Desktop.
Save sorrycc/11031875 to your computer and use it in GitHub Desktop.
/**
* 检测客户端系统中,是否安装了某种字体
*
* 在IE中,通过<object classid>的方式,提取系统中的字体库进行判断
* 非IE中,通过字体宽度与默认字体进行比较
*
* @example
qext.FontDetect.detect(['微软雅黑','Microsoft Yahei'],function(isExist){
//获得html节点
var htmlElm = qing.dom.query('html')[0];
//开启clearType并存在雅黑字体
if(isExist){
qing.dom.addClass(htmlElm,'mod-with-msyahei');
}else{
qing.dom.addClass(htmlElm,'mod-without-msyahei');
}
});
*
* @author 赵先烈([email protected]
* @date 2011-12-22
*/
(function () {
var qext = {};
var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used
qext.browser = {
version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
safari: /webkit/.test(userAgent),
opera: /opera/.test(userAgent),
msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
};
qext.dom = {
setStyles: function (elem, styles) {
var s = elem.style;
for (var key in styles) {
if (styles.hasOwnProperty(key)) {
s[key] = styles[key];
}
}
},
create: function (elem, attr) {
var e = document.createElement(elem);
for (var key in attr) {
if (attr.hasOwnProperty(key)) {
e[key] = attr[key];
}
}
return e;
}
};
qext.FontDetect = (function () {
/**
* 文字默认宽度
*/
var _defaultWidth = null;
/**
* IE下通过object标签,classid生成一个对象,获取系统字体
*/
var _dlgHelper = null;
/**
* 是否已经初始化
*/
var _isInitialedInIE = false;
/**
* 检查字体宽度
* @param {Object} family
*/
var checkOffsetWidth = function (family) {
var node = document.createElement("p");
qext.dom.setStyles(node, {
"font-family": family + ", Times New Roman",
"font-size": '300pt',
"display": "inline",
"position": "absolute",
"top": "-10000px",
"left": "-10000px"
});
//qext.dom.addClass(node, "sp-font-detect");
node.innerHTML = "mmmmmmmmml";
document.body.appendChild(node);
var width = node.offsetWidth;
document.body.removeChild(node);
return width;
};
/**
* 获取文字实际宽度
*/
var getDefaultWidth = function () {
if (!_defaultWidth)
_defaultWidth = checkOffsetWidth("Times New Roman");
return _defaultWidth;
};
/**
* 初始化
*/
var init = function () {
if (qext.browser.msie) {
_dlgHelper = qext.dom.create('object', {
id: "sp-font-detect-obj",
classid: "clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b"
});
qext.dom.setStyles(_dlgHelper, {
"position": "absolute",
"top": "-10000px",
"left": "-10000px",
"width": "1px",
"height": "1px"
});
document.body.appendChild(_dlgHelper);
_isInitialedInIE = true;
}
};
/**
* 检测某个值是否存在于数组中
* @param {Object} target
* @param {Object} arr
*/
var isInArray = function (target, arr) {
for (var j = 0, flen = arr.length; j < flen; j++) {
if (target == arr[j]) {
return true;
}
}
return false;
};
/**
* 是否开启了clearType
*@function isClearTypeOn
*@return {Boolean} 如果支持,显示true;否则返回false
*/
var isClearTypeOn = function () {
if (typeof screen.fontSmoothingEnabled != "undefined")
return screen.fontSmoothingEnabled;
else
try {
var f = document.createElement("canvas");
f.width = "35";
f.height = "35";
f.style.display = "none";
document.body.appendChild(f);
var o = f.getContext("2d");
o.textBaseline = "top";
o.font = "32px Arial";
o.fillStyle = "black";
o.strokeStyle = "black";
o.fillText("E", 0, 0);
for (var r = 8; r <= 32; r++)
for (var u = 1; u <= 32; u++) {
var q = o.getImageData(u, r, 1, 1).data[3];
if (q != 255 && q != 0) {
document.body.removeChild(f);
return true;
}
}
document.body.removeChild(f);
return false;
} catch (y) {
return false;
}
};
/**
* 检测是否存在某种字体
* @param {Array} familys 待检测的字体
* @param {Function} callback 回调,eg:function(isExist){}
* @config {Boolean} isExist 是否存在
*/
var detectFont = function (familys, callback) {
if (typeof callback !== "function") {
callback = new Function();
}
// 指定“Times New Roman”为默认字体
if (isInArray("Times New Roman", familys)) {
return callback(true);
}
//IE中,用object的classid来判断字体
if (qext.browser.msie) {
if (!_isInitialedInIE) {
init();
}
var sysFonts = _dlgHelper.fonts;
if (sysFonts.count) {
var fs = '';
for (var i = 1, len = sysFonts.count; i <= len; i++) {
fs += sysFonts(i) + "|";
if (isInArray(sysFonts(i), familys)) {
return callback(true);
}
}
// alert(fs);
}
return callback(false);
}
//非IE浏览器中,用比较宽度的方法来判断
else {
var familyWidth = 0;
var defaultWidth = getDefaultWidth();
for (var j = 0, flen = familys.length; j < flen; j++) {
familyWidth = checkOffsetWidth(familys[j]);
if (familyWidth !== defaultWidth) {
return callback(true);
}
}
return callback(false);
}
};
/**
* 监测:先检测机器是否开启了clearType,然后监测是否包含某种字体
* @param {Array} familys [description]
* @param {Function} callback [description]
* @return {Null}
*/
var detect = function (familys, callback) {
var flag = (navigator.userAgent.indexOf("Windows NT 5.1") > -1) ? isClearTypeOn() : true;
if (!flag) {
callback(false);
} else {
detectFont(familys, callback);
}
return null;
};
return {
isClearTypeOn: isClearTypeOn,
detectFont: detectFont,
detect: detect
};
})();
function doFontDetect(fonts) {
var result = 0;
if (typeof fonts == "string") fonts = [fonts];
qext.FontDetect.detect(fonts, function (support) {
if (support) {
if (qext.FontDetect.isClearTypeOn()) {
result = 2;
} else {
result = 1;
}
}
});
return result;
}
window._ap_fontDetect = doFontDetect;
// _ap_fontDetect(['\u5fae\u8f6f\u96c5\u9ed1', 'Microsoft Yahei']);
/*qext.FontDetect.detect(['\u5fae\u8f6f\u96c5\u9ed1', 'Microsoft Yahei'], function (support) {
if (support) {
if (qext.FontDetect.isClearTypeOn) {
alert("support yahei and clearType");
} else {
alert("support yahei but not clearType");
}
} else {
alert("not support yahei");
}
});*/
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment