2 -- luatexja/pretreat.lua
4 luatexbase.provides_module({
5 name = 'luatexja.pretreat',
10 module('luatexja.pretreat', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
13 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
14 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
15 luatexja.load_module('stack'); local ltjs = luatexja.stack
17 local floor = math.floor
18 local has_attr = node.has_attribute
19 local set_attr = node.set_attribute
20 local node_traverse = node.traverse
21 local node_type = node.type
22 local node_remove = node.remove
23 local node_next = node.next
24 local node_free = node.free
25 local tex_getcount = tex.getcount
27 local id_glyph = node.id('glyph')
28 local id_math = node.id('math')
29 local id_whatsit = node.id('whatsit')
30 local sid_user = node.subtype('user_defined')
32 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
33 local attr_icflag = luatexbase.attributes['ltj@icflag']
35 local ltjf_font_metric_table = ltjf.font_metric_table
36 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
37 local attr_orig_char = luatexbase.attributes['ltj@origchar']
38 local STCK = luatexja.userid_table.STCK
40 ------------------------------------------------------------------------
41 -- MAIN PROCESS STEP 1: replace fonts
42 ------------------------------------------------------------------------
45 local function suppress_hyphenate_ja(head)
46 local non_math, p = true, head
50 if pid == id_glyph then
51 if (has_attr(p, attr_icflag) or 0)<=0 and ltjc_is_ucs_in_japanese_char(p) then
52 p.font = has_attr(p, attr_curjfnt) or p.font
53 p.subtype = floor(p.subtype*0.5)*2
54 set_attr(p, attr_orig_char, p.char)
56 elseif pid == id_math then
57 p = node_next(p) -- skip math on
58 while p and p.id~=id_math do p = node_next(p) end
59 elseif pid == id_whatsit and p.subtype==sid_user and p.user_id==STCK then
60 wt[#wt+1] = p; head = node_remove(head, p)
68 -- mode: true iff this function is called from hpack_filter
69 function set_box_stack_level(head, mode)
70 local box_set, cl = 0, tex.currentgrouplevel + 1
71 for _,p in pairs(wt) do
72 if mode and p.value==cl then box_set = 1 end; node_free(p)
74 ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
79 luatexbase.add_to_callback('hpack_filter',
81 return set_box_stack_level(head, true)
82 end,'ltj.hpack_filter_pre',1)
83 luatexbase.add_to_callback('pre_linebreak_filter',
85 return set_box_stack_level(head, false)
86 end,'ltj.pre_linebreak_filter_pre',1)
87 luatexbase.add_to_callback('hyphenate',
89 return suppress_hyphenate_ja(head)