OSDN Git Service

Added "ltj.find_char_class" and "ltj.define_jfont" callbacks; otf.lua contains their...
[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
23 local id_glyph = node.id('glyph')
24 local id_whatsit = node.id('whatsit')
25 local sid_user = node.subtype('user_defined')
26
27 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
28 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
29 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
30 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
31
32 local ltjf_font_metric_table = ltjf.font_metric_table
33 local ltjf_find_char_class = ltjf.find_char_class
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       if ltjc.is_ucs_in_japanese_char(p) then
44          local v = has_attr(p, attr_curjfnt)
45          if v then 
46             p.font = v 
47             set_attr(p, attr_jchar_class,
48                      ltjf_find_char_class(p.char, ltjf_font_metric_table[v]))
49          end
50          v = has_attr(p, attr_ykblshift)
51          if v then 
52             set_attr(p, attr_yablshift, v)
53          else
54             unset_attr(p, attr_yablshift)
55          end
56          if p.subtype%2==1 then p.subtype = p.subtype - 1 end
57          -- p.lang=lang_ja
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); break
74       end
75    end
76    if box_set then 
77       box_stack_level = tex.getcount('ltj@@stack') + 1 
78    else 
79       box_stack_level = tex.getcount('ltj@@stack') 
80    end
81    return head
82 end
83
84 -- CALLBACKS
85 luatexbase.add_to_callback('hpack_filter', 
86    function (head)
87      return set_box_stack_level(head, true)
88    end,'ltj.hpack_filter_pre',1)
89 luatexbase.add_to_callback('pre_linebreak_filter', 
90   function (head)
91      return set_box_stack_level(head, false)
92   end,'ltj.pre_linebreak_filter_pre',1)
93 luatexbase.add_to_callback('hyphenate', 
94  function (head,tail)
95     return suppress_hyphenate_ja(head)
96  end,'ltj.hyphenate')