OSDN Git Service

30389e21212a054e419a2fce21f261d032db5f34
[luatex-ja/luatexja.git] / src / ltj-setwidth.lua
1 --
2 -- src/ltj-setwidth.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
7
8 local Dnode = node.direct or node
9 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
10 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
11 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
12 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
13 local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
14 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
15 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
16
17 local node_traverse = Dnode.traverse
18 local node_new = Dnode.new
19 local node_remove = luatexja.Dnode_remove -- Dnode.remove
20 local node_tail = Dnode.tail
21 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
22 local has_attr = Dnode.has_attribute
23 local set_attr = Dnode.set_attribute
24 local node_insert_before = Dnode.insert_before
25 local node_insert_after = Dnode.insert_after
26 local round = tex.round
27
28 local id_glyph = node.id('glyph')
29 local id_kern = node.id('kern')
30 local id_hlist = node.id('hlist')
31 local id_vlist = node.id('vlist')
32 local id_rule = node.id('rule')
33 local id_math = node.id('math')
34
35 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
36 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
37 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
38 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
39 local attr_icflag = luatexbase.attributes['ltj@icflag']
40
41 local ltjf_font_metric_table = ltjf.font_metric_table
42
43 local PACKED       = luatexja.icflag_table.PACKED
44 local PROCESSED    = luatexja.icflag_table.PROCESSED
45 local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
46 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
47
48 local get_pr_begin_flag
49 do
50    local floor = math.floor
51    get_pr_begin_flag = function (p)
52       local i = has_attr(p, attr_icflag) or 0
53       return i - i%PROCESSED_BEGIN_FLAG
54    end
55 end
56
57 local head, dir
58 local ltjw = {} --export
59 luatexja.setwidth = ltjw
60
61 luatexbase.create_callback("luatexja.set_width", "data", 
62                            function (fstable, fmtable, jchar_class) 
63                               return fstable 
64                            end)
65 local call_callback = luatexbase.call_callback
66
67 local fshift =  { down = 0, left = 0}
68
69 -- mode: true iff p will be always encapsuled by a hbox
70 local function capsule_glyph(p, met, class)
71    local char_data = met.char_type[class]
72    if not char_data then return node_next(p) end
73    local fwidth, pwidth = char_data.width, getfield(p, 'width')
74    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
75    fshift.down = char_data.down; fshift.left = char_data.left
76    fshift = call_callback("luatexja.set_width", fshift, met, class)
77    local fheight, fdepth = char_data.height, char_data.depth
78    if (pwidth ~= fwidth or getfield(p, 'height') ~= fheight or getfield(p, 'depth') ~= fdepth) then
79       local y_shift
80          = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
81       local q
82       head, q = node_remove(head, p)
83       setfield(p, 'yoffset', -fshift.down); setfield(p, 'next', nil)
84       setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth))
85       local box = node_new(id_hlist)
86       setfield(box, 'width', fwidth)
87       setfield(box, 'height', fheight)
88       setfield(box, 'depth', fdepth)
89       setfield(box, 'head', p)
90       setfield(box, 'shift', y_shift)
91       setfield(box, 'dir', dir)
92       set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
93       head = q and node_insert_before(head, q, box) 
94                or node_insert_after(head, node_tail(head), box)
95       return q
96    else
97       set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
98       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
99       setfield(p, 'yoffset', getfield(p, 'yoffset') 
100                   - (has_attr(p, attr_ykblshift) or 0) - fshift.down)
101       return node_next(p)
102    end
103 end
104 luatexja.setwidth.capsule_glyph = capsule_glyph
105
106 local function capsule_glyph_math(p, met, class)
107    local char_data = met.char_type[class]
108    if not char_data then return nil end
109    local fwidth, pwidth = char_data.width, getfield(p, 'width')
110    fwidth = (fwidth ~= 'prop') and fwidth or pwidth
111    fshift.down = char_data.down; fshift.left = char_data.left
112    fshift = call_callback("luatexja.set_width", fshift, met, class)
113    local fheight, fdepth = char_data.height, char_data.depth
114    local y_shift, ca
115       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0), char_data.align
116    setfield(p, 'yoffset', -fshift.down)
117    if ca~='left'  then
118       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left
119                   + (((ca=='right') and fwidth - pwidth) or round((fwidth - pwidth)*0.5)))
120    else
121       setfield(p, 'xoffset', getfield(p, 'xoffset') - fshift.left)
122    end
123    local box = node_new(id_hlist); 
124    setfield(box, 'width', fwidth)
125    setfield(box, 'height', fheight)
126    setfield(box, 'depth', fdepth)
127    setfield(box, 'head', p)
128    setfield(box, 'shift', y_shift)
129    setfield(box, 'dir', tex.mathdir)
130    set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
131    return box
132 end
133 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
134
135 function luatexja.setwidth.set_ja_width(ahead, adir)
136    local p = ahead; head  = p; dir = adir or 'TLT'
137    local m = false -- is in math mode?
138    while p do
139       local pid = getid(p)
140       if (pid==id_glyph) 
141       and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
142          local pf = getfont(p)
143          if pf == has_attr(p, attr_curjfnt) then
144             p = capsule_glyph(p, ltjf_font_metric_table[pf], 
145                               has_attr(p, attr_jchar_class))
146          else
147             set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
148             setfield(p, 'yoffset',
149                      getfield(p, 'yoffset') - (has_attr(p,attr_yablshift) or 0))
150             p = node_next(p)
151          end
152       elseif pid==id_math then
153          m = (getsubtype(p)==0); p = node_next(p)
154       else
155          if m then
156             -- 数式の位置補正
157             if pid==id_hlist or pid==id_vlist then
158                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
159                   setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_yablshift) or 0))
160                end
161             elseif pid==id_rule then
162                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
163                   local v = has_attr(p,attr_yablshift) or 0
164                   setfield(p, 'height', getfield(p, 'height')-v)
165                   setfield(p, 'depth', getfield(p, 'depth')+v)
166                   set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
167                end
168             end
169          end
170          p = node_next(p)
171       end
172    end
173    -- adjust attr_icflag
174    tex.setattribute('global', attr_icflag, 0)
175    return head
176 end
177