OSDN Git Service

Reduced 'global variables' in ltj-jfmglue.lua.
[luatex-ja/luatexja.git] / src / ltj-setwidth.lua
1 --
2 -- luatexja/setwidth.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.setwidth',
6   date = '2012/07/19',
7   version = '0.2',
8   description = '',
9 })
10 module('luatexja.setwidth', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 luatexja.load_module('base');      local ltjb = luatexja.base
14 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
15
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
25
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')
32
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_ykblshift = luatexbase.attributes['ltj@ykblshift']
37 local attr_icflag = luatexbase.attributes['ltj@icflag']
38 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
39
40 local ltjf_font_metric_table = ltjf.font_metric_table
41
42 local PACKED = 2
43 local PROCESSED = 8
44 local IC_PROCESSED = 9
45
46 head = nil
47
48 luatexbase.create_callback("luatexja.set_width", "data", 
49                            function (fstable, fmtable, jchar_class) 
50                               return fstable 
51                            end)
52
53 local fshift =  { down = 0, left = 0}
54 -- mode: true iff p will be always encapsuled by a hbox
55 function capsule_glyph(p, dir, mode, met, class)
56    local char_data = met.size_cache.char_type[class]
57    if not char_data then return node_next(p) end
58    local fwidth
59    if char_data.width ~= 'prop' then
60       fwidth = char_data.width
61    else fwidth = p.width end
62    local fheight, fdepth = char_data.height, char_data.depth
63    fshift.down = char_data.down; fshift.left = char_data.left
64    fshift = luatexbase.call_callback("luatexja.set_width", fshift, met, class)
65    if (mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth) then
66       local y_shift, total = - p.yoffset + (has_attr(p,attr_ykblshift) or 0), fwidth - p.width
67       local q; head, q = node.remove(head, p)
68       p.yoffset, p.next = -fshift.down, nil
69       if total ~= 0 and char_data.align~='left' then
70          p.xoffset = p.xoffset - fshift.left
71             + (((char_data.align=='right') and total) or round(total*0.5))
72       else
73          p.xoffset = p.xoffset - fshift.left
74       end
75       local box = node_new(id_hlist); 
76       box.width, box.height, box.depth = fwidth, fheight, fdepth
77       box.glue_set, box.glue_order, box.head = 0, 0, p
78       box.shift = y_shift; box.dir = dir or 'TLT'
79       set_attr(box, attr_icflag, PACKED)
80       set_attr(box, attr_uniqid, has_attr(p, attr_uniqid) or 0)
81       if q then
82          head = node_insert_before(head, q, box)
83       else
84          head = node_insert_after(head, node_tail(head), box)
85       end
86       return q
87    else
88       p.xoffset = p.xoffset - fshift.left
89       p.yoffset = p.yoffset - (has_attr(p, attr_ykblshift) or 0) - fshift.down
90       return node_next(p)
91    end
92 end
93
94 function set_ja_width(ahead, dir)
95    local p = ahead; head  = ahead
96    local m = false -- is in math mode?
97    while p do
98       if (p.id==id_glyph) and (has_attr(p, attr_icflag) or 0)<=0 then
99          if p.font == has_attr(p, attr_curjfnt) then
100             set_attr(p, attr_icflag, PROCESSED)
101             p = capsule_glyph(p, dir, false, ltjf_font_metric_table[p.font], 
102                               has_attr(p, attr_jchar_class))
103          else
104             set_attr(p, attr_icflag, PROCESSED) 
105             p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
106          end
107       elseif p.id==id_math then
108          m = (p.subtype==0); p = node_next(p)
109       else
110          if m then
111             if p.id==id_hlist or p.id==id_vlist then
112                p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
113             elseif p.id==id_rule then
114                local v = has_attr(p,attr_yablshift) or 0
115                p.height = p.height - v; p.depth = p.depth + v 
116             end
117          end
118          p = node_next(p)
119       end
120    end
121    -- adjust attr_icflag
122    tex.setattribute('global', attr_icflag, 0)
123    return head
124 end