OSDN Git Service

Moved some Lua codes to avoid circular dependencies
[luatex-ja/luatexja.git] / src / ltj-pretreat.lua
1 --
2 -- luatexja/ltj-pretreat.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
7 luatexja.load_module('stack');     local ltjs = luatexja.stack
8 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
9 luatexja.load_module('direction'); local ltjd = luatexja.direction
10
11 local Dnode = node.direct or node
12
13 local nullfunc = function(n) return n end
14 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
15 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
16
17 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
18 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
19 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
20 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
21 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
22 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
23
24 local pairs = pairs
25 local floor = math.floor
26 local has_attr = Dnode.has_attribute
27 local set_attr = Dnode.set_attribute
28 local node_traverse = Dnode.traverse
29 local node_remove = Dnode.remove
30 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
31 local node_prev = (Dnode ~= node) and Dnode.getprev or node.prev
32 local node_free = Dnode.free
33 local node_end_of_math = Dnode.end_of_math
34 local tex_getcount = tex.getcount
35
36 local id_glyph = node.id('glyph')
37 local id_math = node.id('math')
38 local id_whatsit = node.id('whatsit')
39 local sid_user = node.subtype('user_defined')
40
41 local attr_dir = luatexbase.attributes['ltj@dir']
42 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
43 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
44 local attr_icflag = luatexbase.attributes['ltj@icflag']
45
46 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
47 local ltjf_get_vert_glyph = ltjf.get_vert_glyph
48 local ltjf_replace_altfont = ltjf.replace_altfont
49 local attr_orig_char = luatexbase.attributes['ltj@origchar']
50 local STCK  = luatexja.userid_table.STCK
51 local DIR   = luatexja.userid_table.DIR
52 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
53
54 local dir_tate = luatexja.dir_table.dir_tate
55
56 ------------------------------------------------------------------------
57 -- MAIN PROCESS STEP 1: replace fonts
58 ------------------------------------------------------------------------
59 local wt, wtd = {}, {}
60 do
61    local ltjd_get_dir_count = ltjd.get_dir_count
62    local start_time_measure, stop_time_measure 
63       = ltjb.start_time_measure, ltjb.stop_time_measure
64    local head, real_head
65    local is_dir_tate
66    local suppress_hyphenate_ja_aux = {}
67    suppress_hyphenate_ja_aux[id_glyph] = function(p)
68       if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
69          local pc = getchar(p)
70          local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
71          setfield(p, 'font', pf);  set_attr(p, attr_curjfnt, pf)
72          setfield(p, 'subtype', floor(getsubtype(p)*0.5)*2)
73          set_attr(p, attr_orig_char, pc)
74       end
75       return p
76    end
77    suppress_hyphenate_ja_aux[id_math] = function(p)
78       return node_end_of_math(node_next(p)) end
79    suppress_hyphenate_ja_aux[50] = function(p) return p end
80    suppress_hyphenate_ja_aux[id_whatsit] = function(p)
81       if getsubtype(p)==sid_user then 
82          local uid = getfield(p, 'user_id')
83          if uid==STCK then
84             wt[#wt+1] = p; node_remove(head, p)
85          elseif uid==DIR then
86             if has_attr(p, attr_icflag)<PROCESSED_BEGIN_FLAG  then
87                ltjs.list_dir = has_attr(p, attr_dir)
88             else
89                wtd[#wtd+1] = p; node_remove(head, p)
90             end
91          end
92       end
93       return p
94    end
95
96    local function suppress_hyphenate_ja (h)
97       start_time_measure('ltj_hyphenate')
98       head = to_direct(h); real_head = node_next(head)
99       local p = head
100       for i = 1,#wt do wt[i]=nil end
101       for i = 1,#wtd do wtd[i]=nil end
102       ltjs.list_dir=ltjd_get_dir_count()
103       while p do
104          local flag
105          local pfunc = suppress_hyphenate_ja_aux[getid(p)]
106          if pfunc then p = pfunc(p) end
107          p = node_next(p)
108       end
109       stop_time_measure('ltj_hyphenate'); start_time_measure('tex_hyphenate')
110       lang.hyphenate(h)
111       stop_time_measure('tex_hyphenate')
112       return h
113    end
114
115    luatexbase.add_to_callback('hyphenate',
116                               function (head,tail)
117                                  return suppress_hyphenate_ja(head)
118                               end,'ltj.hyphenate')
119 end
120
121 -- mode: true iff this function is called from hpack_filter
122 local function set_box_stack_level(head, mode)
123    local box_set, cl = 0, tex.currentgrouplevel + 1
124    for _,p  in pairs(wt) do
125       if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
126    end
127    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
128    for _,p  in pairs(wtd) do
129       node_free(p)
130    end
131    is_dir_tate = ltjs.list_dir == dir_tate
132    if is_dir_tate then
133       for p in Dnode.traverse_id(id_glyph,to_direct(head)) do
134          if (has_attr(p, attr_icflag) or 0)<=0 and has_attr(p, attr_curjfnt)==getfont(p) then
135             local pfn = has_attr(p, attr_curtfnt) or getfont(p)
136             local pc = getchar(p)
137             local pf = ltjf_replace_altfont(pfn, pc)
138             set_attr(p, attr_dir, pc)
139             pc = ltjf_get_vert_glyph(pf, pc) or pc
140             setfield(p, 'char', pc); set_attr(p, attr_orig_char, pc)
141             setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
142          end
143       end
144    end
145    return head
146 end
147
148 -- CALLBACKS
149 luatexbase.add_to_callback('hpack_filter',
150    function (head)
151      return set_box_stack_level(head, true)
152    end,'ltj.hpack_filter_pre',1)
153 luatexbase.add_to_callback('pre_linebreak_filter',
154   function (head)
155      return set_box_stack_level(head, false)
156   end,'ltj.pre_linebreak_filter_pre',1)
157
158 luatexja.pretreat = {
159    set_box_stack_level = set_box_stack_level,
160 }