dompdf / php-font-lib

A library to read, parse, export and make subsets of different types of font files.
GNU Lesser General Public License v2.1
1.73k stars 255 forks source link

A little explanation regarding glyphs #30

Closed AydinHassan closed 8 months ago

AydinHassan commented 8 years ago

Hi,

Full disclosure: I know nothing regarding fonts.

I am currently tasked with building some functionality which can render all of the glyphs in a font as an image. So basically just an image with a grid of glyphs.

I found this library and saw that I could extract all the glyphs from a given font. I assumed I would be able to pass a font, grab the glyphs and then render each glyph on an image. So I came up with the following code:

require_once __DIR__ . '/vendor/autoload.php';
use FontLib\Font;

$font = Font::load('sample-fonts/IntelClear-Light.ttf');

$glyphIndexArray = $font->getUnicodeCharMap();

$text = '';
foreach ($glyphIndexArray as $decimalCode => $index) {
    if ($count % 30 === 0) {
        $text .= "\n";

    }

    if ($decimalCode < 32 || ($decimalCode > 126 && $decimalCode < 160)) {
        continue;
    }

    $text .= sprintf("&#%s;", $decimalCode);
    $count++;
}

//render as image
$image_height = 10000;
$image_width = 6100;
$size = 26;

$fcol =[hexdec(55), hexdec(55), hexdec(55)];
$scol =[hexdec(00), hexdec(00), hexdec(00)];
$bgcol =[hexdec('ff'), hexdec('ff'), hexdec('ff')];

$bbox = imagettfbbox($size, 0, 'sample-fonts/IntelClear-Light.ttf', $text);
$image_height = ($bbox[1] - $bbox[0]) + 60;

$image = imagecreatetruecolor($image_width, $image_height);

$font_colour = imagecolorallocate($image, $fcol[0], $fcol[1], $fcol[2]);
$s_colour = imagecolorallocate($image, $scol[0], $scol[1], $scol[2]);
$bg_colour = imagecolorallocate($image, $bgcol[0], $bgcol[1], $bgcol[2]);

imagefilledrectangle($image, 0, 0, $image_width, $image_height, $bg_colour);

imagettftext($image, $size, 0, 11, (45 + 1), $font_colour, 'sample-fonts/IntelClear-Light.ttf', $text);
header('Content-Type: image/jpeg');
imagepng($image);
imagedestroy($image);

This seems to work, after hours of playing. But what I can't understand is the getUnicodeCharMap() function. For this particular font it returns something like:

array (size=434)
  0 => int 1
  8 => int 1
  9 => int 2
  13 => int 2
  29 => int 1
  32 => int 3
  33 => int 9
  34 => int 13
  35 => int 83
  36 => int 56
  37 => int 47
  38 => int 89
  39 => int 12
  40 => int 94
...

So the way I am currently using it is treating the keys as the unicode decimal value for the particular character.

Is this correct? If so: What is the value of each key?

For example. Lets pick element 2.

8 (the key) is the unicode decimal value What is 1 (the value) ?

Thanks and sorry for the n00b questions!

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/28125133-a-little-explanation-regarding-glyphs?utm_campaign=plugin&utm_content=tracker%2F317728&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F317728&utm_medium=issues&utm_source=github).
bsweeney commented 8 years ago

I believe this method returns a character code -> glyph table index mapping. The array index is the decimal value of the Unicode character. The array value is the index of the glyph in the glyph table.

But @PhenX can school both of us on this.

oxygen commented 8 years ago

Take a look at their demo page, and go the glyph tab after loading a font. "You can find a demo GUI here."

This is the source code of that demo page: https://github.com/traitify/traitify-php/blob/master/vendor/phenx/php-font-lib/www/font_info.php https://github.com/traitify/traitify-php/blob/master/vendor/phenx/php-font-lib/www/js/glyph.js