OSDN Git Service

Suppress 'underfull {h,v}box' warning for 'empty' boxes, as original [Lua]TeX.
[luatex-ja/luatexja.git] / src / ltj-setwidth.lua
1 --
2 -- src/ltj-setwidth.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('stack');     local ltjs = luatexja.stack
7 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
8 luatexja.load_module('direction'); local ltjd = luatexja.direction
9
10 local Dnode = node.direct or node
11 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
12 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
13 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
14 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
15 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
16 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
17 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
18
19 local node_traverse = Dnode.traverse
20 local node_new = Dnode.new
21 local node_remove = Dnode.remove
22 local node_tail = Dnode.tail
23 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
24 local has_attr = Dnode.has_attribute
25 local set_attr = Dnode.set_attribute
26 local node_insert_before = Dnode.insert_before
27 local node_insert_after = Dnode.insert_after
28 local round = tex.round
29
30 local id_glyph = node.id('glyph')
31 local id_kern = node.id('kern')
32 local id_hlist = node.id('hlist')
33 local id_vlist = node.id('vlist')
34 local id_rule = node.id('rule')
35 local id_math = node.id('math')
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 dir_tate = luatexja.dir_table.dir_tate
41
42 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
43 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
44 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
45 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
46 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
47 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
48 local attr_icflag = luatexbase.attributes['ltj@icflag']
49
50 local ltjf_font_metric_table = ltjf.font_metric_table
51
52 local PACKED       = luatexja.icflag_table.PACKED
53 local PROCESSED    = luatexja.icflag_table.PROCESSED
54 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
55 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
56
57 local get_pr_begin_flag
58 do
59    local floor = math.floor
60    get_pr_begin_flag = function (p)
61       local i = has_attr(p, attr_icflag) or 0
62       return i - i%PROCESSED_BEGIN_FLAG
63    end
64 end
65
66 local head, dir
67 local ltjw = {} --export
68 luatexja.setwidth = ltjw
69
70 luatexbase.create_callback("luatexja.set_width", "data",
71                            function (fstable, fmtable, jchar_class)
72                               return fstable
73                            end)
74 local call_callback = luatexbase.call_callback
75
76 local fshift =  { down = 0, left = 0}
77
78 local function capsule_glyph_yoko(p, met, class)
79    local char_data = met.char_type[class]
80    if not char_data then return node_next(p) end
81    local fwidth, pwidth = char_data.width, getfield(p, 'width')
82    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
83    fshift.down = char_data.down; fshift.left = char_data.left
84    fshift = call_callback("luatexja.set_width", fshift, met, class)
85    local fheight, fdepth = char_data.height, char_data.depth
86    if (pwidth ~= fwidth or getfield(p, 'height') ~= fheight or getfield(p, 'depth') ~= fdepth) then
87       local y_shift
88          = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
89       local q
90       head, q = node_remove(head, p)
91       setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
92       setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
93       local box = node_new(id_hlist)
94       setfield(box, 'width', fwidth)
95       setfield(box, 'height', fheight)
96       setfield(box, 'depth', fdepth)
97       setfield(box, 'head', p)
98       setfield(box, 'shift', y_shift)
99       setfield(box, 'dir', dir)
100       set_attr(box, attr_icflag, PACKED)
101       head = q and node_insert_before(head, q, box)
102                or node_insert_after(head, node_tail(head), box)
103       return q
104    else
105       set_attr(p, attr_icflag, PROCESSED)
106       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
107       setfield(p, 'yoffset', getfield(p, 'yoffset')
108                   - (has_attr(p, attr_ykblshift) or 0) - fshift.down)
109       return node_next(p)
110    end
111 end
112 local function capsule_glyph_tate(p, met, class)
113    local char_data = met.char_type[class]
114    if not char_data then return node_next(p) end
115    local ascent, descent = met.ascent, met.descent
116    local fwidth, pwidth = char_data.width, ascent + descent
117    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
118    fshift.down = char_data.down; fshift.left = char_data.left
119    fshift = call_callback("luatexja.set_width", fshift, met, class)
120    local fheight, fdepth = char_data.height, char_data.depth
121
122    setfield(p, 'char', ltjd.get_vert_glyph(getfont(p), getchar(p)))
123
124    local y_shift
125       = - getfield(p, 'yoffset') + (has_attr(p,attr_tkblshift) or 0)
126    local q
127    head, q = node_remove(head, p)
128    local box = node_new(id_hlist)
129    setfield(box, 'width', fwidth)
130    setfield(box, 'height', fheight)
131    setfield(box, 'depth', fdepth)
132    setfield(box, 'shift', y_shift)
133    setfield(box, 'dir', dir)
134
135    local k1 = node_new(id_kern)
136    setfield(k1, 'kern',
137             getfield(p, 'xoffset') + ascent
138                + char_data.align*(fwidth-pwidth) - fshift.left)
139    local ws = node_new(id_whatsit, sid_save)
140    local wm = node_new(id_whatsit, sid_matrix)
141    setfield(wm, 'data', '0 1 -1 0')
142    local k2 = node_new(id_kern)
143    setfield(k2, 'kern', -fshift.down - fdepth)
144    local k3 = node_new(id_kern)
145    setfield(k3, 'kern', - getfield(p, 'width') +fshift.down + fdepth)
146    local wr = node_new(id_whatsit, sid_restore)
147    setfield(box, 'head', k1); setfield(k1, 'next', ws)
148    setfield(ws, 'next', wm);  setfield(wm, 'next', k2);
149    setfield(k2, 'next', p);   setfield(p, 'next', k3);
150    setfield(k3, 'next', wr);
151
152    set_attr(box, attr_icflag, PACKED)
153    head = q and node_insert_before(head, q, box)
154       or node_insert_after(head, node_tail(head), box)
155    return q
156 end
157
158 local function capsule_glyph_math(p, met, class)
159    local char_data = met.char_type[class]
160    if not char_data then return nil end
161    local fwidth, pwidth = char_data.width, getfield(p, 'width')
162    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
163    fshift.down = char_data.down; fshift.left = char_data.left
164    fshift = call_callback("luatexja.set_width", fshift, met, class)
165    local fheight, fdepth = char_data.height, char_data.depth
166    local y_shift, ca
167       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
168    setfield(p, 'yoffset', -fshift.down)
169    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
170    local box = node_new(id_hlist);
171    setfield(box, 'width', fwidth)
172    setfield(box, 'height', fheight)
173    setfield(box, 'depth', fdepth)
174    setfield(box, 'head', p)
175    setfield(box, 'shift', y_shift)
176    setfield(box, 'dir', tex.mathdir)
177    set_attr(box, attr_icflag, PACKED)
178    return box
179 end
180 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
181
182 local tex_set_attr = tex.setattribute
183 function luatexja.setwidth.set_ja_width(ahead, adir)
184    local p = ahead; head  = p; dir = adir or 'TLT'
185    local m = false -- is in math mode?
186    local is_dir_tate = ltjs.list_dir==dir_tate
187    local capsule_glyph = is_dir_tate and capsule_glyph_tate or capsule_glyph_yoko
188    local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
189    while p do
190       local pid = getid(p)
191       if (pid==id_glyph)
192       and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
193          local pf = getfont(p)
194          if pf == has_attr(p, attr_curjfnt) then
195             p = capsule_glyph(p, ltjf_font_metric_table[pf],
196                               has_attr(p, attr_jchar_class))
197          else
198             -- TODO: neg. offset does not increase depth
199             --local d = getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0)
200             set_attr(p, attr_icflag, PROCESSED)
201             setfield(p, 'yoffset',
202                      getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
203             p = node_next(p)
204          end
205       elseif pid==id_math then
206          m = (getsubtype(p)==0); p = node_next(p)
207       else
208          if m then
209             -- 数式の位置補正
210             if pid==id_hlist or pid==id_vlist then
211                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
212                   setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_ablshift) or 0))
213                end
214             elseif pid==id_rule then
215                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
216                   local v = has_attr(p,attr_yablshift) or 0
217                   setfield(p, 'height', getfield(p, 'height')-v)
218                   setfield(p, 'depth', getfield(p, 'depth')+v)
219                   set_attr(p, attr_icflag, PROCESSED)
220                end
221             end
222          end
223          p = node_next(p)
224       end
225    end
226    -- adjust attr_icflag
227    tex_set_attr('global', attr_icflag, 0)
228    return head
229 end
230