PHP-GD:处理 Unicode 字符
我正在开发一个 Web 服务,该服务使用 PHP GD 扩展和用户选择的 TTF 字体来呈现字符。
这在 ASCII 领域工作得很好,但存在一些问题:
要呈现的字符串为 UTF-8。我想将用户可选择的字体列表限制为只能正确渲染字符串的字体,因为某些字体仅具有 ASCII 字符、ISO 8601 等的字形。
在包含一些装饰字符的情况下,可以用所选字体渲染大部分字符,用 Arial(或任何包含扩展字形的字体)渲染装饰字符。
PHP-GD 似乎不支持查询字体元数据来确定是否可以用给定字体呈现字符。将字体指标导入 PHP 的好方法是什么?是否有可以以 XML 或其他可解析格式转储的命令行实用程序?
I am developing a web service that renders characters using the PHP GD extension, using a user-selected TTF font.
This works fine in ASCII-land, but there are a few problems:
The string to be rendered comes in as UTF-8. I would like to limit the list of user-selectable fonts to be only those which can render the string properly, as some fonts only have glyphs for ASCII characters, ISO 8601, etc.
In the case where some decorative characters are included, it would be fine to render the majority of characters in the selected font and render the decorative characters in Arial (or whatever font contains the extended glyphs).
It does not seem like PHP-GD has support for querying the font metadata sufficiently to figure out if a character can be rendered in a given font. What is a good way to get font metrics into PHP? Is there a command-line utility that can dump in XML or other parsable format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
针对 Pango 和 fontconfig 构建的 PHP-Cairo 应该有足够的大脑来进行字体替换在适当的时候。
PHP-Cairo built against Pango and fontconfig should have enough brains to do font substitution when appropriate.
您可以尝试使用 pdflib 扩展中的 pdf_info_font() 。很好的例子是: http: //www.pdflib.com/pdflib-cookbook/fonts/font-metrics-info/php-font-metrics-info/
You can try to use pdf_info_font() from pdflib extension. Good example is there: http://www.pdflib.com/pdflib-cookbook/fonts/font-metrics-info/php-font-metrics-info/
如果您没有 unicode 字体,则需要尝试类似的内容
http://php.net/manual/en/function.utf8-decode.php
如果混合字符和utf-8字符相等则可以使用它。如果没有,那就不能。
If you don't have a unicode font, you'll need to try something like
Taken from http://php.net/manual/en/function.utf8-decode.php
If the mixed and utf-8 characters are equal then you can use it. If not, then you can't.
我最终使用 TTX 实用程序来转储字体指标。然后我可以检查生成的 .ttx 文件并查看字符 > 字形映射。我手动执行此操作,但可以自动解析 XML 文件。
一个示例 GNU Makefile,它从同一目录中的一组 TrueType 字体生成 .ttx 文件:
I ended up using the TTX utility to dump font metrics. I could then inspect the resulting .ttx files and look at the character->glyph map. I did this manually, but automatic parsing of the XML files is possible.
An example GNU Makefile which generates the .ttx files from a set of TrueType fonts in the same directory: