| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Original script modified in November 2003 to take advantage of |
| 4 |
# the character-validation range routines, and updated to the |
| 5 |
# current Unicode information (Version 4.0.1) |
| 6 |
# |
| 7 |
# NOTE: there is an 'alias' facility for blocks which are not present in |
| 8 |
# the current release, but are needed for ABI compatibility. This |
| 9 |
# must be accomplished MANUALLY! Please see the comments below under |
| 10 |
# 'blockAliases' |
| 11 |
# |
| 12 |
import sys |
| 13 |
import string |
| 14 |
import rangetab |
| 15 |
|
| 16 |
# |
| 17 |
# blockAliases is a small hack - it is used for mapping block names which |
| 18 |
# were were used in the 3.1 release, but are missing or changed in the current |
| 19 |
# release. The format is "OldBlockName:NewBlockName1[,NewBlockName2[,...]]" |
| 20 |
blockAliases = [] |
| 21 |
blockAliases.append("CombiningMarksforSymbols:CombiningDiacriticalMarksforSymbols") |
| 22 |
blockAliases.append("Greek:GreekandCoptic") |
| 23 |
blockAliases.append("PrivateUse:PrivateUseArea,SupplementaryPrivateUseArea-A," + |
| 24 |
"SupplementaryPrivateUseArea-B") |
| 25 |
|
| 26 |
# minTableSize gives the minimum number of ranges which must be present |
| 27 |
# before a range table is produced. If there are less than this |
| 28 |
# number, inline comparisons are generated |
| 29 |
minTableSize = 8 |
| 30 |
|
| 31 |
blockfile = "Blocks-4.0.1.txt" |
| 32 |
catfile = "UnicodeData-4.0.1.txt" |
| 33 |
|
| 34 |
|
| 35 |
# |
| 36 |
# Now process the "blocks" file, reducing it to a dictionary |
| 37 |
# indexed by blockname, containing a tuple with the applicable |
| 38 |
# block range |
| 39 |
# |
| 40 |
BlockNames = {} |
| 41 |
try: |
| 42 |
blocks = open(blockfile, "r") |
| 43 |
except: |
| 44 |
print("Missing %s, aborting ..." % blockfile) |
| 45 |
sys.exit(1) |
| 46 |
|
| 47 |
for line in blocks.readlines(): |
| 48 |
if line[0] == '#': |
| 49 |
continue |
| 50 |
line = line.strip() |
| 51 |
if line == '': |
| 52 |
continue |
| 53 |
try: |
| 54 |
fields = line.split(';') |
| 55 |
range = fields[0].strip() |
| 56 |
(start, end) = range.split("..") |
| 57 |
name = fields[1].strip() |
| 58 |
name = name.replace(' ', '') |
| 59 |
except: |
| 60 |
print("Failed to process line: %s" % (line)) |
| 61 |
continue |
| 62 |
start = int(start, 16) |
| 63 |
end = int(end, 16) |
| 64 |
try: |
| 65 |
BlockNames[name].append((start, end)) |
| 66 |
except: |
| 67 |
BlockNames[name] = [(start, end)] |
| 68 |
blocks.close() |
| 69 |
print("Parsed %d blocks descriptions" % (len(BlockNames.keys()))) |
| 70 |
|
| 71 |
for block in blockAliases: |
| 72 |
alias = block.split(':') |
| 73 |
alist = alias[1].split(',') |
| 74 |
for comp in alist: |
| 75 |
if comp in BlockNames: |
| 76 |
if alias[0] not in BlockNames: |
| 77 |
BlockNames[alias[0]] = [] |
| 78 |
for r in BlockNames[comp]: |
| 79 |
BlockNames[alias[0]].append(r) |
| 80 |
else: |
| 81 |
print("Alias %s: %s not in Blocks" % (alias[0], comp)) |
| 82 |
continue |
| 83 |
|
| 84 |
# |
| 85 |
# Next process the Categories file. This is more complex, since |
| 86 |
# the file is in code sequence, and we need to invert it. We use |
| 87 |
# a dictionary with index category-name, with each entry containing |
| 88 |
# all the ranges (codepoints) of that category. Note that category |
| 89 |
# names comprise two parts - the general category, and the "subclass" |
| 90 |
# within that category. Therefore, both "general category" (which is |
| 91 |
# the first character of the 2-character category-name) and the full |
| 92 |
# (2-character) name are entered into this dictionary. |
| 93 |
# |
| 94 |
try: |
| 95 |
data = open(catfile, "r") |
| 96 |
except: |
| 97 |
print("Missing %s, aborting ..." % catfile) |
| 98 |
sys.exit(1) |
| 99 |
|
| 100 |
nbchar = 0; |
| 101 |
Categories = {} |
| 102 |
for line in data.readlines(): |
| 103 |
if line[0] == '#': |
| 104 |
continue |
| 105 |
line = line.strip() |
| 106 |
if line == '': |
| 107 |
continue |
| 108 |
try: |
| 109 |
fields = line.split(';') |
| 110 |
point = fields[0].strip() |
| 111 |
value = 0 |
| 112 |
while point != '': |
| 113 |
value = value * 16 |
| 114 |
if point[0] >= '0' and point[0] <= '9': |
| 115 |
value = value + ord(point[0]) - ord('0') |
| 116 |
elif point[0] >= 'A' and point[0] <= 'F': |
| 117 |
value = value + 10 + ord(point[0]) - ord('A') |
| 118 |
elif point[0] >= 'a' and point[0] <= 'f': |
| 119 |
value = value + 10 + ord(point[0]) - ord('a') |
| 120 |
point = point[1:] |
| 121 |
name = fields[2] |
| 122 |
except: |
| 123 |
print("Failed to process line: %s" % (line)) |
| 124 |
continue |
| 125 |
|
| 126 |
nbchar = nbchar + 1 |
| 127 |
# update entry for "full name" |
| 128 |
try: |
| 129 |
Categories[name].append(value) |
| 130 |
except: |
| 131 |
try: |
| 132 |
Categories[name] = [value] |
| 133 |
except: |
| 134 |
print("Failed to process line: %s" % (line)) |
| 135 |
# update "general category" name |
| 136 |
try: |
| 137 |
Categories[name[0]].append(value) |
| 138 |
except: |
| 139 |
try: |
| 140 |
Categories[name[0]] = [value] |
| 141 |
except: |
| 142 |
print("Failed to process line: %s" % (line)) |
| 143 |
|
| 144 |
data.close() |
| 145 |
print("Parsed %d char generating %d categories" % (nbchar, len(Categories.keys()))) |
| 146 |
|
| 147 |
# |
| 148 |
# The data is now all read. Time to process it into a more useful form. |
| 149 |
# |
| 150 |
# reduce the number list into ranges |
| 151 |
for cat in Categories.keys(): |
| 152 |
list = Categories[cat] |
| 153 |
start = -1 |
| 154 |
prev = -1 |
| 155 |
end = -1 |
| 156 |
ranges = [] |
| 157 |
for val in list: |
| 158 |
if start == -1: |
| 159 |
start = val |
| 160 |
prev = val |
| 161 |
continue |
| 162 |
elif val == prev + 1: |
| 163 |
prev = val |
| 164 |
continue |
| 165 |
elif prev == start: |
| 166 |
ranges.append((prev, prev)) |
| 167 |
start = val |
| 168 |
prev = val |
| 169 |
continue |
| 170 |
else: |
| 171 |
ranges.append((start, prev)) |
| 172 |
start = val |
| 173 |
prev = val |
| 174 |
continue |
| 175 |
if prev == start: |
| 176 |
ranges.append((prev, prev)) |
| 177 |
else: |
| 178 |
ranges.append((start, prev)) |
| 179 |
Categories[cat] = ranges |
| 180 |
|
| 181 |
# |
| 182 |
# Assure all data is in alphabetic order, since we will be doing binary |
| 183 |
# searches on the tables. |
| 184 |
# |
| 185 |
bkeys = sorted(BlockNames.keys()) |
| 186 |
|
| 187 |
ckeys = sorted(Categories.keys()) |
| 188 |
|
| 189 |
# |
| 190 |
# Generate the resulting files |
| 191 |
# |
| 192 |
try: |
| 193 |
output = open("codegen/unicode.inc", "w") |
| 194 |
except: |
| 195 |
print("Failed to open codegen/unicode.inc") |
| 196 |
sys.exit(1) |
| 197 |
|
| 198 |
# |
| 199 |
# For any categories with more than minTableSize ranges we generate |
| 200 |
# a range table suitable for xmlCharInRange |
| 201 |
# |
| 202 |
for name in ckeys: |
| 203 |
if len(Categories[name]) <= minTableSize or name == 'Cs': |
| 204 |
continue |
| 205 |
ranges = Categories[name] |
| 206 |
group = rangetab.gen_range_tables(output, 'xml' + name, 'S', 'L', ranges) |
| 207 |
output.write("static const xmlChRangeGroup xml%sG = %s;\n\n" % |
| 208 |
(name, group)) |
| 209 |
|
| 210 |
for name in ckeys: |
| 211 |
if name == 'Cs': |
| 212 |
continue |
| 213 |
ranges = Categories[name] |
| 214 |
output.write("static int\nxmlUCSIsCat%s(int code) {\n" % name) |
| 215 |
if len(Categories[name]) > minTableSize: |
| 216 |
output.write(" return(xmlCharInRange((unsigned int)code, &xml%sG)" |
| 217 |
% name) |
| 218 |
else: |
| 219 |
start = 1 |
| 220 |
for range in ranges: |
| 221 |
(begin, end) = range; |
| 222 |
if start: |
| 223 |
output.write(" return("); |
| 224 |
start = 0 |
| 225 |
else: |
| 226 |
output.write(" ||\n "); |
| 227 |
if (begin == end): |
| 228 |
output.write("(code == %s)" % (hex(begin))) |
| 229 |
else: |
| 230 |
output.write("((code >= %s) && (code <= %s))" % ( |
| 231 |
hex(begin), hex(end))) |
| 232 |
output.write(");\n}\n\n") |
| 233 |
|
| 234 |
# |
| 235 |
# Range tables for blocks |
| 236 |
# |
| 237 |
|
| 238 |
blockGroups = '' |
| 239 |
for block in bkeys: |
| 240 |
name = block.replace('-', '') |
| 241 |
ranges = BlockNames[block] |
| 242 |
group = rangetab.gen_range_tables(output, 'xml' + name, 'S', 'L', ranges) |
| 243 |
output.write("\n") |
| 244 |
if blockGroups != '': |
| 245 |
blockGroups += ",\n" |
| 246 |
blockGroups += ' {"%s",\n %s}' % (block, group) |
| 247 |
|
| 248 |
output.write("static const xmlUnicodeRange xmlUnicodeBlocks[] = {\n") |
| 249 |
output.write(blockGroups) |
| 250 |
output.write("\n};\n\n") |
| 251 |
|
| 252 |
output.close() |