dompdf/php-font-lib

Hello, can generate a font file that contains only that string based on a string?

wilbur-yu opened this issue · 1 comments

I now have a page that needs to generate a font file containing only these words based on the text the user enters, because the Chinese font base is too large to be used in a browser.

try this:

$subset = str_split("my subset");
$subset = array_unique($subset);
sort($subset);

// Load font
$font_obj = Font::load("/path/to/font/file.ttf");
$font_obj->parse();

// Define subset
$font_obj->setSubset($subset);
$font_obj->reduce();

// Write new font
$tmp_name = "/path/to/font/newfile.ttf";
touch($tmp_name);
$font_obj->open($tmp_name, BinaryStream::modeReadWrite);
$font_obj->encode(["OS/2"]);
$font_obj->close();

Note that this has some limitations that may be undesirable (like lack of glyph substitution). But if you just need a straight subset it will work.