OSDN Git Service

... and more
[luatex-ja/luatexja.git] / src / ltj-setwidth.lua
1 --
2 -- src/ltj-setwidth.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
7
8 local Dnode = node.direct or node
9
10 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
11 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
12 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
13 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
14 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
15 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
16 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
17
18 local node_traverse = Dnode.traverse
19 local node_new = Dnode.new
20 local node_remove = Dnode.remove
21 local node_tail = Dnode.tail
22 local node_next = Dnode.getnext
23 local has_attr = Dnode.has_attribute
24 local set_attr = Dnode.set_attribute
25 local node_insert_before = Dnode.insert_before
26 local node_insert_after = Dnode.insert_after
27 local round = tex.round
28
29 local id_glyph = node.id('glyph')
30 local id_kern = node.id('kern')
31 local id_hlist = node.id('hlist')
32 local id_vlist = node.id('vlist')
33 local id_rule = node.id('rule')
34 local id_math = node.id('math')
35
36 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
37 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
38 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
39 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
40 local attr_icflag = luatexbase.attributes['ltj@icflag']
41
42 local ltjf_font_metric_table = ltjf.font_metric_table
43
44 local PACKED       = luatexja.icflag_table.PACKED
45 local PROCESSED    = luatexja.icflag_table.PROCESSED
46 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
47 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
48
49 local get_pr_begin_flag
50 do
51    local floor = math.floor
52    get_pr_begin_flag = function (p)
53       local i = has_attr(p, attr_icflag) or 0
54       return i - i%PROCESSED_BEGIN_FLAG
55    end
56 end
57
58 local head, dir
59 local ltjw = {} --export
60 luatexja.setwidth = ltjw
61
62 luatexbase.create_callback("luatexja.set_width", "data", 
63                            function (fstable, fmtable, jchar_class) 
64                               return fstable 
65                            end)
66 local call_callback = luatexbase.call_callback
67
68 local fshift =  { down = 0, left = 0}
69
70 -- mode: true iff p will be always encapsuled by a hbox
71 local function capsule_glyph(p, met, class)
72    local char_data = met.char_type[class]
73    if not char_data then return node_next(p) end
74    local fwidth, pwidth = char_data.width, getfield(p, 'width')
75    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
76    fshift.down = char_data.down; fshift.left = char_data.left
77    fshift = call_callback("luatexja.set_width", fshift, met, class)
78    local fheight, fdepth = char_data.height, char_data.depth
79    if (pwidth ~= fwidth or getfield(p, 'height') ~= fheight or getfield(p, 'depth') ~= fdepth) then
80       local y_shift, ca
81          = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
82       local q
83       head, q = node_remove(head, p)
84       setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
85       ca = (ca~='left') 
86          and -fshift.left + (((ca=='right') and fwidth-pwidth) or round((fwidth-pwidth)*0.5))
87          or  -fshift.left
88       setfield(p, 'xoffset', getfield(p, 'xoffset') + ca)
89       local box = node_new(id_hlist); 
90       setfield(box, 'width', fwidth)
91       setfield(box, 'height', fheight)
92       setfield(box, 'depth', fdepth)
93       setfield(box, 'head', p)
94       setfield(box, 'shift', y_shift)
95       setfield(box, 'dir', dir)
96       set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
97       head = q and node_insert_before(head, q, box) 
98                or node_insert_after(head, node_tail(head), box)
99       return q
100    else
101       set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
102       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
103       setfield(p, 'yoffset', getfield(p, 'yoffset') 
104                   - (has_attr(p, attr_ykblshift) or 0) - fshift.down)
105       return node_next(p)
106    end
107 end
108 luatexja.setwidth.capsule_glyph = capsule_glyph
109
110 local function capsule_glyph_math(p, met, class)
111    local char_data = met.char_type[class]
112    if not char_data then return nil end
113    local fwidth, pwidth = char_data.width, getfield(p, 'width')
114    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
115    fshift.down = char_data.down; fshift.left = char_data.left
116    fshift = call_callback("luatexja.set_width", fshift, met, class)
117    local fheight, fdepth = char_data.height, char_data.depth
118    local y_shift, ca
119       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
120    setfield(p, 'yoffset', -fshift.down)
121    if ca~='left'  then
122       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left
123                   + (((ca=='right') and fwidth - pwidth) or round((fwidth - pwidth)*0.5)))
124    else
125       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
126    end
127    local box = node_new(id_hlist); 
128    setfield(box, 'width', fwidth)
129    setfield(box, 'height', fheight)
130    setfield(box, 'depth', fdepth)
131    setfield(box, 'head', p)
132    setfield(box, 'shift', y_shift)
133    setfield(box, 'dir', tex.mathdir)
134    set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
135    return box
136 end
137 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
138
139 function luatexja.setwidth.set_ja_width(ahead, adir)
140    local p = ahead; head  = p; dir = adir or 'TLT'
141    local m = false -- is in math mode?
142    while p do
143       local pid = getid(p)
144       if (pid==id_glyph) 
145       and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
146          local pf = getfont(p)
147          if pf == has_attr(p, attr_curjfnt) then
148             p = capsule_glyph(p, ltjf_font_metric_table[pf], 
149                               has_attr(p, attr_jchar_class))
150          else
151             set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
152             setfield(p, 'yoffset',
153                      getfield(p, 'yoffset') - (has_attr(p,attr_yablshift) or 0))
154             p = node_next(p)
155          end
156       elseif pid==id_math then
157          m = (getsubtype(p)==0); p = node_next(p)
158       else
159          if m then
160             -- 数式の位置補正
161             if pid==id_hlist or pid==id_vlist then
162                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
163                   setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_yablshift) or 0))
164                end
165             elseif pid==id_rule then
166                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
167                   local v = has_attr(p,attr_yablshift) or 0
168                   setfield(p, 'height', getfield(p, 'height')-v)
169                   setfield(p, 'depth', getfield(p, 'depth')+v)
170                   set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
171                end
172             end
173          end
174          p = node_next(p)
175       end
176    end
177    -- adjust attr_icflag
178    tex.setattribute('global', attr_icflag, 0)
179    return head
180 end
181