OSDN Git Service

Use direct node sccess model in ltj-math.lua and ltj-setwidth.lua.
[luatex-ja/luatexja.git] / src / ltj-math.lua
1 --
2 -- luatexja/ltj-math.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
7 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
8 luatexja.load_module('stack');     local ltjs = luatexja.stack
9 luatexja.load_module('setwidth');  local ltjw = luatexja.setwidth
10
11 local Dnode = node.direct or node
12
13 local setfield = (Dnode == node.direct) and Dnode.setfield or function(n, i, c) n[i] = c end
14 local getfield = (Dnode == node.direct) and Dnode.getfield or function(n, i) return n[i] end
15 local getid = (Dnode == node.direct) and Dnode.getid or function(n) return n.id end
16 local getlist = (Dnode == node.direct) and Dnode.getlist or function(n) return n.head end
17 local getchar = (Dnode == node.direct) and Dnode.getlist or function(n) return n.char end
18
19 local nullfunc = function(n) return n end
20 local to_node = (Dnode == node.direct) and Dnode.tonode or nullfunc
21 local to_direct = (Dnode == node.direct) and Dnode.todirect or nullfunc
22
23 local node_traverse = Dnode.traverse
24 local node_new = Dnode.new
25 local node_next = Dnode.next
26 local node_free = Dnode.free
27 local has_attr = Dnode.has_attribute
28 local set_attr = Dnode.set_attribute
29 local tex_getcount = tex.getcount
30
31 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
32 local attr_icflag = luatexbase.attributes['ltj@icflag']
33 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
34 local attr_jfam = luatexbase.attributes['jfam']
35 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
36
37 local id_glyph = node.id('glyph')
38 local id_hlist = node.id('hlist')
39 local id_vlist = node.id('vlist')
40 local id_mchar = node.id('math_char')
41 local id_sub_box = node.id('sub_box')
42 local id_radical = node.id('radical')
43 local id_choice  = node.id('choice')
44 local id_accent  = node.id('accent')
45 local id_style   = node.id('style')
46 local id_frac    = node.id('fraction')
47 local id_simple  = node.id('noad')
48 local id_sub_mlist = node.id('sub_mlist')
49
50 local PROCESSED  = luatexja.icflag_table.PROCESSED
51
52 local ltjf_font_metric_table = ltjf.font_metric_table
53 local ltjf_find_char_class = ltjf.find_char_class
54
55 -- table of mathematical characters
56 local is_math_letters = {}
57
58 local conv_jchar_to_hbox_A
59
60 -- sty : 0 (display or text), 1 (script), >=2 (scriptscript)
61 local function conv_jchar_to_hbox(head, sty)
62    for p in node_traverse(head) do
63       local pid = getid(p)
64       if pid == id_simple or pid == id_accent then
65          setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
66          setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
67          setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
68       elseif pid == id_choice then
69          setfield(p, 'display', conv_jchar_to_hbox_A(getfield(p, 'display'), 0))
70          setfield(p, 'text', conv_jchar_to_hbox_A(getfield(p, 'text'), 0))
71          setfield(p, 'script', conv_jchar_to_hbox_A(getfield(p, 'script'), 1))
72          setfield(p, 'scriptscript', conv_jchar_to_hbox_A(getfield(p, 'scriptscript'), 2))
73       elseif pid == id_frac then
74          setfield(p, 'num', conv_jchar_to_hbox_A(getfield(p, 'num'), sty+1))
75          setfield(p, 'denom', conv_jchar_to_hbox_A(getfield(p, 'denom'), sty+1))
76       elseif pid == id_radical then
77          setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
78          setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
79          setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
80          if getfield(p, 'degree') then
81             setfield(p, 'degree', conv_jchar_to_hbox_A(getfield(p, 'degree'), sty + 1))
82          end
83       elseif pid == id_style then
84          local ps = getfield(p, 'style')
85          if ps == "display'" or  ps == 'display'
86             or  ps == "text'" or  ps == 'text' then
87             sty = 0
88          elseif  ps == "script'" or  ps == 'script' then
89             sty = 1
90          else sty = 2
91          end
92        end
93    end 
94    return head
95 end 
96
97 local MJT  = luatexja.stack_table_index.MJT
98 local MJS  = luatexja.stack_table_index.MJS
99 local MJSS = luatexja.stack_table_index.MJSS
100 local capsule_glyph = ltjw.capsule_glyph
101
102 conv_jchar_to_hbox_A = 
103 function (p, sty)
104    if not p then return nil
105    else
106       local pid = getid(p)
107       if pid == id_sub_mlist then
108          if getlist(p) then
109             setfield(p, 'head', conv_jchar_to_hbox(getlist(p), sty))
110          end
111       elseif pid == id_mchar then
112          local fam = has_attr(p, attr_jfam) or -1
113          local pc = getchar(p)
114          if (not is_math_letters[pc]) and ltjc.is_ucs_in_japanese_char(to_node(p)) and fam>=0 then
115             local f = ltjs.get_penalty_table(MJT + 0x100 * sty + fam, -1, tex_getcount('ltj@@stack'))
116             if f ~= -1 then
117                local q = node_new(id_sub_box)
118                local r = node_new(id_glyph); setfield(r, 'next', nil)
119                setfield(r, 'char', pc); setfield(r, 'font', f); setfield(r, 'subtype', 256)
120                local k = has_attr(r,attr_ykblshift) or 0 
121                set_attr(r, attr_ykblshift, 0)
122                -- ltj-setwidth 内で実際の位置補正はおこなうので,補正量を退避
123                ltjw.head = r; 
124                local met = ltjf_font_metric_table[f]
125                capsule_glyph(r, tex.mathdir , true, met, ltjf_find_char_class(pc, met));
126                setfield(q, 'head', ltjw.head); node_free(p); p=q;
127                set_attr(getlist(q), attr_yablshift, k)
128             end
129          end
130       elseif pid == id_sub_box and getlist(p) then
131          -- \hbox で直に与えられた内容は上下位置を補正する必要はない
132          set_attr(getlist(p), attr_icflag, PROCESSED)
133       end
134    end
135    return p
136 end
137
138 luatexbase.add_to_callback('mlist_to_hlist', 
139    function (n, display_type, penalties)
140       local head = to_node(conv_jchar_to_hbox(to_direct(n), 0))
141       head = node.mlist_to_hlist(head, display_type, penalties)
142       return head
143    end,'ltj.mlist_to_hlist', 1)
144
145 luatexja.math = { is_math_letters = is_math_letters }