OSDN Git Service

lltjfont.sty: fix definition of \font{series,shape}force
[luatex-ja/luatexja.git] / src / ltj-math.lua
index 15813fc..b6ef1a9 100644 (file)
@@ -1,5 +1,5 @@
 --
--- luatexja/ltj-math.lua
+-- ltj-math.lua
 --
 
 luatexja.load_module('base');      local ltjb = luatexja.base
@@ -9,27 +9,24 @@ luatexja.load_module('jfont');     local ltjf = luatexja.jfont
 luatexja.load_module('stack');     local ltjs = luatexja.stack
 luatexja.load_module('setwidth');  local ltjw = luatexja.setwidth
 
-local Dnode = node.direct or node
-
-local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
-local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
-local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
-local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
-local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
+local setfield = node.direct.setfield
+local getfield = node.direct.getfield
+local getid = node.direct.getid
+local getsubtype = node.direct.getsubtype
+local getlist = node.direct.getlist
 -- getlist cannot be used for sub_box nodes. Use instead λp. getfield(p, 'head')
-local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
-
-local nullfunc = function(n) return n end
-local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
-local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
-
-local node_traverse = Dnode.traverse
-local node_new = Dnode.new
-local node_next = (Dnode ~= node) and Dnode.getnext or node.next
-local node_remove = Dnode.remove
-local node_free = Dnode.free
-local has_attr = Dnode.has_attribute
-local set_attr = Dnode.set_attribute
+local getchar = node.direct.getchar
+
+local to_node = node.direct.tonode
+local to_direct = node.direct.todirect
+
+local node_traverse = node.direct.traverse
+local node_new = node.direct.new
+local node_next = node.direct.getnext
+local node_remove = node.direct.remove
+local node_free = node.direct.free
+local has_attr = node.direct.has_attribute
+local set_attr = node.direct.set_attribute
 local tex_getcount = tex.getcount
 
 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
@@ -67,6 +64,7 @@ local ltjd_make_dir_whatsit = ltjd.make_dir_whatsit
 local is_math_letters = {}
 local list_dir
 
+-- vcenter noad は軸に揃えるため,欧文ベースライン補正がかかる
 local function conv_vcenter(sb)
    local h = getfield(sb, 'head');local hd = getlist(h)
    if getid(hd)==id_whatsit and 
@@ -74,10 +72,9 @@ local function conv_vcenter(sb)
       local d = node_next(hd)
       if getid(d)==id_vlist and has_attr(d, attr_dir)>=dir_node_auto then
         node_free(hd); setfield(h, 'head', nil); node_free(h)
-        setfield(sb, 'head', d); h = d
+        setfield(sb, 'head', d);  set_attr(d, attr_icflag, 0)
       end
    end
-   set_attr(h, attr_icflag, PROCESSED)
    return sb
 end
 
@@ -152,7 +149,7 @@ function (p, sty)
                set_attr(r, attr_ykblshift, 0)
                -- ltj-setwidth 内で実際の位置補正はおこなうので,補正量を退避
                local met = ltjf_font_metric_table[f]
-               r = capsule_glyph_math(r, met, ltjf_find_char_class(pc, met));
+               r = capsule_glyph_math(r, met, met.char_type[ltjf_find_char_class(pc, met)]);
                setfield(q, 'head', r); node_free(p); p=q;
                set_attr(r, attr_yablshift, k)
             end
@@ -168,18 +165,27 @@ function (p, sty)
    return p
 end
 
-local node_remove = Dnode.remove
-luatexbase.add_to_callback('mlist_to_hlist',
-   function (n, display_type, penalties)
-      n = to_direct(n); list_dir = ltjd_get_dir_count()
-      if getid(n)==id_whatsit and getsubtype(n)==sid_user and
-      getfield(n, 'user_id') == DIR then
-        local old_n = n; n = node_remove(n, n)
-        node_free(old_n)
-      end
-      return node.mlist_to_hlist(
-        to_node(conv_jchar_to_hbox(n, 0)),
-        display_type, penalties)
-   end,'ltj.mlist_to_hlist', 1)
+do
+  local function mlist_callback_ltja(n)
+    local n = to_direct(n); list_dir = ltjd_get_dir_count()
+    if getid(n)==id_whatsit and getsubtype(n)==sid_user and
+        getfield(n, 'user_id') == DIR then
+      local old_n = n; n = node_remove(n, n)
+      node_free(old_n); if not n then return nil end
+    end
+    return to_node(conv_jchar_to_hbox(n, 0))
+  end
+  -- LaTeX 2020-02-02 seems to have pre_mlist_to_hlist_filter callback
+  if luatexbase.callbacktypes['pre_mlist_to_hlist_filter'] then
+    luatexbase.add_to_callback('pre_mlist_to_hlist_filter',
+      mlist_callback_ltja, 'ltj.mlist_to_hlist_pre', 1)
+  else
+    local mlist_to_hlist = node.mlist_to_hlist
+    luatexbase.add_to_callback('mlist_to_hlist',
+      function (n, display_type, penalties)
+        return mlist_to_hlist(mlist_callback_ltja(n),display_type, penalties)
+      end,'ltj.mlist_to_hlist', 1)
+  end
+end
 
 luatexja.math = { is_math_letters = is_math_letters }