OSDN Git Service

ltj-inputbuf.lua: add a close paren.
[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('direction'); local ltjd = luatexja.direction
7 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
8 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
9 luatexja.load_module('stack');     local ltjs = luatexja.stack
10 luatexja.load_module('setwidth');  local ltjw = luatexja.setwidth
11
12 local Dnode = node.direct or node
13
14 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
15 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
16 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
17 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
18 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
19 -- getlist cannot be used for sub_box nodes. Use instead λp. getfield(p, 'head')
20 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
21
22 local nullfunc = function(n) return n end
23 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
24 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
25
26 local node_traverse = Dnode.traverse
27 local node_new = Dnode.new
28 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
29 local node_remove = Dnode.remove
30 local node_free = Dnode.free
31 local has_attr = Dnode.has_attribute
32 local set_attr = Dnode.set_attribute
33 local tex_getcount = tex.getcount
34
35 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
36 local attr_dir = luatexbase.attributes['ltj@dir']
37 local attr_icflag = luatexbase.attributes['ltj@icflag']
38 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
39 local attr_jfam = luatexbase.attributes['jfam']
40 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
41
42 local id_glyph = node.id('glyph')
43 local id_hlist = node.id('hlist')
44 local id_vlist = node.id('vlist')
45 local id_mchar = node.id('math_char')
46 local id_sub_box = node.id('sub_box')
47 local id_radical = node.id('radical')
48 local id_choice  = node.id('choice')
49 local id_accent  = node.id('accent')
50 local id_style   = node.id('style')
51 local id_frac    = node.id('fraction')
52 local id_simple  = node.id('noad')
53 local id_sub_mlist = node.id('sub_mlist')
54 local id_whatsit = node.id('whatsit')
55 local sid_user = node.subtype('user_defined')
56 local DIR  = luatexja.userid_table.DIR
57 local dir_node_auto   = luatexja.dir_table.dir_node_auto
58
59 local PROCESSED  = luatexja.icflag_table.PROCESSED
60
61 local ltjf_font_metric_table = ltjf.font_metric_table
62 local ltjf_find_char_class = ltjf.find_char_class
63 local ltjd_get_dir_count = ltjd.get_dir_count
64 local ltjd_make_dir_whatsit = ltjd.make_dir_whatsit
65
66 -- table of mathematical characters
67 local is_math_letters = {}
68 local list_dir
69
70 local function conv_vcenter(sb)
71    local h = getfield(sb, 'head');local hd = getlist(h)
72    if getid(hd)==id_whatsit and 
73    getsubtype(hd)==sid_user and getfield(hd, 'user_id')==DIR then
74       local d = node_next(hd)
75       if getid(d)==id_vlist and has_attr(d, attr_dir)>=dir_node_auto then
76          node_free(hd); setfield(h, 'head', nil); node_free(h)
77          setfield(sb, 'head', d); h = d
78       end
79    end
80    set_attr(h, attr_icflag, PROCESSED)
81    return sb
82 end
83
84 local conv_jchar_to_hbox_A
85
86 -- sty : 0 (display or text), 1 (script), >=2 (scriptscript)
87 local function conv_jchar_to_hbox(head, sty)
88    for p in node_traverse(head) do
89       local pid = getid(p)
90       if pid == id_simple or pid == id_accent then
91          if getsubtype(p)==12 then
92             conv_vcenter(getfield(p, 'nucleus'))
93          else
94             setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
95          end
96          setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
97          setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
98       elseif pid == id_choice then
99          setfield(p, 'display', conv_jchar_to_hbox_A(getfield(p, 'display'), 0))
100          setfield(p, 'text', conv_jchar_to_hbox_A(getfield(p, 'text'), 0))
101          setfield(p, 'script', conv_jchar_to_hbox_A(getfield(p, 'script'), 1))
102          setfield(p, 'scriptscript', conv_jchar_to_hbox_A(getfield(p, 'scriptscript'), 2))
103       elseif pid == id_frac then
104          setfield(p, 'num', conv_jchar_to_hbox_A(getfield(p, 'num'), sty+1))
105          setfield(p, 'denom', conv_jchar_to_hbox_A(getfield(p, 'denom'), sty+1))
106       elseif pid == id_radical then
107          setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
108          setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
109          setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
110          if getfield(p, 'degree') then
111             setfield(p, 'degree', conv_jchar_to_hbox_A(getfield(p, 'degree'), sty + 1))
112          end
113       elseif pid == id_style then
114          local ps = getfield(p, 'style')
115          if ps == "display'" or  ps == 'display'
116             or  ps == "text'" or  ps == 'text' then
117             sty = 0
118          elseif  ps == "script'" or  ps == 'script' then
119             sty = 1
120          else sty = 2
121          end
122        end
123    end
124    return head
125 end
126
127 local MJT  = luatexja.stack_table_index.MJT
128 local MJS  = luatexja.stack_table_index.MJS
129 local MJSS = luatexja.stack_table_index.MJSS
130 local capsule_glyph_math = ltjw.capsule_glyph_math
131 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
132
133 conv_jchar_to_hbox_A =
134 function (p, sty)
135    if not p then return nil
136    else
137       local pid = getid(p)
138       if pid == id_sub_mlist then
139          if getfield(p, 'head') then
140             setfield(p, 'head', conv_jchar_to_hbox(getfield(p, 'head'), sty))
141          end
142       elseif pid == id_mchar then
143          local fam = has_attr(p, attr_jfam) or -1
144          local pc = getchar(p)
145          if (not is_math_letters[pc]) and is_ucs_in_japanese_char(p) and fam>=0 then
146             local f = ltjs.get_stack_table(MJT + 0x100 * sty + fam, -1, tex_getcount('ltj@@stack'))
147             if f ~= -1 then
148                local q = node_new(id_sub_box)
149                local r = node_new(id_glyph); setfield(r, 'next', nil)
150                setfield(r, 'char', pc); setfield(r, 'font', f); setfield(r, 'subtype', 256)
151                local k = has_attr(r,attr_ykblshift) or 0
152                set_attr(r, attr_ykblshift, 0)
153                -- ltj-setwidth 内で実際の位置補正はおこなうので,補正量を退避
154                local met = ltjf_font_metric_table[f]
155                r = capsule_glyph_math(r, met, met.char_type[ltjf_find_char_class(pc, met)]);
156                setfield(q, 'head', r); node_free(p); p=q;
157                set_attr(r, attr_yablshift, k)
158             end
159          end
160       elseif pid == id_sub_box and getfield(p, 'head') then
161          -- \hbox で直に与えられた内容は上下位置を補正する必要はない
162          local h = getfield(p, 'head')
163          h = ltjd_make_dir_whatsit(h, h, list_dir, 'math')
164          setfield(p, 'head', h)
165          set_attr(h, attr_icflag, PROCESSED)
166       end
167    end
168    return p
169 end
170
171 local node_remove = Dnode.remove
172 luatexbase.add_to_callback('mlist_to_hlist',
173    function (n, display_type, penalties)
174       n = to_direct(n); list_dir = ltjd_get_dir_count()
175       if getid(n)==id_whatsit and getsubtype(n)==sid_user and
176       getfield(n, 'user_id') == DIR then
177          local old_n = n; n = node_remove(n, n)
178          node_free(old_n)
179       end
180       return node.mlist_to_hlist(
181          to_node(conv_jchar_to_hbox(n, 0)),
182          display_type, penalties)
183    end,'ltj.mlist_to_hlist', 1)
184
185 luatexja.math = { is_math_letters = is_math_letters }