OSDN Git Service

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