Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell whether a font supports a given character in Imagick?

Tags:

php

fonts

imagick

I'm using Imagick to generate simple logos, which are just text on a background.

I'm usually looping through all available fonts, to present the user with a choice of different renderings for every font (one image per font).

The problem is, some fonts don't support the ASCII characters (I think they've been designed for a given language only). And I guess that some of the fonts which support ASCII characters, will fail with non-ASCII characters as well.

Anyway, I end up with images such as these:

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

Is there a programmatic way in Imagick to tell whether a given font supports all the characters in a given string?

That would help me filter out those fonts which do not support the text the user typed in, and avoid displaying any garbage images such as the ones above.

like image 859
BenMorel Avatar asked Dec 16 '13 14:12

BenMorel


1 Answers

I don't know a way using imagemagik, but you could use the php-font-parser library from here:

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

Specifically, you can parse a font for each letter in your required string and check the return value:

    $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;

Above code comes from the font parser projects fonttest.php class:

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

like image 72
DEzra Avatar answered Sep 17 '22 19:09

DEzra