我正在使用 Imagick生成简单的 Logo ,这些 Logo 只是背景上的文本。
我通常会遍历所有可用的字体,为用户呈现每种字体的不同渲染选择(每种字体一张图片)。
问题是,有些字体不支持 ASCII 字符(我认为它们是专为特定语言设计的)。而且我猜某些支持 ASCII 字符的字体在使用非 ASCII 字符时也会失败。
无论如何,我最终会得到如下图片:
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/