OSDN Git Service

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