OSDN Git Service

a4073936f07c946c6b2fcdfd98029f5889596288
[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 or function(n) return getfield(n,'width') end
19 local getdepth = node.direct.getdepth or function(n) return getfield(n,'depth') end
20 local getwhd = node.direct.getwhd or function(n)
21   return getfield(n,'width'), getfield(n,'height'),getfield(n,'depth') end
22
23 local setwhd = node.direct.setwhd or function(n,w,h,d)
24   setfield(n,'width',w); setfield(n,'height',h); setfield(n,'depth',d) end
25 local setchar = node.direct.setchar or function(n,c) setfield(n,'char',c) end
26 local setnext = node.direct.setnext or function(n,c) setfield(n,'next',c) end
27 local setdir = node.direct.setdir or function(n,c) setfield(n,'dir',c) end
28 local setkern = node.direct.setkern or function(n,c) setfield(n,'kern',c) end
29 local setoffsets = node.direct.setoffsets or function(n,x,y)
30   setfield(n,'xoffset',x); setfield(n,'yoffset',y)  end
31 local getoffsets = node.direct.getoffsets or function(n)
32   return getfield(n,'xoffset'), getfield(n,'yoffset')  end
33
34 local node_traverse_id = node.direct.traverse_id
35 local node_traverse = node.direct.traverse
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 local node_new = luatexja.dnode_new
46
47 local id_glyph  = node.id 'glyph'
48 local id_kern   = node.id 'kern'
49 local id_hlist  = node.id 'hlist'
50 local id_vlist  = node.id 'vlist'
51 local id_rule   = node.id 'rule'
52 local id_math   = node.id 'math'
53 local id_whatsit= node.id 'whatsit'
54 local sid_save   = node.subtype 'pdf_save'
55 local sid_restore = node.subtype 'pdf_restore'
56 local sid_matrix  = node.subtype 'pdf_setmatrix'
57 local dir_tate = luatexja.dir_table.dir_tate
58
59 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
60 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
61 local attr_icflag = luatexbase.attributes['ltj@icflag']
62 local attr_vert_ori = luatexbase.attributes['ltj@vert@ori']
63
64 local ltjf_font_extra_info = ltjf.font_extra_info
65
66 local PACKED       = luatexja.icflag_table.PACKED
67 local PROCESSED    = luatexja.icflag_table.PROCESSED
68
69 local get_pr_begin_flag
70 do
71    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
72    local floor = math.floor
73    get_pr_begin_flag = function (p)
74       local i = has_attr(p, attr_icflag) or 0
75       return i - i%PROCESSED_BEGIN_FLAG
76    end
77 end
78
79
80 local ltjw = {} --export
81 luatexja.setwidth = ltjw
82
83 luatexbase.create_callback("luatexja.set_width", "data",
84                            function (fstable, fmtable, char_data)
85                               return fstable
86                            end)
87 local call_callback = luatexbase.call_callback
88
89 local fshift =  { down = 0, left = 0 }
90
91 local min, max, floor, abs = math.min, math.max, math.floor, math.abs
92
93 local rule_subtype = (status.luatex_version>=85) and 3 or 0
94
95 -- 和文文字の位置補正(横)
96 local function capsule_glyph_yoko(p, met, char_data, head, dir)
97    if not char_data then return node_next(p), head, p end
98    fshift.down = char_data.down; fshift.left = char_data.left
99    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
100    local kbl = has_attr(p, attr_ykblshift) or 0
101    --
102    -- f*: whd specified in JFM
103    local pwidth, pheight,pdepth = getwhd(p)
104    local fwidth = char_data.width or pwidth
105    local fheight= char_data.height or pheight
106    local fdepth = char_data.depth or pdepth
107    if pwidth==fwidth then
108       -- 補正後glyph node は ht: p.height - kbl - down, dp: p.depth + min(0, kbl+down) を持つ
109       -- 設定されるべき寸法: ht: fheight - kbl, dp: fdepth + kbl
110       local ht_diff = fheight + fshift.down - pheight
111       local dp_diff = fdepth  + kbl - pdepth - min(kbl + fshift.down, 0)
112       if ht_diff == 0 and dp_diff ==0 then -- offset only
113          set_attr(p, attr_icflag, PROCESSED)
114          local xo, yo = getoffsets(p)
115          setoffsets(p, xo - fshift.left, yo - kbl - fshift.down)
116          return node_next(p), head, p
117       elseif ht_diff >= 0 and dp_diff >=0 then -- rule
118          local box = node_new(id_rule, rule_subtype, p)
119          local xo, yo = getoffsets(p)
120          setoffsets(p, xo, yo - kbl - fshift.down)
121          setwhd(box, 0, fheight - kbl, fdepth + kbl)
122          setdir(box, dir)
123          set_attr(box, attr_icflag, PACKED)
124          set_attr(p, attr_icflag, PROCESSED)
125          head = p and node_insert_before(head, p, box)
126             or node_insert_after(head, node_tail(head), box)
127          return node_next(p), head, p, box
128       end
129    end
130
131    local q
132    head, q = node_remove(head, p)
133    if pwidth > fwidth then
134       if char_data.round_threshold then
135          local frac = pwidth / fwidth
136          local quot = floor(frac+0.5)
137          if abs(frac-quot) <char_data.round_threshold then fwidth = fwidth * quot end
138       end
139    end
140    local xo, yo = getoffsets(p)
141    setoffsets(p, xo + char_data.align*(fwidth-pwidth) - fshift.left,
142               yo - fshift.down);
143    setnext(p, nil)
144    local box = node_new(id_hlist, nil, p)
145    setwhd(box, fwidth, fheight, fdepth)
146    setfield(box, 'head', p)
147    setfield(box, 'shift', 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 = has_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    setfield(box, 'head', p)
177    setfield(box, 'shift', 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 (has_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 + (has_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)
222    setfield(box, 'shift', y_shift)
223    setdir(box, dir)
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)
234    setfield(box, 'head', ws)
235    setnext(ws, wm);  setnext(wm, k2);
236    setnext(k2, p);   setnext(p,  k3);
237    setnext(k3, wr);
238
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)
243    return q, head, box
244 end
245 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
246
247 local function capsule_glyph_math(p, met, char_data)
248    if not char_data then return nil end
249    local fwidth, pwidth = char_data.width, getwidth(p)
250    fwidth = fwidth or pwidth
251    fshift.down = char_data.down; fshift.left = char_data.left
252    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
253    local fheight, fdepth = char_data.height, char_data.depth
254    local y_shift
255       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
256    setfield(p, 'yoffset', -fshift.down)
257    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
258    local box = node_new(id_hlist, nil, p);
259    setwhd(box, fwidth, fheight, fdepth)
260    setfield(box, 'head', p)
261    setfield(box, 'shift', y_shift)
262    setdir(box, tex.mathdir)
263    set_attr(box, attr_icflag, PACKED)
264    return box
265 end
266 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
267
268 -- 数式の位置補正
269 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
270    for p in node_traverse(head) do
271       local pid = getid(p)
272       if p==last then
273          return
274       elseif (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
275          if pid==id_hlist or pid==id_vlist then
276             setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_ablshift) or 0)) 
277          elseif pid==id_rule then
278             local v = has_attr(p,attr_ablshift) or 0
279             setfield(p, 'height', getfield(p, 'height')-v)
280             setfield(p, 'depth', getdepth(p)+v)
281             set_attr(p, attr_icflag, PROCESSED)
282          elseif pid==id_glyph then
283             -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
284             -- (see conv_jchar_to_hbox_A in ltj-math.lua)
285             setfield(p, 'yoffset',
286                      getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
287          end
288          set_attr(p, attr_icflag, PROCESSED)
289       end
290    end
291 end
292
293 -- discretionary の位置補正
294 do
295    local attr_yablshift = luatexbase.attributes['ltj@yablshift']
296    local attr_tablshift = luatexbase.attributes['ltj@tablshift']
297    local attr_ablshift
298    local disc, tex_dir
299    local function ashift_disc_inner(field)
300       local head = getfield(disc, field)
301       if not head then return end
302       local y_adjust, node_depth, adj_depth = 0, 0, 0
303       for lp in node_traverse_id(id_glyph, head) do
304          y_adjust = has_attr(lp,attr_ablshift) or 0
305          local ld = getdepth(lp)
306          node_depth = max(ld + min(y_adjust, 0), node_depth)
307          adj_depth = (y_adjust>0) and max(ld + y_adjust, adj_depth) or adj_depth
308          setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
309       end
310       if adj_depth>node_depth then
311          local r = node_new(id_rule,rule_subtype, head)
312          setwhd(r, 0, 0, adj_depth); setdir(r, tex_dir)
313          set_attr(r, attr_icflag, PROCESSED)
314          if field=='post' then
315             node_insert_after(head, head, r)
316          else
317             setfield(disc, field, (node_insert_before(head, head, r)))
318          end
319       end
320    end
321    function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
322       attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
323       disc, tex_dir = d, dir
324       ashift_disc_inner 'pre'
325       ashift_disc_inner 'post'
326       ashift_disc_inner 'replace'
327    end
328 end