OSDN Git Service

luatexja-adjust.sty: removed unused code
[luatex-ja/luatexja.git] / src / ltj-direction.lua
index 0bea28f..693021a 100644 (file)
@@ -4,6 +4,7 @@
 
 luatexja.load_module('base');      local ltjb = luatexja.base
 luatexja.load_module('stack');     local ltjs = luatexja.stack
+luatexja.load_module('rmlgbm');    local ltjr = luatexja.rmlgbm
 luatexja.direction = {}
 
 local attr_dir = luatexbase.attributes['ltj@dir']
@@ -41,66 +42,117 @@ local sid_user = node.subtype('user_defined')
 local PROCESSED    = luatexja.icflag_table.PROCESSED
 local PACKED       = luatexja.icflag_table.PACKED
 local DIR = luatexja.stack_table_index.DIR
+local STCK = luatexja.userid_table.STCK
 local wh_DIR = luatexja.userid_table.DIR
 local dir_tate = 3
 local dir_yoko = 4
 
+
+local get_dir_count 
+do
+   local gc = tex.getcount
+   get_dir_count = function() return gc('ltj@dir@count') end
+end
+luatexja.direction.get_dir_count = get_dir_count 
+
+-- \tate, \yoko
 do
   local node_next = node.next
   local function set_list_direction(v, name)
-    if node.next(tex.nest[tex.nest.ptr].head) then
-      ltjb.package_error('luatexja',
-                        "Use `\\" .. name .. "' at top of list", 
-                        'Direction change command by LuaTeX-ja is available\n'
-                           .. 'only while current list is null.')
-    else
-       ltjs.set_stack_table(luatexja.stack_table_index.DIR, v, true)
-    end
+     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==wh_DIR then
+           w.value=v
+        end
+     else
+        local w = node_next(to_direct(lv.head))
+        if to_node(w) then
+           if getid(w)==id_whatsit and getsubtype(w)==sid_user
+           and getfield('user_id', wh_DIR) then
+              setfield(w, 'value', v)
+           else
+              ltjb.package_error(
+                 'luatexja',
+                 "Use `\\" .. name .. "' at top of list",
+                 'Direction change command by LuaTeX-ja is available\n'
+                 .. 'only while current list is null.')
+           end
+        else
+           local w = node_new(id_whatsit, sid_user)
+           setfield(w, 'next', hd)
+           setfield(w, 'user_id', wh_DIR)
+           setfield(w, 'type', 100)
+           setfield(w, 'value', v)
+           Dnode.write(w)
+        end
+     end
   end
   luatexja.direction.set_list_direction = set_list_direction
 end
 
-do 
+-- ボックスに dir whatsit を追加
+do
    local tex_getcount = tex.getcount
-   local function set_dir_flag(h)
-      local new_dir = ltjs.table_current_stack[DIR]
-      local w = node_new(id_whatsit, sid_user)
-      setfield(w, 'user_id', wh_DIR)
-      setfield(w, 'type', 100)
-      setfield(w, 'value', new_dir)
-      setfield(w, 'next', to_direct(h))
-      return to_node(w)
+   local function set_dir_flag(h, gc)
+      if gc=='fin_row' or gc == 'preamble'  then
+        return h
+      else
+        local hd = to_direct(h)
+        local w
+        if hd and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
+        and getfield(hd, 'user_id')==wh_DIR then
+           return h
+        else
+           w = node_new(id_whatsit, sid_user)
+           setfield(w, 'next', hd)
+            setfield(w, 'user_id', wh_DIR)
+            setfield(w, 'type', 100)
+            setfield(w, 'value', ltjs.list_dir)
+            return to_node(w)
+        end
+      end
    end
    luatexbase.add_to_callback('hpack_filter', set_dir_flag, 'ltj.set_dir_flag', 10000)
