OSDN Git Service

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