OSDN Git Service

luatexja-core.sty: stopped to use \ltj@dir@count in \everyhbox and \everyvbox hooks.
[luatex-ja/luatexja.git] / src / ltj-direction.lua
index 2359ae2..c23f5ab 100644 (file)
@@ -57,7 +57,7 @@ local dir_dtou = luatexja.dir_table.dir_dtou
 local dir_node_auto   = luatexja.dir_table.dir_node_auto
 local dir_node_manual = luatexja.dir_table.dir_node_manual
 
-
+local page_direction
 --
 local function adjust_badness(hd)
    if not node_next(hd) and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
@@ -74,35 +74,63 @@ local function adjust_badness(hd)
 end
 
 local get_dir_count
--- \tate, \yoko
 do
+   local function get_dir_count_inner(h)
+      if h then
+        if h.id==id_whatsit and h.subtype==sid_user and h.user_id==DIR then
+              local ic = node.has_attribute(h, attr_icflag)
+              return (ic<PROCESSED_BEGIN_FLAG) 
+                 and (node.has_attribute(h,attr_dir)%dir_node_auto) or 0
+        else
+           return 0
+        end
+      else
+        return 0
+      end
+   end
    function get_dir_count()
-      return tex_getcount('ltj@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
+        end
+      end
+      return page_direction
    end
-   luatexja.direction.get_dir_count = get_dir_count 
+   luatexja.direction.get_dir_count = get_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
+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[tex_nest.ptr], tex.lists.page_head
-      if lv.mode == 1 and w then
-        if w.id==id_whatsit and w.subtype==sid_user
-        and w.user_id==DIR then
-           node_set_attr(w, attr_dir, v)
-        end
-      elseif tex.currentgrouptype==6 then
+      local lv, w = tex_nest.ptr, tex.lists.page_head
+      if not v then v,name  = get_dir_count(), nil end
+      if tex.currentgrouptype==6 then
         ltjb.package_error(
                  'luatexja',
                  "You can't use `\\" .. name .. "' in an align",
                 "To change direction in an align, \n"
                    .. "you shold use \\hbox or \\vbox.")
       else
-        local w = node_next(lv.head)
+        local w = (lv==0) and tex.lists.page_head or tex_nest[lv].head.next
         if w then
-           w = to_direct(w)
-           if getid(w)==id_whatsit and getsubtype(w)==sid_user
-           and getfield(w, 'user_id')==DIR then
-              set_attr(w, attr_dir, v)
+           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)
            else
               ltjb.package_error(
                  'luatexja',
@@ -117,6 +145,7 @@ do
            setfield(w, 'type', 110)
            set_attr(w, attr_dir, v)
            Dnode.write(w)
+           if lv==0 then page_direction = v end
         end
         tex_set_attr('global', attr_icflag, 0)
       end
@@ -452,6 +481,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 w = getfield(b, 'width')
    local h = getfield(b, 'height')
@@ -497,6 +527,7 @@ do
         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
@@ -739,7 +770,7 @@ end
 -- \ifydir, \iftdir, \ifddir
 do
    local getbox = tex.getbox
-   local function dir_conditional(n, mode)
+   local function box_dir_conditional(n, mode)
       local s = getbox(n)
       local res = false
       if s then
@@ -755,7 +786,7 @@ do
       end
       tex.sprint(cat_lp, '\\if' .. tostring(res))
    end
-   luatexja.direction.dir_conditional = dir_conditional
+   luatexja.direction.box_dir_conditional = box_dir_conditional
 end
 
 -- 縦書き用字形への変換テーブル
@@ -911,12 +942,14 @@ do
       if info=='box' then
         local head = to_direct(tex.lists.contrib_head)
         local nb
-        head, _, nb
-           = make_dir_whatsit(head, 
-                              node_tail(head),
-                              get_dir_count(), 
-                              'buildpage')
-        tex.lists.contrib_head = to_node(head)
+        if head then
+           head, _, nb
+              = make_dir_whatsit(head, 
+                                 node_tail(head),
+                                 get_dir_count(), 
+                                 'buildpage')
+           tex.lists.contrib_head = to_node(head)
+        end
       end
    end
    luatexbase.add_to_callback('buildpage_filter',