-   luatexbase.add_to_callback('vpack_filter', 
-                             function (h)
-                                local box_set, cl = 0, tex.currentgrouplevel + 1
-                                for w in traverse_id(id_whatsit, to_direct(h)) do
-                                   if getfield(w, 'value')==cl then box_set = 1; break end
-                                end
-                                ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
-                                return set_dir_flag(h)
-                             end, 'ltj.set_dir_flag', 1)
-   luatexbase.add_to_callback('post_linebreak_filter', 
+
+   local function set_dir_flag_vbox(h, gc)
+      local w = to_direct(h)
+      ltjs.list_dir = get_dir_count()
+      if getid(w)==id_whatsit and getsubtype(w)==sid_user
+      and getfield(w, 'user_id')==wh_DIR then
+         ltjs.list_dir = getfield(w, 'value')
+      --print('VB', gc, ltjs.list_dir)
+      --luatexja.ext_show_node_list(h, '   > ', print)
+         return h
+      else
+      --print('VB', gc, ltjs.list_dir)
+      --luatexja.ext_show_node_list(h, '   > ', print)
+         return set_dir_flag(h, gc)
+      end
+   end
+   luatexbase.add_to_callback('vpack_filter', set_dir_flag_vbox, 'ltj.set_dir_flag', 1)
+   luatexbase.add_to_callback('post_linebreak_filter',
                              function (h)
-                                local new_dir = ltjs.table_current_stack[DIR]
+                                local new_dir = ltjs.list_dir
                                 for line in traverse_id(id_hlist, to_direct(h)) do
                                    set_attr(line, attr_dir, new_dir)
                                 end
-                                return h
+                                return set_dir_flag(h, tostring(gc))
                              end, 'ltj.set_dir_flag', 100)
 
 end
 
+
+
 local make_dir_node
 do
-   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_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_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_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_neg =function (w,h,d) return -w end
    local get_w =function (w,h,d) return w end
    local dir_node_aux = {
@@ -109,7 +161,7 @@ do
         height = get_w_half,
         depth  = get_w_half,
         [id_hlist] = {
-           { 'kern', get_h }, 
+           { 'kern', get_h },
            { 'whatsit', sid_save },
            { 'rotate', '0 1 -1 0' },
            { 'kern', get_w_neg_half },
@@ -125,19 +177,19 @@ do
            { 'kern', get_h_neg},
            { 'whatsit', sid_restore },
         },
-      }, 
+      },
       [dir_tate] = { -- tate を yoko 中で組む
         width  = get_h_d,
         height = get_w,
         depth  = function() return 0 end,
         [id_hlist] = {
-           { 'kern', get_d }, 
+           { 'kern', get_d },
            { 'whatsit', sid_save },
            { 'rotate', '0 -1 1 0' },
            { 'kern', get_w_neg },
            { 'box' },
            { 'whatsit', sid_restore },
-        }, 
+        },
         [id_vlist] = {
            { 'whatsit', sid_save },
            { 'rotate', '0 -1 1 0' },
@@ -145,40 +197,58 @@ do
            { 'kern', get_d_neg },
            { 'box' },
            { 'whatsit', sid_restore },
-        }, 
+        },
       },
    }
 
-   make_dir_node = function (head, b, new_dir)
+   clean_dir_whatsit = function(b)
+      local bh = getlist(b)
+      local dir
+      for x in traverse_id(id_whatsit, bh) do
+        if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==wh_DIR then
+            setfield(b, 'head', (node_remove(bh, x)))
+            dir = getfield(bh, 'value')
+            set_attr(b, attr_dir, dir)
+            set_attr(b, attr_icflag, PROCESSED)
+            node_free(x); break
+        end
+      end
+      return dir
+   end
+   luatexja.direction.clean_dir_whatsit = clean_dir_whatsit
+   make_dir_node = function (head, b, new_dir, origin)
       -- head: list head, b: box
+      -- origin: コール元 (for debug)
       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
       -- (new b): b か dir_node に被せられた b
       local old_dir
       local bh = getlist(b)
-      if bh and getid(bh)==id_whatsit and getsubtype(bh)==sid_user
-          and getfield(bh, 'user_id')==wh_DIR then
+      for x in traverse_id(id_whatsit, bh) do
+        if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==wh_DIR then
             old_dir = getfield(bh, 'value')
-            setfield(b, 'head', node_next(bh))
-           set_attr(b, attr_icflag, PROCESSED)
-            node_free(bh)
-            print('FROM WHATSit')
-      else
-        old_dir = has_attr(b, attr_dir)
-        if old_dir==0 then old_dir =4 end
+            set_attr(b, attr_dir, old_dir)
+            setfield(b, 'head', (node_remove(bh, x)))
+            node_free(x); break
+        end
       end
