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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
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 { }
local format = string.format
--[[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.synonyms = characters.synonyms or { }
characters.context = characters.context or { }
characters.categories = {
lu = "Letter Uppercase",
ll = "Letter Lowercase",
lt = "Letter Titlecase",
lm = "Letter Modifier",
lo = "Letter Other",
mn = "Mark Nonspacing",
mc = "Mark Spacing Combining",
me = "Mark Enclosing",
nd = "Number Decimal Digit",
nl = "Number Letter",
no = "Number Other",
pc = "Punctuation Connector",
pd = "Punctuation Dash",
ps = "Punctuation Open",
pe = "Punctuation Close",
pi = "Punctuation Initial Quote",
pf = "Punctuation Final Quote",
po = "Punctuation Other",
sm = "Symbol Math",
sc = "Symbol Currency",
sk = "Symbol Modifier",
so = "Symbol Other",
zs = "Separator Space",
zl = "Separator Line",
zp = "Separator Paragraph",
cc = "Other Control",
cf = "Other Format",
cs = "Other Surrogate",
co = "Other Private Use",
cn = "Other Not Assigned",
}
--~ special : cf (softhyphen) zs (emspace)
--~ characters: ll lm lo lt lu mn nl no pc pd pe pf pi po ps sc sk sm so
characters.is_character = table.tohash {
"ll","lm","lo","lt","lu","mn","nl","no","pc","pd","pe","pf","pi","po","ps","sc","sk","sm","so"
}
characters.is_command = table.tohash {
"cf","zs"
}
-- linebreak:
--
-- normative : BK CR LF CM SG GL CB SP ZW NL WJ JL JV JT H2 H3
-- informative : XX OP CL QU NS EX SY IS PR PO NU AL ID IN HY BB BA SA AI B2
-- east asian width:
--
-- N A H W F Na
do
local _empty_table_ = { __index = function(t,k) return "" end }
function table.set_empty_metatable(t)
setmetatable(t,_empty_table_)
end
end
table.set_empty_metatable(characters.data)
--[[ldx--
<p>At this point we assume that the big data table is loaded. From this
table we derive a few more.</p>
--ldx]]--
-- used ?
characters.context.unicodes = characters.context.unicodes or { }
characters.context.utfcodes = characters.context.utfcodes or { }
characters.context.enccodes = characters.context.enccodes or { }
characters.context.fallbacks = characters.context.fallbacks or { }
function characters.context.rehash()
local unicodes, utfcodes, enccodes, fallbacks, utfchar = characters.context.unicodes, characters.context.utfcodes, characters.context.enccodes, characters.context.fallbacks, utf.char
for k,v in pairs(characters.data) do
local contextname, adobename, specials = v.contextname, v.adobename, v.specials
if contextname then
local slot = v.unicodeslot
unicodes[contextname] = slot
utfcodes[contextname] = utfchar(slot)
end
local encname = adobename or contextname
if encname then
enccodes[encname] = k
end
if specials and specials[1] == "compat" and specials[2] == 0x0020 and specials[3] then
local s = specials[3]
fallbacks[k] = s
fallbacks[s] = k
end
end
for name,code in pairs(characters.synonyms) do
if not enccodes[name] then enccodes[name] = code 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.sprint(tex.ctxcatcodes,format("\\NC %s\\NC %s\\NC\\NR",label,characters.valid(d[name])))
end
tex.sprint(tex.ctxcatcodes,"\\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.sprint(tex.ctxcatcodes,"\\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.makeactive(n,name)
tex.sprint(tex.ctxcatcodes,format("\\catcode%s=13\\unexpanded\\def %s{\\%s}",n,utf.char(n),name))
end
function tex.uprint(n)
tex.sprint(tex.ctxcatcodes,utf.char(n))
end
--~ function characters.context.define()
--~ local unicodes, utfcodes = characters.context.unicodes, characters.context.utfcodes
--~ local flush, tc, char = tex.sprint, tex.ctxcatcodes, utf.char
--~ local is_character, is_command = characters.is_character, characters.is_command
--~ for u, chr in pairs(characters.data) do
--~ local contextname = chr.contextname
--~ if contextname then
--~ local category = chr.category
--~ if is_character[category] then
--~ -- by this time, we're still in normal catcode mode
--~ if chr.unicodeslot < 128 then
--~ flush(tc, "\\chardef\\" .. contextname .. "=" .. u) -- unicodes[contextname])
--~ else
--~ flush(tc, "\\let\\" .. contextname .. "=" .. char(u)) -- utfcodes[contextname])
--~ end
--~ elseif is_command[category] then
--~ flush("\\catcode"..u.."=13\\unexpanded\\def "..char(u).."{\\"..contextname.."}")
--~ -- characters.makeactive(u,contextname)
--~ end
--~ end
--~ end
--~ end
characters.activated = { }
function characters.context.define()
local unicodes, utfcodes = characters.context.unicodes, characters.context.utfcodes
local flush, tc, char = tex.sprint, tex.ctxcatcodes, utf.char
local is_character, is_command = characters.is_character, characters.is_command
for u, chr in pairs(characters.data) do
local contextname = chr.contextname
if contextname then
local category = chr.category
if is_character[category] then
-- by this time, we're still in normal catcode mode
if chr.unicodeslot < 128 then
flush(tc, "\\chardef\\" .. contextname .. "=" .. u) -- unicodes[contextname])
else
flush(tc, "\\let\\" .. contextname .. "=" .. char(u)) -- utfcodes[contextname])
end
elseif is_command[category] then
flush("{\\catcode"..u.."=13\\unexpanded\\gdef "..char(u).."{\\"..contextname.."}}")
characters.activated[u] = true
end
end
end
end
function characters.context.activate()
for u,_ in pairs(characters.activated) do
tex.sprint(tex.ctxcatcodes,"\\catcode "..u.."=13 ")
end
end
function characters.charcode(box)
local b = tex.box[box]
local l = b.list
tex.sprint((l and l.id == node.id('glyph') and l.char) or 0)
end
--[[ldx--
<p>Setting the lccodes is also done in a loop over the data table.</p>
--ldx]]--
function characters.setcodes()
local flush, tc, format = tex.sprint, tex.ctxcatcodes, string.format
for code, chr in pairs(characters.data) do
local cc = chr.category
if cc == 'll' or cc == 'lu' or cc == 'lt' then
local lc, uc = chr.lccode, chr.uccode
if not lc then chr.lccode, lc = code, code end
if not uc then chr.uccode, uc = code, code end
flush(tc, format("\\setcclcuc %i %i %i ",code,lc,uc))
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 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)
local c = characters.data[n]
if c and c.contextname then
tex.sprint(tex.texcatcodes, "\\"..c.contextname)
else
tex.sprint(unicode.utf8.char(n))
end
end
function characters.shape(n)
local shcode = characters.data[n].shcode
if not shcode then
return n, nil
elseif type(shcode) == "table" then
return shcode[1], shcode[#shcode]
else
return shcode, nil
end
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)
--~ 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(format("\\pdfglyphtounicode{%s}{%04X}", v.adobename, v.unicodeslot))
--~ end
--~ end
--~ f:write("%\n")
--~ f:write("%\n")
--~ f:write("\\endinput")
--~ f:close()
--~ end
end
|