OSDN Git Service

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