OSDN Git Service

Fix #29003, and changed 2nd argument of find_char_class for simplicity.
[luatex-ja/luatexja.git] / src / ltj-pretreat.lua
1 --
2 -- luatexja/pretreat.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.pretreat',
6   date = '2011/06/27',
7   version = '0.1',
8   description = '',
9 })
10 module('luatexja.pretreat', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
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
16
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
26 local id_glyph = node.id('glyph')
27 local id_math = node.id('math')
28 local id_whatsit = node.id('whatsit')
29 local sid_user = node.subtype('user_defined')
30
31 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
32 local attr_icflag = luatexbase.attributes['ltj@icflag']
33 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
34 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
35
36 local ltjf_font_metric_table = ltjf.font_metric_table
37 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
38 local attr_orig_char = luatexbase.attributes['ltj@origchar']
39
40 ------------------------------------------------------------------------
41 -- MAIN PROCESS STEP 1: replace fonts
42 ------------------------------------------------------------------------
43 box_stack_level = 0
44 -- This is used in jfmglue.lua.
45
46 local function suppress_hyphenate_ja(head)
47    local non_math = true
48    for p in node_traverse(head) do
49       if p.id == id_glyph and non_math then
50          if (has_attr(p, attr_icflag) or 0)<=0 and ltjc_is_ucs_in_japanese_char(p) then
51             p.font = has_attr(p, attr_curjfnt) or p.font
52             set_attr(p, attr_orig_char, p.char)
53             set_attr(p, attr_yablshift, has_attr(p, attr_ykblshift) or 0)
54             p.subtype = floor(p.subtype/2)*2
55          end
56       elseif p.id == id_math then 
57          non_math = (p.subtype ~= 0)
58       end
59    end
60    lang.hyphenate(head)
61    return head
62 end
63
64 -- mode: true iff this function is called from hpack_filter
65 function set_box_stack_level(head, mode)
66    local box_set = false
67    local p = head
68    local cl = tex.currentgrouplevel + 1
69    for p in node.traverse_id(id_whatsit, head) do
70       if p.subtype==sid_user and p.user_id==30112 then
71          local g = p
72          if mode and g.value==cl then box_set = true end
73          head, p = node_remove(head, g); node_free(g); break
74       end
75    end
76    box_stack_level = tex.getcount('ltj@@stack') + (box_set and 1 or 0)
77    return head
78 end
79
80 -- CALLBACKS
81 luatexbase.add_to_callback('hpack_filter', 
82    function (head)
83      return set_box_stack_level(head, true)
84    end,'ltj.hpack_filter_pre',1)
85 luatexbase.add_to_callback('pre_linebreak_filter', 
86   function (head)
87      return set_box_stack_level(head, false)
88   end,'ltj.pre_linebreak_filter_pre',1)
89 luatexbase.add_to_callback('hyphenate', 
90  function (head,tail)
91     return suppress_hyphenate_ja(head)
92  end,'ltj.hyphenate')