Advertisement
Guest User

big5 convert

a guest
Feb 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. # Converts Nano-X BG2UBG.KU Big5/Unicode conversion table to a format we
  2. # can use with the HZK fonts.
  3.  
  4. import struct
  5. import sys
  6.  
  7. table = open(sys.argv[1]).read()
  8.  
  9. done = set()
  10. print 'uint16_t hzk2uni[] = {'
  11.  
  12. for i in range(0, len(table) / 4 - 1):
  13.   big5, unicode = struct.unpack('<HH', table[i * 4 : i * 4 + 4])
  14.  
  15.   # decode Big5
  16.   cl = big5 & 0xff
  17.   ch = big5 >> 8
  18.   if cl < 127:
  19.     cl -= 64
  20.   else:
  21.     cl -= 98
  22.   if ch >= 0xa4:
  23.     seq = ((ch - 164) * 157) + cl
  24.     if seq > 5809:
  25.       seq -= 408;
  26.   if ch <= 0xa3:
  27.     seq = (((ch - 161) * 157) + cl) + 13094
  28.  
  29.   if not seq in done:
  30.     print '    [0x%04x] = 0x%04x,' % (seq, unicode)
  31.     done.add(seq)
  32.  
  33. print '};'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement