summaryrefslogtreecommitdiff
path: root/tex/context/base/scrp-cjk.lua
blob: 6ac6c5b11cf17ac1b25bf50137e4a50f8a564f45 (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
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
if not modules then modules = { } end modules ['scrp-cjk'] = {
    version   = 1.001,
    comment   = "companion to scrp-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local has_attribute      = node.has_attribute
local make_glue_node     = nodes.glue
local make_penalty_node  = nodes.penalty
local insert_node_after  = node.insert_after
local insert_node_before = node.insert_before
local remove_node        = nodes.remove

local glyph   = node.id('glyph')
local glue    = node.id('glue')
local penalty = node.id('penalty')

local preproc = attributes.private('preproc')
local prestat = attributes.private('prestat')

scripts.cjk = scripts.cjk or { }

local kind_to_number = scripts.kind_to_number
local number_to_kind = scripts.number_to_kind
local hash           = scripts.hash
local cjk            = scripts.cjk

local fontdata       = fonts.identifiers
local quaddata       = fonts.quads

-- raggedleft is controlled by leftskip and we might end up with a situation where
-- the intercharacter spacing interferes with this; the solution is to patch the
-- nodelist but better is to use veryraggedleft

local inter_char_stretch        = 0
local inter_char_half_shrink    = 0
local inter_char_hangul_penalty = 0

local function set_parameters(font,data)
    -- beware: parameters can be nil in e.g. punk variants
    local quad = quaddata[font]
    inter_char_half_shrink    = data.inter_char_half_shrink_factor * quad
    inter_char_stretch        = data.inter_char_stretch_factor * quad
    inter_char_hangul_penalty = data.inter_char_hangul_penalty
end

-- a test version did compensate for crappy halfwidth but we can best do that
-- at font definition time and/or just assume a correct font

local function nobreak(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
end
local function stretch_break(head,current)
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
end
local function shrink_break(head,current)
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
end
local function nobreak_stretch(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
end
local function korean_break(head,current)
    insert_node_before(head,current,make_penalty_node(inter_char_hangul_penalty))
end

local function nobreak_shrink(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
end
local function nobreak_autoshrink(head,current)
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
end

local function nobreak_stretch_nobreak_shrink(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
end
local function nobreak_stretch_nobreak_autoshrink(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
end

local function nobreak_shrink_nobreak_stretch(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
end
local function nobreak_autoshrink_nobreak_stretch(head,current)
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
end

local function nobreak_shrink_break_stretch(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
end
local function nobreak_autoshrink_break_stretch(head,current)
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
end

local function nobreak_shrink_break_stretch_nobreak_shrink(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
end
local function nobreak_autoshrink_break_stretch_nobreak_autoshrink(head,current)
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
end
local function nobreak_autoshrink_break_stretch_nobreak_shrink(head,current)
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
end
local function nobreak_shrink_break_stretch_nobreak_autoshrink(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    if true then
        insert_node_before(head,current,make_penalty_node(10000))
        insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    end
end

local function nobreak_stretch_break_shrink(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
end
local function nobreak_stretch_break_autoshrink(head,current)
    insert_node_before(head,current,make_penalty_node(10000))
    insert_node_before(head,current,make_glue_node(0,inter_char_stretch,0))
    if true then
        insert_node_before(head,current,make_glue_node(0,0,inter_char_half_shrink))
    end
end

-- hangul (korean)

local injectors = { -- [previous] [current]
    jamo_final = {
        jamo_initial     = korean_break,
        korean           = korean_break,
        chinese          = korean_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak,
        full_width_open  = stretch_break,
        full_width_close = nobreak,
    --  hyphen           = nil,
        non_starter      = korean_break,
        other            = korean_break,
    },
    korean = {
        jamo_initial     = korean_break,
        korean           = korean_break,
        chinese          = korean_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak,
        full_width_open  = stretch_break,
        full_width_close = nobreak,
    --  hyphen           = nil,
        non_starter      = korean_break,
        other            = korean_break,
    },
    chinese = {
        jamo_initial     = korean_break,
        korean           = korean_break,
        chinese          = korean_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak,
        full_width_open  = stretch_break,
        full_width_close = nobreak,
    --  hyphen           = nil,
        non_starter      = korean_break,
        other            = korean_break,
    },
    hyphen = {
        jamo_initial     = stretch_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak,
        full_width_open  = stretch_break,
        full_width_close = nobreak,
    --  hyphen           = nil,
        non_starter      = stretch_break,
        other            = stretch_break,
    },
    start = {
    --  jamo_initial     = nil,
    --  korean           = nil,
    --  chinese          = nil,
    --  half_width_open  = nil,
    --  half_width_close = nil,
    --  full_width_open  = nil,
    --  full_width_close = nil,
    --  hyphen           = nil,
    --  non_starter      = nil,
    --  other            = nil,
    },
    other = {
        jamo_initial     = stretch_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak,
        full_width_open  = stretch_break,
        full_width_close = nobreak,
    --  hyphen           = nil,
        non_starter      = stretch_break,
        other            = stretch_break,
    },
    non_starter = {
        jamo_initial     = stretch_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak,
        full_width_open  = stretch_break,
        full_width_close = nobreak,
    --  hyphen           = nil,
        non_starter      = nobreak,
        other            = nobreak,
    },
    full_width_open = {
        jamo_initial     = nobreak,
        korean           = nobreak,
        chinese          = nobreak,
        half_width_open  = nobreak,
        half_width_close = nobreak,
        full_width_open  = nobreak,
        full_width_close = nobreak,
        hyphen           = nobreak,
        non_starter      = nobreak,
        other            = nobreak,
    },
    half_width_open = {
        jamo_initial     = nobreak,
        korean           = nobreak,
        chinese          = nobreak,
        half_width_open  = nobreak,
        half_width_close = nobreak,
        full_width_open  = nobreak,
        full_width_close = nobreak,
        hyphen           = nobreak,
        non_starter      = nobreak,
        other            = nobreak,
    },
    full_width_close = {
        jamo_initial     = stretch_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak_stretch,
        full_width_open  = stretch_break,
        full_width_close = nobreak_stretch,
        hyphen           = nobreak_stretch,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
    half_width_close = {
        jamo_initial     = stretch_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = stretch_break,
        half_width_close = nobreak_stretch,
        full_width_open  = stretch_break,
        full_width_close = nobreak_stretch,
        hyphen           = nobreak_stretch,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
}

local dataset = {
    inter_char_stretch_factor     = 0.50, -- of quad
    inter_char_half_shrink_factor = 0.50, -- of quad
    inter_char_hangul_penalty     =   50,
}

local function process(head,first,last)
    if first ~= last then
        local lastfont, previous, originals, last = nil, "start", nil, nil
        while true do
            local upcoming, id = first.next, first.id
            if id == glyph then
                local a = has_attribute(first,prestat)
                local current = number_to_kind[a]
                local action = injectors[previous]
                if action then
                    action = action[current]
                    if action then
                        local font = first.font
                        if font ~= lastfont then
                            lastfont = font
                            set_parameters(font,dataset)
                        end
                        action(head,first)
                    end
                end
                previous = current
            else -- glue
                local p, n = first.prev, upcoming
                if p and n then
                    local pid, nid = p.id, n.id
                    if pid == glyph and nid == glyph then
                        local pa, na = has_attribute(p,prestat), has_attribute(n,prestat)
                        local pcjk, ncjk = pa and number_to_kind[pa], na and number_to_kind[na]
                        if not pcjk                 or not ncjk
                            or pcjk == "korean"     or ncjk == "korean"
                            or pcjk == "other"      or ncjk == "other"
                            or pcjk == "jamo_final" or ncjk == "jamo_initial" then
                            previous = "start"
                        else -- if head ~= first then
                            remove_node(head,first,true)
                            previous = pcjk
                    --    else
                    --        previous = pcjk
                        end
                    else
                        previous = "start"
                    end
                else
                    previous = "start"
                end
            end
            if upcoming == last then -- was stop
                break
            else
                first = upcoming
            end
        end
    end
end

scripts.install {
    name = "hangul",
    process = process,
}

-- hanzi (chinese)

local injectors = { -- [previous] [current]
    jamo_final = {
        jamo_initial     = korean_break,
        korean           = korean_break,
        chinese          = stretch_break,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_break_shrink,
        full_width_close = nobreak_stretch,
    --  hyphen           = nil,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
    korean = {
        jamo_initial     = korean_break,
        korean           = korean_break,
        chinese          = stretch_break,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_break_shrink,
        full_width_close = nobreak_stretch,
    --  hyphen           = nil,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
    chinese = {
        jamo_initial     = korean_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_break_shrink,
        full_width_close = nobreak_stretch,
        hyphen           = nobreak_stretch,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
    hyphen = {
        jamo_initial     = korean_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_break_shrink,
        full_width_close = nobreak_stretch,
    --  hyphen           = nil,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
    start = {
    --  jamo_initial     = nil,
    --  korean           = nil,
    --  chinese          = nil,
        half_width_open  = nobreak_autoshrink,
        half_width_close = nil,
        full_width_open  = nobreak_shrink,
        full_width_close = nobreak,
    --  hyphen           = nil,
        non_starter      = nobreak,
    --  other            = nil,
    },
    other = {
        jamo_initial     = stretch_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_break_shrink,
        full_width_close = nobreak_stretch,
    --  hyphen           = nil,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
    non_starter = {
        jamo_initial     = stretch_break,
        korean           = stretch_break,
        chinese          = stretch_break,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_break_shrink,
        full_width_close = nobreak_stretch,
    --  hyphen           = nil,
        non_starter      = nobreak_stretch,
        other            = stretch_break,
    },
    full_width_open = {
        jamo_initial     = nobreak_stretch,
        korean           = nobreak_stretch,
        chinese          = nobreak_stretch,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_break_shrink,
        full_width_close = nobreak_stretch,
        hyphen           = nobreak_stretch,
        non_starter      = nobreak_stretch,
        other            = nobreak_stretch,
    },
    half_width_open = {
        jamo_initial     = nobreak_stretch,
        korean           = nobreak_stretch,
        chinese          = nobreak_stretch,
        half_width_open  = nobreak_stretch_break_autoshrink,
        half_width_close = nobreak_stretch,
        full_width_open  = nobreak_stretch_nobreak_shrink,
        full_width_close = nobreak_stretch,
        hyphen           = nobreak_stretch,
        non_starter      = nobreak_stretch,
        other            = nobreak_stretch,
    },
    full_width_close = {
        jami_initial     = nobreak_shrink_break_stretch,
        korean           = nobreak_shrink_break_stretch,
        chinese          = stretch_break, -- nobreak_shrink_break_stretch,
        half_width_open  = nobreak_shrink_break_stretch_nobreak_autoshrink,
        half_width_close = nobreak_shrink_nobreak_stretch,
        full_width_open  = nobreak_shrink_break_stretch_nobreak_shrink,
        full_width_close = nobreak_shrink_nobreak_stretch,
        hyphen           = nobreak_shrink_break_stretch,
        non_starter      = nobreak_shrink_break_stretch,
        other            = nobreak_shrink_break_stretch,
    },
    half_width_close = {
        jami_initial     = nobreak_shrink_break_stretch,
        korean           = nobreak_autoshrink_break_stretch,
        chinese          = stretch_break, -- nobreak_autoshrink_break_stretch,
        half_width_open  = nobreak_autoshrink_break_stretch_nobreak_autoshrink,
        half_width_close = nobreak_autoshrink_nobreak_stretch,
        full_width_open  = nobreak_autoshrink_break_stretch_nobreak_shrink,
        full_width_close = nobreak_autoshrink_nobreak_stretch,
        hyphen           = nobreak_autoshrink_break_stretch,
        non_starter      = nobreak_autoshrink_break_stretch,
        other            = nobreak_autoshrink_break_stretch,
    },
}

local dataset = {
    inter_char_stretch_factor     = 0.50, -- of quad
    inter_char_half_shrink_factor = 0.50, -- of quad
    inter_char_hangul_penalty     =   50,
}

local function process(head,first,last)
    if first ~= last then
        local lastfont, previous, originals, last = nil, "start", nil, nil
        while true do
            local upcoming, id = first.next, first.id
            if id == glyph then
                local a = has_attribute(first,prestat)
                local current = number_to_kind[a]
                local action = injectors[previous]
                if action then
                    action = action[current]
                    if action then
                        local font = first.font
                        if font ~= lastfont then
                            lastfont = font
                            set_parameters(font,dataset)
                        end
                        action(head,first)
                    end
                end
                previous = current
            else -- glue
                local p, n = first.prev, upcoming
                if p and n then
                    local pid, nid = p.id, n.id
                    if pid == glyph and nid == glyph then
                        local pa, na = has_attribute(p,prestat), has_attribute(n,prestat)
                        local pcjk, ncjk = pa and number_to_kind[pa], na and number_to_kind[na]
                        if not pcjk                 or not ncjk
                            or pcjk == "korean"     or ncjk == "korean"
                            or pcjk == "other"      or ncjk == "other"
                            or pcjk == "jamo_final" or ncjk == "jamo_initial" then
                            previous = "start"
                        else -- if head ~= first then
                            remove_node(head,first,true)
                            previous = pcjk
                    --    else
                    --        previous = pcjk
                        end
                    else
                        previous = "start"
                    end
                else
                    previous = "start"
                end
            end
            if upcoming == last then -- was stop
                break
            else
                first = upcoming
            end
        end
    end
end

scripts.install {
    name = "hanzi",
    process = process,
}