OSDN Git Service

457ecd806ca4dbfe5f8d649a5a6385f9aba2b0b7
[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
43 char_data = {}
44 head = nil
45
46 -- return true if and only if p is a Japanese character node
47 local function is_japanese_glyph_node(p)
48    return p.font==has_attr(p, attr_curjfnt)
49 end
50
51 luatexbase.create_callback("luatexja.set_width", "data", 
52                            function (fstable, fmtable, jchar_class) 
53                               return fstable 
54                            end)
55
56 local fshift =  { down = 0, left = 0}
57 -- mode: true iff p will be always encapsuled by a hbox
58 function capsule_glyph(p, dir, mode, met, class)
59    local h, box, q, fwidth
60    if char_data.width ~= 'prop' then
61       fwidth = char_data.width
62    else fwidth = p.width end
63    local fheight = char_data.height
64    local fdepth = char_data.depth
65    fshift.down = char_data.down; fshift.left = char_data.left
66    fshift = luatexbase.call_callback("luatexja.set_width", fshift, met, class)
67 --   local ti = 
68    p.xoffset= p.xoffset - fshift.left
69    if mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth then
70       local y_shift = - p.yoffset + (has_attr(p,attr_yablshift) or 0)
71       p.yoffset = -fshift.down
72       head, q = node.remove(head, p)
73       local total = fwidth - p.width
74       if total == 0 then
75          h = p; p.next = nil
76       else
77          h = node_new(id_kern); h.subtype = 0
78          if char_data.align=='right' then
79             h.kern = total; p.next = nil; h.next = p
80          elseif char_data.align=='middle' then
81             h.kern = round(total/2); p.next = h
82             h = node_new(id_kern); h.subtype = 0
83             h.kern = total - round(total/2); h.next = p
84          else -- left
85             h.kern = total; p.next = h; h = p
86          end
87       end
88       box = node_new(id_hlist); 
89       box.width = fwidth; box.height = fheight; box.depth = fdepth
90       box.glue_set = 0; box.glue_order = 0; box.head = h
91       box.shift = y_shift; box.dir = dir or 'TLT'
92       set_attr(box, attr_icflag, PACKED)
93       set_attr(box, attr_uniqid, has_attr(p, attr_uniqid) or 0)
94       if q then
95          head = node_insert_before(head, q, box)
96       else
97          head = node_insert_after(head, node_tail(head), box)
98       end
99       return q
100    else
101       p.yoffset = p.yoffset - (has_attr(p, attr_yablshift) or 0) - fshift.down
102       return node_next(p)
103    end
104 end
105
106 function set_ja_width(ahead, dir)
107    local p = ahead; head  = ahead
108    local m = false -- is in math mode?
109    while p do
110       if p.id==id_glyph then
111          if is_japanese_glyph_node(p) then
112             local met = ltjf_font_metric_table[p.font]
113             local class = has_attr(p, attr_jchar_class)
114             char_data = ltjf.metrics[met.jfm].size_cache[met.size].char_type[class]
115             if char_data then
116                p = capsule_glyph(p, dir, false, met, class)
117             else
118                p = node_next(p)
119             end
120          else 
121             p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
122          end
123       elseif p.id==id_math then
124          m = (p.subtype==0); p = node_next(p)
125       else
126          if m then
127             if p.id==id_hlist or p.id==id_vlist then
128                p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
129             elseif p.id==id_rule then
130                local v = has_attr(p,attr_yablshift) or 0
131                p.height = p.height - v; p.depth = p.depth + v 
132             end
133          end
134          p = node_next(p)
135       end
136    end
137    -- adjust attr_icflag
138    tex.attribute[attr_icflag] = -(0x7FFFFFFF)
139    return head
140 end