OSDN Git Service

luatexja-preset.sty: sync with jfontmaps.
[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) - fshift.left)
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    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
118    local box = node_new(id_hlist); 
119    setfield(box, 'width', fwidth)
120    setfield(box, 'height', fheight)
121    setfield(box, 'depth', fdepth)
122    setfield(box, 'head', p)
123    setfield(box, 'shift', y_shift)
124    setfield(box, 'dir', tex.mathdir)
125    set_attr(box, attr_icflag, PACKED + get_pr_begin_flag(p))
126    return box
127 end
128 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
129
130 function luatexja.setwidth.set_ja_width(ahead, adir)
131    local p = ahead; head  = p; dir = adir or 'TLT'
132    local m = false -- is in math mode?
133    while p do
134       local pid = getid(p)
135       if (pid==id_glyph) 
136       and ((has_attr(p, attr_icflag) or 0)%PROCESSED_BEGIN_FLAG)<=0 then
137          local pf = getfont(p)
138          if pf == has_attr(p, attr_curjfnt) then
139             p = capsule_glyph(p, ltjf_font_metric_table[pf], 
140                               has_attr(p, attr_jchar_class))
141          else
142             set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
143             setfield(p, 'yoffset',
144                      getfield(p, 'yoffset') - (has_attr(p,attr_yablshift) or 0))
145             p = node_next(p)
146          end
147       elseif pid==id_math then
148          m = (getsubtype(p)==0); p = node_next(p)
149       else
150          if m then
151             -- 数式の位置補正
152             if pid==id_hlist or pid==id_vlist then
153                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
154                   setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_yablshift) or 0))
155                end
156             elseif pid==id_rule then
157                if (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
158                   local v = has_attr(p,attr_yablshift) or 0
159                   setfield(p, 'height', getfield(p, 'height')-v)
160                   setfield(p, 'depth', getfield(p, 'depth')+v)
161                   set_attr(p, attr_icflag, PROCESSED + get_pr_begin_flag(p))
162                end
163             end
164          end
165          p = node_next(p)
166       end
167    end
168    -- adjust attr_icflag
169    tex.setattribute('global', attr_icflag, 0)
170    return head
171 end
172