OSDN Git Service

luatexja-fontspec-24.sty: fix \newjfontfamily
[luatex-ja/luatexja.git] / src / ltj-pretreat.lua
1 --
2 -- luatexja/ltj-pretreat.lua
3 --
4
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
8
9 local Dnode = node.direct or node
10
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
14
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
21
22 local pairs = pairs
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 =luatexja.Dnode_remove -- Dnode.remove
28 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
29 local node_free = Dnode.free
30 local node_end_of_math = Dnode.end_of_math
31 local tex_getcount = tex.getcount
32
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')
37
38 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
39 local attr_icflag = luatexbase.attributes['ltj@icflag']
40
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
45 local lang_ja = token.create('ltj@japanese')[2]
46
47 ------------------------------------------------------------------------
48 -- MAIN PROCESS STEP 1: replace fonts
49 ------------------------------------------------------------------------
50 local wt
51 do
52    local head
53
54    local suppress_hyphenate_ja_aux = {}
55    suppress_hyphenate_ja_aux[id_glyph] = function(p)
56       if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
57          local pc = getchar(p)
58          local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
59          setfield(p, 'font', pf);  set_attr(p, attr_curjfnt, pf)
60          setfield(p, 'lang', lang_ja)
61          -- setfield(p, 'subtype', floor(getsubtype(p)*0.5)*2)
62          set_attr(p, attr_orig_char, pc)
63       end
64       return p
65    end
66    suppress_hyphenate_ja_aux[id_math] = function(p) return node_end_of_math(node_next(p)) end
67    suppress_hyphenate_ja_aux[id_whatsit] = function(p)
68       if getsubtype(p)==sid_user and getfield(p, 'user_id')==STCK then
69          wt[#wt+1] = p; head = node_remove(head, p)
70       end
71       return p
72    end
73
74    local function suppress_hyphenate_ja (h)
75       local p = to_direct(h)
76       wt, head = {}, p
77       while p do
78          local pfunc = suppress_hyphenate_ja_aux[getid(p)]
79          p = node_next(pfunc and pfunc(p) or p)
80       end
81       head = to_node(head)
82       lang.hyphenate(head)
83       return head
84    end
85    
86    luatexbase.add_to_callback('hyphenate', 
87                               function (head,tail)
88                                  return suppress_hyphenate_ja(head)
89                               end,'ltj.hyphenate')
90 end
91
92 -- mode: true iff this function is called from hpack_filter
93 local function set_box_stack_level(head, mode)
94    local box_set, cl = 0, tex.currentgrouplevel + 1
95    for _,p  in pairs(wt) do
96       if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
97    end
98    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
99    return head
100 end
101
102 -- CALLBACKS
103 luatexbase.add_to_callback('hpack_filter', 
104    function (head)
105      return set_box_stack_level(head, true)
106    end,'ltj.hpack_filter_pre',1)
107 luatexbase.add_to_callback('pre_linebreak_filter', 
108   function (head)
109      return set_box_stack_level(head, false)
110   end,'ltj.pre_linebreak_filter_pre',1)
111
112 luatexja.pretreat = {
113    set_box_stack_level = set_box_stack_level,
114 }