OSDN Git Service

sync with jsclasses/2016-12-01
[luatex-ja/luatexja.git] / src / ltj-jfmglue.lua
index 1a3a975..843c3f9 100644 (file)
@@ -122,7 +122,6 @@ end
 local function skip_table_to_glue(n)
    local g, st = node_new(id_glue), ltjs.fast_get_stack_skip(n)
    setglue(g, st.width, st.stretch, st.shrink, st.stretch_order, st.shrink_order)
-   -- luatexja.ext_show_node(to_node(g), 'sg ',print) 
    return g, (st.width==1073741823)
 end
 
@@ -302,7 +301,7 @@ end
 local ltjw_apply_ashift_math = ltjw.apply_ashift_math
 local ltjw_apply_ashift_disc = ltjw.apply_ashift_disc
 local min, max = math.min, math.max
-local function calc_np_aux_glyph_common(lp)
+local function calc_np_aux_glyph_common(lp, acc_flag)
    Np.nuc = lp
    Np.first= (Np.first or lp)
    if getfield(lp, 'lang') == lang_ja then
@@ -380,7 +379,7 @@ local function calc_np_aux_glyph_common(lp)
               node_free(r)
            elseif (ct.left_protruding or 0) == 0 then
               head = insert_before(head, npn, r)
-              Np.first = (Np.first==npn) and r or npn
+              Np.first = acc_flag and Np.first or ((Np.first==npn) and r or npn)
            elseif (ct.right_protruding or 0) == 0 then
               insert_after(head, npn, r); Np.last, lp = r, r
            else
@@ -396,7 +395,10 @@ local function calc_np_aux_glyph_common(lp)
       return true, lp
    end
 end
