OSDN Git Service

Imported ajmacros.sty
[luatex-ja/luatexja.git] / src / luatexja / math.lua
1 --
2 -- luatexja/math.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.math',
6   date = '2011/08/14',
7   version = '0.1',
8   description = 'Handling routines for Japanese characters in math mode',
9 })
10 module('luatexja.math', package.seeall)
11
12 require('luatexja.base');      local ltjb = luatexja.base
13 require('luatexja.charrange'); local ltjc = luatexja.charrange
14 require('luatexja.jfont');     local ltjf = luatexja.jfont
15 require('luatexja.stack');     local ltjs = luatexja.stack
16 require('luatexja.setwidth');  local ltjw = luatexja.setwidth
17
18 local node_new = node.new
19 local node_next = node.next
20 local node_free = node.free
21 local has_attr = node.has_attribute
22 local set_attr = node.set_attribute
23
24 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
25 local attr_icflag = luatexbase.attributes['ltj@icflag']
26 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
27 local attr_jfam = luatexbase.attributes['jfam']
28
29 local id_glyph = node.id('glyph')
30 local id_hlist = node.id('hlist')
31 local id_vlist = node.id('vlist')
32 local id_mchar = node.id('math_char')
33 local id_sub_box = node.id('sub_box')
34 local id_radical = node.id('radical')
35 local id_choice  = node.id('choice')
36 local id_accent  = node.id('accent')
37 local id_style   = node.id('style')
38 local id_frac    = node.id('fraction')
39 local id_simple  = node.id('noad')
40 local id_sub_mlist = node.id('sub_mlist')
41
42 local PROCESSED = 8
43
44 local conv_jchar_to_hbox_A
45
46 -- sty : 0 (display or text), 1 (script), >=2 (scriptscript)
47 local function conv_jchar_to_hbox(head, sty)
48    local p = head
49    local bhead = head
50    while p do
51       if p.id == id_simple or p.id == id_accent then
52          p.nucleus = conv_jchar_to_hbox_A(p.nucleus, sty)
53          p.sub = conv_jchar_to_hbox_A(p.sub, sty + 1)
54          p.sup = conv_jchar_to_hbox_A(p.sup, sty + 1)
55       elseif p.id == id_choice then
56          p.text = conv_jchar_to_hbox(p.text, 0)
57          p.script = conv_jchar_to_hbox(p.script, 1)
58          p.scriptscript = conv_jchar_to_hbox(p.scriptscript, 2)
59       elseif p.id == id_frac then
60          p.num = conv_jchar_to_hbox_A(p.num, sty + 1)
61          p.denom = conv_jchar_to_hbox_A(p.denom, sty + 1)
62       elseif p.id == id_radical then
63          p.nucleus = conv_jchar_to_hbox_A(p.nucleus, sty)
64          p.sub = conv_jchar_to_hbox_A(p.sub, sty + 1)
65          p.sup = conv_jchar_to_hbox_A(p.sup, sty + 1)
66          if p.degree then
67             p.degree = conv_jchar_to_hbox_A(p.degree, sty + 1)
68          end
69       elseif p.id == id_style then
70          if p.style == "display'" or  p.style == 'display'
71             or  p.style == "text'" or  p.style == 'text' then
72             sty = 0
73          elseif  p.style == "script'" or  p.style == 'script' then
74             sty = 1
75          else sty = 2
76          end
77        end
78        p = node.next(p)
79    end 
80    return head
81 end 
82
83 conv_jchar_to_hbox_A = 
84 function (p, sty)
85    if not p then return nil
86    elseif p.id == id_sub_mlist then
87       if p.head then
88          p.head = conv_jchar_to_hbox(p.head, sty)
89       end
90    elseif p.id == id_mchar then
91       local fam = has_attr(p, attr_jfam) or -1
92       if ltjc.is_ucs_in_japanese_char(p) and fam>=0 then
93          local mode = 'mjss'
94          if sty == 0 then mode = 'mjtext'
95          elseif sty == 1 then mode = 'mjscr'
96          end
97          local f = ltjs.get_penalty_table(mode, fam, -1, tex.getcount('ltj@@stack'))
98          if f ~= -1 then
99             local q = node_new(id_sub_box)
100             local r = node_new(id_glyph); r.next = nil
101             r.char = p.char; r.font = f; r.subtype = 256
102             set_attr(r, attr_icflag, PROCESSED)
103             local class = ltjf.find_char_class(p.char, ltjf.font_metric_table[f].jfm)
104             set_attr(r, attr_jchar_class, class)
105             ltjw.met_tb = ltjf.font_metric_table[f]
106             ltjw.char_data = ltjf.metrics[ltjw.met_tb.jfm].char_type[class]
107             ltjw.head = r; ltjw.capsule_glyph(r, tex.mathdir , true);
108             q.head = ltjw.head; node_free(p); p=q;
109          end
110       end
111    end
112    return p
113 end
114
115 luatexbase.add_to_callback('mlist_to_hlist', 
116    function (n, display_type, penalties)
117       local head = conv_jchar_to_hbox(n, 0);
118       head = node.mlist_to_hlist(head, display_type, penalties)
119       return head
120    end,'ltj.mlist_to_hlist', 1)