OSDN Git Service

moved the greater part of luatexja.setwidth.set_ja_width to luatexja.jfmglue.main
[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('stack');     local ltjs = luatexja.stack
7 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
8 luatexja.load_module('direction'); local ltjd = luatexja.direction
9
10 local Dnode = node.direct or node
11 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
12 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
13 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
14 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
15 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
16 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
17 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
18
19 local node_traverse_id = Dnode.traverse_id
20 local node_traverse = Dnode.traverse
21 local node_new = Dnode.new
22 local node_copy = Dnode.copy
23 local node_remove = Dnode.remove
24 local node_tail = Dnode.tail
25 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
26 local has_attr = Dnode.has_attribute
27 local set_attr = Dnode.set_attribute
28 local node_insert_before = Dnode.insert_before
29 local node_insert_after = Dnode.insert_after
30 local round = tex.round
31
32 local id_glyph = node.id('glyph')
33 local id_kern = node.id('kern')
34 local id_hlist = node.id('hlist')
35 local id_vlist = node.id('vlist')
36 local id_rule = node.id('rule')
37 local id_math = node.id('math')
38 local id_whatsit = node.id('whatsit')
39 local sid_save = node.subtype('pdf_save')
40 local sid_restore = node.subtype('pdf_restore')
41 local sid_matrix = node.subtype('pdf_setmatrix')
42 local dir_tate = luatexja.dir_table.dir_tate
43
44 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
45 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
46 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
47 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
48 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
49 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
50 local attr_icflag = luatexbase.attributes['ltj@icflag']
51
52 local ltjf_font_metric_table = ltjf.font_metric_table
53 local ltjf_get_vert_glyph = ltjf.get_vert_glyph
54
55 local PACKED       = luatexja.icflag_table.PACKED
56 local PROCESSED    = luatexja.icflag_table.PROCESSED
57 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
58 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
59
60 local get_pr_begin_flag
61 do
62    local floor = math.floor
63    get_pr_begin_flag = function (p)
64       local i = has_attr(p, attr_icflag) or 0
65       return i - i%PROCESSED_BEGIN_FLAG
66    end
67 end
68
69 local ltjw = {} --export
70 luatexja.setwidth = ltjw
71
72 luatexbase.create_callback("luatexja.set_width", "data",
73                            function (fstable, fmtable, jchar_class)
74                               return fstable
75                            end)
76 local call_callback = luatexbase.call_callback
77
78 local fshift =  { down = 0, left = 0}
79
80 local function capsule_glyph_yoko(p, met, class, head, dir)
81    local char_data = met.char_type[class]
82    if not char_data then return node_next(p), head, p end
83    local fwidth, pwidth = char_data.width, getfield(p, 'width')
84    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
85    fshift.down = char_data.down; fshift.left = char_data.left
86    fshift = call_callback("luatexja.set_width", fshift, met, class)
87    local fheight, fdepth = char_data.height, char_data.depth
88    if (pwidth ~= fwidth or getfield(p, 'height') ~= fheight 
89          or getfield(p, 'depth') ~= fdepth) then
90       local y_shift
91          = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
92       local q
93       head, q = node_remove(head, p)
94       setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
95       setfield(p, 'xoffset', getfield(p, 'xoffset') 
96                   + char_data.align*(fwidth-pwidth) - fshift.left)
97       local box = node_new(id_hlist)
98       setfield(box, 'width', fwidth)
99       setfield(box, 'height', fheight)
100       setfield(box, 'depth', fdepth)
101       setfield(box, 'head', p)
102       setfield(box, 'shift', y_shift)
103       setfield(box, 'dir', dir)
104       set_attr(box, attr_icflag, PACKED)
105       head = q and node_insert_before(head, q, box)
106                or node_insert_after(head, node_tail(head), box)
107       return q, head, box
108    else
109       set_attr(p, attr_icflag, PROCESSED)
110       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
111       setfield(p, 'yoffset', getfield(p, 'yoffset')
112                   - (has_attr(p, attr_ykblshift) or 0) - fshift.down)
113       return node_next(p), head, p
114    end
115 end
116 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
117
118 local function capsule_glyph_tate(p, met, class, head, dir)
119    local char_data = met.char_type[class]
120    if not char_data then return node_next(p), head end
121    local ascent, descent = met.ascent, met.descent
122    local fwidth, pwidth = char_data.width, ascent + descent
123    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
124    fshift.down = char_data.down; fshift.left = char_data.left
125    fshift = call_callback("luatexja.set_width", fshift, met, class)
126    local fheight, fdepth = char_data.height, char_data.depth
127
128    setfield(p, 'char', ltjf_get_vert_glyph(getfont(p), getchar(p)))
129
130    local y_shift
131       = - getfield(p, 'yoffset') + (has_attr(p,attr_tkblshift) or 0)
132    local q
133    head, q = node_remove(head, p)
134    local box = node_new(id_hlist)
135    setfield(box, 'width', fwidth)
136    setfield(box, 'height', fheight)
137    setfield(box, 'depth', fdepth)
138    setfield(box, 'shift', y_shift)
139    setfield(box, 'dir', dir)
140
141    --local k1 = node_new(id_kern)
142    --setfield(k1, 'kern',
143    --    getfield(p, 'xoffset') + ascent
144    --       + char_data.align*(fwidth-pwidth) - fshift.left)
145    setfield(p, 'xoffset', -fshift.down)
146    setfield(p, 'yoffset', - (getfield(p, 'xoffset') + ascent
147                                 + char_data.align*(fwidth-pwidth) - fshift.left) )
148    local ws = node_new(id_whatsit, sid_save)
149    local wm = node_new(id_whatsit, sid_matrix)
150    setfield(wm, 'data', '0 1 -1 0')
151    local pwnh = - 0.5*getfield(p, 'width')
152    local k2 = node_new(id_kern); setfield(k2, 'kern', - 0.5*getfield(p, 'width'))
153    local k3 = node_copy(k2)
154    local wr = node_new(id_whatsit, sid_restore)
155    --setfield(box, 'head', k1); setfield(k1, 'next', ws)
156    setfield(box, 'head', ws)
157    setfield(ws, 'next', wm);  setfield(wm, 'next', k2);
158    setfield(k2, 'next', p);   setfield(p, 'next', k3);
159    setfield(k3, 'next', wr);
160
161    set_attr(box, attr_icflag, PACKED)
162    head = q and node_insert_before(head, q, box)
163       or node_insert_after(head, node_tail(head), box)
164    return q, head, box
165 end
166 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
167
168 local function capsule_glyph_math(p, met, class)
169    local char_data = met.char_type[class]
170    if not char_data then return nil end
171    local fwidth, pwidth = char_data.width, getfield(p, 'width')
172    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
173    fshift.down = char_data.down; fshift.left = char_data.left
174    fshift = call_callback("luatexja.set_width", fshift, met, class)
175    local fheight, fdepth = char_data.height, char_data.depth
176    local y_shift, ca
177       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
178    setfield(p, 'yoffset', -fshift.down)
179    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
180    local box = node_new(id_hlist);
181    setfield(box, 'width', fwidth)
182    setfield(box, 'height', fheight)
183    setfield(box, 'depth', fdepth)
184    setfield(box, 'head', p)
185    setfield(box, 'shift', y_shift)
186    setfield(box, 'dir', tex.mathdir)
187    set_attr(box, attr_icflag, PACKED)
188    return box
189 end
190 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
191
192 local tex_set_attr = tex.setattribute
193 function luatexja.setwidth.set_ja_width(head)
194    local attr_ablshift = (ltjs.list_dir==dir_tate) and attr_tablshift or attr_yablshift
195    for m in node_traverse_id(id_math, head) do
196       if getsubtype(m)==0 then
197          -- 数式の位置補正
198          for p in node_traverse(m) do
199             local pid = getid(p)
200             if pid==id_math  and getsubtype(p)==1 then 
201                break
202             elseif pid==id_hlist or pid==id_vlist then
203                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
204                   setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_ablshift) or 0))
205                end
206             elseif pid==id_rule then
207                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
208                   local v = has_attr(p,attr_yablshift) or 0
209                   setfield(p, 'height', getfield(p, 'height')-v)
210                   setfield(p, 'depth', getfield(p, 'depth')+v)
211                   set_attr(p, attr_icflag, PROCESSED)
212                end
213             end
214          end
215       end
216    end
217    -- adjust attr_icflag
218    tex_set_attr('global', attr_icflag, 0)
219    return head
220 end