table_API_readme.txt 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. This folder is a subpackage of ttLib. Each module here is a
  2. specialized TT/OT table converter: they can convert raw data
  3. to Python objects and vice versa. Usually you don't need to
  4. use the modules directly: they are imported and used
  5. automatically when needed by ttLib.
  6. If you are writing you own table converter the following is
  7. important.
  8. The modules here have pretty strange names: this is due to the
  9. fact that we need to map TT table tags (which are case sensitive)
  10. to filenames (which on Mac and Win aren't case sensitive) as well
  11. as to Python identifiers. The latter means it can only contain
  12. [A-Za-z0-9_] and cannot start with a number.
  13. ttLib provides functions to expand a tag into the format used here:
  14. >>> from fontTools import ttLib
  15. >>> ttLib.tagToIdentifier("FOO ")
  16. 'F_O_O_'
  17. >>> ttLib.tagToIdentifier("cvt ")
  18. '_c_v_t'
  19. >>> ttLib.tagToIdentifier("OS/2")
  20. 'O_S_2f_2'
  21. >>> ttLib.tagToIdentifier("glyf")
  22. '_g_l_y_f'
  23. >>>
  24. And vice versa:
  25. >>> ttLib.identifierToTag("F_O_O_")
  26. 'FOO '
  27. >>> ttLib.identifierToTag("_c_v_t")
  28. 'cvt '
  29. >>> ttLib.identifierToTag("O_S_2f_2")
  30. 'OS/2'
  31. >>> ttLib.identifierToTag("_g_l_y_f")
  32. 'glyf'
  33. >>>
  34. Eg. the 'glyf' table converter lives in a Python file called:
  35. _g_l_y_f.py
  36. The converter itself is a class, named "table_" + expandedtag. Eg:
  37. class table__g_l_y_f:
  38. etc.
  39. Note that if you _do_ need to use such modules or classes manually,
  40. there are two convenient API functions that let you find them by tag:
  41. >>> ttLib.getTableModule('glyf')
  42. <module 'ttLib.tables._g_l_y_f'>
  43. >>> ttLib.getTableClass('glyf')
  44. <class ttLib.tables._g_l_y_f.table__g_l_y_f at 645f400>
  45. >>>
  46. You must subclass from DefaultTable.DefaultTable. It provides some default
  47. behavior, as well as a constructor method (__init__) that you don't need to
  48. override.
  49. Your converter should minimally provide two methods:
  50. class table_F_O_O_(DefaultTable.DefaultTable): # converter for table 'FOO '
  51. def decompile(self, data, ttFont):
  52. # 'data' is the raw table data. Unpack it into a
  53. # Python data structure.
  54. # 'ttFont' is a ttLib.TTfile instance, enabling you to
  55. # refer to other tables. Do ***not*** keep a reference to
  56. # it: it will cause a circular reference (ttFont saves
  57. # a reference to us), and that means we'll be leaking
  58. # memory. If you need to use it in other methods, just
  59. # pass it around as a method argument.
  60. def compile(self, ttFont):
  61. # Return the raw data, as converted from the Python
  62. # data structure.
  63. # Again, 'ttFont' is there so you can access other tables.
  64. # Same warning applies.
  65. If you want to support TTX import/export as well, you need to provide two
  66. additional methods:
  67. def toXML(self, writer, ttFont):
  68. # XXX
  69. def fromXML(self, (name, attrs, content), ttFont):
  70. # XXX