Skip to content

Instantly share code, notes, and snippets.

@agusmu
Forked from ErikKalkoken/supported_chars.php
Created October 12, 2022 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agusmu/612bb8e4d3b16e0b98f83aa0bf356f46 to your computer and use it in GitHub Desktop.
Save agusmu/612bb8e4d3b16e0b98f83aa0bf356f46 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