OSDN Git Service

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