root/lang/ruby/ssb/trunk/libs/ssb/emoji.rb @ 2282

Revision 2282, 2.8 kB (checked in by tokuhirom, 5 years ago)

lang/ruby/ssb: added ez hex cref pictogram support.

  • Property svn:mime-type set to text/x-ruby; charset=utf-8
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date Author Rev URL
Line 
1# emoji.rb - emoji convert
2#
3# Author:: MIZOGUCHI Coji <mizoguchi.coji at gmail.com>
4# License:: Distributes under the same terms as Ruby
5#
6# $Id$
7#
8require 'yaml'
9
10module SSB
11  module Emoji
12    SJIS_ONE_BYTE  = '[\x00-\x7F\xA1-\xDF]'
13    SJIS_TWO_BYTES = '[\x81-\x9F\xE0-\xFC][\x40-\x7E\x80-\xFC]'
14    SJIS_IMODE_PICTOGRAM = '\xF8[\x9F-\xFC]|\xF9[\x40-\x7E\x80-\xB0]|\xF9[\xB1-\xFC]'
15    RE_IMODE_SJIS = Regexp.new("((#{SJIS_IMODE_PICTOGRAM})|(#{SJIS_ONE_BYTE}|#{SJIS_TWO_BYTES}))")
16
17    # TODO: speed up!
18    def self.ez_uni2number(uni)
19      table = YAML.load_file(File.join(SSB::CONFIG[:dat_dir], 'kddi-table.yaml'))
20      table.each {|x|
21        if x['unicode'] == uni
22          return x['number']
23        end
24      }
25      nil
26    end
27
28    def self.emoji_conv(term, str)
29      ret = str
30      # SJIS HEX Binary
31      ret.gsub!(RE_IMODE_SJIS) do |s|
32        orig_match = $1
33        if not $2.nil?
34          sprintf '<img class="emoji" src="emoji/docomo/%X.gif" />', orig_match.unpack('n')[0]
35        else
36          orig_match
37        end
38      end
39
40      # SJIS 10進数実体参照
41      ret.gsub!(/&#([0-9]+);/) do |s|
42        '<img class="emoji" src="emoji/docomo/' + $1.to_i.to_s(16).upcase + '.gif" />'
43      end
44
45      # UNICODE 16進数実体参照 (とりあえず拡張絵文字だけ)
46      ret.gsub!(/(&#x([0-9a-zA-Z]+);)/) do |s|
47        code =
48          case ($2.hex)
49          when 0xE63E..0xE69B
50            # 4705
51            $2.hex + 4705
52          when 0xE69C..0xE6DA, 0xE6AC..0xE6BA
53            # 4772
54            $2.hex + 4772
55          when 0xE6DB..0xE70A, 0xE70C..0xE757
56            # 4773
57            $2.hex + 4773
58          else
59            $1
60          end
61        if code.is_a?(Integer)
62          '<img class="emoji" src="emoji/docomo/' + (code).to_s(16).upcase + '.gif" />'
63        else
64          code
65        end
66      end
67
68      # KDDI localsrc
69      if term.get_carrier == KtaiSpec::CARRIER_KDDI
70        ret.gsub!(/(&#x([0-9a-zA-Z]+);)/) do |s|
71          number = SSB::Emoji.ez_uni2number($2)
72          if number.nil?
73            $1
74          else
75            sprintf '<img class="emoji" src="emoji/kddi/%03d.gif" />', number
76          end
77        end
78
79        ret.gsub!(/ (localsrc)=\"?([^>\" ]+)\"?/im) { |s|
80          ' class="emoji" src="emoji/kddi/' + '%03d' % $2.to_i + '.gif"'
81        }
82      end
83
84      # Thirdforce
85      re_sb_emoji = Regexp.new('\x1B\$(..)\x0F', 0, 'n')
86      if term.get_carrier == KtaiSpec::CARRIER_SOFTBANK
87        ret.gsub!(re_sb_emoji) { |s|
88          page_map = {
89            ?G => 1,
90            ?E => 2,
91            ?F => 3,
92            ?O => 4,
93            ?P => 5,
94            ?Q => 6,
95          }
96          code = ( (0xE0 + page_map[$1[0]] - 1 ) << 8) + ($1[1] - ?! + 1)
97
98          sprintf "<img class='emoji' src='emoji/softbank/%04X.gif' />" % code
99        }
100      end
101
102      ret
103    end
104  end
105end
106
Note: See TracBrowser for help on using the browser.