2 -- src/ltj-setwidth.lua
5 luatexja.load_module('base'); local ltjb = luatexja.base
6 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
8 local Dnode = node.direct or node
10 local setfield = (Dnode == node.direct) and Dnode.setfield or function(n, i, c) n[i] = c end
11 local getfield = (Dnode == node.direct) and Dnode.getfield or function(n, i) return n[i] end
12 local getid = (Dnode == node.direct) and Dnode.getid or function(n) return n.id end
13 local getfont = (Dnode == node.direct) and Dnode.getfont or function(n) return n.font end
14 local getlist = (Dnode == node.direct) and Dnode.getlist or function(n) return n.head end
15 local getchar = (Dnode == node.direct) and Dnode.getlist or function(n) return n.char end
16 local getsubtype = (Dnode == node.direct) and Dnode.getlist or function(n) return n.subtype end
18 local node_traverse = Dnode.traverse
19 local node_new = Dnode.new
20 local node_remove = Dnode.remove
21 local node_tail = Dnode.tail
22 local node_next = Dnode.getnext
23 local has_attr = Dnode.has_attribute
24 local set_attr = Dnode.set_attribute
25 local node_insert_before = Dnode.insert_before
26 local node_insert_after = Dnode.insert_after
27 local round = tex.round
29 local id_glyph = node.id('glyph')
30 local id_kern = node.id('kern')
31 local id_hlist = node.id('hlist')
32 local id_vlist = node.id('vlist')
33 local id_rule = node.id('rule')
34 local id_math = node.id('math')
36 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
37 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
38 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
39 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
40 local attr_icflag = luatexbase.attributes['ltj@icflag']
42 local ltjf_font_metric_table = ltjf.font_metric_table
44 local PACKED = luatexja.icflag_table.PACKED
45 local PROCESSED = luatexja.icflag_table.PROCESSED
46 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
47 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
49 local get_pr_begin_flag
51 local floor = math.floor
52 get_pr_begin_flag = function (p)
53 local i = has_attr(p, attr_icflag) or 0
54 return i - i%PROCESSED_BEGIN_FLAG
59 luatexja.setwidth = ltjw
61 luatexbase.create_callback("luatexja.set_width", "data",
62 function (fstable, fmtable, jchar_class)
65 local call_callback = luatexbase.call_callback
67 local fshift = { down = 0, left = 0}
69 -- mode: true iff p will be always encapsuled by a hbox
70 local function capsule_glyph(p, dir, mode, met, class)
71 local char_data = met.char_type[class]
72 if not char_data then return node_next(p) end
73 local fwidth, pwidth = char_data.width, getfield(p, 'width')
74 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
75 fshift.down = char_data.down; fshift.left = char_data.left
76 fshift = call_callback("luatexja.set_width", fshift, met, class)
77 local fheight, fdepth = char_data.height, char_data.depth
78 if (mode or pwidth ~= fwidth or getfield(p, 'height') ~= fheight or getfield(p, 'depth') ~= fdepth) then
80 = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
82 ltjw.head, q = node_remove(ltjw.head, p)
83 setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
85 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left
86 + (((ca=='right') and fwidth - pwidth) or round((fwidth - pwidth)*0.5)))
88 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
90 local box = node_new(id_hlist);
91 setfield(box, 'width', fwidth)
92 setfield(box, 'height', fheight)
93 setfield(box, 'depth', fdepth)
94 setfield(box, 'head', p)
95 setfield(box, 'shift', y_shift)
96 setfield(box, 'dir', dir or 'TLT')
97 set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
98 ltjw.head = q and node_insert_before(ltjw.head, q, box)
99 or node_insert_after(ltjw.head, node_tail(ltjw.head), box)
102 set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
103 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
104 setfield(p, 'yoffset', getfield(p, 'yoffset') - (has_attr(p, attr_ykblshift) or 0) - fshift.down)
108 luatexja.setwidth.capsule_glyph = capsule_glyph
110 function luatexja.setwidth.set_ja_width(ahead, dir)
111 local p = ahead; ltjw.head = p
112 local m = false -- is in math mode?
116 and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
117 local pf = getfont(p)
118 if pf == has_attr(p, attr_curjfnt) then
119 p = capsule_glyph(p, dir, false, ltjf_font_metric_table[pf],
120 has_attr(p, attr_jchar_class))
122 set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
123 setfield(p, 'yoffset',
124 getfield(p, 'yoffset') - (has_attr(p,attr_yablshift) or 0))
127 elseif pid==id_math then
128 m = (getsubtype(p)==0); p = node_next(p)
132 if pid==id_hlist or pid==id_vlist then
133 if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
134 setfield(p, 'shift', getfield(p, 'shift') + (has_attr(p,attr_yablshift) or 0))
136 elseif pid==id_rule then
137 if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
138 local v = has_attr(p,attr_yablshift) or 0
139 setfield(p, 'height', getfield(p, 'height')-v)
140 setfield(p, 'depth', getfield(p, 'depth')+v)
141 set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
148 -- adjust attr_icflag
149 tex.setattribute('global', attr_icflag, 0)