OSDN Git Service

mapped internal charrange number 0 to external charrange number 217
[luatex-ja/luatexja.git] / src / ltj-charrange.lua
1 --
2 -- luatexja/charrange.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.charrange',
6   date = '2014/01/19',
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 217, 1  2       216, 217 and (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 はエラーにしない(隠し)
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 .. ",\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    if ind == 31*ATTR_RANGE then ind=0 end
55    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
56       jcr_table_main[i]=ind
57    end
58 end
59
60 function char_to_range(c) -- return the external range number
61    if not c or c<0 or c>0x10FFFF then
62          ltjb.package_error('luatexja',
63                             'bad character code (' .. tostring(c) .. ')',
64                             'A character number must be between 0 and 0x10ffff.\n' ..
65                              'So I changed this one to zero.')
66          return -1
67    elseif c<0x80 then return -1
68    else 
69       local r = jcr_table_main[c] or 217
70       return (r and r~=0) and r or 217 
71    end
72 end
73
74 function get_range_setting(i) -- i: internal range number
75    return floor(tex.getattribute(kcat_attr_table[i])/pow_table[i])%2
76 end
77
78 --  glyph_node p は和文文字か?
79 function is_ucs_in_japanese_char(p)
80    local c = p.char
81    if c<0x80 then
82       return false
83    else
84       local i=jcr_table_main[c]
85       return (floor(
86                  has_attr(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
87    end
88 end
89
90 -- EXT
91 function toggle_char_range(g, i) -- i: external range number
92    if type(i)~='number' then
93               ltjb.package_error('luatexja',
94                                  "invalid character range number (" .. tostring(i).. ")",
95                                  "A character range number must be a number, ignored.")
96    elseif i==0 then return
97    else
98       local kc
99       if i>0 then kc=0 else kc=1; i=-i end
100       if i>=7*ATTR_RANGE then i=0 end
101       local attr = kcat_attr_table[i]
102       local a = tex.getattribute(attr)
103       tex.setattribute(g,attr,(floor(a/pow_table[i+1])*2+kc)*pow_table[i]+a%pow_table[i])
104    end
105 end
106
107 -- EOF