OSDN Git Service

Rewrited process_input_buffer callback (ticket #25231).
[luatex-ja/luatexja.git] / src / luatexja / charrange.lua
1 --
2 -- luatexja/charrange.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.charrange',
6   date = '2011/04/01',
7   version = '0.1',
8   description = 'Handling the range of Japanese characters',
9 })
10 module('luatexja.charrange', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 local floor = math.floor
14 local has_attr = node.has_attribute
15
16 -- jcr_table_main[chr_code] = index
17 -- index : internal 0, 1, 2, ..., 216               0: 'other'
18 --         external    1  2       216, (out of range): 'other'
19
20 -- initialize 
21 local jcr_table_main = {}
22 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
23
24 for i=0x80 ,0xFF      do jcr_table_main[i]=1 end
25 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
26
27 -- EXT: add characters to a range
28 function add_char_range(b,e,ind) -- ind: external range number
29    if ind<0 or ind>216 then 
30       ltj.error('Invalid range number (' .. ind ..
31                 '), should be in the range 1..216.',
32              {}); return
33    end
34    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
35       jcr_table_main[i]=ind
36    end
37 end
38
39 function char_to_range(c) -- return the (external) range number
40    if c<0x80 or c>=ucs_out then return -1
41    else 
42       local i = jcr_table_main[c] or 0
43       if i==0 then return 217 else return i end
44    end
45 end
46
47 function get_range_setting(i) -- i: internal range number
48    return floor(tex.getattribute(
49                         luatexbase.attributes['ltj@kcat'..floor(i/31)])
50                      /math.pow(2, i%31))%2
51 end
52
53 --  glyph_node p は和文文字か?
54 function is_ucs_in_japanese_char(p)
55    local c = p.char
56    if c<0x80 then return false 
57    else 
58       local i=jcr_table_main[c] 
59       return (floor(
60                  has_attr(p, luatexbase.attributes['ltj@kcat'..floor(i/31)])
61                  /math.pow(2, i%31))%2 ~= jcr_noncjk) 
62    end
63 end
64
65 -- EXT
66 function toggle_char_range(g, i) -- i: external range number
67    if i==0 then return 
68    else
69       local kc
70       if i>0 then kc=0 else kc=1; i=-i end
71       if i>216 then i=0 end
72       local attr = luatexbase.attributes['ltj@kcat'..floor(i/31)]
73       local a = tex.getattribute(attr)
74       local k = math.pow(2, i%31)
75       tex.setattribute(g,attr,(floor(a/k/2)*2+kc)*k+a%k)
76    end
77 end
78
79 -- EOF