2 -- luatexja/setwidth.lua
4 luatexbase.provides_module({
5 name = 'luatexja.setwidth',
10 module('luatexja.setwidth', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
13 require('luatexja.base'); local ltjb = luatexja.base
14 require('luatexja.jfont'); local ltjf = luatexja.jfont
16 local node_type = node.type
17 local node_new = node.new
18 local node_tail = node.tail
19 local node_next = node.next
20 local has_attr = node.has_attribute
21 local set_attr = node.set_attribute
22 local node_insert_before = node.insert_before
23 local node_insert_after = node.insert_after
24 local round = tex.round
26 local id_glyph = node.id('glyph')
27 local id_kern = node.id('kern')
28 local id_hlist = node.id('hlist')
29 local id_vlist = node.id('vlist')
30 local id_rule = node.id('rule')
31 local id_math = node.id('math')
33 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
34 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
35 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
36 local attr_icflag = luatexbase.attributes['ltj@icflag']
43 -- return true if and only if p is a Japanese character node
44 local function is_japanese_glyph_node(p)
45 return p.font==has_attr(p, attr_curjfnt)
48 -- mode: true iff p will be always encapsuled by a hbox
49 function capsule_glyph(p, dir, mode)
50 local h, box, q, fwidth, fheight, fdepth
51 p.xoffset= p.xoffset - char_data.left
52 if char_data.width ~= 'prop' then
53 fwidth = char_data.width
54 else fwidth = p.width end
55 fheight = char_data.height
56 fdepth = char_data.depth
57 if mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth then
58 local y_shift = - p.yoffset + (has_attr(p,attr_yablshift) or 0)
59 p.yoffset = -char_data.down
60 head, q = node.remove(head, p)
61 local total = fwidth - p.width
65 h = node_new(id_kern); h.subtype = 0
66 if char_data.align=='right' then
67 h.kern = total; p.next = nil; h.next = p
68 elseif char_data.align=='middle' then
69 h.kern = round(total/2); p.next = h
70 h = node_new(id_kern); h.subtype = 0
71 h.kern = total - round(total/2); h.next = p
73 h.kern = total; p.next = h; h = p
76 box = node_new(id_hlist);
77 box.width = fwidth; box.height = fheight; box.depth = fdepth
78 box.glue_set = 0; box.glue_order = 0; box.head = h
79 box.shift = y_shift; box.dir = dir or 'TLT'
80 set_attr(box, attr_icflag, PACKED)
82 head = node_insert_before(head, q, box)
84 head = node_insert_after(head, node_tail(head), box)
88 p.yoffset = p.yoffset - (has_attr(p, attr_yablshift) or 0) - char_data.down
93 function set_ja_width(ahead, dir)
94 local p = ahead; head = ahead
95 local m = false -- is in math mode?
97 if p.id==id_glyph then
98 if is_japanese_glyph_node(p) then
99 local met = ltjf.font_metric_table[p.font]
100 char_data = ltjf.metrics[met.jfm].size_cache[met.size].char_type[has_attr(p, attr_jchar_class)]
101 p = capsule_glyph(p, dir, false)
103 p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
105 elseif p.id==id_math then
106 m = (p.subtype==0); p = node_next(p)
109 if p.id==id_hlist or p.id==id_vlist then
110 p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
111 elseif p.id==id_rule then
112 local v = has_attr(p,attr_yablshift) or 0
113 p.height = p.height - v; p.depth = p.depth + v
119 -- adjust attr_icflag
120 tex.attribute[attr_icflag] = -(0x7FFFFFFF)