OSDN Git Service

Merge branch 'kmaeda_tarticle' into kitagawa_tfont
[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 do
51   local node_next = node.next
52   local function set_list_direction(v, name)
53     if node.next(tex.nest[tex.nest.ptr].head) then
54       ltjb.package_error('luatexja',
55                          "Use `\\" .. name .. "' at top of list",
56                          'Direction change command by LuaTeX-ja is available\n'
57                             .. 'only while current list is null.')
58     else
59        ltjs.set_stack_table(luatexja.stack_table_index.DIR, v, true)
60     end
61   end
62   luatexja.direction.set_list_direction = set_list_direction
63 end
64
65 do
66    local tex_getcount = tex.getcount
67    local function set_dir_flag(h, gc)
68       if gc=='fin_row' or gc == 'preamble'  then
69          return h
70       else
71          local hd, new_dir = to_direct(h), ltjs.table_current_stack[DIR]
72          local w
73          if hd and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
74          and getfield(hd, 'user_id')==wh_DIR then
75             w = hd
76          else
77             w = node_new(id_whatsit, sid_user)
78             setfield(w, 'next', hd)
79          end
80          setfield(w, 'user_id', wh_DIR)
81          setfield(w, 'type', 100)
82          setfield(w, 'value', new_dir)
83          return to_node(w)
84       end
85    end
86    luatexbase.add_to_callback('hpack_filter', set_dir_flag, 'ltj.set_dir_flag', 10000)
87    luatexbase.add_to_callback('vpack_filter',
88                               function (h, gc)
89                                  local box_set, cl = 0, tex.currentgrouplevel + 1
90                                  local hd = to_direct(h)
91                                  for w in traverse_id(id_whatsit, hd) do
92                                     if getsubtype(w)==sid_user and
93                                     getfield(w, 'user_id')==STCK and
94                                     getfield(w, 'value')==cl then box_set = 1;
95                                        hd = node_remove(hd, w); node_free(w); break
96                                     end
97                                  end
98                                 ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
99                                 return set_dir_flag(to_node(hd), gc)
100                               end, 'ltj.set_dir_flag', 1)
101    luatexbase.add_to_callback('post_linebreak_filter',
102                               function (h)
103                                  local new_dir = ltjs.table_current_stack[DIR]
104                                  for line in traverse_id(id_hlist, to_direct(h)) do
105                                     set_attr(line, attr_dir, new_dir)
106                                  end
107                                  return set_dir_flag(h, tostring(gc))
108                               end, 'ltj.set_dir_flag', 100)
109
110 end
111
112 local make_dir_node
113 do
114    local get_h =function (w,h,d) return h end
115    local get_d =function (w,h,d) return d end
116    local get_h_d =function (w,h,d) return h+d end
117    local get_h_neg =function (w,h,d) return -h end
118    local get_d_neg =function (w,h,d) return -d end
119    local get_w_half =function (w,h,d) return 0.5*w end
120    local get_w_neg_half =function (w,h,d) return -0.5*w end
121    local get_w_neg =function (w,h,d) return -w end
122    local get_w =function (w,h,d) return w end
123    local dir_node_aux = {
124       [dir_yoko] = { -- yoko を tate 中で組む
125          width  = get_h_d,
126          height = get_w_half,
127          depth  = get_w_half,
128          [id_hlist] = {
129             { 'kern', get_h },
130             { 'whatsit', sid_save },
131             { 'rotate', '0 1 -1 0' },
132             { 'kern', get_w_neg_half },
133             { 'box' },
134             { 'kern', get_w_neg_half },
135             { 'whatsit', sid_restore },
136          },
137          [id_vlist] = {
138             { 'kern', get_w},
139             { 'whatsit', sid_save },
140             { 'rotate', '0 1 -1 0' },
141             { 'box' },
142             { 'kern', get_h_neg},
143             { 'whatsit', sid_restore },
144          },
145       },
146       [dir_tate] = { -- tate を yoko 中で組む
147          width  = get_h_d,
148          height = get_w,
149          depth  = function() return 0 end,
150          [id_hlist] = {
151             { 'kern', get_d },
152             { 'whatsit', sid_save },
153             { 'rotate', '0 -1 1 0' },
154             { 'kern', get_w_neg },
155             { 'box' },
156             { 'whatsit', sid_restore },
157          },
158          [id_vlist] = {
159             { 'whatsit', sid_save },
160             { 'rotate', '0 -1 1 0' },
161             { 'kern', get_h_neg },
162             { 'kern', get_d_neg },
163             { 'box' },
164             { 'whatsit', sid_restore },
165          },
166       },
167    }
168
169    remove_dir_whatsit = function (head, b)
170       local bh = getlist(b)
171       if bh and getid(bh)==id_whatsit and getsubtype(bh)==sid_user
172           and getfield(bh, 'user_id')==wh_DIR then
173              setfield(b, 'head', node_next(bh))
174              set_attr(b, attr_icflag, PROCESSED)
175       end
176       return head, node_next(b)
177    end
178    make_dir_node = function (head, b, new_dir, origin)
179       -- head: list head, b: box
180       -- origin: コール元 (for debug)
181       -- return value: (new head), (next of b), (new b), (is_b_dir_node)
182       -- (new b): b か dir_node に被せられた b
183       local old_dir= nil
184       local bh = getlist(b)
185       for x in traverse_id(id_whatsit, bh) do
186          if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==wh_DIR then
187              old_dir = getfield(bh, 'value')
188              setfield(b, 'head', (node_remove(bh, x)))
189              set_attr(b, attr_icflag, PROCESSED)
190              node_free(x); break
191          end
192       end
193       if not old_dir then
194          old_dir = has_attr(b, attr_dir) or dir_yoko
195          if old_dir==0 then old_dir =dir_yoko end
196       end
197       if old_dir==new_dir then
198          set_attr(b, attr_icflag, PROCESSED)
199          return head, node_next(b), b, false
200       elseif  -old_dir == new_dir  then
201          return head, node_next(b), b, true
202       else
203          local nh, nb, ret, flag
204          if old_dir < 0 then
205             -- b itself is a dir node; just unwrap
206             local bc = node_next(node_next(
207                                     node_next(node_next(bh))))
208             node_remove(bh, bc);
209             nh, nb =  insert_before(head, b, bc), nil
210             nh, nb = node_remove(head, b)
211             setfield(b, 'next', nil); Dnode.flush_list(b)
212             ret, flag = bc, false
213          else
214             local bid = getid(b)
215             local db = node_new(bid) -- dir node
216             nh, nb =  insert_before(head, b, db), nil
217             nh, nb = node_remove(nh, b)
218             local w = getfield(b, 'width')
219             local h = getfield(b, 'height')
220             local d = getfield(b, 'depth')
221             local info = dir_node_aux[old_dir]
222             set_attr(db, attr_dir, -new_dir)
223             set_attr(b, attr_icflag, PROCESSED)
224             set_attr(db, attr_icflag, PROCESSED)
225             setfield(db, 'dir', getfield(b, 'dir'))
226             setfield(db, 'shift', 0)
227             setfield(db, 'width',  info.width(w,h,d))
228             setfield(db, 'height', info.height(w,h,d))
229             setfield(db, 'depth',  info.depth(w,h,d))
230             local db_head, db_tail  = nil
231             for _,v in ipairs(info[bid]) do
232                local cmd, arg, nn = v[1], v[2]
233                if cmd=='kern' then
234                   nn = node_new(id_kern)
235                   setfield(nn, 'kern', arg(w, h, d))
236                elseif cmd=='whatsit' then
237                   nn = node_new(id_whatsit, arg)
238                elseif cmd=='rotate' then
239                   nn = node_new(id_whatsit, sid_matrix)
240                   setfield(nn, 'data', arg)
241                elseif cmd=='box' then
242                   nn = b; setfield(b, 'next', nil)
243                end
244                if db_head then
245                   insert_after(db_head, db_tail, nn)
246                   db_tail = nn
247                else
248                   db_head, db_tail = nn, nn
249                end
250             end
251             setfield(db, 'head', db_head)
252             ret, flag = db, true
253          end
254          return nh, nb, ret, flag
255       end
256    end
257    local function process_dir_node(head, gc)
258       local h = to_direct(head)
259       local x, new_dir = h, ltjs.table_current_stack[DIR] or dir_yoko
260       while x do
261          local xid = getid(x)
262          if (xid==id_hlist and has_attr(x, attr_icflag)~=PACKED) or xid==id_vlist then
263             h, x = make_dir_node(h, x, new_dir, 'process_dir_node:' .. gc)
264          else
265             x = node_next(x)
266          end
267       end
268       return to_node(h)
269    end
270    luatexja.direction.make_dir_node = make_dir_node
271    luatexbase.add_to_callback('vpack_filter',
272                               process_dir_node, 'ltj.dir_node', 10001)
273 end
274
275 local font_vert_table = {} -- key: fontnumber
276 do
277    local font_vert_basename = {} -- key: basename
278    local function add_feature_table(tname, src, dest)
279       for i,v in pairs(src) do
280          if type(v.slookups)=='table' then
281             local s = v.slookups[tname]
282             if s and not dest[i] then
283                dest[i] = s
284             end
285          end
286       end
287    end
288
289    local function prepare_vert_data(n, id)
290       -- test if already loaded
291       if type(id)=='number' then -- sometimes id is an integer
292          font_vert_table[n] = font_vert_table[id]; return
293       elseif (not id) or font_vert_table[n]  then return
294       end
295
296       local fname = id.filename
297       local bname = file.basename(fname)
298       if not fname then
299          font_vert_table[n] = {}; return
300       elseif font_vert_basename[bname] then
301          font_vert_table[n] = font_vert_basename[bname]; return
302       end
303
304       local vtable = {}
305       local a = id.resources.sequences
306       if a then
307          local s = id.shared.rawdata.descriptions
308          for i,v in pairs(a) do
309             if v.features.vert or v.features.vrt2 then
310                add_feature_table(v.subtables[1], s, vtable)
311             end
312          end
313       end
314       font_vert_basename[bname] = vtable
315       font_vert_table[n] = vtable
316    end
317
318    function luatexja.direction.get_vert_glyph(n, chr)
319       local fn = font_vert_table[n]
320       return fn and fn[chr] or chr
321    end
322
323    luatexbase.add_to_callback('luatexja.define_font',
324                               function (res, name, size, id)
325                                  prepare_vert_data(id, res)
326                               end,
327                               'prepare_vert_data', 1)
328
329    local function a (n, dat) font_vert_table[n] = dat end
330    luatexja.rmlgbm.vert_addfunc = a
331
332 end