OSDN Git Service

Forgot to include ltj-direction.lua^^;
[luatex-ja/luatexja.git] / src / ltj-direction.lua
index 040ce92..c05bc35 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
 
@@ -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,7 +101,6 @@ do
       end
       return page_direction
    end
-   local abs = math.abs
    function get_adjust_dir_count()
       for i=tex_nest.ptr, 1, -1 do
          local v = tex_nest[i]
@@ -116,14 +117,17 @@ do
 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 
+         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
@@ -145,7 +149,7 @@ do
            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
@@ -250,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)
@@ -274,14 +278,14 @@ 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_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
@@ -290,20 +294,20 @@ 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', function(w,h,d,nw,nh,nd) return -nd end },
+              { 'box' , get_h},
+              { 'kern', function(w,h,d,nw,nh,nd) return nd-w end },
               { 'whatsit', sid_restore },
            },
            [id_vlist] = {
               { 'whatsit', sid_save },
               { 'rotate', '0 1 -1 0' },
               { 'kern' , zero },
-              { 'box' , get_w_neg },
+              { 'box' , function(w,h,d,nw,nh,nd) return -nh-nd end },
               { 'kern', get_h_d_neg},
               { 'whatsit', sid_restore },
            },
@@ -315,8 +319,9 @@ do
            [id_hlist] = {
               { 'whatsit', sid_save },
               { 'rotate', '0 -1 1 0' },
-              { 'kern', get_w_neg },
+              { 'kern', function(w,h,d,nw,nh,nd) return -nh end },
               { 'box', get_d_neg },
+              { 'kern', function(w,h,d,nw,nh,nd) return nh-w end },
               { 'whatsit', sid_restore },
            },
            [id_vlist] = {
@@ -336,8 +341,9 @@ do
            [id_hlist] = {
               { 'whatsit', sid_save },
               { 'rotate', '0 -1 1 0' },
-              { 'kern', get_w_neg },
+              { 'kern', function (w,h,d,nw,nh,nd) return -nh end },
               { 'box' , get_d_neg },
+              { 'kern', function (w,h,d,nw,nh,nd) return nh-w end },
               { 'whatsit', sid_restore },
            },
            [id_vlist] = {
@@ -356,7 +362,7 @@ do
               { 'whatsit', sid_save },
               { 'rotate', '-1 0 0 -1' },
               { 'kern', get_w_neg },
-              { 'box', zero },
+              { 'box',  function (w,h,d,nw,nh,nd) return h-nd end },
               { 'whatsit', sid_restore },
            },
            [id_vlist] = {
@@ -376,16 +382,16 @@ do
            [id_hlist] = {
               { 'whatsit', sid_save },
               { 'rotate', '0 1 -1 0' },
-              { 'kern', zero },
+              { 'kern', function (w,h,d,nw,nh,nd) return -nd end },
               { 'box', get_h },
-              { 'kern', get_w_neg },
+              { 'kern', function (w,h,d,nw,nh,nd) return nd-w end },
               { 'whatsit', sid_restore },
            },
            [id_vlist] = {
                { 'kern', zero },
               { 'whatsit', sid_save },
               { 'rotate', '0 1 -1 0' },
-              { 'box', get_w_neg },
+              { 'box', function (w,h,d,nw,nh,nd) return -nd-nh end },
               { 'kern', get_h_d_neg },
               { 'whatsit', sid_restore },
            },
@@ -398,14 +404,15 @@ do
               { 'whatsit', sid_save },
               { 'rotate', '-1 0 0 -1' },
               { 'kern', get_w_neg },
-              { 'box', zero },
+              { 'box', function (w,h,d,nw,nh,nd) return h-nd end },
               { 'whatsit', sid_restore },
            },
            [id_vlist] = {
               { 'whatsit', sid_save },
               { 'rotate', ' -1 0 0 -1' },
-              { 'kern', get_h_d_neg }, 
+              { 'kern', function (w,h,d,nw,nh,nd) return -nh-nd end }, 
               { 'box', get_w_neg },
+              { 'kern', function (w,h,d,nw,nh,nd) return nh+nd-h-d end }, 
               { 'whatsit', sid_restore },
            },
         },
