OSDN Git Service

da849e77067b453fb6063d5031b1d4e2070fcf4d
[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 Dnode = node.direct or node
11 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
12 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
13 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
14 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
15 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
16 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
17 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
18
19 local node_traverse_id = Dnode.traverse_id
20 local node_traverse = Dnode.traverse
21 local node_new = Dnode.new
22 local node_copy = Dnode.copy
23 local node_remove = Dnode.remove
24 local node_tail = Dnode.tail
25 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
26 local has_attr = Dnode.has_attribute
27 local set_attr = Dnode.set_attribute
28 local node_insert_before = Dnode.insert_before
29 local node_insert_after = Dnode.insert_after
30 local round = tex.round
31
32 local id_glyph = node.id('glyph')
33 local id_kern = node.id('kern')
34 local id_hlist = node.id('hlist')
35 local id_vlist = node.id('vlist')
36 local id_rule = node.id('rule')
37 local id_math = node.id('math')
38 local id_whatsit = node.id('whatsit')
39 local sid_save = node.subtype('pdf_save')
40 local sid_restore = node.subtype('pdf_restore')
41 local sid_matrix = node.subtype('pdf_setmatrix')
42 local dir_tate = luatexja.dir_table.dir_tate
43
44 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
45 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
46 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
47 local attr_icflag = luatexbase.attributes['ltj@icflag']
48
49 local ltjf_font_metric_table = ltjf.font_metric_table
50 local ltjf_get_vert_glyph = ltjf.get_vert_glyph
51
52 local PACKED       = luatexja.icflag_table.PACKED
53 local PROCESSED    = luatexja.icflag_table.PROCESSED
54 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
55 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
56
57 local get_pr_begin_flag
58 do
59    local floor = math.floor
60    get_pr_begin_flag = function (p)
61       local i = has_attr(p, attr_icflag) or 0
62       return i - i%PROCESSED_BEGIN_FLAG
63    end
64 end
65
66 local ltjw = {} --export
67 luatexja.setwidth = ltjw
68
69 luatexbase.create_callback("luatexja.set_width", "data",
70                            function (fstable, fmtable, jchar_class)
71                               return fstable
72                            end)
73 local call_callback = luatexbase.call_callback
74
75 local fshift =  { down = 0, left = 0}
76
77 local min = math.min
78 local function capsule_glyph_yoko(p, met, class, head, dir)
79    local char_data = met.char_type[class]
80    if not char_data then return node_next(p), head, p end
81    -- f*: whd specified in JFM
82    local fwidth, pwidth = char_data.width, getfield(p, 'width')
83    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
84    fshift.down = char_data.down; fshift.left = char_data.left
85    fshift = call_callback("luatexja.set_width", fshift, met, class)
86    local fheight, fdepth = char_data.height, char_data.depth
87    local kbl = has_attr(p, attr_ykblshift) or 0
88    -- 
89    local need_hbox
90    if pwidth==fwidth then
91       -- 補正後glyph node は ht: p.height - kbl - down, dp: p.depth + min(0, kbl+down) を持つ
92       -- 設定されるべき寸法: ht: fheight - kbl, dp: fdepth + kbl
93       local ht_diff = fheight + fshift.down - getfield(p, 'height') 
94       local dp_diff = fdepth  + kbl - getfield(p, 'depth') - min(kbl + fshift.down, 0)
95       if  ht_diff == 0 and dp_diff ==0 then -- offset only
96          set_attr(p, attr_icflag, PROCESSED)
97          setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
98          setfield(p, 'yoffset', - kbl - fshift.down)
99          return node_next(p), head, p
100       elseif ht_diff >= 0 and dp_diff >=0 then -- rule
101          local box = node_new(id_rule)
102          setfield(p, 'yoffset', - kbl - fshift.down)
103          setfield(box, 'width', 0)
104          setfield(box, 'height', fheight - kbl)
105          setfield(box, 'depth', fdepth + kbl)
106          setfield(box, 'dir', dir)
107          set_attr(box, attr_icflag, PACKED)
108          head = p and node_insert_before(head, p, box)
109             or node_insert_after(head, node_tail(head), box)
110          return node_next(p), head, p, box
111       else
112          need_hbox = true
113       end
114    else 
115       need_hbox = true
116    end
117
118    if need_hbox then
119       local q
120       head, q = node_remove(head, p)
121       setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
122       setfield(p, 'xoffset', getfield(p, 'xoffset') 
123                   + char_data.align*(fwidth-pwidth) - fshift.left)
124       local box = node_new(id_hlist)
125       setfield(box, 'width', fwidth)
126       setfield(box, 'height', fheight)
127       setfield(box, 'depth', fdepth)
128       setfield(box, 'head', p)
129       setfield(box, 'shift', kbl)
130       setfield(box, 'dir', dir)
131       set_attr(box, attr_icflag, PACKED)
132       head = q and node_insert_before(head, q, box)
133                or node_insert_after(head, node_tail(head), box)
134       return q, head, box
135    end
136 end
137
138 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
139
140 local function capsule_glyph_tate(p, met, class, head, dir)
141    local char_data = met.char_type[class]
142    if not char_data then return node_next(p), head end
143    local ascent, descent = met.ascent, met.descent
144    local fwidth, pwidth = char_data.width, ascent + descent
145    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
146    fshift.down = char_data.down; fshift.left = char_data.left
147    fshift = call_callback("luatexja.set_width", fshift, met, class)
148    local fheight, fdepth = char_data.height, char_data.depth
149
150    setfield(p, 'char', ltjf_get_vert_glyph(getfont(p), getchar(p)))
151
152    local y_shift
153       = - getfield(p, 'yoffset') + (has_attr(p,attr_tkblshift) or 0)
154    local q
155    head, q = node_remove(head, p)
156    local box = node_new(id_hlist)
157    setfield(box, 'width', fwidth)
158    setfield(box, 'height', fheight)
159    setfield(box, 'depth', fdepth)
160    setfield(box, 'shift', y_shift)
161    setfield(box, 'dir', dir)
162
163    setfield(p, 'xoffset', -fshift.down)
164    setfield(p, 'yoffset', - (getfield(p, 'xoffset') + ascent
165                                 + char_data.align*(fwidth-pwidth) - fshift.left) )
166    local ws = node_new(id_whatsit, sid_save)
167    local wm = node_new(id_whatsit, sid_matrix)
168    setfield(wm, 'data', '0 1 -1 0')
169    local pwnh = - 0.5*getfield(p, 'width')
170    local k2 = node_new(id_kern); setfield(k2, 'kern', - 0.5*getfield(p, 'width'))
171    local k3 = node_copy(k2)
172    local wr = node_new(id_whatsit, sid_restore)
173    setfield(box, 'head', ws)
174    setfield(ws, 'next', wm);  setfield(wm, 'next', k2);
175    setfield(k2, 'next', p);   setfield(p, 'next', k3);
176    setfield(k3, 'next', wr);
177
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 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
184
185 local function capsule_glyph_math(p, met, class)
186    local char_data = met.char_type[class]
187    if not char_data then return nil end
188    local fwidth, pwidth = char_data.width, getfield(p, 'width')
189    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
190    fshift.down = char_data.down; fshift.left = char_data.left
191    fshift = call_callback("luatexja.set_width", fshift, met, class)
192    local fheight, fdepth = char_data.height, char_data.depth
193    local y_shift, ca
194       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
195    setfield(p, 'yoffset', -fshift.down)
196    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
197    local box = node_new(id_hlist);
198    setfield(box, 'width', fwidth)
199    setfield(box, 'height', fheight)
200    setfield(box, 'depth', fdepth)
201    setfield(box, 'head', p)
202    setfield(box, 'shift', y_shift)
203    setfield(box, 'dir', tex.mathdir)
204    set_attr(box, attr_icflag, PACKED)
205    return box
206 end
207 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
208
209 -- 数式の位置補正
210 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
211    for p in node_traverse(head) do
212       local pid = getid(p)
213       if p==last then 
214          return
215       elseif (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
216          if pid==id_hlist or pid==id_vlist then
217             setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_ablshift) or 0))
218          elseif pid==id_rule then
219             local v = has_attr(p,attr_ablshift) or 0
220             setfield(p, 'height', getfield(p, 'height')-v)
221             setfield(p, 'depth', getfield(p, 'depth')+v)
222             set_attr(p, attr_icflag, PROCESSED)
223          elseif pid==id_glyph then
224             -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
225             -- (see conv_jchar_to_hbox_A in ltj-math.lua)
226             setfield(p, 'yoffset',
227                      getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
228          end
229       end
230    end
231 end