forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc-emoji-citrus-gen.rb
131 lines (121 loc) · 3.91 KB
/
enc-emoji-citrus-gen.rb
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require File.expand_path('../jisx0208', __FILE__)
ENCODES = [
{
:name => "SHIFT_JIS-DOCOMO",
:src_zone => [0xF8..0xFC, 0x40..0xFC, 8],
:dst_ilseq => 0xFFFE,
:map => [
[0xE63E..0xE757, JISX0208::Char.from_sjis(0xF89F)],
],
},
{
:name => "ISO-2022-JP-KDDI",
:src_zone => [0x21..0x7E, 0x21..0x7E, 8],
:dst_ilseq => 0xFFFE,
:map => [
[0xE468..0xE5B4, JISX0208::Char.new(0x7521)],
[0xE5B5..0xE5CC, JISX0208::Char.new(0x7867)],
[0xE5CD..0xE5DF, JISX0208::Char.new(0x7921)],
[0xEA80..0xEAFA, JISX0208::Char.new(0x7934)],
[0xEAFB..0xEB0D, JISX0208::Char.new(0x7854)],
[0xEB0E..0xEB8E, JISX0208::Char.new(0x7A51)],
],
},
{
:name => "SHIFT_JIS-KDDI",
:src_zone => [0xF3..0xFC, 0x40..0xFC, 8],
:dst_ilseq => 0xFFFE,
:map => [
[0xE468..0xE5B4, JISX0208::Char.from_sjis(0xF640)],
[0xE5B5..0xE5CC, JISX0208::Char.from_sjis(0xF7E5)],
[0xE5CD..0xE5DF, JISX0208::Char.from_sjis(0xF340)],
[0xEA80..0xEAFA, JISX0208::Char.from_sjis(0xF353)],
[0xEAFB..0xEB0D, JISX0208::Char.from_sjis(0xF7D2)],
[0xEB0E..0xEB8E, JISX0208::Char.from_sjis(0xF3CF)],
],
},
{
:name => "SHIFT_JIS-SOFTBANK",
:src_zone => [0xF3..0xFC, 0x40..0xFC, 8],
:dst_ilseq => 0xFFFE,
:map => [
[0xE001..0xE05A, JISX0208::Char.from_sjis(0xF941)],
[0xE101..0xE15A, JISX0208::Char.from_sjis(0xF741)],
[0xE201..0xE25A, JISX0208::Char.from_sjis(0xF7A1)],
[0xE301..0xE34D, JISX0208::Char.from_sjis(0xF9A1)],
[0xE401..0xE44C, JISX0208::Char.from_sjis(0xFB41)],
[0xE501..0xE53E, JISX0208::Char.from_sjis(0xFBA1)],
],
},
]
def zone(*args)
bits = args.pop
[*args.map{|range| "0x%02X-0x%02X" % [range.begin, range.end] }, bits].join(' / ')
end
def header(params)
(<<END_HEADER_TEMPLATE % [params[:name], zone(*params[:src_zone]), params[:dst_ilseq]])
# DO NOT EDIT THIS FILE DIRECTLY
TYPE ROWCOL
NAME %s
SRC_ZONE %s
OOB_MODE ILSEQ
DST_ILSEQ 0x%04X
DST_UNIT_BITS 16
END_HEADER_TEMPLATE
end
def generate_to_ucs(params, pairs)
pairs.sort_by! {|u, c| c }
name = "EMOJI_#{params[:name]}%UCS"
open("#{name}.src", "w") do |io|
io.print header(params.merge(name: name.tr('%', '/')))
io.puts
io.puts "BEGIN_MAP"
io.print pairs.inject("") {|acc, uc| acc += "0x%04X = 0x%04X\n" % uc.reverse }
io.puts "END_MAP"
end
end
def generate_from_ucs(params, pairs)
pairs.sort_by! {|u, c| u }
name = "UCS%EMOJI_#{params[:name]}"
open("#{name}.src", "w") do |io|
io.print header(params.merge(name: name.tr('%', '/')))
io.puts
io.puts "BEGIN_MAP"
io.print pairs.inject("") {|acc, uc| acc += "0x%04X = 0x%04X\n" % uc }
io.puts "END_MAP"
end
end
def make_pairs(code_map)
pairs = code_map.inject([]) {|acc, (range, ch)|
acc += range.map{|uni| pair = [uni, Integer(ch)]; ch = ch.succ; next pair }
}
end
ENCODES.each do |params|
pairs = make_pairs(params[:map], ¶ms[:conv])
generate_to_ucs(params, pairs)
generate_from_ucs(params, pairs)
end
# generate KDDI-UNDOC for Shift_JIS-KDDI
kddi_sjis_map = ENCODES.select{|enc| enc[:name] == "SHIFT_JIS-KDDI"}.first[:map]
pairs = kddi_sjis_map.inject([]) {|acc, (range, ch)|
acc += range.map{|uni| pair = [ch.to_sjis - 0x700, Integer(ch)]; ch = ch.succ; next pair }
}
params = {
:name => "SHIFT_JIS-KDDI-UNDOC",
:src_zone => [0xF3..0xFC, 0x40..0xFC, 8],
:dst_ilseq => 0xFFFE,
}
generate_from_ucs(params, pairs)
generate_to_ucs(params, pairs)
# generate KDDI-UNDOC for ISO-2022-JP-KDDI
kddi_2022_map = ENCODES.select{|enc| enc[:name] == "ISO-2022-JP-KDDI"}.first[:map]
pairs = kddi_2022_map.each_with_index.inject([]) {|acc, ((range, ch), i)|
sjis = kddi_sjis_map[i][1]
acc += range.map{|uni| pair = [sjis.to_sjis - 0x700, Integer(ch)]; ch = ch.succ; sjis = sjis.succ; next pair }
}
params = {
:name => "ISO-2022-JP-KDDI-UNDOC",
:src_zone => [0x21..0x7E, 0x21..0x7E, 8],
:dst_ilseq => 0xFFFE,
}
generate_from_ucs(params, pairs)