OSDN Git Service

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