2 -- luatexja/charrange.lua
4 luatexbase.provides_module({
5 name = 'luatexja.charrange',
7 description = 'Handling the range of Japanese characters',
9 module('luatexja.charrange', package.seeall)
10 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12 luatexja.load_module('base'); local ltjb = luatexja.base
15 local floor = math.floor
17 local has_attr = node.has_attribute
18 local kcat_attr_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)
24 pow_table[31*ATTR_RANGE] = pow(2, 31)
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'
32 local jcr_table_main = jcr_table_main
33 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
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
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" ..
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].')
54 for i=math.max(0x80,b),math.min(ucs_out-1,e) do
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.')
66 elseif c<0x80 then return -1
67 else return jcr_table_main[c] or 0 end
70 function get_range_setting(i) -- i: internal range number
71 return floor(tex.getattribute(kcat_attr_table[i])/pow_table[i])%2
74 -- glyph_node p は和文文字か?
75 function is_ucs_in_japanese_char(p)
80 local i=jcr_table_main[c]
82 has_attr(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
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
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])