OSDN Git Service

30094330c608b2d18e351d6fc7e0fa98f25c4cc7
[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_yablshift = luatexbase.attributes['ltj@yablshift']
62 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
63 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
64 local attr_icflag = luatexbase.attributes['ltj@icflag']
65 local attr_vert_ori = luatexbase.attributes['ltj@vert@ori']
66
67 local ltjf_font_extra_info = ltjf.font_extra_info
68
69 local PACKED       = luatexja.icflag_table.PACKED
70 local PROCESSED    = luatexja.icflag_table.PROCESSED
71
72 local get_pr_begin_flag
73 do
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
79    end
80 end
81
82
83 local ltjw = {} --export
84 luatexja.setwidth = ltjw
85
86 luatexbase.create_callback("luatexja.set_width", "data",
87                            function (fstable, fmtable, char_data)
88                               return fstable
89                            end)
90 local call_callback = luatexbase.call_callback
91
92 local fshift =  { down = 0, left = 0 }
93
94 local min, max, floor, abs = math.min, math.max, math.floor, math.abs
95
96 local rule_subtype = (status.luatex_version>=85) and 3 or 0
97
98 -- 和文文字の位置補正(横)
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
104    --
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)
125          setdir(box, dir)
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
131       end
132    end
133
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
140       end
141    end
142    local xo, yo = getoffsets(p)
143    setoffsets(p, xo + char_data.align*(fwidth-pwidth) - fshift.left,
144               yo - fshift.down);
145    setnext(p, nil)
146    local box = node_new(id_hlist, nil, p)
147    setwhd(box, fwidth, fheight, fdepth)
148    setlist(box, p); setshift(box, kbl)
149    setdir(box, dir)
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)
153    return q, head, box
154 end
155
156 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
157
158 -- 和文文字の位置補正(縦)
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
169    local q
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);
174    setnext(p, nil)
175    local box = node_new(id_hlist, nil, p)
176    setwhd(box, fwidth, fheight, fdepth)
177    setlist(box, p); setshift(box, kbl)
178    setdir(box, dir)
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)
182    return q, head, box
183 end
184
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
190    do
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 r, l = met.rotation[pc], f.properties and f.properties.language
196           if ((r==true) or (type(r)=="table" and not r[l])) and (get_attr(p, attr_vert_ori) or 0)<=0 then
197             return capsule_glyph_tate_rot(p, met, char_data, head, dir,
198               0.5*(get_ascender(pf)-get_descender(pf)))
199           end
200       end
201       pwidth, ascender = feir.vheight[pc]*met.size, feir.vorigin[pc]*met.size
202      -- print(pwidth/65536.,ascender/65536.)
203    end
204    -- luatexja.ext_show_node(node.direct.tonode(p), 'B> ', print)
205    local xo, yo = getoffsets(p)
206    --pwidth = pwidth - yo
207    fwidth = fwidth or pwidth
208    if pwidth>fwidth and char_data.round_threshold then
209       local frac = pwidth / fwidth
210       local quot = floor(frac+0.5)
211       if abs(frac-quot) <char_data.round_threshold then fwidth = fwidth * quot end
212    end
213    fshift.down = char_data.down; fshift.left = char_data.left
214    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
215    local fheight = char_data.height or 0
216    local fdepth  = char_data.depth or 0
217    local y_shift = xo + (get_attr(p,attr_tkblshift) or 0)
218    local q
219    head, q = node_remove(head, p)
220    local box = node_new(id_hlist, nil, p)
221    setwhd(box, fwidth, fheight, fdepth); setshift(box, y_shift)
222    setdir(box, dir)
223    -- print(yo, ascender, char_data.align, fwidth-pwidth)
224    setoffsets(p, -fshift.down,
225               yo -(ascender + char_data.align*(fwidth-pwidth) - fshift.left) )
226    local ws = node_new(id_whatsit, sid_save)
227    local wm = node_new(id_whatsit, sid_matrix)
228    setfield(wm, 'data', '0 1 -1 0')
229    local pwnh = -round(0.5*getwidth(p))
230    local k2 = node_new(id_kern, 1); setkern(k2, pwnh)
231    local k3 = node_new(id_kern, 1); setkern(k3, -getwidth(p)-pwnh)
232    local wr = node_new(id_whatsit, sid_restore)
233    setlist(box, ws)
234    setnext(ws, wm);  setnext(wm, k2);
235    setnext(k2, p);   setnext(p,  k3);
236    setnext(k3, wr);
237
238    set_attr(box, attr_icflag, PACKED)
239    -- luatexja.ext_show_node(node.direct.tonode(box), 'A> ', print)
240    head = q and node_insert_before(head, q, box)
241       or node_insert_after(head, node_tail(head), box)
242    return q, head, box
243 end
244 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
245
246 do
247     local cap_math_aux = {[0]=1, [1]=0 }
248     setmetatable(cap_math_aux, {__index=function() return 0.5 end})
249 local function capsule_glyph_math(p, met, char_data, sty)
250    if not char_data then return nil end
251    local fwidth, pwidth = char_data.width, getwidth(p)
252    fwidth = fwidth or pwidth
253    fshift.down = char_data.down; fshift.left = char_data.left
254    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
255    local fheight, fdepth = char_data.height, char_data.depth
256    print(utf.char(getchar(p)), get_attr(p,attr_ykblshift) or 0, get_attr(p,attr_yablshift) or 0, sty)
257    local y_shift = - getfield(p, 'yoffset') 
258                    + cap_math_aux[sty]*((get_attr(p,attr_ykblshift) or 0) - (get_attr(p,attr_yablshift) or 0))
259    setfield(p, 'yoffset', -fshift.down)
260    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
261    local box = node_new(id_hlist, nil, p);
262    setwhd(box, fwidth, fheight, fdepth)
263    setlist(box, p); setshift(box, y_shift)
264    setdir(box, tex.mathdir)
265    set_attr(box, attr_icflag, PACKED)
266    return box
267 end
268 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
269 end
270
271 -- 数式の位置補正
272 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
273    for p in node_traverse(head) do
274       local pid = getid(p)
275       if p==last then
276          return
277       elseif (get_attr(p, attr_icflag) or 0) ~= PROCESSED then
278          if pid==id_hlist or pid==id_vlist then
279             setshift(p, getshift(p) +  (get_attr(p,attr_ablshift) or 0))
280          elseif pid==id_rule then
281             local v = get_attr(p,attr_ablshift) or 0
282             setheight(p, getheight(p)-v); setdepth(p, getdepth(p)+v)
283             set_attr(p, attr_icflag, PROCESSED)
284          elseif pid==id_glyph then
285             -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
286             -- (see conv_jchar_to_hbox_A in ltj-math.lua)
287             setfield(p, 'yoffset',
288                      getfield(p, 'yoffset') - (get_attr(p,attr_ablshift) or 0))
289          end
290          set_attr(p, attr_icflag, PROCESSED)
291       end
292    end
293 end
294
295 -- discretionary の位置補正
296 do
297    local attr_yablshift = luatexbase.attributes['ltj@yablshift']
298    local attr_tablshift = luatexbase.attributes['ltj@tablshift']
299    local attr_ablshift
300    local disc, tex_dir
301    local function ashift_disc_inner(field)
302       local head = getfield(disc, field)
303       if not head then return end
304       local y_adjust, node_depth, adj_depth = 0, 0, 0
305       for lp in node_traverse_id(id_glyph, head) do
306          y_adjust = get_attr(lp,attr_ablshift) or 0
307          local ld = getdepth(lp)
308          node_depth = max(ld + min(y_adjust, 0), node_depth)
309          adj_depth = (y_adjust>0) and max(ld + y_adjust, adj_depth) or adj_depth
310          setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
311       end
312       if adj_depth>node_depth then
313          local r = node_new(id_rule,rule_subtype, head)
314          setwhd(r, 0, 0, adj_depth); setdir(r, tex_dir)
315          set_attr(r, attr_icflag, PROCESSED)
316          if field=='post' then
317             node_insert_after(head, head, r)
318          else
319             setfield(disc, field, (node_insert_before(head, head, r)))
320          end
321       end
322    end
323    function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
324       attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
325       disc, tex_dir = d, dir
326       ashift_disc_inner 'pre'
327       ashift_disc_inner 'post'
328       ashift_disc_inner 'replace'
329    end
330 end