2 -- luatexja/ltj-math.lua
5 luatexja.load_module('base'); local ltjb = luatexja.base
6 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
7 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
8 luatexja.load_module('stack'); local ltjs = luatexja.stack
9 luatexja.load_module('setwidth'); local ltjw = luatexja.setwidth
11 local Dnode = node.direct or node
13 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
14 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
15 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
16 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
17 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
19 local nullfunc = function(n) return n end
20 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
21 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
23 local node_traverse = Dnode.traverse
24 local node_new = Dnode.new
25 local node_next = Dnode.getnext
26 local node_free = Dnode.free
27 local has_attr = Dnode.has_attribute
28 local set_attr = Dnode.set_attribute
29 local tex_getcount = tex.getcount
31 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
32 local attr_icflag = luatexbase.attributes['ltj@icflag']
33 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
34 local attr_jfam = luatexbase.attributes['jfam']
35 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
37 local id_glyph = node.id('glyph')
38 local id_hlist = node.id('hlist')
39 local id_vlist = node.id('vlist')
40 local id_mchar = node.id('math_char')
41 local id_sub_box = node.id('sub_box')
42 local id_radical = node.id('radical')
43 local id_choice = node.id('choice')
44 local id_accent = node.id('accent')
45 local id_style = node.id('style')
46 local id_frac = node.id('fraction')
47 local id_simple = node.id('noad')
48 local id_sub_mlist = node.id('sub_mlist')
50 local PROCESSED = luatexja.icflag_table.PROCESSED
52 local ltjf_font_metric_table = ltjf.font_metric_table
53 local ltjf_find_char_class = ltjf.find_char_class
55 -- table of mathematical characters
56 local is_math_letters = {}
58 local conv_jchar_to_hbox_A
60 -- sty : 0 (display or text), 1 (script), >=2 (scriptscript)
61 local function conv_jchar_to_hbox(head, sty)
62 for p in node_traverse(head) do
64 if pid == id_simple or pid == id_accent then
65 setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
66 setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
67 setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
68 elseif pid == id_choice then
69 setfield(p, 'display', conv_jchar_to_hbox_A(getfield(p, 'display'), 0))
70 setfield(p, 'text', conv_jchar_to_hbox_A(getfield(p, 'text'), 0))
71 setfield(p, 'script', conv_jchar_to_hbox_A(getfield(p, 'script'), 1))
72 setfield(p, 'scriptscript', conv_jchar_to_hbox_A(getfield(p, 'scriptscript'), 2))
73 elseif pid == id_frac then
74 setfield(p, 'num', conv_jchar_to_hbox_A(getfield(p, 'num'), sty+1))
75 setfield(p, 'denom', conv_jchar_to_hbox_A(getfield(p, 'denom'), sty+1))
76 elseif pid == id_radical then
77 setfield(p, 'nucleus', conv_jchar_to_hbox_A(getfield(p, 'nucleus'), sty))
78 setfield(p, 'sub', conv_jchar_to_hbox_A(getfield(p, 'sub'), sty+1))
79 setfield(p, 'sup', conv_jchar_to_hbox_A(getfield(p, 'sup'), sty+1))
80 if getfield(p, 'degree') then
81 setfield(p, 'degree', conv_jchar_to_hbox_A(getfield(p, 'degree'), sty + 1))
83 elseif pid == id_style then
84 local ps = getfield(p, 'style')
85 if ps == "display'" or ps == 'display'
86 or ps == "text'" or ps == 'text' then
88 elseif ps == "script'" or ps == 'script' then
97 local MJT = luatexja.stack_table_index.MJT
98 local MJS = luatexja.stack_table_index.MJS
99 local MJSS = luatexja.stack_table_index.MJSS
100 local capsule_glyph_math = ltjw.capsule_glyph_math
101 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
103 conv_jchar_to_hbox_A =
105 if not p then return nil
108 if pid == id_sub_mlist then
110 setfield(p, 'head', conv_jchar_to_hbox(getlist(p), sty))
112 elseif pid == id_mchar then
113 local fam = has_attr(p, attr_jfam) or -1
114 local pc = getchar(p)
115 if (not is_math_letters[pc]) and is_ucs_in_japanese_char(p) and fam>=0 then
116 local f = ltjs.get_stack_table(MJT + 0x100 * sty + fam, -1, tex_getcount('ltj@@stack'))
118 local q = node_new(id_sub_box)
119 local r = node_new(id_glyph); setfield(r, 'next', nil)
120 setfield(r, 'char', pc); setfield(r, 'font', f); setfield(r, 'subtype', 256)
121 local k = has_attr(r,attr_ykblshift) or 0
122 set_attr(r, attr_ykblshift, 0)
123 -- ltj-setwidth 内で実際の位置補正はおこなうので,補正量を退避
124 local met = ltjf_font_metric_table[f]
125 r = capsule_glyph_math(r, met, ltjf_find_char_class(pc, met));
126 setfield(q, 'head', r); node_free(p); p=q;
127 set_attr(r, attr_yablshift, k)
130 elseif pid == id_sub_box and getlist(p) then
131 -- \hbox で直に与えられた内容は上下位置を補正する必要はない
132 set_attr(getlist(p), attr_icflag, PROCESSED)
138 luatexbase.add_to_callback('mlist_to_hlist',
139 function (n, display_type, penalties)
140 local head = to_node(conv_jchar_to_hbox(to_direct(n), 0))
141 head = node.mlist_to_hlist(head, display_type, penalties)
143 end,'ltj.mlist_to_hlist', 1)
145 luatexja.math = { is_math_letters = is_math_letters }