#!/usr/bin/env python

# Wrotten by Vincent Wei for MiniGUI 3.4
# Create on 2019-03-07

from __future__ import print_function, division, absolute_import

import io, sys

CHAR_STOP = 0xFF
NR_UNICODE_CHARS = CHAR_STOP + 1

#if len (sys.argv) != 4:
#    print ("usage: ./gen-sbc-bidi-tables.py", file=sys.stderr)
#    sys.exit (1)

filenames = ['ucd/UnicodeData.txt']

files = [io.open (x, encoding='utf-8') for x in filenames]

headers = []
headers.insert (0, ["UnicodeData.txt does not have a header."])

# Read fileds[] in UnicodeData.txt to data

data = [{} for f in files]
for i, f in enumerate (files):
    for line in f:

        j = line.find ('#')
        if j >= 0:
            line = line[:j]

        fields = [x.strip () for x in line.split (';')]
        if len (fields) == 1:
            continue

        u = int (fields[0], 16)
        if u > CHAR_STOP:
            break;

        data[i][u] = fields[4]

bidi_types = ["BIDI_TYPE_LTR" for i in range(0, NR_UNICODE_CHARS)]

# Fill bidi_types
d = data[0]
for u, v in d.items():
    v = v.strip()
    if cmp(v, 'L') == 0:
        bidi_types[u] = 'BIDI_TYPE_LTR'
    elif cmp(v, 'R') == 0:
        bidi_types[u] = 'BIDI_TYPE_RTL'
    elif cmp(v, 'EN') == 0:
        bidi_types[u] = 'BIDI_TYPE_EN'
    elif cmp(v, 'ES') == 0:
        bidi_types[u] = 'BIDI_TYPE_ES'
    elif cmp(v, 'ET') == 0:
        bidi_types[u] = 'BIDI_TYPE_ET'
    elif cmp(v, 'AN') == 0:
        bidi_types[u] = 'BIDI_TYPE_AN'
    elif cmp(v, 'CS') == 0:
        bidi_types[u] = 'BIDI_TYPE_CS'
    elif cmp(v, 'B') == 0:
        bidi_types[u] = 'BIDI_TYPE_BS'
    elif cmp(v, 'S') == 0:
        bidi_types[u] = 'BIDI_TYPE_SS'
    elif cmp(v, 'WS') == 0:
        bidi_types[u] = 'BIDI_TYPE_WS'
    elif cmp(v, 'ON') == 0:
        bidi_types[u] = 'BIDI_TYPE_ON'
    elif cmp(v, 'BN') == 0:
        bidi_types[u] = 'BIDI_TYPE_BN'
    elif cmp(v, 'NSM') == 0:
        bidi_types[u] = 'BIDI_TYPE_NSM'
    elif cmp(v, 'AL') == 0:
        bidi_types[u] = 'BIDI_TYPE_AL'
    elif cmp(v, 'LRO') == 0:
        bidi_types[u] = 'BIDI_TYPE_LRO'
    elif cmp(v, 'RLO') == 0:
        bidi_types[u] = 'BIDI_TYPE_RLO'
    elif cmp(v, 'LRE') == 0:
        bidi_types[u] = 'BIDI_TYPE_LRE'
    elif cmp(v, 'RLE') == 0:
        bidi_types[u] = 'BIDI_TYPE_RLE'
    elif cmp(v, 'PDF') == 0:
        bidi_types[u] = 'BIDI_TYPE_PDF'
    elif cmp(v, 'LRI') == 0:
        bidi_types[u] = 'BIDI_TYPE_LRI'
    elif cmp(v, 'RLI') == 0:
        bidi_types[u] = 'BIDI_TYPE_RLI'
    elif cmp(v, 'FSI') == 0:
        bidi_types[u] = 'BIDI_TYPE_FSI'
    elif cmp(v, 'PDI') == 0:
        bidi_types[u] = 'BIDI_TYPE_PDI'
    else:
        print ("Unknown BIDI type: %s" % v)

print ("/* == Start of generated table == */")
print ("/*")
print (" * The following table is generated by running:")
print (" *")
print (" *   ./gen-ascii-bidi-tables.py")
print (" *")
print (" * on ucd/UnicodeData.txt files with these headers:")
print (" *")
for h in headers:
    for l in h:
        print (" * %s" % (l.strip()))
print (" */")
print ()

print ("#ifndef _MGFONT_ISO8859_BIDI_TABLES_H")
print ("#define _MGFONT_ISO8859_BIDI_TABLES_H")
print ("")
print ("static const BidiType __mg_iso8859_1_bidi_char_type_map[] =")
print ("{")

for i in range(0, 0x100, 4):
    print ("    %s, %s, %s, %s," % (bidi_types[i], bidi_types[i + 1], bidi_types[i + 2], bidi_types[i + 3], ))

print ("};")
print ()
print ("#endif /* !_MGFONT_ISO8859_BIDI_TABLES_H */")

print ()
print ("/* == End of generated table == */")

