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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
if not modules then modules = { } end modules ['char-ini'] = {
version = 1.001,
comment = "companion to char-ini.tex",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
utf = utf or unicode.utf
tex = tex or { }
function tex.ctxprint(...)
tex.sprint(tex.ctxcatcodes,...)
end
--[[ldx--
<p>This module implements some methods and creates additional datastructured
from the big character table that we use for all kind of purposes:
<type>char-def.lua</type>.</p>
--ldx]]--
characters = characters or { }
characters.data = characters.data or { }
characters.context = characters.context or { }
_empty_table_ = { }
_empty_table_.__index = function(t,k) return "" end
setmetatable(characters.data,_empty_table_)
--[[ldx--
<p>At this point we assume that the big data table is loaded. From this
table we derive a few more.</p>
--ldx]]--
characters.context.unicodes = characters.context.unicodes or { }
characters.context.utfcodes = characters.context.utfcodes or { }
characters.context.enccodes = characters.context.enccodes or { }
function characters.context.rehash()
local unicodes, utfcodes, enccodes = characters.context.unicodes, characters.context.utfcodes, characters.context.enccodes
for k,v in pairs(characters.data) do
local contextname, adobename = v.contextname, v.adobename
if contextname then
unicodes[contextname] = v.unicodeslot
utfcodes[contextname] = utf.char(v.unicodeslot)
end
local encname = adobename or contextname
if encname then
enccodes[encname] = k
end
end
end
--[[ldx--
<p>The <type>context</type> namespace is used to store methods and data
which is rather specific to <l n='context'/>.</p>
--ldx]]--
function characters.context.show(n)
local n = characters.number(n)
local d = characters.data[n]
if d then
local function entry(label,name)
tex.ctxprint(string.format("\\NC %s\\NC %s\\NC\\NR",label,characters.valid(d[name])))
end
tex.ctxprint("\\starttabulate[|Tl|Tl|]")
entry("unicode index" , "unicodeslot")
entry("context name" , "contextname")
entry("adobe name" , "adobename")
entry("category" , "category")
entry("description" , "description")
entry("uppercase code", "uccode")
entry("lowercase code", "lccode")
entry("specials" , "specials")
tex.ctxprint("\\stoptabulate ")
end
end
--[[ldx--
<p>Instead of using a <l n='tex'/> file to define the named glyphs, we
use the table. After all, we have this information available anyway.</p>
--ldx]]--
function characters.context.define()
local unicodes, utfcodes = characters.context.unicodes, characters.context.utfcodes
local flush, tc = tex.sprint, tex.ctxcatcodes
for _, chr in pairs(characters.data) do
local contextname = chr.contextname
if contextname then
-- by this time, we're still in normal catcode mode
if chr.unicodeslot < 128 then
flush(tc, "\\chardef\\" .. contextname .. "=" .. unicodes[contextname])
else
flush(tc, "\\let\\" .. contextname .. "=" .. utfcodes[contextname])
end
end
end
end
--[[ldx--
<p>Setting the lccodes is also done in a loop over the data table.</p>
--ldx]]--
function characters.setcodes()
local flush, tc = tex.sprint, tex.ctxcatcodes
for code, chr in pairs(characters.data) do
local cc = chr.category
if cc == 'll' or cc == 'lu' or cc == 'lt' then
if not chr.lccode then chr.lccode = code end
if not chr.uccode then chr.uccode = code end
flush(tc, '\\setcclcuc '.. code .. ' ' .. chr.lccode .. ' ' .. chr.uccode .. ' ')
end
end
end
--[[ldx--
<p>Next comes a whole series of helper methods. These are (will be) part
of the official <l n='api'/>.</p>
--ldx]]--
--[[ldx--
<p>This converts a string (if given) into a number.</p>
--ldx]]--
function characters.number(n)
if type(n) == "string" then return tonumber(n,16) else return n end
end
--[[ldx--
<p>Checking for valid characters.</p>
--ldx]]--
function characters.is_valid(s)
return s or ""
end
function characters.checked(s, default)
return s or default
end
characters.valid = characters.is_valid
--[[ldx--
<p>The next method is used when constructing the main table, although nowadays
we do this in one step. The index can be a string or a number.</p>
--ldx]]--
function characters.define(c)
characters.data[characters.number(c.unicodeslot)] = c
end
--[[ldx--
<p></p>
--ldx]]--
-- set a table entry; index is number (can be different from unicodeslot)
function characters.set(n, c)
characters.data[characters.number(n)] = c
end
--[[ldx--
<p>Get a table entry happens by number. Keep in mind that the unicodeslot
can be different (not likely).</p>
--ldx]]--
function characters.get(n)
return characters.data[characters.number(n)]
end
--[[ldx--
<p>A couple of convenience methods. Beware, these are not that fast due
to the checking.</p>
--ldx]]--
function characters.hexindex(n)
return string.format("%04X", characters.valid(characters.data[characters.number(n)].unicodeslot))
end
function characters.contextname(n)
return characters.valid(characters.data[characters.number(n)].contextname)
end
function characters.adobename(n)
return characters.valid(characters.data[characters.number(n)].adobename)
end
function characters.description(n)
return characters.valid(characters.data[characters.number(n)].description)
end
function characters.category(n)
return characters.valid(characters.data[characters.number(n)].category)
end
--[[ldx--
<p>Requesting lower and uppercase codes:</p>
--ldx]]--
function characters.uccode(n) return characters.data[n].uccode or n end
function characters.lccode(n) return characters.data[n].lccode or n end
function characters.flush(n)
if characters.data[n].contextname then
tex.sprint(tex.texcatcodes, "\\"..characters.data[n].contextname)
else
tex.sprint(unicode.utf8.char(n))
end
end
function characters.shape(n)
return characters.data[n].shcode or n
end
--[[ldx--
<p>Categories play an important role, so here are some checkers.</p>
--ldx]]--
function characters.is_of_category(token,category)
if type(token) == "string" then
return characters.data[utf.byte(token)].category == category
else
return characters.data[token].category == category
end
end
function characters.i_is_of_category(i,category) -- by index (number)
local cd = characters.data[i]
return cd and cd.category == category
end
function characters.n_is_of_category(n,category) -- by name (string)
local cd = characters.data[utf.byte(n)]
return cd and cd.category == category
end
--[[ldx--
<p>The following code is kind of messy. It is used to generate the right
unicode reference tables.</p>
--ldx]]--
function characters.setpdfunicodes()
local flush, tc, sf = tex.sprint, tex.ctxcatcodes, string.format
for _,v in pairs(characters.data) do
if v.adobename then
flush(tc,sf("\\pdfglyphtounicode{%s}{%04X}", v.adobename, v.unicodeslot))
end
end
end
--[[ldx--
<p>The next method generates a table for good old <l n='pdftex'/>.</p>
<typing>
characters.pdftex.make_pdf_to_unicodetable("pdfr-def.tex")
</typing>
--ldx]]--
characters.pdftex = characters.pdftex or { }
function characters.pdftex.make_pdf_to_unicodetable(filename)
local sf = string.format
f = io.open(filename,'w')
if f then
f:write("% This file is generated with Luatex using the\n")
f:write("% character tables that come with ConTeXt MkIV.\n")
f:write("%\n")
f:write("\\ifx\\pdfglyphtounicode\\undefined\\endinput\\fi\n") -- just to be sure
for _, v in pairs(characters.data) do
if v.adobename then
f:write(sf("\\pdfglyphtounicode{%s}{%04X}", v.adobename, v.unicodeslot))
end
end
f:write("%\n")
f:write("%\n")
f:write("\\endinput")
f:close()
end
end
|