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
14 local Dnode = node.direct or node
15 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
16 local has_attr = Dnode.has_attribute
17 local has_attr_node = node.has_attribute
20 local floor = math.floor
22 local kcat_attr_table = {}
24 for i = 0, 31*ATTR_RANGE-1 do
25 kcat_attr_table[i] = luatexbase.attributes['ltj@kcat'..floor(i/31)]
26 pow_table[i] = pow(2, i%31)
28 pow_table[31*ATTR_RANGE] = pow(2, 31)
30 -- jcr_table_main[chr_code] = index
31 -- index : internal 0, 1, 2, ..., 216 0: 'other'
32 -- external 217, 1 2 216, 217 and (out of range): 'other'
36 local jcr_table_main = jcr_table_main
37 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
39 for i=0x80 ,0xFF do jcr_table_main[i]=1 end
40 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
42 -- EXT: add characters to a range
43 function add_char_range(b,e,ind) -- ind: external range number
44 if not ind or ind<0 or ind>31*ATTR_RANGE then -- 0 はエラーにしない(隠し)
45 ltjb.package_error('luatexja',
46 "invalid character range number (" .. ind .. ")",
47 "A character range number should be in the range 1.."
48 .. 31*ATTR_RANGE .. ",\n" ..
51 elseif b<0x80 or e>=ucs_out then
52 ltjb.package_warning('luatexja',
53 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
54 'I take the intersection with [0x80, 0x10ffff].')
58 if ind == 31*ATTR_RANGE then ind=0 end
59 for i=math.max(0x80,b),math.min(ucs_out-1,e) do
64 function char_to_range(c) -- return the external range number
65 c=ltjb.in_unicode(c, false)
66 if c<0x80 then return -1
68 local r = jcr_table_main[c] or 217
69 return (r and r~=0) and r or 217
73 function get_range_setting(i) -- i: internal range number
74 return floor(tex.getattribute(kcat_attr_table[i])/pow_table[i])%2
77 -- glyph_node p は和文文字か?
79 function is_ucs_in_japanese_char_node(p)
84 local i=jcr_table_main[c]
86 has_attr_node(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
89 is_ucs_in_japanese_char = is_ucs_in_japanese_char_node
91 function is_ucs_in_japanese_char_direct(p)
96 local i=jcr_table_main[c]
98 has_attr(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
103 function toggle_char_range(g, i) -- i: external range number
104 if type(i)~='number' then
105 ltjb.package_error('luatexja',
106 "invalid character range number (" .. tostring(i).. ")",
107 "A character range number must be a number, ignored.")
108 elseif i==0 then return
111 if i>0 then kc=0 else kc=1; i=-i end
112 if i>=7*ATTR_RANGE then i=0 end
113 local attr = kcat_attr_table[i]
114 local a = tex.getattribute(attr)
115 tex.setattribute(g,attr,(floor(a/pow_table[i+1])*2+kc)*pow_table[i]+a%pow_table[i])