OSDN Git Service

ltj-direction.lua: Fix 'Attempt to double-free pdf_save node.'
[luatex-ja/luatexja.git] / src / ltj-direction.lua
index c23f5ab..e1a7ffd 100644 (file)
@@ -33,6 +33,7 @@ local traverse = Dnode.traverse
 local traverse_id = Dnode.traverse_id
 local start_time_measure, stop_time_measure 
    = ltjb.start_time_measure, ltjb.stop_time_measure
+local abs = math.abs
 
 local id_kern = node.id('kern')
 local id_hlist = node.id('hlist')
@@ -54,6 +55,8 @@ local DIR  = luatexja.userid_table.DIR
 local dir_tate = luatexja.dir_table.dir_tate
 local dir_yoko = luatexja.dir_table.dir_yoko
 local dir_dtou = luatexja.dir_table.dir_dtou
+local dir_utod = luatexja.dir_table.dir_utod
+local dir_math_mod    = luatexja.dir_table.dir_math_mod
 local dir_node_auto   = luatexja.dir_table.dir_node_auto
 local dir_node_manual = luatexja.dir_table.dir_node_manual
 
@@ -73,7 +76,7 @@ local function adjust_badness(hd)
    end
 end
 
-local get_dir_count
+local get_dir_count, get_adjust_dir_count
 do
    local function get_dir_count_inner(h)
       if h then
@@ -91,7 +94,6 @@ do
    function get_dir_count()
       for i=tex_nest.ptr, 1, -1 do
         local h = tex_nest[i].head.next
-        --luatexja.ext_show_node_list(h, 'GDR'..  i .. '> ', print)
         if h then
            local t = get_dir_count_inner(h)
            if t~=0 then return t end
@@ -99,25 +101,36 @@ do
       end
       return page_direction
    end
+   function get_adjust_dir_count()
+      for i=tex_nest.ptr, 1, -1 do
+         local v = tex_nest[i]
+        local h, m = v.head.next, v.mode
+        if abs(m)== ltjs.vmode and h then
+           local t = get_dir_count_inner(h)
+           if t~=0 then return t end
+        end
+      end
+      return page_direction
+   end
    luatexja.direction.get_dir_count = get_dir_count
+   luatexja.direction.get_adjust_dir_count = get_adjust_dir_count
 end
 
--- \ifydir etc.
-do
-   local cs_true, cs_false = '\\iftrue', '\\iffalse'
-   luatexja.direction.dir_conditional = function(v)
-      local d = get_dir_count()
-      tex.sprint(cat_lp, (d==v) and cs_true or cs_false )
-   end
-end
 
--- \tate, \yoko
+-- \tate, \yoko,\dtou, \utod
 do
    local node_next = node.next
    local node_set_attr = node.set_attribute
    local function set_list_direction(v, name)
       local lv, w = tex_nest.ptr, tex.lists.page_head
