PHP-GD:处理 Unicode 字符

发布于 2024-09-02 07:40:36 字数 368 浏览 3 评论 0原文

我正在开发一个 Web 服务,该服务使用 PHP GD 扩展和用户选择的 TTF 字体来呈现字符。

这在 ASCII 领域工作得很好,但存在一些问题:

  1. 要呈现的字符串为 UTF-8。我想将用户可选择的字体列表限制为只能正确渲染字符串的字体,因为某些字体仅具有 ASCII 字符、ISO 8601 等的字形。

  2. 在包含一些装饰字符的情况下,可以用所选字体渲染大部分字符,用 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:

  1. 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.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鱼窥荷 2024-09-09 07:40:36

针对 Pango 和 fontconfig 构建的 PHP-Cairo 应该有足够的大脑来进行字体替换在适当的时候。

PHP-Cairo built against Pango and fontconfig should have enough brains to do font substitution when appropriate.

揽月 2024-09-09 07:40:36

您可以尝试使用 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/

若相惜即相离 2024-09-09 07:40:36

如果您没有 unicode 字体,则需要尝试类似的内容

 <?php 
$trans = new Latin1UTF8(); 

$mixed = "MIXED TEXT INPUT"; 

print "Original: ".$mixed; 
print "Latin1:   ".$trans->mixed_to_latin1($mixed); 
print "UTF-8:    ".$trans->mixed_to_utf8($mixed); 

class Latin1UTF8 { 

    private $latin1_to_utf8; 
    private $utf8_to_latin1; 
    public function __construct() { 
        for($i=32; $i<=255; $i++) { 
            $this->latin1_to_utf8[chr($i)] = utf8_encode(chr($i)); 
            $this->utf8_to_latin1[utf8_encode(chr($i))] = chr($i); 
        } 
    } 

    public function mixed_to_latin1($text) { 
        foreach( $this->utf8_to_latin1 as $key => $val ) { 
            $text = str_replace($key, $val, $text); 
        } 
        return $text; 
    } 

    public function mixed_to_utf8($text) { 
        return utf8_encode($this->mixed_to_latin1($text)); 
    } 
} 
?>

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

 <?php 
$trans = new Latin1UTF8(); 

$mixed = "MIXED TEXT INPUT"; 

print "Original: ".$mixed; 
print "Latin1:   ".$trans->mixed_to_latin1($mixed); 
print "UTF-8:    ".$trans->mixed_to_utf8($mixed); 

class Latin1UTF8 { 

    private $latin1_to_utf8; 
    private $utf8_to_latin1; 
    public function __construct() { 
        for($i=32; $i<=255; $i++) { 
            $this->latin1_to_utf8[chr($i)] = utf8_encode(chr($i)); 
            $this->utf8_to_latin1[utf8_encode(chr($i))] = chr($i); 
        } 
    } 

    public function mixed_to_latin1($text) { 
        foreach( $this->utf8_to_latin1 as $key => $val ) { 
            $text = str_replace($key, $val, $text); 
        } 
        return $text; 
    } 

    public function mixed_to_utf8($text) { 
        return utf8_encode($this->mixed_to_latin1($text)); 
    } 
} 
?>

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.

臻嫒无言 2024-09-09 07:40:36

我最终使用 TTX 实用程序来转储字体指标。然后我可以检查生成的 .ttx 文件并查看字符 > 字形映射。我手动执行此操作,但可以自动解析 XML 文件。

一个示例 GNU Makefile,它从同一目录中的一组 TrueType 字体生成 .ttx 文件:

all: fontmetrics

fontmetrics: $(patsubst %.ttf,%.ttx,$(wildcard *.ttf))
.PHONY: fontmetrics

clean:
    rm -f *.ttx

%.ttx: %.ttf
    ttx -t cmap 
lt;

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:

all: fontmetrics

fontmetrics: $(patsubst %.ttf,%.ttx,$(wildcard *.ttf))
.PHONY: fontmetrics

clean:
    rm -f *.ttx

%.ttx: %.ttf
    ttx -t cmap 
lt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文