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 setfield = node.direct.setfield
11 local getfield = node.direct.getfield
12 local getid = node.direct.getid
13 local getfont = node.direct.getfont
14 local getlist = node.direct.getlist
15 local getchar = node.direct.getchar
16 local getsubtype = node.direct.getsubtype
18 local node_traverse_id = node.direct.traverse_id
19 local node_traverse = node.direct.traverse
20 local node_new = node.direct.new
21 local node_copy = node.direct.copy
22 local node_remove = node.direct.remove
23 local node_tail = node.direct.tail
24 local node_next = node.direct.getnext or node.next
25 local has_attr = node.direct.has_attribute
26 local set_attr = node.direct.set_attribute
27 local node_insert_before = node.direct.insert_before
28 local node_insert_after = node.direct.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_ykblshift = luatexbase.attributes['ltj@ykblshift']
44 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
45 local attr_icflag = luatexbase.attributes['ltj@icflag']
47 local ltjf_font_metric_table = ltjf.font_metric_table
48 local ltjf_font_extra_info = ltjf.font_extra_info
50 local PACKED = luatexja.icflag_table.PACKED
51 local PROCESSED = luatexja.icflag_table.PROCESSED
53 local get_pr_begin_flag
55 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
56 local floor = math.floor
57 get_pr_begin_flag = function (p)
58 local i = has_attr(p, attr_icflag) or 0
59 return i - i%PROCESSED_BEGIN_FLAG
63 local ltjw = {} --export
64 luatexja.setwidth = ltjw
66 luatexbase.create_callback("luatexja.set_width", "data",
67 function (fstable, fmtable, char_data)
70 local call_callback = luatexbase.call_callback
72 local fshift = { down = 0, left = 0}
74 local min, max = math.min, math.max
76 local rule_subtype = (status.luatex_version>=85) and 3 or 0
79 local function capsule_glyph_yoko(p, met, char_data, head, dir)
80 if not char_data then return node_next(p), head, p end
81 fshift.down = char_data.down; fshift.left = char_data.left
82 fshift = call_callback("luatexja.set_width", fshift, met, char_data)
83 local kbl = has_attr(p, attr_ykblshift) or 0
85 -- f*: whd specified in JFM
86 local fwidth, pwidth = char_data.width, getfield(p, 'width')
87 fwidth = fwidth or pwidth
88 local fheight, pheight = char_data.height, getfield(p, 'height')
89 fheight = fheight or pheight
90 local fdepth, pdepth = char_data.depth,getfield(p, 'depth')
91 fdepth = fdepth or pdepth
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 - pheight
96 local dp_diff = fdepth + kbl - pdepth - 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', getfield(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,rule_subtype)
104 setfield(p, 'yoffset', getfield(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, PROCESSED)
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
118 head, q = node_remove(head, p)
119 setfield(p, 'yoffset', getfield(p, 'yoffset') -fshift.down);
120 setfield(p, 'next', nil)
121 setfield(p, 'xoffset', getfield(p, 'xoffset')
122 + char_data.align*(fwidth-pwidth) - fshift.left)
123 local box = node_new(id_hlist)
124 setfield(box, 'width', fwidth)
125 setfield(box, 'height', fheight)
126 setfield(box, 'depth', fdepth)
127 setfield(box, 'head', p)
128 setfield(box, 'shift', kbl)
129 setfield(box, 'dir', dir)
130 set_attr(box, attr_icflag, PACKED)
131 head = q and node_insert_before(head, q, box)
132 or node_insert_after(head, node_tail(head), box)
136 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
139 local function capsule_glyph_tate(p, met, char_data, head, dir)
140 if not char_data then return node_next(p), head end
141 local ascent, descent = met.ascent, met.descent
142 local fwidth, pwidth = char_data.width
144 local pf = getfont(p)
145 local pc = getchar(p)
146 setfield(p, 'char', pc)
147 pwidth = ltjf_font_extra_info[pf] and ltjf_font_extra_info[pf][pc]
148 and ltjf_font_extra_info[pf][pc].vwidth
149 and ltjf_font_extra_info[pf][pc].vwidth * met.size or (ascent+descent)
150 pwidth = pwidth + (met.v_advance[pc] or 0)
151 ascent = met.v_origin[pc] and ascent - met.v_origin[pc] or ascent
153 fwidth = fwidth or pwidth
154 fshift.down = char_data.down; fshift.left = char_data.left
155 fshift = call_callback("luatexja.set_width", fshift, met, char_data)
156 local fheight = char_data.height or 0
157 local fdepth = char_data.depth or 0
159 = getfield(p, 'xoffset') + (has_attr(p,attr_tkblshift) or 0)
161 head, q = node_remove(head, p)
162 local box = node_new(id_hlist)
163 setfield(box, 'width', fwidth)
164 setfield(box, 'height', fheight)
165 setfield(box, 'depth', fdepth)
166 setfield(box, 'shift', y_shift)
167 setfield(box, 'dir', dir)
169 setfield(p, 'xoffset', - fshift.down)
170 setfield(p, 'yoffset', getfield(p, 'yoffset') -(ascent
171 + char_data.align*(fwidth-pwidth) - fshift.left) )
172 local ws = node_new(id_whatsit, sid_save)
173 local wm = node_new(id_whatsit, sid_matrix)
174 setfield(wm, 'data', '0 1 -1 0')
175 local pwnh = -round(0.5*getfield(p, 'width'))
176 local k2 = node_new(id_kern, 1); setfield(k2, 'kern', pwnh)
177 local k3 = node_new(id_kern, 1); setfield(k3, 'kern', -getfield(p, 'width')-pwnh)
178 local wr = node_new(id_whatsit, sid_restore)
179 setfield(box, 'head', ws)
180 setfield(ws, 'next', wm); setfield(wm, 'next', k2);
181 setfield(k2, 'next', p); setfield(p, 'next', k3);
182 setfield(k3, 'next', wr);
184 set_attr(box, attr_icflag, PACKED)
185 head = q and node_insert_before(head, q, box)
186 or node_insert_after(head, node_tail(head), box)
189 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
191 local function capsule_glyph_math(p, met, char_data)
192 if not char_data then return nil end
193 local fwidth, pwidth = char_data.width, getfield(p, 'width')
194 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
195 fshift.down = char_data.down; fshift.left = char_data.left
196 fshift = call_callback("luatexja.set_width", fshift, met, char_data)
197 local fheight, fdepth = char_data.height, char_data.depth
199 = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
200 setfield(p, 'yoffset', -fshift.down)
201 setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
202 local box = node_new(id_hlist);
203 setfield(box, 'width', fwidth)
204 setfield(box, 'height', fheight)
205 setfield(box, 'depth', fdepth)
206 setfield(box, 'head', p)
207 setfield(box, 'shift', y_shift)
208 setfield(box, 'dir', tex.mathdir)
209 set_attr(box, attr_icflag, PACKED)
212 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
215 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
216 for p in node_traverse(head) do
220 elseif (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
221 if pid==id_hlist or pid==id_vlist then
222 setfield(p, 'shift', getfield(p, 'shift') + (has_attr(p,attr_ablshift) or 0))
223 elseif pid==id_rule then
224 local v = has_attr(p,attr_ablshift) or 0
225 setfield(p, 'height', getfield(p, 'height')-v)
226 setfield(p, 'depth', getfield(p, 'depth')+v)
227 set_attr(p, attr_icflag, PROCESSED)
228 elseif pid==id_glyph then
229 -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
230 -- (see conv_jchar_to_hbox_A in ltj-math.lua)
231 setfield(p, 'yoffset',
232 getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
234 set_attr(p, attr_icflag, PROCESSED)
239 -- discretionary の位置補正
241 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
242 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
245 local function ashift_disc_inner(field)
246 local head = getfield(disc, field)
247 if not head then return end
248 local y_adjust, node_depth, adj_depth = 0, 0, 0
249 for lp in node_traverse_id(id_glyph, head) do
250 y_adjust = has_attr(lp,attr_ablshift) or 0
251 node_depth = max(getfield(lp, 'depth') + min(y_adjust, 0), node_depth)
252 adj_depth = (y_adjust>0) and max(getfield(lp, 'depth') + y_adjust, adj_depth) or adj_depth
253 setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
255 if adj_depth>node_depth then
256 local r = node_new(id_rule,rule_subtype)
257 setfield(r, 'width', 0); setfield(r, 'height', 0)
258 setfield(r, 'depth', adj_depth); setfield(r, 'dir', tex_dir)
259 set_attr(r, attr_icflag, PROCESSED)
260 if field=='post' then
261 node_insert_after(head, head, r)
263 setfield(disc, field, (node_insert_before(head, head, r)))
267 function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
268 attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
269 disc, tex_dir = d, dir
270 ashift_disc_inner('pre')
271 ashift_disc_inner('post')
272 ashift_disc_inner('replace')