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_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
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
44 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
45 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
46 local attr_icflag = luatexbase.attributes['ltj@icflag']
48 local ltjf_font_metric_table = ltjf.font_metric_table
49 local ltjf_font_extra_info = ltjf.font_extra_info
50 local ltjf_get_vert_glyph = ltjf.get_vert_glyph
52 local PACKED = luatexja.icflag_table.PACKED
53 local PROCESSED = luatexja.icflag_table.PROCESSED
54 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
55 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
57 local get_pr_begin_flag
59 local floor = math.floor
60 get_pr_begin_flag = function (p)
61 local i = has_attr(p, attr_icflag) or 0
62 return i - i%PROCESSED_BEGIN_FLAG
66 local ltjw = {} --export
67 luatexja.setwidth = ltjw
69 luatexbase.create_callback("luatexja.set_width", "data",
70 function (fstable, fmtable, jchar_class)
73 local call_callback = luatexbase.call_callback
75 local fshift = { down = 0, left = 0}
77 local min, max = math.min, math.max
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 -- f*: whd specified in JFM
84 local fwidth, pwidth = char_data.width, getfield(p, 'width')
85 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
86 fshift.down = char_data.down; fshift.left = char_data.left
87 fshift = call_callback("luatexja.set_width", fshift, met, class)
88 local fheight, fdepth = char_data.height, char_data.depth
89 local kbl = has_attr(p, attr_ykblshift) or 0
92 if pwidth==fwidth then
93 -- 補正後glyph node は ht: p.height - kbl - down, dp: p.depth + min(0, kbl+down) を持つ
94 -- 設定されるべき寸法: ht: fheight - kbl, dp: fdepth + kbl
95 local ht_diff = fheight + fshift.down - getfield(p, 'height')
96 local dp_diff = fdepth + kbl - getfield(p, 'depth') - min(kbl + fshift.down, 0)
97 if ht_diff == 0 and dp_diff ==0 then -- offset only
98 set_attr(p, attr_icflag, PROCESSED)
99 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
100 setfield(p, 'yoffset', - kbl - fshift.down)
101 return node_next(p), head, p
102 elseif ht_diff >= 0 and dp_diff >=0 then -- rule
103 local box = node_new(id_rule)
104 setfield(p, 'yoffset', - kbl - fshift.down)
105 setfield(box, 'width', 0)
106 setfield(box, 'height', fheight - kbl)
107 setfield(box, 'depth', fdepth + kbl)
108 setfield(box, 'dir', dir)
109 set_attr(box, attr_icflag, PACKED)
110 set_attr(p, attr_icflag, PACKED)
111 head = p and node_insert_before(head, p, box)
112 or node_insert_after(head, node_tail(head), box)
113 return node_next(p), head, p, box
123 head, q = node_remove(head, p)
124 setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
125 setfield(p, 'xoffset', getfield(p, 'xoffset')
126 + char_data.align*(fwidth-pwidth) - fshift.left)
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', kbl)
133 setfield(box, 'dir', dir)
134 set_attr(box, attr_icflag, PACKED)
135 head = q and node_insert_before(head, q, box)
136 or node_insert_after(head, node_tail(head), box)
141 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
144 local function capsule_glyph_tate(p, met, class, head, dir)
145 local char_data = met.char_type[class]
146 if not char_data then return node_next(p), head end
147 local ascent, descent = met.ascent, met.descent
148 local fwidth, pwidth = char_data.width
150 local pf = getfont(p)
151 local pc = ltjf_get_vert_glyph(pf, getchar(p))
152 setfield(p, 'char', pc)
153 pwidth = ltjf_font_extra_info[pf] and ltjf_font_extra_info[pf][pc]
154 and ltjf_font_extra_info[pf][pc].vwidth
155 and ltjf_font_extra_info[pf][pc].vwidth * met.size or (ascent+descent)
157 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
158 fshift.down = char_data.down; fshift.left = char_data.left
159 fshift = call_callback("luatexja.set_width", fshift, met, class)
160 local fheight, fdepth = char_data.height, char_data.depth
163 = - getfield(p, 'yoffset') + (has_attr(p,attr_tkblshift) or 0)
165 head, q = node_remove(head, p)
166 local box = node_new(id_hlist)
167 setfield(box, 'width', fwidth)
168 setfield(box, 'height', fheight)
169 setfield(box, 'depth', fdepth)
170 setfield(box, 'shift', y_shift)
171 setfield(box, 'dir', dir)
173 setfield(p, 'xoffset', -fshift.down)
174 setfield(p, 'yoffset', - (getfield(p, 'xoffset') + ascent
175 + char_data.align*(fwidth-pwidth) - fshift.left) )
176 local ws = node_new(id_whatsit, sid_save)
177 local wm = node_new(id_whatsit, sid_matrix)
178 setfield(wm, 'data', '0 1 -1 0')
179 local pwnh = - 0.5*getfield(p, 'width')
180 local k2 = node_new(id_kern); setfield(k2, 'kern', - 0.5*getfield(p, 'width'))
181 local k3 = node_copy(k2)
182 local wr = node_new(id_whatsit, sid_restore)
183 setfield(box, 'head', ws)
184 setfield(ws, 'next', wm); setfield(wm, 'next', k2);
185 setfield(k2, 'next', p); setfield(p, 'next', k3);
186 setfield(k3, 'next', wr);
188 set_attr(box, attr_icflag, PACKED)
189 head = q and node_insert_before(head, q, box)
190 or node_insert_after(head, node_tail(head), box)
193 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
195 local function capsule_glyph_math(p, met, class)
196 local char_data = met.char_type[class]
197 if not char_data then return nil end
198 local fwidth, pwidth = char_data.width, getfield(p, 'width')
199 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
200 fshift.down = char_data.down; fshift.left = char_data.left
201 fshift = call_callback("luatexja.set_width", fshift, met, class)
202 local fheight, fdepth = char_data.height, char_data.depth
204 = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
205 setfield(p, 'yoffset', -fshift.down)
206 setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
207 local box = node_new(id_hlist);
208 setfield(box, 'width', fwidth)
209 setfield(box, 'height', fheight)
210 setfield(box, 'depth', fdepth)
211 setfield(box, 'head', p)
212 setfield(box, 'shift', y_shift)
213 setfield(box, 'dir', tex.mathdir)
214 set_attr(box, attr_icflag, PACKED)
217 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
220 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
221 for p in node_traverse(head) do
225 elseif (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
226 if pid==id_hlist or pid==id_vlist then
227 setfield(p, 'shift', getfield(p, 'shift') + (has_attr(p,attr_ablshift) or 0))
228 elseif pid==id_rule then
229 local v = has_attr(p,attr_ablshift) or 0
230 setfield(p, 'height', getfield(p, 'height')-v)
231 setfield(p, 'depth', getfield(p, 'depth')+v)
232 set_attr(p, attr_icflag, PROCESSED)
233 elseif pid==id_glyph then
234 -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
235 -- (see conv_jchar_to_hbox_A in ltj-math.lua)
236 setfield(p, 'yoffset',
237 getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
243 -- discretionary の位置補正
245 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
246 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
249 local function ashift_disc_inner(field)
250 local head = getfield(disc, field)
251 if not head then return end
252 local y_adjust, node_depth, adj_depth = 0, 0, 0
253 for lp in node_traverse_id(id_glyph, head) do
254 y_adjust = has_attr(lp,attr_ablshift) or 0
255 node_depth = max(getfield(lp, 'depth') + min(y_adjust, 0), node_depth)
256 adj_depth = (y_adjust>0) and max(getfield(lp, 'depth') + y_adjust, adj_depth) or adj_depth
257 setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
259 if adj_depth>node_depth then
260 local r = node_new(id_rule)
261 setfield(r, 'width', 0); setfield(r, 'height', 0)
262 setfield(r, 'depth', adj_depth); setfield(r, 'dir', tex_dir)
263 set_attr(r, attr_icflag, PROCESSED)
264 if field=='post' then
265 node_insert_after(head, head, r)
267 setfield(disc, field, (node_insert_before(head, head, r)))
271 function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
272 attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
273 disc, tex_dir = d, dir
274 ashift_disc_inner('pre')
275 ashift_disc_inner('post')
276 ashift_disc_inner('replace')