How to count all the font families in the font file
bay-anandv opened this issue · 3 comments
Actually we can get the font name but can't able to total count of available font families in the font file
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.";
}
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.";
}
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.