php - 有没有办法判断字体是否支持 Imagick 中的给定字符?

标签 php fonts imagick

我正在使用 Imagick生成简单的 Logo ,这些 Logo 只是背景上的文本。

我通常会遍历所有可用的字体,为用户呈现每种字体的不同渲染选择(每种字体一张图片)。

问题是,有些字体不支持 ASCII 字符(我认为它们是专为特定语言设计的)。而且我猜某些支持 ASCII 字符的字体在使用非 ASCII 字符时也会失败。

无论如何,我最终会得到如下图片:

Imagick non-supported font characters Imagick non-supported font characters Imagick non-supported font characters

Imagick 中是否有编程方式来判断给定字体是否支持给定字符串中的所有字符?

这将帮助我过滤掉那些不支持用户输入的文本的字体,并避免显示任何垃圾图像,例如上面的图像。

最佳答案

我不知道使用 imagemagik 的方法,但您可以从这里使用 php-font-parser 库:

https://github.com/Pomax/PHP-Font-Parser

具体来说,您可以为所需字符串中的每个字母解析一个字体并检查返回值:

    $fonts = array("myfont.ttf");

    /**
     * For this test, we'll print the header information for the
     * loaded font, and try to find the letter "g".
     */
    $letter = "g";
    $json = false;
    while($json === false && count($fonts)>0) {
            $font = new OTTTFont(array_pop($fonts));
            echo "font header data:\n" . $font->toString() . "\n";
            $data = $font->get_glyph($letter);
            if($data!==false) {
                    $json = $data->toJSON(); }}

    if($json===false) { die("the letter '$letter' could not be found!"); }
    echo "glyph information for '$letter':\n" . $json;

以上代码来自字体解析器项目fonttest.php类:

https://github.com/Pomax/PHP-Font-Parser/blob/master/fonttest.php

关于php - 有没有办法判断字体是否支持 Imagick 中的给定字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20613228/

相关文章:

objective-c - 如何确定 NSFont 是否安装在 Objective-C 的 Mac OSX 机器上?

c# - 如何设置 WPF 应用程序的默认字体?

php - 如何在 Imagick 中设置裁剪颜色?

php - 在 Azure WebApp 上配置 Imagick 并启用 Composer

php - 如果页面尚不存在,我如何以编程方式在 WordPress 中创建该页面?

php - Kohana 3分页

php - 标题不起作用

java - 是否可以在 Java 服务器中添加 PHP 脚本?

css - Font Awesome : Why isn't my arrow icon loading on an :after instance?

php - Imagick 构造函数使 PHP/CGI 崩溃