2 -- src/ltj-setwidth.lua
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
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
19 local node_traverse = Dnode.traverse
20 local node_new = Dnode.new
21 local node_copy = Dnode.copy
22 local node_remove = Dnode.remove
23 local node_tail = Dnode.tail
24 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
25 local has_attr = Dnode.has_attribute
26 local set_attr = Dnode.set_attribute
27 local node_insert_before = Dnode.insert_before
28 local node_insert_after = Dnode.insert_after
29 local round = tex.round
31 local id_glyph = node.id('glyph')
32 local id_kern = node.id('kern')
33 local id_hlist = node.id('hlist')
34 local id_vlist = node.id('vlist')
35 local id_rule = node.id('rule')
36 local id_math = node.id('math')
37 local id_whatsit = node.id('whatsit')
38 local sid_save = node.subtype('pdf_save')
39 local sid_restore = node.subtype('pdf_restore')
40 local sid_matrix = node.subtype('pdf_setmatrix')
41 local dir_tate = luatexja.dir_table.dir_tate
43 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
44 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
45 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
46 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
47 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
48 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
49 local attr_icflag = luatexbase.attributes['ltj@icflag']
51 local ltjf_font_metric_table = ltjf.font_metric_table
52 local ltjf_get_vert_glyph = ltjf.get_vert_glyph
54 local PACKED = luatexja.icflag_table.PACKED
55 local PROCESSED = luatexja.icflag_table.PROCESSED
56 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
57 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
59 local get_pr_begin_flag
61 local floor = math.floor
62 get_pr_begin_flag = function (p)
63 local i = has_attr(p, attr_icflag) or 0
64 return i - i%PROCESSED_BEGIN_FLAG
68 local ltjw = {} --export
69 luatexja.setwidth = ltjw
71 luatexbase.create_callback("luatexja.set_width", "data",
72 function (fstable, fmtable, jchar_class)
75 local call_callback = luatexbase.call_callback
77 local fshift = { down = 0, left = 0}
79 local function capsule_glyph_yoko(p, met, class, head, dir)
80 local char_data = met.char_type[class]
81 if not char_data then return node_next(p), head end
82 local fwidth, pwidth = char_data.width, getfield(p, 'width')
83 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
84 fshift.down = char_data.down; fshift.left = char_data.left
85 fshift = call_callback("luatexja.set_width", fshift, met, class)
86 local fheight, fdepth = char_data.height, char_data.depth
87 if (pwidth ~= fwidth or getfield(p, 'height') ~= fheight
88 or getfield(p, 'depth') ~= fdepth) then
90 = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
92 head, q = node_remove(head, p)
93 setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
94 setfield(p, 'xoffset', getfield(p, 'xoffset')
95 + char_data.align*(fwidth-pwidth) - fshift.left)
96 local box = node_new(id_hlist)
97 setfield(box, 'width', fwidth)
98 setfield(box, 'height', fheight)
99 setfield(box, 'depth', fdepth)
100 setfield(box, 'head', p)
101 setfield(box, 'shift', y_shift)
102 setfield(box, 'dir', dir)
103 set_attr(box, attr_icflag, PACKED)
104 head = q and node_insert_before(head, q, box)
105 or node_insert_after(head, node_tail(head), box)
108 set_attr(p, attr_icflag, PROCESSED)
109 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
110 setfield(p, 'yoffset', getfield(p, 'yoffset')
111 - (has_attr(p, attr_ykblshift) or 0) - fshift.down)
112 return node_next(p), head
115 local function capsule_glyph_tate(p, met, class, head, dir)
116 local char_data = met.char_type[class]
117 if not char_data then return node_next(p), head end
118 local ascent, descent = met.ascent, met.descent
119 local fwidth, pwidth = char_data.width, ascent + descent
120 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
121 fshift.down = char_data.down; fshift.left = char_data.left
122 fshift = call_callback("luatexja.set_width", fshift, met, class)
123 local fheight, fdepth = char_data.height, char_data.depth
125 setfield(p, 'char', ltjf_get_vert_glyph(getfont(p), getchar(p)))
128 = - getfield(p, 'yoffset') + (has_attr(p,attr_tkblshift) or 0)
130 head, q = node_remove(head, p)
131 local box = node_new(id_hlist)
132 setfield(box, 'width', fwidth)
133 setfield(box, 'height', fheight)
134 setfield(box, 'depth', fdepth)
135 setfield(box, 'shift', y_shift)
136 setfield(box, 'dir', dir)
138 --local k1 = node_new(id_kern)
139 --setfield(k1, 'kern',
140 -- getfield(p, 'xoffset') + ascent
141 -- + char_data.align*(fwidth-pwidth) - fshift.left)
142 setfield(p, 'xoffset', -fshift.down)
143 setfield(p, 'yoffset', - (getfield(p, 'xoffset') + ascent
144 + char_data.align*(fwidth-pwidth) - fshift.left) )
145 local ws = node_new(id_whatsit, sid_save)
146 local wm = node_new(id_whatsit, sid_matrix)
147 setfield(wm, 'data', '0 1 -1 0')
148 local pwnh = - 0.5*getfield(p, 'width')
149 local k2 = node_new(id_kern); setfield(k2, 'kern', - 0.5*getfield(p, 'width'))
150 local k3 = node_copy(k2)
151 local wr = node_new(id_whatsit, sid_restore)
152 --setfield(box, 'head', k1); setfield(k1, 'next', ws)
153 setfield(box, 'head', ws)
154 setfield(ws, 'next', wm); setfield(wm, 'next', k2);
155 setfield(k2, 'next', p); setfield(p, 'next', k3);
156 setfield(k3, 'next', wr);
158 set_attr(box, attr_icflag, PACKED)
159 head = q and node_insert_before(head, q, box)
160 or node_insert_after(head, node_tail(head), box)
164 local function capsule_glyph_math(p, met, class)
165 local char_data = met.char_type[class]
166 if not char_data then return nil end
167 local fwidth, pwidth = char_data.width, getfield(p, 'width')
168 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
169 fshift.down = char_data.down; fshift.left = char_data.left
170 fshift = call_callback("luatexja.set_width", fshift, met, class)
171 local fheight, fdepth = char_data.height, char_data.depth
173 = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
174 setfield(p, 'yoffset', -fshift.down)
175 setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
176 local box = node_new(id_hlist);
177 setfield(box, 'width', fwidth)
178 setfield(box, 'height', fheight)
179 setfield(box, 'depth', fdepth)
180 setfield(box, 'head', p)
181 setfield(box, 'shift', y_shift)
182 setfield(box, 'dir', tex.mathdir)
183 set_attr(box, attr_icflag, PACKED)
186 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
188 local tex_set_attr = tex.setattribute
189 function luatexja.setwidth.set_ja_width(ahead, adir)
190 local p = ahead; head = p; dir = adir or 'TLT'
191 local m = false -- is in math mode?
192 local is_dir_tate = ltjs.list_dir==dir_tate
193 local capsule_glyph = is_dir_tate and capsule_glyph_tate or capsule_glyph_yoko
194 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
195 -- local attr_ablshift = (ltjs.list_dir==dir_tate) and attr_tablshift or attr_yablshift
199 and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
200 local pf = getfont(p)
201 if pf == has_attr(p, attr_curjfnt) then
202 p, head = capsule_glyph(p, ltjf_font_metric_table[pf],
203 has_attr(p, attr_jchar_class), head, dir)
205 -- if pf ~= has_attr(p, attr_curjfnt) then
206 -- TODO: neg. offset does not increase depth
207 --local d = getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0)
208 set_attr(p, attr_icflag, PROCESSED)
209 setfield(p, 'yoffset',
210 getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
215 elseif pid==id_math then
216 m = (getsubtype(p)==0); p = node_next(p)
220 if pid==id_hlist or pid==id_vlist then
221 if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
222 setfield(p, 'shift', getfield(p, 'shift') + (has_attr(p,attr_ablshift) or 0))
224 elseif pid==id_rule then
225 if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
226 local v = has_attr(p,attr_yablshift) or 0
227 setfield(p, 'height', getfield(p, 'height')-v)
228 setfield(p, 'depth', getfield(p, 'depth')+v)
229 set_attr(p, attr_icflag, PROCESSED)
236 -- adjust attr_icflag
237 tex_set_attr('global', attr_icflag, 0)