OSDN Git Service

57db242c19f81120bbc7bd89201c6d7a27ccdbc5
[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
27 local id_glyph = node.id('glyph')
28 local id_kern = node.id('kern')
29 local id_hlist = node.id('hlist')
30 local id_vlist = node.id('vlist')
31 local id_rule = node.id('rule')
32 local id_math = node.id('math')
33
34 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
35 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
36 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
37 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
38 local attr_icflag = luatexbase.attributes['ltj@icflag']
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 local PROCESSED_BEGIN_FLAG = 16
46
47 do
48    local floor = math.floor
49    function get_pr_begin_flag(p)
50       return  floor((has_attr(p, attr_icflag) or 0)
51                  /PROCESSED_BEGIN_FLAG)*PROCESSED_BEGIN_FLAG
52    end
53 end
54 local get_pr_begin_flag = get_pr_begin_flag
55
56 head = nil
57
58 luatexbase.create_callback("luatexja.set_width", "data", 
59                            function (fstable, fmtable, jchar_class) 
60                               return fstable 
61                            end)
62
63 local fshift =  { down = 0, left = 0}
64
65 -- mode: true iff p will be always encapsuled by a hbox
66 function capsule_glyph(p, dir, mode, met, class)
67    local char_data = met.size_cache.char_type[class]
68    if not char_data then return node_next(p) end
69    local fwidth = (char_data.width ~= 'prop') and char_data.width or p.width
70    local fheight, fdepth = char_data.height, char_data.depth
71    fshift.down = char_data.down; fshift.left = char_data.left
72    fshift = luatexbase.call_callback("luatexja.set_width", fshift, met, class)
73    if (mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth) then
74       local y_shift, total = - p.yoffset + (has_attr(p,attr_ykblshift) or 0), fwidth - p.width
75       local q; head, q = node.remove(head, p)
76       p.yoffset, p.next = -fshift.down, nil
77       if total ~= 0 and char_data.align~='left' then
78          p.xoffset = p.xoffset - fshift.left
79             + (((char_data.align=='right') and total) or round(total*0.5))
80       else
81          p.xoffset = p.xoffset - fshift.left
82       end
83       local box = node_new(id_hlist); 
84       box.width, box.height, box.depth = fwidth, fheight, fdepth
85       box.head, box.shift, box.dir = p, y_shift, (dir or 'TLT')
86       box.glue_set, box.glue_order = 0, 0
87       set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
88       head = q and node_insert_before(head, q, box) 
89                or node_insert_after(head, node_tail(head), box)
90       return q
91    else
92       set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
93       p.xoffset = p.xoffset - fshift.left
94       p.yoffset = p.yoffset - (has_attr(p, attr_ykblshift) or 0) - fshift.down
95       return node_next(p)
96    end
97 end
98
99 function set_ja_width(ahead, dir)
100    local p = ahead; head  = ahead
101    local m = false -- is in math mode?
102    while p do
103       if (p.id==id_glyph) 
104       and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
105          if p.font == has_attr(p, attr_curjfnt) then
106             p = capsule_glyph(p, dir, false, ltjf_font_metric_table[p.font], 
107                               has_attr(p, attr_jchar_class))
108          else
109             set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
110             p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
111          end
112       elseif p.id==id_math then
113          m = (p.subtype==0); p = node_next(p)
114       else
115          if m then
116             if p.id==id_hlist or p.id==id_vlist then
117                p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
118             elseif p.id==id_rule then
119                local v = has_attr(p,attr_yablshift) or 0
120                p.height = p.height - v; p.depth = p.depth + v 
121             end
122          end
123          p = node_next(p)
124       end
125    end
126    -- adjust attr_icflag
127    tex.setattribute('global', attr_icflag, 0)
128    return head
129 end