OSDN Git Service

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