2 -- luatexja/ltj-pretreat.lua
5 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
6 luatexja.load_module('stack'); local ltjs = luatexja.stack
7 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
9 local Dnode = node.direct or node
11 local nullfunc = function(n) return n end
12 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
13 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
15 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
16 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
17 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
18 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
19 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
20 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
23 local floor = math.floor
24 local has_attr = Dnode.has_attribute
25 local set_attr = Dnode.set_attribute
26 local node_traverse = Dnode.traverse
27 local node_remove = Dnode.remove
28 local node_next = Dnode.getnext
29 local node_free = Dnode.free
30 local node_end_of_math = Dnode.end_of_math
31 local tex_getcount = tex.getcount
33 local id_glyph = node.id('glyph')
34 local id_math = node.id('math')
35 local id_whatsit = node.id('whatsit')
36 local sid_user = node.subtype('user_defined')
38 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
39 local attr_icflag = luatexbase.attributes['ltj@icflag']
41 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
42 local ltjf_replace_altfont = ltjf.replace_altfont
43 local attr_orig_char = luatexbase.attributes['ltj@origchar']
44 local STCK = luatexja.userid_table.STCK
46 ------------------------------------------------------------------------
47 -- MAIN PROCESS STEP 1: replace fonts
48 ------------------------------------------------------------------------
53 local suppress_hyphenate_ja_aux = {}
54 suppress_hyphenate_ja_aux[id_glyph] = function(p)
55 if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
57 local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
58 setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
59 setfield(p, 'subtype', floor(getsubtype(p)*0.5)*2)
60 set_attr(p, attr_orig_char, pc)
64 suppress_hyphenate_ja_aux[id_math] = function(p) return node_end_of_math(node_next(p)) end
65 suppress_hyphenate_ja_aux[id_whatsit] = function(p)
66 if getsubtype(p)==sid_user and getfield(p, 'user_id')==STCK then
67 wt[#wt+1] = p; head = node_remove(head, p)
72 local function suppress_hyphenate_ja (h)
73 local p = to_direct(h)
76 local pfunc = suppress_hyphenate_ja_aux[getid(p)]
77 if pfunc then p = pfunc(p) end
85 luatexbase.add_to_callback('hyphenate',
87 return suppress_hyphenate_ja(head)
91 -- mode: true iff this function is called from hpack_filter
92 local function set_box_stack_level(head, mode)
93 local box_set, cl = 0, tex.currentgrouplevel + 1
94 for _,p in pairs(wt) do
95 if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
97 ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
102 luatexbase.add_to_callback('hpack_filter',
104 return set_box_stack_level(head, true)
105 end,'ltj.hpack_filter_pre',1)
106 luatexbase.add_to_callback('pre_linebreak_filter',
108 return set_box_stack_level(head, false)
109 end,'ltj.pre_linebreak_filter_pre',1)
111 luatexja.pretreat = {
112 set_box_stack_level = set_box_stack_level,