【发布时间】:2013-01-07 12:02:39
【问题描述】:
某些字体不支持 CSS 斜体或粗体(例如 Zapfino 不支持斜体,因为它已经很像脚本了)。我想检测何时不支持字体样式,以便我可以在编辑器中禁用样式按钮。
我尝试了like this:
that.checkFontData = function( fontList )
{ var test = $("<span style='font-size:24px;absolute;visibility:hidden;height:auto;width:auto;white-space:nowrap;'>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345678910</span>");
$('body').append( test );
for (var i= 0, iMax = fontList.length; i < iMax; ++i)
{ test.css( 'fontFamily', fontList[i] );
var normalWidth = test.outerWidth( true );
var normalHeight = test.outerHeight( true );
test.css( 'fontStyle', 'italic' );
var italicWidth = test.outerWidth( true );
var italicHeight = test.outerHeight( true );
test.css( 'fontStyle', 'normal' );
test.css( 'fontWeight', 'bold' );
var boldWidth = test.outerWidth( true );
var boldHeight = test.outerHeight( true );
console.log( fontList[i] + ", normal: " + normalWidth + ", bold: " + boldWidth + ", italic: " + italicWidth );
}
test.remove( );
};
但它不起作用...许多提供斜体或粗体的字体报告相同的宽度。
接下来,我想用画布元素来检测这一点,但是,可惜的是,Firefox 甚至不会在画布中呈现斜体文本,所以这个想法搞砸了。
你有什么建议?
【问题讨论】:
-
OCR,如果真的很重要。想不出更好的了
-
手动检查,然后对结果进行硬编码
-
拥有像 P1nGu1n 这样的硬编码结果建议将是我认为最好的最合乎逻辑的解决方案。大概您可以控制自己的字体 - 这应该很容易。
-
我应该看到以下链接 [test ccs][1] [1]:stackoverflow.com/a/2333865/1926761
标签: javascript css