Class: NRSER::UnicodeMath::CharacterTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/nrser/temp/unicode_math.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, upper_start, lower_start) ⇒ CharacterTranslator

Returns a new instance of CharacterTranslator.



16
17
18
19
20
# File 'lib/nrser/temp/unicode_math.rb', line 16

def initialize name, upper_start, lower_start
  @name = name
  @upper_start = upper_start
  @lower_start = lower_start
end

Instance Method Details

#translate(string) ⇒ Object Also known as: []



35
36
37
# File 'lib/nrser/temp/unicode_math.rb', line 35

def translate string
  string.each_char.map( &method( :translate_char ) ).join
end

#translate_char(char) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nrser/temp/unicode_math.rb', line 22

def translate_char char
  upper_offset = char.ord - 'A'.ord
  lower_offset = char.ord - 'a'.ord
  
  if upper_offset >= 0 && upper_offset < 26
    [ @upper_start.hex + upper_offset ].pack "U"
  elsif lower_offset >= 0 && lower_offset < 26
    [ @lower_start.hex + lower_offset ].pack "U"
  else
    char
  end
end