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.73k stars 256 forks source link

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

Closed wilbur-yu closed 4 years ago

wilbur-yu commented 4 years ago

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.

bsweeney commented 4 years ago

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.