OSDN Git Service

Add {start,stop}_time_measure()
[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 =luatexja.Dnode_remove -- Dnode.remove
30 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
31 local node_free = Dnode.free
32 local node_end_of_math = Dnode.end_of_math
33 local tex_getcount = tex.getcount
34
35 local id_glyph = node.id('glyph')
36 local id_math = node.id('math')
37 local id_whatsit = node.id('whatsit')
38 local sid_user = node.subtype('user_defined')
39
40 local attr_dir = luatexbase.attributes['ltj@dir']
41 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
42 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
43 local attr_icflag = luatexbase.attributes['ltj@icflag']
44
45 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
46 local ltjd_get_vert_glyph = ltjd.get_vert_glyph
47 local ltjf_replace_altfont = ltjf.replace_altfont
48 local attr_orig_char = luatexbase.attributes['ltj@origchar']
49 local STCK = luatexja.userid_table.STCK
50 local DIR = luatexja.userid_table.DIR
51
52 local dir_tate = 3
53 local dir_yoko = 4
54
55 ------------------------------------------------------------------------
56 -- MAIN PROCESS STEP 1: replace fonts
57 ------------------------------------------------------------------------
58 local wt
59 do
60    local start_time_measure, stop_time_measure 
61       = ltjb.start_time_measure, ltjb.stop_time_measure
62    local head
63    local is_dir_tate
64    local dir_frozen
65    local suppress_hyphenate_ja_aux = {}
66    suppress_hyphenate_ja_aux[id_glyph] = function(p)
67       if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
68          local pc = getchar(p)
69          local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
70          setfield(p, 'font', pf);  set_attr(p, attr_curjfnt, pf)
71          setfield(p, 'subtype', floor(getsubtype(p)*0.5)*2)
72          set_attr(p, attr_orig_char, pc)
73       end
74       return p
75    end
76    suppress_hyphenate_ja_aux[id_math] = function(p) 
77       dir_frozen = true
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; head = node_remove(head, p)
85          elseif uid==DIR and not dir_frozen then
86             ltjs.list_dir = getfield(p, 'value')
87          end
88          dir_frozen = true
89       end
90       return p
91    end
92
93    local function suppress_hyphenate_ja (h)
94       start_time_measure('ltj_hyphenate')
95       local p = to_direct(h)
96       wt, head, dir_frozen = {}, p, false
97       ltjs.list_dir=ltjd.get_dir_count()
98       while p do
99          local pfunc = suppress_hyphenate_ja_aux[getid(p)]
100          if pfunc then p = pfunc(p) else dir_frozen = true end
101          p = node_next(p)
102       end
103       head = to_node(head)
104       stop_time_measure('ltj_hyphenate'); start_time_measure('tex_hyphenate')
105       lang.hyphenate(head)
106       stop_time_measure('tex_hyphenate')
107       return head
108    end
109
110    luatexbase.add_to_callback('hyphenate',
111                               function (head,tail)
112                                  return suppress_hyphenate_ja(head)
113                               end,'ltj.hyphenate')
114 end
115
116 -- mode: true iff this function is called from hpack_filter
117 local function set_box_stack_level(head, mode)
118    local box_set, cl = 0, tex.currentgrouplevel + 1
119    for _,p  in pairs(wt) do
120       if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
121    end
122    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
123    is_dir_tate = ltjs.list_dir == dir_tate
124    if is_dir_tate then
125       for p in Dnode.traverse_id(id_glyph,to_direct(head)) do
126          if (has_attr(p, attr_icflag) or 0)<=0 and has_attr(p, attr_curjfnt)==getfont(p) then
127             local pfn = has_attr(p, attr_curtfnt) or getfont(p)
128             local pc = getchar(p)
129             local pf = ltjf_replace_altfont(pfn, pc)
130             set_attr(p, attr_dir, pc)
131             pc = ltjd_get_vert_glyph(pf, pc) or pc
132             setfield(p, 'char', pc); set_attr(p, attr_orig_char, pc)
133             setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
134          end
135       end
136    end
137    return head
138 end
139
140 -- CALLBACKS
141 luatexbase.add_to_callback('hpack_filter',
142    function (head)
143      return set_box_stack_level(head, true)
144    end,'ltj.hpack_filter_pre',1)
145 luatexbase.add_to_callback('pre_linebreak_filter',
146   function (head)
147      return set_box_stack_level(head, false)
148   end,'ltj.pre_linebreak_filter_pre',1)
149
150 luatexja.pretreat = {
151    set_box_stack_level = set_box_stack_level,
152 }