OSDN Git Service

Another fix of #32527.
[luatex-ja/luatexja.git] / src / ltj-math.lua
index 3354ddd..5f9e650 100644 (file)
@@ -1,25 +1,19 @@
 --
--- luatexja/math.lua
+-- luatexja/ltj-math.lua
 --
-luatexbase.provides_module({
-  name = 'luatexja.math',
-  date = '2011/08/14',
-  version = '0.1',
-  description = 'Handling routines for Japanese characters in math mode',
-})
-module('luatexja.math', package.seeall)
 
-require('luatexja.base');      local ltjb = luatexja.base
-require('luatexja.charrange'); local ltjc = luatexja.charrange
-require('luatexja.jfont');     local ltjf = luatexja.jfont
-require('luatexja.stack');     local ltjs = luatexja.stack
-require('luatexja.setwidth');  local ltjw = luatexja.setwidth
+luatexja.load_module('base');      local ltjb = luatexja.base
+luatexja.load_module('charrange'); local ltjc = luatexja.charrange
+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 node_new = node.new
 local node_next = node.next
 local node_free = node.free
 local has_attr = node.has_attribute
 local set_attr = node.set_attribute
+local tex_getcount = tex.getcount
 
 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
 local attr_icflag = luatexbase.attributes['ltj@icflag']
@@ -40,13 +34,13 @@ local id_frac    = node.id('fraction')
 local id_simple  = node.id('noad')
 local id_sub_mlist = node.id('sub_mlist')
 
-local PROCESSED = 8
+local PROCESSED  = luatexja.icflag_table.PROCESSED
 
 local ltjf_font_metric_table = ltjf.font_metric_table
 local ltjf_find_char_class = ltjf.find_char_class
 
 -- table of mathematical characters
-is_math_letters = {}
+local is_math_letters = {}
 
 local conv_jchar_to_hbox_A
 
@@ -88,34 +82,40 @@ local function conv_jchar_to_hbox(head, sty)
    return head
 end 
 
+local MJT  = luatexja.stack_table_index.MJT
+local MJS  = luatexja.stack_table_index.MJS
+local MJSS = luatexja.stack_table_index.MJSS
+
 conv_jchar_to_hbox_A = 
 function (p, sty)
    if not p then return nil
-   elseif p.id == id_sub_mlist then
-      if p.head then
-        p.head = conv_jchar_to_hbox(p.head, sty)
-      end
-   elseif p.id == id_mchar then
-      local fam = has_attr(p, attr_jfam) or -1
-      if (not is_math_letters[p.char]) and ltjc.is_ucs_in_japanese_char(p) and fam>=0 then
-        local mode = 'mjss'
-        if sty == 0 then mode = 'mjtext'
-        elseif sty == 1 then mode = 'mjscr'
-        end
-        local f = ltjs.get_penalty_table(mode, fam, -1, tex.getcount('ltj@@stack'))
-        if f ~= -1 then
-           local q = node_new(id_sub_box)
-           local r = node_new(id_glyph); r.next = nil
-           r.char = p.char; r.font = f; r.subtype = 256
-           set_attr(r, attr_icflag, PROCESSED)
-           set_attr(r, attr_yablshift, 0)
-           local met = ltjf_font_metric_table[f]
-           local class = ltjf_find_char_class(p.char, met)
-           set_attr(r, attr_jchar_class, class)
-           ltjw.char_data = ltjf.metrics[met.jfm].size_cache[met.size].char_type[class]
-           ltjw.head = r; ltjw.capsule_glyph(r, tex.mathdir , true, met, class);
-           q.head = ltjw.head; node_free(p); p=q;
-        end
+   else
+      local pid = p.id
+      if pid == id_sub_mlist then
+         if p.head then
+            p.head = conv_jchar_to_hbox(p.head, sty)
+         end
+      elseif pid == id_mchar then
+         local fam = has_attr(p, attr_jfam) or -1
+         if (not is_math_letters[p.char]) and ltjc.is_ucs_in_japanese_char(p) and fam>=0 then
+            local f = ltjs.get_penalty_table(MJT + 0x100 * sty + fam, -1, tex_getcount('ltj@@stack'))
+            if f ~= -1 then
+               local q = node_new(id_sub_box)
+               local r = node_new(id_glyph); r.next = nil
+               r.char = p.char; r.font = f; r.subtype = 256
+               local k = has_attr(r,attr_ykblshift) or 0 
+               set_attr(r, attr_ykblshift, 0)
+               -- ltj-setwidth 内で実際の位置補正はおこなうので,補正量を退避
+               ltjw.head = r; 
+               local met = ltjf_font_metric_table[f]
+               ltjw.capsule_glyph(r, tex.mathdir , true, met, ltjf_find_char_class(p.char, met));
+               q.head = ltjw.head; node_free(p); p=q;
+               set_attr(q.head, attr_yablshift, k)
+            end
+         end
+      elseif pid == id_sub_box and p.head then
+         -- \hbox で直に与えられた内容は上下位置を補正する必要はない
+         set_attr(p.head, attr_icflag, PROCESSED)
       end
    end
    return p
@@ -127,3 +127,5 @@ luatexbase.add_to_callback('mlist_to_hlist',
       head = node.mlist_to_hlist(head, display_type, penalties)
       return head
    end,'ltj.mlist_to_hlist', 1)
+
+luatexja.math = { is_math_letters = is_math_letters }