6

Currenly, I'm using fonttools(https://github.com/fonttools/fonttools) to convert font file ttf to woff2 by ttx command with 2 steps

  • convert ttf to ttx
  • then convert ttx to woff2

But it's too slow and ttx file to big, is there any way to convert ttf to woff2 directly by using fonttools to improve performance?

Quan Vo
  • 1,639
  • 2
  • 13
  • 28

3 Answers3

12

With fonttools installed in your Python (virtualenv, pipenv, etc):

$ python
>>> from fontTools.ttLib import TTFont
>>> f = TTFont('path/to/your/file.otf')
>>> f.flavor='woff2'
>>> f.save('path/to/your/file.woff2')

NOTE: you might need to install other fontTools dependencies ('brotli', others) to allow saving with flavor=woff2 to work correctly.

djangodude
  • 5,362
  • 3
  • 27
  • 39
  • 6
    Or as a shell oneliner: python -c "from fontTools.ttLib import TTFont; f = TTFont('path/to/your/file.otf');f.flavor='woff2';f.save('path/to/your/file.woff2')" – BertD Apr 18 '21 at 21:29
4
pip install --user --upgrade fonttools[woff]
python3
>>> from fontTools.ttLib.woff2 import compress
>>> compress('filename.otf','filename.woff2')
  • This will [only] accept .otf and .ttf
  • It produces smaller files than other techniques (ie: woff2-tools)

https://fonttools.readthedocs.io/en/latest/ttLib/woff2.html?highlight=ttLib.woff2%3A#fontTools.ttLib.woff2.compress

wattahay
  • 161
  • 6
2

There's Google's woff2 CLI, so you can do this in command line instead of manually writing some scripts.

Minh Nghĩa
  • 854
  • 1
  • 11
  • 28