summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luatexja/ltj-charrange.lua
blob: 8608300357d9e4a3759f96c3a2b6714c37533b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
--
-- luatexja/charrange.lua
--
luatexbase.provides_module({
  name = 'luatexja.charrange',
  date = '2011/04/01',
  version = '0.1',
  description = 'Handling the range of Japanese characters',
})
module('luatexja.charrange', package.seeall)
local err, warn, info, log = luatexbase.errwarinf(_NAME)

luatexja.load_module('base');      local ltjb = luatexja.base

local floor = math.floor
local has_attr = node.has_attribute

-- jcr_table_main[chr_code] = index
-- index : internal 0, 1, 2, ..., 216               0: 'other'
--         external    1  2       216, (out of range): 'other'

-- initialize 
local jcr_table_main = {}
local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000

for i=0x80 ,0xFF      do jcr_table_main[i]=1 end
for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end

-- EXT: add characters to a range
function add_char_range(b,e,ind) -- ind: external range number
   if not ind or ind<0 or ind>216 then 
      ltjb.package_error('luatexja',
			 "invalid character range number (" .. ind .. ")",
			 {"A character range number should be in the range 1..216,",
			  "ignored."})
      return
   elseif b<0x80 or e>=ucs_out then
      ltjb.package_warning('luatexja',
			 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
			   'I take the intersection with [0x80, 0x10ffff].')
   elseif b>e then
      local j=b; e=b; b=j
   end 
   for i=math.max(0x80,b),math.min(ucs_out-1,e) do
      jcr_table_main[i]=ind
   end
end

function char_to_range(c) -- return the (external) range number
   if c<0 or c>0x10FFFF then
	 ltjb.package_error('luatexja',
			    'bad character code (' .. c .. ')',
			    {'A character number must be between 0 and 0x10ffff.',
			     'So I changed this one to zero.'})
	 c=0
   elseif c<0x80 then return -1
   else 
      local i = jcr_table_main[c] or 0
      if i==0 then return 217 else return i end
   end
end

function get_range_setting(i) -- i: internal range number
   return floor(tex.getattribute(
			luatexbase.attributes['ltj@kcat'..floor(i/31)])
		     /math.pow(2, i%31))%2
end

--  glyph_node p は和文文字か?
function is_ucs_in_japanese_char(p)
   local c = p.char
   if c<0x80 then return false 
   else 
      local i=jcr_table_main[c] 
      return (floor(
		 has_attr(p, luatexbase.attributes['ltj@kcat'..floor(i/31)])
		 /math.pow(2, i%31))%2 ~= jcr_noncjk) 
   end
end

-- EXT
function toggle_char_range(g, i) -- i: external range number
   if i==0 then return 
   else
      local kc
      if i>0 then kc=0 else kc=1; i=-i end
      if i>216 then i=0 end
      local attr = luatexbase.attributes['ltj@kcat'..floor(i/31)]
      local a = tex.getattribute(attr)
      local k = math.pow(2, i%31)
      tex.setattribute(g,attr,(floor(a/k/2)*2+kc)*k+a%k)
   end
end

-- EOF