gen-arabic-joining-list.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. """usage: ./gen-arabic-joining-table.py ArabicShaping.txt Scripts.txt
  3. Input files:
  4. * https://unicode.org/Public/UCD/latest/ucd/ArabicShaping.txt
  5. * https://unicode.org/Public/UCD/latest/ucd/Scripts.txt
  6. """
  7. import os.path, sys
  8. if len (sys.argv) != 3:
  9. sys.exit (__doc__)
  10. files = [open (x, encoding='utf-8') for x in sys.argv[1:]]
  11. headers = [[f.readline (), f.readline ()] for f in files]
  12. while files[0].readline ().find ('##################') < 0:
  13. pass
  14. def read (f):
  15. mapping = {}
  16. for line in f:
  17. j = line.find ('#')
  18. if j >= 0:
  19. line = line[:j]
  20. fields = [x.strip () for x in line.split (';')]
  21. if len (fields) == 1:
  22. continue
  23. uu = fields[0].split ('..')
  24. start = int (uu[0], 16)
  25. if len (uu) == 1:
  26. end = start
  27. else:
  28. end = int (uu[1], 16)
  29. t = fields[1]
  30. for u in range (start, end + 1):
  31. mapping[u] = t
  32. return mapping
  33. def read_joining_uu (f):
  34. values = set ()
  35. for line in f:
  36. if line[0] == '#':
  37. continue
  38. fields = [x.strip () for x in line.split (';')]
  39. if len (fields) == 1:
  40. continue
  41. if fields[2] in {'T', 'U'}:
  42. continue
  43. values.add (int (fields[0], 16))
  44. return sorted (values)
  45. def print_has_arabic_joining (scripts, joining_uu):
  46. print ("static bool")
  47. print ("has_arabic_joining (hb_script_t script)")
  48. print ("{")
  49. print (" /* List of scripts that have data in arabic-table. */")
  50. print (" switch ((int) script)")
  51. print (" {")
  52. for script in sorted ({scripts[u] for u in joining_uu if scripts[u] not in {'Common', 'Inherited'}}):
  53. print (" case HB_SCRIPT_{}:".format (script.upper ()))
  54. print (" return true;")
  55. print ()
  56. print (" default:")
  57. print (" return false;")
  58. print (" }")
  59. print ("}")
  60. print ()
  61. print ("/* == Start of generated function == */")
  62. print ("/*")
  63. print (" * The following function is generated by running:")
  64. print (" *")
  65. print (" * ./gen-arabic-joining-list.py ArabicShaping.txt Scripts.txt")
  66. print (" *")
  67. print (" * on files with these headers:")
  68. print (" *")
  69. for h in headers:
  70. for l in h:
  71. print (" * %s" % (l.strip ()))
  72. print (" */")
  73. print ()
  74. print ("#ifndef HB_OT_SHAPER_ARABIC_JOINING_LIST_HH")
  75. print ("#define HB_OT_SHAPER_ARABIC_JOINING_LIST_HH")
  76. print ()
  77. print_has_arabic_joining (read (files[1]), read_joining_uu (files[0]))
  78. print ()
  79. print ("#endif /* HB_OT_SHAPER_ARABIC_JOINING_LIST_HH */")
  80. print ()
  81. print ("/* == End of generated function == */")