2 -- luatexja/ltj-pretreat.lua
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
11 local to_node = node.direct.tonode
12 local to_direct = node.direct.todirect
14 local setfield = node.direct.setfield
15 local getid = node.direct.getid
16 local getfont = node.direct.getfont
17 local getchar = node.direct.getchar
18 local getfield = node.direct.getfield
19 local getsubtype = node.direct.getsubtype
22 local floor = math.floor
23 local has_attr = node.direct.has_attribute
24 local set_attr = node.direct.set_attribute
25 local node_traverse = node.direct.traverse
26 local node_remove = node.direct.remove
27 local node_next = node.direct.getnext
28 local node_free = node.direct.free
29 local node_end_of_math = node.direct.end_of_math
30 local tex_getcount = tex.getcount
32 local id_glyph = node.id('glyph')
33 local id_math = node.id('math')
34 local id_whatsit = node.id('whatsit')
35 local sid_user = node.subtype('user_defined')
37 local attr_dir = luatexbase.attributes['ltj@dir']
38 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
39 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
40 local attr_icflag = luatexbase.attributes['ltj@icflag']
42 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
43 local ltjs_orig_char_table = ltjs.orig_char_table
44 local ltjf_replace_altfont = ltjf.replace_altfont
45 local ltjf_font_extra_info = ltjf.font_extra_info
46 local attr_orig_char = luatexbase.attributes['ltj@origchar']
47 local STCK = luatexja.userid_table.STCK
48 local DIR = luatexja.userid_table.DIR
49 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
51 local dir_tate = luatexja.dir_table.dir_tate
52 local lang_ja = luatexja.lang_ja
54 ------------------------------------------------------------------------
55 -- MAIN PROCESS STEP 1: replace fonts
56 ------------------------------------------------------------------------
57 local wt, wtd = {}, {}
59 local ltjd_get_dir_count = ltjd.get_dir_count
60 local start_time_measure, stop_time_measure
61 = ltjb.start_time_measure, ltjb.stop_time_measure
63 local suppress_hyphenate_ja_aux = {
64 [id_math] = function(p) return node_next(node_end_of_math(node_next(p))) end,
65 [id_whatsit] = function(p)
66 if getsubtype(p)==sid_user then
67 local uid = getfield(p, 'user_id')
69 wt[#wt+1] = p; node_remove(head, p)
71 if has_attr(p, attr_icflag)<PROCESSED_BEGIN_FLAG then
72 ltjs.list_dir = has_attr(p, attr_dir)
73 else -- こっちのケースは通常使用では起こらない
74 wtd[#wtd+1] = p; node_remove(head, p)
81 setmetatable(suppress_hyphenate_ja_aux,
83 __index = function() return node_next end,
85 local function suppress_hyphenate_ja (h)
86 start_time_measure('ltj_hyphenate')
88 for i = 1,#wt do wt[i]=nil end
89 for i = 1,#wtd do wtd[i]=nil end
90 for i,_ in pairs(ltjs_orig_char_table) do
91 ltjs_orig_char_table[i] = nil
93 ltjs.list_dir=ltjd_get_dir_count()
98 while pid==id_glyph do
100 if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p, pc) then
102 ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc))
103 setfield(p, 'lang', lang_ja)
104 ltjs_orig_char_table[p] = pc
106 p = node_next(p); pid = getid(p)
108 p = (suppress_hyphenate_ja_aux[pid])(p)
111 stop_time_measure('ltj_hyphenate'); start_time_measure('tex_hyphenate')
112 lang.hyphenate(h, nil)
113 stop_time_measure('tex_hyphenate')
117 ltjb.add_to_callback('hyphenate',
119 return suppress_hyphenate_ja(head)
123 -- mode: true iff this function is called from hpack_filter
124 local ltjs_report_stack_level = ltjs.report_stack_level
125 local ltjf_font_metric_table = ltjf.font_metric_table
126 local font_getfont = font.getfont
127 local function set_box_stack_level(head, mode)
128 local box_set, cl = 0, tex.currentgrouplevel + 1
129 for _,p in pairs(wt) do
130 if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
132 ltjs_report_stack_level(tex_getcount('ltj@@stack') + box_set)
133 for _,p in pairs(wtd) do
136 if ltjs.list_dir == dir_tate then
137 for p in node.direct.traverse_id(id_glyph,to_direct(head)) do
138 if (has_attr(p, attr_icflag) or 0)<=0 and getfield(p, 'lang')==lang_ja then
139 local pc = ltjs_orig_char_table[p]
140 local nf = ltjf_replace_altfont( has_attr(p, attr_curtfnt) or getfont(p) , pc)
141 setfield(p, 'font', nf)
142 if ltjf_font_metric_table[nf].vert_activated then
143 local pc = getchar(p)
144 pc = (ltjf_font_extra_info[nf] and ltjf_font_extra_info[nf][pc] and ltjf_font_extra_info[nf][pc].vform)
145 if pc and font_getfont(nf).characters[pc] then setfield(p, 'char', pc) end
154 ltjb.add_to_callback('hpack_filter',
156 return set_box_stack_level(head, true)
157 end,'ltj.set_stack_level',1)
158 ltjb.add_to_callback('pre_linebreak_filter',
160 return set_box_stack_level(head, false)
161 end,'ltj.set_stack_level',1)
163 luatexja.pretreat = {
164 set_box_stack_level = set_box_stack_level,
165 orig_char_table = orig_char_table,