OSDN Git Service

6785c048a21260ee74c640aafdf9406091596fd5
[luatex-ja/luatexja.git] / src / luatexja / 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 require('luatexja.charrange'); local ltjc = luatexja.charrange
14 require('luatexja.jfont');     local ltjf = luatexja.jfont
15 require('luatexja.stack');     local ltjs = luatexja.stack
16
17 local has_attr = node.has_attribute
18 local set_attr = node.set_attribute
19 local unset_attr = node.unset_attribute
20 local node_remove = node.remove
21 local node_next = node.next
22 local node_free = node.free
23
24 local id_glyph = node.id('glyph')
25 local id_whatsit = node.id('whatsit')
26 local sid_user = node.subtype('user_defined')
27
28 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
29 local attr_icflag = luatexbase.attributes['ltj@icflag']
30 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
31 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
32
33 local ltjf_font_metric_table = ltjf.font_metric_table
34
35 ------------------------------------------------------------------------
36 -- MAIN PROCESS STEP 1: replace fonts
37 ------------------------------------------------------------------------
38 box_stack_level = 0
39 -- This is used in jfmglue.lua.
40
41 local function suppress_hyphenate_ja(head)
42    for p in node.traverse_id(id_glyph, head) do
43       local i = has_attr(p, attr_icflag) or 0
44       if i==0 and ltjc.is_ucs_in_japanese_char(p) then
45          local v = has_attr(p, attr_curjfnt)
46          if v then 
47             p.font = v 
48          end
49          v = has_attr(p, attr_ykblshift)
50          if v then 
51             set_attr(p, attr_yablshift, v)
52          else
53             unset_attr(p, attr_yablshift)
54          end
55          if p.subtype%2==1 then p.subtype = p.subtype - 1 end
56          -- p.lang=lang_ja
57       end
58    end
59    lang.hyphenate(head)
60    return head
61 end
62
63 -- mode: true iff this function is called from hpack_filter
64 function set_box_stack_level(head, mode)
65    local box_set = false
66    local p = head
67    local cl = tex.currentgrouplevel + 1
68    for p in node.traverse_id(id_whatsit, head) do
69       if p.subtype==sid_user and p.user_id==30112 then
70          local g = p
71          if mode and g.value==cl then box_set = true end
72          head, p = node_remove(head, g); node_free(g); break
73       end
74    end
75    if box_set then 
76       box_stack_level = tex.getcount('ltj@@stack') + 1 
77    else 
78       box_stack_level = tex.getcount('ltj@@stack') 
79    end
80    return head
81 end
82
83 -- CALLBACKS
84 luatexbase.add_to_callback('hpack_filter', 
85    function (head)
86      return set_box_stack_level(head, true)
87    end,'ltj.hpack_filter_pre',1)
88 luatexbase.add_to_callback('pre_linebreak_filter', 
89   function (head)
90      return set_box_stack_level(head, false)
91   end,'ltj.pre_linebreak_filter_pre',1)
92 luatexbase.add_to_callback('hyphenate', 
93  function (head,tail)
94     return suppress_hyphenate_ja(head)
95  end,'ltj.hyphenate')