-      print(old_dir, new_dir)
-      if old_dir==new_dir  then 
+      if not old_dir then
+        old_dir = has_attr(b, attr_dir) or 0
+         if old_dir ==0 then
+            old_dir = ltjs.list_dir
+         end
+      end
+      if old_dir==new_dir then
         set_attr(b, attr_icflag, PROCESSED)
         return head, node_next(b), b, false
-      elseif  -old_dir == new_dir  then 
+      elseif  -old_dir == new_dir  then
         return head, node_next(b), b, true
       else
         local nh, nb, ret, flag
-        if old_dir < 0 then 
+        if old_dir < 0 then
            -- b itself is a dir node; just unwrap
            local bc = node_next(node_next(
                                    node_next(node_next(bh))))
-           node_remove(bh, bc); 
+           node_remove(bh, bc);
            nh, nb =  insert_before(head, b, bc), nil
            nh, nb = node_remove(head, b)
            setfield(b, 'next', nil); Dnode.flush_list(b)
@@ -214,7 +284,7 @@ do
               elseif cmd=='box' then
                  nn = b; setfield(b, 'next', nil)
               end
-              if db_head then 
+              if db_head then
                  insert_after(db_head, db_tail, nn)
                  db_tail = nn
               else
@@ -227,13 +297,13 @@ do
         return nh, nb, ret, flag
       end
    end
-   local function process_dir_node(head)
+   local function process_dir_node(head, gc)
       local h = to_direct(head)
-      local x, new_dir = h, ltjs.table_current_stack[DIR]
+      local x, new_dir = h, ltjs.list_dir or dir_yoko
       while x do
         local xid = getid(x)
         if (xid==id_hlist and has_attr(x, attr_icflag)~=PACKED) or xid==id_vlist then
-           h, x = make_dir_node(h, x, new_dir)
+           h, x = make_dir_node(h, x, new_dir, 'process_dir_node:' .. gc)
         else
            x = node_next(x)
         end
@@ -241,6 +311,75 @@ do
       return to_node(h)
    end
    luatexja.direction.make_dir_node = make_dir_node
-   luatexbase.add_to_callback('vpack_filter', 
+   luatexbase.add_to_callback('vpack_filter',
                              process_dir_node, 'ltj.dir_node', 10001)
 end
+
+
+-- 縦書き用字形への変換テーブル
+local font_vert_table = {} -- key: fontnumber
+do
+   local font_vert_basename = {} -- key: basename
+   local function add_feature_table(tname, src, dest)
+      for i,v in pairs(src) do
+        if type(v.slookups)=='table' then
+           local s = v.slookups[tname]
+           if s and not dest[i] then
+              dest[i] = s
+           end
+        end
+      end
+   end
+
+   local function prepare_vert_data(n, id)
+      -- test if already loaded
+      if type(id)=='number' then -- sometimes id is an integer
+         font_vert_table[n] = font_vert_table[id]; return
+      elseif (not id) or font_vert_table[n]  then return
+      end
+      local fname = id.filename
+      local bname = file.basename(fname)
+      if not fname then
+         font_vert_table[n] = {}; return
+      elseif font_vert_basename[bname] then
+         font_vert_table[n] = font_vert_basename[bname]; return
+      end
+      local vtable = {}
+      local a = id.resources.sequences
+      if a then
+        local s = id.shared.rawdata.descriptions
+        for i,v in pairs(a) do
+           if v.features.vert or v.features.vrt2 then
+              add_feature_table(v.subtables[1], s, vtable)
+           end
+        end
+      end
+      font_vert_basename[bname] = vtable
+      font_vert_table[n] = vtable
+   end
+   -- 縦書き用字形への変換
+   function luatexja.direction.get_vert_glyph(n, chr)
+      local fn = font_vert_table[n]
+      return fn and fn[chr] or chr
+   end
+   luatexbase.add_to_callback('luatexja.define_font',
+                             function (res, name, size, id)
+                                prepare_vert_data(id, res)
+                             end,
+                             'prepare_vert_data', 1)
+
+   local function a (n, dat) font_vert_table[n] = dat end
+   luatexja.rmlgbm.vert_addfunc = a
+
+end
+
+-- PACKED の hbox から文字を取り出す
+-- luatexja.jfmglue.check_box などで使用
+do
+   local function glyph_from_packed(h)
+      local b = getlist(h)
+      return (getid(b)==id_kern) 
+        and node_next(node_next(node_next(node_next(b)))) or b
+   end
+   luatexja.direction.glyph_from_packed = glyph_from_packed
+end