-      if not v then v,name  = get_dir_count(), nil end
+      if not v then 
+         v,name  = get_dir_count(), nil
+        if lv>=1 and abs(tex_nest[lv-1].mode) == ltjs.mmode and v == dir_tate then
+           v = dir_utod
+        end
+      elseif v=='adj' then
+         v,name = get_adjust_dir_count(), nil
+      end
       if tex.currentgrouptype==6 then
         ltjb.package_error(
                  'luatexja',
@@ -127,14 +140,16 @@ do
       else
         local w = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
         if w then
-           luatexja.ext_show_node_list(w, 'set_dir', print)
            if (not w.next) and 
               w.id==id_whatsit and w.subtype==sid_user and w.user_id==DIR then
               node_set_attr(w, attr_dir, v)
+               if lv==0 then page_direction = v end
+           elseif lv==0 and not page_direction then
+              page_direction = v -- for first call of \yoko (in luatexja-core.sty)
            else
               ltjb.package_error(
                  'luatexja',
-                 "Use `\\" .. name .. "' at top of list",
+                 "Use `\\" .. tostring(name) .. "' at top of list",
                  'Direction change command by LuaTeX-ja is available\n'
                    .. 'only when the current list is null.')
            end
@@ -147,7 +162,7 @@ do
            Dnode.write(w)
            if lv==0 then page_direction = v end
         end
-        tex_set_attr('global', attr_icflag, 0)
+         tex_set_attr('global', attr_icflag, 0)
       end
       tex_set_attr('global', attr_dir, 0)
    end
@@ -239,7 +254,7 @@ do
       for i=1,#wh do  
         hd = node_remove(hd, wh[i]); node_free(wh[i]); wh[i] = nil 
       end
-      if gc=='fin_row' or gc == 'preamble'  then
+      if gc=='fin_row' then -- gc == 'preamble' case is treated in dir_adjust_vpack
         if hd  then
            set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
            tex_set_attr('global', attr_icflag, 0)
@@ -263,14 +278,17 @@ end
 -- dir_node に包む方法を書いたテーブル
 local dir_node_aux
 do
+   local floor = math.floor
    local get_h =function (w,h,d) return h end
    local get_d =function (w,h,d) return d end
    local get_h_d =function (w,h,d) return h+d end
    local get_h_d_neg =function (w,h,d) return -h-d end
    local get_h_neg =function (w,h,d) return -h end
    local get_d_neg =function (w,h,d) return -d end
-   local get_w_half =function (w,h,d) return 0.5*w end
-   local get_w_neg_half =function (w,h,d) return -0.5*w end
+   local get_w_half =function (w,h,d) return floor(0.5*w) end
+   local get_w_neg_half =function (w,h,d) return floor(-0.5*w) end
+   local get_w_half_rem =function (w,h,d) return w-floor(0.5*w) end
+   local get_w_neg_half_rem =function (w,h,d) return -w-floor(-0.5*w) end
    local get_w_neg =function (w,h,d) return -w end
    local get_w =function (w,h,d) return w end
    local zero = function() return 0 end
@@ -279,13 +297,13 @@ do
         [dir_tate] = { -- tate 中で組む
            width  = get_h_d,
            height = get_w_half,
-           depth  = get_w_half,
+           depth  = get_w_half_rem,
            [id_hlist] = {
               { 'whatsit', sid_save },
               { 'rotate', '0 1 -1 0' },
               { 'kern', get_w_neg_half },
               { 'box' , get_h },
-              { 'kern', get_w_neg_half },
+              { 'kern', get_w_neg_half_rem },
               { 'whatsit', sid_restore },
            },
            [id_vlist] = {
@@ -426,19 +444,25 @@ do
    local getbox = tex.getbox
    local function check_dir(reg_num)
       start_time_measure('box_primitive_hook')
-      local list_dir = get_dir_count()
-      local b = tex.getbox(tex_getcount('ltj@tempcnta'))
+      local list_dir = get_dir_count()%dir_math_mod
+      local b = getbox(tex_getcount('ltj@tempcnta'))
       if b then
         local box_dir = get_box_dir(to_direct(b), dir_yoko)
-        if box_dir%dir_node_auto ~= list_dir%dir_node_auto then
-           luatexja.ext_show_node_list(tex.nest[tex.nest.ptr].head, 'LIST> ', print)
-           luatexja.ext_show_node_list(b, 'BOX> ', print)
+        if box_dir%dir_math_mod ~= list_dir then
+          --  print('NEST', tex_nest.ptr, tex_getcount('ltj@tempcnta'))
+          --  luatexja.ext_show_node_list(
+           --     (tex_nest.ptr==0) and tex.lists.page_head or tex_nest[tex_nest.ptr].head,
+           --     'LIST' .. tostring(list_dir) .. '> ', print)
+          -- luatexja.ext_show_node_list(b, 'BOX' .. tostring(box_dir) .. '> ', print)
            ltjb.package_error(
               'luatexja',
               "Incompatible direction list can't be unboxed",
               'I refuse to unbox a box in differrent direction.')
         end
       end
+      if luatexja.global_temp and tex.globaldefs~=luatexja.global_temp then 
+        tex.globaldefs = luatexja.global_temp
+      end
       stop_time_measure('box_primitive_hook')
    end
    luatexja.direction.check_dir = check_dir
@@ -467,7 +491,7 @@ local function unwrap_dir_node(b, head, box_dir)
       setfield(wh, 'value', nil)
    end
    -- recalc. info
-   local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
+   local info = dir_node_aux[b_dir%dir_math_mod][box_dir%dir_math_mod][getid(bc)]
    for _,v in ipairs(info) do 
       if v[1]=='box' then
         shift_old = v[2](
@@ -481,8 +505,7 @@ end
 
 -- is_manual: 寸法変更に伴うものか?
 local function create_dir_node(b, b_dir, new_dir, is_manual)
-   --print('create new node', b_dir, new_dir)
-   local info = dir_node_aux[b_dir][new_dir]
+   local info = dir_node_aux[b_dir%dir_math_mod][new_dir%dir_math_mod]
    local w = getfield(b, 'width')
    local h = getfield(b, 'height')
    local d = getfield(b, 'depth')
@@ -505,6 +528,7 @@ end
 local make_dir_whatsit, process_dir_node
 do
    make_dir_whatsit = function (head, b, new_dir, origin)
+      new_dir = new_dir%dir_math_mod
       -- head: list head, b: box
       -- origin: コール元 (for debug)
       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
@@ -513,70 +537,72 @@ do
       local box_dir, dn =  get_box_dir(b, ltjs.list_dir)
       -- 既に b の中身にあるwhatsit
 
-      if box_dir==new_dir then
-        -- 組方向が一緒のボックスなので,何もしなくて良い
-        return head, node_next(b), b, false
-      elseif  box_dir%dir_node_auto == new_dir  then
-        -- dir_node としてカプセル化されている
-        local bc = node_next(node_next(node_next(bh)))
-        local _, dnc = get_box_dir(b, 0)
-        if dnc then -- free all other dir_node
-           Dnode.flush_list(getfield(dnc, 'value'))
-           setfield(dnc, 'value', nil)
+      if box_dir%dir_math_mod==new_dir then
+        if box_dir>=dir_node_auto then
+           -- dir_node としてカプセル化されている
+           local bc = node_next(node_next(node_next(bh)))
+           local _, dnc = get_box_dir(b, 0)
+           if dnc then -- free all other dir_node
+              Dnode.flush_list(getfield(dnc, 'value'))
+              setfield(dnc, 'value', nil)
+           end
+           set_attr(b, attr_dir, box_dir%dir_math_mod + dir_node_auto)
+           return head, node_next(b), b, true
+        else
+           -- 組方向が一緒 (up to math dir) のボックスなので,何もしなくて良い
+           return head, node_next(b), b, false
         end
-        set_attr(b, attr_dir, box_dir%dir_node_auto + dir_node_auto)
-        return head, node_next(b), b, true
       else
-        --luatexja.ext_show_node_list(to_node(b), 'mkd> ', print)
         -- 組方向を合わせる必要あり
          local nh, nb, ret, flag
         if box_dir>= dir_node_auto then -- unwrap
            local b_dir
             head, nb, b, b_dir = unwrap_dir_node(b, head, box_dir)
            bh = getlist(b)
-           if b_dir==new_dir then
+           if b_dir%dir_math_mod==new_dir then
               -- dir_node の中身が周囲の組方向とあっている
               return head, nb, b, false 
            else box_dir = b_dir end
         end
-            local db
-            local dnh = getfield(dn, 'value')
-            for x in traverse(dnh) do
-               if has_attr(x, attr_dir)%dir_node_auto == new_dir then
-                  setfield(dn, 'value', to_node(node_remove(dnh, x)))
-                  db=x; break
-               end
-            end
-           Dnode.flush_list(dnh)
-            db = db or create_dir_node(b, box_dir, new_dir, false)
-            local w = getfield(b, 'width')
-            local h = getfield(b, 'height')
-            local d = getfield(b, 'depth')
-            nh, nb =  insert_before(head, b, db), nil
-            nh, nb = node_remove(nh, b)
-            local db_head, db_tail  = nil
-            for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
-               local cmd, arg, nn = v[1], v[2]
-               if cmd=='kern' then
-                  nn = node_new(id_kern)
-                  setfield(nn, 'kern', arg(w, h, d))
-               elseif cmd=='whatsit' then
-                  nn = node_new(id_whatsit, arg)
-               elseif cmd=='rotate' then
-                  nn = node_new(id_whatsit, sid_matrix)
-                  setfield(nn, 'data', arg)
-               elseif cmd=='box' then
-                  nn = b; setfield(b, 'next', nil)
-                 setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
-               end
-               if db_head then
-                  insert_after(db_head, db_tail, nn)
-                  db_tail = nn
-              else
-                 db_head, db_tail = nn, nn
-              end
-              setfield(db, 'head', db_head)
-              ret, flag = db, true
+        box_dir = box_dir%dir_math_mod
+        local db
+        local dnh = getfield(dn, 'value')
+        for x in traverse(dnh) do
+           if has_attr(x, attr_dir)%dir_math_mod == new_dir then
+              setfield(dn, 'value', to_node(node_remove(dnh, x)))
+              db=x; break
+           end
+        end
+        Dnode.flush_list(getfield(dn, 'value'))
+        db = db or create_dir_node(b, box_dir, new_dir, false)
+        local w = getfield(b, 'width')
+        local h = getfield(b, 'height')
+        local d = getfield(b, 'depth')
+        nh, nb =  insert_before(head, b, db), nil
+        nh, nb = node_remove(nh, b)
+        local db_head, db_tail  = nil
+        for _,v in ipairs(dir_node_aux[box_dir][new_dir][getid(b)]) do
+           local cmd, arg, nn = v[1], v[2]
+           if cmd=='kern' then
+              nn = node_new(id_kern)
+              setfield(nn, 'kern', arg(w, h, d))
+           elseif cmd=='whatsit' then
+              nn = node_new(id_whatsit, arg)
+           elseif cmd=='rotate' then
+              nn = node_new(id_whatsit, sid_matrix)
+              setfield(nn, 'data', arg)
+           elseif cmd=='box' then
+              nn = b; setfield(b, 'next', nil)
+              setfield(nn, 'shift', getfield(nn, 'shift') + arg(w,h,d))
+           end
+           if db_head then
+              insert_after(db_head, db_tail, nn)
+              db_tail = nn
+           else
+              db_head, db_tail = nn, nn
+           end
+           setfield(db, 'head', db_head)
+           ret, flag = db, true
         end
         return nh, nb, ret, flag
       end
@@ -630,7 +656,9 @@ end
 do
    local getbox, setdimen = tex.getbox, tex.setdimen
    local function get_box_dim_common(key, s, l_dir)
+      -- s: not dir_node.
       local s_dir, wh = get_box_dir(s, dir_yoko)
+      s_dir = s_dir%dir_math_mod
       if s_dir ~= l_dir then
          local not_found = true
          for x in traverse(getfield(wh, 'value')) do
@@ -654,12 +682,12 @@ do
       local gt = tex.globaldefs; tex.globaldefs = 0
       local s = getbox(n)
       if s then
-         local l_dir = get_dir_count()
+         local l_dir = (get_dir_count())%dir_math_mod
          s = to_direct(s)
          local b_dir = get_box_dir(s,dir_yoko)
          if b_dir<dir_node_auto then
             get_box_dim_common(key, s, l_dir)
-         elseif b_dir%dir_node_auto==l_dir then
+         elseif b_dir%dir_math_mod==l_dir then
             setdimen('ltj@tempdima', getfield(s, key))
          else
             get_box_dim_common(key, 
@@ -675,6 +703,7 @@ do
    -- return value: (changed dimen of box itself?)
    local function set_box_dim_common(key, s, l_dir)
       local s_dir, wh = get_box_dir(s, dir_yoko)
+      s_dir = s_dir%dir_math_mod
       if s_dir ~= l_dir then
          if not wh then
             wh = create_dir_whatsit(getlist(s), 'set_box_dim', s_dir)
@@ -717,12 +746,12 @@ do
       local n = tex_getcount('ltj@tempcnta')
       local s = getbox(n)
       if s then
-        local l_dir = get_dir_count()
+        local l_dir = (get_dir_count())%dir_math_mod
         s = to_direct(s)
          local b_dir = get_box_dir(s,dir_yoko)
          if b_dir<dir_node_auto then
             set_box_dim_common(key, s, l_dir)
-        elseif b_dir%dir_node_auto == l_dir then
+        elseif b_dir%dir_math_mod == l_dir then
            -- s is dir_node
            setfield(s, key, tex.getdimen('ltj@tempdima'))
            if b_dir<dir_node_manual then
@@ -730,8 +759,8 @@ do
            end
          else
            local sid, sl = getid(s), getlist(s)
-           local b = node_next(node_next(node_next(sl)))
-           local info = dir_node_aux[get_box_dir(b,dir_yoko)][b_dir%dir_node_auto]
+           local b = node_next(node_next(node_next(sl))) -- 本来の box
+           local info = dir_node_aux[get_box_dir(b,dir_yoko)%dir_math_mod][b_dir%dir_node_auto]
            local shift_old
            for _,v in ipairs(info[sid]) do 
               if v[1]=='box' then
@@ -767,26 +796,49 @@ do
    luatexja.direction.set_box_dim = set_box_dim
 end
 
--- \ifydir, \iftdir, \ifddir
 do
    local getbox = tex.getbox
-   local function box_dir_conditional(n, mode)
+   local function get_register_dir(n)
       local s = getbox(n)
-      local res = false
       if s then
          s = to_direct(s)
          local b_dir = get_box_dir(s, dir_yoko)
          if b_dir<dir_node_auto then
-           res = (b_dir==mode)
+           return b_dir
          else
            local b_dir = get_box_dir(
               node_next(node_next(node_next(getlist(s)))), dir_yoko)
-           res = (b_dir==mode)
+           return b_dir
          end
+      else
+         return 0
+      end
+   end
+   luatexja.direction.get_register_dir = get_register_dir
+end
+
+do
+   local getbox, setbox, copy_list = tex.getbox, tex.setbox, Dnode.copy_list
+   -- raise, lower
+   function luatexja.direction.raise_box()
+      start_time_measure('box_primitive_hook')
+      local list_dir = get_dir_count()
+      local s = getbox('ltj@afbox')
+      if s then
+        local sd = to_direct(s)
+        local box_dir = get_box_dir(sd, dir_yoko)
+        if box_dir%dir_math_mod ~= list_dir then
+           setbox(
+              'ltj@afbox', 
+              to_node(
+                 copy_list(make_dir_whatsit(sd, sd, list_dir, 'box_move'))
+                 -- without copy_list, we get a segfault
+              )
+           )
+        end
       end
-      tex.sprint(cat_lp, '\\if' .. tostring(res))
+      stop_time_measure('box_primitive_hook')
    end
-   luatexja.direction.box_dir_conditional = box_dir_conditional
 end
 
 -- 縦書き用字形への変換テーブル
@@ -861,7 +913,7 @@ end
 local id_adjust = node.id('adjust')
 function luatexja.direction.check_adjust_direction()
    start_time_measure('box_primitive_hook')
-   local list_dir = tex_getcount('ltj@adjdir@count')
+   local list_dir = get_adjust_dir_count()
    local a = tex_nest[tex_nest.ptr].tail
    local ad = to_direct(a)
    if a and getid(ad)==id_adjust then