Module: Forkforge::Unicode

Extended by:
Unicode
Included in:
Unicode
Defined in:
lib/forkforge/unicode.rb

Instance Method Summary collapse

Instance Method Details

#camel_to_underscore(s, constant = false) ⇒ Object



24
25
26
27
28
29
# File 'lib/forkforge/unicode.rb', line 24

def camel_to_underscore s, constant = false
  result = s.gsub(/(?<!\A)./) { |m|
    Letter::is_uppercase(m) ? "_#{m}" : m
  }
  constant ? uppercase(result) : lowercase(result)
end

#compose(s, tag = :font, format = :full) ⇒ Object

As an opposite to decomposition: composes symbols using given string and tag (when possible). This function is not intended to be used directly. Normally one uses one of aliases:

  • circle
  • super
  • sub
  • wide

NB This is not a composition as it is understood by Unicode.Org (love them.)

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/forkforge/unicode.rb', line 51

def compose s, tag = :font, format = :full
  composed = s.codepoints.map { |cp|
    (result = Forkforge::UnicodeData::compose_cp(cp, tag, format == :full)).vacant? ? \
      [Forkforge::UnicodeData::to_codepoint(cp)] : result
  }

  raise UnicodeException, "AMBIGUITIES FOUND, FIXME FIXME FIXME" \
    if format == :lazy && (composed.length != s.length)

  case format
  when :full then Hash[s.split('').map.with_index { |ch, idx| [[ch,idx], composed[idx]] }]
  when :lazy then composed.join
  when :risk then composed.map(&:first).join
  else composed
  end
end

#decompose(s, tags = []) ⇒ Object

Decomposes symbols to their combined representation, e.g. ASCII c-cedilla to 2 symbols



81
82
83
84
85
# File 'lib/forkforge/unicode.rb', line 81

def decompose s, tags = []
  s.codepoints.map { |cp|
    Forkforge::UnicodeData::decompose_cp cp, tags
  }.flatten.map { |cp| cp.to_i(16) }.pack('U*')
end

#fraktur(s) ⇒ Object



74
75
76
77
78
# File 'lib/forkforge/unicode.rb', line 74

def fraktur s
  s.split('').map do |c|
    Forkforge::UnicodeData::code_points.send(:math_fraktur_bold, c).to_s
  end.join
end

#lookup(pattern) ⇒ Object



37
38
39
40
41
# File 'lib/forkforge/unicode.rb', line 37

def lookup pattern
  Forkforge::UnicodeData::all_character_name(pattern).map { |k, v|
    Forkforge::UnicodeData::to_char k
  }
end

#underscore_to_camel(s) ⇒ Object



31
32
33
34
35
# File 'lib/forkforge/unicode.rb', line 31

def underscore_to_camel s
  (lowercase s).gsub(/((?<=\A)|_)(\w)/) {
    titlecase $~[2]
  }
end