Module: Unicode::Name

Defined in:
lib/unicode/name.rb,
lib/unicode/name/index.rb,
lib/unicode/name/constants.rb

Constant Summary collapse

HANGUL_START =
44032
HANGUL_END =
55203
HANGUL_MEDIAL_MAX =
588
HANGUL_FINAL_MAX =
28
VERSION =
"1.9.0"
UNICODE_VERSION =
"13.0.0"
DATA_DIRECTORY =
File.expand_path(File.dirname(__FILE__) + "/../../../data/").freeze
INDEX_FILENAME =
(DATA_DIRECTORY + "/name.marshal.gz").freeze

Class Method Summary collapse

Class Method Details

.aliases(char) ⇒ Object



38
39
40
41
42
# File 'lib/unicode/name.rb', line 38

def self.aliases(char)
  codepoint = char.unpack("U")[0]
  require_relative "name/index" unless defined? ::Unicode::Name::INDEX
  INDEX[:ALIASES][codepoint]
end

.correct(char) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/unicode/name.rb', line 26

def self.correct(char)
  codepoint = char.unpack("U")[0]
  require_relative "name/index" unless defined? ::Unicode::Name::INDEX
  if correction = INDEX[:ALIASES][codepoint] &&
                  INDEX[:ALIASES][codepoint][:correction] &&
                  INDEX[:ALIASES][codepoint][:correction][-1]
    correction
  else
    unicode_name(char)
  end
end

.label(char) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/unicode/name.rb', line 44

def self.label(char)
  codepoint = char.unpack("U")[0]
  codepoint_pretty = "%.4X" % codepoint
  require_relative "name/index" unless defined? ::Unicode::Name::INDEX
  require "unicode/types" unless defined? ::Unicode::Types
  case Unicode::Types.type(char)
  when "Graphic", "Format"
    nil
  when "Control"
    "<control-#{codepoint_pretty}>"
  when "Private-use"
    "<private-use-#{codepoint_pretty}>"
  when "Surrogate"
    "<surrogate-#{codepoint_pretty}>"
  when "Noncharacter"
    "<noncharacter-#{codepoint_pretty}>"
  when "Reserved"
    "<reserved-#{codepoint_pretty}>"
  end
end

.readable(char) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/unicode/name.rb', line 65

def self.readable(char)
  unicode_name(char) ||
  ( as = aliases(char) ) &&
  ( as[:control]      && as[:control][0]      ||
    as[:figment]      && as[:figment][0]      ||
    as[:alternate]    && as[:alternate][0]    ||
    as[:abbreviation] && as[:abbreviation][0]  ) ||
  label(char)
end

.unicode_name(char) ⇒ Object Also known as: of

Don’t overwrite Module.name



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/unicode/name.rb', line 11

def self.unicode_name(char) 
  codepoint = char.unpack("U")[0]
  require_relative "name/index" unless defined? ::Unicode::Name::INDEX
  if res = INDEX[:NAMES][codepoint]
    res
  elsif INDEX[:CJK].any?{ |cjk_range| codepoint >= cjk_range[0] && codepoint <= cjk_range[1] }
    "CJK UNIFIED IDEOGRAPH-%.4X" % codepoint
  elsif codepoint >= HANGUL_START && codepoint <= HANGUL_END
    "HANGUL SYLLABLE %s" % hangul_decomposition(codepoint)
  else
    nil
  end
end