OSDN Git Service

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