summaryrefslogtreecommitdiff
path: root/pdf/glyphdump.py
blob: 25b2afcccb2aca997e4138d93f999c1d314879e6 (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
#!/usr/bin/python

import sys

agl = []
comments = []
agltab = []
aglmap = {}
aglnames = []

f = open("glyphlist.txt", "r")
for line in f.readlines():
	if line[0] == '#':
		comments.append(line.strip());
		continue
	line = line[:-1]
	name, list = line.split(';')
	list = map(lambda x: int(x, 16), list.split(' '))
	agl.append((name, list))

for name, ucslist in agl:
	num = len(ucslist)
	ucs = ucslist[0]
	agltab.append((name, ucs))
	if ucs not in aglmap:
		aglmap[ucs] = []
	aglmap[ucs].append(name)

print "/*"
for line in comments:
	print line
print "*/"
print

agltab.sort()
print "static const struct { char *name; int ucs; }"
print "aglcodes[] = {"
for name, ucs in agltab:
	print "{\"%s\", 0x%04X}," % (name, ucs)
print "};"
print

keys = aglmap.keys()
keys.sort()
print "static const struct { int ucs; int ofs; }"
print "agldupcodes[] = {"
for ucs in keys:
	namelist = aglmap[ucs]
	ofs = len(aglnames)
	if len(namelist) > 1:
		print "{0x%04X, %d}," % (ucs, ofs)
		for name in namelist:
			aglnames.append(name)
		aglnames.append(0)
print "};"
print

print "static char *agldupnames[] = {"
for name in aglnames:
	if name:
		print ("\"%s\"," % name),
	else:
		print "0,"
print "};"
print

print """
#include "fitz.h"
#include "mupdf.h"

int pdf_lookupagl(char *name)
{
	char buf[64];
	char *p;
	int l = 0;
	int r = nelem(aglcodes) - 1;

	fz_strlcpy(buf, name, sizeof buf);

	/* kill anything after first period and underscore */
	p = strchr(buf, '.');
	if (p) p[0] = 0;
	p = strchr(buf, '_');
	if (p) p[0] = 0;

	while (l <= r)
	{
		int m = (l + r) >> 1;
		int c = strcmp(buf, aglcodes[m].name);
		if (c < 0)
			r = m - 1;
		else if (c > 0)
			l = m + 1;
		else
			return aglcodes[m].ucs;
	}

	if (strstr(buf, "uni") == buf)
		return strtol(buf + 3, nil, 16);
	else if (strstr(buf, "u") == buf)
		return strtol(buf + 1, nil, 16);
	else if (strstr(buf, "a") == buf && strlen(buf) >= 3)
		return strtol(buf + 1, nil, 10);

	return 0;
}

static char *aglnoname[1] = { 0 };

char **pdf_lookupaglnames(int ucs)
{
	int l = 0;
	int r = nelem(agldupcodes) - 1;
	while (l <= r)
	{
		int m = (l + r) >> 1;
		if (ucs < agldupcodes[m].ucs)
			r = m - 1;
		else if (ucs > agldupcodes[m].ucs)
			l = m + 1;
		else
			return agldupnames + agldupcodes[m].ofs;
	}
	return aglnoname;
}
"""