OSDN Git Service

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