summaryrefslogtreecommitdiff
path: root/fc-blanks/fc-blanks.py
blob: 410178a6796e1906237712ae0582c0cf9788c00d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#! /usr/bin/python

from __future__ import absolute_import
from __future__ import print_function
try:
    from urllib2 import urlopen
    from urllib2 import URLError
except ImportError:
    from urllib.request import urlopen
    from urllib.error import URLError

import sys
import os
from lxml import html
from six.moves import range

datafile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'list-unicodeset.html')
try:
    fp = urlopen('http://unicode.org/cldr/utility/list-unicodeset.jsp?a=[%3AGC%3DZs%3A][%3ADI%3A]&abb=on&ucd=on&esc=on&g')
    data = fp.read()
    fp.close()
    # check before writing if data isn't corrupted.
    dom = html.fromstring(data)
    x = dom.xpath('/html/body/form/p/text()')
    p = x[1]
    fp = open(datafile, 'wb');
    fp.write(data);
    fp.close();
except (URLError, IndexError):
    # fall back reading the static data in repo
    try:
        fp = open(datafile)
        data = fp.read()
        fp.close()
    except IOError:
        sys.stderr.write("Error: No static data to generate the blank data. please make sure the network connection is reachable to Unicode.org\n")
        sys.exit(1)

dom = html.fromstring(data)
x = dom.xpath('/html/body/form/p/text()')
p = x[1]
if p[0] == '[' and p[-1] == ']':
    p = p.replace('[', '').replace(']', '')
else:
    sys.exit(1)
fescape = False
funicode = False
frange = False
fprocess = False
v = 0
vbegin = 0
vend = 0
n = 0
l = []

def insert(db, begin, end):
    db.append([begin, end])

for i in p:
    if i == '\\':
        if n > 0:
            if frange == True and funicode == True:
                vend = v
                insert(l, vbegin, vend)
                fprocess = True
            elif funicode == True:
                vbegin = v
                vend = v
                insert(l, vbegin, vend)
                fprocess = True
        funicode = False
        fescape = True
    elif i.lower() == 'u' and fescape == True:
        funicode = True
        fescape = False
    elif i >= '0' and i <= '9' or i.lower() >= 'a' and i.lower() <= 'f':
        if fescape == True:
            raise RuntimeError("Unexpected escape code")
        if funicode == True:
            v <<= 4
            v += int(i, 16)
        else:
            raise RuntimeError("Unable to parse Unicode")
    elif i == ' ':
        if fescape == True:
            funicode = True
            fescape = False
            v = 0x20
        if frange == True and funicode == True:
            vend = v
            insert(l, vbegin, vend)
            fprocess = True
        elif funicode == True:
            vbegin = v
            vend = v
            insert(l, vbegin, vend)
            fprocess = True
        funicode = False
        frange = False
    elif i == '-':
        if fescape == True:
            raise RuntimeError("Unexpected escape code")
        vbegin = v
        v = 0
        funicode = False
        frange = True
    else:
        raise RuntimeError("Unable to parse Unicode: %s" % i)

    if fprocess == True:
        vbegin = 0
        vend = 0
        v = 0
        fprocess = False
        funicode = False
        frange = False
    n += 1

if frange == True and funicode == True:
    vend = v
    insert(l, vbegin, vend)
elif funicode == True:
    vbegin = vend = v
    insert(l, vbegin, vend)

ncode = 0
for i in l:
    ncode += (i[1] - i[0] + 1)

a = int(x[0].split(' ')[0].replace(',', ''))
if a != ncode:
    sys.stderr.write("Unexpected the amount of code points: %d (expected %d)\n" % (ncode, a))
    sys.exit(1)

# exception; BRAILLE PATTERN BLANK
insert(l, 0x2800, 0x2800)

while True:
    s = sys.stdin.readline().rstrip()
    if s == "@@@":
        break
    print(s)

print("static FcChar32 _fcBlanks[%s] = {" % (ncode + 1))
k = 0
for i in sorted(l, key=lambda a: a[0]):
    for j in range(i[0], i[1] + 1):
        if k != 0:
            print(",")
        print("    0x%04x" % j, end=' ')
        k += 1

print("};")
print('''
static FcBlanks fcBlanks = {
    %s,
    -1,
    _fcBlanks
};
''' % (ncode + 1))