OSDN Git Service

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