OSDN Git Service

try to reduce "Command \selectfont has changed." warning message
[luatex-ja/luatexja.git] / src / ltj-jfmglue.lua
1 --
2 -- luatexja/ltj-jfmglue.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.jfmglue',
6   date = '2014/02/02',
7   description = 'Insertion process of JFM glues and kanjiskip',
8 })
9 module('luatexja.jfmglue', package.seeall)
10 local err, warn, info, log = luatexbase .errwarinf(_NAME)
11
12 luatexja.load_module('base');      local ltjb = luatexja.base
13 luatexja.load_module('stack');     local ltjs = luatexja.stack
14 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
15 luatexja.load_module('direction'); local ltjd = luatexja.direction
16 local pairs = pairs
17
18 local Dnode = node.direct or node
19
20 local nullfunc = function(n) return n end
21 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
22 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
23
24 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
25 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
26 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
27 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
28 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
29 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
30 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
31
32 local has_attr = Dnode.has_attribute
33 local set_attr = Dnode.set_attribute
34 local insert_before = Dnode.insert_before
35 local insert_after = Dnode.insert_after
36 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
37 local round = tex.round
38 local ltjd_make_dir_node = ltjd.make_dir_node
39 local ltjf_font_metric_table = ltjf.font_metric_table
40 local ltjf_find_char_class = ltjf.find_char_class
41 local node_new = Dnode.new
42 local node_copy = Dnode.copy
43 local node_remove = luatexja.Dnode_remove -- Dnode.remove
44 local node_tail = Dnode.tail
45 local node_free = Dnode.free
46 local node_end_of_math = Dnode.end_of_math
47
48 local dir_tate = 3
49 local dir_yoko  = 4
50
51 local id_glyph = node.id('glyph')
52 local id_hlist = node.id('hlist')
53 local id_vlist = node.id('vlist')
54 local id_rule = node.id('rule')
55 local id_ins = node.id('ins')
56 local id_mark = node.id('mark')
57 local id_adjust = node.id('adjust')
58 local id_disc = node.id('disc')
59 local id_whatsit = node.id('whatsit')
60 local id_math = node.id('math')
61 local id_glue = node.id('glue')
62 local id_kern = node.id('kern')
63 local id_penalty = node.id('penalty')
64
65 local id_glue_spec = node.id('glue_spec')
66 local id_jglyph    = 512 -- Japanese character
67 local id_box_like  = 256 -- vbox, shifted hbox
68 local id_pbox      = 257 -- already processed nodes (by \unhbox)
69 local id_pbox_w    = 258 -- cluster which consists of a whatsit
70 local sid_user = node.subtype('user_defined')
71
72 local sid_start_link = node.subtype('pdf_start_link')
73 local sid_start_thread = node.subtype('pdf_start_thread')
74 local sid_end_link = node.subtype('pdf_end_link')
75 local sid_end_thread = node.subtype('pdf_end_thread')
76
77 local ITALIC       = luatexja.icflag_table.ITALIC
78 local PACKED       = luatexja.icflag_table.PACKED
79 local KINSOKU      = luatexja.icflag_table.KINSOKU
80 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
81 local PROCESSED    = luatexja.icflag_table.PROCESSED
82 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
83 local BOXBDD       = luatexja.icflag_table.BOXBDD
84 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
85 local kanji_skip
86 local xkanji_skip
87 local table_current_stack
88 local list_dir
89
90 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
91 local attr_dir = luatexbase.attributes['ltj@dir']
92 local attr_icflag = luatexbase.attributes['ltj@icflag']
93
94 local function get_attr_icflag(p)
95    return (has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG
96 end
97
98 -------------------- Helper functions
99
100 local function copy_attr(new, old)
101   -- 仕様が決まるまで off にしておく
102 end
103
104 -- This function is called only for acquiring `special' characters.
105 local function fast_find_char_class(c,m)
106    return m.chars[c] or 0
107 end
108
109 -- 文字クラスの決定
110 local slow_find_char_class
111 do
112    local start_time_measure = ltjb.start_time_measure
113    local stop_time_measure = ltjb.stop_time_measure
114    slow_find_char_class = function (c, m, oc)
115       start_time_measure('slow_find_chr')
116       local cls = ltjf_find_char_class(oc, m)
117       if not c and  cls==0 then 
118          stop_time_measure('slow_find_chr')
119          return ltjf_find_char_class(-c, m), oc
120       else
121          stop_time_measure('slow_find_chr')
122          return cls, oc
123       end
124    end
125 end
126
127 local zero_glue = node_new(id_glue)
128 spec_zero_glue = to_node(node_new(id_glue_spec))
129   -- must be public, since mentioned from other sources
130 local spec_zero_glue = to_direct(spec_zero_glue)
131 setfield(spec_zero_glue, 'width', 0)
132 setfield(spec_zero_glue, 'stretch', 0)
133 setfield(spec_zero_glue, 'shrink', 0)
134 setfield(spec_zero_glue, 'stretch_order', 0)
135 setfield(spec_zero_glue, 'shrink_order', 0)
136 setfield(zero_glue, 'spec', spec_zero_glue)
137
138 local function skip_table_to_spec(n)
139    local g, st = node_new(id_glue_spec), ltjs.fast_get_stack_skip(n)
140    setfield(g, 'width', st.width)
141    setfield(g, 'stretch', st.stretch)
142    setfield(g, 'shrink', st.shrink)
143    setfield(g, 'stretch_order', st.stretch_order)
144    setfield(g, 'shrink_order', st.shrink_order)
145    return g
146 end
147
148
149 -- penalty 値の計算
150 local function add_penalty(p,e)
151    local pp = getfield(p, 'penalty')
152    if pp>=10000 then
153       if e<=-10000 then pp = 0 end
154    elseif pp<=-10000 then
155       if e>=10000 then pp = 0 end
156    else
157       pp = pp + e
158       if pp>=10000 then      setfield(p, 'penalty', 10000)
159       elseif pp<=-10000 then setfield(p, 'penalty', -10000)
160       else                   setfield(p, 'penalty', pp) end
161    end
162    return
163 end
164
165 -- 「異なる JFM」の間の調整方法
166 diffmet_rule = math.two_paverage
167 function math.two_add(a,b) return a+b end
168 function math.two_average(a,b) return (a+b)*0.5 end
169 function math.two_paverage(a,b) return (a+b)/2 end
170 function math.two_pleft(a,b) return a end
171 function math.two_pright(a,b) return b end
172
173 local head -- the head of current list
174
175 local Np, Nq, Bp
176 local widow_Bp, widow_Np -- \jcharwidowpenalty 挿入位置管理用
177
178 local ihb_flag -- JFM グルー挿入抑止用 flag
179                -- on: \inhibitglue 指定時,hlist の周囲
180
181 -------------------- hlist 内の文字の検索
182
183 local first_char, last_char, find_first_char
184 do
185 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
186 local function check_box(box_ptr, box_end)
187    local p = box_ptr; local found_visible_node = false
188    if not p then
189       find_first_char = false; last_char = nil
190       return true
191    end
192    while p and p~=box_end do
193       local pid = getid(p)
194       if pid==id_kern and getsubtype(p)==2 then
195          p = node_next(node_next(node_next(p))); pid = getid(p) -- p must be glyph_node
196        end
197       if pid==id_glyph then
198          repeat
199             if find_first_char then
200                first_char = p; find_first_char = false
201             end
202             last_char = p; found_visible_node = true; p=node_next(p)
203             if (not p) or p==box_end then
204                return found_visible_node
205             end
206          until getid(p)~=id_glyph
207          pid = getid(p) -- p must be non-nil
208       end
209       if pid==id_kern then
210          local pa = get_attr_icflag(p)
211          --if pa==IC_PROCESSED or pa == PACKED then
212          if pa==IC_PROCESSED then
213             -- do nothing
214          elseif getsubtype(p)==2 then
215             p = node_next(node_next(p));
216             -- Note that another node_next will be executed outside this if-statement.
217          else
218             found_visible_node = true
219             find_first_char = false; last_char = nil
220          end
221       elseif pid==id_hlist then
222          if PACKED == get_attr_icflag(p) then
223             local s = ltjd_glyph_from_packed(p)
224             if find_first_char then
225                first_char = s; find_first_char = false
226             end
227             last_char = s; found_visible_node = true
228          else
229             if getfield(p, 'shift')==0 then
230                if check_box(getlist(p), nil) then found_visible_node = true end
231             else
232                find_first_char = false; last_char = nil
233             end
234          end
235       elseif pid==id_math then
236          if find_first_char then
237             first_char = p; find_first_char = false
238          end
239          last_char = p; found_visible_node = true
240          --elseif pid==id_rule and get_attr_icflag(p)==PACKED then -- do nothing
241       elseif not (pid==id_ins   or pid==id_mark
242                   or pid==id_adjust or pid==id_whatsit
243                   or pid==id_penalty) then
244          found_visible_node = true
245          find_first_char = false; last_char = nil
246       end
247       p = node_next(p)
248    end
249    return found_visible_node
250 end
251
252 function check_box_high(Nx, box_ptr, box_end)
253    first_char = nil;  last_char = nil;  find_first_char = true
254    if check_box(box_ptr, box_end) then
255       local first_char = first_char
256       if first_char then
257          if getid(first_char)==id_glyph then
258             if getfont(first_char) == (has_attr(first_char, attr_curjfnt) or -1) then
259                set_np_xspc_jachar(Nx, first_char)
260             else
261                set_np_xspc_alchar(Nx, getchar(first_char),first_char, 1)
262             end
263          else -- math_node
264             set_np_xspc_alchar(Nx, -1,first_char)
265          end
266       end
267    end
268    return last_char
269 end
270 end
271 -------------------- Np の計算と情報取得
272
273 luatexbase.create_callback("luatexja.jfmglue.whatsit_getinfo", "data",
274                            function (Np, lp, Nq)
275                               if Np.nuc then return Np
276                               else
277                                  return Np  -- your code
278                               end
279                            end)
280 luatexbase.create_callback("luatexja.jfmglue.whatsit_after", "data",
281                            function (stat, Nq, Np) return false end)
282
283 -- calc next Np
284 do
285
286 local function set_attr_icflag_processed(p)
287    if get_attr_icflag(p)<= ITALIC then
288       set_attr(p, attr_icflag, PROCESSED)
289    end
290 end
291
292 local function check_next_ickern(lp)
293    if lp and getid(lp) == id_kern and ITALIC == get_attr_icflag(lp) then
294       set_attr(lp, attr_icflag, IC_PROCESSED)
295       Np.last = lp; return node_next(lp)
296    else
297       Np.last = Np.nuc; return lp
298    end
299 end
300
301 local function calc_np_pbox(lp, last)
302    Np.first = Np.first or lp; Np.id = id_pbox
303    local lpa, nc = KINSOKU, nil
304    set_attr(lp, attr_icflag, get_attr_icflag(lp));
305    while lp ~=last and (lpa>=PACKED) and (lpa<BOXBDD) do
306       nc, lp = lp, node_next(lp); lpa = lp and has_attr(lp, attr_icflag) or 0
307      -- get_attr_icflag() ではいけない!
308    end
309    Np.nuc = nc
310    return check_next_ickern(lp)
311 end
312
313
314 local calc_np_auxtable = {
315    [id_glyph] = function (lp)
316       Np.first, Np.nuc = (Np.first or lp), lp;
317       Np.id = (getfont(lp) == (has_attr(lp, attr_curjfnt) or -1))
318          and id_jglyph or id_glyph
319       return true, check_next_ickern(node_next(lp));
320    end,
321    [id_hlist] = function(lp)
322       local op, flag
323       head, lp, op, flag = ltjd_make_dir_node(head, lp, list_dir, 'jfm hlist')
324       set_attr(op, attr_icflag, PROCESSED)
325       Np.first = Np.first or op; Np.last = op; Np.nuc = op;
326       Np.id = (flag or getfield(op, 'shift')~=0) and id_box_like or id_hlist
327       return true, lp
328    end,
329    [id_vlist] =  function(lp)
330       local op
331       head, lp, op = ltjd_make_dir_node(head, lp, list_dir, 'jfm:' .. getid(lp))
332       Np.first = Np.first or op; Np.last = op; Np.nuc = op;
333       Np.id = id_box_like;
334       return true, lp
335    end,
336    box_like = function(lp)
337       Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp;
338       Np.id = id_box_like;
339       return true, node_next(lp)
340    end,
341    skip = function(lp)
342       set_attr(lp, attr_icflag, PROCESSED)
343       return false, node_next(lp)
344    end,
345    [id_whatsit] = function(lp)
346       local lps = getsubtype(lp)
347       if lps==sid_user then
348          if getfield(lp, 'user_id')==luatexja.userid_table.IHB then
349             local lq = node_next(lp);
350             head = node_remove(head, lp); node_free(lp); ihb_flag = true
351             return false, lq;
352          else
353             set_attr(lp, attr_icflag, PROCESSED)
354             luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo",
355                                      Np, lp, Nq)
356             if Np.nuc then
357                Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc;
358                return true, node_next(lp)
359             else
360                return false, node_next(lp)
361             end
362          end
363       else
364          -- we do special treatment for these whatsit nodes.
365          if lps == sid_start_link or lps == sid_start_thread then
366             Np.first = lp
367          elseif lps == sid_end_link or lps == sid_end_thread then
368             Np.first, Nq.last = nil, lp;
369          end
370          set_attr(lp, attr_icflag, PROCESSED)
371          return false, node_next(lp)
372       end
373    end,
374    [id_math] = function(lp)
375       Np.first, Np.nuc = (Np.first or lp), lp;
376       set_attr(lp, attr_icflag, PROCESSED)
377       lp  = node_end_of_math(lp)
378       set_attr(lp, attr_icflag, PROCESSED)
379       Np.last, Np.id = lp, id_math;
380       return true, node_next(lp);
381    end,
382    discglue = function(lp)
383       Np.first, Np.nuc, Np.last = (Np.first or lp), lp, lp;
384       Np.id = getid(lp); set_attr(lp, attr_icflag, PROCESSED)
385       return true, node_next(lp)
386    end,
387    [id_kern] = function(lp)
388       Np.first = Np.first or lp
389       if getsubtype(lp)==2 then
390          set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
391          set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
392          set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
393          set_attr(lp, attr_icflag, PROCESSED); Np.nuc = lp
394          Np.id = (getfont(lp) == (has_attr(lp, attr_curjfnt) or -1)) and id_jglyph or id_glyph
395          return true, check_next_ickern(node_next(lp));
396       else
397          Np.id = id_kern; set_attr(lp, attr_icflag, PROCESSED)
398          Np.last = lp; return true, node_next(lp)
399       end
400    end,
401    [id_penalty] = function(lp)
402       Bp[#Bp+1] = lp; set_attr(lp, attr_icflag, PROCESSED)
403       return false, node_next(lp)
404    end,
405 }
406 calc_np_auxtable[id_rule]   = calc_np_auxtable.box_like
407 calc_np_auxtable[13]        = calc_np_auxtable.box_like
408 calc_np_auxtable[id_ins]    = calc_np_auxtable.skip
409 calc_np_auxtable[id_mark]   = calc_np_auxtable.skip
410 calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
411 calc_np_auxtable[id_disc]   = calc_np_auxtable.discglue
412 calc_np_auxtable[id_glue]   = calc_np_auxtable.discglue
413
414 function calc_np(lp, last)
415    local k
416    -- We assume lp = node_next(Np.last)
417    Np, Nq, ihb_flag = Nq, Np, nil
418    -- We clear `predefined' entries of Np before pairs() loop,
419    -- because using only pairs() loop is slower.
420    Np.post, Np.pre, Np.xspc = nil, nil, nil
421    Np.first, Np.id, Np.last, Np.met, Np.class= nil, nil, nil, nil
422    Np.auto_kspc, Np.auto_xspc, Np.char, Np.nuc = nil, nil, nil, nil
423    for k in pairs(Np) do Np[k] = nil end
424
425    for k = 1,#Bp do Bp[k] = nil end
426    while lp ~= last  do
427       local lpa = has_attr(lp, attr_icflag) or 0
428        -- unbox 由来ノードの検出
429       if lpa>=PACKED then
430          if lpa%PROCESSED_BEGIN_FLAG == BOXBDD then
431             local lq = node_next(lp)
432             head = node_remove(head, lp); node_free(lp); lp = lq
433          else return calc_np_pbox(lp, last)
434          end -- id_pbox
435       else
436          k, lp = calc_np_auxtable[getid(lp)](lp)
437          if k then return lp end
438       end
439    end
440    Np = nil; return lp
441 end
442
443 end
444 local calc_np = calc_np
445
446 -- extract informations from Np
447 -- We think that "Np is a Japanese character" if Np.met~=nil,
448 --            "Np is an alphabetic character" if Np.pre~=nil,
449 --            "Np is not a character" otherwise.
450 after_hlist = nil -- global
451 local after_alchar, extract_np
452 do
453   local PRE  = luatexja.stack_table_index.PRE
454   local POST = luatexja.stack_table_index.POST
455   local KCAT = luatexja.stack_table_index.KCAT
456   local XSP  = luatexja.stack_table_index.XSP
457
458 -- 和文文字のデータを取得
459    local attr_jchar_class = luatexbase.attributes['ltj@charclass']
460    local attr_orig_char = luatexbase.attributes['ltj@origchar']
461    local attr_autospc = luatexbase.attributes['ltj@autospc']
462    local attr_autoxspc = luatexbase.attributes['ltj@autoxspc']
463    function set_np_xspc_jachar(Nx, x)
464       local m = ltjf_font_metric_table[getfont(x)]
465       local cls, c
466       if list_dir == dir_tate then
467          local c1, c2 = getchar(x), has_attr(x, attr_orig_char)
468          c = has_attr(x, attr_dir) or c1 or c2
469          cls = ltjf_find_char_class(c, m)
470          if cls==0 then cls = slow_find_char_class(c2, m, c1) end
471       else
472          cls, c = slow_find_char_class(has_attr(x, attr_orig_char), m, getchar(x))
473       end
474       Nx.met, Nx.char = m, c; Nx.class = cls;
475       if cls~=0 then set_attr(x, attr_jchar_class, cls) end
476       Nx.pre  = table_current_stack[PRE + c]  or 0
477       Nx.post = table_current_stack[POST + c] or 0
478       Nx.xspc = table_current_stack[XSP  + c] or 3
479       Nx.kcat = table_current_stack[KCAT + c] or 0
480       Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
481    end
482    local set_np_xspc_jachar = set_np_xspc_jachar
483
484 -- 欧文文字のデータを取得
485    local floor = math.floor
486    function set_np_xspc_alchar(Nx, c,x, lig)
487       if c~=-1 then
488          local f = (lig ==1) and nullfunc or node_tail
489          local xc, xs = getfield(x, 'components'), getsubtype(x)
490          while xc and xs and xs%4>=2 do
491             x = f(xc); xc, xs = getfield(x, 'components'), getsubtype(x)
492          end
493          c = getchar(x)
494          Nx.pre  = table_current_stack[PRE + c]  or 0
495          Nx.post = table_current_stack[POST + c] or 0
496          Nx.xspc = table_current_stack[XSP  + c] or 3
497          Nx.char = 'jcharbdd'
498       else
499          Nx.pre, Nx.post, Nx.char = 0, 0, -1
500          Nx.xspc = table_current_stack[XSP - 1] or 3
501       end
502       Nx.met = nil
503       Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
504    end
505    local set_np_xspc_alchar = set_np_xspc_alchar
506
507 -- Np の情報取得メインルーチン
508    extract_np = function ()
509       local x, i = Np.nuc, Np.id;
510       if i ==  id_jglyph then return set_np_xspc_jachar(Np, x)
511       elseif i == id_glyph then return set_np_xspc_alchar(Np, getchar(x), x, 1)
512       elseif i == id_hlist then Np.last_char = check_box_high(Np, getlist(x), nil)
513       elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
514       elseif i == id_disc then Np.last_char = check_box_high(Np, getfield(x, 'replace'), nil)
515       elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
516       end
517    end
518
519    -- change the information for the next loop
520    -- (will be done if Nx is an alphabetic character or a hlist)
521    after_hlist = function (Nx)
522       local s = Nx.last_char
523       if s then
524          if getid(s)==id_glyph then
525             if getfont(s) == (has_attr(s, attr_curjfnt) or -1) then
526                set_np_xspc_jachar(Nx, s)
527             else
528                set_np_xspc_alchar(Nx, getchar(s), s, 2)
529             end
530          else
531             set_np_xspc_alchar(Nx, -1, s)
532          end
533       else
534          Nx.pre, Nx.met = nil, nil
535       end
536    end
537
538    after_alchar = function (Nx)
539       local x = Nx.nuc
540       return set_np_xspc_alchar(Nx, getchar(x), x, 2)
541    end
542
543 end
544
545 -------------------- 最下層の処理
546
547 -- change penalties (or create a new penalty, if needed)
548 local function handle_penalty_normal(post, pre, g)
549    local a = (pre or 0) + (post or 0)
550    if #Bp == 0 then
551       if (a~=0 and not(g and getid(g)==id_kern)) then
552          local p = node_new(id_penalty)
553          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
554          setfield(p, 'penalty', a)
555          head = insert_before(head, Np.first, p)
556          Bp[1]=p;
557          set_attr(p, attr_icflag, KINSOKU)
558       end
559    else for _, v in pairs(Bp) do add_penalty(v,a) end
560    end
561 end
562
563 local function handle_penalty_always(post, pre, g)
564    local a = (pre or 0) + (post or 0)
565    if #Bp == 0 then
566       if not (g and getid(g)==id_glue) then
567          local p = node_new(id_penalty)
568          if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
569          setfield(p, 'penalty', a)
570          head = insert_before(head, Np.first, p)
571          Bp[1]=p
572          set_attr(p, attr_icflag, KINSOKU)
573       end
574    else for _, v in pairs(Bp) do add_penalty(v,a) end
575    end
576 end
577
578 local function handle_penalty_suppress(post, pre, g)
579    local a = (pre or 0) + (post or 0)
580    if #Bp == 0 then
581       if g and getid(g)==id_glue then
582          local p = node_new(id_penalty)
583          setfield(p, 'penalty', 10000); head = insert_before(head, Np.first, p)
584          Bp[1]=p
585          set_attr(p, attr_icflag, KINSOKU)
586       end
587    else for _, v in pairs(Bp) do add_penalty(v,a) end
588    end
589 end
590
591 -- 和文文字間の JFM glue を node 化
592 local function new_jfm_glue(m, bc, ac)
593 -- bc, ac: char classes
594    local g, d = m.char_type[bc][ac], 0
595    local n
596    if g then
597       n,d = node_copy(g[2]), g[3]
598       if g[1] then
599          local f = node_new(id_glue)
600          set_attr(f, attr_icflag, g[4])
601          setfield(f, 'spec', n)
602          return f, d
603       end
604    end
605    return n, d
606 end
607
608 -- Nq.last (kern w) .... (glue/kern g) Np.first
609 local function real_insert(g)
610    if g then
611       head  = insert_before(head, Np.first, g)
612       Np.first = g
613    end
614 end
615
616
617 -------------------- 和文文字間空白量の決定
618
619 -- get kanjiskip
620 local get_kanjiskip
621 local get_kanjiskip_normal, get_kanjiskip_jfm
622 do
623    local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
624    local KANJI_SKIP_JFM   = luatexja.icflag_table.KANJI_SKIP_JFM
625    get_kanjiskip_normal = function ()
626       if Np.auto_kspc or Nq.auto_kspc then
627          return node_copy(kanji_skip)
628       else
629          local g = node_copy(zero_glue)
630          set_attr(g, attr_icflag, KANJI_SKIP)
631          return g
632       end
633    end
634
635    get_kanjiskip_jfm = function ()
636       local g
637       if Np.auto_kspc or Nq.auto_kspc then
638          g = node_new(id_glue); --copy_attr(g, Nq.nuc)
639          local gx = node_new(id_glue_spec);
640          setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
641          local pm, qm = Np.met, Nq.met
642          local bk = qm.kanjiskip or {0, 0, 0}
643          if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
644             setfield(gx, 'width', bk[1])
645             setfield(gx, 'stretch', bk[2])
646             setfield(gx, 'shrink', bk[3])
647          else
648             local ak = pm.kanjiskip or {0, 0, 0}
649             setfield(gx, 'width', round(diffmet_rule(bk[1], ak[1])))
650             setfield(gx, 'stretch', round(diffmet_rule(bk[2], ak[2])))
651             setfield(gx, 'shrink', -round(diffmet_rule(-bk[3], -ak[3])))
652          end
653          setfield(g, 'spec', gx)
654       else
655          g =  node_copy(zero_glue)
656       end
657       set_attr(g, attr_icflag, KANJI_SKIP_JFM)
658       return g
659    end
660 end
661
662 local calc_ja_ja_aux
663 do
664    local bg_ag = 2*id_glue - id_glue
665    local bg_ak = 2*id_glue - id_kern
666    local bk_ag = 2*id_kern - id_glue
667    local bk_ak = 2*id_kern - id_kern
668
669    calc_ja_ja_aux = function (gb,ga, db, da)
670       local rbb, rab = (1-db)/2, (1-da)/2 -- 「前の文字」由来のグルーの割合
671       local rba, raa = (1+db)/2, (1+da)/2 -- 「前の文字」由来のグルーの割合
672       if diffmet_rule ~= math.two_pleft and diffmet_rule ~= math.two_pright
673           and diffmet_rule ~= math.two_paverage then
674          rbb, rab, rba, raa = 1,0,0,1
675       end
676       if not gb then
677          if ga then
678             gb = node_new(id_kern); setfield(gb, 'kern', 0)
679          else return nil end
680       elseif not ga then
681          ga = node_new(id_kern); setfield(ga, 'kern', 0)
682       end
683
684       local k = 2*getid(gb) - getid(ga)
685       if k == bg_ag then
686          local bs, as = getfield(gb, 'spec'), getfield(ga, 'spec')
687          -- 両方とも glue.
688          local bd, ad = getfield(bs, 'width'), getfield(as, 'width')
689          setfield(bs, 'width', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
690          bd, ad = getfield(bs, 'stretch'), getfield(as, 'stretch')
691          setfield(bs, 'stretch', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
692          bd, ad = getfield(bs, 'shrink'), getfield(as, 'shrink')
693          setfield(bs, 'shrink', -round(diffmet_rule(-rbb*bd - rba*ad, -rab*bd - raa*ad)))
694          node_free(ga)
695          return gb
696       elseif k == bk_ak then
697          -- 両方とも kern.
698          local bd, ad = getfield(gb, 'kern'), getfield(ga, 'kern')
699          setfield(gb, 'kern', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
700          node_free(ga)
701          return gb
702       elseif k == bk_ag then
703          local as = getfield(ga, 'spec')
704          -- gb: kern, ga: glue
705          local bd, ad = getfield(gb, 'kern'), getfield(as, 'width')
706          setfield(as, 'width', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
707          ad = getfield(as, 'stretch')
708          setfield(bs, 'stretch', round(diffmet_rule(rba*ad, raa*ad)))
709          ad = getfield(as, 'shrink')
710          setfield(bs, 'shrink', -round(diffmet_rule(-rba*ad, -raa*ad)))
711          node_free(gb)
712          return ga
713       else
714          local bs = getfield(gb, 'spec')
715          -- gb: glue, ga: kern
716          local bd, ad = getfield(bs, 'width'), getfield(ga, 'kern')
717          setfield(bs, 'width', round(diffmet_rule(rbb*bd + rba*ad, rab*bd + raa*ad)))
718          bd = getfield(bs, 'stretch')
719          setfield(bs, 'stretch', round(diffmet_rule(rbb*bd, rab*bd)))
720          bd = getfield(bs, 'shrink')
721          setfield(bs, 'shrink', -round(diffmet_rule(-rbb*bd, -rab*bd)))
722          node_free(ga)
723          return gb
724       end
725    end
726 end
727
728 local function calc_ja_ja_glue()
729    if  ihb_flag then return nil
730    else
731       local qm, pm = Nq.met, Np.met
732       if (qm.char_type==pm.char_type) and (qm.var==pm.var) then
733          return new_jfm_glue(qm, Nq.class, Np.class)
734       else
735          local npn, nqn = Np.nuc, Nq.nuc
736          local gb, db = new_jfm_glue(qm, Nq.class,
737                                      slow_find_char_class(has_attr(npn, attr_orig_char),
738                                                           qm, getchar(npn)))
739          local ga, da = new_jfm_glue(pm,
740                                      slow_find_char_class(has_attr(nqn, attr_orig_char),
741                                                           pm, getchar(nqn)),
742                                Np.class)
743          return calc_ja_ja_aux(gb, ga, db, da);
744       end
745    end
746 end
747
748 -------------------- 和欧文間空白量の決定
749
750 -- get xkanjiskip
751 local get_xkanjiskip
752 local get_xkanjiskip_normal, get_xkanjiskip_jfm
753 do
754    local XKANJI_SKIP   = luatexja.icflag_table.XKANJI_SKIP
755    local XKANJI_SKIP_JFM   = luatexja.icflag_table.XKANJI_SKIP_JFM
756    get_xkanjiskip_normal = function (Nn)
757       if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
758          local f = node_copy(xkanji_skip)
759          return f
760       else
761          local g = node_copy(zero_glue)
762          set_attr(g, attr_icflag, XKANJI_SKIP)
763          return g
764       end
765    end
766    get_xkanjiskip_jfm = function (Nn)
767       local g
768       if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
769          g = node_new(id_glue)
770          local gx = node_new(id_glue_spec);
771          setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
772          local bk = Nn.met.xkanjiskip or {0, 0, 0}
773          setfield(gx, 'width', bk[1])
774          setfield(gx, 'stretch', bk[2])
775          setfield(gx, 'shrink', bk[3])
776          setfield(g, 'spec', gx)
777       else
778          g = node_copy(zero_glue)
779       end
780       set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
781       return g
782    end
783 end
784
785 -------------------- 隣接した「塊」間の処理
786
787 local function get_OA_skip()
788    if not ihb_flag then
789       local pm = Np.met
790       return new_jfm_glue(pm,
791         fast_find_char_class(((Nq.id == id_math and -1) or (type(Nq.char)=='string' and Nq.char or 'jcharbdd')), pm), Np.class)
792    else return nil
793    end
794 end
795 local function get_OB_skip()
796    if not ihb_flag then
797       local qm = Nq.met
798       return new_jfm_glue(qm, Nq.class,
799         fast_find_char_class(((Np.id == id_math and -1) or'jcharbdd'), qm))
800    else return nil
801    end
802 end
803
804 -- (anything) .. jachar
805 local function handle_np_jachar(mode)
806    local qid = Nq.id
807    if qid==id_jglyph or ((qid==id_pbox or qid==id_pbox_w) and Nq.met) then
808       local g = calc_ja_ja_glue() or get_kanjiskip() -- M->K
809       handle_penalty_normal(Nq.post, Np.pre, g); real_insert(g)
810    elseif Nq.met then  -- qid==id_hlist
811       local g = get_OA_skip() or get_kanjiskip() -- O_A->K
812       handle_penalty_normal(0, Np.pre, g); real_insert(g)
813    elseif Nq.pre then
814       local g = get_OA_skip() or get_xkanjiskip(Np) -- O_A->X
815       handle_penalty_normal((qid==id_hlist and 0 or Nq.post), Np.pre, g); real_insert(g)
816    else
817       local g = get_OA_skip() -- O_A
818       if qid==id_glue then handle_penalty_normal(0, Np.pre, g)
819       elseif qid==id_kern then handle_penalty_suppress(0, Np.pre, g)
820       else handle_penalty_always(0, Np.pre, g)
821       end
822       real_insert(g)
823    end
824    if mode and Np.kcat%2~=1 then
825       widow_Np.first, widow_Bp, Bp = Np.first, Bp, widow_Bp
826    end
827 end
828
829
830 -- jachar .. (anything)
831 local function handle_nq_jachar()
832     if Np.pre then
833       local g = get_OB_skip() or get_xkanjiskip(Nq) -- O_B->X
834       handle_penalty_normal(Nq.post, (Np.id==id_hlist and 0 or Np.pre), g); real_insert(g)
835    else
836       local g = get_OB_skip() -- O_B
837       if Np.id==id_glue then handle_penalty_normal(Nq.post, 0, g)
838       elseif Np.id==id_kern then handle_penalty_suppress(Nq.post, 0, g)
839       else handle_penalty_always(Nq.post, 0, g)
840       end
841       real_insert(g)
842    end
843 end
844
845 -- (anything) .. (和文文字で始まる hlist)
846 local function handle_np_ja_hlist()
847    local qid = Nq.id
848    if qid==id_jglyph or ((qid==id_pbox or Nq.id == id_pbox_w) and Nq.met) then
849       local g = get_OB_skip() or get_kanjiskip() -- O_B->K
850       handle_penalty_normal(Nq.post, 0, g); real_insert(g)
851    elseif Nq.met then  -- Nq.id==id_hlist
852       local g = get_kanjiskip() -- K
853       handle_penalty_suppress(0, 0, g); real_insert(g)
854    elseif Nq.pre then
855       local g = get_xkanjiskip(Np) -- X
856       handle_penalty_suppress(0, 0, g); real_insert(g)
857    end
858 end
859
860 -- (和文文字で終わる hlist) .. (anything)
861 local function handle_nq_ja_hlist()
862    if Np.pre then
863       local g = get_xkanjiskip(Nq) -- X
864       handle_penalty_suppress(0, 0, g); real_insert(g)
865    end
866 end
867
868
869 -- Nq が前側のクラスタとなることによる修正
870 do
871    local adjust_nq_aux = {
872       [id_glyph] = function()
873                       local x = Nq.nuc
874                       return set_np_xspc_alchar(Nq, getchar(x),x, 2)
875                    end, -- after_alchar(Nq)
876       [id_hlist]  = function() after_hlist(Nq) end,
877       [id_pbox]  = function() after_hlist(Nq) end,
878       [id_disc]  = function() after_hlist(Nq) end,
879       [id_pbox_w]  = function()
880                         luatexbase.call_callback("luatexja.jfmglue.whatsit_after",
881                                                  false, Nq, Np)
882                      end,
883    }
884
885    function adjust_nq()
886       local x = adjust_nq_aux[Nq.id]
887       if x then x()  end
888    end
889 end
890
891
892 -------------------- 開始・終了時の処理
893 do
894
895 -- リスト末尾の処理
896 local JWP  = luatexja.stack_table_index.JWP
897 local function handle_list_tail(mode)
898    adjust_nq(); Np = Nq
899    if mode then
900       -- the current list is to be line-breaked.
901       -- Insert \jcharwidowpenalty
902       Bp = widow_Bp; Np = widow_Np
903       if Np.first then
904          handle_penalty_normal(0,
905                                table_current_stack[JWP] or 0)
906       end
907    else
908       -- the current list is the contents of a hbox
909       local npi, pm = Np.id, Np.met
910       if npi == id_jglyph or (npi==id_pbox and pm) then
911          local g = new_jfm_glue(pm, Np.class, fast_find_char_class('boxbdd', pm))
912          if g then
913             set_attr(g, attr_icflag, BOXBDD)
914             head = insert_after(head, Np.last, g)
915          end
916       end
917    end
918 end
919
920 -- リスト先頭の処理
921 local function handle_list_head(par_indented)
922    local npi, pm = Np.id, Np.met
923    if npi ==  id_jglyph or (npi==id_pbox and pm) then
924       if not ihb_flag then
925          local g = new_jfm_glue(pm, fast_find_char_class(par_indented, pm), Np.class)
926          if g then
927             set_attr(g, attr_icflag, BOXBDD)
928             if getid(g)==id_glue and #Bp==0 then
929                local h = node_new(id_penalty)
930                setfield(h, 'penalty', 10000); set_attr(h, attr_icflag, BOXBDD)
931             end
932             head = insert_before(head, Np.first, g)
933          end
934       end
935    end
936 end
937
938 -- initialize
939 -- return value: (the initial cursor lp), (last node)
940 local init_var
941 do
942    local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
943    local XKANJI_SKIP   = luatexja.icflag_table.XKANJI_SKIP
944    local KSK  = luatexja.stack_table_index.KSK
945    local XSK  = luatexja.stack_table_index.XSK
946    local DIR  = luatexja.stack_table_index.DIR
947    init_var = function (mode)
948       -- 1073741823: max_dimen
949       Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
950       table_current_stack = ltjs.table_current_stack
951
952       list_dir = ltjs.list_dir or dir_yoko
953       kanji_skip = node_new(id_glue)
954       setfield(kanji_skip, 'spec', skip_table_to_spec(KSK))
955       set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
956       get_kanjiskip = (getfield(getfield(kanji_skip, 'spec'), 'width') == 1073741823)
957          and get_kanjiskip_jfm or get_kanjiskip_normal
958
959       xkanji_skip = node_new(id_glue)
960       setfield(xkanji_skip, 'spec', skip_table_to_spec(XSK))
961       set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
962       get_xkanjiskip = (getfield(getfield(xkanji_skip, 'spec'), 'width') == 1073741823)
963          and get_xkanjiskip_jfm or get_xkanjiskip_normal
964
965       Np = {
966          auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
967          first=nil, id=nil, last=nil, met=nil, nuc=nil,
968          post=nil, pre=nil, xspc=nil,
969       }
970       Nq = {
971          auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
972          first=nil, id=nil, last=nil, met=nil, nuc=nil,
973          post=nil, pre=nil, xspc=nil,
974       }
975       if mode then
976          -- the current list is to be line-breaked:
977          -- hbox from \parindent is skipped.
978          local lp, par_indented, lpi, lps  = head, 'boxbdd', getid(head), getsubtype(head)
979          while lp and ((lpi==id_whatsit and lps~=sid_user)
980                        or ((lpi==id_hlist) and (lps==3))) do
981             if (lpi==id_hlist) and (lps==3) then
982                Np.char, par_indented = 'parbdd', 'parbdd'
983                Np.width = getfield(lp, 'width')
984             end
985             lp=node_next(lp); lpi, lps = getid(lp), getsubtype(lp) end
986          return lp, node_tail(head), par_indented
987       else
988          return head, nil, 'boxbdd'
989       end
990    end
991 end
992
993 local function cleanup(mode)
994    -- adjust attr_icflag for avoiding error
995    tex.setattribute('global', attr_icflag, 0)
996    node_free(kanji_skip); node_free(xkanji_skip)
997    if mode then
998       local h = node_next(head)
999       if getid(h) == id_penalty and getfield(h, 'penalty') == 10000 then
1000          h = node_next(h)
1001          if getid(h) == id_glue and getsubtype(h) == 15 and not node_next(h) then
1002             return false
1003          end
1004       end
1005       return head
1006    else
1007       set_attr(head, attr_icflag,
1008                get_attr_icflag(head) + PROCESSED_BEGIN_FLAG);
1009       return head
1010    end
1011 end
1012 -------------------- 外部から呼ばれる関数
1013
1014 -- main interface
1015 function main(ahead, mode)
1016    if not ahead then return ahead end
1017    head = ahead;
1018    local lp, last, par_indented = init_var(mode)
1019    lp = calc_np(lp, last)
1020    if Np then
1021       extract_np(); handle_list_head(par_indented)
1022    else
1023       return cleanup(mode)
1024    end
1025    lp = calc_np(lp, last)
1026    while Np do
1027       extract_np();
1028       adjust_nq();
1029       local pid, pm = Np.id, Np.met
1030       -- 挿入部
1031       if pid == id_jglyph then
1032          handle_np_jachar(mode)
1033       elseif pm then
1034          if pid==id_hlist then handle_np_ja_hlist()
1035          else handle_np_jachar() end
1036       elseif Nq.met then
1037          if Nq.id==id_hlist then handle_nq_ja_hlist()
1038          else handle_nq_jachar() end
1039       end
1040       lp = calc_np(lp, last)
1041    end
1042    handle_list_tail(mode)
1043    return cleanup(mode)
1044 end
1045 end
1046
1047 do
1048    local IHB  = luatexja.userid_table.IHB
1049    local BPAR = luatexja.userid_table.BPAR
1050    local node_prev = (Dnode ~= node) and Dnode.getprev or node.prev
1051    local node_write = Dnode.write
1052
1053    -- \inhibitglue
1054    function create_inhibitglue_node()
1055       local tn = node_new(id_whatsit, sid_user)
1056       setfield(tn, 'user_id', IHB)
1057       setfield(tn, 'type', 100)
1058       setfield(tn, 'value', 1)
1059       node_write(tn)
1060    end
1061
1062    -- Node for indicating beginning of a paragraph
1063    -- (for ltjsclasses)
1064    function create_beginpar_node()
1065       local tn = node_new(id_whatsit, sid_user)
1066       setfield(tn, 'user_id', BPAR)
1067       setfield(tn, 'type', 100)
1068       setfield(tn, 'value', 1)
1069       node_write(tn)
1070    end
1071
1072    local function whatsit_callback(Np, lp, Nq)
1073       if Np and Np.nuc then return Np
1074       elseif Np and getfield(lp, 'user_id') == BPAR then
1075          Np.first = lp; Np.nuc = lp; Np.last = lp
1076          Np.char = 'parbdd'
1077          Np.met = nil
1078          Np.pre = 0; Np.post = 0
1079          Np.xspc = 0
1080          Np.auto_xspc = false
1081          return Np
1082       end
1083    end
1084
1085     local function whatsit_after_callback(s, Nq, Np)
1086        if not s and getfield(Nq.nuc, 'user_id') == BPAR then
1087          local x, y = node_prev(Nq.nuc), Nq.nuc
1088          Nq.first, Nq.nuc, Nq.last = x, x, x
1089          head = node_remove(head, y)
1090          node_free(y)
1091       end
1092       return s
1093    end
1094
1095    luatexbase.add_to_callback("luatexja.jfmglue.whatsit_getinfo", whatsit_callback,
1096                               "luatexja.beginpar.np_info", 1)
1097    luatexbase.add_to_callback("luatexja.jfmglue.whatsit_after", whatsit_after_callback,
1098                               "luatexja.beginpar.np_info_after", 1)
1099
1100 end