OSDN Git Service

0fb33a55e4501d8d75511bdd5ad98924390d6f79
[luatex-ja/luatexja.git] / src / luatexja / 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 require('luatexja.base');      local ltjb = luatexja.base
14 require('luatexja.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
38 local PACKED = 2
39
40 met_tb = {}
41 char_data = {}
42 head = nil
43
44 -- return true if and only if p is a Japanese character node
45 local function is_japanese_glyph_node(p)
46    return p.font==has_attr(p, attr_curjfnt)
47 end
48
49 -- mode: true iff p will be always encapsuled by a hbox
50 function capsule_glyph(p, dir, mode)
51    local h, box, q, fwidth, fheight, fdepth
52    p.xoffset= p.xoffset - round(met_tb.size*char_data.left)
53    if char_data.width ~= 'prop' then
54       fwidth = round(char_data.width*met_tb.size)
55    else fwidth = p.width end
56    fheight = round(met_tb.size*char_data.height)
57    fdepth = round(met_tb.size*char_data.depth)
58    if mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth then
59       local y_shift = - p.yoffset + (has_attr(p,attr_yablshift) or 0)
60       p.yoffset = -round(met_tb.size*char_data.down)
61       head, q = node.remove(head, p)
62       local total = fwidth - p.width
63       if total == 0 then
64          h = p; p.next = nil
65       else
66          h = node_new(id_kern); h.subtype = 0
67          if char_data.align=='left' then
68             h.kern = total; p.next = h; h = p
69          elseif char_data.align=='right' then
70             h.kern = total; p.next = nil; h.next = p
71          elseif char_data.align=='middle' then
72             h.kern = round(total/2); p.next = h
73             h = node_new(id_kern); h.subtype = 0
74             h.kern = total - round(total/2); h.next = p
75          end
76       end
77       box = node_new(id_hlist); 
78       box.width = fwidth; box.height = fheight; box.depth = fdepth
79       box.glue_set = 0; box.glue_order = 0; box.head = h
80       box.shift = y_shift; box.dir = dir or 'TLT'
81       set_attr(box, attr_icflag, PACKED)
82       if q then
83          head = node_insert_before(head, q, box)
84       else
85          head = node_insert_after(head, node_tail(head), box)
86       end
87       return q
88    else
89       p.yoffset = p.yoffset - (has_attr(p, attr_yablshift) or 0) - round(met_tb.size*char_data.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 then
99          if is_japanese_glyph_node(p) then
100             met_tb = ltjf.font_metric_table[p.font]
101             char_data = ltjf.metrics[met_tb.jfm].char_type[has_attr(p, attr_jchar_class)]
102             p = capsule_glyph(p, dir, false)
103          else 
104             p.yoffset = p.yoffset - (has_attr(p,attr_yablshift) or 0); p = node_next(p)
105          end
106       elseif p.id==id_math then
107          m = (p.subtype==0); p = node_next(p)
108       else
109          if m then
110             if p.id==id_hlist or p.id==id_vlist then
111                p.shift = p.shift + (has_attr(p,attr_yablshift) or 0)
112             elseif p.id==id_rule then
113                local v = has_attr(p,attr_yablshift) or 0
114                p.height = p.height - v; p.depth = p.depth + v 
115             end
116          end
117          p = node_next(p)
118       end
119    end
120    -- adjust attr_icflag
121    tex.attribute[attr_icflag] = -(0x7FFFFFFF)
122    return head
123 end