OSDN Git Service

ltj-math.lua: bugfix of math style
[luatex-ja/luatexja.git] / src / ltj-math.lua
1 --
2 -- 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 setfield = node.direct.setfield
13 local getfield = node.direct.getfield
14 local getid = node.direct.getid
15 local getsubtype = node.direct.getsubtype
16 local getlist = node.direct.getlist
17 local getchar = node.direct.getchar
18 local getnucleus = node.direct.getnucleus
19 local getsup = node.direct.getsup
20 local getsub = node.direct.getsub
21 local getshift = node.direct.getshift
22 local setnext = node.direct.setnext
23 local setnucleus = node.direct.setnucleus
24 local setsup = node.direct.setsup
25 local setsub = node.direct.setsub
26 local setlist = node.direct.setlist
27 local setshift = node.direct.setshift
28
29 local to_node = node.direct.tonode
30 local to_direct = node.direct.todirect
31
32 local node_traverse = node.direct.traverse
33 local node_new = node.direct.new
34 local node_next = node.direct.getnext
35 local node_remove = node.direct.remove
36 local node_free = node.direct.flush_node or node.direct.free
37 local get_attr = node.direct.get_attribute
38 local set_attr = node.direct.set_attribute
39 local getcount = tex.getcount
40 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
41
42 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
43 local attr_dir = luatexbase.attributes['ltj@dir']
44 local attr_icflag = luatexbase.attributes['ltj@icflag']
45 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
46 local attr_jfam = luatexbase.attributes['jfam']
47 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
48
49 local id_glyph   = node.id 'glyph'
50 local id_hlist   = node.id 'hlist'
51 local id_vlist   = node.id 'vlist'
52 local id_mchar   = node.id 'math_char'
53 local id_sub_box = node.id 'sub_box'
54 local id_radical = node.id 'radical'
55 local id_choice  = node.id 'choice'
56 local id_accent  = node.id 'accent'
57 local id_style   = node.id 'style'
58 local id_frac    = node.id 'fraction'
59 local id_simple  = node.id 'noad'
60 local id_sub_mlist = node.id 'sub_mlist'
61 local id_whatsit = node.id 'whatsit'
62 local sid_user = node.subtype 'user_defined'
63 local DIR  = luatexja.userid_table.DIR
64 local dir_node_auto   = luatexja.dir_table.dir_node_auto
65
66 local PROCESSED  = luatexja.icflag_table.PROCESSED
67
68 local ltjf_font_metric_table = ltjf.font_metric_table
69 local ltjf_find_char_class = ltjf.find_char_class
70 local ltjd_get_dir_count = ltjd.get_dir_count
71 local ltjd_make_dir_whatsit = ltjd.make_dir_whatsit
72
73 -- table of mathematical characters
74 local is_math_letters = {}
75 local list_dir
76
77 -- vcenter noad は軸に揃えるため,欧文ベースライン補正がかかる
78 local function conv_vcenter(sb)
79    local h = getlist(sb) ; local hd = getlist(h)
80    if getid(hd)==id_whatsit and getsubtype(hd)==sid_user
81       and getfield(hd, 'user_id')==DIR then
82       local d = node_next(hd)
83       if getid(d)==id_vlist and get_attr(d, attr_dir)>=dir_node_auto then
84          node_free(hd); setlist(h, nil); node_free(h)
85          setlist(sb, d);  set_attr(d, attr_icflag, 0)
86       end
87    end
88    return sb
89 end
90
91 local cjhh_A
92 local max, min = math.max, math.min
93 -- sty : -1 (display), 0 (text), 1 (script), >=2 (scriptscript)
94 local function conv_jchar_to_hbox(head, sty)
95    for p in node_traverse(head) do
96       local pid = getid(p)
97       if pid == id_simple or pid == id_accent then
98          if getsubtype(p)==12 then
99             conv_vcenter(getnucleus(p))
100          else
101             setnucleus(p, cjh_A(getnucleus(p), sty))
102          end
103          setsub(p, cjh_A(getsub(p), max(sty+1,1)))
104          setsup(p, cjh_A(getsup(p), max(sty+1,1)))
105       elseif pid == id_choice then
106          setfield(p, 'display', cjh_A(getfield(p, 'display'), -1))
107          setfield(p, 'text', cjh_A(getfield(p, 'text'), 0))
108          setfield(p, 'script', cjh_A(getfield(p, 'script'), 1))
109          setfield(p, 'scriptscript', cjh_A(getfield(p, 'scriptscript'), 2))
110       elseif pid == id_frac then
111          setfield(p, 'num', cjh_A(getfield(p, 'num'), sty+1))
112          setfield(p, 'denom', cjh_A(getfield(p, 'denom'), sty+1))
113       elseif pid == id_radical then
114          setnucleus(p, cjh_A(getnucleus(p), sty))
115          setsub(p, cjh_A(getsub(p), max(sty+1,1)))
116          setsup(p, cjh_A(getsup(p), max(sty+1,1)))
117          if getfield(p, 'degree') then
118             setfield(p, 'degree', cjh_A(getfield(p, 'degree'), 2))
119          end
120       elseif pid == id_style then
121          local ps = getfield(p, 'style')
122          if ps == "display'" or  ps == 'display' then
123             sty = -1
124          elseif ps == "text'" or ps == 'text' then
125             sty = 0
126          elseif  ps == "script'" or  ps == 'script' then
127             sty = 1
128          else sty = 2
129          end
130        end
131    end
132    return head
133 end
134
135 local MJT  = luatexja.stack_table_index.MJT
136 local MJS  = luatexja.stack_table_index.MJS
137 local MJSS = luatexja.stack_table_index.MJSS
138 local capsule_glyph_math = ltjw.capsule_glyph_math
139 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
140 local setfont = node.direct.setfont
141 local setchar = node.direct.setchar
142
143 cjh_A = function (p, sty)
144    if not p then return nil
145    else
146       local pid = getid(p)
147       if pid == id_sub_mlist then
148          if getlist(p) then
149             setlist(p, conv_jchar_to_hbox(getlist(p), sty))
150          end
151       elseif pid == id_mchar then
152          local pc, fam = getchar (p), get_attr(p, attr_jfam) or -1
153          if (not is_math_letters[pc]) and is_ucs_in_japanese_char(p) and fam>=0 then
154             local f = ltjs.get_stack_table(MJT + 0x100 * min(max(sty,0),2) + fam, -1, getcount(cnt_stack))
155             if f ~= -1 then
156                local q = node_new(id_sub_box)
157                local r = node_new(id_glyph, 256); setnext(r, nil); setfont(r, f, pc)
158                local met = ltjf_font_metric_table[f]
159                r = capsule_glyph_math(
160                  r, met, met.char_type[ltjf_find_char_class(pc, met)], sty)
161                setlist(q, r); node_free(p); p=q;
162             end
163          end
164       elseif pid == id_sub_box and getlist(p) then
165          -- \hbox で直に与えられた内容は上下位置を補正する必要はない
166          local h = getlist(p); h = ltjd_make_dir_whatsit(h, h, list_dir, 'math')
167          setlist(p, h); setshift(h, getshift(h)-get_attr(h, attr_yablshift))
168          --set_attr(h, attr_icflag, PROCESSED)
169       end
170    end
171    return p
172 end
173
174 do
175   local function mlist_callback_ltja(n, display_type)
176     local n = to_direct(n); list_dir = ltjd_get_dir_count()
177     if getid(n)==id_whatsit and getsubtype(n)==sid_user and getfield(n, 'user_id') == DIR then
178       local old_n = n; n = node_remove(n, n)
179       node_free(old_n); if not n then return nil end
180     end
181     return to_node(conv_jchar_to_hbox(n, (display_type=='display') and -1 or 0))
182   end
183   -- LaTeX 2020-02-02 seems to have pre_mlist_to_hlist_filter callback
184   if luatexbase.callbacktypes['pre_mlist_to_hlist_filter'] then
185     luatexbase.add_to_callback('pre_mlist_to_hlist_filter',
186       mlist_callback_ltja, 'ltj.mlist_to_hlist_pre', 1)
187   else
188     local mlist_to_hlist = node.mlist_to_hlist
189     luatexbase.add_to_callback('mlist_to_hlist',
190       function (n, display_type, penalties)
191         return mlist_to_hlist(mlist_callback_ltja(n),display_type, penalties)
192       end,'ltj.mlist_to_hlist', 1)
193   end
194 end
195
196 luatexja.math = { is_math_letters = is_math_letters }