1#! /usr/bin/env python
2
3from __future__ import print_function, division, absolute_import
4from fontTools.misc.py23 import *
5from fontTools.ttLib import TTFont
6from fontTools.ttLib.tables import otTables
7import sys
8
9if len(sys.argv) != 2:
10	print("usage: layout-features.py fontfile.ttf")
11	sys.exit(1)
12fontfile = sys.argv[1]
13if fontfile.rsplit(".", 1)[-1] == "ttx":
14	font = TTFont()
15	font.importXML(fontfile)
16else:
17	font = TTFont(fontfile)
18
19for tag in ('GSUB', 'GPOS'):
20	if not tag in font: continue
21	print("Table:", tag)
22	table = font[tag].table
23	if not table.ScriptList or not table.FeatureList: continue
24	featureRecords = table.FeatureList.FeatureRecord
25	for script in table.ScriptList.ScriptRecord:
26		print("  Script:", script.ScriptTag)
27		if not script.Script:
28			print ("    Null script.")
29			continue
30		languages = list(script.Script.LangSysRecord)
31		if script.Script.DefaultLangSys:
32			defaultlangsys = otTables.LangSysRecord()
33			defaultlangsys.LangSysTag = "default"
34			defaultlangsys.LangSys = script.Script.DefaultLangSys
35			languages.insert(0, defaultlangsys)
36		for langsys in languages:
37			print("    Language:", langsys.LangSysTag)
38			if not langsys.LangSys:
39				print ("    Null language.")
40				continue
41			features = [featureRecords[index] for index in langsys.LangSys.FeatureIndex]
42			if langsys.LangSys.ReqFeatureIndex != 0xFFFF:
43				record = featureRecords[langsys.LangSys.ReqFeatureIndex]
44				requiredfeature = otTables.FeatureRecord()
45				requiredfeature.FeatureTag = 'required(%s)' % record.FeatureTag
46				requiredfeature.Feature = record.Feature
47				features.insert(0, requiredfeature)
48			for feature in features:
49				print("      Feature:", feature.FeatureTag)
50				lookups = feature.Feature.LookupListIndex
51				print("        Lookups:", ','.join(str(l) for l in lookups))
52