2 -- src/ltj-direction.lua
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 = {}
10 local attr_dir = luatexbase.attributes['ltj@dir']
11 local attr_icflag = luatexbase.attributes['ltj@icflag']
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
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')
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
56 local gc = tex.getcount
57 get_dir_count = function() return gc('ltj@dir@count') end
59 luatexja.direction.get_dir_count = get_dir_count
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)
74 local w = node_next(to_direct(lv.head))
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)
83 "Use `\\" .. name .. "' at top of list",
84 'Direction change command by LuaTeX-ja is available\n'
85 .. 'only while current list is null.')
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)
94 tex.setattribute('global', attr_dir, 0)
96 tex.setattribute('global', attr_icflag, 0)
99 luatexja.direction.set_list_direction = set_list_direction
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)
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
122 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
123 tex.setattribute('global', attr_icflag, 0)
127 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
131 luatexbase.add_to_callback('hpack_filter',
132 create_dir_whatsit_hpack, 'ltj.create_dir_whatsit', 10000)
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)
141 elseif gc=='fin_row' or gc == 'preamble' then
143 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
144 tex.setattribute('global', attr_icflag, 0)
148 return to_node(create_dir_whatsit(hd, gc, ltjs.list_dir))
151 luatexbase.add_to_callback('vpack_filter',
152 create_dir_whatsit_vbox, 'ltj.create_dir_whatsit', 1)
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)
161 return to_node(create_dir_whatsit(hd, gc, new_dir))
163 luatexbase.add_to_callback('post_linebreak_filter',
164 create_dir_whatsit_parbox, 'ltj.create_dir_whatsit', 10000)
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
181 [dir_yoko] = { -- yoko を
182 [dir_tate] = { -- tate 中で組む
188 { 'whatsit', sid_save },
189 { 'rotate', '0 1 -1 0' },
190 { 'kern', get_w_neg_half },
192 { 'kern', get_w_neg_half },
193 { 'whatsit', sid_restore },
197 { 'whatsit', sid_save },
198 { 'rotate', '0 1 -1 0' },
200 { 'kern', get_h_neg},
201 { 'whatsit', sid_restore },
205 [dir_tate] = { -- tate を
206 [dir_yoko] = { -- yoko 中で組む
209 depth = function() return 0 end,
212 { 'whatsit', sid_save },
213 { 'rotate', '0 -1 1 0' },
214 { 'kern', get_w_neg },
216 { 'whatsit', sid_restore },
219 { 'whatsit', sid_save },
220 { 'rotate', '0 -1 1 0' },
221 { 'kern', get_h_neg },
222 { 'kern', get_d_neg },
224 { 'whatsit', sid_restore },
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)
236 if bh and getid(bh)==id_whatsit
237 and getsubtype(bh)==sid_user and getfield(bh, 'user_id')==DIR then
240 dir = has_attr(bh, attr_dir)
241 set_attr(b, attr_dir, dir)
242 tex.setattribute('global', attr_dir, 0)
245 return (dir==0 and default or dir), c
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))))
256 nh = insert_before(head, b, bc)
257 nh, nb = node_remove(nh, b)
259 setfield(b, 'list', nil); Dnode.flush_list(bh)
260 local d, wh = get_box_dir(bc, 0)
262 wh = create_dir_whatsit(getlist(bc), 'unwrap', d)
264 local h = getfield(wh, 'value')
265 if h then setfield(b, 'next', h) end
266 setfield(wh, 'value', to_node(b))
271 -- \wd, \ht, \dp の代わり
273 local getbox = tex.getbox
274 local function get_box_dim(key, n)
275 local gt = tex.globaldefs; tex.globaldefs = 0
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
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))
296 tex.setdimen('ltj@tempdima', getfield(s, key))
299 tex.setdimen('ltj@tempdima', 0)
303 luatexja.direction.get_box_dim = get_box_dim
304 local function set_box_dim(key)
305 local n = tex.getcount('ltj@tempcnta')
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')
315 setfield(s, key, tex.getdimen('ltj@tempdima'))
319 luatexja.direction.set_box_dim = set_box_dim
322 local make_dir_whatsit
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
338 local nh, nb, ret, flag
340 -- b itself is a dir node; just unwrap
341 nh, nb, ret = unwrap_dir_node(b,head)
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]
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)
375 insert_after(db_head, db_tail, nn)
378 db_head, db_tail = nn, nn
381 setfield(db, 'head', db_head)
384 return nh, nb, ret, flag
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
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)
400 stop_time_measure('direction_vpack')
403 luatexja.direction.make_dir_whatsit = make_dir_whatsit
404 luatexbase.add_to_callback('vpack_filter',
405 process_dir_node, 'ltj.dir_whatsit', 10001)
409 local font_vert_table = {} -- key: fontnumber
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
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
429 local fname = id.filename
430 local bname = file.basename(fname)
432 font_vert_table[n] = {}; return
433 elseif font_vert_basename[bname] then
434 font_vert_table[n] = font_vert_basename[bname]; return
437 local a = id.resources.sequences
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)
446 font_vert_basename[bname] = vtable
447 font_vert_table[n] = vtable
450 function luatexja.direction.get_vert_glyph(n, chr)
451 local fn = font_vert_table[n]
452 return fn and fn[chr] or chr
454 luatexbase.add_to_callback('luatexja.define_font',
455 function (res, name, size, id)
456 prepare_vert_data(id, res)
458 'prepare_vert_data', 1)
460 local function a (n, dat) font_vert_table[n] = dat end
461 luatexja.rmlgbm.vert_addfunc = a
465 -- PACKED の hbox から文字を取り出す
466 -- luatexja.jfmglue.check_box などで使用
468 local function glyph_from_packed(h)
470 return (getid(b)==id_kern)
471 and node_next(node_next(node_next(node_next(b)))) or b
473 luatexja.direction.glyph_from_packed = glyph_from_packed