OSDN Git Service

Stop use of stack system for direction.
[luatex-ja/luatexja.git] / src / ltj-direction.lua
1 --
2 -- src/ltj-direction.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('rmlgbm');    local ltjr = luatexja.rmlgbm
8 luatexja.direction = {}
9
10 local attr_dir = luatexbase.attributes['ltj@dir']
11 local attr_icflag = luatexbase.attributes['ltj@icflag']
12
13 local Dnode = node.direct or node
14 local nullfunc = function (n) return n end
15 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
16 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
17 local has_attr = Dnode.has_attribute
18 local set_attr = Dnode.set_attribute
19 local insert_before = Dnode.insert_before
20 local insert_after = Dnode.insert_after
21 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
22 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
23 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
24 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
25 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
26 local node_new = Dnode.new
27 local node_tail = Dnode.tail
28 local node_free = Dnode.free
29 local node_remove = Dnode.remove
30 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
31 local traverse_id = Dnode.traverse_id
32
33 local id_kern = node.id('kern')
34 local id_hlist = node.id('hlist')
35 local id_vlist = node.id('vlist')
36 local id_whatsit = node.id('whatsit')
37 local sid_save = node.subtype('pdf_save')
38 local sid_restore = node.subtype('pdf_restore')
39 local sid_matrix = node.subtype('pdf_setmatrix')
40 local sid_user = node.subtype('user_defined')
41
42 local PROCESSED    = luatexja.icflag_table.PROCESSED
43 local PACKED       = luatexja.icflag_table.PACKED
44 local DIR = luatexja.stack_table_index.DIR
45 local STCK = luatexja.userid_table.STCK
46 local wh_DIR = luatexja.userid_table.DIR
47 local dir_tate = 3
48 local dir_yoko = 4
49
50
51 local get_dir_count 
52 do
53    local gc = tex.getcount
54    get_dir_count = function() return gc('ltj@dir@count') end
55 end
56 luatexja.direction.get_dir_count = get_dir_count 
57
58 -- \tate, \yoko
59 do
60   local node_next = node.next
61   local function set_list_direction(v, name)
62      local lv, w = tex.nest[tex.nest.ptr], tex.lists.page_head
63      if lv.mode == 1 and w then
64         if w.id==id_whatsit and w.subtype==sid_user
65         and w.user_id==wh_DIR then
66            w.value=v
67         end
68      else
69         local w = node_next(to_direct(lv.head))
70         if to_node(w) then
71            if getid(w)==id_whatsit and getsubtype(w)==sid_user
72            and getfield('user_id', wh_DIR) then
73               setfield(w, 'value', v)
74            else
75               ltjb.package_error(
76                  'luatexja',
77                  "Use `\\" .. name .. "' at top of list",
78                  'Direction change command by LuaTeX-ja is available\n'
79                  .. 'only while current list is null.')
80            end
81         else
82            local w = node_new(id_whatsit, sid_user)
83            setfield(w, 'next', hd)
84            setfield(w, 'user_id', wh_DIR)
85            setfield(w, 'type', 100)
86            setfield(w, 'value', v)
87            Dnode.write(w)
88         end
89      end
90   end
91   luatexja.direction.set_list_direction = set_list_direction
92 end
93
94 -- ボックスに dir whatsit を追加
95 do
96    local tex_getcount = tex.getcount
97    local function set_dir_flag(h, gc)
98       if gc=='fin_row' or gc == 'preamble'  then
99          return h
100       else
101          local hd = to_direct(h)
102          local w
103          if hd and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
104          and getfield(hd, 'user_id')==wh_DIR then
105             return h
106          else
107             w = node_new(id_whatsit, sid_user)
108             setfield(w, 'next', hd)
109             setfield(w, 'user_id', wh_DIR)
110             setfield(w, 'type', 100)
111             setfield(w, 'value', ltjs.list_dir)
112             return to_node(w)
113          end
114       end
115    end
116    luatexbase.add_to_callback('hpack_filter', set_dir_flag, 'ltj.set_dir_flag', 10000)
117
118    local function set_dir_flag_vbox(h, gc)
119       local w = to_direct(h)
120       ltjs.list_dir = get_dir_count()
121       if getid(w)==id_whatsit and getsubtype(w)==sid_user
122       and getfield(w, 'user_id')==wh_DIR then
123          ltjs.list_dir = getfield(w, 'value')
124       --print('VB', gc, ltjs.list_dir)
125       --luatexja.ext_show_node_list(h, '   > ', print)
126          return h
127       else
128       --print('VB', gc, ltjs.list_dir)
129       --luatexja.ext_show_node_list(h, '   > ', print)
130          return set_dir_flag(h, gc)
131       end
132    end
133    luatexbase.add_to_callback('vpack_filter', set_dir_flag_vbox, 'ltj.set_dir_flag', 1)
134    luatexbase.add_to_callback('post_linebreak_filter',
135                               function (h)
136                                  local new_dir = ltjs.list_dir
137                                  for line in traverse_id(id_hlist, to_direct(h)) do
138                                     set_attr(line, attr_dir, new_dir)
139                                  end
140                                  return set_dir_flag(h, tostring(gc))
141                               end, 'ltj.set_dir_flag', 100)
142
143 end
144
145
146
147 local make_dir_node
148 do
149    local get_h =function (w,h,d) return h end
150    local get_d =function (w,h,d) return d end
151    local get_h_d =function (w,h,d) return h+d end
152    local get_h_neg =function (w,h,d) return -h end
153    local get_d_neg =function (w,h,d) return -d end
154    local get_w_half =function (w,h,d) return 0.5*w end
155    local get_w_neg_half =function (w,h,d) return -0.5*w end
156    local get_w_neg =function (w,h,d) return -w end
157    local get_w =function (w,h,d) return w end
158    local dir_node_aux = {
159       [dir_yoko] = { -- yoko を tate 中で組む
160          width  = get_h_d,
161          height = get_w_half,
162          depth  = get_w_half,
163          [id_hlist] = {
164             { 'kern', get_h },
165             { 'whatsit', sid_save },
166             { 'rotate', '0 1 -1 0' },
167             { 'kern', get_w_neg_half },
168             { 'box' },
169             { 'kern', get_w_neg_half },
170             { 'whatsit', sid_restore },
171          },
172          [id_vlist] = {
173             { 'kern', get_w},
174             { 'whatsit', sid_save },
175             { 'rotate', '0 1 -1 0' },
176             { 'box' },
177             { 'kern', get_h_neg},
178             { 'whatsit', sid_restore },
179          },
180       },
181       [dir_tate] = { -- tate を yoko 中で組む
182          width  = get_h_d,
183          height = get_w,
184          depth  = function() return 0 end,
185          [id_hlist] = {
186             { 'kern', get_d },
187             { 'whatsit', sid_save },
188             { 'rotate', '0 -1 1 0' },
189             { 'kern', get_w_neg },
190             { 'box' },
191             { 'whatsit', sid_restore },
192          },
193          [id_vlist] = {
194             { 'whatsit', sid_save },
195             { 'rotate', '0 -1 1 0' },
196             { 'kern', get_h_neg },
197             { 'kern', get_d_neg },
198             { 'box' },
199             { 'whatsit', sid_restore },
200          },
201       },
202    }
203
204    clean_dir_whatsit = function(b)
205       local bh = getlist(b)
206       local dir
207       for x in traverse_id(id_whatsit, bh) do
208          if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==wh_DIR then
209              setfield(b, 'head', (node_remove(bh, x)))
210              dir = getfield(bh, 'value')
211              set_attr(b, attr_dir, dir)
212              set_attr(b, attr_icflag, PROCESSED)
213              node_free(x); break
214          end
215       end
216       return dir
217    end
218    luatexja.direction.clean_dir_whatsit = clean_dir_whatsit
219    make_dir_node = function (head, b, new_dir, origin)
220       -- head: list head, b: box
221       -- origin: コール元 (for debug)
222       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
223       -- (new b): b か dir_node に被せられた b
224       local old_dir
225       local bh = getlist(b)
226       for x in traverse_id(id_whatsit, bh) do
227          if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==wh_DIR then
228              old_dir = getfield(bh, 'value')
229              set_attr(b, attr_dir, old_dir)
230              setfield(b, 'head', (node_remove(bh, x)))
231              node_free(x); break
232          end
233       end
234       if not old_dir then
235          old_dir = has_attr(b, attr_dir) or 0
236          if old_dir ==0 then
237             old_dir = ltjs.list_dir
238          end
239       end
240       if old_dir==new_dir then
241          set_attr(b, attr_icflag, PROCESSED)
242          return head, node_next(b), b, false
243       elseif  -old_dir == new_dir  then
244          return head, node_next(b), b, true
245       else
246          local nh, nb, ret, flag
247          if old_dir < 0 then
248             -- b itself is a dir node; just unwrap
249             local bc = node_next(node_next(
250                                     node_next(node_next(bh))))
251             node_remove(bh, bc);
252             nh, nb =  insert_before(head, b, bc), nil
253             nh, nb = node_remove(head, b)
254             setfield(b, 'next', nil); Dnode.flush_list(b)
255             ret, flag = bc, false
256          else
257             local bid = getid(b)
258             local db = node_new(bid) -- dir node
259             nh, nb =  insert_before(head, b, db), nil
260             nh, nb = node_remove(nh, b)
261             local w = getfield(b, 'width')
262             local h = getfield(b, 'height')
263             local d = getfield(b, 'depth')
264             local info = dir_node_aux[old_dir]
265             set_attr(db, attr_dir, -new_dir)
266             set_attr(b, attr_icflag, PROCESSED)
267             set_attr(db, attr_icflag, PROCESSED)
268             setfield(db, 'dir', getfield(b, 'dir'))
269             setfield(db, 'shift', 0)
270             setfield(db, 'width',  info.width(w,h,d))
271             setfield(db, 'height', info.height(w,h,d))
272             setfield(db, 'depth',  info.depth(w,h,d))
273             local db_head, db_tail  = nil
274             for _,v in ipairs(info[bid]) do
275                local cmd, arg, nn = v[1], v[2]
276                if cmd=='kern' then
277                   nn = node_new(id_kern)
278                   setfield(nn, 'kern', arg(w, h, d))
279                elseif cmd=='whatsit' then
280                   nn = node_new(id_whatsit, arg)
281                elseif cmd=='rotate' then
282                   nn = node_new(id_whatsit, sid_matrix)
283                   setfield(nn, 'data', arg)
284                elseif cmd=='box' then
285                   nn = b; setfield(b, 'next', nil)
286                end
287                if db_head then
288                   insert_after(db_head, db_tail, nn)
289                   db_tail = nn
290                else
291                   db_head, db_tail = nn, nn
292                end
293             end
294             setfield(db, 'head', db_head)
295             ret, flag = db, true
296          end
297          return nh, nb, ret, flag
298       end
299    end
300    local function process_dir_node(head, gc)
301       local h = to_direct(head)
302       local x, new_dir = h, ltjs.list_dir or dir_yoko
303       while x do
304          local xid = getid(x)
305          if (xid==id_hlist and has_attr(x, attr_icflag)~=PACKED) or xid==id_vlist then
306             h, x = make_dir_node(h, x, new_dir, 'process_dir_node:' .. gc)
307          else
308             x = node_next(x)
309          end
310       end
311       return to_node(h)
312    end
313    luatexja.direction.make_dir_node = make_dir_node
314    luatexbase.add_to_callback('vpack_filter',
315                               process_dir_node, 'ltj.dir_node', 10001)
316 end
317
318
319 -- 縦書き用字形への変換テーブル
320 local font_vert_table = {} -- key: fontnumber
321 do
322    local font_vert_basename = {} -- key: basename
323    local function add_feature_table(tname, src, dest)
324       for i,v in pairs(src) do
325          if type(v.slookups)=='table' then
326             local s = v.slookups[tname]
327             if s and not dest[i] then
328                dest[i] = s
329             end
330          end
331       end
332    end
333
334    local function prepare_vert_data(n, id)
335       -- test if already loaded
336       if type(id)=='number' then -- sometimes id is an integer
337          font_vert_table[n] = font_vert_table[id]; return
338       elseif (not id) or font_vert_table[n]  then return
339       end
340       local fname = id.filename
341       local bname = file.basename(fname)
342       if not fname then
343          font_vert_table[n] = {}; return
344       elseif font_vert_basename[bname] then
345          font_vert_table[n] = font_vert_basename[bname]; return
346       end
347       local vtable = {}
348       local a = id.resources.sequences
349       if a then
350          local s = id.shared.rawdata.descriptions
351          for i,v in pairs(a) do
352             if v.features.vert or v.features.vrt2 then
353                add_feature_table(v.subtables[1], s, vtable)
354             end
355          end
356       end
357       font_vert_basename[bname] = vtable
358       font_vert_table[n] = vtable
359    end
360    -- 縦書き用字形への変換
361    function luatexja.direction.get_vert_glyph(n, chr)
362       local fn = font_vert_table[n]
363       return fn and fn[chr] or chr
364    end
365    luatexbase.add_to_callback('luatexja.define_font',
366                               function (res, name, size, id)
367                                  prepare_vert_data(id, res)
368                               end,
369                               'prepare_vert_data', 1)
370
371    local function a (n, dat) font_vert_table[n] = dat end
372    luatexja.rmlgbm.vert_addfunc = a
373
374 end
375
376 -- PACKED の hbox から文字を取り出す
377 -- luatexja.jfmglue.check_box などで使用
378 do
379    local function glyph_from_packed(h)
380       local b = getlist(h)
381       return (getid(b)==id_kern) 
382          and node_next(node_next(node_next(node_next(b)))) or b
383    end
384    luatexja.direction.glyph_from_packed = glyph_from_packed
385 end