OSDN Git Service

... and more
[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 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
18
19 ATTR_RANGE = 7
20 local floor = math.floor
21 local pow = math.pow
22 local kcat_attr_table = {}
23 local pow_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)
27 end
28 pow_table[31*ATTR_RANGE] = pow(2, 31)
29
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'
33
34 -- initialize
35 jcr_table_main = {}
36 local jcr_table_main = jcr_table_main
37 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
38
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
41
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" ..
49                           "ignored.")
50       return
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].')
55    elseif b>e then
56       local j=b; e=b; b=j
57    end
58    if ind == 31*ATTR_RANGE then ind=0 end
59    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
60       jcr_table_main[i]=ind
61    end
62 end
63
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
67    else 
68       local r = jcr_table_main[c] or 217
69       return (r and r~=0) and r or 217 
70    end
71 end
72
73 function get_range_setting(i) -- i: internal range number
74    return floor(tex.getattribute(kcat_attr_table[i])/pow_table[i])%2
75 end
76
77 --  glyph_node p は和文文字か?
78
79 function is_ucs_in_japanese_char_node(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_node(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
87    end
88 end
89 is_ucs_in_japanese_char = is_ucs_in_japanese_char_node
90
91 function is_ucs_in_japanese_char_direct(p)
92    local c = getchar(p)
93    if c<0x80 then
94       return false
95    else
96       local i=jcr_table_main[c]
97       return (floor(
98                  has_attr(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
99    end
100 end
101
102 -- EXT
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
109    else
110       local kc
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])
116    end
117 end
118
119 -- EOF