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
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')
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
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.')
60 ltjs.set_stack_table(luatexja.stack_table_index.DIR, v, true)
63 luatexja.direction.set_list_direction = set_list_direction
66 -- ボックスに dir whatsit を追加
68 local tex_getcount = tex.getcount
69 local function set_dir_flag(h, gc)
70 if gc=='fin_row' or gc == 'preamble' then
73 local hd, new_dir = to_direct(h), ltjs.table_current_stack[DIR]
75 if hd and getid(hd)==id_whatsit and getsubtype(hd)==sid_user
76 and getfield(hd, 'user_id')==wh_DIR then
79 w = node_new(id_whatsit, sid_user)
80 setfield(w, 'next', hd)
82 setfield(w, 'user_id', wh_DIR)
83 setfield(w, 'type', 100)
84 setfield(w, 'value', new_dir)
88 luatexbase.add_to_callback('hpack_filter', set_dir_flag, 'ltj.set_dir_flag', 10000)
89 luatexbase.add_to_callback('vpack_filter',
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
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',
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)
109 return set_dir_flag(h, tostring(gc))
110 end, 'ltj.set_dir_flag', 100)
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 中で組む
134 { 'whatsit', sid_save },
135 { 'rotate', '0 1 -1 0' },
136 { 'kern', get_w_neg_half },
138 { 'kern', get_w_neg_half },
139 { 'whatsit', sid_restore },
143 { 'whatsit', sid_save },
144 { 'rotate', '0 1 -1 0' },
146 { 'kern', get_h_neg},
147 { 'whatsit', sid_restore },
150 [dir_tate] = { -- tate を yoko 中で組む
153 depth = function() return 0 end,
156 { 'whatsit', sid_save },
157 { 'rotate', '0 -1 1 0' },
158 { 'kern', get_w_neg },
160 { 'whatsit', sid_restore },
163 { 'whatsit', sid_save },
164 { 'rotate', '0 -1 1 0' },
165 { 'kern', get_h_neg },
166 { 'kern', get_d_neg },
168 { 'whatsit', sid_restore },
173 clean_dir_whatsit = function(b)
174 local bh = getlist(b)
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)
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
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)
205 old_dir = has_attr(b, attr_dir) or dir_yoko
206 if old_dir==0 then old_dir =dir_yoko 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
214 local nh, nb, ret, flag
216 -- b itself is a dir node; just unwrap
217 local bc = node_next(node_next(
218 node_next(node_next(bh))))
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
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]
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)
256 insert_after(db_head, db_tail, nn)
259 db_head, db_tail = nn, nn
262 setfield(db, 'head', db_head)
265 return nh, nb, ret, flag
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
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)
281 luatexja.direction.make_dir_node = make_dir_node
282 luatexbase.add_to_callback('vpack_filter',
283 process_dir_node, 'ltj.dir_node', 10001)
288 local font_vert_table = {} -- key: fontnumber
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
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
308 local fname = id.filename
309 local bname = file.basename(fname)
311 font_vert_table[n] = {}; return
312 elseif font_vert_basename[bname] then
313 font_vert_table[n] = font_vert_basename[bname]; return
316 local a = id.resources.sequences
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)
325 font_vert_basename[bname] = vtable
326 font_vert_table[n] = vtable
329 function luatexja.direction.get_vert_glyph(n, chr)
330 local fn = font_vert_table[n]
331 return fn and fn[chr] or chr
333 luatexbase.add_to_callback('luatexja.define_font',
334 function (res, name, size, id)
335 prepare_vert_data(id, res)
337 'prepare_vert_data', 1)
339 local function a (n, dat) font_vert_table[n] = dat end
340 luatexja.rmlgbm.vert_addfunc = a
344 -- PACKED の hbox から文字を取り出す
345 -- luatexja.jfmglue.check_box などで使用
347 local function glyph_from_packed(h)
349 return (getid(b)==id_kern)
350 and node_next(node_next(node_next(node_next(b)))) or b
352 luatexja.direction.glyph_from_packed = glyph_from_packed