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.74k stars 255 forks source link

How to count all the font families in the font file #131

Closed bay-anandv closed 8 months ago

bay-anandv commented 9 months ago

Actually we can get the font name but can't able to total count of available font families in the font file

bsweeney commented 9 months ago

The Collection methods are only indirectly exposed since font loading returns a File instance. So, you can't just count($font); but since the underlying class is a Collection you can iterate it to get the total number of fonts.

if ($font instanceof FontLib\TrueType\Collection) {
  $count = 0;
  foreach ($font as $fontMem) {
     $count++;
  }
  echo "Font collection contains $count fonts.";
}
bay-anandv commented 9 months ago

I have followed this way it's not give me a correct result

use FontLib\TrueType\Collection; use FontLib\Font; $trueTypeFont = Font::Load($tempFontFile); $trueTypeFont->parse(); $font_temp = $trueTypeFont->getFontName(); echo $trueTypeFont->getFontName();

if ($trueTypeFont instanceof FontLib\TrueType\Collection) { $count = 0; foreach ($trueTypeFont as $fontMem) { $count++; } echo "Font collection contains $count fonts."; }

bsweeney commented 9 months ago

The Font class is a shim around the actual methods. If you know your font is a collection you can directly load it as a collection.

$fc = new \FontLib\TrueType\Collection();
$fc->load($test_file);
echo count($fc);

I doubt that'll give you a different count. I'd need a sample of the font to determine why it's not the correct count.