import_unicode.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #*
  2. #* GRUB -- GRand Unified Bootloader
  3. #* Copyright (C) 2010 Free Software Foundation, Inc.
  4. #*
  5. #* GRUB is free software: you can redistribute it and/or modify
  6. #* it under the terms of the GNU General Public License as published by
  7. #* the Free Software Foundation, either version 3 of the License, or
  8. #* (at your option) any later version.
  9. #*
  10. #* GRUB is distributed in the hope that it will be useful,
  11. #* but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. #* GNU General Public License for more details.
  14. #*
  15. #* You should have received a copy of the GNU General Public License
  16. #* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. #*
  18. import re
  19. import sys
  20. if len (sys.argv) < 3:
  21. print ("Usage: %s SOURCE DESTINATION" % sys.argv[0])
  22. exit (0)
  23. infile = open (sys.argv[3], "r")
  24. joining = {}
  25. for line in infile:
  26. line = re.sub ("#.*$", "", line)
  27. line = line.replace ("\n", "")
  28. line = line.replace (" ", "")
  29. if len (line) == 0 or line[0] == '\n':
  30. continue
  31. sp = line.split (";")
  32. curcode = int (sp[0], 16)
  33. if sp[2] == "U":
  34. joining[curcode] = "NONJOINING"
  35. elif sp[2] == "L":
  36. joining[curcode] = "LEFT"
  37. elif sp[2] == "R":
  38. joining[curcode] = "RIGHT"
  39. elif sp[2] == "D":
  40. joining[curcode] = "DUAL"
  41. elif sp[2] == "C":
  42. joining[curcode] = "CAUSING"
  43. else:
  44. print ("Unknown joining type '%s'" % sp[2])
  45. exit (1)
  46. infile.close ()
  47. infile = open (sys.argv[1], "r")
  48. outfile = open (sys.argv[4], "w")
  49. outfile.write ("#include <grub/unicode.h>\n")
  50. outfile.write ("\n")
  51. outfile.write ("struct grub_unicode_compact_range grub_unicode_compact[] = {\n")
  52. begincode = -2
  53. lastcode = -2
  54. lastbiditype = "X"
  55. lastmirrortype = False
  56. lastcombtype = -1
  57. arabicsubst = {}
  58. for line in infile:
  59. sp = line.split (";")
  60. curcode = int (sp[0], 16)
  61. curcombtype = int (sp[3], 10)
  62. curbiditype = sp[4]
  63. curmirrortype = (sp[9] == "Y")
  64. if curcombtype <= 255 and curcombtype >= 253:
  65. print ("UnicodeData.txt uses combination type %d. Conflict." \
  66. % curcombtype)
  67. raise
  68. if sp[2] != "Lu" and sp[2] != "Ll" and sp[2] != "Lt" and sp[2] != "Lm" \
  69. and sp[2] != "Lo"\
  70. and sp[2] != "Me" and sp[2] != "Mc" and sp[2] != "Mn" \
  71. and sp[2] != "Nd" and sp[2] != "Nl" and sp[2] != "No" \
  72. and sp[2] != "Pc" and sp[2] != "Pd" and sp[2] != "Ps" \
  73. and sp[2] != "Pe" and sp[2] != "Pi" and sp[2] != "Pf" \
  74. and sp[2] != "Po" \
  75. and sp[2] != "Sm" and sp[2] != "Sc" and sp[2] != "Sk" \
  76. and sp[2] != "So"\
  77. and sp[2] != "Zs" and sp[2] != "Zl" and sp[2] != "Zp" \
  78. and sp[2] != "Cc" and sp[2] != "Cf" and sp[2] != "Cs" \
  79. and sp[2] != "Co":
  80. print ("WARNING: Unknown type %s" % sp[2])
  81. if curcombtype == 0 and sp[2] == "Me":
  82. curcombtype = 253
  83. if curcombtype == 0 and sp[2] == "Mc":
  84. curcombtype = 254
  85. if curcombtype == 0 and sp[2] == "Mn":
  86. curcombtype = 255
  87. if (curcombtype >= 2 and curcombtype <= 6) \
  88. or (curcombtype >= 37 and curcombtype != 84 and curcombtype != 91 and curcombtype != 103 and curcombtype != 107 and curcombtype != 118 and curcombtype != 122 and curcombtype != 129 and curcombtype != 130 and curcombtype != 132 and curcombtype != 202 and \
  89. curcombtype != 214 and curcombtype != 216 and \
  90. curcombtype != 218 and curcombtype != 220 and \
  91. curcombtype != 222 and curcombtype != 224 and curcombtype != 226 and curcombtype != 228 and \
  92. curcombtype != 230 and curcombtype != 232 and curcombtype != 233 and \
  93. curcombtype != 234 and \
  94. curcombtype != 240 and curcombtype != 253 and \
  95. curcombtype != 254 and curcombtype != 255):
  96. print ("WARNING: Unknown combining type %d" % curcombtype)
  97. if curcode in joining:
  98. curjoin = joining[curcode]
  99. elif sp[2] == "Me" or sp[2] == "Mn" or sp[2] == "Cf":
  100. curjoin = "TRANSPARENT"
  101. else:
  102. curjoin = "NONJOINING"
  103. if sp[1].startswith ("ARABIC LETTER "):
  104. arabname = sp[1][len ("ARABIC LETTER "):]
  105. form = 0
  106. if arabname.endswith (" ISOLATED FORM"):
  107. arabname = arabname[0:len (arabname) - len (" ISOLATED FORM")]
  108. form = 1
  109. if arabname.endswith (" FINAL FORM"):
  110. arabname = arabname[0:len (arabname) - len (" FINAL FORM")]
  111. form = 2
  112. if arabname.endswith (" MEDIAL FORM"):
  113. arabname = arabname[0:len (arabname) - len (" MEDIAL FORM")]
  114. form = 3
  115. if arabname.endswith (" INITIAL FORM"):
  116. arabname = arabname[0:len (arabname) - len (" INITIAL FORM")]
  117. form = 4
  118. if arabname not in arabicsubst:
  119. arabicsubst[arabname]={}
  120. arabicsubst[arabname][form] = curcode;
  121. if form == 0:
  122. arabicsubst[arabname]['join'] = curjoin
  123. if lastcode + 1 != curcode or curbiditype != lastbiditype \
  124. or curcombtype != lastcombtype or curmirrortype != lastmirrortype \
  125. or curjoin != lastjoin:
  126. if begincode != -2 and (lastbiditype != "L" or lastcombtype != 0 or \
  127. lastmirrortype):
  128. outfile.write (("{0x%x, 0x%x, GRUB_BIDI_TYPE_%s, %d, %d, GRUB_JOIN_TYPE_%s},\n" \
  129. % (begincode, lastcode - begincode + 1, \
  130. lastbiditype, \
  131. lastcombtype, lastmirrortype, \
  132. lastjoin)))
  133. if lastcode - begincode + 1 >= 0x200:
  134. print ("Too long range")
  135. raise
  136. begincode = curcode
  137. lastcode = curcode
  138. lastjoin = curjoin
  139. lastbiditype = curbiditype
  140. lastcombtype = curcombtype
  141. lastmirrortype = curmirrortype
  142. if lastbiditype != "L" or lastcombtype != 0 or lastmirrortype:
  143. outfile.write (("{0x%x, 0x%x, GRUB_BIDI_TYPE_%s, %d, %d, GRUB_JOIN_TYPE_%s},\n" \
  144. % (begincode, lastcode, lastbiditype, lastcombtype, \
  145. lastmirrortype, lastjoin)))
  146. outfile.write ("{0, 0, 0, 0, 0, 0},\n")
  147. outfile.write ("};\n")
  148. infile.close ()
  149. infile = open (sys.argv[2], "r")
  150. outfile.write ("struct grub_unicode_bidi_pair grub_unicode_bidi_pairs[] = {\n")
  151. for line in infile:
  152. line = re.sub ("#.*$", "", line)
  153. line = line.replace ("\n", "")
  154. line = line.replace (" ", "")
  155. if len (line) == 0 or line[0] == '\n':
  156. continue
  157. sp = line.split (";")
  158. code1 = int (sp[0], 16)
  159. code2 = int (sp[1], 16)
  160. outfile.write ("{0x%x, 0x%x},\n" % (code1, code2))
  161. outfile.write ("{0, 0},\n")
  162. outfile.write ("};\n")
  163. infile.close ()
  164. outfile.write ("struct grub_unicode_arabic_shape grub_unicode_arabic_shapes[] = {\n ")
  165. for x in sorted(arabicsubst):
  166. try:
  167. if arabicsubst[x]['join'] == "DUAL":
  168. outfile.write ("{0x%x, 0x%x, 0x%x, 0x%x, 0x%x},\n " % (arabicsubst[x][0], arabicsubst[x][1], arabicsubst[x][2], arabicsubst[x][3], arabicsubst[x][4]))
  169. elif arabicsubst[x]['join'] == "RIGHT":
  170. outfile.write ("{0x%x, 0x%x, 0x%x, 0x%x, 0x%x},\n " % (arabicsubst[x][0], arabicsubst[x][1], arabicsubst[x][2], 0, 0))
  171. elif arabicsubst[x]['join'] == "LEFT":
  172. outfile.write ("{0x%x, 0x%x, 0x%x, 0x%x, 0x%x},\n " % (arabicsubst[x][0], arabicsubst[x][1], 0, 0, arabicsubst[x][4]))
  173. except:
  174. pass
  175. outfile.write ("{0, 0, 0, 0, 0},\n")
  176. outfile.write ("};\n")
  177. outfile.close ()