2 -- luatexja/ltj-math.lua
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
12 local Dnode = node.direct or node
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 -- getlist cannot be used for sub_box nodes. Use instead λp. getfield(p, 'head')
19 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
21 local nullfunc = function(n) return n end
22 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
23 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
25 local node_traverse = Dnode.traverse
26 local node_new = Dnode.new
27 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
28 local node_free = Dnode.free
29 local has_attr = Dnode.has_attribute
30 local set_attr = Dnode.set_attribute
31 local tex_getcount = tex.getcount
33 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
34 local attr_dir = luatexbase.attributes['ltj@dir']
35 local attr_icflag = luatexbase.attributes['ltj@icflag']
36 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
37 local attr_jfam = luatexbase.attributes['jfam']
38 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
40 local id_glyph = node.id('glyph')
41 local id_hlist = node.id('hlist')
42 local id_vlist = node.id('vlist')
43 local id_mchar = node.id('math_char')
44 local id_sub_box = node.id('sub_box')
45 local id_radical = node.id('radical')
46 local id_choice = node.id('choice')
47 local id_accent = node.id('accent')
48 local id_style = node.id('style')
49 local id_frac = node.id('fraction')
50 local id_simple = node.id('noad')
51 local id_sub_mlist = node.id('sub_mlist')
52 local id_whatsit = node.id('whatsit')
53 local sid_user = node.subtype('user_defined')
54 local DIR = luatexja.userid_table.DIR
56 local PROCESSED = luatexja.icflag_table.PROCESSED
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
63 -- table of mathematical characters
64 local is_math_letters = {}
66 local conv_jchar_to_hbox_A
68 -- sty : 0 (display or text), 1 (script), >=2 (scriptscript)
69 local function conv_jchar_to_hbox(head, sty)
70 for p in node_traverse(head) do
72 if pid == id_simple or pid == id_accent then
73 setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
74 setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
75 setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
76 elseif pid == id_choice then
77 setfield(p, 'display', conv_jchar_to_hbox_A(getfield(p, 'display'), 0))
78 setfield(p, 'text', conv_jchar_to_hbox_A(getfield(p, 'text'), 0))
79 setfield(p, 'script', conv_jchar_to_hbox_A(getfield(p, 'script'), 1))
80 setfield(p, 'scriptscript', conv_jchar_to_hbox_A(getfield(p, 'scriptscript'), 2))
81 elseif pid == id_frac then
82 setfield(p, 'num', conv_jchar_to_hbox_A(getfield(p, 'num'), sty+1))
83 setfield(p, 'denom', conv_jchar_to_hbox_A(getfield(p, 'denom'), sty+1))
84 elseif pid == id_radical then
85 setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
86 setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
87 setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
88 if getfield(p, 'degree') then
89 setfield(p, 'degree', conv_jchar_to_hbox_A(getfield(p, 'degree'), sty + 1))
91 elseif pid == id_style then
92 local ps = getfield(p, 'style')
93 if ps == "display'" or ps == 'display'
94 or ps == "text'" or ps == 'text' then
96 elseif ps == "script'" or ps == 'script' then
105 local MJT = luatexja.stack_table_index.MJT
106 local MJS = luatexja.stack_table_index.MJS
107 local MJSS = luatexja.stack_table_index.MJSS
108 local capsule_glyph_math = ltjw.capsule_glyph_math
109 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
112 conv_jchar_to_hbox_A =
114 if not p then return nil
117 if pid == id_sub_mlist then
118 if getfield(p, 'head') then
119 setfield(p, 'head', conv_jchar_to_hbox(getfield(p, 'head'), sty))
121 elseif pid == id_mchar then
122 local fam = has_attr(p, attr_jfam) or -1
123 local pc = getchar(p)
124 if (not is_math_letters[pc]) and is_ucs_in_japanese_char(p) and fam>=0 then
125 local f = ltjs.get_stack_table(MJT + 0x100 * sty + fam, -1, tex_getcount('ltj@@stack'))
127 local q = node_new(id_sub_box)
128 local r = node_new(id_glyph); setfield(r, 'next', nil)
129 setfield(r, 'char', pc); setfield(r, 'font', f); setfield(r, 'subtype', 256)
130 local k = has_attr(r,attr_ykblshift) or 0
131 set_attr(r, attr_ykblshift, 0)
132 -- ltj-setwidth 内で実際の位置補正はおこなうので,補正量を退避
133 local met = ltjf_font_metric_table[f]
134 r = capsule_glyph_math(r, met, ltjf_find_char_class(pc, met));
135 setfield(q, 'head', r); node_free(p); p=q;
136 set_attr(r, attr_yablshift, k)
139 elseif pid == id_sub_box and getfield(p, 'head') then
140 -- \hbox で直に与えられた内容は上下位置を補正する必要はない
141 local h = getfield(p, 'head')
142 h = ltjd_make_dir_whatsit(h, h, list_dir, 'math')
143 setfield(p, 'head', h)
144 set_attr(h, attr_icflag, PROCESSED)
150 local node_remove = Dnode.remove
151 luatexbase.add_to_callback('mlist_to_hlist',
152 function (n, display_type, penalties)
153 n = to_direct(n); list_dir = ltjd_get_dir_count()
154 if getid(n)==id_whatsit and getsubtype(n)==sid_user and
155 getfield(n, 'user_id') == DIR then
156 local old_n = n; n = node_remove(n, n)
159 return node.mlist_to_hlist(
160 to_node(conv_jchar_to_hbox(n, 0)),
161 display_type, penalties)
162 end,'ltj.mlist_to_hlist', 1)
164 luatexja.math = { is_math_letters = is_math_letters }