OSDN Git Service

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