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 luatexja.load_module 'lotf_aux'; local ltju = luatexja.lotf_aux
11 local setfield = node.direct.setfield
12 local getfield = node.direct.getfield
13 local getid = node.direct.getid
14 local getfont = node.direct.getfont
15 local getlist = node.direct.getlist
16 local getchar = node.direct.getchar
17 local getsubtype = node.direct.getsubtype
18 local getwidth = node.direct.getwidth
19 local getshift = node.direct.getshift
20 local getoffsets = node.direct.getoffsets
21 local getheight = node.direct.getheight
22 local getdepth = node.direct.getdepth
23 local getwhd = node.direct.getwhd
25 local setwhd = node.direct.setwhd
26 local setchar = node.direct.setchar
27 local setnext = node.direct.setnext
28 local setdir = node.direct.setdir
29 local setkern = node.direct.setkern
30 local setshift = node.direct.setshift
31 local setoffsets = node.direct.setoffsets
32 local setheight = node.direct.setheight
33 local setdepth = node.direct.setdepth
34 local setlist = node.direct.setlist
36 local node_traverse = node.direct.traverse
37 local node_copy = node.direct.copy
38 local node_remove = node.direct.remove
39 local node_tail = node.direct.tail
40 local node_next = node.direct.getnext
41 local get_attr = node.direct.get_attribute
42 local set_attr = node.direct.set_attribute
43 local node_insert_before = node.direct.insert_before
44 local node_insert_after = node.direct.insert_after
45 local round = tex.round
46 local node_new = luatexja.dnode_new
48 local id_glyph = node.id 'glyph'
49 local id_kern = node.id 'kern'
50 local id_hlist = node.id 'hlist'
51 local id_vlist = node.id 'vlist'
52 local id_rule = node.id 'rule'
53 local id_math = node.id 'math'
54 local id_whatsit= node.id 'whatsit'
55 local sid_save = node.subtype 'pdf_save'
56 local sid_restore = node.subtype 'pdf_restore'
57 local sid_matrix = node.subtype 'pdf_setmatrix'
58 local dir_tate = luatexja.dir_table.dir_tate
60 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
61 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
62 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
63 local attr_icflag = luatexbase.attributes['ltj@icflag']
64 local attr_vert_ori = luatexbase.attributes['ltj@vert@ori']
66 local ltjf_font_extra_info = ltjf.font_extra_info
67 local ltjs_orig_char_table = ltjs.orig_char_table
69 local PACKED = luatexja.icflag_table.PACKED
70 local PROCESSED = luatexja.icflag_table.PROCESSED
72 local get_pr_begin_flag
74 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
75 local floor = math.floor
76 get_pr_begin_flag = function (p)
77 local i = get_attr(p, attr_icflag) or 0
78 return i - i%PROCESSED_BEGIN_FLAG
83 local ltjw = {} --export
84 luatexja.setwidth = ltjw
86 luatexbase.create_callback("luatexja.set_width", "data",
87 function (fstable, fmtable, char_data)
90 local call_callback = luatexbase.call_callback
92 local fshift = { down = 0, left = 0 }
94 local min, max, floor, abs = math.min, math.max, math.floor, math.abs
96 local rule_subtype = (status.luatex_version>=85) and 3 or 0
99 local function capsule_glyph_yoko(p, met, char_data, head, dir)
100 if not char_data then return node_next(p), head, p end
101 fshift.down = char_data.down; fshift.left = char_data.left
102 fshift = call_callback("luatexja.set_width", fshift, met, char_data)
103 local kbl = get_attr(p, attr_ykblshift) or 0
105 -- f*: whd specified in JFM
106 local pwidth, pheight,pdepth = getwhd(p)
107 local fwidth = char_data.width or pwidth
108 local fheight= char_data.height or pheight
109 local fdepth = char_data.depth or pdepth
110 if pwidth==fwidth then
111 -- 補正後glyph node は ht: p.height - kbl - down, dp: p.depth + min(0, kbl+down) を持つ
112 -- 設定されるべき寸法: ht: fheight - kbl, dp: fdepth + kbl
113 local ht_diff = fheight + fshift.down - pheight
114 local dp_diff = fdepth + kbl - pdepth - min(kbl + fshift.down, 0)
115 if ht_diff == 0 and dp_diff ==0 then -- offset only
116 set_attr(p, attr_icflag, PROCESSED)
117 local xo, yo = getoffsets(p)
118 setoffsets(p, xo - fshift.left, yo - kbl - fshift.down)
119 return node_next(p), head, p
120 elseif ht_diff >= 0 and dp_diff >=0 then -- rule
121 local box = node_new(id_rule, rule_subtype, p)
122 local xo, yo = getoffsets(p)
123 setoffsets(p, xo, yo - kbl - fshift.down)
124 setwhd(box, 0, fheight - kbl, fdepth + kbl)
126 set_attr(box, attr_icflag, PACKED)
127 set_attr(p, attr_icflag, PROCESSED)
128 head = p and node_insert_before(head, p, box)
129 or node_insert_after(head, node_tail(head), box)
130 return node_next(p), head, p, box
134 local q; head, q = node_remove(head, p)
135 if pwidth > fwidth then
136 if char_data.round_threshold then
137 local frac = pwidth / fwidth
138 local quot = floor(frac+0.5)
139 if abs(frac-quot) <char_data.round_threshold then fwidth = fwidth * quot end
142 local xo, yo = getoffsets(p)
143 setoffsets(p, xo + char_data.align*(fwidth-pwidth) - fshift.left,
146 local box = node_new(id_hlist, nil, p)
147 setwhd(box, fwidth, fheight, fdepth)
148 setlist(box, p); setshift(box, kbl)
150 set_attr(box, attr_icflag, PACKED)
151 head = q and node_insert_before(head, q, box)
152 or node_insert_after(head, node_tail(head), box)
156 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
159 -- UTR#50 で R もしくは Tr と指定されているが,縦組用グリフがないもの
160 local function capsule_glyph_tate_rot(p, met, char_data, head, dir, asc)
161 fshift.down = char_data.down; fshift.left = char_data.left
162 fshift = call_callback("luatexja.set_width", fshift, met, char_data)
163 local kbl = get_attr(p, attr_tkblshift) or 0
164 -- f*: whd specified in JFM
165 local pwidth, pheight,pdepth = getwhd(p)
166 local fwidth = char_data.width or pwidth
167 local fheight= char_data.height or pheight
168 local fdepth = char_data.depth or pdepth
170 head, q = node_remove(head, p)
171 local xo, yo = getoffsets(p)
172 setoffsets(p, xo + char_data.align*(fwidth-pwidth) - fshift.left,
173 yo - fshift.down - asc);
175 local box = node_new(id_hlist, nil, p)
176 setwhd(box, fwidth, fheight, fdepth)
177 setlist(box, p); setshift(box, kbl)
179 set_attr(box, attr_icflag, PACKED)
180 head = q and node_insert_before(head, q, box)
181 or node_insert_after(head, node_tail(head), box)
185 local font_getfont = font.getfont
186 local get_ascender, get_descender = ltju.get_ascender, ltju.get_descender
187 local function capsule_glyph_tate(p, met, char_data, head, dir)
188 if not char_data then return node_next(p), head end
189 local fwidth, pwidth, ascender = char_data.width
191 local pf, pc = getfont(p), getchar(p)
192 local feir = ltjf_font_extra_info[pf]
193 if met.rotation and met.vert_activated then
194 local f = font_getfont(pf)
195 local pco = ltjs_orig_char_table[p] or pc
196 local r, l = (pco==pc) and met.rotation[pc], f.properties and f.properties.language
197 if ((r==true) or (type(r)=="table" and not r[l])) and (get_attr(p, attr_vert_ori) or 0)<=0 then
198 return capsule_glyph_tate_rot(p, met, char_data, head, dir,
199 0.5*(get_ascender(pf)-get_descender(pf)))
202 pwidth, ascender = feir.vheight[pc]*met.size, feir.vorigin[pc]*met.size
203 -- print(pwidth/65536.,ascender/65536.)
205 -- luatexja.ext_show_node(node.direct.tonode(p), 'B> ', print)
206 local xo, yo = getoffsets(p)
207 --pwidth = pwidth - yo
208 fwidth = fwidth or pwidth
209 if pwidth>fwidth and char_data.round_threshold then
210 local frac = pwidth / fwidth
211 local quot = floor(frac+0.5)
212 if abs(frac-quot) <char_data.round_threshold then fwidth = fwidth * quot end
214 fshift.down = char_data.down; fshift.left = char_data.left
215 fshift = call_callback("luatexja.set_width", fshift, met, char_data)
216 local fheight = char_data.height or 0
217 local fdepth = char_data.depth or 0
218 local y_shift = xo + (get_attr(p,attr_tkblshift) or 0)
220 head, q = node_remove(head, p)
221 local box = node_new(id_hlist, nil, p)
222 setwhd(box, fwidth, fheight, fdepth); setshift(box, y_shift)
224 -- print(yo, ascender, char_data.align, fwidth-pwidth)
225 setoffsets(p, -fshift.down,
226 yo -(ascender + char_data.align*(fwidth-pwidth) - fshift.left) )
227 local ws = node_new(id_whatsit, sid_save)
228 local wm = node_new(id_whatsit, sid_matrix)
229 setfield(wm, 'data', '0 1 -1 0')
230 local pwnh = -round(0.5*getwidth(p))
231 local k2 = node_new(id_kern, 1); setkern(k2, pwnh)
232 local k3 = node_new(id_kern, 1); setkern(k3, -getwidth(p)-pwnh)
233 local wr = node_new(id_whatsit, sid_restore)
235 setnext(ws, wm); setnext(wm, k2);
236 setnext(k2, p); setnext(p, k3);
239 set_attr(box, attr_icflag, PACKED)
240 -- luatexja.ext_show_node(node.direct.tonode(box), 'A> ', print)
241 head = q and node_insert_before(head, q, box)
242 or node_insert_after(head, node_tail(head), box)
245 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
248 local font_getfont, famfont = font.getfont, node.family_font
249 local cap_math_aux = {
250 [-1]=function() return 1 end, [0]=function() return 1 end,
252 local sf, tf = famfont(2,1), famfont(2,0)
253 return font_getfont(sf).size/font_getfont(tf).size
256 local ssf, tf = famfont(2,2), famfont(2,0)
257 return font_getfont(ssf).size/font_getfont(tf).size
260 setmetatable(cap_math_aux, {__index=function(t,k) return t[2] end})
261 local function capsule_glyph_math(p, met, char_data, sty)
262 if not char_data then return nil end
263 local fwidth, pwidth = char_data.width, getwidth(p)
264 fwidth = fwidth or pwidth
265 fshift.down = char_data.down; fshift.left = char_data.left
266 fshift = call_callback("luatexja.set_width", fshift, met, char_data)
267 local fheight, fdepth = char_data.height, char_data.depth
268 local y_shift = - getfield(p, 'yoffset')
269 + cap_math_aux[sty]()*
270 ((get_attr(p,attr_ykblshift) or 0) - (get_attr(p,attr_yablshift) or 0))
271 setfield(p, 'yoffset', -fshift.down)
272 setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
273 local box = node_new(id_hlist, nil, p);
274 setwhd(box, fwidth, fheight, fdepth)
275 setlist(box, p); setshift(box, y_shift)
276 setdir(box, tex.mathdir)
277 set_attr(box, attr_icflag, PACKED)
280 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
284 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
285 for p, pid in node_traverse(head) do
288 elseif (get_attr(p, attr_icflag) or 0) ~= PROCESSED then
289 if pid==id_hlist or pid==id_vlist then
290 setshift(p, getshift(p) + (get_attr(p,attr_ablshift) or 0))
291 elseif pid==id_rule then
292 local v = get_attr(p,attr_ablshift) or 0
293 setheight(p, getheight(p)-v); setdepth(p, getdepth(p)+v)
294 set_attr(p, attr_icflag, PROCESSED)
295 elseif pid==id_glyph then
296 -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
297 -- (see conv_jchar_to_hbox_A in ltj-math.lua)
298 setfield(p, 'yoffset',
299 getfield(p, 'yoffset') - (get_attr(p,attr_ablshift) or 0))
301 set_attr(p, attr_icflag, PROCESSED)
306 -- discretionary の位置補正
308 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
309 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
312 local traverse_glyph = node.direct.traverse_glyph
313 local function ashift_disc_inner(field)
314 local head = getfield(disc, field)
315 if not head then return end
316 local y_adjust, node_depth, adj_depth = 0, 0, 0
317 for lp in traverse_glyph(head) do
318 y_adjust = get_attr(lp,attr_ablshift) or 0
319 local ld = getdepth(lp)
320 node_depth = max(ld + min(y_adjust, 0), node_depth)
321 adj_depth = (y_adjust>0) and max(ld + y_adjust, adj_depth) or adj_depth
322 setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
324 if adj_depth>node_depth then
325 local r = node_new(id_rule,rule_subtype, head)
326 setwhd(r, 0, 0, adj_depth); setdir(r, tex_dir)
327 set_attr(r, attr_icflag, PROCESSED)
328 if field=='post' then
329 node_insert_after(head, head, r)
331 setfield(disc, field, (node_insert_before(head, head, r)))
335 function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
336 attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
337 disc, tex_dir = d, dir
338 ashift_disc_inner 'pre'
339 ashift_disc_inner 'post'
340 ashift_disc_inner 'replace'