@@ -437,23 +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
-            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)
+        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
-      tex.globaldefs = luatexja.global_temp
+      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
@@ -481,23 +490,13 @@ local function unwrap_dir_node(b, head, box_dir)
       Dnode.flush_list(getfield(wh, 'value'))
       setfield(wh, 'value', nil)
    end
-   -- recalc. info
-   local info = dir_node_aux[b_dir][box_dir%dir_node_auto][getid(bc)]
-   for _,v in ipairs(info) do 
-      if v[1]=='box' then
-        shift_old = v[2](
-           getfield(bc,'width'), getfield(bc, 'height'), getfield(bc, 'depth'))
-        break
-      end
-   end
-   setfield(bc, 'shift', getfield(bc, 'shift') - shift_old)
+   setfield(bc, 'shift', 0)
    return nh, nb, bc, b_dir
 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')
@@ -520,6 +519,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)
@@ -528,70 +528,75 @@ 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')
+        local dn_w = getfield(db, 'width')
+        local dn_h = getfield(db, 'height')
+        local dn_d = getfield(db, '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, dn_w, dn_h, dn_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', arg(w, h, d, dn_w, dn_h, dn_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
@@ -645,7 +650,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
@@ -669,12 +676,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, 
@@ -690,6 +697,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)
@@ -732,40 +740,49 @@ 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
               set_attr(s, attr_dir, b_dir%dir_node_auto + dir_node_manual)
            end
-         else
+           -- re-calculate shift and kern (TODO)
            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 shift_old
-           for _,v in ipairs(info[sid]) do 
-              if v[1]=='box' then
-                 shift_old = v[2](
-                    getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth'))
-                 break
+           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 bw, bh, bd 
+              = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
+           local sw, sh, sd 
+              = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
+           -- re-calculate shift and kern
+           for i,v in ipairs(info[sid]) do 
+              if getid(sl)==id_kern then
+                 setfield(sl, 'kern', v[2](bw,bh,bd,sw,sh,sd) )
+              elseif getid(sl)==sid then
+                 setfield(sl, 'shift', v[2](bw,bh,bd,sw,sh,sd) )
               end
+              sl = node_next(sl)
            end
-           if set_box_dim_common(key, b, l_dir) then
-              local bw, bh, bd 
-                 = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
-              -- re-calculate shift
+         else
+           local sid, sl = getid(s), getlist(s)
+           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 bw, bh, bd 
+              = getfield(b,'width'), getfield(b, 'height'), getfield(b, 'depth')
+           local sw, sh, sd 
+              = getfield(s,'width'), getfield(s, 'height'), getfield(s, 'depth')
+           if set_box_dim_common(key, b, l_dir) then
+              -- re-calculate shift and kern
               for i,v in ipairs(info[sid]) do 
                  if getid(sl)==id_kern then
-                    setfield(sl, 'kern', v[2](bw,bh,bd) )
+                    setfield(sl, 'kern', v[2](bw,bh,bd,sw,sh,sd) )
                  elseif getid(sl)==sid then
-                    local d = getfield(sl, 'shift')
-                    setfield(sl, 'shift', 
-                             getfield(sl, 'shift') - shift_old + v[2](bw,bh,bd) )
+                    setfield(sl, 'shift', v[2](bw,bh,bd,sw,sh,sd) )
                  end
                  sl = node_next(sl)
               end
@@ -803,6 +820,30 @@ do
    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
+      stop_time_measure('box_primitive_hook')
+   end
+end
+
 -- 縦書き用字形への変換テーブル
 local font_vert_table = {} -- key: fontnumber
 do