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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
if not modules then modules = { } end modules ['typo-cap'] = {
version = 1.001,
comment = "companion to typo-cap.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
local next, type = next, type
local format, insert = string.format, table.insert
local div, getrandom = math.div, utilities.randomizer.get
local trace_casing = false trackers .register("typesetters.casing", function(v) trace_casing = v end)
local check_kerns = true directives.register("typesetters.casing.checkkerns", function(v) check_kerns = v end)
local report_casing = logs.reporter("typesetting","casing")
local nodes, node = nodes, node
local nuts = nodes.nuts
local tonode = nuts.tonode
local tonut = nuts.tonut
local getfield = nuts.getfield
local getnext = nuts.getnext
local getprev = nuts.getprev
local getid = nuts.getid
----- getattr = nuts.getattr
local takeattr = nuts.takeattr
local getfont = nuts.getfont
local getsubtype = nuts.getsubtype
local getchar = nuts.getchar
local getdisc = nuts.getdisc
local setfield = nuts.setfield
local setattr = nuts.setattr
local setchar = nuts.setchar
local setfont = nuts.setfont
local copy_node = nuts.copy
local end_of_math = nuts.end_of_math
local traverse_id = nuts.traverse_id
local insert_after = nuts.insert_after
local find_attribute = nuts.find_attribute
local nodecodes = nodes.nodecodes
local skipcodes = nodes.skipcodes
local kerncodes = nodes.kerncodes
local glyph_code = nodecodes.glyph
local kern_code = nodecodes.kern
local disc_code = nodecodes.disc
local math_code = nodecodes.math
local kerning_code = kerncodes.kerning
local enableaction = nodes.tasks.enableaction
local newkern = nuts.pool.kern
local fonthashes = fonts.hashes
local fontdata = fonthashes.identifiers
local fontchar = fonthashes.characters
local variables = interfaces.variables
local v_reset = variables.reset
local texsetattribute = tex.setattribute
local unsetvalue = attributes.unsetvalue
typesetters = typesetters or { }
local typesetters = typesetters
typesetters.cases = typesetters.cases or { }
local cases = typesetters.cases
cases.actions = { }
local actions = cases.actions
local a_cases = attributes.private("case")
local extract = bit32.extract
local run = 0 -- a trick to make neighbouring ranges work
local blocked = { }
local function set(tag,font)
if run == 2^6 then
run = 1
else
run = run + 1
end
local a = font * 0x10000 + tag * 0x100 + run
blocked[a] = false
return a
end
local function get(a)
local font = extract(a,16,12) -- 4000
local tag = extract(a, 8, 8) -- 250
local run = extract(a, 0, 8) -- 50
return tag, font, run
end
-- print(get(set( 1, 0)))
-- print(get(set( 1, 99)))
-- print(get(set( 2, 96)))
-- print(get(set( 30, 922)))
-- print(get(set(250,4000)))
-- a previous implementation used char(0) as placeholder for the larger font, so we needed
-- to remove it before it can do further harm ... that was too tricky as we use char 0 for
-- other cases too
--
-- we could do the whole glyph run here (till no more attributes match) but then we end up
-- with more code .. maybe i will clean this up anyway as the lastfont hack is somewhat ugly
-- ... on the other hand, we need to deal with cases like:
--
-- \WORD {far too \Word{many \WORD{more \word{pushed} in between} useless} words}
local uccodes = characters.uccodes
local lccodes = characters.lccodes
local categories = characters.categories
-- true false true == mixed
local function replacer(start,codes)
local char = getchar(start)
local dc = codes[char]
if dc then
local fnt = getfont(start)
local ifc = fontchar[fnt]
if type(dc) == "table" then
for i=1,#dc do
if not ifc[dc[i]] then
return start, false
end
end
for i=#dc,1,-1 do
local chr = dc[i]
if i == 1 then
setchar(start,chr)
else
local g = copy_node(start)
setchar(g,chr)
insert_after(start,start,g)
end
end
return start, true
elseif ifc[dc] then
setchar(start,dc)
return start, true
end
end
return start, false
end
local registered, n = { }, 0
local function register(name,f)
if type(f) == "function" then
n = n + 1
actions[n] = f
registered[name] = n
return n
else
local n = registered[f]
registered[name] = n
return n
end
end
cases.register = register
local function WORD(start,attr,lastfont,n,count,where,first)
lastfont[n] = false
return replacer(first or start,uccodes)
end
local function word(start,attr,lastfont,n,count,where,first)
lastfont[n] = false
return replacer(first or start,lccodes)
end
local function Words(start,attr,lastfont,n,count,where,first) -- looks quite complex
if where == "post" then
return
end
if count == 1 and where ~= "post" then
replacer(first or start,uccodes)
return start, true, true
else
return start, false, true
end
end
local function Word(start,attr,lastfont,n,count,where,first)
blocked[attr] = true
return Words(start,attr,lastfont,n,count,where,first)
end
local function camel(start,attr,lastfont,n,count,where,first)
local _, done_1 = word(start,attr,lastfont,n,count,where,first)
local _, done_2 = Words(start,attr,lastfont,n,count,where,first)
return start, done_1 or done_2, true
end
-- local function mixed(start,attr,lastfont,n,count,where,first)
-- if where == "post" then
-- return
-- end
-- local used = first or start
-- local char = getchar(first)
-- local dc = uccodes[char]
-- if not dc then
-- return start, false, true
-- elseif dc == char then
-- local lfa = lastfont[n]
-- if lfa then
-- setfont(first,lfa)
-- return start, true, true
-- else
-- return start, false, true
-- end
-- else
-- replacer(first or start,uccodes)
-- return start, true, true
-- end
-- end
local function mixed(start,attr,lastfont,n,count,where,first)
if where == "post" then
return
end
local used = first or start
local char = getchar(used)
local dc = uccodes[char]
if not dc then
return start, false, true
elseif dc == char then
local lfa = lastfont[n]
if lfa then
setfont(used,lfa)
return start, true, true
else
return start, false, true
end
else
if check_kerns then
local p = getprev(used)
if p and getid(p) == glyph_code then
local c = lccodes[char]
local c = type(c) == "table" and c[1] or c
replacer(used,uccodes)
local fp = getfont(p)
local fc = getfont(used)
if fp ~= fc then
local k = fonts.getkern(fontdata[fp],getchar(p),c)
if k ~= 0 then
insert_after(p,p,newkern(k))
end
end
else
replacer(used,uccodes)
end
else
replacer(used,uccodes)
end
return start, true, true
end
end
local function Capital(start,attr,lastfont,n,count,where,first,once) -- 3
local used = first or start
if count == 1 and where ~= "post" then
local lfa = lastfont[n]
if lfa then
local dc = uccodes[getchar(used)]
if dc then
setfont(used,lfa)
end
end
end
local s, d, c = replacer(first or start,uccodes)
if once then
lastfont[n] = false -- here
end
return start, d, c
end
local function capital(start,attr,lastfont,n,where,count,first,count) -- 4
return Capital(start,attr,lastfont,n,where,count,first,true)
end
local function none(start,attr,lastfont,n,count,where,first)
return start, false, true
end
local function randomized(start,attr,lastfont,n,count,where,first)
local used = first or start
local char = getchar(used)
local font = getfont(used)
local tfm = fontchar[font]
lastfont[n] = false
local kind = categories[char]
if kind == "lu" then
while true do
local n = getrandom("capital lu",0x41,0x5A)
if tfm[n] then -- this also intercepts tables
setchar(used,n)
return start, true
end
end
elseif kind == "ll" then
while true do
local n = getrandom("capital ll",0x61,0x7A)
if tfm[n] then -- this also intercepts tables
setchar(used,n)
return start, true
end
end
end
return start, false
end
register(variables.WORD, WORD) -- 1
register(variables.word, word) -- 2
register(variables.Word, Word) -- 3
register(variables.Words, Words) -- 4
register(variables.capital,capital) -- 5
register(variables.Capital,Capital) -- 6
register(variables.none, none) -- 7 (dummy)
register(variables.random, randomized) -- 8
register(variables.mixed, mixed) -- 9
register(variables.camel, camel) -- 10
register(variables.cap, variables.capital) -- clone
register(variables.Cap, variables.Capital) -- clone
function cases.handler(head) -- not real fast but also not used on much data
local start = tonut(head)
local lastfont = { }
local lastattr = nil
local done = false
local count = 0
local previd = nil
local prev = nil
while start do -- while because start can jump ahead
local id = getid(start)
if id == glyph_code then
-- local attr = getattr(start,a_cases)
local attr = takeattr(start,a_cases)
if attr and attr > 0 and not blocked[attr] then
if attr ~= lastattr then
lastattr = attr
count = 1
else
count = count + 1
end
-- setattr(start,a_cases,unsetvalue) -- not needed
local n, id, m = get(attr)
if lastfont[n] == nil then
lastfont[n] = id
end
local action = actions[n] -- map back to low number
if action then
start, ok = action(start,attr,lastfont,n,count)
if ok then
done = true
end
if trace_casing then
report_casing("case trigger %a, instance %a, fontid %a, result %a",n,m,id,ok)
end
elseif trace_casing then
report_casing("unknown case trigger %a",n)
end
end
elseif id == disc_code then
-- local attr = getattr(start,a_cases)
local attr = takeattr(start,a_cases)
if attr and attr > 0 and not blocked[attr] then
if attr ~= lastattr then
lastattr = attr
count = 0
end
-- setattr(start,a_cases,unsetvalue) -- not needed
local n, id, m = get(attr)
if lastfont[n] == nil then
lastfont[n] = id
end
local action = actions[n] -- map back to low number
if action then
local pre, post, replace = getdisc(start)
if replace then
local cnt = count
for g in traverse_id(glyph_code,replace) do
cnt = cnt + 1
takeattr(g,a_cases)
-- setattr(g,a_cases,unsetvalue)
local _, _, quit = action(start,attr,lastfont,n,cnt,"replace",g)
if quit then break end
end
end
if pre then
local cnt = count
for g in traverse_id(glyph_code,pre) do
cnt = cnt + 1
takeattr(g,a_cases)
-- setattr(g,a_cases,unsetvalue)
local _, _, quit = action(start,attr,lastfont,n,cnt,"pre",g)
if quit then break end
end
end
if post then
local cnt = count
for g in traverse_id(glyph_code,post) do
cnt = cnt + 1
takeattr(g,a_cases)
-- setattr(g,a_cases,unsetvalue)
local _, _, quit = action(start,attr,lastfont,n,cnt,"post",g)
if quit then break end
end
end
end
count = count + 1
end
elseif id == math_code then
start = end_of_math(start)
count = 0
elseif prev_id == kern_code and getsubtype(prev) == kerning_code then
-- still inside a word ...normally kerns are added later
else
count = 0
end
if start then
prev = start
previd = id
start = getnext(start)
end
end
return head, done
end
-- function cases.handler(head) -- not real fast but also not used on much data
-- local attr, start = find_attribute(tonut(head),a_cases)
-- if not start then
-- return head, false
-- end
-- local lastfont = { }
-- local lastattr = nil
-- local done = false
-- local count = 0
-- local previd = nil
-- local prev = nil
-- while start do
-- while start do -- while because start can jump ahead
-- local id = getid(start)
-- if id == glyph_code then
-- -- local attr = getattr(start,a_cases)
-- local attr = takeattr(start,a_cases)
-- if attr and attr > 0 and not blocked[attr] then
-- if attr ~= lastattr then
-- lastattr = attr
-- count = 1
-- else
-- count = count + 1
-- end
-- -- setattr(start,a_cases,unsetvalue) -- not needed
-- local n, id, m = get(attr)
-- if lastfont[n] == nil then
-- lastfont[n] = id
-- end
-- local action = actions[n] -- map back to low number
-- if action then
-- start, ok = action(start,attr,lastfont,n,count)
-- if ok then
-- done = true
-- end
-- if trace_casing then
-- report_casing("case trigger %a, instance %a, fontid %a, result %a",n,m,id,ok)
-- end
-- elseif trace_casing then
-- report_casing("unknown case trigger %a",n)
-- end
-- end
-- elseif id == disc_code then
-- -- local attr = getattr(start,a_cases)
-- local attr = takeattr(start,a_cases)
-- if attr and attr > 0 and not blocked[attr] then
-- if attr ~= lastattr then
-- lastattr = attr
-- count = 0
-- end
-- -- setattr(start,a_cases,unsetvalue) -- not needed
-- local n, id, m = get(attr)
-- if lastfont[n] == nil then
-- lastfont[n] = id
-- end
-- local action = actions[n] -- map back to low number
-- if action then
-- local pre, post, replace = getdisc(start)
-- if replace then
-- local cnt = count
-- for g in traverse_id(glyph_code,replace) do
-- cnt = cnt + 1
-- takeattr(g,a_cases)
-- -- setattr(g,a_cases,unsetvalue)
-- local _, _, quit = action(start,attr,lastfont,n,cnt,"replace",g)
-- if quit then break end
-- end
-- end
-- if pre then
-- local cnt = count
-- for g in traverse_id(glyph_code,pre) do
-- cnt = cnt + 1
-- takeattr(g,a_cases)
-- -- setattr(g,a_cases,unsetvalue)
-- local _, _, quit = action(start,attr,lastfont,n,cnt,"pre",g)
-- if quit then break end
-- end
-- end
-- if post then
-- local cnt = count
-- for g in traverse_id(glyph_code,post) do
-- cnt = cnt + 1
-- takeattr(g,a_cases)
-- -- setattr(g,a_cases,unsetvalue)
-- local _, _, quit = action(start,attr,lastfont,n,cnt,"post",g)
-- if quit then break end
-- end
-- end
-- end
-- count = count + 1
-- end
-- elseif id == math_code then
-- start = end_of_math(start)
-- count = 0
-- elseif prev_id == kern_code and getsubtype(prev) == kerning_code then
-- -- still inside a word ...normally kerns are added later
-- else
-- count = 0
-- start = getnext(start)
-- break
-- end
-- if start then
-- prev = start
-- previd = id
-- start = getnext(start)
-- end
-- end
-- if start then
-- attr, start = find_attribute(start,a_cases)
-- end
-- end
-- return head, done
-- end
-- function cases.handler(head) -- let's assume head doesn't change ... no reason
-- local done = false
-- local lastfont = { }
-- for first, last, size, attr in nuts.words(tonut(head),a_cases) do
-- local n, id, m = get(attr)
-- if lastfont[n] == nil then
-- lastfont[n] = id
-- end
-- local action = actions[n]
-- if action then
-- local _, ok = action(first,attr,lastfont,n)
-- if ok then
-- done = true
-- end
-- end
-- end
-- return head, done
-- end
local enabled = false
function cases.set(n,id)
if n == v_reset then
n = unsetvalue
else
n = registered[n] or tonumber(n)
if n then
if not enabled then
enableaction("processors","typesetters.cases.handler")
if trace_casing then
report_casing("enabling case handler")
end
enabled = true
end
n = set(n,id)
else
n = unsetvalue
end
end
texsetattribute(a_cases,n)
-- return n -- bonus
end
-- interface
interfaces.implement {
name = "setcharactercasing",
actions = cases.set,
arguments = { "string", "integer" }
}
|