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 luatexja.load_module('base'); local ltjb = luatexja.base
14 luatexja.load_module('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']
37 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
39 local ltjf_font_metric_table = ltjf.font_metric_table
46 -- return true if and only if p is a Japanese character node
47 local function is_japanese_glyph_node(p)
48 return p.font==has_attr(p, attr_curjfnt)
51 luatexbase.create_callback("luatexja.set_width", "data",
52 function (fstable, fmtable, jchar_class)
56 local fshift = { down = 0, left = 0}
57 -- mode: true iff p will be always encapsuled by a hbox
58 function capsule_glyph(p, dir, mode, met, class)
59 local h, box, q, fwidth
60 if char_data.width ~= 'prop' then
61 fwidth = char_data.width
62 else fwidth = p.width end
63 local fheight = char_data.height
64 local fdepth = char_data.depth
65 fshift.down = char_data.down; fshift.left = char_data.left
66 fshift = luatexbase.call_callback("luatexja.set_width", fshift, met, class)
68 p.xoffset= p.xoffset - fshift.left
69 if (mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth) then
70 local y_shift = - p.yoffset + (has_attr(p,attr_yablshift) or 0)
71 p.yoffset = -fshift.down
72 head, q = node.remove(head, p)
73 local total = fwidth - p.width
77 h = node_new(id_kern); h.subtype = 0
78 if char_data.align=='right' then
79 h.kern = total; p.next = nil; h.next = p
80 elseif char_data.align=='middle' then
81 h.kern = round(total*0.5); p.next = h
82 h = node_new(id_kern); h.subtype = 0
83 h.kern = total - round(total*0.5); h.next = p
85 h.kern = total; p.next = h; h = p
88 box = node_new(id_hlist);
89 box.width = fwidth; box.height = fheight; box.depth = fdepth
90 box.glue_set = 0; box.glue_order = 0; box.head = h
91 box.shift = y_shift; box.dir = dir or 'TLT'
92 set_attr(box, attr_icflag, PACKED)
93 set_attr(box, attr_uniqid, has_attr(p, attr_uniqid) or 0)
95 head = node_insert_before(head, q, box)
97 head = node_insert_after(head, node_tail(head), box)
101 p.yoffset = p.yoffset - (has_attr(p, attr_yablshift) or 0) - fshift.down
106 function set_ja_width(ahead, dir)
107 local p = ahead; head = ahead
108 local m = false -- is in math mode?
110 if p.id==id_glyph then
111 if is_japanese_glyph_node(p) then
112 local met = ltjf_font_metric_table[p.font]
113 local class = has_attr(p, attr_jchar_class)
114 char_data = ltjf.metrics[met.jfm].size_cache[met.size].char_type[class]
116 p = capsule_glyph(p, dir, false, met, class)
121 p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
123 elseif p.id==id_math then
124 m = (p.subtype==0); p = node_next(p)
127 if p.id==id_hlist or p.id==id_vlist then
128 p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
129 elseif p.id==id_rule then
130 local v = has_attr(p,attr_yablshift) or 0
131 p.height = p.height - v; p.depth = p.depth + v
137 -- adjust attr_icflag
138 tex.attribute[attr_icflag] = -(0x7FFFFFFF)