OSDN Git Service

Fix lltjp-unicode-math.sty.
[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 local tex_getattr = tex.getattribute
19
20 ATTR_RANGE = 7
21 local floor = math.floor
22 local pow = math.pow
23 local kcat_attr_table = {}
24 local pow_table = {}
25 local fn_table = {} -- used in is_ucs_in_japanese_char_direct
26 for i = 0, 31*ATTR_RANGE-1 do
27    kcat_attr_table[i] = luatexbase.attributes['ltj@kcat'..floor(i/31)]
28    pow_table[i] =  pow(2, i%31)
29    fn_table[i] = function(p)
30       return floor(has_attr(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk
31    end
32 end
33 pow_table[31*ATTR_RANGE] = pow(2, 31)
34
35 -- jcr_table_main[chr_code] = index
36 -- index : internal 0,   1, 2, ..., 216               0: 'other'
37 --         external 217, 1  2       216, 217 and (out of range): 'other'
38
39 -- initialize
40 jcr_table_main = {}
41 local jcr_table_main = jcr_table_main
42 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
43
44 for i=0x80 ,0xFF      do jcr_table_main[i]=1 end
45 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
46
47 -- EXT: add characters to a range
48 function add_char_range(b,e,ind) -- ind: external range number
49    if not ind or ind<0 or ind>31*ATTR_RANGE then -- 0 はエラーにしない(隠し)
50       ltjb.package_error('luatexja',
51                          "invalid character range number (" .. ind .. ")",
52                          "A character range number should be in the range 1.."
53                           .. 31*ATTR_RANGE .. ",\n" ..
54                           "ignored.")
55       return
56    elseif b<0x80 or e>=ucs_out then
57       ltjb.package_warning('luatexja',
58                          'bad character range ([' .. b .. ',' .. e .. ']). ' ..
59                            'I take the intersection with [0x80, 0x10ffff].')
60    elseif b>e then
61       local j=b; e=b; b=j
62    end
63    if ind == 31*ATTR_RANGE then ind=0 end
64    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
65       jcr_table_main[i]=ind
66    end
67 end
68
69 function char_to_range(c) -- return the external range number
70    c=ltjb.in_unicode(c, false)
71    if c<0x80 then return -1
72    else 
73       local r = jcr_table_main[c] or 217
74       return (r and r~=0) and r or 217 
75    end
76 end
77
78 function get_range_setting(i) -- i: internal range number
79    return floor(tex_getattr(kcat_attr_table[i])/pow_table[i])%2
80 end
81
82 --  glyph_node p は和文文字か?
83 function is_ucs_in_japanese_char_node(p)
84    local c = p.char
85    if c<0x80 then
86       return false
87    else
88       local i=jcr_table_main[c]
89       return (floor(
90                  has_attr_node(p, kcat_attr_table[i])/pow_table[i])%2 ~= jcr_noncjk)
91    end
92 end
93 is_ucs_in_japanese_char = is_ucs_in_japanese_char_node
94
95 function is_ucs_in_japanese_char_direct(p)
96    local c = getchar(p)
97    if c<0x80 then
98       return false
99    else
100       return fn_table[jcr_table_main[c]](p)
101    end
102 end
103
104 function is_japanese_char_curlist(c) -- assume that c>=0x80
105    local i=jcr_table_main[c]
106    return get_range_setting(i)~= jcr_noncjk
107 end
108
109 -- EXT
110 function toggle_char_range(g, i) -- i: external range number
111    if type(i)~='number' then
112               ltjb.package_error('luatexja',
113                                  "invalid character range number (" .. tostring(i).. ")",
114                                  "A character range number must be a number, ignored.")
115    elseif i==0 then return
116    else
117       local kc
118       if i>0 then kc=0 else kc=1; i=-i end
119       if i>=7*ATTR_RANGE then i=0 end
120       local attr = kcat_attr_table[i]
121       local a = tex_getattr(attr)
122       tex.setattribute(g,attr,(floor(a/pow_table[i+1])*2+kc)*pow_table[i]+a%pow_table[i])
123    end
124 end
125
126 -- EOF