OSDN Git Service

e3478ab81802641d1c5e0a067d29aca9f85c6532
[luatex-ja/luatexja.git] / src / ltj-charrange.lua
1 --
2 -- luatexja/charrange.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.charrange',
6   date = '2012/10/21',
7   description = 'Handling the range of Japanese characters',
8 })
9 module('luatexja.charrange', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
11
12 luatexja.load_module('base');      local ltjb = luatexja.base
13
14 ATTR_RANGE = 7
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, 31*ATTR_RANGE-1 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[31*ATTR_RANGE] = 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 jcr_table_main = {}
32 local jcr_table_main = jcr_table_main
33 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
34
35 for i=0x80 ,0xFF      do jcr_table_main[i]=1 end
36 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
37
38 -- EXT: add characters to a range
39 function add_char_range(b,e,ind) -- ind: external range number
40    if not ind or ind<0 or ind>=31*ATTR_RANGE then -- 0 は error にしない(隠し)
41       ltjb.package_error('luatexja',
42                          "invalid character range number (" .. ind .. ")",
43                          "A character range number should be in the range 1.."
44                           .. 31*ATTR_RANGE-1 .. ",\n" ..
45                           "ignored.")
46       return
47    elseif b<0x80 or e>=ucs_out then
48       ltjb.package_warning('luatexja',
49                          'bad character range ([' .. b .. ',' .. e .. ']). ' ..
50                            'I take the intersection with [0x80, 0x10ffff].')
51    elseif b>e then
52       local j=b; e=b; b=j
53    end
54    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
55       jcr_table_main[i]=ind
56    end
57 end
58
59 function char_to_range(c) -- return the (external) range number
60    if not c or c<0 or c>0x10FFFF then
61          ltjb.package_error('luatexja',
62                             'bad character code (' .. tostring(c) .. ')',
63                             'A character number must be between 0 and 0x10ffff.\n' ..
64                              'So I changed this one to zero.')
65          c=0
66    elseif c<0x80 then return -1
67    else return  jcr_table_main[c] or 0 end
68 end
69
70 function get_range_setting(i) -- i: internal range number
71    return floor(tex.getattribute(kcat_attr_table[i])/pow_table[i])%2
72 end
73
74 --  glyph_node p は和文文字か?
75 function is_ucs_in_japanese_char(p)
76    local c = p.char
77    if c<0x80 then
78       return false
79    else
80       local i=jcr_table_main[c]
81       return (floor(
82                  has_attr(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
83    end
84 end
85
86 -- EXT
87 function toggle_char_range(g, i) -- i: external range number
88    if type(i)~='number' then
89               ltjb.package_error('luatexja',
90                                  "invalid character range number (" .. tostring(i).. ")",
91                                  "A character range number must be a number, ignored.")
92    elseif i==0 then return
93    else
94       local kc
95       if i>0 then kc=0 else kc=1; i=-i end
96       if i>=7*ATTR_RANGE then i=0 end
97       local attr = kcat_attr_table[i]
98       local a = tex.getattribute(attr)
99       tex.setattribute(g,attr,(floor(a/pow_table[i+1])*2+kc)*pow_table[i]+a%pow_table[i])
100    end
101 end
102
103 -- EOF