-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluatexko.lua
2438 lines (2200 loc) · 72.1 KB
/
luatexko.lua
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- luatexko.lua
--
-- Copyright (c) 2013-2023 Dohyun Kim <nomos at ktug org>
-- Soojin Nam <jsunam at gmail com>
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3c
-- of this license or (at your option) any later version.
-- The latest version of this license is in
-- http://www.latex-project.org/lppl.txt
-- and version 1.3c or later is part of all distributions of LaTeX
-- version 2006/05/20 or later.
luatexbase.provides_module {
name = 'luatexko',
date = '2023/09/11',
version = '3.6',
description = 'typesetting Korean with LuaTeX',
author = 'Dohyun Kim, Soojin Nam',
license = 'LPPL v1.3+',
}
luatexko = luatexko or {}
local luatexko = luatexko
local dimensions = node.dimensions
local end_of_math = node.end_of_math
local getglue = node.getglue
local getnext = node.getnext
local getprev = node.getprev
local getproperty = node.getproperty
local has_attribute = node.has_attribute
local has_glyph = node.has_glyph
local insert_after = node.insert_after
local insert_before = node.insert_before
local nodecopy = node.copy
local nodecount = node.count
local nodefree = node.free
local nodenew = node.new
local noderemove = node.remove
local nodeslide = node.slide
local nodewrite = node.write
local rangedimensions = node.rangedimensions
local set_attribute = node.set_attribute
local setglue = node.setglue
local setproperty = node.setproperty
local unset_attribute = node.unset_attribute
local fontcurrent = font.current
local fontfonts = font.fonts
local fontgetfont = font.getfont
local getparameters = font.getparameters
local texattribute = tex.attribute
local texcount = tex.count
local texset = tex.set
local texsp = tex.sp
local set_macro = token.set_macro
local mathmax = math.max
local stringformat = string.format
local stringunpack = string.unpack
local tableconcat = table.concat
local tableinsert = table.insert
local tableunpack = table.unpack
local add_to_callback = luatexbase.add_to_callback
local attributes = luatexbase.attributes
local call_callback = luatexbase.call_callback
local create_callback = luatexbase.create_callback
local module_warning = luatexbase.module_warning
local new_attribute = luatexbase.new_attribute
local new_user_whatsit = luatexbase.new_user_whatsit
local registernumber = luatexbase.registernumber
local function warning (...)
return module_warning("luatexko", stringformat(...))
end
local dirid = node.id"dir"
local discid = node.id"disc"
local glueid = node.id"glue"
local glyphid = node.id"glyph"
local hlistid = node.id"hlist"
local kernid = node.id"kern"
local localparid = node.id"local_par"
local mathid = node.id"math"
local penaltyid = node.id"penalty"
local ruleid = node.id"rule"
local vlistid = node.id"vlist"
local whatsitid = node.id"whatsit"
local literal_whatsit = node.subtype"pdf_literal"
local directmode = 2
local fontkern = 0
local userkern = 1
local italcorr = 3
local lua_number = 100
local lua_value = 108
local spaceskip = 13
local indentbox = 3
local nohyphen = registernumber"l@nohyphenation" or -1 -- verbatim
local langkor = registernumber"koreanlanguage" or 16383
local hangulfontattr = attributes.luatexkohangulfontattr
local hanjafontattr = attributes.luatexkohanjafontattr
local fallbackfontattr = attributes.luatexkofallbackfontattr
local autojosaattr = attributes.luatexkoautojosaattr
local classicattr = attributes.luatexkoclassicattr
local dotemphattr = attributes.luatexkodotemphattr
local rubyattr = attributes.luatexkorubyattr
local hangulbyhangulattr = attributes.luatexkohangulbyhangulattr
local hanjabyhanjaattr = attributes.luatexkohanjabyhanjaattr
local unicodeattr = new_attribute"luatexkounicodeattr"
local stretch_f = 5/100 -- should be consistent for ruby
local function get_font_data (fontid)
return fontgetfont(fontid) or fontfonts[fontid] or {}
end
local function get_font_param (f, key)
local t
if type(f) == "number" then
t = getparameters(f)
if t and t[key] then
return t[key]
end
f = get_font_data(f)
end
if type(f) == "table" then
t = f.parameters
return t and t[key]
end
end
local function option_in_font (fontdata, optionname)
if type(fontdata) == "number" then
fontdata = get_font_data(fontdata)
end
if fontdata.shared then
return fontdata.shared.features[optionname]
end
end
local function font_opt_dim (fd, optname)
local dim = option_in_font(fd, optname)
if dim then
local params, m, u
if type(fd) == "number" then
params = getparameters(fd)
else
params = fd.parameters
end
if type(dim) == "string" then
m, u = dim:match"^(.+)(e[mx])%s*$"
end
if m and u and params then
if u == "em" then
dim = m * params.quad
else
dim = m * params.x_height
end
else
dim = texsp(dim)
end
return dim
end
end
local function has_harf_data (f)
if type(f) == "number" then
f = get_font_data(f)
end
return f.hb
end
local harfbuzz = luaotfload.harfbuzz
local os2tag = harfbuzz and harfbuzz.Tag.new"OS/2"
local fontoptions = {
is_not_harf = setmetatable( {}, { __index = function (t, fid)
if fid then
local bool
if has_harf_data(fid) then
bool = false
else
bool = true
end
t[fid] = bool
return bool
end
end }),
mode = setmetatable( {}, { __index = function (t, fid)
if fid then
local m = option_in_font(fid, "mode") or false
if m == "harf" and not has_harf_data(fid) then
m = "node" -- default mode when 'mode=harf' in non-luahbtex
end
t[fid] = m
return m
end
end }),
is_widefont = setmetatable( {}, { __index = function(t, fid)
if fid then
local fontdata = get_font_data(fid)
local format = fontdata.format
local encode = fontdata.encodingbytes
local bool = encode == 2 or format == "opentype" or format == "truetype"
t[fid] = bool
return bool
end
end }),
is_hangulscript = setmetatable( {}, { __index = function(t, fid)
if fid then
local bool = option_in_font(fid, "script") == "hang"
t[fid] = bool
return bool
end
end }),
compresspunctuations = setmetatable( {}, { __index = function(t, fid)
if fid then
local bool = option_in_font(fid, "compresspunctuations") or false
t[fid] = bool
return bool
end
end }),
removeclassicspaces = setmetatable( {}, { __index = function(t, fid)
if fid then
local bool = option_in_font(fid, "removeclassicspaces") or false
t[fid] = bool
return bool
end
end }),
slantvalue = setmetatable( {}, { __index = function(t, fid)
if fid then
local val = option_in_font(fid, "slant") or false
t[fid] = val
return val
end
end }),
charraise = setmetatable( {}, { __index = function(t, fid)
if fid then
local dim = font_opt_dim(fid, "charraise") or false
t[fid] = dim
return dim
end
end }),
intercharacter = setmetatable( {}, { __index = function(t, fid)
if fid then
local dim = font_opt_dim(fid, "intercharacter") or false
t[fid] = dim
return dim
end
end }),
intercharstretch = setmetatable( {}, { __index = function(t, fid)
if fid then
local dim = font_opt_dim(fid, "intercharstretch") or false
t[fid] = dim
return dim
end
end }),
interhangul = setmetatable( {}, { __index = function(t, fid)
if fid then
local dim = font_opt_dim(fid, "interhangul") or false
t[fid] = dim
return dim
end
end }),
interlatincjk = setmetatable( {}, { __index = function(t, fid)
if fid then
local dim = font_opt_dim(fid, "interlatincjk") or false
t[fid] = dim
return dim
end
end }),
en_size = setmetatable( {}, { __index = function(t, fid)
if fid then
val = (get_font_param(fid, "quad") or 655360)/2
t[fid] = val
return val
end
return 327680
end } ),
hangulspaceskip = setmetatable( {}, { __index = function(t, fid)
if fid then
local newwd
if has_harf_data(fid) then
newwd = getparameters(fid) or false
if newwd then
newwd = { newwd.space, newwd.space_stretch, newwd.space_shrink, newwd.extra_space }
end
else
local newsp = nodenew(glyphid)
newsp.char, newsp.font = 32, fid
newsp = nodes.simple_font_handler(newsp)
newwd = newsp and newsp.width or false
if newwd then
newwd = { texsp(newwd), texsp(newwd/2), texsp(newwd/3), texsp(newwd/3) }
end
if newsp then nodefree(newsp) end
end
t[fid] = newwd
return newwd
end
end } ),
monospaced = setmetatable( {}, { __index = function(t, fid)
if fid then
-- space_stretch has been set to zero by fontloader
if get_font_param(fid, "space_stretch") == 0 then
t[fid] = true; return true
end
-- but not in harf mode; so we simply test widths of some glyphs
local chars = get_font_data(fid).characters or {}
local i, M = chars[0x69], chars[0x4D]
if i and M and i.width == M.width then
t[fid] = true; return true
end
t[fid] = false; return false
end
end } ),
tonemark_xmax = setmetatable( {}, { __index = function(t, fid)
if fid then
-- check horizontal metric
local fontdata = get_font_data(fid)
local shared = fontdata.shared or {}
local rawdata = shared.rawdata or {}
local descriptions = rawdata.descriptions or {}
local description = descriptions[0x302E] or {}
local bbox = description.boundingbox or {}
local xmax = bbox[3] or -1
t[fid] = xmax
return xmax
end
return 0
end } ),
asc_desc = setmetatable( {}, { __index = function(t, fid)
if fid then
local asc, desc
-- luaharfbuzz's Font:get_h_extents() gets ascender value from hhea table;
-- Node mode's parameters.ascender is gotten from OS/2 table.
-- TypoAscender in OS/2 table seems to be more suitable for our purpose.
local hb = has_harf_data(fid)
if hb and os2tag then
local hbface = hb.shared.face
local tags = hbface:get_table_tags()
local hasos2 = false
for _,v in ipairs(tags) do
if v == os2tag then
hasos2 = true
break
end
end
if hasos2 then
local os2 = hbface:get_table(os2tag)
local length = os2:get_length()
if length > 69 then -- sTypoAscender (int16)
local data = os2:get_data()
local typoascender = stringunpack(">h", data, 69)
local typodescender = stringunpack(">h", data, 71)
asc = typoascender * hb.scale
desc = -typodescender * hb.scale
end
end
end
asc = asc or get_font_param(fid, "ascender") or false
desc = desc or get_font_param(fid, "descender") or false
t[fid] = { asc, desc }
return { asc, desc }
end
return { }
end } ),
}
local function char_in_font(fontdata, char)
if type(fontdata) == "number" then
fontdata = get_font_data(fontdata)
end
if fontdata.characters then
return fontdata.characters[char]
end
end
local function harf_reordered_tonemark (curr)
if not fontoptions.is_not_harf[curr.font] then
local props = getproperty(curr) or {}
local actualtext = props.luaotfload_startactualtext or ""
return actualtext:find"302[EF]$"
end
end
local function my_node_props (n)
local t = getproperty(n)
if not t then
t = {}
setproperty(n, t)
end
t.luatexko = t.luatexko or {}
return t.luatexko
end
local function is_hanja (c)
return c >= 0x3400 and c <= 0xA4C6
or c >= 0xF900 and c <= 0xFAFF
or c >= 0x20000 and c <= 0x3FFFD
or c >= 0x2E81 and c <= 0x2FD5
end
local function is_hangul (c)
return c >= 0xAC00 and c <= 0xD7A3
end
local function is_chosong (c)
return c >= 0x1100 and c <= 0x115F
or c >= 0xA960 and c <= 0xA97C
end
local function is_jungsong (c)
return c >= 0x1160 and c <= 0x11A7
or c >= 0xD7B0 and c <= 0xD7C6
end
local function is_jongsong (c)
return c >= 0x11A8 and c <= 0x11FF
or c >= 0xD7CB and c <= 0xD7FB
end
local hangul_tonemark = {
[0x302E] = true, [0x302F] = true,
}
local function is_compat_jamo (c)
return c >= 0x3131 and c <= 0x318E
end
local function is_combining (c)
return c >= 0x302A and c <= 0x302F
or c == 0x3099 or c == 0x309A
-- variation selectors
or c >= 0xFE00 and c <= 0xFE0F
or c >= 0xE0100 and c <= 0xE01EF
-- others (probably non-cjk)
or c >= 0x0300 and c <= 0x036F
or c >= 0x1AB0 and c <= 0x1AFF
or c >= 0x1DC0 and c <= 0x1DFF
or c >= 0x20D0 and c <= 0x20FF
or c >= 0xFE20 and c <= 0xFE2F
end
local function is_noncjk_char (c)
return c >= 0x30 and c <= 0x39
or c >= 0x41 and c <= 0x5A
or c >= 0x61 and c <= 0x7A
or c >= 0xC0 and c <= 0xD6
or c >= 0xD8 and c <= 0xF6
or c >= 0xF8 and c <= 0x10FF
or c >= 0x1200 and c <= 0x1FFF
or c >= 0xA4D0 and c <= 0xA95F
or c >= 0xA980 and c <= 0xABFF
or c >= 0xFB00 and c <= 0xFDFF
or c >= 0xFE70 and c <= 0xFEFF
end
local function is_kana (c)
return c >= 0x3041 and c <= 0x3096
or c >= 0x30A1 and c <= 0x30FA
or c >= 0x31F0 and c <= 0x31FF
or c >= 0xFF66 and c <= 0xFF6F
or c >= 0xFF71 and c <= 0xFF9D
or c == 0x309F or c == 0x30FF
or c >= 0x1B000 and c <= 0x1B16F
end
local function is_hangul_jamo (c)
return is_hangul(c)
or is_compat_jamo(c)
or is_chosong(c)
or is_jungsong(c)
or is_jongsong(c)
end
local intercharclass = { [0] =
{ [0] = nil, {1,1}, nil, {.5,.5} },
{ [0] = nil, nil, nil, {.5,.5} }, -- openers
{ [0] = {1,1}, {1,1}, nil, {.5,.5}, nil, {1,1}, {1,1} }, -- closers
{ [0] = {.5,.5},{.5,.5},{.5,.5},{1,.5}, {.5,.5},{.5,.5},{.5,.5},{.5,.5} }, -- middle dots
{ [0] = {1,0}, {1,0}, nil, {1.5,.5},nil, {1,0}, {1,0} }, -- full stops
{ [0] = nil, {1,1}, nil, {.5,.5} }, -- leaders and ellipses
{ [0] = {1,1}, {1,1}, nil, {.5,.5} }, -- questions and exclamations
{ [0] = {.5,.5},{.5,.5},nil, {.5,.5} }, -- vertical colons
}
local charclass = setmetatable({
[0x2018] = 1, [0x201C] = 1, [0x2329] = 1, [0x3008] = 1,
[0x300A] = 1, [0x300C] = 1, [0x300E] = 1, [0x3010] = 1,
[0x3014] = 1, [0x3016] = 1, [0x3018] = 1, [0x301A] = 1,
[0x301D] = 1, [0xFE17] = 1, [0xFE35] = 1, [0xFE37] = 1,
[0xFE39] = 1, [0xFE3B] = 1, [0xFE3D] = 1, [0xFE3F] = 1,
[0xFE41] = 1, [0xFE43] = 1, [0xFE47] = 1, [0xFF08] = 1,
[0xFF3B] = 1, [0xFF5B] = 1, [0xFF5F] = 1, [0xFF62] = 1,
--
[0x2019] = 2, [0x201D] = 2, [0x232A] = 2, [0x3001] = 2,
[0x3009] = 2, [0x300B] = 2, [0x300D] = 2, [0x300F] = 2,
[0x3011] = 2, [0x3015] = 2, [0x3017] = 2, [0x3019] = 2,
[0x301B] = 2, [0x301E] = 2, [0x301F] = 2, [0xFE10] = 2,
[0xFE11] = 2, [0xFE18] = 2, [0xFE36] = 2, [0xFE38] = 2,
[0xFE3A] = 2, [0xFE3C] = 2, [0xFE3E] = 2, [0xFE40] = 2,
[0xFE42] = 2, [0xFE44] = 2, [0xFE48] = 2, [0xFF09] = 2,
[0xFF0C] = 2, [0xFF3D] = 2, [0xFF5D] = 2, [0xFF60] = 2,
[0xFF63] = 2, [0xFF64] = 2,
--
[0x00B7] = 3, [0x30FB] = 3, [0xFF1A] = 3, [0xFF1B] = 3,
[0xFF65] = 3,
--
[0x3002] = 4, [0xFE12] = 4, [0xFF0E] = 4, [0xFF61] = 4,
--
[0x2015] = 5, [0x2025] = 5, [0x2026] = 5, [0xFE19] = 5,
[0xFE30] = 5, [0xFE31] = 5,
--
[0xFE15] = 6, [0xFE16] = 6, [0xFF01] = 6, [0xFF1F] = 6,
}, { __index = function() return 0 end })
local special_classes = {
[0] = charclass,
setmetatable({ -- vert
[0xFF1A] = 7, [0xFF1B] = 7, -- 0xFE13, 0xFE14
}, { __index = charclass }),
setmetatable({ -- SC
[0xFF01] = 4, [0xFF1A] = 2, [0xFF1B] = 2, [0xFF1F] = 4,
}, { __index = charclass }),
setmetatable({ -- TC
[0x3001] = 3, [0x3002] = 3, [0xFF0C] = 3, [0xFF0E] = 3,
}, { __index = charclass }),
setmetatable({ -- TC vert
[0x3001] = 3, [0x3002] = 3, [0xFF0C] = 3, [0xFF0E] = 3,
[0xFF1A] = 7, [0xFF1B] = 7, -- 0xFE13, 0xFE14
}, { __index = charclass }),
setmetatable({ -- JP vert
[0xFF1B] = 7, -- 0xFE14
}, { __index = charclass }),
}
local function get_char_class (c, classic)
return special_classes[classic or 0][c]
end
local breakable_after = setmetatable({
[0x21] = true, [0x22] = true, [0x25] = true, [0x27] = true,
[0x29] = true, [0x2C] = true, [0x2D] = true, [0x2E] = true,
[0x3A] = true, [0x3B] = true, [0x3E] = true, [0x3F] = true,
[0x5D] = true, [0x7D] = true, [0x7E] = true, [0xBB] = true,
[0x2013] = true, [0x2014] = true, [0x25A1] = true, [0x25CB] = true,
[0x2E80] = true, [0x3003] = true, [0x3005] = true, [0x3007] = true,
[0x301C] = true, [0x3035] = true, [0x303B] = true, [0x303C] = true,
[0x309B] = true, [0x309C] = true,
[0x309D] = true, [0x309E] = true, [0x30A0] = true, [0x30FC] = true,
[0x30FD] = true, [0x30FE] = true, [0xFE13] = true, [0xFE14] = true,
[0xFE32] = true, [0xFE50] = true, [0xFE51] = true, [0xFE52] = true,
[0xFE54] = true, [0xFE55] = true, [0xFE57] = true, [0xFE57] = true,
[0xFE58] = true, [0xFE5A] = true, [0xFE5C] = true, [0xFE5E] = true,
[0xFF1E] = true, [0xFF5E] = true, [0xFF70] = true, [0x226B] = true, -- ≫
[0xFF9E] = true, [0xFF9F] = true,
},{ __index = function (_,c)
return is_hangul_jamo(c) -- chosong also is breakable_after
or is_noncjk_char(c)
or is_hanja(c)
or is_combining(c)
or is_kana(c)
or charclass[c] >= 2
end })
luatexko.breakableafter = breakable_after
local breakable_before = setmetatable({
[0x28] = true, [0x3C] = true, [0x5B] = true, [0x60] = true,
[0x7B] = true, [0xAB] = true, [0x25A1] = true, [0x25CB] = true,
[0x3007] = true, [0xFE59] = true, [0xFE5B] = true, [0xFE5D] = true,
[0xFF1C] = true, [0x226A] = true, -- ≪
-- small kana
[0x3041] = 1000, [0x3043] = 1000, [0x3045] = 1000, [0x3047] = 1000,
[0x3049] = 1000, [0x3063] = 1000, [0x3083] = 1000, [0x3085] = 1000,
[0x3087] = 1000, [0x308E] = 1000, [0x3095] = 1000, [0x3096] = 1000,
[0x30A1] = 1000, [0x30A3] = 1000, [0x30A5] = 1000, [0x30A7] = 1000,
[0x30A9] = 1000, [0x30C3] = 1000, [0x30E3] = 1000, [0x30E5] = 1000,
[0x30E7] = 1000, [0x30EE] = 1000, [0x30F5] = 1000, [0x30F6] = 1000,
[0x31F0] = 1000, [0x31F1] = 1000, [0x31F2] = 1000, [0x31F3] = 1000,
[0x31F4] = 1000, [0x31F5] = 1000, [0x31F6] = 1000, [0x31F7] = 1000,
[0x31F8] = 1000, [0x31F9] = 1000, [0x31FA] = 1000, [0x31FB] = 1000,
[0x31FC] = 1000, [0x31FD] = 1000, [0x31FE] = 1000, [0x31FF] = 1000,
[0xFF67] = 1000, [0xFF68] = 1000, [0xFF69] = 1000, [0xFF6A] = 1000,
[0xFF6B] = 1000, [0xFF6C] = 1000, [0xFF6D] = 1000, [0xFF6E] = 1000,
[0xFF6F] = 1000, [0x1B150] = 1000, [0x1B151] = 1000, [0x1B152] = 1000,
[0x1B164] = 1000, [0x1B165] = 1000, [0x1B166] = 1000, [0x1B167] = 1000,
-- nonstarter
[0xA015] = false, -- YI SYLLABLE WU
},{ __index = function(_,c)
return is_hangul(c)
or is_compat_jamo(c)
or is_chosong(c)
or is_hanja(c)
or is_kana(c)
or charclass[c] == 1
end
})
luatexko.breakablebefore = breakable_before
local function is_cjk_char (c)
return is_hangul_jamo(c)
or is_hanja(c)
or hangul_tonemark[c]
or is_kana(c)
or charclass[c] >= 1
or rawget(breakable_before, c) and c >= 0x2000
or rawget(breakable_after, c) and c >= 0x2000
end
local active_processes = {}
-- font fallback
local force_hangul = {
[0x21] = true, -- !
[0x27] = true, -- '
[0x28] = true, -- (
[0x29] = true, -- )
[0x2C] = true, -- ,
[0x2E] = true, -- .
[0x3A] = true, -- :
[0x3B] = true, -- ;
[0x3F] = true, -- ?
[0x60] = true, -- `
[0xB7] = true, -- ·
[0x2014] = true, -- —
[0x2015] = true, -- ―
[0x2018] = true, -- ‘
[0x2019] = true, -- ’
[0x201C] = true, -- “
[0x201D] = true, -- ”
[0x2026] = true, -- …
[0x203B] = true, -- ※
}
luatexko.forcehangulchars = force_hangul
local forcehf_f, forcehf_id = new_user_whatsit("forcehf","luatexko")
function luatexko.updateforcehangul (value)
local what = forcehf_f()
what.type = lua_value -- function
what.value = value
nodewrite(what)
end
local function process_fonts (head)
local curr, currfont, currlang, newfont = head, 0, nohyphen, 0
while curr do
local id = curr.id
if id == glyphid then
currfont, currlang = curr.font, curr.lang
if curr.font ~= 0 -- exclude nullfont
and not has_attribute(curr, unicodeattr) then
local c = curr.char
local done
if is_combining(c) then
local p = getprev(curr)
if p and p.id == glyphid then
if curr.font ~= p.font then
curr.font = p.font
end
curr.lang = p.lang
if hangul_tonemark[c] then
if not active_processes.reorderTM and
fontoptions.is_not_harf[curr.font] and
fontoptions.is_hangulscript[curr.font] then
luatexko.activate("reorderTM") -- activate reorderTM here
end
set_attribute(curr, unicodeattr, c)
else
curr.attr = p.attr -- inherit previous attr including unicodeattr
end
done = true
end
end
if not done then
if curr.subtype == 1 and curr.lang ~= nohyphen and is_cjk_char(c) then
curr.lang = langkor -- suppress hyphenation of cjk chars
end
local hf = has_attribute(curr, hangulfontattr) or false
local hjf = has_attribute(curr, hanjafontattr) or false
if hf and force_hangul[c]
and fontoptions.is_widefont[curr.font] -- exclude legacy fonts
and curr.lang ~= nohyphen and not fontoptions.monospaced[curr.font] -- exclude ttfamily
then
curr.font = hf
elseif hf and has_attribute(curr, hangulbyhangulattr) and is_hangul_jamo(c) then
curr.font = hf
elseif hjf and has_attribute(curr, hanjabyhanjaattr) and is_hanja(c) then
curr.font = hjf
elseif not char_in_font(curr.font, c) then
local fbf = has_attribute(curr, fallbackfontattr) or false
for _,f in ipairs{ hf, hjf, fbf } do
if f and char_in_font(f, c) then
curr.font = f
break
end
end
end
set_attribute(curr, unicodeattr, c)
end
end
newfont = curr.font
elseif id == glueid
and currfont ~= 0
and currfont ~= newfont
and currlang ~= nohyphen
and curr.subtype == spaceskip
-- fontloader's "node" mode sets space_stretch to zero
-- when the font is a monospaced font (fontspec's \setmonofont
-- command does the same thing), which we will bypass here
-- for alignment of CJK and Latin glyphs in verbatim environment.
-- See http://www.ktug.org/xe/index.php?document_srl=249772
and not fontoptions.monospaced[currfont] then
local params = getparameters(currfont)
local oldwd, oldst, oldsh, oldsto, oldsho = getglue(curr)
if params and oldsto == 0 and oldsho == 0 then
local p = getprev(curr)
local sf = p and p.char and tex.getsfcode(p.char) or 1000
if sf == 0 or sf > 1000 then
local p, pf = getprev(p), 0
while p and pf == 0 do
pf = p.char and tex.getsfcode(p.char) or 1000
p = getprev(p)
end
if sf == 0 then sf = pf end
if pf < 1000 then sf = 1000 end
end
if oldwd == (sf < 2000 and params.space or params.space+params.extra_space)
and oldst == texsp(params.space_stretch * (sf/1000))
and oldsh == texsp(params.space_shrink * (1000/sf)) then
local newwd = fontoptions.hangulspaceskip[newfont]
if newwd then
setglue(curr,
sf < 2000 and newwd[1] or newwd[1]+newwd[4],
texsp(newwd[2] * (sf/1000)),
texsp(newwd[3] * (1000/sf)))
end
end
end
elseif id == discid then
process_fonts(curr.pre)
process_fonts(curr.post)
process_fonts(curr.replace)
elseif id == mathid then
curr = end_of_math(curr)
elseif id == whatsitid and
curr.user_id == forcehf_id and
curr.type == lua_value then
local value = curr.value
if type(value) == "function" then
value()
end
end
curr = getnext(curr)
end
end
-- linebreak
local allowbreak_false_nodes = {
[hlistid] = true,
[vlistid] = true,
[ruleid] = true,
[discid] = true,
[glueid] = true,
[penaltyid] = true,
}
local function is_blocking_node (curr)
local id, subtype = curr.id, curr.subtype
return allowbreak_false_nodes[id] or id == kernid and subtype == userkern
end
local function hbox_char_font (box, init, glyfonly)
local mynext = init and getnext or getprev
local curr = init and box.list or nodeslide(box.list)
while curr do
local id = curr.id
if id == glyphid then
local c = has_attribute(curr, unicodeattr) or curr.char
if c and not is_combining(c) then
return c, curr.font
end
elseif curr.list then
return hbox_char_font(curr, init, glyfonly)
elseif not glyfonly and is_blocking_node(curr) then
return
end
curr = mynext(curr)
end
end
local function get_actualtext (curr)
local actual = my_node_props(curr).startactualtext
if type(actual) == "table" then
return actual.init, actual[1], actual[#actual]
end
end
local function goto_end_actualtext (curr)
local n = getnext(curr)
while n do
if n.id == whatsitid and
n.mode == directmode and
my_node_props(n).endactualtext then
curr = n; break
end
n = getnext(n)
end
return curr
end
local function insert_glue_before (head, curr, par, br, brb, classic, ict, dim, fid)
local pn = nodenew(penaltyid)
if not br then
pn.penalty = 10000
elseif type(brb) == "number" then
pn.penalty = brb
elseif par and nodecount(glyphid, curr) <= 2 then
pn.penalty = 1000 -- supress orphan
else
pn.penalty = 50
end
dim = dim or 0
local gl = nodenew(glueid)
local en = fontoptions.en_size[fid]
if ict then
en = classic and en or en/4
setglue(gl, en * ict[1] + dim, nil, en * ict[2])
else
local str = fontoptions.intercharstretch[fid] or stretch_f*en
setglue(gl, dim, str, str*0.6)
end
head = insert_before(head, curr, pn)
return insert_before(head, curr, gl)
end
local function maybe_linebreak (head, curr, pc, pcl, cc, old, fid, par)
local ccl = get_char_class(cc, old)
if pc and cc and curr.lang ~= nohyphen then
local ict = intercharclass[pcl][ccl]
local brb = breakable_before[cc]
local br = brb and breakable_after[pc]
local dim = fontoptions.intercharacter[fid]
if ict or br or dim and (pcl >= 1 or ccl >= 1) then
head = insert_glue_before(head, curr, par, br, brb, old, ict, dim, fid)
end
end
return head, cc, ccl
end
local function process_linebreak (head, par)
local curr, pc, pcl = head, false, 0
while curr do
local id = curr.id
if id == glyphid then
local c = has_attribute(curr, unicodeattr) or curr.char
if c and not is_combining(curr.char) then -- we are in pre-shaping stage
local old = has_attribute(curr, classicattr)
head, pc, pcl = maybe_linebreak(head, curr, pc, pcl, c, old, curr.font, par)
end
elseif id == hlistid and curr.list then
local old = has_attribute(curr, classicattr)
local c, f = hbox_char_font(curr, true)
if c and f then
head = maybe_linebreak(head, curr, pc, pcl, c, old, f, par)
end
pc = hbox_char_font(curr)
pcl = pc and get_char_class(pc, old) or 0
elseif id == whatsitid and curr.mode == directmode then
local glyf, c, fin = get_actualtext(curr)
if c and fin and glyf then
local old = has_attribute(glyf, classicattr)
head = maybe_linebreak(head, curr, pc, pcl, c, old, glyf.font, par)
pc, pcl, curr = fin, 0, goto_end_actualtext(curr)
end
elseif id == mathid then
pc, pcl, curr = 0x30, 0, end_of_math(curr)
elseif id == dirid then
pc, pcl = curr.dir:sub(1,1) == "-" and 0x30, 0 -- pop dir
elseif is_blocking_node(curr) then
pc, pcl = false, 0
end
curr = getnext(curr)
end
return head
end
-- interhangul & interlatincjk
local function do_interhangul_option (head, curr, pc, c, fontid, par)
local cc = (is_hangul(c) or is_compat_jamo(c) or is_chosong(c)) and 1 or 0
if cc*pc == 1 and curr.lang ~= nohyphen then
local dim = fontoptions.interhangul[fontid]
if dim then
head = insert_glue_before(head, curr, par, true, true, false, false, dim, fontid)
end
end
return head, cc
end
local function process_interhangul (head, par)
local curr, pc = head, 0
while curr do
local id = curr.id
if id == glyphid then
local c = has_attribute(curr, unicodeattr) or curr.char
if c and not is_combining(curr.char) then -- we are in pre-shaping stage
head, pc = do_interhangul_option(head, curr, pc, c, curr.font, par)
if is_jungsong(c) or is_jongsong(c) or hangul_tonemark[c] then
pc = 1
end
end
elseif id == hlistid and curr.list then
local c, f = hbox_char_font(curr, true)
if c and f then
head = do_interhangul_option(head, curr, pc, c, f, par)
end
c = hbox_char_font(curr)
pc = c and is_hangul_jamo(c) and 1 or 0
elseif id == whatsitid and curr.mode == directmode then
local glyf, c = get_actualtext(curr)
if c and glyf then
head = do_interhangul_option(head, curr, pc, c, glyf.font, par)
pc, curr = 1, goto_end_actualtext(curr)
end
elseif id == mathid then
pc, curr = 0, end_of_math(curr)
elseif is_blocking_node(curr) or id == dirid then
pc = 0
end
curr = getnext(curr)
end
return head
end
local function do_interlatincjk_option (head, curr, pc, pf, pcl, c, cf, par)
local cc = is_cjk_char(c) and 1 or is_noncjk_char(c) and 2 or 0
local old = has_attribute(curr, classicattr)
local ccl = get_char_class(c, old)
if cc*pc == 2 and curr.lang ~= nohyphen then
local brb = cc == 2 or breakable_before[c] -- numletter != br_before
if brb then
local f = cc == 1 and cf or pf
local dim = fontoptions.interlatincjk[f]
if dim then
local ict = old and intercharclass[pcl][ccl] -- under classic env. only
if ict then
dim = fontoptions.intercharacter[f] or 0
end
head = insert_glue_before(head, curr, par, true, brb, old, ict, dim, f)