webfonts

subsetOTF(srcFontPath, dstFontPath, glyphNames, obfuscateNames=False, removeFeatures=False, removeKerning=False, removeHinting=False, verbose=True)

Subset an OpenType font using the FontTools subsetter.

Parameters
  • srcFontPath (str) – The path to the source OpenType font.

  • dstFontPath (str) – The path to the target subsetted OpenType font.

  • glyphNames (list) – A list of glyph names to be included in the subsetted font.

  • obfuscateNames (bool) – Obfuscate font names.

  • removeFeatures (bool) – Remove all OpenType features.

  • removeKerning (bool) – Remove all kerning.

  • removeHinting (bool) – Remove hinting data.

Returns

A boolean indicating if the subsetted font was generated successfully.

from hTools3.modules.webfonts import subsetOTF

srcFont = '/path/to/myFont.otf'
dstFont = srcFont.replace('.otf', '_subset.otf')
glyphNames = 'space exclam quotedbl numbersign dollar percent ampersand quotesingle parenleft parenright asterisk plus comma hyphen period slash zero one two three four five six seven eight nine colon semicolon less equal greater question at A B C D E F G H I J K L M N O P Q R S T U V W X Y Z bracketleft backslash bracketright asciicircum underscore grave a b c d e f g h i j k l m n o p q r s t u v w x y z braceleft bar braceright asciitilde'.split()

subsetOTF(srcFont, dstFont, glyphNames, obfuscateNames=False, removeFeatures=False, removeKerning=False, removeHinting=False, verbose=True)
encodeBase64(fontPath)

Convert a font at a given path to base64 encoding.

Parameters

fontPath (str) – The path to the source OpenType font.

Returns

A string of binary data encoded in the base64 format.

from hTools3.modules.webfonts import encodeBase64

ttfPath = '/path/to/myFont.ttf'
fontData = encodeBase64(ttfPath)

print(fontData)
fontFaceBase64Woff(fontName, fontData, fontWeight='normal', fontStyle='normal', fontFormat='woff', fontDisplay='auto', sass=False)

Generate a CSS or SASS @font-face declaration for a base64-encoded font.

Parameters
  • fontName (str) – The font-family of the font.

  • fontData (str) – The font data encoded in base64 format.

  • fontWeight (str) – The font-weight of the font. Options are normal and bold.

  • fontStyle (str) – The font-style of the font. Options are normal and italic.

  • fontFormat (str) – The format of the linked font file. Options are woff, woff2, truetype, opentype.

  • sass (bool) – Output code in sass format instead of plain CSS.

from hTools3.modules.webfonts import encodeBase64, fontFaceBase64Woff

fontPath = '/path/to/myFont.woff'
fontData = encodeBase64(fontPath)
fontFace = fontFaceBase64Woff('My Family', fontData, fontWeight='normal', fontStyle='normal', fontFormat='woff', sass=False)

print(fontFace)
sfnt2woff(fontPath, woffPath=None)

Generate a .woff file from an .otf or .ttf font.

from hTools3.modules.webfonts import sfnt2woff

fontPath = '/path/to/myFont.ttf'
woffPath = fontPath.replace('.ttf', '.woff')

sfnt2woff(fontPath, woffPath)
sfnt2woff2(fontPath, woffPath=None)

Generate a .woff2 file from an .otf or .ttf font.

from hTools3.modules.webfonts import sfnt2woff2

fontPath = '/path/to/myFont.ttf'
woff2Path = fontPath.replace('.ttf', '.woff2')

sfnt2woff2(fontPath, woff2Path)