检查是否安装了特定字体?

时间:2013-04-26 17:45:18

标签: fonts

我用乌尔都语创建了一个RTL网站。它还需要“Alvi Nastaleeq”乌尔都语字体才能正确查看。如何检查访问者是否安装了这样的字体?如果字体不是fount,则显示一条消息而不是网站上写着“这是网站完全在乌尔都语编译。要查看它,请安装Alvi Nastaleeq字体”。怎么做?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用javascript:http://www.kirupa.com/html5/detect_whether_font_is_installed.htm

代码:

//
// Call this function and pass in the name of the font you want to check for availability.
//
function doesFontExist(fontName) {
    // creating our in-memory Canvas element where the magic happens
    var canvas = document.createElement("canvas");
    var context = canvas.getContext("2d");

    // the text whose final pixel size I want to measure
    var text = "abcdefghijklmnopqrstuvwxyz0123456789";

    // specifying the baseline font
    context.font = "72px monospace";

    // checking the size of the baseline text
    var baselineSize = context.measureText(text).width;

    // specifying the font whose existence we want to check
    context.font = "72px '" + fontName + "', monospace";

    // checking the size of the font we want to check
    var newSize = context.measureText(text).width;

    // removing the Canvas element we created
    delete canvas;

    //
    // If the size of the two text instances is the same, the font does not exist because it is being rendered
    // using the default sans-serif font
    //
    if (newSize == baselineSize) {
        return false;
    } else {
        return true;
    }
}

doesFontExist("Comic Sans MS");