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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
# Parse a CMap file and dump it as a C struct.
import sys
# Decode a subset of CMap syntax (only what is needed for our built-in resources)
# We require that tokens are whitespace separated.
def dumpcmap(filename):
codespacerange = []
usecmap = ""
cmapname = ""
wmode = 0
map = {}
def tocode(s):
if s[0] == '<' and s[-1] == '>':
return int(s[1:-1], 16)
return int(s, 10)
def map_cidchar(lo, v):
map[lo] = v
def map_cidrange(lo, hi, v):
while lo <= hi:
map[lo] = v
lo = lo + 1
v = v + 1
def add_bf(lo, v):
# Decode unicode surrogate pairs
if len(v) == 2 and v[0] >= 0xd800 and v[0] <= 0xdbff and v[1] >= 0xdc00 and v[1] <= 0xdfff:
map[lo] = ((v[0] - 0xd800) << 10) + (v[1] - 0xdc00) + 0x10000
elif len(v) == 1:
map[lo] = v[0]
elif len(v) <= 8:
map[lo] = v[:]
else:
print "/* warning: too long one-to-many mapping: %s */" % (v)
def map_bfchar(lo, bf):
bf = bf[1:-1] # drop < >
v = [int(bf[i:i+4],16) for i in xrange(0, len(bf), 4)]
add_bf(lo, v)
def map_bfrange(lo, hi, bf):
bf = bf[1:-1] # drop < >
v = [int(bf[i:i+4],16) for i in xrange(0, len(bf), 4)]
while lo <= hi:
add_bf(lo, v)
lo = lo + 1
v[-1] = v[-1] + 1
current = None
for line in open(filename, "r").readlines():
if line[0] == '%':
continue
line = line.strip().split()
if len(line) == 0:
continue
if line[0] == '/CMapName':
cmapname = line[1][1:]
elif line[0] == '/WMode':
wmode = int(line[1])
elif len(line) > 1 and line[1] == 'usecmap':
usecmap = line[0][1:]
elif len(line) > 1 and line[1] == 'begincodespacerange': current = 'codespacerange'
elif len(line) > 1 and line[1] == 'begincidrange': current = 'cidrange'
elif len(line) > 1 and line[1] == 'beginbfrange': current = 'bfrange'
elif len(line) > 1 and line[1] == 'begincidchar': current = 'cidchar'
elif len(line) > 1 and line[1] == 'beginbfchar': current = 'bfchar'
elif line[0] == 'begincodespacerange': current = 'codespacerange'
elif line[0] == 'begincidrange': current = 'cidrange'
elif line[0] == 'beginbfrange': current = 'bfrange'
elif line[0] == 'begincidchar': current = 'cidchar'
elif line[0] == 'beginbfchar': current = 'bfchar'
elif line[0].startswith("end"):
current = None
elif current == 'codespacerange' and len(line) == 2:
n, a, b = (len(line[0])-2)/2, tocode(line[0]), tocode(line[1])
codespacerange.append((n, a, b))
elif current == 'cidrange' and len(line) == 3:
a, b, c = tocode(line[0]), tocode(line[1]), tocode(line[2])
map_cidrange(a, b, c)
elif current == 'cidchar' and len(line) == 2:
a, b = tocode(line[0]), tocode(line[1])
map_cidchar(a, b)
elif current == 'bfchar' and len(line) == 2:
a, b = tocode(line[0]), line[1]
map_bfchar(a, b)
elif current == 'bfrange' and len(line) == 3:
a, b, c = tocode(line[0]), tocode(line[1]), line[2]
map_bfrange(a, b, c)
# Create ranges
ranges = []
xranges = []
mranges = []
mdata = []
out_lo = -100
out_hi = -100
out_v_lo = 0
out_v_hi = 0
def flush_range():
if out_lo >= 0:
if out_lo > 0xffff or out_hi > 0xffff or out_v_lo > 0xffff:
xranges.append((out_lo, out_hi, out_v_lo))
else:
ranges.append((out_lo, out_hi, out_v_lo))
keys = map.keys()
keys.sort()
for code in keys:
v = map[code]
if type(v) is not int:
flush_range()
out_lo = out_hi = -100
mranges.append((code, len(mdata)))
mdata.append(len(v))
mdata.extend(v)
else:
if code != out_hi + 1 or v != out_v_hi + 1:
flush_range()
out_lo = out_hi = code
out_v_lo = out_v_hi = v
else:
out_hi = out_hi + 1
out_v_hi = out_v_hi + 1
flush_range()
# Print C file
cname = cmapname.replace('-', '_')
print
print "/*", cmapname, "*/"
print
if len(ranges) > 0:
print "static const pdf_range cmap_%s_ranges[] = {" % cname
for r in ranges:
print "{%d,%d,%d}," % r
print "};"
print
if len(xranges) > 0:
print "static const pdf_xrange cmap_%s_xranges[] = {" % cname
for r in xranges:
print "{%d,%d,%d}," % r
print "};"
print
if len(mranges) > 0:
print "static const pdf_mrange cmap_%s_mranges[] = {" % cname
for r in mranges:
print "{%d,%d}," % r
print "};"
print
print "static const int cmap_%s_table[] = {" % cname
n = mdata[0]
i = 0
for r in mdata:
if i <= n:
sys.stdout.write("%d," % r)
i = i + 1
else:
sys.stdout.write("\n%d," % r)
i = 1
n = r
sys.stdout.write("\n")
print "};"
print
print "static pdf_cmap cmap_%s = {" % cname
print "\t{ -1, pdf_drop_cmap_imp },"
print "\t/* cmapname */ \"%s\"," % cmapname
print "\t/* usecmap */ \"%s\", NULL," % usecmap
print "\t/* wmode */ %d," % wmode
print "\t/* codespaces */ %d, {" % len(codespacerange)
if len(codespacerange) > 0:
for codespace in codespacerange:
fmt = "\t\t{ %%d, 0x%%0%dx, 0x%%0%dx }," % (codespace[0]*2, codespace[0]*2)
print fmt % codespace
else:
print "\t\t{ 0, 0, 0 },"
print "\t},"
if len(ranges) > 0:
print "\t%d, %d, (pdf_range*)cmap_%s_ranges," % (len(ranges),len(ranges),cname)
else:
print "\t0, 0, NULL, /* ranges */"
if len(xranges) > 0:
print "\t%d, %d, (pdf_xrange*)cmap_%s_xranges," % (len(xranges),len(xranges),cname)
else:
print "\t0, 0, NULL, /* xranges */"
if len(mranges) > 0:
print "\t%d, %d, (pdf_mrange*)cmap_%s_mranges," % (len(mranges),len(mranges),cname)
else:
print "\t0, 0, NULL, /* mranges */"
if len(mdata) > 0:
print "\t%d, %d, (int*)cmap_%s_table," % (len(mdata),len(mdata),cname)
else:
print "\t0, 0, NULL, /* table */"
print "\t0, 0, 0, NULL /* splay tree */"
print "};"
print "/* This is an automatically generated file. Do not edit. */"
for arg in sys.argv[1:]:
dumpcmap(arg)
|