OSDN Git Service

initial version of luatexja-zhfonts.sty.
[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
8 local floor = math.floor
9 local has_attr = node.has_attribute
10 local set_attr = node.set_attribute
11 local node_traverse = node.traverse
12 local node_type = node.type
13 local node_remove = node.remove
14 local node_next = node.next
15 local node_free = node.free
16 local tex_getcount = tex.getcount
17
18 local id_glyph = node.id('glyph')
19 local id_math = node.id('math')
20 local id_whatsit = node.id('whatsit')
21 local sid_user = node.subtype('user_defined')
22
23 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
24 local attr_icflag = luatexbase.attributes['ltj@icflag']
25
26 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
27 local attr_orig_char = luatexbase.attributes['ltj@origchar']
28 local STCK = luatexja.userid_table.STCK
29
30 ------------------------------------------------------------------------
31 -- MAIN PROCESS STEP 1: replace fonts
32 ------------------------------------------------------------------------
33 local wt
34
35 local function suppress_hyphenate_ja(head)
36    local non_math, p = true, head
37    wt = {}
38    while p do
39       local pid = p.id
40       if pid == id_glyph then
41          if (has_attr(p, attr_icflag) or 0)<=0 and ltjc_is_ucs_in_japanese_char(p) then
42             p.font = has_attr(p, attr_curjfnt) or p.font
43             p.subtype = floor(p.subtype*0.5)*2
44             set_attr(p, attr_orig_char, p.char)
45          end
46       elseif pid == id_math then 
47          p = node_next(p) -- skip math on
48          while p and p.id~=id_math do p = node_next(p) end
49       elseif pid == id_whatsit and p.subtype==sid_user and p.user_id==STCK then
50          wt[#wt+1] = p; head = node_remove(head, p)
51       end
52       p = node_next(p)
53    end
54    lang.hyphenate(head)
55    return head
56 end
57
58 -- mode: true iff this function is called from hpack_filter
59 local function set_box_stack_level(head, mode)
60    local box_set, cl = 0, tex.currentgrouplevel + 1
61    for _,p  in pairs(wt) do
62       if mode and p.value==cl then box_set = 1 end; node_free(p)
63    end
64    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
65    return head
66 end
67
68 -- CALLBACKS
69 luatexbase.add_to_callback('hpack_filter', 
70    function (head)
71      return set_box_stack_level(head, true)
72    end,'ltj.hpack_filter_pre',1)
73 luatexbase.add_to_callback('pre_linebreak_filter', 
74   function (head)
75      return set_box_stack_level(head, false)
76   end,'ltj.pre_linebreak_filter_pre',1)
77 luatexbase.add_to_callback('hyphenate', 
78  function (head,tail)
79     return suppress_hyphenate_ja(head)
80  end,'ltj.hyphenate')
81
82 luatexja.pretreat = {
83    set_box_stack_level = set_box_stack_level,
84 }