OSDN Git Service

d2b22fee9b8fc0c8a30cd90ffd18def8bf66ebba
[luatex-ja/luatexja.git] / src / ltj-pretreat.lua
1 --
2 -- luatexja/pretreat.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.pretreat',
6   date = '2012/07/19',
7   version = '0.2',
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 local tex_getcount = tex.getcount
26
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')
31
32 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
33 local attr_icflag = luatexbase.attributes['ltj@icflag']
34
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
39 ------------------------------------------------------------------------
40 -- MAIN PROCESS STEP 1: replace fonts
41 ------------------------------------------------------------------------
42 box_stack_level = 0
43 -- This is used in jfmglue.lua.
44
45 local function suppress_hyphenate_ja(head)
46    local non_math, p = true, nil
47    for p in node_traverse(head) do
48       if p.id == id_glyph and non_math then
49          if (has_attr(p, attr_icflag) or 0)<=0 and ltjc_is_ucs_in_japanese_char(p) then
50             p.font = has_attr(p, attr_curjfnt) or p.font
51             p.subtype = floor(p.subtype*0.5)*2
52             set_attr(p, attr_orig_char, p.char)
53          end
54       elseif p.id == id_math then 
55          non_math = (p.subtype ~= 0)
56       end
57    end
58    lang.hyphenate(head)
59    return head
60 end
61
62 -- mode: true iff this function is called from hpack_filter
63 function set_box_stack_level(head, mode)
64    local box_set = false
65    local p = head
66    local cl = tex.currentgrouplevel + 1
67    for p in node.traverse_id(id_whatsit, head) do
68       if p.subtype==sid_user and p.user_id==30112 then
69          local g = p
70          if mode and g.value==cl then box_set = true end
71          head, p = node_remove(head, g); node_free(g); break
72       end
73    end
74    box_stack_level = tex_getcount('ltj@@stack') + (box_set and 1 or 0)
75    return head
76 end
77
78 -- CALLBACKS
79 luatexbase.add_to_callback('hpack_filter', 
80    function (head)
81      return set_box_stack_level(head, true)
82    end,'ltj.hpack_filter_pre',1)
83 luatexbase.add_to_callback('pre_linebreak_filter', 
84   function (head)
85      return set_box_stack_level(head, false)
86   end,'ltj.pre_linebreak_filter_pre',1)
87 luatexbase.add_to_callback('hyphenate', 
88  function (head,tail)
89     return suppress_hyphenate_ja(head)
90  end,'ltj.hyphenate')