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
43 local IC_PROCESSED = 9
48 -- return true if and only if p is a Japanese character node
49 local function is_japanese_glyph_node(p)
50 return p.font==has_attr(p, attr_curjfnt)
53 luatexbase.create_callback("luatexja.set_width", "data",
54 function (fstable, fmtable, jchar_class)
58 local fshift = { down = 0, left = 0}
59 -- mode: true iff p will be always encapsuled by a hbox
60 function capsule_glyph(p, dir, mode, met, class)
61 local h, box, q, fwidth
62 if char_data.width ~= 'prop' then
63 fwidth = char_data.width
64 else fwidth = p.width end
65 local fheight = char_data.height
66 local fdepth = char_data.depth
67 fshift.down = char_data.down; fshift.left = char_data.left
68 fshift = luatexbase.call_callback("luatexja.set_width", fshift, met, class)
69 p.xoffset = p.xoffset - fshift.left
70 if (mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth) then
71 local y_shift = - p.yoffset + (has_attr(p,attr_yablshift) or 0)
72 p.yoffset = -fshift.down
73 head, q = node.remove(head, p)
74 local total = fwidth - p.width
78 h = node_new(id_kern); h.subtype = 0
79 if char_data.align=='right' then
80 h.kern = total; p.next = nil; h.next = p
81 elseif char_data.align=='middle' then
82 h.kern = round(total*0.5); p.next = h
83 h = node_new(id_kern); h.subtype = 0
84 h.kern = total - round(total*0.5); h.next = p
86 h.kern = total; p.next = h; h = p
89 box = node_new(id_hlist);
90 box.width = fwidth; box.height = fheight; box.depth = fdepth
91 box.glue_set = 0; box.glue_order = 0; box.head = h
92 box.shift = y_shift; box.dir = dir or 'TLT'
93 set_attr(box, attr_icflag, PACKED)
94 set_attr(box, attr_uniqid, has_attr(p, attr_uniqid) or 0)
96 head = node_insert_before(head, q, box)
98 head = node_insert_after(head, node_tail(head), box)
102 p.yoffset = p.yoffset - (has_attr(p, attr_yablshift) or 0) - fshift.down
107 function set_ja_width(ahead, dir)
108 local p = ahead; head = ahead
109 local m = false -- is in math mode?
111 if (p.id==id_glyph) and (has_attr(p, attr_icflag, PROCESSED) or 0)<=0 then
112 if is_japanese_glyph_node(p) then
113 local met = ltjf_font_metric_table[p.font]
114 local class = has_attr(p, attr_jchar_class)
115 char_data = met.size_cache.char_type[class]
116 set_attr(p, attr_icflag, PROCESSED)
118 p = capsule_glyph(p, dir, false, met, class)
124 set_attr(p, attr_icflag, PROCESSED)
125 p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
127 elseif p.id==id_math then
128 m = (p.subtype==0); p = node_next(p)
131 if p.id==id_hlist or p.id==id_vlist then
132 p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
133 elseif p.id==id_rule then
134 local v = has_attr(p,attr_yablshift) or 0
135 p.height = p.height - v; p.depth = p.depth + v
141 -- adjust attr_icflag
142 tex.setattribute('global', attr_icflag, 0)