Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subset Font By Specific Glyphs

I have a 14MB TTF containing mostly Simplified Chinese characters.

I'd like to reduce the size by creating a subset which only contains the specific characters within an HTML page.

So, ideally, I'd like to pass a (Linux) program an block of text and have it recreate the font based on the characters contained.

E.g.

 ./magic-font-squisher input.tff "ABC123水小长"

or

 ./magic-font-squisher input.tff /path/to/test.html

The new font would then only have those 9 characters present.

like image 525
Terence Eden Avatar asked May 21 '13 15:05

Terence Eden


2 Answers

You can do it by scripting FontForge. This works, though it can probably be made more clever or wrapped in a containing script.

#!/usr/bin/env fontforge
Open($1); # first param
SelectAll();
SelectFewer(0u41, 0u43, 0u31, 0u33); # a range
SelectFewer(0u6c34); # or a single codepoint
SelectFewer(0u5c0f);
SelectFewer(0u957f);
DetachAndRemoveGlyphs();
Save($2); # second param
Quit(0);

I had to define FONTFORGE_LANGUAGE before running it:

FONTFORGE_LANGUAGE=ff ./squish source.ttf squished.ttf
like image 66
threedaymonk Avatar answered Nov 04 '22 00:11

threedaymonk


Have used https://bitbucket.org/philip/font-optimizer/src

./subset.pl --chars="ABC 123 水小长" input.ttf output.ttf

Which is exactly what I want.

How I Found It

Google Font Directory contains a subset tool. The README says

--string=: Generate subset for just the specified string. Useful for creating a menu subset. Generally we use subset.pl from Font Optimizer for this, however.

A search for Font Optimizer took me to a demo site which allows you to test the script.

There is also a forked GitHub repo which has better documentation.

like image 5
Terence Eden Avatar answered Nov 04 '22 01:11

Terence Eden