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
9 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
10 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
11 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
12 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
13 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
14 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
15 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
17 local node_traverse = Dnode.traverse
18 local node_new = Dnode.new
19 local node_remove = luatexja.Dnode_remove -- Dnode.remove
20 local node_tail = Dnode.tail
21 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
22 local has_attr = Dnode.has_attribute
23 local set_attr = Dnode.set_attribute
24 local node_insert_before = Dnode.insert_before
25 local node_insert_after = Dnode.insert_after
26 local round = tex.round
28 local id_glyph = node.id('glyph')
29 local id_kern = node.id('kern')
30 local id_hlist = node.id('hlist')
31 local id_vlist = node.id('vlist')
32 local id_rule = node.id('rule')
33 local id_math = node.id('math')
35 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
36 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
37 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
38 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
39 local attr_icflag = luatexbase.attributes['ltj@icflag']
41 local ltjf_font_metric_table = ltjf.font_metric_table
43 local PACKED = luatexja.icflag_table.PACKED
44 local PROCESSED = luatexja.icflag_table.PROCESSED
45 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
46 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
48 local get_pr_begin_flag
50 local floor = math.floor
51 get_pr_begin_flag = function (p)
52 local i = has_attr(p, attr_icflag) or 0
53 return i - i%PROCESSED_BEGIN_FLAG
58 local ltjw = {} --export
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, 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 (pwidth ~= fwidth or getfield(p, 'height') ~= fheight or getfield(p, 'depth') ~= fdepth) then
80 = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
82 head, q = node_remove(head, p)
83 setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
84 setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth))
85 local box = node_new(id_hlist)
86 setfield(box, 'width', fwidth)
87 setfield(box, 'height', fheight)
88 setfield(box, 'depth', fdepth)
89 setfield(box, 'head', p)
90 setfield(box, 'shift', y_shift)
91 setfield(box, 'dir', dir)
92 set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
93 head = q and node_insert_before(head, q, box)
94 or node_insert_after(head, node_tail(head), box)
97 set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
98 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
99 setfield(p, 'yoffset', getfield(p, 'yoffset')
100 - (has_attr(p, attr_ykblshift) or 0) - fshift.down)
104 luatexja.setwidth.capsule_glyph = capsule_glyph
106 local function capsule_glyph_math(p, met, class)
107 local char_data = met.char_type[class]
108 if not char_data then return nil end
109 local fwidth, pwidth = char_data.width, getfield(p, 'width')
110 fwidth = (fwidth ~= 'prop') and fwidth or pwidth
111 fshift.down = char_data.down; fshift.left = char_data.left
112 fshift = call_callback("luatexja.set_width", fshift, met, class)
113 local fheight, fdepth = char_data.height, char_data.depth
115 = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
116 setfield(p, 'yoffset', -fshift.down)
118 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left
119 + (((ca=='right') and fwidth - pwidth) or round((fwidth - pwidth)*0.5)))
121 setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
123 local box = node_new(id_hlist);
124 setfield(box, 'width', fwidth)
125 setfield(box, 'height', fheight)
126 setfield(box, 'depth', fdepth)
127 setfield(box, 'head', p)
128 setfield(box, 'shift', y_shift)
129 setfield(box, 'dir', tex.mathdir)
130 set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
133 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
135 function luatexja.setwidth.set_ja_width(ahead, adir)
136 local p = ahead; head = p; dir = adir or 'TLT'
137 local m = false -- is in math mode?
141 and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
142 local pf = getfont(p)
143 if pf == has_attr(p, attr_curjfnt) then
144 p = capsule_glyph(p, ltjf_font_metric_table[pf],
145 has_attr(p, attr_jchar_class))
147 set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
148 setfield(p, 'yoffset',
149 getfield(p, 'yoffset') - (has_attr(p,attr_yablshift) or 0))
152 elseif pid==id_math then
153 m = (getsubtype(p)==0); p = node_next(p)
157 if pid==id_hlist or pid==id_vlist then
158 if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
159 setfield(p, 'shift', getfield(p, 'shift') + (has_attr(p,attr_yablshift) or 0))
161 elseif pid==id_rule then
162 if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
163 local v = has_attr(p,attr_yablshift) or 0
164 setfield(p, 'height', getfield(p, 'height')-v)
165 setfield(p, 'depth', getfield(p, 'depth')+v)
166 set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
173 -- adjust attr_icflag
174 tex.setattribute('global', attr_icflag, 0)