OSDN Git Service

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