module Jpmobile::Emoticon

絵文字関連処理

Constants

AU_EMAILJIS_REGEXP
AU_SJIS_REGEXP
CONVERSION_TABLE_TO_PC_EMAIL

for PC conversion “GETA”

DOCOMO_SJIS_REGEXP
EMOTICON_UNICODES
GETA
SJIS_REGEXP
SJIS_TO_EMAIL_JIS

for au email

SJIS_TO_UNICODE
SOFTBANK_SJIS_REGEXP
SOFTBANK_UNICODE_REGEXP
SOFTBANK_WEBCODE_REGEXP
UNICODE_TO_SJIS
UTF8_REGEXP

Public Class Methods

emoticons_to_image(str) click to toggle source
# File lib/jpmobile/emoticon.rb, line 244
def self.emoticons_to_image(str)
  if @@pc_emoticon_hash
    utf8_to_unicodecr(str).gsub(%r&#x([0-9a-f]{4});/) do |match|
      img = @@pc_emoticon_hash[$1.upcase] || (@@pc_emoticon_hash[("%x" % ($1.scanf("%x").first - 0x1000)).upcase] rescue nil)
      if img
        "<img src=\"#{@@pc_emoticon_image_path}/#{img}.gif\" alt=\"#{img}\" />"
      else
        ""
      end
    end
  end
end
external_to_unicodecr_au(str) click to toggle source

str のなかでau絵文字をUnicode数値文字参照に置換した文字列を返す。

# File lib/jpmobile/emoticon.rb, line 42
def self.external_to_unicodecr_au(str)
  str.gsub(AU_SJIS_REGEXP) do |match|
    sjis = match.unpack('n').first
    unicode = AU_SJIS_TO_UNICODE[sjis]
    unicode ? ("&#x%04x;"%unicode) : match
  end
end
external_to_unicodecr_au_mail(in_str) click to toggle source

str のなかでau絵文字をUnicode数値文字参照に置換した文字列を返す。(メール専用)

# File lib/jpmobile/emoticon.rb, line 51
def self.external_to_unicodecr_au_mail(in_str)
  str = Jpmobile::Util.ascii_8bit(in_str)
  str.gsub(Jpmobile::Util.jis_string_regexp) do |jis_string|
    jis_string.gsub(%r[\x21-\x7e]{2}/) do |match|
      jis = match.unpack('n').first
      unicode = AU_EMAILJIS_TO_UNICODE[jis]
      unicode ? Jpmobile::Util.ascii_8bit("\x1b\x28\x42&#x%04x;\x1b\x24\x42"%unicode) : match
    end
  end
end
external_to_unicodecr_docomo(str) click to toggle source

str のなかでDoCoMo絵文字をUnicode数値文字参照に置換した文字列を返す。

# File lib/jpmobile/emoticon.rb, line 33
def self.external_to_unicodecr_docomo(str)
  str.gsub(DOCOMO_SJIS_REGEXP) do |match|
    sjis = match.unpack('n').first
    unicode = DOCOMO_SJIS_TO_UNICODE[sjis]
    unicode ? ("&#x%04x;"%unicode) : match
  end
end
external_to_unicodecr_softbank(str) click to toggle source

strのなかでUTF8のSoftBank絵文字を(+0x1000だけシフトして)Unicode数値文字参照に変換した文字列を返す。

# File lib/jpmobile/emoticon.rb, line 63
def self.external_to_unicodecr_softbank(str)
  # SoftBank Unicode
  str.gsub(SOFTBANK_UNICODE_REGEXP) do |match|
    unicode = match.unpack('U').first
    "&#x%04x;" % (unicode+0x1000)
  end
end
external_to_unicodecr_softbank_sjis(str) click to toggle source
# File lib/jpmobile/emoticon.rb, line 70
def self.external_to_unicodecr_softbank_sjis(str)
  # SoftBank Shift_JIS
  str.gsub(SOFTBANK_SJIS_REGEXP) do |match|
    sjis = match.unpack('n').first
    unicode = SOFTBANK_SJIS_TO_UNICODE[sjis]
    "&#x%04x;" % (unicode+0x1000)
  end
end
external_to_unicodecr_vodafone(str) click to toggle source
# File lib/jpmobile/emoticon.rb, line 78
def self.external_to_unicodecr_vodafone(str)
  external_to_unicodecr_softbank(str)
end
pc_emoticon?() click to toggle source
# File lib/jpmobile/emoticon.rb, line 224
def self.pc_emoticon?
  if @@pc_emoticon_yaml and File.exist?(@@pc_emoticon_yaml) and @@pc_emoticon_image_path

    unless @@pc_emoticon_hash
      begin
        yaml_hash = YAML.load_file(@@pc_emoticon_yaml)
        @@pc_emoticon_hash = Hash[*(yaml_hash.values.inject([]){ |r, v| r += v.to_a.flatten; r})]
        @@pc_emoticon_image_path.chop if @@pc_emoticon_image_path.match(%r\/$/)

        return true
      rescue => ex
      end
    else
      return true
    end
  end

  return false
end
pc_emoticon_image_path() click to toggle source
# File lib/jpmobile/emoticon.rb, line 210
def self.pc_emoticon_image_path
  @@pc_emoticon_image_path
end
pc_emoticon_image_path=(path) click to toggle source
# File lib/jpmobile/emoticon.rb, line 213
def self.pc_emoticon_image_path=(path)
  @@pc_emoticon_image_path=(path)
end
pc_emoticon_yaml() click to toggle source
# File lib/jpmobile/emoticon.rb, line 220
def self.pc_emoticon_yaml
  @@pc_emoticon_yaml
end
pc_emoticon_yaml=(file) click to toggle source
# File lib/jpmobile/emoticon.rb, line 217
def self.pc_emoticon_yaml=(file)
  @@pc_emoticon_yaml = file
end
unicodecr_to_au_email(in_str) click to toggle source

str のなかでUnicode数値文字参照で表記された絵文字をメール送信用JISコードに変換する au 専用

# File lib/jpmobile/emoticon.rb, line 152
def self.unicodecr_to_au_email(in_str)
  str = Jpmobile::Util.ascii_8bit(in_str)
  regexp = Regexp.compile(Jpmobile::Util.ascii_8bit("&#x([0-9a-f]{4});"), Regexp::IGNORECASE)
  str = str.gsub(regexp) do |match|
    unicode = $1.scanf("%x").first
    converted = CONVERSION_TABLE_TO_AU[unicode]

    # メール用エンコーディングに変換する
    case converted
    when Integer
      if sjis = UNICODE_TO_SJIS[converted]
        if email_jis = SJIS_TO_EMAIL_JIS[sjis]
          Jpmobile::Util.ascii_8bit("\x1b\x24\x42#{[email_jis].pack('n')}\x1b\x28\x42")
        else
          Jpmobile::Util.ascii_8bit([sjis].pack('n'))
        end
      else
        match
      end
    when String
      # FIXME: 絵文字の代替が文章でいいかどうかの検証
      Jpmobile::Util.ascii_8bit(Jpmobile::Util.utf8_to_jis(converted))
    else
      match
    end
  end
  regexp = Regexp.compile(Regexp.escape(Jpmobile::Util.ascii_8bit("\x1b\x28\x42\x1b\x24\x42")), Regexp::IGNORECASE)
  str.gsub(regexp, '')
end
unicodecr_to_external(str, conversion_table=nil, to_sjis=true) click to toggle source

str のなかでUnicode数値文字参照で表記された絵文字を携帯側エンコーディングに置換する。

キャリア間の変換に conversion_table を使う。conversion_tablenilを与えると、 キャリア間の変換は行わない。

携帯側エンコーディングがShift_JIS場合は to_sjistrue を指定する。 true を指定すると変換テーブルに文字列が指定されている場合にShift_JISで出力される。

# File lib/jpmobile/emoticon.rb, line 89
def self.unicodecr_to_external(str, conversion_table=nil, to_sjis=true)
  str.gsub(%r&#x([0-9a-f]{4});/) do |match|
    unicode = $1.scanf("%x").first
    if conversion_table
      converted = conversion_table[unicode] # キャリア間変換
    else
      converted = unicode # 変換しない
    end

    # 携帯側エンコーディングに変換する
    case converted
    when Integer
      # 変換先がUnicodeで指定されている。つまり対応する絵文字がある。
      if sjis = UNICODE_TO_SJIS[converted]
        if to_sjis
          sjis_emotion = Jpmobile::Util.sjis([sjis].pack('n'))
        else
          [converted].pack("U")
        end
      elsif webcode = SOFTBANK_UNICODE_TO_WEBCODE[converted-0x1000]
        [converted-0x1000].pack('U')
      elsif converted == GETA
        # PCで〓を表示する場合
        [GETA].pack("U")
      else
        # キャリア変換テーブルに指定されていたUnicodeに対応する
        # 携帯側エンコーディングが見つからない(変換テーブルの不備の可能性あり)。
        match
      end
    when String
      # 変換先が数値参照だと、再変換する
      if converted.match(%r&#x([0-9a-f]{4});/)
        self.unicodecr_to_external(converted, conversion_table, to_sjis)
      else
        # 変換先が文字列で指定されている。
        to_sjis ? Jpmobile::Util.utf8_to_sjis(converted) : converted
      end
    when nil
      # 変換先が定義されていない。
      match
    end
  end
end
unicodecr_to_softbank_email(str) click to toggle source

str のなかでUnicode数値文字参照で表記された絵文字をメール送信用JISコードに変換する softbank 専用

# File lib/jpmobile/emoticon.rb, line 184
def self.unicodecr_to_softbank_email(str)
  str.gsub(%r&#x([0-9a-f]{4});/) do |match|
    unicode = $1.scanf("%x").first
    converted = CONVERSION_TABLE_TO_SOFTBANK[unicode]

    # メール用エンコーディングに変換する
    case converted
    when Integer
      if sjis = SOFTBANK_UNICODE_TO_SJIS[converted-0x1000]
        Jpmobile::Util.sjis([sjis].pack('n'))
      else
        match
      end
    when String
      # FIXME: 絵文字の代替が文章でいいかどうかの検証
      Jpmobile::Util.utf8_to_sjis(converted)
    else
      match
    end
  end
end
unicodecr_to_utf8(str) click to toggle source

str のなかでUnicode数値文字参照で表記された絵文字をUTF-8に置換する。

# File lib/jpmobile/emoticon.rb, line 133
def self.unicodecr_to_utf8(str)
  str.gsub(%r&#x([0-9a-f]{4});/) do |match|
    unicode = $1.scanf("%x").first
    if UNICODE_TO_SJIS[unicode] || SOFTBANK_UNICODE_TO_WEBCODE[unicode-0x1000]
      [unicode].pack('U')
    else
      match
    end
  end
end
utf8_to_unicodecr(str) click to toggle source

str のなかでUTF-8で表記された絵文字をUnicode数値文字参照に置換する。

# File lib/jpmobile/emoticon.rb, line 144
def self.utf8_to_unicodecr(str)
  str.gsub(UTF8_REGEXP) do |match|
    "&#x%04x;" % match.unpack('U').first
  end
end