OSDN Git Service

Cleanup.
[luatex-ja/luatexja.git] / src / ltj-setwidth.lua
1 --
2 -- luatexja/setwidth.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.setwidth',
6   date = '2013/12/05',
7   description = '',
8 })
9 module('luatexja.setwidth', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
11
12 luatexja.load_module('base');      local ltjb = luatexja.base
13 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
14
15 local node_type = node.type
16 local node_new = node.new
17 local node_tail = node.tail
18 local node_next = node.next
19 local has_attr = node.has_attribute
20 local set_attr = node.set_attribute
21 local node_insert_before = node.insert_before
22 local node_insert_after = node.insert_after
23 local round = tex.round
24
25 local id_glyph = node.id('glyph')
26 local id_kern = node.id('kern')
27 local id_hlist = node.id('hlist')
28 local id_vlist = node.id('vlist')
29 local id_rule = node.id('rule')
30 local id_math = node.id('math')
31
32 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
33 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
34 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
35 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
36 local attr_icflag = luatexbase.attributes['ltj@icflag']
37
38 local ltjf_font_metric_table = ltjf.font_metric_table
39
40 local PACKED       = luatexja.icflag_table.PACKED
41 local PROCESSED    = luatexja.icflag_table.PROCESSED
42 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
43 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
44
45 do
46    local floor = math.floor
47    function get_pr_begin_flag(p)
48       local i = has_attr(p, attr_icflag) or 0
49       return i - i%PROCESSED_BEGIN_FLAG
50    end
51 end
52 local get_pr_begin_flag = get_pr_begin_flag
53
54 head = nil;
55
56 luatexbase.create_callback("luatexja.set_width", "data", 
57                            function (fstable, fmtable, jchar_class) 
58                               return fstable 
59                            end)
60
61 local fshift =  { down = 0, left = 0}
62
63 -- mode: true iff p will be always encapsuled by a hbox
64 function capsule_glyph(p, dir, mode, met, class)
65    local char_data = met.char_type[class]
66    if not char_data then return node_next(p) end
67    local fwidth, pwidth = char_data.width, p.width
68    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
69    fshift.down = char_data.down; fshift.left = char_data.left
70    fshift = luatexbase.call_callback("luatexja.set_width", fshift, met, class)
71    local fheight, fdepth = char_data.height, char_data.depth
72    if (mode or pwidth ~= fwidth or p.height ~= fheight or p.depth ~= fdepth) then
73       local y_shift, ca
74          = - p.yoffset + (has_attr(p,attr_ykblshift) or 0), char_data.align
75       local q; head, q = node.remove(head, p)
76       p.yoffset, p.next = -fshift.down, nil
77       if ca~='left'  then
78          p.xoffset = p.xoffset - fshift.left
79             + (((ca=='right') and fwidth - pwidth) or round((fwidth - pwidth)*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 not needed
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       local pid = p.id
104       if (pid==id_glyph) 
105       and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
106          local pf = p.font
107          if pf == has_attr(p, attr_curjfnt) then
108             p = capsule_glyph(p, dir, false, ltjf_font_metric_table[pf], 
109                               has_attr(p, attr_jchar_class))
110          else
111             set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
112             p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
113          end
114       elseif p.id==id_math then
115          m = (p.subtype==0); p = node_next(p)
116       else
117          if m then
118             -- 数式の位置補正
119             if pid==id_hlist or pid==id_vlist then
120                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
121                   p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
122                end
123             elseif pid==id_rule then
124                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
125                   local v = has_attr(p,attr_yablshift) or 0
126                   p.height = p.height - v; p.depth = p.depth + v 
127                   set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
128                end
129             end
130          end
131          p = node_next(p)
132       end
133    end
134    -- adjust attr_icflag
135    tex.setattribute('global', attr_icflag, 0)
136    return head
137 end