OSDN Git Service

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