OSDN Git Service

Forgot to update ltjclasses.dtx
[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 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
17 local getdepth = node.direct.getdepth or function(n) return getfield(n,'depth') end
18 local getwhd = node.direct.getwhd or function(n)
19   return getfield(n,'width'), getfield(n,'height'),getfield(n,'depth') end
20
21 local setwhd = node.direct.setwhd or function(n,w,h,d)
22   setfield(n,'width',w); setfield(n,'height',h); setfield(n,'depth',d) end
23 local setchar = node.direct.setchar or function(n,c) setfield(n,'char',c) end
24 local setnext = node.direct.setnext or function(n,c) setfield(n,'next',c) end
25 local setdir = node.direct.setdir or function(n,c) setfield(n,'dir',c) end
26 local setoffsets = node.direct.setoffsets or function(n,x,y)
27   setfield(n,'xoffset',x); setfield(n,'yoffset',y)  end
28 local getoffsets = node.direct.getoffsets or function(n)
29   return getfield(n,'xoffset'), getfield(n,'yoffset')  end
30
31 local node_traverse_id = node.direct.traverse_id
32 local node_traverse = node.direct.traverse
33 local node_new = node.direct.new
34 local node_copy = node.direct.copy
35 local node_remove = node.direct.remove
36 local node_tail = node.direct.tail
37 local node_next = node.direct.getnext
38 local has_attr = node.direct.has_attribute
39 local set_attr = node.direct.set_attribute
40 local node_insert_before = node.direct.insert_before
41 local node_insert_after = node.direct.insert_after
42 local round = tex.round
43
44 local id_glyph = node.id('glyph')
45 local id_kern = node.id('kern')
46 local id_hlist = node.id('hlist')
47 local id_vlist = node.id('vlist')
48 local id_rule = node.id('rule')
49 local id_math = node.id('math')
50 local id_whatsit = node.id('whatsit')
51 local sid_save = node.subtype('pdf_save')
52 local sid_restore = node.subtype('pdf_restore')
53 local sid_matrix = node.subtype('pdf_setmatrix')
54 local dir_tate = luatexja.dir_table.dir_tate
55
56 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
57 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
58 local attr_icflag = luatexbase.attributes['ltj@icflag']
59
60 local ltjf_font_extra_info = ltjf.font_extra_info
61
62 local PACKED       = luatexja.icflag_table.PACKED
63 local PROCESSED    = luatexja.icflag_table.PROCESSED
64
65 local get_pr_begin_flag
66 do
67    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
68    local floor = math.floor
69    get_pr_begin_flag = function (p)
70       local i = has_attr(p, attr_icflag) or 0
71       return i - i%PROCESSED_BEGIN_FLAG
72    end
73 end
74
75 local ltjw = {} --export
76 luatexja.setwidth = ltjw
77
78 luatexbase.create_callback("luatexja.set_width", "data",
79                            function (fstable, fmtable, char_data)
80                               return fstable
81                            end)
82 local call_callback = luatexbase.call_callback
83
84 local fshift =  { down = 0, left = 0}
85
86 local min, max = math.min, math.max
87
88 local rule_subtype = (status.luatex_version>=85) and 3 or 0
89
90 -- 和文文字の位置補正(横)
91 local function capsule_glyph_yoko(p, met, char_data, head, dir)
92    if not char_data then return node_next(p), head, p end
93    fshift.down = char_data.down; fshift.left = char_data.left
94    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
95    local kbl = has_attr(p, attr_ykblshift) or 0
96    --
97    -- f*: whd specified in JFM
98    local pwidth, pheight,pdepth = getwhd(p)
99    local fwidth = char_data.width or pwidth
100    local fheight= char_data.height or pheight
101    local fdepth = char_data.depth or pdepth
102    if pwidth==fwidth then
103       -- 補正後glyph node は ht: p.height - kbl - down, dp: p.depth + min(0, kbl+down) を持つ
104       -- 設定されるべき寸法: ht: fheight - kbl, dp: fdepth + kbl
105       local ht_diff = fheight + fshift.down - pheight
106       local dp_diff = fdepth  + kbl - pdepth - min(kbl + fshift.down, 0)
107       if ht_diff == 0 and dp_diff ==0 then -- offset only
108          set_attr(p, attr_icflag, PROCESSED)
109          local xo, yo = getoffsets(p)
110          setoffsets(p, xo - fshift.left, yo - kbl - fshift.down)
111          return node_next(p), head, p
112       elseif ht_diff >= 0 and dp_diff >=0 then -- rule
113          local box = node_new(id_rule,rule_subtype)
114          local xo, yo = getoffsets(p)
115          setoffsets(p, xo, yo - kbl - fshift.down)
116          setwhd(box, 0, fheight - kbl, fdepth + kbl)
117          setdir(box, dir)
118          set_attr(box, attr_icflag, PACKED)
119          set_attr(p, attr_icflag, PROCESSED)
120          head = p and node_insert_before(head, p, box)
121             or node_insert_after(head, node_tail(head), box)
122          return node_next(p), head, p, box
123       end
124    end
125
126    local q
127    head, q = node_remove(head, p)
128    local xo, yo = getoffsets(p)
129    setoffsets(p, xo + char_data.align*(fwidth-pwidth) - fshift.left,
130               yo - fshift.down);
131    setnext(p, nil)
132    local box = node_new(id_hlist)
133    setwhd(box, fwidth, fheight, fdepth)
134    setfield(box, 'head', p)
135    setfield(box, 'shift', kbl)
136    setdir(box, dir)
137    set_attr(box, attr_icflag, PACKED)
138    head = q and node_insert_before(head, q, box)
139       or node_insert_after(head, node_tail(head), box)
140    return q, head, box
141 end
142
143 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
144
145 -- 和文文字の位置補正(縦)
146 -- UTR#50 で R もしくは Tr と指定されているが,縦組用グリフがないもの
147 local function capsule_glyph_tate_rot(p, met, char_data, head, dir, asc)
148    fshift.down = char_data.down; fshift.left = char_data.left
149    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
150    local kbl = has_attr(p, attr_tkblshift) or 0
151    -- f*: whd specified in JFM
152    local pwidth, pheight,pdepth = getwhd(p)
153    local fwidth = char_data.width or pwidth
154    local fheight= char_data.height or pheight
155    local fdepth = char_data.depth or pdepth
156    local q
157    head, q = node_remove(head, p)
158    local xo, yo = getoffsets(p)
159    setoffsets(p, xo + char_data.align*(fwidth-pwidth) - fshift.left,
160               yo - fshift.down - asc);
161    setnext(p, nil)
162    local box = node_new(id_hlist)
163    setwhd(box, fwidth, fheight, fdepth)
164    setfield(box, 'head', p)
165    setfield(box, 'shift', kbl)
166    setdir(box, dir)
167    set_attr(box, attr_icflag, PACKED)
168    head = q and node_insert_before(head, q, box)
169       or node_insert_after(head, node_tail(head), box)
170    return q, head, box
171 end
172
173 local function capsule_glyph_tate(p, met, char_data, head, dir)
174    if not char_data then return node_next(p), head end
175    local ascent, descent = met.ascent, met.descent
176    local fwidth, pwidth = char_data.width
177    do
178       local pf = getfont(p)
179       local pc = getchar(p)
180       local cei = ltjf_font_extra_info[pf] and  ltjf_font_extra_info[pf][pc]
181       if cei and met.vert_activated then
182          if cei.rotation then
183             return capsule_glyph_tate_rot(p, met, char_data, head, dir, 0.5*(ascent-descent))
184          end
185       end
186       pwidth = (cei and cei.vwidth) and cei.vwidth * met.size or (ascent+descent)
187       pwidth = pwidth + (met.v_advance[pc] or 0)
188       ascent = met.v_origin[pc] and ascent - met.v_origin[pc] or ascent
189    end
190    fwidth = fwidth or pwidth
191    fshift.down = char_data.down; fshift.left = char_data.left
192    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
193    local fheight = char_data.height or 0
194    local fdepth  = char_data.depth or 0
195    local xo, yo = getoffsets(p)
196    local y_shift = xo + (has_attr(p,attr_tkblshift) or 0)
197    local q
198    head, q = node_remove(head, p)
199    local box = node_new(id_hlist)
200    setwhd(box, fwidth, fheight, fdepth)
201    setfield(box, 'shift', y_shift)
202    setdir(box, dir)
203
204    setoffsets(p, -fshift.down,
205               yo -(ascent + char_data.align*(fwidth-pwidth) - fshift.left) )
206    local ws = node_new(id_whatsit, sid_save)
207    local wm = node_new(id_whatsit, sid_matrix)
208    setfield(wm, 'data', '0 1 -1 0')
209    local pwnh = -round(0.5*getfield(p, 'width'))
210    local k2 = node_new(id_kern, 1); setfield(k2, 'kern', pwnh)
211    local k3 = node_new(id_kern, 1); setfield(k3, 'kern', -getfield(p, 'width')-pwnh)
212    local wr = node_new(id_whatsit, sid_restore)
213    setfield(box, 'head', ws)
214    setnext(ws, wm);  setnext(wm, k2);
215    setnext(k2, p);   setnext(p,  k3);
216    setnext(k3, wr);
217
218    set_attr(box, attr_icflag, PACKED)
219    head = q and node_insert_before(head, q, box)
220       or node_insert_after(head, node_tail(head), box)
221    return q, head, box
222 end
223 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
224
225 local function capsule_glyph_math(p, met, char_data)
226    if not char_data then return nil end
227    local fwidth, pwidth = char_data.width, getfield(p, 'width')
228    fwidth = fwidth or pwidth
229    fshift.down = char_data.down; fshift.left = char_data.left
230    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
231    local fheight, fdepth = char_data.height, char_data.depth
232    local y_shift
233       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
234    setfield(p, 'yoffset', -fshift.down)
235    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
236    local box = node_new(id_hlist);
237    setwhd(box, fwidth, fheight, fdepth)
238    setfield(box, 'head', p)
239    setfield(box, 'shift', y_shift)
240    setdir(box, tex.mathdir)
241    set_attr(box, attr_icflag, PACKED)
242    return box
243 end
244 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
245
246 -- 数式の位置補正
247 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
248    for p in node_traverse(head) do
249       local pid = getid(p)
250       if p==last then
251          return
252       elseif (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
253          if pid==id_hlist or pid==id_vlist then
254             setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_ablshift) or 0)) 
255          elseif pid==id_rule then
256             local v = has_attr(p,attr_ablshift) or 0
257             setfield(p, 'height', getfield(p, 'height')-v)
258             setfield(p, 'depth', getdepth(p)+v)
259             set_attr(p, attr_icflag, PROCESSED)
260          elseif pid==id_glyph then
261             -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
262             -- (see conv_jchar_to_hbox_A in ltj-math.lua)
263             setfield(p, 'yoffset',
264                      getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
265          end
266          set_attr(p, attr_icflag, PROCESSED)
267       end
268    end
269 end
270
271 -- discretionary の位置補正
272 do
273    local attr_yablshift = luatexbase.attributes['ltj@yablshift']
274    local attr_tablshift = luatexbase.attributes['ltj@tablshift']
275    local attr_ablshift
276    local disc, tex_dir
277    local function ashift_disc_inner(field)
278       local head = getfield(disc, field)
279       if not head then return end
280       local y_adjust, node_depth, adj_depth = 0, 0, 0
281       for lp in node_traverse_id(id_glyph, head) do
282          y_adjust = has_attr(lp,attr_ablshift) or 0
283          local ld = getdepth(lp)
284          node_depth = max(ld + min(y_adjust, 0), node_depth)
285          adj_depth = (y_adjust>0) and max(ld + y_adjust, adj_depth) or adj_depth
286          setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
287       end
288       if adj_depth>node_depth then
289          local r = node_new(id_rule,rule_subtype)
290          setwhd(r, 0, 0, adj_depth); setdir(r, tex_dir)
291          set_attr(r, attr_icflag, PROCESSED)
292          if field=='post' then
293             node_insert_after(head, head, r)
294          else
295             setfield(disc, field, (node_insert_before(head, head, r)))
296          end
297       end
298    end
299    function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
300       attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
301       disc, tex_dir = d, dir
302       ashift_disc_inner('pre')
303       ashift_disc_inner('post')
304       ashift_disc_inner('replace')
305    end
306 end