OSDN Git Service

Added the table of non-Kanji characters of JIS X 0208:1990.
[luatex-ja/luatexja.git] / src / ltj-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 luatexja.load_module('base');      local ltjb = luatexja.base
14
15 local floor = math.floor
16 local pow = math.pow
17 local has_attr = node.has_attribute
18 local kcat_attr_table = {}
19 local pow_table = {}
20 for i = 0, 216 do
21    kcat_attr_table[i] = luatexbase.attributes['ltj@kcat'..floor(i/31)]
22    pow_table[i] =  pow(2, i%31)
23 end
24 pow_table[217] = pow(2, 31)
25
26 -- jcr_table_main[chr_code] = index
27 -- index : internal 0, 1, 2, ..., 216               0: 'other'
28 --         external    1  2       216, (out of range): 'other'
29
30 -- initialize 
31 local jcr_table_main = {}
32 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
33
34 for i=0x80 ,0xFF      do jcr_table_main[i]=1 end
35 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
36
37 -- EXT: add characters to a range
38 function add_char_range(b,e,ind) -- ind: external range number
39    if not ind or ind<0 or ind>216 then -- 0 は error にしない(隠し)
40       ltjb.package_error('luatexja',
41                          "invalid character range number (" .. ind .. ")",
42                          {"A character range number should be in the range 1..216,",
43                           "ignored."})
44       return
45    elseif b<0x80 or e>=ucs_out then
46       ltjb.package_warning('luatexja',
47                          'bad character range ([' .. b .. ',' .. e .. ']). ' ..
48                            'I take the intersection with [0x80, 0x10ffff].')
49    elseif b>e then
50       local j=b; e=b; b=j
51    end 
52    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
53       jcr_table_main[i]=ind
54    end
55 end
56
57 function char_to_range(c) -- return the (external) range number
58    if not c or c<0 or c>0x10FFFF then
59          ltjb.package_error('luatexja',
60                             'bad character code (' .. tostring(c) .. ')',
61                             {'A character number must be between 0 and 0x10ffff.',
62                              'So I changed this one to zero.'})
63          c=0
64    elseif c<0x80 then return -1
65    else 
66       local i = jcr_table_main[c] or 0
67       if i==0 then return 217 else return i end
68    end
69 end
70
71 function get_range_setting(i) -- i: internal range number
72    return floor(tex.getattribute(kcat_attr_table[i])/pow_table[i])%2
73 end
74
75 --  glyph_node p は和文文字か?
76 function is_ucs_in_japanese_char(p)
77    local c = p.char
78    if c<0x80 then 
79       return false 
80    else 
81       local i=jcr_table_main[c] 
82       return (floor(
83                  has_attr(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk) 
84    end
85 end
86
87 -- EXT
88 function toggle_char_range(g, i) -- i: external range number
89    if type(i)~='number' then
90               ltjb.package_error('luatexja',
91                                  "invalid character range number (" .. tostring(i).. ")",
92                                  "A character range number must be a number, ignored.")
93    elseif i==0 then return 
94    else
95       local kc
96       if i>0 then kc=0 else kc=1; i=-i end
97       if i>216 then i=0 end
98       local attr = kcat_attr_table[i]
99       local a = tex.getattribute(attr)
100       tex.setattribute(g,attr,(floor(a/pow_table[i+1])*2+kc)*pow_table[i]+a%pow_table[i])
101    end
102 end
103
104 -- EOF