OSDN Git Service

ltj-setwidth.lua: better? criteria for rotating glyphs
[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 = node.direct.traverse
37 local node_copy = node.direct.copy
38 local node_remove = node.direct.remove
39 local node_tail = node.direct.tail
40 local node_next = node.direct.getnext
41 local get_attr = node.direct.get_attribute
42 local set_attr = node.direct.set_attribute
43 local node_insert_before = node.direct.insert_before
44 local node_insert_after = node.direct.insert_after
45 local round = tex.round
46 local node_new = luatexja.dnode_new
47
48 local id_glyph  = node.id 'glyph'
49 local id_kern   = node.id 'kern'
50 local id_hlist  = node.id 'hlist'
51 local id_vlist  = node.id 'vlist'
52 local id_rule   = node.id 'rule'
53 local id_math   = node.id 'math'
54 local id_whatsit= node.id 'whatsit'
55 local sid_save   = node.subtype 'pdf_save'
56 local sid_restore = node.subtype 'pdf_restore'
57 local sid_matrix  = node.subtype 'pdf_setmatrix'
58 local dir_tate = luatexja.dir_table.dir_tate
59
60 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
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 local ltjs_orig_char_table = ltjs.orig_char_table
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 pco = ltjs_orig_char_table[p] or pc
196           local r = met.rotation[pco]
197           local l = f.properties and f.properties.language
198           r = (r==true) or (type(r)=="table" and not r[l])
199           if r then
200             r = met.rotation[pc]
201             r = (r==true) or (type(r)=="table" and not r[l])
202           end
203           if r and (get_attr(p, attr_vert_ori) or 0)<=0 then
204             return capsule_glyph_tate_rot(p, met, char_data, head, dir,
205               0.5*(get_ascender(pf)-get_descender(pf)))
206           end
207       end
208       pwidth, ascender = feir.vheight[pc]*met.size, feir.vorigin[pc]*met.size
209      -- print(pwidth/65536.,ascender/65536.)
210    end
211    -- luatexja.ext_show_node(node.direct.tonode(p), 'B> ', print)
212    local xo, yo = getoffsets(p)
213    --pwidth = pwidth - yo
214    fwidth = fwidth or pwidth
215    if pwidth>fwidth and char_data.round_threshold then
216       local frac = pwidth / fwidth
217       local quot = floor(frac+0.5)
218       if abs(frac-quot) <char_data.round_threshold then fwidth = fwidth * quot end
219    end
220    fshift.down = char_data.down; fshift.left = char_data.left
221    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
222    local fheight = char_data.height or 0
223    local fdepth  = char_data.depth or 0
224    local y_shift = xo + (get_attr(p,attr_tkblshift) or 0)
225    local q
226    head, q = node_remove(head, p)
227    local box = node_new(id_hlist, nil, p)
228    setwhd(box, fwidth, fheight, fdepth); setshift(box, y_shift)
229    setdir(box, dir)
230    -- print(yo, ascender, char_data.align, fwidth-pwidth)
231    setoffsets(p, -fshift.down,
232               yo -(ascender + char_data.align*(fwidth-pwidth) - fshift.left) )
233    local ws = node_new(id_whatsit, sid_save)
234    local wm = node_new(id_whatsit, sid_matrix)
235    setfield(wm, 'data', '0 1 -1 0')
236    local pwnh = -round(0.5*getwidth(p))
237    local k2 = node_new(id_kern, 1); setkern(k2, pwnh)
238    local k3 = node_new(id_kern, 1); setkern(k3, -getwidth(p)-pwnh)
239    local wr = node_new(id_whatsit, sid_restore)
240    setlist(box, ws)
241    setnext(ws, wm);  setnext(wm, k2);
242    setnext(k2, p);   setnext(p,  k3);
243    setnext(k3, wr);
244
245    set_attr(box, attr_icflag, PACKED)
246    -- luatexja.ext_show_node(node.direct.tonode(box), 'A> ', print)
247    head = q and node_insert_before(head, q, box)
248       or node_insert_after(head, node_tail(head), box)
249    return q, head, box
250 end
251 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
252
253 do
254 local font_getfont, famfont = font.getfont, node.family_font
255 local cap_math_aux = {
256   [-1]=function() return 1 end, [0]=function() return 1 end,
257   [1]=function()
258      local sf, tf = famfont(2,1), famfont(2,0)
259      return font_getfont(sf).size/font_getfont(tf).size
260   end,
261   [2]=function()
262      local ssf, tf = famfont(2,2), famfont(2,0)
263      return font_getfont(ssf).size/font_getfont(tf).size
264   end
265 }
266 setmetatable(cap_math_aux, {__index=function(t,k) return t[2] end})
267 local function capsule_glyph_math(p, met, char_data, sty)
268    if not char_data then return nil end
269    local fwidth, pwidth = char_data.width, getwidth(p)
270    fwidth = fwidth or pwidth
271    fshift.down = char_data.down; fshift.left = char_data.left
272    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
273    local fheight, fdepth = char_data.height, char_data.depth
274    local y_shift = - getfield(p, 'yoffset') 
275      + cap_math_aux[sty]()*
276        ((get_attr(p,attr_ykblshift) or 0) - (get_attr(p,attr_yablshift) or 0))
277    setfield(p, 'yoffset', -fshift.down)
278    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
279    local box = node_new(id_hlist, nil, p);
280    setwhd(box, fwidth, fheight, fdepth)
281    setlist(box, p); setshift(box, y_shift)
282    setdir(box, tex.mathdir)
283    set_attr(box, attr_icflag, PACKED)
284    return box
285 end
286 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
287 end
288
289 -- 数式の位置補正
290 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
291    for p, pid in node_traverse(head) do
292       if p==last then
293          return
294       elseif (get_attr(p, attr_icflag) or 0) ~= PROCESSED then
295          if pid==id_hlist or pid==id_vlist then
296             setshift(p, getshift(p) +  (get_attr(p,attr_ablshift) or 0))
297          elseif pid==id_rule then
298             local v = get_attr(p,attr_ablshift) or 0
299             setheight(p, getheight(p)-v); setdepth(p, getdepth(p)+v)
300             set_attr(p, attr_icflag, PROCESSED)
301          elseif pid==id_glyph then
302             -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
303             -- (see conv_jchar_to_hbox_A in ltj-math.lua)
304             setfield(p, 'yoffset',
305                      getfield(p, 'yoffset') - (get_attr(p,attr_ablshift) or 0))
306          end
307          set_attr(p, attr_icflag, PROCESSED)
308       end
309    end
310 end
311
312 -- discretionary の位置補正
313 do
314    local attr_yablshift = luatexbase.attributes['ltj@yablshift']
315    local attr_tablshift = luatexbase.attributes['ltj@tablshift']
316    local attr_ablshift
317    local disc, tex_dir
318    local traverse_glyph = node.direct.traverse_glyph
319    local function ashift_disc_inner(field)
320       local head = getfield(disc, field)
321       if not head then return end
322       local y_adjust, node_depth, adj_depth = 0, 0, 0
323       for lp in traverse_glyph(head) do
324          y_adjust = get_attr(lp,attr_ablshift) or 0
325          local ld = getdepth(lp)
326          node_depth = max(ld + min(y_adjust, 0), node_depth)
327          adj_depth = (y_adjust>0) and max(ld + y_adjust, adj_depth) or adj_depth
328          setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
329       end
330       if adj_depth>node_depth then
331          local r = node_new(id_rule,rule_subtype, head)
332          setwhd(r, 0, 0, adj_depth); setdir(r, tex_dir)
333          set_attr(r, attr_icflag, PROCESSED)
334          if field=='post' then
335             node_insert_after(head, head, r)
336          else
337             setfield(disc, field, (node_insert_before(head, head, r)))
338          end
339       end
340    end
341    function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
342       attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
343       disc, tex_dir = d, dir
344       ashift_disc_inner 'pre'
345       ashift_disc_inner 'post'
346       ashift_disc_inner 'replace'
347    end
348 end