OSDN Git Service

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