Subsetting large fonts

205 views
Skip to first unread message

Kazuo Teramoto

unread,
Nov 3, 2015, 4:45:06 AM11/3/15
to anki-a...@googlegroups.com
Hi!

I like to share a small tip, is a little command-line focused...

I had problems with large Japanese fonts in my slow tablet. The cards
took a long time to fully render.

To solve this I used pyftsubset from fonttools
(https://github.com/behdad/fonttools) to subset the fonts, i.e., create
a new font that only include the used glyphs. In my case this reduced
the total font size from 38Mb to 2.8Mb! Now AnkiDroid render each card
in an instant.

The nice thing is that pyftsubset accepts a utf8 text file as input and
it will include all the glyphs needed to render the file. A simple way
to use it is to export all the notes in plain text from Anki desktop and
pass this file to pyftsubset like so:

$ pyftsubset _kanjistrokeorders.ttf --text-file=/home/kazuo/personal/anki/All\ Decks.txt

It will, per default, generate a new font file
_kanjistrokeorders.ttf.subset

If you need extra glyphs, e.g. they are used in the template but not on
the notes, you can also includes the --text= option. And you can reduce
the set if appropriated (e.g., for 3 of 4 fonts I only need the glyphs
of the first field).

I hope this can help, at least, a small subset of the AnkiDroid users.

Regards,
Kazuo Teramoto

Mike Morrison

unread,
Nov 3, 2015, 7:44:47 AM11/3/15
to anki-a...@googlegroups.com
Awesome tip, thanks!
> --
> You received this message because you are subscribed to the Google Groups "AnkiDroid" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to anki-android...@googlegroups.com.
> To post to this group, send email to anki-a...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/anki-android/20151103094456.24266.40463%40latifa.
> For more options, visit https://groups.google.com/d/optout.

Flavio Lerda

unread,
Nov 3, 2015, 11:17:43 AM11/3/15
to anki-a...@googlegroups.com
Very nice suggestion!

We should add that to the FAQ. Maybe phrasing it as something that would fit in https://github.com/ankidroid/Anki-Android/wiki/FAQ?

Here is a first cut of what the text might look good there.
Please feel free to make corrections (since I did not actually attempt to use the tool -- yet).

(Advanced) My font files are too large. Is there a way to make them smaller?

There is a way but it is rather advanced: it involves running some programs on the command line, so if you are not comfortable with that, probably you want to stick with what you have or find an alternative, smaller font.

Most of the time, you will have a font that includes characters besides the ones you actually need: if, for example, you use a Japanese or Chinese font, there are lots more character than what your notes are likely to include. This might also work for latin-alphabet languages, if the font that you used actually contains additional characters for other languages that you are not using in your cards.

It is possible to use a tool called pyftsubset (from https://github.com/behdad/fonttools) to generate a font containing only a subset of the characters in the original font, which will reduce the overall size of the font.

In order to remove the unused characters, you need to get a list of the characters you actually do use. The easiest way is to export your cards as plain text using the desktop app into a file and pass that to the tool. Alternatively, if you only use the font for a specific set of fields, you can extract only the content of those fields, for example, by exporting to Excel format and only keeping the columns you are interested in and then export to text from Excel. Either way, you should end up with a text file (e.g., cards.txt) that contains all the characters you need.

After that you can run the tool as follows (assuming your font file is called font.ttf):

$ subset font.ttf --text-file=cards.txt

and obtain a new file (called font.ttf.subset by default) that you can then rename and use in place of the original font.

Note that if you add new content, you might need to repeat the process, so this is not recommended if you are actively growing the set of characters in your deck, since you would have to repeat the steps above each time a new character is used.

Reply all
Reply to author
Forward
0 new messages