Debian Codegraph @

texmf-dist/tex/luatex/luatexko/luatexko.lua

raw
1 -- luatexko.lua
2 --
3 -- Copyright (c) 2013-2026 Dohyun Kim <nomosnomos at gmail com>
4 -- Soojin Nam <jsunam at gmail com>
5 --
6 -- This work may be distributed and/or modified under the
7 -- conditions of the LaTeX Project Public License, either version 1.3c
8 -- of this license or (at your option) any later version.
9 -- The latest version of this license is in
10 -- http://www.latex-project.org/lppl.txt
11 -- and version 1.3c or later is part of all distributions of LaTeX
12 -- version 2006/05/20 or later.
13
14 luatexbase.provides_module {
15 name = 'luatexko',
16 date = '2026/02/20',
17 version = '5.7',
18 description = 'typesetting Korean with LuaTeX',
19 author = 'Dohyun Kim, Soojin Nam',
20 license = 'LPPL v1.3+',
21 }
22
23 luatexko = luatexko or {}
24 local luatexko = luatexko
25
26 local dimensions = node.dimensions
27 local end_of_math = node.end_of_math
28 local getglue = node.getglue
29 local getnext = node.getnext
30 local getprev = node.getprev
31 local getproperty = node.getproperty
32 local has_attribute = node.has_attribute
33 local has_glyph = node.has_glyph
34 local insert_after = node.insert_after
35 local insert_before = node.insert_before
36 local nodecopy = node.copy
37 local nodefree = node.free
38 local nodenew = node.new
39 local noderemove = node.remove
40 local nodeslide = node.slide
41 local nodewrite = node.write
42 local rangedimensions = node.rangedimensions
43 local set_attribute = node.set_attribute
44 local setglue = node.setglue
45 local setproperty = node.setproperty
46 local unset_attribute = node.unset_attribute
47
48 local fontgetfont = font.getfont
49 local getparameters = font.getparameters
50
51 local function warning (fmt, ...)
52 return luatexbase.module_warning("luatexko", fmt:format(...))
53 end
54
55 local dirid = node.id"dir"
56 local discid = node.id"disc"
57 local glueid = node.id"glue"
58 local glyphid = node.id"glyph"
59 local hlistid = node.id"hlist"
60 local ins_id = node.id"ins"
61 local kernid = node.id"kern"
62 local localparid = node.id"local_par"
63 local mathid = node.id"math"
64 local penaltyid = node.id"penalty"
65 local ruleid = node.id"rule"
66 local vlistid = node.id"vlist"
67 local whatsitid = node.id"whatsit"
68 local literal_whatsit = node.subtype"pdf_literal"
69 local save_whatsit = node.subtype"pdf_save"
70 local restore_whatsit = node.subtype"pdf_restore"
71 local matrix_whatsit = node.subtype"pdf_setmatrix"
72 local nohyphen = luatexbase.registernumber"l@nohyphenation" or -1 -- verbatim
73 local langkor = luatexbase.registernumber"koreanlanguage" or 16383
74
75 local hangulfontattr = luatexbase.attributes.luatexkohangulfontattr
76 local hanjafontattr = luatexbase.attributes.luatexkohanjafontattr
77 local fallbackfontattr = luatexbase.attributes.luatexkofallbackfontattr
78 local autojosaattr = luatexbase.attributes.luatexkoautojosaattr
79 local classicattr = luatexbase.attributes.luatexkoclassicattr
80 local dotemphattr = luatexbase.attributes.luatexkodotemphattr
81 local rubyattr = luatexbase.attributes.luatexkorubyattr
82 local hangulbyhangulattr = luatexbase.attributes.luatexkohangulbyhangulattr
83 local hanjabyhanjaattr = luatexbase.attributes.luatexkohanjabyhanjaattr
84 local inhibitglueattr = luatexbase.new_attribute"luatexko_inhibitglue_attr"
85 local unicodeattr = luatexbase.new_attribute"luatexko_unicode_attr"
86 local charhead = luatexbase.new_attribute"luatexko_char_head_attr"
87 local verticalattr -- set later at otfregister
88 local charraiseattr -- set later at otfregister
89
90 local stretch_f = 5/100 -- should be consistent for ruby
91
92 local function get_font_data (fontid)
93 return fontgetfont(fontid) or font.fonts[fontid] or {}
94 end
95
96 local function get_font_param (f, key)
97 local t
98 if type(f) == "number" then
99 t = getparameters(f)
100 if t and t[key] then
101 return t[key]
102 end
103 f = get_font_data(f)
104 end
105 if type(f) == "table" then
106 t = f.parameters
107 return t and t[key]
108 end
109 end
110
111 local function option_in_font (fontdata, optionname)
112 if type(fontdata) == "number" then
113 fontdata = get_font_data(fontdata)
114 end
115 if fontdata.shared then
116 return fontdata.shared.features[optionname]
117 end
118 end
119
120 local function font_opt_dim (fd, optname)
121 local dim = option_in_font(fd, optname)
122 if dim then
123 local params, m, u
124 if type(dim) == "string" then
125 m, u = dim:match"^(.+)(e[mx])%s*$"
126 end
127 if m and u then
128 local params = type(fd) == "number" and getparameters(fd) or fd.parameters
129 if params then
130 dim = u == "em" and m * params.quad or m * params.x_height
131 end
132 else
133 dim = tex.sp(dim)
134 end
135 return dim
136 end
137 end
138
139 local function has_harf_data (f)
140 if type(f) == "number" then
141 f = get_font_data(f)
142 end
143 return f.hb
144 end
145
146 local harfbuzz = luaotfload.harfbuzz
147 local get_asc_desc
148 if harfbuzz then
149 local os2tag = harfbuzz.Tag.new"OS/2"
150 function get_asc_desc (hb) -- fontdata.hb
151 if hb and os2tag then
152 local hbface = hb.shared.face
153 local os2 = hbface:get_table(os2tag)
154 if os2:get_length() > 69 then -- sTypoAscender (int16)
155 local data = os2:get_data()
156 local typoascender = string.unpack(">h", data, 69)
157 local typodescender = string.unpack(">h", data, 71)
158 return typoascender * hb.scale, -typodescender * hb.scale
159 end
160 end
161 end
162 end
163
164 local fontoptions = {
165 is_widefont = setmetatable( {}, { __index = function(t, fid)
166 if fid then
167 local fontdata = get_font_data(fid)
168 local format = fontdata.format
169 local encode = fontdata.encodingbytes
170 local bool = encode == 2 or format == "opentype" or format == "truetype"
171 t[fid] = bool
172 return bool
173 end
174 end }),
175
176 is_hangulscript = setmetatable( {}, { __index = function(t, fid)
177 if fid then
178 local bool = option_in_font(fid, "script") == "hang"
179 t[fid] = bool
180 return bool
181 end
182 end }),
183
184 compresspunctuations = setmetatable( {}, { __index = function(t, fid)
185 if fid then
186 local bool = option_in_font(fid, "compresspunctuations")
187 and not option_in_font(fid, "halt")
188 and not option_in_font(fid, "vhal")
189 or false
190 t[fid] = bool
191 return bool
192 end
193 end }),
194
195 removeclassicspaces = setmetatable( {}, { __index = function(t, fid)
196 if fid then
197 local bool = option_in_font(fid, "removeclassicspaces") or false
198 t[fid] = bool
199 return bool
200 end
201 end }),
202
203 slantvalue = setmetatable( {}, { __index = function(t, fid)
204 if fid then
205 local val = option_in_font(fid, "slant") or false
206 t[fid] = val
207 return val
208 end
209 end }),
210
211 charraise = setmetatable( {}, { __index = function(t, fid)
212 if fid then
213 local dim = font_opt_dim(fid, "charraise") or false
214 t[fid] = dim
215 return dim
216 end
217 end }),
218
219 intercharacter = setmetatable( {}, { __index = function(t, fid)
220 if fid then
221 local dim = font_opt_dim(fid, "intercharacter") or false
222 t[fid] = dim
223 return dim
224 end
225 end }),
226
227 intercharstretch = setmetatable( {}, { __index = function(t, fid)
228 if fid then
229 local dim = font_opt_dim(fid, "intercharstretch") or false
230 t[fid] = dim
231 return dim
232 end
233 end }),
234
235 intercharpenalty = setmetatable( {}, { __index = function(t, fid)
236 if fid then
237 local pena = option_in_font(fid, "intercharpenalty") or false
238 t[fid] = pena
239 return pena
240 end
241 end }),
242
243 interhangul = setmetatable( {}, { __index = function(t, fid)
244 if fid then
245 local dim = font_opt_dim(fid, "interhangul") or false
246 t[fid] = dim
247 return dim
248 end
249 end }),
250
251 interlatincjk = setmetatable( {}, { __index = function(t, fid)
252 if fid then
253 local dim = font_opt_dim(fid, "interlatincjk") or false
254 t[fid] = dim
255 return dim
256 end
257 end }),
258
259 is_vertical = setmetatable( {}, { __index = function(t, fid)
260 if fid then
261 local vertical = option_in_font(fid, "vertical") or false
262 t[fid] = vertical
263 return vertical
264 end
265 end }),
266
267 en_size = setmetatable( {}, { __index = function(t, fid)
268 if fid then
269 local val = (get_font_param(fid, "quad") or 655360)/2
270 t[fid] = val
271 return val
272 end
273 return 327680
274 end } ),
275
276 hangulspaceskip = setmetatable( {}, { __index = function(t, fid)
277 if fid then
278 local newwd
279 if has_harf_data(fid) then
280 newwd = getparameters(fid) or false
281 if newwd then
282 newwd = { newwd.space, newwd.space_stretch, newwd.space_shrink, newwd.extra_space }
283 end
284 else
285 local newsp = nodenew(glyphid)
286 newsp.char, newsp.font = 32, fid
287 newsp = nodes.simple_font_handler(newsp) -- incorrect in vertical writing. backward compat.
288 newwd = newsp and newsp.width or false
289 if newwd then
290 newwd = { tex.sp(newwd), tex.sp(newwd/2), tex.sp(newwd/3), tex.sp(newwd/3) }
291 end
292 if newsp then nodefree(newsp) end
293 end
294 t[fid] = newwd
295 return newwd
296 end
297 end } ),
298
299 monospaced = setmetatable( {}, { __index = function(t, fid)
300 if fid then
301 -- space_stretch has been set to zero by fontloader
302 if get_font_param(fid, "space_stretch") == 0 then
303 t[fid] = true; return true
304 end
305 -- but not in harf mode; so we simply test widths of some glyphs
306 local chars = get_font_data(fid).characters or {}
307 local i, M = chars[0x69], chars[0x4D]
308 if i and M and i.width == M.width then
309 t[fid] = true; return true
310 end
311 t[fid] = false; return false
312 end
313 end } ),
314
315 asc_desc = setmetatable( {}, { __index = function(t, fid)
316 if fid then
317 local asc, desc = get_font_param(fid, "ascender"), get_font_param(fid, "descender")
318 -- luaharfbuzz's Font:get_h_extents() gets ascender value from hhea table;
319 -- Node mode's parameters.ascender is gotten from OS/2 table.
320 -- TypoAscender in OS/2 table seems to be more suitable for our purpose.
321 if not (asc and desc) then
322 asc, desc = get_asc_desc(has_harf_data(fid))
323 end
324 asc, desc = asc or false, desc or false
325 t[fid] = { asc, desc }
326 return { asc, desc }
327 end
328 return { }
329 end } ),
330
331 vertcharraise = setmetatable( {}, { __index = function(t, fid)
332 if fid then
333 local fontdata = get_font_data(fid)
334 local vertraise = fontdata and fontdata.vertcharraise or false
335 t[fid] = vertraise
336 return vertraise
337 end
338 end } ),
339
340 hb_char_bbox = { }, -- for vertical writing or fake slant
341 tsb_data = { }, -- for vertical writing
342 }
343
344 local function char_in_font(fontdata, char)
345 if type(fontdata) == "number" then
346 fontdata = get_font_data(fontdata)
347 end
348 if fontdata.characters then
349 return fontdata.characters[char]
350 end
351 end
352
353 local function harf_actual_literal (curr)
354 if curr.id == whatsitid and curr.subtype == literal_whatsit then
355 local data = curr.data
356 return data == "EMC" and 2 or data and data:find"BDC$" and 1 or false
357 end
358 end
359
360 local function is_hanja (c)
361 return c >= 0x3400 and c <= 0xA4C6
362 or c >= 0xF900 and c <= 0xFAFF
363 or c >= 0xFF10 and c <= 0xFF19
364 or c >= 0xFF21 and c <= 0xFF3A
365 or c >= 0xFF41 and c <= 0xFF5A
366 or c >= 0x20000 and c <= 0x3FFFD
367 or c >= 0x2E81 and c <= 0x2FD5
368 end
369
370 local function is_hangul (c)
371 return c >= 0xAC00 and c <= 0xD7A3
372 end
373
374 local function is_chosong (c)
375 return c >= 0x1100 and c <= 0x115F
376 or c >= 0xA960 and c <= 0xA97C
377 end
378
379 local function is_jungsong (c)
380 return c >= 0x1160 and c <= 0x11A7
381 or c >= 0xD7B0 and c <= 0xD7C6
382 end
383
384 local function is_jongsong (c)
385 return c >= 0x11A8 and c <= 0x11FF
386 or c >= 0xD7CB and c <= 0xD7FB
387 end
388
389 local hangul_tonemark = {
390 [0x302E] = true, [0x302F] = true,
391 }
392
393 local function is_compat_jamo (c)
394 return c >= 0x3131 and c <= 0x318E
395 end
396
397 local function is_combining (c)
398 return is_jungsong(c)
399 or is_jongsong(c)
400 or c >= 0x302A and c <= 0x302F
401 or c == 0x3099 or c == 0x309A or c == 0x3035
402 -- variation selectors
403 or c >= 0xFE00 and c <= 0xFE0F
404 or c >= 0xE0100 and c <= 0xE01EF
405 -- others (probably non-cjk)
406 or c >= 0x0300 and c <= 0x036F
407 or c >= 0x1AB0 and c <= 0x1AFF
408 or c >= 0x1DC0 and c <= 0x1DFF
409 or c >= 0x20D0 and c <= 0x20FF
410 or c >= 0xFE20 and c <= 0xFE2F
411 end
412
413 local function is_noncjk_char (c)
414 return c >= 0x30 and c <= 0x39
415 or c >= 0x41 and c <= 0x5A
416 or c >= 0x61 and c <= 0x7A
417 or c >= 0xC0 and c <= 0xD6
418 or c >= 0xD8 and c <= 0xF6
419 or c >= 0xF8 and c <= 0x10FF
420 or c >= 0x1200 and c <= 0x1FFF
421 or c >= 0xA4D0 and c <= 0xA95F
422 or c >= 0xA980 and c <= 0xABFF
423 or c >= 0xFB00 and c <= 0xFDFF
424 or c >= 0xFE70 and c <= 0xFEFF
425 end
426
427 local function is_kana (c)
428 return c >= 0x3041 and c <= 0x3096
429 or c >= 0x30A1 and c <= 0x30FA
430 or c >= 0x31F0 and c <= 0x31FF
431 or c >= 0xFF66 and c <= 0xFF6F
432 or c >= 0xFF71 and c <= 0xFF9D
433 or c == 0x309F or c == 0x30FF
434 or c >= 0x1B000 and c <= 0x1B16F
435 end
436
437 local function is_hangul_jamo (c)
438 return is_hangul(c)
439 or is_compat_jamo(c)
440 or is_chosong(c)
441 or is_jungsong(c)
442 or is_jongsong(c)
443 or hangul_tonemark[c]
444 end
445
446 local intercharclass = { [0] =
447 { [0] = nil, {1,1}, nil, {.5,.5} },
448 { [0] = nil, nil, nil, {.5,.5} }, -- openers
449 { [0] = {1,1}, {1,1}, nil, {.5,.5}, nil, {1,1}, {1,1} }, -- closers
450 { [0] = {.5,.5},{.5,.5},{.5,.5},{1,.5}, {.5,.5},{.5,.5},{.5,.5},{.5,.5} }, -- middle dots
451 { [0] = {1,0}, {1,0}, nil, {1.5,.5},nil, {1,0}, {1,0} }, -- full stops
452 { [0] = nil, {1,1}, nil, {.5,.5} }, -- leaders and ellipses
453 { [0] = {1,1}, {1,1}, nil, {.5,.5} }, -- questions and exclamations
454 { [0] = {.5,.5},{.5,.5},nil, {.5,.5} }, -- vertical colons
455 }
456
457 local charclass = setmetatable({
458 [0x2018] = 1, -- ‘
459 [0x201C] = 1, -- “
460 [0x2329] = 1, -- 〈
461 [0x3008] = 1, -- 〈
462 [0x300A] = 1, -- 《
463 [0x300C] = 1, -- 「
464 [0x300E] = 1, -- 『
465 [0x3010] = 1, -- 【
466 [0x3014] = 1, -- 〔
467 [0x3016] = 1, -- 〖
468 [0x3018] = 1, -- 〘
469 [0x301A] = 1, -- 〚
470 [0x301D] = 1, -- 〝 REVERSED DOUBLE PRIME QUOTATION MARK
471 [0xFE17] = 1, -- ︗
472 [0xFE35] = 1, -- ︵
473 [0xFE37] = 1, -- ︷
474 [0xFE39] = 1, -- ︹
475 [0xFE3B] = 1, -- ︻
476 [0xFE3D] = 1, -- ︽
477 [0xFE3F] = 1, -- ︿
478 [0xFE41] = 1, -- ﹁
479 [0xFE43] = 1, -- ﹃
480 [0xFE47] = 1, -- ﹇
481 [0xFF08] = 1, -- (
482 [0xFF3B] = 1, -- [
483 [0xFF5B] = 1, -- {
484 [0xFF5F] = 1, -- ⦅
485 --
486 [0x2019] = 2, -- ’
487 [0x201D] = 2, -- ”
488 [0x232A] = 2, -- 〉
489 [0x3001] = 2, -- 、
490 [0x3009] = 2, -- 〉
491 [0x300B] = 2, -- 》
492 [0x300D] = 2, -- 」
493 [0x300F] = 2, -- 』
494 [0x3011] = 2, -- 】
495 [0x3015] = 2, -- 〕
496 [0x3017] = 2, -- 〗
497 [0x3019] = 2, -- 〙
498 [0x301B] = 2, -- 〛
499 [0x301E] = 2, -- 〞 DOUBLE PRIME QUOTATION MARK
500 [0x301F] = 2, -- 〟 LOW DOUBLE PRIME QUOTATION MARK
501 [0xFE10] = 2, -- ︐
502 [0xFE11] = 2, -- ︑
503 [0xFE18] = 2, -- ︘
504 [0xFE36] = 2, -- ︶
505 [0xFE38] = 2, -- ︸
506 [0xFE3A] = 2, -- ︺
507 [0xFE3C] = 2, -- ︼
508 [0xFE3E] = 2, -- ︾
509 [0xFE40] = 2, -- ﹀
510 [0xFE42] = 2, -- ﹂
511 [0xFE44] = 2, -- ﹄
512 [0xFE48] = 2, -- ﹈
513 [0xFF09] = 2, -- )
514 [0xFF0C] = 2, -- ,
515 [0xFF3D] = 2, -- ]
516 [0xFF5D] = 2, -- }
517 [0xFF60] = 2, -- ⦆
518 --
519 [0x00B7] = 3, -- ·
520 [0x30FB] = 3, -- ・
521 [0xFF1A] = 3, -- :
522 [0xFF1B] = 3, -- ;
523 --
524 [0x3002] = 4, -- 。
525 [0xFE12] = 4, -- ︒
526 [0xFF0E] = 4, -- .
527 --
528 [0x2015] = 5, -- ―
529 [0x2025] = 5, -- ‥
530 [0x2026] = 5, -- …
531 [0xFE19] = 5, -- ︙
532 [0xFE30] = 5, -- ︰
533 [0xFE31] = 5, -- ︱
534 --
535 [0xFE15] = 6, -- ︕
536 [0xFE16] = 6, -- ︖
537 [0xFF01] = 6, -- !
538 [0xFF1F] = 6, -- ?
539 }, { __index = function() return 0 end })
540
541 local get_char_class
542 do
543 local special_classes = {
544 [0] = charclass,
545 setmetatable({ -- vert
546 [0xFF1A] = 7, [0xFF1B] = 7, -- 0xFE13, 0xFE14
547 }, { __index = charclass }),
548 setmetatable({ -- SC
549 [0xFF01] = 4, [0xFF1A] = 2, [0xFF1B] = 2, [0xFF1F] = 4,
550 }, { __index = charclass }),
551 setmetatable({ -- TC
552 [0x3001] = 3, [0x3002] = 3, [0xFF0C] = 3, [0xFF0E] = 3,
553 }, { __index = charclass }),
554 setmetatable({ -- TC vert
555 [0x3001] = 3, [0x3002] = 3, [0xFF0C] = 3, [0xFF0E] = 3,
556 [0xFF1A] = 7, [0xFF1B] = 7, -- 0xFE13, 0xFE14
557 }, { __index = charclass }),
558 setmetatable({ -- JP vert
559 [0xFF1B] = 7, -- 0xFE14
560 }, { __index = charclass }),
561 }
562
563 function get_char_class (c, classic)
564 return special_classes[classic or 0][c]
565 end
566 end
567
568 local breakable_after = setmetatable({
569 [0x21] = true, -- !
570 [0x22] = true, -- "
571 [0x25] = true, -- %
572 [0x27] = true, -- '
573 [0x29] = true, -- )
574 [0x2C] = true, -- ,
575 [0x2D] = true, -- -
576 [0x2E] = true, -- .
577 [0x3A] = true, -- :
578 [0x3B] = true, -- ;
579 [0x3E] = true, -- >
580 [0x3F] = true, -- ?
581 [0x5D] = true, -- ]
582 [0x7D] = true, -- }
583 [0x7E] = true, -- ~
584 [0xBB] = true, -- »
585 [0x226B] = true, -- ≫MUCH GREATER-THAN
586 [0x25A1] = true, -- □
587 [0x25CB] = true, -- ○
588 [0x2E80] = true, -- ⺀ CJK RADICAL REPEAT
589 [0x3000] = true,
590 [0x3003] = true, -- 〃 DITTO MARK
591 [0x3005] = true, -- 々
592 [0x3007] = true, -- 〇
593 [0x3013] = true, -- 〓 GETA MARK
594 [0x303B] = true, -- 〻 VERTICAL IDEOGRAPHIC ITERATION MARK
595 [0x309B] = true, -- ゛ KATAKANA-HIRAGANA VOICED SOUND MARK
596 [0x309C] = true, -- ゜ KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
597 [0x309D] = true, -- ゝ HIRAGANA ITERATION MARK
598 [0x309E] = true, -- ゞ HIRAGANA VOICED ITERATION MARK
599 [0x30A0] = true, -- ゠ KATAKANA-HIRAGANA DOUBLE HYPHEN
600 [0x30FC] = true, -- ー KATAKANA-HIRAGANA PROLONGED SOUND MARK
601 [0x30FD] = true, -- ヽ KATAKANA ITERATION MARK
602 [0x30FE] = true, -- ヾ KATAKANA VOICED ITERATION MARK
603 [0xFE13] = true, -- ︓
604 [0xFE14] = true, -- ︔
605 [0xFE50] = true, -- ﹐ SMALL COMMA
606 [0xFE51] = true, -- ﹑ SMALL IDEOGRAPHIC COMMA
607 [0xFE52] = true, -- ﹒ SMALL FULL STOP
608 [0xFE54] = true, -- ﹔
609 [0xFE55] = true, -- ﹕
610 [0xFE56] = true, -- ﹖
611 [0xFE57] = true, -- ﹗
612 [0xFE5A] = true, -- ﹚
613 [0xFE5C] = true, -- ﹜
614 [0xFE5E] = true, -- ﹞
615 [0xFF1E] = true, -- >
616 [0xFF70] = true, -- ー HALFWIDTH KATAKANA-HIRAGANA PROLONGED SOUND MARK
617 [0xFF9E] = true, -- ゙ HALFWIDTH KATAKANA VOICED SOUND MARK
618 [0xFF9F] = true, -- ゚ HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
619 [0x3031] = true, -- 〱 VERTICAL KANA REPEAT MARK
620 [0x3032] = true, -- 〲 VERTICAL KANA REPEAT WITH VOICED SOUND MARK
621 [0x3033] = true, -- 〳 VERTICAL KANA REPEAT MARK UPPER HALF (as U+3035 is combining)
622 [0x3034] = true, -- 〴 VERTICAL KANA REPEAT WITH VOICED SOUND MARK UPPER HALF (〃)
623 -- dashes : suppress visible spacing after
624 [0x2013] = 50, -- – (50: dashes)
625 [0x2014] = 50, -- —
626 [0x2015] = 50, -- ―
627 [0x2025] = 50, -- ‥
628 [0x2026] = 50, -- …
629 [0xFE19] = 50, -- ︙ PRESENTATION FORM FOR VERTICAL HORIZONTAL ELLIPSIS
630 [0xFE30] = 50, -- ︰ PRESENTATION FORM FOR VERTICAL TWO DOT LEADER
631 [0xFE31] = 50, -- ︱ PRESENTATION FORM FOR VERTICAL EM DASH
632 [0xFE32] = 50, -- ︲ PRESENTATION FORM FOR VERTICAL EN DASH
633 [0xFE58] = 50, -- ﹘ SMALL EM DASH
634 [0x301C] = 50, -- 〜 WAVE DASH
635 [0x3030] = 50, -- 〰 WAVY DASH
636 [0xFF5E] = 50, -- ~ FULLWIDTH TILDE
637 },{ __index = function (_,c)
638 return is_hangul_jamo(c) -- chosong also is breakable_after
639 or is_noncjk_char(c)
640 or is_hanja(c)
641 or is_combining(c)
642 or is_kana(c)
643 or charclass[c] >= 2
644 end })
645 luatexko.breakableafter = breakable_after
646
647 local breakable_before = setmetatable({
648 [0x28] = true, -- (
649 [0x3C] = true, -- <
650 [0x5B] = true, -- [
651 [0x60] = true, -- `
652 [0x7B] = true, -- {
653 [0xAB] = true, -- «
654 [0x226A] = true, -- ≪
655 [0x25A1] = true, -- □
656 [0x25CB] = true, -- ○
657 [0x3000] = true,
658 [0x3007] = true, -- 〇
659 [0x3013] = true, -- 〓
660 [0xFE59] = true, -- ﹙
661 [0xFE5B] = true, -- ﹛
662 [0xFE5D] = true, -- ﹝
663 [0xFF1C] = true, -- <
664 -- dashes : nobreak but stretching before
665 [0x2013] = 10000, -- –
666 [0x2014] = 10000, -- —
667 [0x2015] = 10000, -- ―
668 [0x2025] = 10000, -- ‥
669 [0x2026] = 10000, -- …
670 [0x3030] = 10000, -- 〰 WAVY DASH
671 [0xFE19] = 10000, -- ︙
672 [0xFE30] = 10000, -- ︰
673 [0xFE31] = 10000, -- ︱
674 [0xFE32] = 10000, -- ︲ PRESENTATION FORM FOR VERTICAL EN DASH
675 [0xFE58] = 10000, -- ﹘ SMALL EM DASH
676 -- small kana
677 [0x3041] = 1000, [0x3043] = 1000, [0x3045] = 1000, [0x3047] = 1000,
678 [0x3049] = 1000, [0x3063] = 1000, [0x3083] = 1000, [0x3085] = 1000,
679 [0x3087] = 1000, [0x308E] = 1000, [0x3095] = 1000, [0x3096] = 1000,
680 [0x30A1] = 1000, [0x30A3] = 1000, [0x30A5] = 1000, [0x30A7] = 1000,
681 [0x30A9] = 1000, [0x30C3] = 1000, [0x30E3] = 1000, [0x30E5] = 1000,
682 [0x30E7] = 1000, [0x30EE] = 1000, [0x30F5] = 1000, [0x30F6] = 1000,
683 [0x31F0] = 1000, [0x31F1] = 1000, [0x31F2] = 1000, [0x31F3] = 1000,
684 [0x31F4] = 1000, [0x31F5] = 1000, [0x31F6] = 1000, [0x31F7] = 1000,
685 [0x31F8] = 1000, [0x31F9] = 1000, [0x31FA] = 1000, [0x31FB] = 1000,
686 [0x31FC] = 1000, [0x31FD] = 1000, [0x31FE] = 1000, [0x31FF] = 1000,
687 [0xFF67] = 1000, [0xFF68] = 1000, [0xFF69] = 1000, [0xFF6A] = 1000,
688 [0xFF6B] = 1000, [0xFF6C] = 1000, [0xFF6D] = 1000, [0xFF6E] = 1000,
689 [0xFF6F] = 1000, [0x1B132] = 1000, [0x1B150] = 1000, [0x1B151] = 1000,
690 [0x1B152] = 1000, [0x1B155] = 1000, [0x1B164] = 1000, [0x1B165] = 1000,
691 [0x1B166] = 1000, [0x1B167] = 1000,
692 -- nonstarter
693 [0xA015] = false, -- YI SYLLABLE WU
694 },{ __index = function(_,c)
695 return is_hangul(c)
696 or is_compat_jamo(c)
697 or is_chosong(c)
698 or is_hanja(c)
699 or is_kana(c)
700 or charclass[c] == 1
701 end
702 })
703 luatexko.breakablebefore = breakable_before
704
705 local function is_cjk_char (c)
706 return is_hangul_jamo(c)
707 or is_hanja(c)
708 or is_kana(c)
709 or charclass[c] >= 1
710 or rawget(breakable_before, c) and c >= 0x2000
711 or rawget(breakable_after, c) and c >= 0x2000
712 end
713
714 local active_processes = {}
715
716 -- font fallback
717
718 local force_hangul = {
719 [0x21] = true, -- !
720 [0x27] = true, -- '
721 [0x28] = true, -- (
722 [0x29] = true, -- )
723 [0x2C] = true, -- ,
724 [0x2E] = true, -- .
725 [0x3A] = true, -- :
726 [0x3B] = true, -- ;
727 [0x3F] = true, -- ?
728 [0x60] = true, -- `
729 [0xB7] = true, -- ·
730 [0x2014] = true, -- —
731 [0x2015] = true, -- ―
732 [0x2018] = true, -- ‘
733 [0x2019] = true, -- ’
734 [0x201C] = true, -- “
735 [0x201D] = true, -- ”
736 [0x2026] = true, -- …
737 [0x203B] = true, -- ※
738 }
739 luatexko.forcehangulchars = force_hangul
740
741 local forcehf_id
742 function luatexko.updateforcehangul (value)
743 if not forcehf_id then
744 forcehf_id = luatexbase.new_whatsit("luatexko_forcehangulfont_whatsit")
745 end
746 local what = nodenew(whatsitid, "user_defined")
747 what.user_id = forcehf_id
748 what.type = 108 -- lua_value : function
749 what.value = value
750 nodewrite(what)
751 end
752
753 local function process_fonts (head)
754 local curr, currfont, currlang, newfont = head, 0, nohyphen, 0
755 local to_free = { }
756 while curr do
757 local id = curr.id
758 if id == glyphid then
759 currfont, currlang = curr.font, curr.lang
760 if curr.font ~= 0 -- exclude nullfont
761 and not has_attribute(curr, unicodeattr) then
762
763 local c = curr.char
764 set_attribute(curr, unicodeattr, c)
765
766 local done
767 if is_combining(c) then
768 local p = getprev(curr)
769 if p and p.id == glyphid then
770 if curr.font ~= p.font then
771 curr.font = p.font
772 end
773 curr.lang = p.lang
774 done = true
775 end
776 if is_jungsong(c) or is_jongsong(c) or hangul_tonemark[c] then
777 done = false
778 if hangul_tonemark[c]
779 and not active_processes.reorderTM
780 and not has_harf_data(curr.font) then
781 luatexko.activate("reorderTM") -- activate reorderTM here
782 end
783 end
784 end
785
786 if not done then
787
788 if curr.subtype == 1 and curr.lang ~= nohyphen and is_cjk_char(c) then
789 curr.lang = langkor -- suppress hyphenation of cjk chars
790 end
791
792 local hf = has_attribute(curr, hangulfontattr) or false
793 local hjf = has_attribute(curr, hanjafontattr) or false
794
795 if hf and force_hangul[c]
796 and fontoptions.is_widefont[curr.font] -- exclude legacy fonts
797 and curr.lang ~= nohyphen and not fontoptions.monospaced[curr.font] -- exclude ttfamily
798 then
799 curr.font = hf
800 elseif hf and has_attribute(curr, hangulbyhangulattr) and is_hangul_jamo(c) then
801 curr.font = hf
802 elseif hjf and has_attribute(curr, hanjabyhanjaattr) and is_hanja(c) then
803 curr.font = hjf
804 elseif not char_in_font(curr.font, c) then
805 local fbf = has_attribute(curr, fallbackfontattr) or false
806 for _,f in ipairs{ hf, hjf, fbf } do
807 if f and char_in_font(f, c) then
808 curr.font = f
809 break
810 end
811 end
812 end
813 end
814 end
815 newfont = curr.font
816 elseif id == glueid
817 and currfont ~= 0
818 and currfont ~= newfont
819 and currlang ~= nohyphen
820 and curr.subtype == 13 -- spaceskip
821 -- fontloader's "node" mode sets space_stretch to zero
822 -- when the font is a monospaced font (fontspec's \setmonofont
823 -- command does the same thing), which we will bypass here
824 -- for alignment of CJK and Latin glyphs in verbatim environment.
825 -- See http://www.ktug.org/xe/index.php?document_srl=249772
826 and not fontoptions.monospaced[currfont] then
827
828 local params = getparameters(currfont)
829 local oldwd, oldst, oldsh, oldsto, oldsho = getglue(curr)
830 if params and oldsto == 0 and oldsho == 0 then
831 local p = getprev(curr)
832 while p and p.id == whatsitid do p = getprev(p) end
833 local sf = p and p.char and tex.getsfcode(p.char) or 1000
834 if sf == 0 or sf > 1000 then
835 local p, pf = getprev(p), 0
836 while p and pf == 0 do
837 pf = p.char and tex.getsfcode(p.char) or 1000
838 p = getprev(p)
839 end
840 if sf == 0 then sf = pf end
841 if pf < 1000 then sf = 1000 end
842 end
843 if oldwd == (sf < 2000 and params.space or params.space+params.extra_space)
844 and oldst == tex.sp(params.space_stretch * (sf/1000))
845 and oldsh == tex.sp(params.space_shrink * (1000/sf)) then
846
847 local newwd = fontoptions.hangulspaceskip[newfont]
848 if newwd then
849 setglue(curr,
850 sf < 2000 and newwd[1] or newwd[1]+newwd[4],
851 tex.sp(newwd[2] * (sf/1000)),
852 tex.sp(newwd[3] * (1000/sf)))
853 end
854 end
855 end
856 elseif id == discid then
857 process_fonts(curr.pre)
858 process_fonts(curr.post)
859 process_fonts(curr.replace)
860 elseif id == mathid then
861 curr = end_of_math(curr)
862 elseif id == whatsitid and
863 curr.user_id == forcehf_id and
864 curr.type == 108 then -- lua_value
865
866 local value = curr.value
867 if type(value) == "function" then
868 value()
869 end
870
871 noderemove(head, curr)
872 to_free[#to_free+1] = curr
873 end
874 curr = getnext(curr)
875 end
876
877 for _, v in ipairs(to_free) do nodefree(v) end
878 end
879
880 -- linebreak
881
882 local is_blocking_node
883 do
884 local blocking_nodes = {
885 [hlistid] = true,
886 [vlistid] = true,
887 [ruleid] = true,
888 [discid] = true,
889 [glueid] = true,
890 [mathid] = true,
891 [dirid] = true,
892 [glyphid] = true,
893 }
894 function is_blocking_node (curr)
895 local id, subtype = curr.id, curr.subtype
896 if id == hlistid then
897 if subtype == 3 then return false end -- indentbox
898 if curr.next and curr.next.id == ins_id then return false end -- footnote
899 end
900 return blocking_nodes[id] or id == kernid and subtype == 1 -- userkern
901 end
902 end
903
904 local function hbox_char_font (box, init, deep)
905 local mynext = init and getnext or getprev
906 local curr = init and box.list or nodeslide(box.list)
907 while curr do
908 local id = curr.id
909 if has_attribute(curr, charhead) then -- various pass-over (especially unboxed) nodes
910 elseif id == glyphid then
911 local c = has_attribute(curr, unicodeattr) or curr.char
912 if c and not is_combining(c) then
913 return c, curr.font
914 end
915 elseif curr.list then
916 return hbox_char_font(curr, init, deep)
917 elseif deep then
918 elseif curr.id == glueid and has_attribute(box, rubyattr) then -- ruby
919 elseif is_blocking_node(curr) then
920 return
921 end
922 curr = mynext(curr)
923 end
924 end
925
926 local iwspaceOffattributeid = luatexbase.attributes.g__tag_interwordspaceOff_attr
927 local insert_glue_before
928 do
929 local function char_orphan_penalty (curr, par)
930 if par then
931 local nn = getnext(curr)
932 local nc = nn.char
933 local ncl = nc and charclass[nc]
934 while nn.id == whatsitid
935 or nn.penalty == 10000
936 or nc and (ncl >= 2 and ncl ~= 5 -- cjk closings
937 or nc < 0xFF and rawget(breakable_after, nc) -- latin closings
938 or is_combining(nc)) do
939 nn = getnext(nn)
940 nc = nn.char
941 ncl = nc and charclass[nc]
942 end
943 if nn.id == glueid and nn.subtype == 15 then -- parfillskip
944 return 1000 -- supress orphan
945 end
946 end
947 end
948 function insert_glue_before (head, curr, par, br, brb, classic, ict, dim, fid)
949 local prev = getprev(curr)
950
951 if prev and prev.penalty then
952 -- repect user's penalty
953 else
954 local pena = not br and 10000
955 or type(brb) == "number" and brb
956 or char_orphan_penalty(curr, par)
957 or fid and fontoptions.intercharpenalty[fid]
958 or 50
959 if not fid or pena ~= 0 then
960 local pn = nodenew(penaltyid)
961 pn.penalty = pena
962 head = insert_before(head, curr, pn)
963 end
964 end
965 if not fid then return head end -- penalty only for non-glyph box + cjk
966
967 dim = dim or 0
968 local gl = nodenew(glueid)
969 local en = fontoptions.en_size[fid]
970 if ict then
971 en = classic and en or en/4
972 setglue(gl, en * ict[1] + dim, nil, en * ict[2])
973 else
974 local str = fontoptions.intercharstretch[fid] or stretch_f*en
975 setglue(gl, dim, str, str*0.6)
976 end
977
978 if iwspaceOffattributeid then
979 gl.attr = prev and prev.attr or curr.attr -- for less tagging
980 set_attribute(gl, iwspaceOffattributeid, 1)
981 end
982
983 set_attribute(gl, inhibitglueattr, 1) -- suppress multiple run of unboxed nodes
984 return insert_before(head, curr, gl)
985 end
986 end
987
988 local process_cjk_punctuation_spacing, process_linebreak
989 do
990 local function maybe_linebreak (head, curr, pc, pcl, cc, old, fid, par)
991 local ccl = get_char_class(cc, old)
992 if pc and cc and curr.lang ~= nohyphen and (is_cjk_char(pc) or is_cjk_char(cc)) then
993 local brap, brbc = breakable_after[pc], breakable_before[cc]
994 if brap == 50 and brbc == 10000 then -- skip dash-dash
995 else
996 local ict = intercharclass[pcl][ccl]
997 local br = brap and brbc or brap == 50 and is_noncjk_char(cc) -- allow dash-latin as well
998 local dim = fontoptions.intercharacter[fid]
999 head = insert_glue_before(head, curr, par, br, brbc, old, ict, dim, fid)
1000 end
1001 end
1002 return head, cc, ccl, fid
1003 end
1004 function process_cjk_punctuation_spacing (head, par)
1005 local pcl, pc, pf, old, pcurr = 0, false, false, false
1006 --[[
1007 -- pcl: 앞 글자 클래스(0..7)
1008 -- pc : 앞 글자 코드
1009 -- pf : 앞 글자 폰트
1010 -- old: 현재 classic 모드임을 표시 (이 함수는 classic 모드에서만 동작한다)
1011 -- pcurr: 글자 첫머리 노드
1012 --]]
1013 local curr = head
1014 while curr do
1015 if has_attribute(curr, charhead) or harf_actual_literal(curr) == 1 then
1016 pcurr = pcurr or curr
1017 else
1018 local id = curr.id
1019 if id == glyphid and curr.lang ~= nohyphen then
1020 local cc = curr.char
1021 old = has_attribute(curr, classicattr)
1022 local ccl = get_char_class(cc, old)
1023 if old and intercharclass[pcl][ccl] then
1024 local cf = charclass[cc] == 0 and pf or curr.font
1025 head = maybe_linebreak(head, pcurr or curr, pc, pcl, cc, old, cf, par)
1026 end
1027 pcl, pc, pf = ccl, cc, curr.font
1028 elseif is_blocking_node(curr) then
1029 if id == glueid and (curr.subtype >= 13 and curr.subtype <= 15 -- spaceskip .. parfillskip
1030 or has_attribute(curr, inhibitglueattr)) then
1031 pcl, pc, pf = 0, false, false
1032 else
1033 if pf and old and intercharclass[pcl][0] then
1034 head = maybe_linebreak(head, pcurr or curr, pc, pcl, 0x4E00, old, pf, par)
1035 end
1036 pcl, pc, pf = 0, 0x4E00, false
1037 end
1038 end
1039 pcurr = false
1040 end
1041 curr = getnext(curr)
1042 end
1043 return head
1044 end
1045 function process_linebreak (head, par)
1046 local curr, pc, pcl, pf, pcurr = head, false, 0, false, false
1047 --[[
1048 -- pc : 앞 글자 코드
1049 -- pcl: 앞 글자 클래스(0..7)
1050 -- pf : 앞 글자 폰트
1051 -- pcurr: 글자 첫머리 노드(unhbox되어 들어오는 노드리스트 처리시 필요)
1052 --]]
1053 while curr do
1054 if has_attribute(curr, charhead) or harf_actual_literal(curr) == 1 then
1055 pcurr = pcurr or curr
1056 else
1057 local id = curr.id
1058 if id == glyphid then
1059 local c = has_attribute(curr, unicodeattr) or curr.char
1060 if c and not is_combining(c) then
1061 local old = has_attribute(curr, classicattr)
1062 local cjk = is_cjk_char(c)
1063 local f = cjk and curr.font or pf or curr.font
1064 if old and cjk and pc == -1 then -- penalty only (f is nil below) for non-glyph box + cjk
1065 head = insert_glue_before(head, pcurr or curr, par, true, false)
1066 pc, pf, pcl = c, f, get_char_class(c, old)
1067 else
1068 head, pc, pcl, pf = maybe_linebreak(head, pcurr or curr, pc, pcl, c, old, f, par)
1069 end
1070 end
1071
1072 elseif (id == hlistid or id == vlistid) and is_blocking_node(curr) then
1073 local old = has_attribute(curr, classicattr)
1074 local c, f = hbox_char_font(curr, true)
1075 if c then
1076 head = maybe_linebreak(head, pcurr or curr, pc, pcl, c, old, pf or f, false) -- par is false
1077 elseif old and pc and is_cjk_char(pc) then -- penalty only
1078 head = insert_glue_before(head, pcurr or curr, false, true, false)
1079 end
1080 c, f = hbox_char_font(curr)
1081 pc, pf, pcl = c or old and -1, pf or f, c and get_char_class(c, old) or 0
1082
1083 elseif id == mathid then
1084 pc, pcl, curr = 0x30, 0, end_of_math(curr)
1085
1086 elseif id == dirid then
1087 if curr.dir:sub(1,1) == "+" then -- push dir
1088 local n = getnext(curr)
1089 if n.id == glyphid then
1090 local old = has_attribute(n, classicattr)
1091 head = maybe_linebreak(head, pcurr or curr, pc, pcl, n.char, old, n.font, par)
1092 end
1093 pc, pcl = false, 0
1094 end
1095
1096 elseif is_blocking_node(curr) then
1097 pc, pcl = false, 0
1098 end
1099 pcurr = false
1100 end
1101 curr = getnext(curr)
1102 end
1103 return head
1104 end
1105 end
1106
1107 -- interhangul & interlatincjk
1108
1109 local process_interhangul
1110 do
1111 local function do_interhangul_option (head, curr, pc, c, fontid, par)
1112 local cc = (is_hangul(c) or is_compat_jamo(c) or is_chosong(c)) and 1 or 0
1113
1114 if cc*pc == 1 and curr.lang ~= nohyphen then
1115 local dim = fontoptions.interhangul[fontid]
1116 if dim then
1117 head = insert_glue_before(head, curr, par, true, true, false, false, dim, fontid)
1118 end
1119 end
1120
1121 return head, cc, fontid
1122 end
1123 function process_interhangul (head, par)
1124 local curr, pc, pf, pcurr = head, 0, false, false
1125 --[[
1126 -- pc: 앞 글자 한글 여부(1 or 0)
1127 -- pf: 앞 글자 폰트
1128 -- pcurr: 글자 첫머리 노드
1129 --]]
1130 while curr do
1131 if has_attribute(curr, charhead) or harf_actual_literal(curr) == 1 then
1132 pcurr = pcurr or curr
1133 else
1134 local id = curr.id
1135 if id == glyphid then
1136 local c = has_attribute(curr, unicodeattr) or curr.char
1137 if c and not is_combining(c) then
1138 head, pc, pf = do_interhangul_option(head, pcurr or curr, pc, c, curr.font, par)
1139 end
1140
1141 elseif id == hlistid and is_blocking_node(curr) then
1142 local c, f = hbox_char_font(curr, true)
1143 if c then
1144 head, pc, pf = do_interhangul_option(head, pcurr or curr, pc, c, pf or f, false)
1145 end
1146 c, f = hbox_char_font(curr)
1147 pc, pf = c and is_hangul_jamo(c) and 1 or 0, pf or f
1148
1149 elseif id == mathid then
1150 pc, curr = 0, end_of_math(curr)
1151 elseif id == dirid then
1152 if curr.dir:sub(1,1) == "+" then
1153 local n = getnext(curr)
1154 if n.id == glyphid then
1155 head = do_interhangul_option(head, pcurr or curr, pc, n.char, n.font, par)
1156 end
1157 pc = 0
1158 end
1159 elseif is_blocking_node(curr) then
1160 pc = 0
1161 end
1162 pcurr = false
1163 end
1164 curr = getnext(curr)
1165 end
1166 return head
1167 end
1168 end
1169
1170 local process_interlatincjk
1171 do
1172 local function do_interlatincjk_option (head, curr, p, pc, pf, c, cf, par)
1173 local cc = is_cjk_char(c) and 1 or is_noncjk_char(c) and 2 or 0
1174 local f = cc == 1 and cf or pf
1175
1176 if p and cc*pc == 2 and curr.lang ~= nohyphen then
1177 local brb = cc == 2 or breakable_before[c] -- numletter != br_before
1178 if brb and brb ~= 10000 and breakable_after[p] == true then -- skip latin-dash and dash-latin
1179 local dimc = fontoptions.interlatincjk[cf] or 0
1180 local dimp = fontoptions.interlatincjk[pf] or 0
1181 local dim = dimc > dimp and dimc or dimp
1182 if dim ~= 0 then
1183 head = insert_glue_before(head, curr, par, true, brb, false, false, dim, f)
1184 end
1185 end
1186 end
1187
1188 return head, cc, f, c
1189 end
1190 function process_interlatincjk (head, par)
1191 local curr, pc, pf, p, pcurr = head, 0, false, false, false
1192 --[[
1193 -- pc: 앞 글자 cjk 여부(1 = cjk, 2 = non-cjk, 0 = other)
1194 -- pf: 앞 글자 폰트
1195 -- p : 앞 글자 코드
1196 -- pcurr: 글자 첫머리 노드
1197 --]]
1198 while curr do
1199 if curr.id == glyphid and is_cjk_char(curr.char) then
1200 pf = curr.font
1201 break
1202 end
1203 curr = getnext(curr)
1204 end
1205
1206 curr = head
1207 while curr do
1208 if has_attribute(curr, charhead) or harf_actual_literal(curr) == 1 then
1209 pcurr = pcurr or curr
1210 else
1211 local id = curr.id
1212 if id == glyphid then
1213 local c = has_attribute(curr, unicodeattr) or curr.char
1214 if c and not is_combining(c) then
1215 head, pc, pf, p = do_interlatincjk_option(head, pcurr or curr, p, pc, pf, c, curr.font, par)
1216 end
1217
1218 elseif id == hlistid and is_blocking_node(curr) then
1219 local c, f = hbox_char_font(curr, true)
1220 if c then
1221 head = do_interlatincjk_option(head, pcurr or curr, p, pc, pf, c, pf or f, false)
1222 end
1223 c, f = hbox_char_font(curr)
1224 pc = c and (is_cjk_char(c) and 1 or is_noncjk_char(c) and 2) or 0
1225 pf, p = pf or f, c
1226
1227 elseif id == mathid then
1228 if pc == 1 then
1229 head = do_interlatincjk_option(head, pcurr or curr, p, pc, pf, 0x30, pf, par)
1230 end
1231 curr, pc, p = end_of_math(curr), 2, 0x30
1232
1233 elseif id == dirid then
1234 if curr.dir:sub(1,1) == "+" then
1235 local n = getnext(curr)
1236 if n.id == glyphid then
1237 head = do_interlatincjk_option(head, pcurr or curr, p, pc, pf, n.char, n.font, par)
1238 end
1239 pc, p = 0, false
1240 end
1241
1242 elseif is_blocking_node(curr) then
1243 pc, p = 0, false
1244 end
1245 pcurr = false
1246 end
1247
1248 curr = getnext(curr)
1249 end
1250 return head
1251 end
1252 end
1253
1254 -- compress punctuations
1255
1256 local function process_glyph_width (head)
1257 local curr = head
1258 while curr do
1259 local id = curr.id
1260 if id == glyphid then
1261 if curr.lang ~= nohyphen
1262 and not (verticalattr and has_attribute(curr, verticalattr)) -- avoid multiple run
1263 and fontoptions.compresspunctuations[curr.font] then
1264
1265 local cc = has_attribute(curr, unicodeattr) or curr.char
1266 local old = has_attribute(curr, classicattr)
1267 local class = get_char_class(cc, old)
1268 if class >= 1 and class <= 4 and
1269 (old or cc < 0x2000 or cc > 0x202F) then -- exclude general puncts
1270
1271 local gpos = class == 1 and getprev(curr) or getnext(curr)
1272 gpos = gpos and gpos.id == kernid and gpos.subtype == 0 -- fontkern
1273
1274 if not gpos then -- avoid multiple run
1275 local diff = char_in_font(curr.font, curr.char).luatexko_diff or 0
1276 local wd = fontoptions.en_size[curr.font] - (curr.width + diff)
1277 if wd ~= 0 then
1278 local k = nodenew(kernid) -- fontkern (subtype 0) is default
1279 k.kern = class == 3 and wd/2 or wd
1280 if class == 1 then
1281 set_attribute(k, charhead, 1)
1282 head = insert_before(head, curr, k)
1283 elseif class == 2 or class == 4 then
1284 head, curr = insert_after(head, curr, k)
1285 else
1286 local k2 = nodecopy(k)
1287 set_attribute(k, charhead, 1)
1288 head = insert_before(head, curr, k)
1289 head, curr = insert_after(head, curr, k2)
1290 end
1291 end
1292 end
1293 end
1294 end
1295 elseif id == mathid then
1296 curr = end_of_math(curr)
1297 end
1298 curr = getnext(curr)
1299 end
1300 return head
1301 end
1302
1303 -- remove classic spaces
1304
1305 local function process_remove_spaces (head)
1306 local curr, to_free, sp, cjk = head, {}, false, false
1307 while curr do
1308 local id = curr.id
1309 if id == glyphid and curr.lang ~= nohyphen and has_attribute(curr, classicattr)
1310 and fontoptions.removeclassicspaces[curr.font] then
1311 local c = has_attribute(curr, unicodeattr) or curr.char or 0
1312 if not is_combining(c) then
1313 local cc = is_cjk_char(c) and 1 or 0
1314 if sp and cjk and (cjk + cc) == 2 then
1315 head = noderemove(head, sp)
1316 to_free[#to_free+1] = sp
1317 end
1318 cjk, sp = cc, false
1319 end
1320 elseif cjk and id == glueid
1321 and curr.subtype == 13 -- spaceskip
1322 and has_attribute(curr, classicattr) then
1323 sp = curr
1324 elseif id == mathid then
1325 curr, cjk, sp = end_of_math(curr), false, false
1326 elseif id == hlistid or is_blocking_node(curr) then
1327 cjk, sp = false, false
1328 end
1329 curr = getnext(curr)
1330 end
1331 for _,v in ipairs(to_free) do nodefree(v) end
1332 return head
1333 end
1334
1335 -- josa
1336
1337 local process_josa
1338 do
1339 local josa_table = {
1340 -- 리을, 중성, 종성
1341 [0xAC00] = {0xC774, 0xAC00, 0xC774}, -- 가 = 이, 가, 이
1342 [0xC740] = {0xC740, 0xB294, 0xC740}, -- 은 = 은, 는, 은
1343 [0xC744] = {0xC744, 0xB97C, 0xC744}, -- 을 = 을, 를, 을
1344 [0xC640] = {0xACFC, 0xC640, 0xACFC}, -- 와 = 과, 와, 과
1345 [0xC73C] = {nil, nil, 0xC73C}, -- 으(로) = , , 으
1346 [0xC774] = {0xC774, nil, 0xC774}, -- 이(라) = 이, , 이
1347 }
1348
1349 local hanja2hangul = { }
1350 local function add_to_hanja2hangul (filename, i, last)
1351 local f = kpse.find_file(filename)
1352 if f then
1353 for c in io.lines(f) do
1354 hanja2hangul[i] = tonumber(c)
1355 i = i + 1
1356 end
1357 else
1358 warning("cannot find %s", filename)
1359 for c = i, last do
1360 hanja2hangul[c] = c
1361 end
1362 end
1363 end
1364
1365 local josa_code = setmetatable({
1366 [0x30] = 3, [0x31] = 1, [0x33] = 3, [0x36] = 3,
1367 [0x37] = 1, [0x38] = 1, [0x4C] = 1, [0x4D] = 3,
1368 [0x4E] = 3, [0x6C] = 1, [0x6D] = 3, [0x6E] = 3,
1369 [0x2160] = 1, [0x2162] = 3, [0x2165] = 3, [0x2166] = 1,
1370 [0x2167] = 1, [0x2169] = 3, [0x216A] = 1, [0x216C] = 3,
1371 [0x216D] = 3, [0x216E] = 3, [0x216F] = 3, [0x2170] = 1,
1372 [0x2172] = 3, [0x2175] = 3, [0x2176] = 1, [0x2177] = 1,
1373 [0x2179] = 3, [0x217A] = 1, [0x217C] = 3, [0x217D] = 3,
1374 [0x217E] = 3, [0x217F] = 3, [0x2460] = 1, [0x2462] = 3,
1375 [0x2465] = 3, [0x2466] = 1, [0x2467] = 1, [0x2469] = 3,
1376 [0x246A] = 1, [0x246C] = 3, [0x246F] = 3, [0x2470] = 1,
1377 [0x2471] = 1, [0x2473] = 3, [0x2474] = 1, [0x2476] = 3,
1378 [0x2479] = 3, [0x247A] = 1, [0x247B] = 1, [0x247D] = 3,
1379 [0x247E] = 1, [0x2480] = 3, [0x2483] = 3, [0x2484] = 1,
1380 [0x2485] = 1, [0x2487] = 3, [0x2488] = 1, [0x248A] = 3,
1381 [0x248D] = 3, [0x248E] = 1, [0x248F] = 1, [0x2491] = 3,
1382 [0x2492] = 1, [0x2494] = 3, [0x2497] = 3, [0x2498] = 1,
1383 [0x2499] = 1, [0x249B] = 3, [0x24A7] = 1, [0x24A8] = 3,
1384 [0x24A9] = 3, [0x24C1] = 1, [0x24C2] = 3, [0x24C3] = 3,
1385 [0x24DB] = 1, [0x24DC] = 3, [0x24DD] = 3, [0x3139] = 1,
1386 [0x3203] = 1, [0x3263] = 1, [0xFF10] = 3, [0xFF11] = 1,
1387 [0xFF13] = 3, [0xFF16] = 3, [0xFF17] = 1, [0xFF18] = 1,
1388 [0xFF2C] = 1, [0xFF2D] = 3, [0xFF2E] = 3, [0xFF4C] = 1,
1389 [0xFF4D] = 3, [0xFF4E] = 3,
1390 },{ __index = function(t, cc)
1391 local c = cc
1392 -- xetexko에 포함된 .tab 파일들을 이용해 한자를 한글로 변환
1393 if c >= 0x4E00 and c <= 0x9FA5 then
1394 if not hanja2hangul[c] then
1395 add_to_hanja2hangul("hanja_hangul.tab", 0x4E00, 0x9FA5)
1396 end
1397 c = hanja2hangul[c]
1398 elseif c >= 0xF900 and c <= 0xFA2D then
1399 if not hanja2hangul[c] then
1400 add_to_hanja2hangul("hanjacom_hangul.tab", 0xF900, 0xFA2D)
1401 end
1402 c = hanja2hangul[c]
1403 elseif c >= 0x3400 and c <= 0x4DB5 then
1404 if not hanja2hangul[c] then
1405 add_to_hanja2hangul("hanjaexa_hangul.tab", 0x3400, 0x4DB5)
1406 end
1407 c = hanja2hangul[c]
1408 end
1409 if is_hangul(c) then
1410 c = (c - 0xAC00) % 28 + 0x11A7
1411 end
1412 if is_chosong(c) then
1413 c = c == 0x1105 and 1 or 3
1414 t[cc] = c; return c
1415 elseif is_jungsong(c) then
1416 c = c ~= 0x1160 and 2
1417 t[cc] = c; return c
1418 elseif is_jongsong(c) then
1419 c = c == 0x11AF and 1 or 3
1420 t[cc] = c; return c
1421 elseif is_noncjk_char(c) and c <= 0x7A
1422 or c >= 0x2160 and c <= 0x217F -- roman
1423 or c >= 0x2460 and c <= 0x24E9 -- ①
1424 or c >= 0x314F and c <= 0x3163 or c >= 0x3187 and c <= 0x318E -- ㅏ
1425 or c >= 0x320E and c <= 0x321E -- ㈎
1426 or c >= 0x326E and c <= 0x327F -- ㉮
1427 or c >= 0xFF10 and c <= 0xFF19 -- 0
1428 or c >= 0xFF21 and c <= 0xFF3A -- A
1429 or c >= 0xFF41 and c <= 0xFF5A -- a
1430 then
1431 t[cc] = 2; return 2
1432 elseif c >= 0x3131 and c <= 0x314E or c >= 0x3165 and c <= 0x3186 -- ㄱ
1433 or c >= 0x3200 and c <= 0x320D -- ㈀
1434 or c >= 0x3260 and c <= 0x326D -- ㉠
1435 then
1436 t[cc] = 3; return 3
1437 end
1438 end })
1439
1440 local function prevjosacode (n, parenlevel, ignore_parens)
1441 local josacode
1442 while n do
1443 local id = n.id
1444 if id == glyphid then
1445 local c = has_attribute(n, unicodeattr) or n.char -- beware hlist/vlist
1446 if ignore_parens and c == 0x29 then -- )
1447 parenlevel = parenlevel + 1
1448 elseif ignore_parens and c == 0x28 then -- (
1449 parenlevel = parenlevel - 1
1450 elseif parenlevel <= 0 then
1451 josacode = josa_code[c]
1452 if josacode then break end
1453 end
1454 elseif id == hlistid or id == vlistid then
1455 local list = n.list
1456 if list then
1457 josacode, parenlevel = prevjosacode(nodeslide(list), parenlevel, ignore_parens)
1458 if josacode then break end
1459 end
1460 end
1461 n = getprev(n)
1462 end
1463 return josacode, parenlevel
1464 end
1465
1466 function process_josa (head)
1467 local curr, tofree = head, {}
1468 while curr do
1469 local id = curr.id
1470 if id == glyphid then
1471 local autojosaattr = has_attribute(curr, autojosaattr)
1472 if autojosaattr then
1473 local cc = curr.char
1474 if cc == 0xC774 then
1475 local n = getnext(curr)
1476 if n and n.char and is_hangul(n.char) then
1477 else
1478 cc = 0xAC00
1479 end
1480 end
1481 local t = josa_table[cc]
1482 if t then
1483 cc = t[prevjosacode(getprev(curr), 0, autojosaattr > 0) or 3]
1484 if cc then
1485 curr.char = cc
1486 else
1487 head = noderemove(head, curr)
1488 tofree[#tofree+1] = curr
1489 end
1490 end
1491 unset_attribute(curr, autojosaattr)
1492 end
1493 elseif id == mathid then
1494 curr = end_of_math(curr)
1495 end
1496 curr = getnext(curr)
1497 end
1498 for _,v in ipairs(tofree) do nodefree(v) end
1499 return head
1500 end
1501 end
1502
1503 -- dotemph
1504
1505 local shift_put_top
1506 do
1507 local function get_font_yshift (f, dotem)
1508 local off = 0
1509 if f then
1510 off = off + (fontoptions.charraise[f] or 0)
1511 if fontoptions.is_vertical[f] then
1512 off = off + (fontoptions.vertcharraise[f]or 0)
1513 - (fontoptions.en_size[f] or 0)*(dotem and 0.8 or 0.5) -- lower a little
1514 end
1515 end
1516 return off
1517 end
1518 function shift_put_top (bot, top, dotem)
1519 local shift = top.shift or 0
1520
1521 if bot.id == hlistid then
1522 bot = has_glyph(bot.list) or {}
1523 end
1524 local bot_off = get_font_yshift(bot.font, dotem)
1525
1526 if bot_off ~= 0 then
1527 if top.id == hlistid then
1528 top = has_glyph(top.list) or {}
1529 end
1530 local top_off = get_font_yshift(top.font, dotem)
1531
1532 return shift + top_off - bot_off -- minus is raise, plus is lower
1533 end
1534
1535 return shift
1536 end
1537 end
1538
1539 local dotemphbox = {}
1540 luatexko.dotemphbox = dotemphbox
1541
1542 local dotemph_id
1543 function luatexko.dotemphboundary (i)
1544 if not dotemph_id then
1545 dotemph_id = luatexbase.new_whatsit("luatexko_dotemph_whatsit")
1546 end
1547 local what = nodenew(whatsitid, "user_defined")
1548 what.user_id = dotemph_id
1549 what.type = 100 -- lua_number
1550 what.value = i
1551 nodewrite(what)
1552 end
1553
1554 local function process_dotemph (head)
1555 local curr, pcurr = head, false
1556 local to_free = { }
1557 while curr do
1558 if has_attribute(curr, charhead) or harf_actual_literal(curr) == 1 then
1559 pcurr = pcurr or curr
1560 else
1561 if curr.list then
1562 curr.list = process_dotemph(curr.list)
1563
1564 elseif curr.id == glyphid then
1565 local dotattr = has_attribute(curr, dotemphattr)
1566 if dotattr and dotemphbox[dotattr] then
1567 unset_attribute(curr, dotemphattr) -- avoid multiple run
1568
1569 local c = has_attribute(curr, unicodeattr) or curr.char
1570 if is_hangul(c) or is_compat_jamo(c) or is_chosong(c) or is_hanja(c) or is_kana(c) then
1571
1572 local box = node.copy(dotemphbox[dotattr])
1573
1574 -- consider charraise
1575 box.shift = shift_put_top(curr, box, true)
1576
1577 local basewd = curr.width
1578 if hangul_tonemark[curr.char] then -- horizontal hangul tonemark
1579 basewd = 2 * basewd
1580 end
1581 -- put the dot before base syllable
1582 local n = getnext(curr)
1583 while n do
1584 if n.id == glyphid then
1585 if not is_combining(has_attribute(n, unicodeattr) or n.char) then break end
1586 basewd = basewd + n.width
1587 elseif n.id == kernid and n.subtype == 0 then -- fontkern
1588 basewd = basewd + n.kern
1589 elseif has_attribute(n, charhead) or (n.id == whatsitid and
1590 (n.subtype == restore_whatsit or harf_actual_literal(n) == 2)) then -- pass
1591 else
1592 break
1593 end
1594 n = getnext(n)
1595 end
1596
1597 local shift = (basewd - box.width)/2
1598 if shift ~= 0 then
1599 local list = box.list
1600 local k = nodenew(kernid)
1601 k.kern, k.subtype = shift, 1 -- userkern
1602 box.list = insert_before(list, list, k)
1603 end
1604
1605 box.width = 0
1606 set_attribute(box, charhead, 1)
1607 head = insert_before(head, pcurr or curr, box)
1608 end
1609 end
1610
1611 elseif curr.id == whatsitid and
1612 curr.user_id == dotemph_id and
1613 curr.type == 100 then -- lua_number
1614
1615 local val = curr.value
1616 nodefree(dotemphbox[val])
1617 dotemphbox[val] = nil
1618
1619 to_free[#to_free+1] = curr
1620 head = noderemove(head, curr)
1621 end
1622 pcurr = false
1623 end
1624 curr = getnext(curr)
1625 end
1626
1627 for _, v in ipairs(to_free) do nodefree(v) end
1628 return head
1629 end
1630
1631 -- uline
1632
1633 function luatexko.get_strike_out_down (box)
1634 local c, f = hbox_char_font(box, true, true) -- ignore blocking nodes
1635 if c and f then
1636 local down
1637 local ex = get_font_param(f, "x_height") or tex.sp"1ex"
1638 if is_cjk_char(c) then
1639 local ascender, descender = table.unpack(fontoptions.asc_desc[f])
1640 if ascender and descender then
1641 down = descender - (ascender + descender)/2
1642 else
1643 down = -0.667*ex
1644 end
1645 else
1646 down = -0.5*ex
1647 end
1648
1649 local raise = fontoptions.charraise[f] or 0
1650 return down - raise
1651 end
1652 return -tex.sp"0.5ex"
1653 end
1654
1655 local uline_id
1656 function luatexko.ulboundary (i, n, subtype)
1657 if not uline_id then
1658 uline_id = luatexbase.new_whatsit("luatexko_uline_whatsit")
1659 end
1660 local what = nodenew(whatsitid, "user_defined")
1661 what.user_id = uline_id
1662 if n then
1663 while n.id ~= ruleid and n.id ~= hlistid and n.id ~= vlistid do
1664 n = getnext(n)
1665 end
1666 what.type = 108 -- lua_value : table
1667 what.value = { i, nodecopy(n), subtype }
1668 else
1669 what.type = 100 -- lua_number
1670 what.value = i
1671 end
1672 nodewrite(what)
1673 end
1674
1675 local process_uline
1676 do
1677 local white_nodes = {
1678 [glueid] = true,
1679 [penaltyid] = true,
1680 [kernid] = true,
1681 [whatsitid] = true,
1682 }
1683
1684 local function skip_white_nodes (n, ltr)
1685 local nextnode = ltr and getnext or getprev
1686 while n do
1687 if has_attribute(n, charhead) or harf_actual_literal(n)
1688 or n.id == kernid and n.subtype == 0 -- fontkern
1689 or not white_nodes[n.id] then
1690 break
1691 end
1692 n = nextnode(n)
1693 end
1694 return n
1695 end
1696
1697 local function draw_uline (head, curr, parent, t, final)
1698 local start, list, subtype = t.start or head, t.list, t.subtype
1699 start = skip_white_nodes(start, true)
1700 if final and start then
1701 nodeslide(start) -- to get correct getprev.
1702 end
1703 curr = skip_white_nodes(curr)
1704 if start and curr then
1705 curr = getnext(curr) or curr
1706
1707 local len = parent and rangedimensions(parent, start, curr)
1708 or dimensions(start, curr)
1709 if len and len ~= 0 then
1710 local g = nodenew(glueid)
1711 setglue(g, len)
1712 g.subtype = subtype
1713 g.leader = final and list or nodecopy(list)
1714 g.attr = list.attr
1715 set_attribute(g, charhead, 1)
1716 local k = nodenew(kernid)
1717 k.kern = -len
1718 k.subtype = 1 -- userkern
1719 set_attribute(k, charhead, 2)
1720 head = insert_before(head, start, g)
1721 head = insert_before(head, start, k)
1722 end
1723 end
1724 return head
1725 end
1726
1727 local ulitems = {}
1728
1729 function process_uline (head, parent, level)
1730 local curr, level = head, level or 0
1731 local to_free = { }
1732 while curr do
1733 if curr.list then
1734 curr.list = process_uline(curr.list, curr, level+1)
1735
1736 elseif curr.id == whatsitid and curr.user_id == uline_id then
1737
1738 local value = curr.value
1739 if curr.type == 108 then -- lua_value
1740 local count, list, subtype = table.unpack(value)
1741 ulitems[count] = {
1742 list = list,
1743 subtype = subtype,
1744 level = level,
1745 start = getnext(curr) or curr,
1746 }
1747 else
1748 local item = ulitems[value]
1749 if item then
1750 head = draw_uline(head, curr, parent, item, true)
1751 ulitems[value] = nil
1752 end
1753 end
1754
1755 to_free[#to_free+1] = curr
1756 head = noderemove(head, curr)
1757
1758 end
1759 curr = getnext(curr)
1760 end
1761
1762 for _, item in pairs(ulitems) do
1763 if item.level == level then
1764 head = draw_uline(head, nodeslide(head), parent, item)
1765 item.start = nil
1766 end
1767 end
1768
1769 for _, v in ipairs(to_free) do nodefree(v) end
1770 return head
1771 end
1772 end
1773
1774 -- ruby
1775
1776 local rubybox = {}
1777 luatexko.rubybox = rubybox
1778
1779 function luatexko.getrubystretchfactor (box)
1780 local _, fid = hbox_char_font(box, true, true)
1781 local str = fontoptions.intercharstretch[fid]
1782 if str then
1783 local em = fontoptions.en_size[fid] * 2
1784 token.set_macro("luatexkostretchfactor", ("%.4f"):format(str/em/2))
1785 end
1786 end
1787
1788 local function process_ruby_pre_linebreak (head)
1789 local curr = head
1790 while curr do
1791 local id = curr.id
1792 if id == hlistid then
1793 local rubyid = has_attribute(curr, rubyattr)
1794 if rubyid then
1795 local k2, r2
1796 local ruby_t = rubybox[rubyid]
1797 if ruby_t and ruby_t[3] then -- rubyoverlap
1798 local side = (ruby_t[1].width - curr.width)/2
1799 if side > 0 then -- ruby is wide
1800 local k, r = nodenew(kernid), nodenew(ruleid)
1801 k.subtype, k.kern = 1, -side -- userkern
1802 r.width, r.height, r.depth = side, 0, 0
1803 k2, r2 = nodecopy(k), nodecopy(r)
1804
1805 local prev = curr.prev -- 문단 첫머리에 루비 돌출 방지
1806 if prev and
1807 ( prev.id == localparid or
1808 prev.id == hlistid and prev.subtype == 3 and prev.width == 0 ) then -- indentbox
1809 k.kern = 0
1810 end -- TODO: \rubyoverlap \setbox0\hbox{\ruby{short}{loooooong}} \noindent\copy0\copy0
1811
1812 set_attribute(k, charhead, 1)
1813 set_attribute(r, charhead, 2)
1814 head = insert_before(head, curr, k)
1815 head = insert_before(head, curr, r)
1816 end
1817 ruby_t[3] = false
1818 end
1819
1820 -- integrated from process_ruby_post_linebreak()
1821 local ruby = ruby_t and ruby_t[1]
1822 if ruby then
1823 local side = (curr.width - ruby.width)/2
1824
1825 if not k2 then -- ruby is not wider than base
1826 local basefirst, rubyfirst = has_glyph(curr.list), has_glyph(ruby.list)
1827 if basefirst and hangul_tonemark[basefirst.char] -- horizontal hangul tonemark
1828 and not (rubyfirst and hangul_tonemark[rubyfirst.char]) then -- no horiz. hangul tm.
1829 side = side + (basefirst.width or 0)/2
1830 end
1831 end
1832
1833 if side ~= 0 then
1834 local list = ruby.list
1835 local k = nodenew(kernid)
1836 k.kern, k.subtype = side, 1 -- userkern
1837 ruby.list = insert_before(list, list, k)
1838 end
1839 ruby.width = 0
1840
1841 -- consider charraise
1842 local shift = shift_put_top(curr, ruby, false)
1843
1844 local f, ascender, descender
1845 _, f = hbox_char_font(curr, true, true)
1846 ascender = fontoptions.asc_desc[f][1] or curr.height
1847 _, f = hbox_char_font(ruby, true, true)
1848 descender = fontoptions.asc_desc[f][2] or ruby.depth
1849
1850 ruby.shift = shift - ascender - descender - ruby_t[2] -- rubysep
1851 set_attribute(ruby, charhead, 1)
1852 head = insert_before(head, curr, ruby)
1853 end
1854
1855 if k2 then
1856 head, curr = insert_after(head, curr, r2)
1857 local n = getnext(curr)
1858 while n and n.id == penaltyid do -- skip penalty node for justification
1859 n, curr = curr, getnext(n)
1860 end
1861 head, curr = insert_after(head, curr, k2)
1862 end
1863
1864 rubybox[rubyid] = nil
1865 end
1866 elseif id == mathid then
1867 curr = end_of_math(curr)
1868 end
1869 curr = getnext(curr)
1870 end
1871 return head
1872 end
1873
1874 -- reorder tone marks
1875
1876 local process_reorder_tonemarks
1877 do
1878 local function conv_tounicode (uni)
1879 if uni < 0x10000 then
1880 return ("%04X"):format(uni)
1881 else -- surrogate
1882 uni = uni - 0x10000
1883 local high = uni // 0x400 + 0xD800
1884 local low = uni % 0x400 + 0xDC00
1885 return ("%04X%04X"):format(high, low)
1886 end
1887 end
1888 local function my_node_props (n)
1889 local t = getproperty(n)
1890 if not t then
1891 t = {}
1892 setproperty(n, t)
1893 end
1894 t.luatexko = t.luatexko or {}
1895 return t.luatexko
1896 end
1897 local function pdfliteral_direct_actual (syllable)
1898 local data
1899 if syllable then
1900 local t = {}
1901 for _,v in ipairs(syllable) do
1902 t[#t + 1] = conv_tounicode(v)
1903 end
1904 data = ("/Span<</ActualText<FEFF%s>>>BDC"):format(table.concat(t))
1905 else
1906 data = "EMC"
1907 end
1908 local what = nodenew(whatsitid, literal_whatsit)
1909 what.mode = 2 -- directmode
1910 what.data = data
1911 if syllable then
1912 my_node_props(what).startactualtext = syllable
1913 else
1914 my_node_props(what).endactualtext = true
1915 end
1916 return what
1917 end
1918 local function goto_end_actualtext (curr)
1919 local n = getnext(curr)
1920 while n do
1921 if n.id == whatsitid and
1922 n.mode == 2 and -- directmode
1923 my_node_props(n).endactualtext then
1924 curr = n; break
1925 end
1926 n = getnext(n)
1927 end
1928 return curr
1929 end
1930 function process_reorder_tonemarks (head)
1931 local curr, init = head
1932 while curr do
1933 local id = curr.id
1934 if id == glyphid and
1935 not has_harf_data(curr.font) and
1936 fontoptions.is_hangulscript[curr.font] then
1937
1938 local uni = has_attribute(curr, unicodeattr) or curr.char
1939 if is_hangul(uni) or is_chosong(uni) or uni == 0x25CC then
1940 init = curr
1941 elseif is_jungsong(uni) or is_jongsong(uni) then
1942 elseif hangul_tonemark[uni] then
1943 if init then
1944 local n, syllable = init, { init = init }
1945 while n do
1946 if n.id == glyphid then
1947 local u = has_attribute(n, unicodeattr) or n.char
1948 if u then syllable[#syllable+1] = u end
1949 end
1950 if n == curr then break end
1951 n = getnext(n)
1952 end
1953
1954 if #syllable > 1 and curr.width > 0 then
1955 local TM = curr
1956
1957 local actual = pdfliteral_direct_actual(syllable)
1958 local endactual = pdfliteral_direct_actual()
1959 actual.attr, endactual.attr = TM.attr, TM.attr -- for tagged pdf
1960 set_attribute(actual, charhead, 1)
1961 head = insert_before(head, init, actual)
1962 head, curr = insert_after(head, curr, endactual)
1963
1964 head = noderemove(head, TM)
1965 head, TM = insert_before(head, init, TM)
1966
1967 local n, i = TM, 1
1968 while n.char do
1969 set_attribute(n, unicodeattr, syllable[i]) -- reorder unicode attr
1970 n, i = getnext(n), i+1
1971 end
1972 end
1973
1974 init = nil
1975 elseif char_in_font(curr.font, 0x25CC) then -- isolated tone mark
1976 local dotcircle = nodecopy(curr)
1977 dotcircle.char = 0x25CC
1978 if curr.width > 0 then
1979 local actual = pdfliteral_direct_actual{ init = curr, uni }
1980 local endactual = pdfliteral_direct_actual()
1981 actual.attr, endactual.attr = dotcircle.attr, dotcircle.attr -- for tagged pdf
1982 set_attribute(actual, charhead, 1)
1983 set_attribute(curr, unicodeattr, 0x25CC)
1984 set_attribute(dotcircle, unicodeattr, uni)
1985 head = insert_before(head, curr, actual)
1986 head, curr = insert_after(head, curr, dotcircle)
1987 head, curr = insert_after(head, curr, endactual)
1988 else
1989 head = insert_before(head, curr, dotcircle)
1990 end
1991 end
1992
1993 else
1994 init = nil
1995 end
1996 elseif id == kernid and curr.subtype ~= 1 then -- skip non-userkern
1997 elseif id == whatsitid then
1998 if curr.mode == 2 -- directmode
1999 and my_node_props(curr).startactualtext then
2000 curr, init = goto_end_actualtext(curr), nil
2001 end
2002 else
2003 init = nil
2004 if id == mathid then curr = end_of_math(curr) end
2005 end
2006 curr = getnext(curr)
2007 end
2008 return head
2009 end
2010 end
2011
2012 -- vertical font
2013
2014 local function activate_process (cbnam, cbfun, name, first)
2015 if not active_processes[name] then
2016 local fmt = "luatexko.%s.%s"
2017 local desc = fmt:format(cbnam, name)
2018 if first then
2019 for k, v in pairs(active_processes) do
2020 if v == cbnam then
2021 luatexbase.declare_callback_rule(v, desc, "before", fmt:format(v, k))
2022 end
2023 end
2024 end
2025 luatexbase.add_to_callback(cbnam, cbfun, desc)
2026 active_processes[name] = cbnam
2027 end
2028 end
2029
2030 local get_hb_char_bbox
2031 do
2032 local cachedir, get_cache_data, store_cache_data
2033 if harfbuzz then
2034 local texmfvar = kpse.var_value"TEXMFVAR"
2035 if texmfvar and texmfvar ~= "" then
2036 for _,v in ipairs(texmfvar:explode(os.type == "unix" and ":" or ";")) do
2037 local dir = ("%s/%s"):format(v,"luatexko_cache")
2038 if lfs.attributes(dir,"mode") ~= "directory" then lfs.mkdirp(dir) end
2039 if file.is_writable(dir) then cachedir = dir; break end
2040 end
2041 end
2042 if cachedir then
2043 local function get_cache_name (fontdata, suffix)
2044 local version = fontdata.hb.shared.face:get_name(harfbuzz.ot.NAME_ID_VERSION_STRING)
2045 or assert(lfs.attributes(fontdata.specification.filename, "modification"))
2046 local name = ("%s_%s_%s"):format(fontdata.fullname, version, suffix)
2047 local hexa = ('%02x'):rep(256/8):format(sha2.digest256(name):byte(1, -1))
2048 return ("%s/%s.lua"):format(cachedir, hexa)
2049 end
2050 function get_cache_data (fontdata, suffix)
2051 local name = get_cache_name(fontdata, suffix)
2052 if lfs.attributes(name, "mode") == "file" then
2053 return require(name)
2054 end
2055 end
2056 function store_cache_data (fontdata, suffix, data)
2057 local name = get_cache_name(fontdata, suffix)
2058 table.tofile(name, data, "return")
2059 end
2060 end
2061 end
2062 local function get_char_bbox (hbfont, gid)
2063 local t = hbfont:get_glyph_extents(gid) -- quite slow for CFF
2064 if t then
2065 return { t.x_bearing, t.y_bearing + t.height, t.x_bearing + t.width, t.y_bearing }
2066 end
2067 end
2068 function get_hb_char_bbox (fontdata, index)
2069 local hbfont = fontdata.hb.shared.font
2070 local key = tostring(hbfont)
2071 local bboxes = fontoptions.hb_char_bbox[key]
2072 local bbox = bboxes and bboxes[index]
2073 if bbox then return bbox end
2074 if cachedir then
2075 local data = get_cache_data(fontdata, "bbox")
2076 if not data then
2077 data = { }
2078 for i = 0, 65534 do
2079 local t = get_char_bbox(hbfont, i)
2080 if not t then break end
2081 data[i] = t
2082 end
2083 store_cache_data(fontdata, "bbox", data)
2084 end
2085 fontoptions.hb_char_bbox[key], bbox = data, data[index]
2086 if bbox then return bbox end
2087 end
2088 if not bboxes then
2089 fontoptions.hb_char_bbox[key] = { }
2090 bboxes = fontoptions.hb_char_bbox[key]
2091 end
2092 bbox = get_char_bbox(hbfont, index) or {0,0,0,0}
2093 bboxes[index] = bbox
2094 return bbox
2095 end
2096 end
2097
2098 local get_HB_variant_char
2099 if harfbuzz then
2100 local dir_ltr = harfbuzz.Direction.new"ltr"
2101 local dir_ttb = harfbuzz.Direction.new"ttb"
2102 function get_HB_variant_char (fontdata, charcode, vertical)
2103 local hbfont = fontdata.hb.shared.font
2104 local spec = fontdata.specification
2105 local shaper = spec.features.raw.shaper
2106 local buff = harfbuzz.Buffer.new()
2107 buff:set_direction(vertical and dir_ttb or dir_ltr)
2108 buff:set_script(spec.script)
2109 buff:set_language(spec.language)
2110 buff:add_codepoints{charcode}
2111 hbfont:set_scale(fontdata.hb.hscale, fontdata.hb.vscale)
2112 harfbuzz.shape_full(hbfont, buff, spec.hb_features, shaper and {shaper} or {})
2113 local glyphs = buff:get_glyphs()
2114 if glyphs and glyphs[1] then
2115 if charcode == 32 then
2116 return glyphs[1].x_advance, glyphs[1].y_advance
2117 end
2118 return glyphs[1].codepoint + fontdata.hb.shared.gid_offset
2119 end
2120 end
2121 end
2122
2123 local process_vertical_font
2124 do
2125 local get_tsb_table_harf, get_tsb_table_node
2126 if harfbuzz then
2127 local vmtxtag = harfbuzz.Tag.new"vmtx"
2128 local vheatag = harfbuzz.Tag.new"vhea"
2129 function get_tsb_table_harf (fontdata)
2130 local tsb_font_data = fontoptions.tsb_data or {}
2131 local hbface = fontdata.hb.shared.face
2132 local key = tostring(hbface)
2133 if tsb_font_data[key] then
2134 return tsb_font_data[key]
2135 end
2136 local vmtx_b = hbface:get_table(vmtxtag)
2137 local vhea_b = hbface:get_table(vheatag)
2138 if vmtx_b:get_length() > 0 and vhea_b:get_length() > 35 then
2139 local numofglyphs = hbface:get_glyph_count()
2140 local data = vhea_b:get_data()
2141 local numofheights = (">H"):unpack(data, 35)
2142 data = vmtx_b:get_data()
2143 local vmtx, pos, ht, tsb = { }, 1
2144 for i = 0, numofglyphs-1 do
2145 if i < numofheights then
2146 ht, pos = (">H"):unpack(data, pos)
2147 end
2148 tsb, pos = (">h"):unpack(data, pos)
2149 vmtx[i] = { ht = ht, tsb = tsb }
2150 end
2151 tsb_font_data[key] = vmtx
2152 return vmtx
2153 end
2154 end
2155 end
2156 do
2157 local streamreader = utilities.files
2158 local openfile = streamreader.open
2159 local closefile = streamreader.close
2160 local readstring = streamreader.readstring
2161 local readulong = streamreader.readcardinal4
2162 local readushort = streamreader.readcardinal2
2163 local readfixed = streamreader.readfixed4
2164 local readshort = streamreader.readinteger2
2165 local setpos = streamreader.setposition
2166
2167 local function get_otf_tables (f, subfont)
2168 if f then
2169 local sfntversion = readstring(f,4)
2170 if sfntversion == "ttcf" then
2171 local ttcversion = readfixed(f)
2172 local numfonts = readulong(f)
2173 if subfont >= 1 and subfont <= numfonts then
2174 local offsets = {}
2175 for i = 1, numfonts do
2176 offsets[i] = readulong(f)
2177 end
2178 setpos(f, offsets[subfont])
2179 sfntversion = readstring(f,4)
2180 end
2181 end
2182 if sfntversion == "OTTO" or sfntversion == "true" or sfntversion == "\0\1\0\0" then
2183 local numtables = readushort(f)
2184 local searchrange = readushort(f)
2185 local entryselector = readushort(f)
2186 local rangeshift = readushort(f)
2187 local tables = {}
2188 for i= 1, numtables do
2189 local tag = readstring(f,4)
2190 tables[tag] = {
2191 checksum = readulong(f),
2192 offset = readulong(f),
2193 length = readulong(f),
2194 }
2195 end
2196 return tables
2197 end
2198 end
2199 end
2200
2201 local function read_maxp (f, t)
2202 if f and t then
2203 setpos(f, t.offset)
2204 return {
2205 version = readfixed(f),
2206 numglyphs = readushort(f),
2207 }
2208 end
2209 end
2210
2211 local function read_vhea (f, t)
2212 if f and t then
2213 setpos(f, t.offset)
2214 return {
2215 version = readfixed(f),
2216 ascent = readshort(f),
2217 descent = readshort(f),
2218 lineGap = readshort(f),
2219 advanceheightmax = readshort(f),
2220 mintopsidebearing = readshort(f),
2221 minbottomsidebrearing = readshort(f),
2222 ymaxextent = readshort(f),
2223 caretsloperise = readshort(f),
2224 caretsloperun = readshort(f),
2225 caretoffset = readshort(f),
2226 reserved1 = readshort(f),
2227 reserved2 = readshort(f),
2228 reserved3 = readshort(f),
2229 reserved4 = readshort(f),
2230 metricdataformat = readshort(f),
2231 numheights = readushort(f),
2232 }
2233 end
2234 end
2235
2236 local function read_vmtx (f, t, numofheights, numofglyphs)
2237 if f and t and numofheights and numofglyphs then
2238 setpos(f, t.offset)
2239 local vmtx = {}
2240 local height = 0
2241 for i = 0, numofheights-1 do
2242 height = readushort(f)
2243 vmtx[i] = {
2244 ht = height,
2245 tsb = readshort(f),
2246 }
2247 end
2248 for i = numofheights, numofglyphs-1 do
2249 vmtx[i] = {
2250 ht = height,
2251 tsb = readshort(f),
2252 }
2253 end
2254 return vmtx
2255 end
2256 end
2257
2258 function get_tsb_table_node (fontdata)
2259 local tsb_font_data = fontoptions.tsb_data or {}
2260 local filename = fontdata.specification.filename or fontdata.filename
2261 local subfont = tonumber(fontdata.subfont) or 1
2262 local key = ("%s::%s"):format(filename, subfont)
2263 if tsb_font_data[key] then
2264 return tsb_font_data[key]
2265 end
2266 local f = openfile(filename, true) -- true: zero-based
2267 if f then
2268 local vmtx
2269 local tables = get_otf_tables(f, subfont)
2270 if tables then
2271 local vhea = read_vhea(f, tables.vhea)
2272 local numofheights = vhea and vhea.numheights
2273 local maxp = read_maxp(f, tables.maxp)
2274 local numofglyphs = maxp and maxp.numglyphs
2275 vmtx = read_vmtx(f, tables.vmtx, numofheights, numofglyphs)
2276 end
2277 closefile(f)
2278 tsb_font_data[key] = vmtx
2279 return vmtx
2280 end
2281 end
2282 end
2283
2284 local function fontdata_warning(activename, ...)
2285 if not active_processes[activename] then
2286 warning(...)
2287 active_processes[activename] = true
2288 end
2289 end
2290
2291 local dfltfntsize = get_font_param(font.current(), "quad") or 655360
2292
2293 function process_vertical_font (fontdata)
2294 local fullname = fontdata.fullname
2295
2296 if not fontdata.hb and fontdata.type == "virtual" then
2297 fontdata_warning("vitrual."..fullname,
2298 "Virtual font `%s' cannot be\nused for vertical writing.", fullname)
2299 return
2300 end
2301
2302 local tsb_tab = fontdata.hb and get_tsb_table_harf(fontdata) or get_tsb_table_node(fontdata)
2303
2304 if not tsb_tab then
2305 fontdata_warning("vertical."..fullname,
2306 "Vertical metrics table (vmtx) not found in the font\n`%s'", fullname)
2307 return
2308 end
2309
2310 local extend = (fontdata.extend or 1000)/1000
2311 local squeeze = (fontdata.squeeze or 1000)/1000
2312
2313 local shared = fontdata.shared or {}
2314 local descriptions = shared.rawdata and shared.rawdata.descriptions or {}
2315 local parameters = fontdata.parameters or {}
2316 local scale = fontdata.hb and fontdata.hb.scale or parameters.factor or 655.36
2317 local quad = parameters.quad or 655360
2318 local xheight = parameters.x_height and parameters.x_height / squeeze * extend or quad/2
2319 local ascender = fontdata.hb and get_asc_desc(fontdata.hb) or parameters.ascender or quad*0.8
2320
2321 local goffset = xheight/2 * (dfltfntsize / quad) -- TODO?
2322
2323 -- declare shift amount of horizontal box inside vertical env.
2324 if fontdata.vertcharraise then return end -- avoid multiple running
2325 fontdata.vertcharraise = goffset
2326
2327 local charraise
2328 if fontdata.hb then
2329 charraise = font_opt_dim(fontdata, "charraise") or 0
2330 fontdata.luatexko_charraise = charraise -- for luamplib
2331 end
2332
2333 for i,v in pairs(fontdata.characters) do
2334 local voff = goffset - (v.width or 0)/2
2335 local gid = v.index
2336 local bbox = fontdata.hb and get_hb_char_bbox(fontdata, gid)
2337 or descriptions[i] and descriptions[i].boundingbox or {0,0,0,0}
2338 local tsb = tsb_tab[gid] and tsb_tab[gid].tsb
2339 local hoff = tsb and (bbox[4] + tsb) * scale * squeeze or ascender
2340
2341 if fontdata.hb then
2342 v.luatexko_voff = voff
2343 v.luatexko_hoff = hoff
2344 else
2345 v.commands = {
2346 { "down", -voff },
2347 { "right", hoff },
2348 { "pdf", "q 0 1 -1 0 0 0 cm" },
2349 { "push" },
2350 { "char", i },
2351 { "pop" },
2352 { "pdf", "Q" },
2353 }
2354 end
2355
2356 local vw = tsb_tab[gid] and tsb_tab[gid].ht
2357 vw = vw and vw * scale * squeeze or quad
2358
2359 -- character width shall be consistent with the width in the font program
2360 local diff = vw - v.width
2361 if diff and diff ~= 0 then
2362 v.luatexko_diff = diff
2363 end
2364
2365 local ht = bbox[3] * scale * extend + voff
2366 local dp = bbox[1] * scale * extend + voff
2367 if fontdata.hb then
2368 ht, dp = ht + charraise, dp + charraise
2369 end
2370 v.height = ht > 0 and ht or 0
2371 v.depth = dp < 0 and -dp or 0
2372 end
2373 local spacechar = char_in_font(fontdata, 32)
2374 if spacechar then
2375 local wd = spacechar.width or parameters.space
2376 wd = wd + (spacechar.luatexko_diff or 0)
2377 parameters.space = wd
2378 parameters.space_stretch = wd/2
2379 parameters.space_shrink = wd/2
2380 end
2381 parameters.ascender = quad/2 + goffset
2382 parameters.descender = quad/2 - goffset
2383
2384 local fea = shared.features or {}
2385 fea.kern = nil -- only for horizontal writing
2386 fea.vert = true -- should be activated by default
2387
2388 if fontdata.hb then
2389 local hb_features, t = fontdata.specification.hb_features or { }, { }
2390 for i,v in ipairs(hb_features) do
2391 t[tostring(v)] = i
2392 end
2393 if t.kern then table.remove(hb_features, t.kern) end
2394 if not t.vert then hb_features[#hb_features+1] = harfbuzz.Feature.new"vert" end
2395 -- now reset hb.space and parameters.space: see also the otfregister.features below
2396 local spacewidth = get_HB_variant_char(fontdata, 32)
2397 local _, spaceheight = get_HB_variant_char(fontdata, 32, true) -- ttb
2398 if spacewidth and spaceheight then
2399 fontdata.hb.space = spacewidth * scale
2400 fontdata.parameters.space = -spaceheight * scale
2401 end
2402 --
2403 if t.vhal then -- harf-mode vhal feature not working properly with luatexko, so an alternative
2404 table.remove(hb_features, t.vhal)
2405 fea.vhal = nil
2406 fea.compresspunctuations = true
2407 activate_process("post_shaping_filter", process_glyph_width, "compresspunctuations")
2408 end
2409 return
2410 end
2411
2412 local res = fontdata.resources or {}
2413 local seq = res.sequences or {}
2414 for _,v in ipairs(seq) do
2415 local fea = v.features or {}
2416 if fea.vhal or fea.vkrn or fea.valt or fea.vpal or fea.vert then
2417 if v.type == "gpos_single" then
2418 for _,vv in pairs(v.steps or {}) do
2419 for _,vvv in pairs(vv.coverage or {}) do
2420 if type(vvv) == "table" and #vvv == 4 then
2421 vvv[1], vvv[2], vvv[3], vvv[4], vvv[5] =
2422 -vvv[2], vvv[1], vvv[4], vvv[3], 0 -- last 0 to avoid multiple run
2423 end
2424 end
2425 end
2426 elseif v.type == "gpos_pair" then
2427 for _,vv in pairs(v.steps or {}) do
2428 for _,vvv in pairs(vv.coverage or {}) do
2429 for _,vvvv in pairs(vvv) do
2430 for _,vvvvv in pairs(vvvv) do
2431 if type(vvvvv) == "table" and #vvvvv == 4 then
2432 vvvvv[1], vvvvv[2], vvvvv[3], vvvvv[4], vvvvv[5] =
2433 -vvvvv[2], vvvvv[1], vvvvv[4], vvvvv[3], 0
2434 end
2435 end
2436 end
2437 end
2438 end
2439 end
2440 end
2441 end
2442 end
2443 end
2444
2445 local process_vertical_diff
2446 do
2447 local iwspaceattributeid = luatexbase.attributes.g__tag_interwordspace_attr
2448 function process_vertical_diff (head)
2449 local curr = head
2450 while curr do
2451 if curr.id == glyphid
2452 and fontoptions.is_vertical[curr.font]
2453 and not has_attribute(curr, verticalattr) then
2454
2455 set_attribute(curr, verticalattr, 1)
2456 local chardata = char_in_font(curr.font, curr.char)
2457 local diff = chardata and chardata.luatexko_diff or 0
2458
2459 if has_harf_data(curr.font) then -- harf-mode
2460 local charraise = fontoptions.charraise[curr.font] or 0
2461 local yofforig = curr.yoffset - charraise
2462 diff = diff + curr.width - yofforig
2463 curr.yoffset = yofforig - (chardata.luatexko_hoff or 0)
2464 curr.xoffset = curr.xoffset + (chardata.luatexko_voff or 0) + charraise
2465 local save = nodenew(whatsitid, save_whatsit)
2466 local matrix = nodenew(whatsitid, matrix_whatsit)
2467 matrix.data = "0 1 -1 0"
2468 local restore = nodenew(whatsitid, restore_whatsit)
2469 save.attr, matrix.attr, restore.attr = curr.attr, curr.attr, curr.attr
2470 set_attribute(save, charhead, 1)
2471 set_attribute(matrix, charhead, 2)
2472 head = insert_before(head, curr, save)
2473 head = insert_before(head, curr, matrix)
2474 if curr.height ~= 0 then
2475 local rule = nodenew(ruleid)
2476 rule.height, rule.depth, rule.width = curr.height, 0, 0
2477 rule.subtype, rule.attr = 3, curr.attr
2478 set_attribute(rule, charhead, 3)
2479 head = insert_before(head, curr, rule)
2480 end
2481 if curr.width ~= 0 then
2482 local kern = nodenew(kernid)
2483 kern.kern = -curr.width
2484 head, curr = insert_after(head, curr, kern)
2485 end
2486 head, curr = insert_after(head, curr, restore)
2487 if iwspaceattributeid then -- for tagging fakespace
2488 local n = getnext(curr)
2489 if n and n.id == glueid and n.subtype == 13 then -- spaceskip
2490 set_attribute(n, iwspaceattributeid, 1)
2491 end
2492 end
2493 end
2494
2495 if tex.sp(diff) ~= 0 then
2496 local k = nodenew(kernid)
2497 k.kern = diff
2498 head, curr = insert_after(head, curr, k)
2499 end
2500 end
2501 curr = getnext(curr)
2502 end
2503 return head
2504 end
2505 end
2506
2507 function luatexko.gethorizboxmoveright ()
2508 for _, v in ipairs{ font.current(),
2509 tex.attribute.luatexkohangulfontattr,
2510 tex.attribute.luatexkohanjafontattr,
2511 tex.attribute.luatexkofallbackfontattr } do
2512 if v and v > 0 then
2513 local amount = fontoptions.vertcharraise[v]
2514 if amount then
2515 amount = amount + (fontoptions.charraise[v] or 0)
2516 token.set_macro("luatexkohorizboxmoveright", tex.sp(amount).."sp")
2517 break
2518 end
2519 end
2520 end
2521 end
2522
2523 -- charraise
2524
2525 local function process_charraise (head)
2526 local curr = head
2527 while curr do
2528 local id = curr.id
2529 if id == glyphid then
2530 if not has_attribute(curr,charraiseattr) then
2531 local raise = fontoptions.charraise[curr.font]
2532 if raise then
2533 curr.yoffset = (curr.yoffset or 0) + raise
2534 end
2535 set_attribute(curr, charraiseattr, 1)
2536 end
2537 elseif id == discid then
2538 process_charraise(curr.pre)
2539 process_charraise(curr.post)
2540 process_charraise(curr.replace)
2541 end
2542 curr = getnext(curr)
2543 end
2544 return head
2545 end
2546
2547 -- fake italic correctioin
2548
2549 local process_fake_slant_corr
2550 do
2551 local function harf_reordered_tonemark (curr)
2552 if has_harf_data(curr.font) then
2553 local props = getproperty(curr) or {}
2554 local actualtext = props.luaotfload_startactualtext
2555 return actualtext and actualtext:find"302[EF]$"
2556 end
2557 end
2558 function process_fake_slant_corr (head) -- for font fallback
2559 local curr = head
2560 while curr do
2561 local id = curr.id
2562 if id == kernid then
2563 if curr.subtype == 3 and curr.kern == 0 then -- italcorr
2564 local p, t = getprev(curr), {}
2565 while p do
2566 if p.id == glyphid and not fontoptions.is_vertical[p.font] then
2567 -- harf font: break before reordered tone mark
2568 if harf_reordered_tonemark(p) then
2569 break
2570 end
2571
2572 local slant = fontoptions.slantvalue[p.font]
2573 if slant and slant > 0 then
2574 t[#t+1] = char_in_font(p.font, p.char).italic or 0
2575 end
2576
2577 local c = has_attribute(p, unicodeattr) or p.char
2578 if is_jungsong(c) or is_jongsong(c) or hangul_tonemark[c] then
2579 else
2580 break
2581 end
2582 elseif p.id == whatsitid then
2583 else
2584 break
2585 end
2586 p = getprev(p)
2587 end
2588
2589 if p.id == glyphid and #t > 0 then
2590 local italic = math.max(table.unpack(t))
2591 if italic > 0 then
2592 curr.kern = italic
2593 end
2594 end
2595 end
2596 elseif id == mathid then
2597 curr = end_of_math(curr)
2598 end
2599 curr = getnext(curr)
2600 end
2601 return head
2602 end
2603 end
2604
2605 local function process_fake_slant_font (fontdata, fsl)
2606 if fsl and fsl > 0 then
2607 fontdata.slant = fsl*1000
2608
2609 local params = fontdata.parameters or {}
2610 params.slant = (params.slant or 0) + fsl*65536 -- slant per point
2611
2612 local hb = has_harf_data(fontdata)
2613 local scale = hb and hb.scale or params.factor or 655.36
2614 local shared = fontdata.shared or {}
2615 local descrs = shared.rawdata and shared.rawdata.descriptions or {}
2616
2617 for i, v in pairs(fontdata.characters) do
2618 local ht = v.height and v.height > 0 and v.height or 0
2619 local wd = v.width and v.width > 0 and v.width or 0
2620 local rbearing = 0
2621
2622 if wd > 0 then -- or, jong/jung italic could by very large value
2623 local bbox = hb and get_hb_char_bbox(fontdata, v.index)
2624 or descrs[i] and descrs[i].boundingbox
2625 if bbox then
2626 rbearing = wd - bbox[3]*scale
2627 end
2628 end
2629
2630 local italic = ht * fsl - rbearing
2631 if italic > 0 then
2632 v.italic = italic
2633 end
2634 end
2635 end
2636 end
2637
2638 -- AC00 11A8, AC00 11F0 ...
2639 -- these should not happen in KS-observing documents
2640 -- but HarfBuzz supports these sequences anyway.
2641 local function normalize_syllable_TC (head)
2642 local curr = head
2643 while curr do
2644 if curr.id == glyphid and not has_harf_data(curr.font) then
2645 local c, f = curr.char, curr.font
2646 if is_hangul(c) and (c - 0xAC00) % 28 == 0 then
2647 local t = getnext(curr)
2648 if t then
2649 if t.id == glyphid then
2650 local tc, tf = t.char, t.font
2651 if is_jongsong(tc) and f == tf then
2652 if tc <= 0x11C2 then
2653 curr.char = c + tc - 0x11A7
2654 noderemove(head, t)
2655 nodefree(t)
2656 else
2657 c = (c - 0xAC00) // 28
2658 curr.char = c // 21 + 0x1100
2659 local v = nodecopy(curr)
2660 v.char = c % 21 + 0x1161
2661 insert_after(head, curr, v)
2662 curr = t
2663 end
2664 end
2665 else
2666 curr = t
2667 end
2668 end
2669 end
2670 end
2671 curr = getnext(curr)
2672 end
2673 end
2674
2675 -- wrap up
2676
2677 luatexbase.add_to_callback ("hyphenate",
2678 function(head)
2679 normalize_syllable_TC(head)
2680 process_fonts(head)
2681 lang.hyphenate(head)
2682 end,
2683 "luatexko.hyphenate.fonts_and_languages")
2684
2685 luatexbase.create_callback("luatexko_prelinebreak_first", "data", function(...) return ... end)
2686 luatexbase.create_callback("luatexko_prelinebreak_second", "data", function(...) return ... end)
2687 luatexbase.create_callback("luatexko_prelinebreak_third", "data", function(...) return ... end)
2688
2689 luatexbase.add_to_callback("pre_shaping_filter", function(h)
2690 local par = h.id == localparid
2691 h = luatexbase.call_callback("luatexko_prelinebreak_first", h, par)
2692 h = process_cjk_punctuation_spacing(h, par)
2693 h = luatexbase.call_callback("luatexko_prelinebreak_second", h, par)
2694 h = process_linebreak(h, par)
2695 h = luatexbase.call_callback("luatexko_prelinebreak_third", h, par)
2696 return h
2697 end, "luatexko.pre_shaping_filter")
2698
2699 local otfregister = fonts.constructors.features.otf.register
2700
2701 otfregister {
2702 name = "removeclassicspaces",
2703 description = "remove spaces in classic typesetting",
2704 default = false,
2705 manipulators = {
2706 node = function()
2707 activate_process("luatexko_prelinebreak_first", process_remove_spaces, "removeclassicspaces")
2708 end,
2709 plug = function()
2710 activate_process("luatexko_prelinebreak_first", process_remove_spaces, "removeclassicspaces")
2711 end,
2712 },
2713 }
2714
2715 otfregister {
2716 name = "interhangul",
2717 description = "insert more glue between Hangul chars",
2718 default = false,
2719 manipulators = {
2720 node = function()
2721 activate_process("luatexko_prelinebreak_second", process_interhangul, "interhangul")
2722 end,
2723 plug = function()
2724 activate_process("luatexko_prelinebreak_second", process_interhangul, "interhangul")
2725 end,
2726 },
2727 }
2728
2729 otfregister {
2730 name = "interlatincjk",
2731 description = "insert glue between CJK and Latin",
2732 default = false,
2733 manipulators = {
2734 node = function()
2735 activate_process("luatexko_prelinebreak_second", process_interlatincjk, "interlatincjk")
2736 end,
2737 plug = function()
2738 activate_process("luatexko_prelinebreak_second", process_interlatincjk, "interlatincjk")
2739 end,
2740 },
2741 }
2742
2743 otfregister {
2744 name = "charraise",
2745 description = "raise chars",
2746 default = false,
2747 manipulators = {
2748 node = function()
2749 if not active_processes.charraise then
2750 charraiseattr = luatexbase.new_attribute"luatexko_char_raise_attr"
2751 end
2752 activate_process("post_shaping_filter", process_charraise, "charraise")
2753 end,
2754 plug = function()
2755 if not active_processes.charraise then
2756 charraiseattr = luatexbase.new_attribute"luatexko_char_raise_attr"
2757 end
2758 activate_process("post_shaping_filter", process_charraise, "charraise")
2759 end,
2760 },
2761 }
2762
2763 otfregister {
2764 name = "compresspunctuations",
2765 description = "compress width of CJK punctuations",
2766 default = false,
2767 manipulators = {
2768 node = function()
2769 activate_process("post_shaping_filter", process_glyph_width, "compresspunctuations")
2770 end,
2771 plug = function()
2772 activate_process("post_shaping_filter", process_glyph_width, "compresspunctuations")
2773 end,
2774 },
2775 }
2776
2777 otfregister {
2778 name = "slant",
2779 description = "fake slant fallback fonts",
2780 default = false,
2781 manipulators = {
2782 node = function(fontdata, _, value)
2783 process_fake_slant_font(fontdata, value)
2784 activate_process("post_shaping_filter", process_fake_slant_corr, "slant")
2785 end,
2786 plug = function(fontdata, _, value)
2787 process_fake_slant_font(fontdata, value)
2788 activate_process("post_shaping_filter", process_fake_slant_corr, "slant")
2789 end,
2790 },
2791 }
2792
2793 otfregister {
2794 name = "vertical",
2795 description = "vertical typesetting",
2796 default = false,
2797 manipulators = {
2798 node = function(fontdata)
2799 if not active_processes.verticalwriting then
2800 verticalattr = luatexbase.new_attribute"luatexko_vertical_attr"
2801 end
2802 process_vertical_font(fontdata)
2803 activate_process("post_shaping_filter", process_vertical_diff, "verticalwriting", 1)
2804 end,
2805 plug = function(fontdata) -- experimental
2806 if not active_processes.verticalwriting then
2807 verticalattr = luatexbase.new_attribute"luatexko_vertical_attr"
2808 end
2809 process_vertical_font(fontdata)
2810 activate_process("post_shaping_filter", process_vertical_diff, "verticalwriting", 1)
2811 end,
2812 },
2813 }
2814
2815 otfregister {
2816 name = "expansion",
2817 description = "glyph expansion",
2818 default = false,
2819 manipulators = {
2820 node = function()
2821 if tex.adjustspacing == 0 then
2822 tex.set("global", "adjustspacing", 2)
2823 end
2824 end,
2825 plug = function(fontdata, _, value)
2826 local setup = fonts.expansions.setups[value] or {}
2827 fontdata.stretch = fontdata.stretch or (setup.stretch or 2)*10
2828 fontdata.shrink = fontdata.shrink or (setup.shrink or 2)*10
2829 fontdata.step = fontdata.step or (setup.step or .5)*10
2830 if tex.adjustspacing == 0 then
2831 tex.set("global", "adjustspacing", 2)
2832 end
2833 end,
2834 },
2835 }
2836
2837 fonts.protrusions.setups.default[0xFF0C] = { 0, 1 }
2838 fonts.protrusions.setups.default[0xFF0E] = { 0, 1 }
2839 fonts.protrusions.setups.default[0xFF1A] = { 0, 1 }
2840 fonts.protrusions.setups.default[0xFF1B] = { 0, 1 }
2841
2842 otfregister {
2843 name = "protrusion",
2844 description = "glyph protrusion",
2845 default = false,
2846 manipulators = {
2847 node = function(fontdata, _, value)
2848 local setup = fonts.protrusions.setups[value] or {}
2849 local quad = fontdata.parameters.quad
2850 local left,right,factor = setup.left or 1, setup.right or 1, setup.factor or 1
2851 for i, v in pairs(fontdata.characters) do
2852 local uni = v.unicode
2853 if uni then
2854 local lr = setup[uni]
2855 if lr then
2856 local wdq = (v.width + (v.luatexko_diff or 0))/quad*1000
2857 local l, r = lr[1], lr[2]
2858 if l and l ~= 0 then v.left_protruding = wdq*l*left*factor end
2859 if r and r ~= 0 then v.right_protruding = wdq*r*right*factor end
2860 end
2861 end
2862 end
2863 if tex.protrudechars == 0 then
2864 tex.set("global", "protrudechars", 2)
2865 end
2866 end,
2867 plug = function(fontdata, _, value)
2868 local setup = fonts.protrusions.setups[value] or {}
2869 local quad = fontdata.parameters.quad
2870 local chrs = fontdata.characters
2871 local left,right,factor = setup.left or 1, setup.right or 1, setup.factor or 1
2872 for i, v in pairs(setup) do
2873 if chrs[i] then
2874 local l, r = v[1], v[2]
2875 for _, ii in ipairs{i, get_HB_variant_char(fontdata,i)} do
2876 local chr = chrs[ii]
2877 if chr then
2878 local wdq = (chr.width + (chr.luatexko_diff or 0))/quad*1000
2879 if l and l ~= 0 then chr.left_protruding = wdq*l*left*factor end
2880 if r and r ~= 0 then chr.right_protruding = wdq*r*right*factor end
2881 end
2882 end
2883 end
2884 end
2885 if tex.protrudechars == 0 then
2886 tex.set("global", "protrudechars", 2)
2887 end
2888 end,
2889 },
2890 }
2891
2892 -- reset hb.space and parameters.space :
2893 -- with \spacekip and noto cjk fonts, harf mode yields different glue width from the node mode.
2894 -- as harf mode checks if space x_advance matches hb.space, and, if not, adjusts the space glue.
2895 -- the fix below is to avoid this ajdustment.
2896 otfregister {
2897 name = "features",
2898 description = "patch harf-mode hb.space",
2899 default = false,
2900 manipulators = {
2901 plug = function(fontdata, _, value)
2902 if value == "harf"
2903 and option_in_font(fontdata, "script") == "hang"
2904 and not option_in_font(fontdata, "vertical") -- already done
2905 then
2906 local spacewidth = get_HB_variant_char(fontdata, 32)
2907 if spacewidth then
2908 fontdata.hb.space = spacewidth * fontdata.hb.scale
2909 fontdata.parameters.space = fontdata.hb.space
2910 end
2911 end
2912 end,
2913 },
2914 }
2915
2916 do
2917 local auxiliary_procs = {
2918 dotemph = {
2919 post_linebreak_filter = process_dotemph,
2920 hpack_filter = process_dotemph,
2921 },
2922 uline = {
2923 post_linebreak_filter = function(h) return process_uline(h) end,
2924 hpack_filter = function(h) return process_uline(h) end,
2925 },
2926 ruby = {
2927 pre_linebreak_filter = process_ruby_pre_linebreak,
2928 hpack_filter = process_ruby_pre_linebreak,
2929 },
2930 autojosa = {
2931 luatexko_prelinebreak_first = process_josa,
2932 },
2933 reorderTM = {
2934 luatexko_prelinebreak_third = process_reorder_tonemarks,
2935 },
2936 }
2937
2938 -- dotemph 등을 수식 한글에서도 작동하게 하려면
2939 -- post_mlist_to_hlist_filter 콜백을 이용해야 한다.
2940
2941 function luatexko.activate (name)
2942 for cbnam, cbfun in pairs( auxiliary_procs[name] ) do
2943 local myname = "luatexko." .. cbnam .. "." .. name
2944 if cbnam == "hpack_filter" then
2945 luatexbase.declare_callback_rule(cbnam, myname, "before", "luaotfload.color_handler")
2946 end
2947 luatexbase.add_to_callback(cbnam, cbfun, myname)
2948 end
2949 active_processes[name] = true
2950 end
2951 end
2952
2953 -- aux functions
2954
2955 function luatexko.deactivateall (str)
2956 luatexko.deactivated = {}
2957 for _, name in ipairs{ "pre_shaping_filter",
2958 "post_shaping_filter",
2959 "pre_linebreak_filter",
2960 "hpack_filter",
2961 "hyphenate",
2962 "post_linebreak_filter",
2963 } do
2964 local t = {}
2965 for i, v in ipairs( luatexbase.callback_descriptions(name) ) do
2966 if v:find(str or "^luatexko%.") then
2967 local ff, dd = luatexbase.remove_from_callback(name, v)
2968 t[#t+1] = { ff, dd, i }
2969 end
2970 end
2971 luatexko.deactivated[name] = t
2972 end
2973 end
2974
2975 function luatexko.reactivateall ()
2976 for name, v in pairs(luatexko.deactivated or {}) do
2977 for _, vv in ipairs(v) do
2978 luatexbase.add_to_callback(name, table.unpack(vv))
2979 end
2980 end
2981 luatexko.deactivated = nil
2982 end
2983
2984 -- xxruby
2985
2986 local function get_unicode_graphemes (s)
2987 local t = { }
2988 local graphemes = require'lua-uni-graphemes'
2989 for _, _, c in graphemes.graphemes(s) do
2990 t[#t+1] = c
2991 end
2992 return t
2993 end
2994 local xxruby_index = luatexbase.new_luafunction"luatexko_xxruby_func"
2995 lua.get_functions_table()[xxruby_index] = function ()
2996 local base = token.scan_argument()
2997 local ruby = token.scan_argument()
2998 local base_t = get_unicode_graphemes(base)
2999 local ruby_t = get_unicode_graphemes(ruby)
3000 local t = { }
3001 local function ruby_args_to_t (arg1, arg2)
3002 for _,v in ipairs{ token.create"ruby",
3003 token.new(0,1), arg1, token.new(0,2),
3004 token.new(0,1), arg2, token.new(0,2), } do
3005 t[#t+1] = v
3006 end
3007 end
3008 if #base_t == #ruby_t then
3009 for i=1, #base_t do
3010 ruby_args_to_t(base_t[i], ruby_t[i])
3011 end
3012 else
3013 warning"lengths of base/ruby characters should be identical.\nrevert to \\ruby."
3014 ruby_args_to_t(base, ruby)
3015 end
3016 tex.sprint(-2, t)
3017 end
3018 token.set_lua("xxruby", xxruby_index, "global", "protected")
3019
3020 local inhibitglue_index = luatexbase.new_luafunction"luatexko_inhibitglue_func"
3021 token.set_lua("inhibitglue", inhibitglue_index, "global", "protected")
3022 lua.get_functions_table()[inhibitglue_index] = function ()
3023 local icp, ics
3024 for _, v in ipairs {
3025 font.current(),
3026 tex.attribute.luatexkohangulfontattr,
3027 tex.attribute.luatexkohanjafontattr,
3028 tex.attribute.luatexkofallbackfontattr,
3029 }
3030 do
3031 icp = icp or fontoptions.intercharpenalty[v]
3032 ics = ics or fontoptions.intercharstretch[v]
3033 if icp and ics then break end
3034 end
3035
3036 if tex.lastnodetype ~= 13 then -- penalty node
3037 icp = icp or 50
3038 if icp ~= 0 then
3039 local p = nodenew(penaltyid)
3040 p.penalty = icp
3041 set_attribute(p, inhibitglueattr, 1)
3042 nodewrite(p)
3043 end
3044 end
3045
3046 ics = ics or stretch_f * fontoptions.en_size[font.current()]
3047 local g = nodenew(glueid)
3048 setglue(g, 0, ics, ics*0.6)
3049 if iwspaceOffattributeid then
3050 set_attribute(g, iwspaceOffattributeid, 1)
3051 end
3052 set_attribute(g, inhibitglueattr, 1)
3053 nodewrite(g)
3054 end
3055