Skip to content

Instantly share code, notes, and snippets.

@ErikKalkoken
Last active October 12, 2022 10:17
Show Gist options
  • Save ErikKalkoken/76afaa87399a255dd7110dfd97a7bf35 to your computer and use it in GitHub Desktop.
Save ErikKalkoken/76afaa87399a255dd7110dfd97a7bf35 to your computer and use it in GitHub Desktop.
Simple script that checks if all characters of a given string are supported by OTF / TTF font and outputs the findings in a simple HTML table.
<?php
/**
* Checks if all characters of a given string are supported by OTF file
* Outputs the findings in a simple HTML table
*
* Needs: php-font-lib @ https://github.com/PhenX/php-font-lib
*/
require_once "../src/FontLib/Autoloader.php";
/**
* Convert a string into an array of decimal Unicode code points.
*
* @param $string [string] The string to convert to codepoints
* @param $encoding [string] The encoding of $string
*
* @return [array] Array of decimal codepoints for every character of $string
*/
function toCodePoint( $string, $encoding )
{
$utf32 = mb_convert_encoding( $string, 'UTF-32', $encoding );
$length = mb_strlen( $utf32, 'UTF-32' );
$result = [];
for( $i = 0; $i < $length; ++$i )
{
$result[] = hexdec( bin2hex( mb_substr( $utf32, $i, 1, 'UTF-32' ) ) );
}
return $result;
}
$text = "エンタメMAP部データチーム";
$textCPs = toCodePoint($text, 'UTF-8');
$font = \FontLib\Font::load('NotoSans-Regular.ttf');
$charMap = $font->getUnicodeCharMap();
echo '
<table>
<tr>
<th>Char</th>
<th>Code Point</th>
<th>Supported</th>
</tr>
';
for ($i = 0; $i < mb_strlen($text); $i++)
{
$char = mb_substr($text, $i, 1);
$cp = $textCPs[$i];
$supported = array_key_exists($cp, $charMap)
? true
: false;
echo '<tr>';
echo '<td>' . $char . '</td>';
echo '<td>' . $cp . '</td>';
echo '<td>' . var_export($supported, true) . '</td>';
echo '</tr>';
}
echo '</table>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment