11

我想知道是否有办法使用 HTML/CSS/JS/PHP 显示所有字体文件字符?

假设我想显示所有可能的“Arial”字符。或者来自自定义 @font-face 字体的字符。

4

4 回答 4

5

字体可以映射范围广泛的 Unicode 字符,使用从 0 到 0x10FFFF 的代码。因此,如果您真的想以任何随机字体查看所有内容,那么按照其他答案中的建议在循环中进行操作确实会有问题。即使对于 Arial 的“简单”示例,最低映射字符是 U+0020,最高映射字符是 U+FB02。但是,并非该范围内的所有内容都已映射,因此如果您在循环中执行此操作,您最终会得到很多空的。更不用说它可能会很慢。

为了有效地做你想做的事,你需要一些东西来破解打开字体文件,检查 cmap 表,并获得一个映射字符列表(而不是一个范围)。我不确定您的设置是什么,但是您可能可以使用FreeType之类的东西在服务器端执行此操作。

于 2012-09-28T15:06:52.690 回答
1

我想建议你试试 GD 库imagettftext()函数。手册中的示例:

// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The string with character sets
for($i=0; $i<256; $i++){ 
     $text.= chr($i); 
} 

// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
于 2012-09-28T13:25:15.770 回答
0

您可以将 chr 函数与 for 循环一起使用。您将拥有一些不存在的角色,但它又快又容易。如果您愿意,您可以更具体地了解您的开始和结束 ascii 值。

for($x=1;$x<=255;$x++) {
    echo chr($x) . "&nbsp;"; 
}

然后你可以只使用 jquery 来动态更改字体。

于 2012-09-28T13:00:17.663 回答
0

我有类似的情况,我通过解析 .svg 文件解决了它。因此,如果您没有 SVG 中的文件,您可以将 ttf 转换为 SVG,然后对其进行解析以获取可用字符的 unicode。

于 2014-06-12T15:09:17.940 回答