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 PACKED = luatexja.icflag_table.PACKED
46 local DIR = luatexja.stack_table_index.DIR
47 local STCK = luatexja.userid_table.STCK
48 local wh_DIR = luatexja.userid_table.DIR
55 local gc = tex.getcount
56 get_dir_count = function() return gc('ltj@dir@count') end
58 luatexja.direction.get_dir_count = get_dir_count
62 local node_next = node.next
63 local function set_list_direction(v, name)
64 local lv, w = tex.nest[tex.nest.ptr], tex.lists.page_head
65 if lv.mode == 1 and w then
66 if w.id==id_whatsit and w.subtype==sid_user
67 and w.user_id==wh_DIR then
71 local w = node_next(to_direct(lv.head))
73 if getid(w)==id_whatsit and getsubtype(w)==sid_user
74 and getfield('user_id', wh_DIR) then
75 setfield(w, 'value', v)
79 "Use `\\" .. name .. "' at top of list",
80 'Direction change command by LuaTeX-ja is available\n'
81 .. 'only while current list is null.')
84 local w = node_new(id_whatsit, sid_user)
85 setfield(w, 'next', hd)
86 setfield(w, 'user_id', wh_DIR)
87 setfield(w, 'type', 100)
88 setfield(w, 'value', v)
93 luatexja.direction.set_list_direction = set_list_direction
96 -- ボックスに dir whatsit を追加
98 local tex_getcount = tex.getcount
99 local function set_dir_flag(h, gc)
100 if gc=='fin_row' or gc == 'preamble' then
103 local hd = to_direct(h)
105 if hd and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
106 and getfield(hd, 'user_id')==wh_DIR then
109 w = node_new(id_whatsit, sid_user)
110 setfield(w, 'next', hd)
111 setfield(w, 'user_id', wh_DIR)
112 setfield(w, 'type', 100)
113 setfield(w, 'value', ltjs.list_dir)
118 luatexbase.add_to_callback('hpack_filter', set_dir_flag, 'ltj.set_dir_flag', 10000)
119 local function set_dir_flag_vbox(h, gc)
120 local w = to_direct(h)
121 ltjs.list_dir = get_dir_count()
122 if getid(w)==id_whatsit and getsubtype(w)==sid_user
123 and getfield(w, 'user_id')==wh_DIR then
124 ltjs.list_dir = getfield(w, 'value')
127 return set_dir_flag(h, gc)
130 luatexbase.add_to_callback('vpack_filter', set_dir_flag_vbox, 'ltj.set_dir_flag', 1)
131 luatexbase.add_to_callback('post_linebreak_filter',
133 stop_time_measure('tex_linebreak')
134 -- start 側は ltj-debug.lua に
135 local new_dir = ltjs.list_dir
136 for line in traverse_id(id_hlist, to_direct(h)) do
137 set_attr(line, attr_dir, new_dir)
139 return set_dir_flag(h, tostring(gc))
140 end, 'ltj.set_dir_flag', 100)
148 local get_h =function (w,h,d) return h end
149 local get_d =function (w,h,d) return d end
150 local get_h_d =function (w,h,d) return h+d end
151 local get_h_neg =function (w,h,d) return -h end
152 local get_d_neg =function (w,h,d) return -d end
153 local get_w_half =function (w,h,d) return 0.5*w end
154 local get_w_neg_half =function (w,h,d) return -0.5*w end
155 local get_w_neg =function (w,h,d) return -w end
156 local get_w =function (w,h,d) return w end
157 local dir_node_aux = {
158 [dir_yoko] = { -- yoko を tate 中で組む
164 { 'whatsit', sid_save },
165 { 'rotate', '0 1 -1 0' },
166 { 'kern', get_w_neg_half },
168 { 'kern', get_w_neg_half },
169 { 'whatsit', sid_restore },
173 { 'whatsit', sid_save },
174 { 'rotate', '0 1 -1 0' },
176 { 'kern', get_h_neg},
177 { 'whatsit', sid_restore },
180 [dir_tate] = { -- tate を yoko 中で組む
183 depth = function() return 0 end,
186 { 'whatsit', sid_save },
187 { 'rotate', '0 -1 1 0' },
188 { 'kern', get_w_neg },
190 { 'whatsit', sid_restore },
193 { 'whatsit', sid_save },
194 { 'rotate', '0 -1 1 0' },
195 { 'kern', get_h_neg },
196 { 'kern', get_d_neg },
198 { 'whatsit', sid_restore },
203 clean_dir_whatsit = function(b)
204 local bh = getlist(b)
206 for x in traverse_id(id_whatsit, bh) do
207 if getsubtype(x)==sid_user and getfield(x, 'user_id')==wh_DIR then
208 setfield(b, 'head', (node_remove(bh, x)))
209 dir = getfield(bh, 'value')
210 set_attr(b, attr_dir, dir)
211 set_attr(b, attr_icflag, PROCESSED)
217 luatexja.direction.clean_dir_whatsit = clean_dir_whatsit
218 make_dir_node = function (head, b, new_dir, origin)
220 -- head: list head, b: box
221 -- origin: コール元 (for debug)
222 -- return value: (new head), (next of b), (new b), (is_b_dir_node)
223 -- (new b): b か dir_node に被せられた b
224 local old_dir = has_attr(b, attr_dir) or 0
225 local bh = getlist(b)
227 if getsubtype(bh)==sid_user and getfield(bh, 'user_id')==wh_DIR then
228 old_dir = getfield(bh, 'value')
229 set_attr(b, attr_dir, old_dir)
230 setfield(b, 'head', (node_remove(bh, bh)))
232 if old_dir ==0 then old_dir = ltjs.list_dir end
234 if old_dir==new_dir then
235 return head, node_next(b), b, false
236 elseif -old_dir == new_dir then
237 return head, node_next(b), b, true
239 local nh, nb, ret, flag
241 -- b itself is a dir node; just unwrap
242 local bc = node_next(node_next(
243 node_next(node_next(bh))))
245 nh, nb = insert_before(head, b, bc), nil
246 nh, nb = node_remove(head, b)
247 setfield(b, 'next', nil); Dnode.flush_list(b)
248 ret, flag = bc, false
251 local db = node_new(bid) -- dir node
252 nh, nb = insert_before(head, b, db), nil
253 nh, nb = node_remove(nh, b)
254 local w = getfield(b, 'width')
255 local h = getfield(b, 'height')
256 local d = getfield(b, 'depth')
257 local info = dir_node_aux[old_dir]
258 set_attr(db, attr_dir, -new_dir)
259 set_attr(b, attr_icflag, PROCESSED)
260 set_attr(db, attr_icflag, PROCESSED)
261 setfield(db, 'dir', getfield(b, 'dir'))
262 setfield(db, 'shift', 0)
263 setfield(db, 'width', info.width(w,h,d))
264 setfield(db, 'height', info.height(w,h,d))
265 setfield(db, 'depth', info.depth(w,h,d))
266 local db_head, db_tail = nil
267 for _,v in ipairs(info[bid]) do
268 local cmd, arg, nn = v[1], v[2]
270 nn = node_new(id_kern)
271 setfield(nn, 'kern', arg(w, h, d))
272 elseif cmd=='whatsit' then
273 nn = node_new(id_whatsit, arg)
274 elseif cmd=='rotate' then
275 nn = node_new(id_whatsit, sid_matrix)
276 setfield(nn, 'data', arg)
277 elseif cmd=='box' then
278 nn = b; setfield(b, 'next', nil)
281 insert_after(db_head, db_tail, nn)
284 db_head, db_tail = nn, nn
287 setfield(db, 'head', db_head)
290 return nh, nb, ret, flag
293 local function process_dir_node(head, gc)
294 start_time_measure('direction_vpack')
295 local h = to_direct(head)
296 local x, new_dir = h, ltjs.list_dir or dir_yoko
299 if (xid==id_hlist and has_attr(x, attr_icflag)~=PACKED) or xid==id_vlist then
300 h, x = make_dir_node(h, x, new_dir, 'process_dir_node:' .. gc)
305 stop_time_measure('direction_vpack')
308 luatexja.direction.make_dir_node = make_dir_node
309 luatexbase.add_to_callback('vpack_filter',
310 process_dir_node, 'ltj.dir_node', 10001)
316 local font_vert_table = {} -- key: fontnumber
318 local font_vert_basename = {} -- key: basename
319 local function add_feature_table(tname, src, dest)
320 for i,v in pairs(src) do
321 if type(v.slookups)=='table' then
322 local s = v.slookups[tname]
323 if s and not dest[i] then
330 local function prepare_vert_data(n, id)
331 -- test if already loaded
332 if type(id)=='number' then -- sometimes id is an integer
333 font_vert_table[n] = font_vert_table[id]; return
334 elseif (not id) or font_vert_table[n] then return
336 local fname = id.filename
337 local bname = file.basename(fname)
339 font_vert_table[n] = {}; return
340 elseif font_vert_basename[bname] then
341 font_vert_table[n] = font_vert_basename[bname]; return
344 local a = id.resources.sequences
346 local s = id.shared.rawdata.descriptions
347 for i,v in pairs(a) do
348 if v.features.vert or v.features.vrt2 then
349 add_feature_table(v.subtables[1], s, vtable)
353 font_vert_basename[bname] = vtable
354 font_vert_table[n] = vtable
357 function luatexja.direction.get_vert_glyph(n, chr)
358 local fn = font_vert_table[n]
359 return fn and fn[chr] or chr
361 luatexbase.add_to_callback('luatexja.define_font',
362 function (res, name, size, id)
363 prepare_vert_data(id, res)
365 'prepare_vert_data', 1)
367 local function a (n, dat) font_vert_table[n] = dat end
368 luatexja.rmlgbm.vert_addfunc = a
372 -- PACKED の hbox から文字を取り出す
373 -- luatexja.jfmglue.check_box などで使用
375 local function glyph_from_packed(h)
377 return (getid(b)==id_kern)
378 and node_next(node_next(node_next(node_next(b)))) or b
380 luatexja.direction.glyph_from_packed = glyph_from_packed