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_id = Dnode.traverse_id
32 local start_time_measure, stop_time_measure
33 = ltjb.start_time_measure, ltjb.stop_time_measure
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')
44 local PROCESSED = luatexja.icflag_table.PROCESSED
45 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
46 local PACKED = luatexja.icflag_table.PACKED
47 local DIR = luatexja.stack_table_index.DIR
48 local STCK = luatexja.userid_table.STCK
49 local wh_DIR = luatexja.userid_table.DIR
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==wh_DIR then
73 local w = node_next(to_direct(lv.head))
75 if getid(w)==id_whatsit and getsubtype(w)==sid_user
76 and getfield(w, 'user_id')==wh_DIR then
77 setfield(w, 'value', v)
81 "Use `\\" .. name .. "' at top of list",
82 'Direction change command by LuaTeX-ja is available\n'
83 .. 'only while current list is null.')
86 local w = node_new(id_whatsit, sid_user)
87 setfield(w, 'next', hd)
88 setfield(w, 'user_id', wh_DIR)
89 setfield(w, 'type', 100)
90 setfield(w, 'value', v)
95 luatexja.direction.set_list_direction = set_list_direction
98 -- ボックスに dir whatsit を追加
100 local tex_getcount = tex.getcount
101 local function set_dir_flag(h, gc)
102 if gc=='fin_row' or gc == 'preamble' then
103 local hd = to_direct(h)
105 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
106 tex.setattribute('global', attr_icflag, 0)
110 local hd = to_direct(h)
111 if hd and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
112 and getfield(hd, 'user_id')==wh_DIR then
113 set_attr(hd, attr_icflag, PROCESSED_BEGIN_FLAG)
114 tex.setattribute('global', attr_icflag, 0)
117 set_attr(hd, attr_icflag, (has_attr(hd, attr_icflag) or 0) + PROCESSED_BEGIN_FLAG)
118 tex.setattribute('global', attr_icflag, 0)
122 local w = node_new(id_whatsit, sid_user)
123 setfield(w, 'next', hd)
124 setfield(w, 'user_id', wh_DIR)
125 setfield(w, 'type', 100)
126 setfield(w, 'value', ltjs.list_dir)
127 set_attr(w, attr_icflag, PROCESSED_BEGIN_FLAG)
128 set_attr(hd, attr_icflag, (has_attr(hd, attr_icflag) or 0) + PROCESSED_BEGIN_FLAG)
129 tex.setattribute('global', attr_icflag, 0)
134 luatexbase.add_to_callback('hpack_filter', set_dir_flag, 'ltj.set_dir_flag', 10000)
135 local function set_dir_flag_vbox(h, gc)
136 local w = to_direct(h)
137 ltjs.list_dir = get_dir_count()
138 if getid(w)==id_whatsit and getsubtype(w)==sid_user
139 and getfield(w, 'user_id')==wh_DIR then
140 ltjs.list_dir = getfield(w, 'value')
143 return set_dir_flag(h, gc)
146 luatexbase.add_to_callback('vpack_filter', set_dir_flag_vbox, 'ltj.set_dir_flag', 1)
147 luatexbase.add_to_callback('post_linebreak_filter',
149 stop_time_measure('tex_linebreak')
150 -- start 側は ltj-debug.lua に
151 local new_dir = ltjs.list_dir
152 for line in traverse_id(id_hlist, to_direct(h)) do
153 set_attr(line, attr_dir, new_dir)
155 return set_dir_flag(h, gc)
156 end, 'ltj.set_dir_flag', 100)
163 local get_h =function (w,h,d) return h end
164 local get_d =function (w,h,d) return d end
165 local get_h_d =function (w,h,d) return h+d end
166 local get_h_neg =function (w,h,d) return -h end
167 local get_d_neg =function (w,h,d) return -d end
168 local get_w_half =function (w,h,d) return 0.5*w end
169 local get_w_neg_half =function (w,h,d) return -0.5*w end
170 local get_w_neg =function (w,h,d) return -w end
171 local get_w =function (w,h,d) return w end
173 [dir_yoko] = { -- yoko を tate 中で組む
179 { 'whatsit', sid_save },
180 { 'rotate', '0 1 -1 0' },
181 { 'kern', get_w_neg_half },
183 { 'kern', get_w_neg_half },
184 { 'whatsit', sid_restore },
188 { 'whatsit', sid_save },
189 { 'rotate', '0 1 -1 0' },
191 { 'kern', get_h_neg},
192 { 'whatsit', sid_restore },
195 [dir_tate] = { -- tate を yoko 中で組む
198 depth = function() return 0 end,
201 { 'whatsit', sid_save },
202 { 'rotate', '0 -1 1 0' },
203 { 'kern', get_w_neg },
205 { 'whatsit', sid_restore },
208 { 'whatsit', sid_save },
209 { 'rotate', '0 -1 1 0' },
210 { 'kern', get_h_neg },
211 { 'kern', get_d_neg },
213 { 'whatsit', sid_restore },
219 local function clean_dir_whatsit(b)
220 local bh = getlist(b)
221 local dir = has_attr(b, attr_dir)
222 for x in traverse_id(id_whatsit, bh) do
223 if getsubtype(x)==sid_user and getfield(x, 'user_id')==wh_DIR then
224 local nh = node_remove(bh, x)
225 setfield(b, 'head', nh)
226 dir = getfield(bh, 'value')
227 set_attr(b, attr_dir, dir)
233 luatexja.direction.clean_dir_whatsit = clean_dir_whatsit
235 -- \wd, \ht, \dp の代わり
237 local getbox = tex.getbox
238 local function get_box_dim(key, n)
239 local gt = tex.globaldefs; tex.globaldefs = 0
243 local s_dir, l_dir = clean_dir_whatsit(s), get_dir_count()
244 if s_dir ~= l_dir then
245 local w = getfield(s, 'width')
246 local h = getfield(s, 'height')
247 local d = getfield(s, 'depth')
248 tex.setdimen('ltj@tempdima', dir_node_aux[s_dir][key](w,h,d))
250 tex.setdimen('ltj@tempdima', getfield(s, key))
253 tex.setdimen('ltj@tempdima', 0)
257 luatexja.direction.get_box_dim = get_box_dim
258 local function set_box_dim(key)
259 local n = tex.getcount('ltj@tempcnta')
263 local s_dir, l_dir = clean_dir_whatsit(s), get_dir_count()
264 if s_dir ~= l_dir then
265 print('setting ' .. key .. ' of different direction box is not supported')
267 setfield(s, key, tex.getdimen('ltj@tempdima'))
275 make_dir_node = function (head, b, new_dir, origin)
276 -- head: list head, b: box
277 -- origin: コール元 (for debug)
278 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
279 -- (new b): b か dir_node に被せられた b
280 local old_dir = has_attr(b, attr_dir) or 0
281 local bh = getlist(b)
283 if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==wh_DIR then
284 old_dir = getfield(bh, 'value')
285 set_attr(b, attr_dir, old_dir)
286 local nh = node_remove(bh, bh)
287 setfield(b, 'head', nh)
289 if old_dir ==0 then old_dir = ltjs.list_dir end
291 if old_dir==new_dir then
292 return head, node_next(b), b, false
293 elseif -old_dir == new_dir then
294 return head, node_next(b), b, true
296 local nh, nb, ret, flag
298 -- b itself is a dir node; just unwrap
299 local bc = node_next(node_next(
300 node_next(node_next(bh))))
302 nh, nb = insert_before(head, b, bc), nil
303 nh, nb = node_remove(head, b)
304 setfield(b, 'next', nil); Dnode.flush_list(b)
305 ret, flag = bc, false
308 local db = node_new(bid) -- dir node
309 nh, nb = insert_before(head, b, db), nil
310 nh, nb = node_remove(nh, b)
311 local w = getfield(b, 'width')
312 local h = getfield(b, 'height')
313 local d = getfield(b, 'depth')
314 local info = dir_node_aux[old_dir]
315 set_attr(db, attr_dir, -new_dir)
316 set_attr(b, attr_icflag, PROCESSED)
317 set_attr(db, attr_icflag, PROCESSED)
318 setfield(db, 'dir', getfield(b, 'dir'))
319 setfield(db, 'shift', 0)
320 setfield(db, 'width', info.width(w,h,d))
321 setfield(db, 'height', info.height(w,h,d))
322 setfield(db, 'depth', info.depth(w,h,d))
323 local db_head, db_tail = nil
324 for _,v in ipairs(info[bid]) do
325 local cmd, arg, nn = v[1], v[2]
327 nn = node_new(id_kern)
328 setfield(nn, 'kern', arg(w, h, d))
329 elseif cmd=='whatsit' then
330 nn = node_new(id_whatsit, arg)
331 elseif cmd=='rotate' then
332 nn = node_new(id_whatsit, sid_matrix)
333 setfield(nn, 'data', arg)
334 elseif cmd=='box' then
335 nn = b; setfield(b, 'next', nil)
338 insert_after(db_head, db_tail, nn)
341 db_head, db_tail = nn, nn
344 setfield(db, 'head', db_head)
347 return nh, nb, ret, flag
350 local function process_dir_node(head, gc)
351 start_time_measure('direction_vpack')
352 local h = to_direct(head)
353 local x, new_dir = h, ltjs.list_dir or dir_yoko
356 if (xid==id_hlist and has_attr(x, attr_icflag)%PROCESSED_BEGIN_FLAG~=PACKED)
357 or xid==id_vlist then
358 h, x = make_dir_node(h, x, new_dir, 'process_dir_node:' .. gc)
363 stop_time_measure('direction_vpack')
366 luatexja.direction.make_dir_node = make_dir_node
367 luatexbase.add_to_callback('vpack_filter',
368 process_dir_node, 'ltj.dir_node', 10001)
372 local font_vert_table = {} -- key: fontnumber
374 local font_vert_basename = {} -- key: basename
375 local function add_feature_table(tname, src, dest)
376 for i,v in pairs(src) do
377 if type(v.slookups)=='table' then
378 local s = v.slookups[tname]
379 if s and not dest[i] then
386 local function prepare_vert_data(n, id)
387 -- test if already loaded
388 if type(id)=='number' then -- sometimes id is an integer
389 font_vert_table[n] = font_vert_table[id]; return
390 elseif (not id) or font_vert_table[n] then return
392 local fname = id.filename
393 local bname = file.basename(fname)
395 font_vert_table[n] = {}; return
396 elseif font_vert_basename[bname] then
397 font_vert_table[n] = font_vert_basename[bname]; return
400 local a = id.resources.sequences
402 local s = id.shared.rawdata.descriptions
403 for i,v in pairs(a) do
404 if v.features.vert or v.features.vrt2 then
405 add_feature_table(v.subtables[1], s, vtable)
409 font_vert_basename[bname] = vtable
410 font_vert_table[n] = vtable
413 function luatexja.direction.get_vert_glyph(n, chr)
414 local fn = font_vert_table[n]
415 return fn and fn[chr] or chr
417 luatexbase.add_to_callback('luatexja.define_font',
418 function (res, name, size, id)
419 prepare_vert_data(id, res)
421 'prepare_vert_data', 1)
423 local function a (n, dat) font_vert_table[n] = dat end
424 luatexja.rmlgbm.vert_addfunc = a
428 -- PACKED の hbox から文字を取り出す
429 -- luatexja.jfmglue.check_box などで使用
431 local function glyph_from_packed(h)
433 return (getid(b)==id_kern)
434 and node_next(node_next(node_next(node_next(b)))) or b
436 luatexja.direction.glyph_from_packed = glyph_from_packed