-local calc_np_auxtable = {
+local calc_np_auxtable
+do
+local dir_tate = luatexja.dir_table.dir_tate
+calc_np_auxtable = {
    [id_glyph] = calc_np_aux_glyph_common,
    [id_hlist] = function(lp)
       local op, flag
@@ -423,10 +425,6 @@ local calc_np_auxtable = {
       Np.id = id_box_like;
       return true, node_next(lp)
    end,
-   skip = function(lp)
-      set_attr(lp, attr_icflag, PROCESSED)
-      return false, node_next(lp)
-   end,
    [id_whatsit] = function(lp)
       local lps = getsubtype(lp)
       if lps==sid_user then
@@ -490,7 +488,7 @@ local calc_np_auxtable = {
         set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
         set_attr(lp, attr_icflag, PROCESSED); lp = node_next(lp)
         set_attr(lp, attr_icflag, PROCESSED);
-        return calc_np_aux_glyph_common(lp)
+        return calc_np_aux_glyph_common(lp, true)
       else
         Np.first = Np.first or lp
         Np.id = id_kern; set_attr(lp, attr_icflag, PROCESSED)
@@ -502,12 +500,14 @@ local calc_np_auxtable = {
       return false, node_next(lp)
    end,
 }
+end
 calc_np_auxtable[id_rule]   = calc_np_auxtable.box_like
 calc_np_auxtable[15]        = calc_np_auxtable.box_like
-calc_np_auxtable[id_ins]    = calc_np_auxtable.skip
-calc_np_auxtable[id_mark]   = calc_np_auxtable.skip
-calc_np_auxtable[id_adjust] = calc_np_auxtable.skip
-calc_np_auxtable[node.id('local_par')] = calc_np_auxtable.skip
+
+local function calc_np_aux_skip (lp)
+   set_attr(lp, attr_icflag, PROCESSED)
+   return false, node_next(lp)
+end
 
 function calc_np(last, lp)
    local k
@@ -532,7 +532,7 @@ function calc_np(last, lp)
            return calc_np_pbox(lp, last)
          end -- id_pbox
       else
-        k, lp = calc_np_auxtable[getid(lp)](lp)
+        k, lp = (calc_np_auxtable[getid(lp)] or calc_np_aux_skip)(lp)
         if k then return lp end
       end
    end
@@ -637,8 +637,11 @@ end
 
 -------------------- 最下層の処理
 
+luatexbase.create_callback('luatexja.adjust_jfmglue', 'simple', nullfunc)
+
 -- change penalties (or create a new penalty, if needed)
 local function handle_penalty_normal(post, pre, g)
+   luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
    local a = (pre or 0) + (post or 0)
    if #Bp == 0 then
       if (a~=0 and not(g and getid(g)==id_kern)) then
@@ -654,6 +657,7 @@ local function handle_penalty_normal(post, pre, g)
 end
 
 local function handle_penalty_always(post, pre, g)
+   luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
    local a = (pre or 0) + (post or 0)
    if #Bp == 0 then
       if not (g and getid(g)==id_glue) or a~=0 then
@@ -669,7 +673,7 @@ local function handle_penalty_always(post, pre, g)
 end
 
 local function handle_penalty_suppress(post, pre, g)
-   local a = (pre or 0) + (post or 0)
+   luatexbase.call_callback('luatexja.adjust_jfmglue', head, Nq, Np, Bp)
    if #Bp == 0 then
       if g and getid(g)==id_glue then
         local p = node_new(id_penalty)
@@ -677,7 +681,24 @@ local function handle_penalty_suppress(post, pre, g)
         Bp[1]=p
          set_attr(p, attr_icflag, KINSOKU)
       end
-   else for _, v in pairs(Bp) do add_penalty(v,a) end
+   else 
+      local a = (pre or 0) + (post or 0)
+      for _, v in pairs(Bp) do add_penalty(v,a) end
+   end
+end
+
+local function handle_penalty_jwp()
+   local a = table_current_stack[luatexja.stack_table_index.JWP]
+   if #widow_Bp == 0 then
+      if a~=0 then
+        local p = node_new(id_penalty)
+        if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
+        setfield(p, 'penalty', a)
+        head = insert_before(head, widow_Np.first, p)
+        widow_Bp[1]=p;
+        set_attr(p, attr_icflag, KINSOKU)
+      end
+   else for _, v in pairs(widow_Bp) do add_penalty(v,a) end
    end
 end
 
@@ -725,10 +746,10 @@ do
       end
       if not gb then
         if ga then
-           gb = node_new(id_kern); setfield(gb, 'kern', 0)
+           gb = node_new(id_kern, 1); setfield(gb, 'kern', 0)
         else return nil end
       elseif not ga then
-        ga = node_new(id_kern); setfield(ga, 'kern', 0)
+        ga = node_new(id_kern, 1); setfield(ga, 'kern', 0)
       end
 
       local k = 2*getid(gb) - getid(ga)
@@ -1023,19 +1044,16 @@ end
 
 -------------------- 開始・終了時の処理
 do
-
+local node_prev = node.direct.getprev
 -- リスト末尾の処理
-local JWP  = luatexja.stack_table_index.JWP
-local function handle_list_tail(mode)
-   adjust_nq(); Np = Nq
+local function handle_list_tail(mode, last)
+   adjust_nq()
    if mode then
       -- the current list is to be line-breaked.
       -- Insert \jcharwidowpenalty
-      Bp = widow_Bp; Np = widow_Np
-      if Np.first then
-        handle_penalty_normal(0, table_current_stack[JWP] or 0)
-      end
+      if widow_Np.first then handle_penalty_jwp() end
    else
+      Np=Nq      
       -- the current list is the contents of a hbox
       local npi, pm = Np.id, Np.met
       if npi == id_jglyph or (npi==id_pbox and pm) then
@@ -1177,7 +1195,7 @@ function main(ahead, mode, dir)
         end
         lp = calc_np(last,lp)
       end
-      handle_list_tail(mode)
+      handle_list_tail(mode, last)
    end
    return cleanup(mode, TEMP)
 end