Class: Fontlike::Transmuter

Inherits:
Object
  • Object
show all
Defined in:
lib/fontlike/transmuter.rb

Constant Summary collapse

CHAR_BASE =
{
  fancy: {upper: [0x1D4D0], lower: [0x1D4EA]},
  shout: {upper: ['A'.ord], lower: ['A'.ord]},
  mono: {upper: [120432], lower: [120458]},
  sans_serif: {upper: [120224], lower: [120250]},
  circle: {upper: [0xFEFF, 0x24B6], lower: [0xFEFF, 0x24D0]},
  double_struck: {upper: [120120], lower: [120146]},
}
DEFAULT_BASE =
CHAR_BASE.fetch(:double_struck)
ALL_CHAR_BASE_UPPER =
CHAR_BASE.values.map {|v| v.fetch(:upper)}
ALL_CHAR_BASE_LOWER =
CHAR_BASE.values.map {|v| v.fetch(:lower)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Transmuter



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fontlike/transmuter.rb', line 19

def initialize(options)
  if options[:style] == :random
    self.upper_base = lambda { ALL_CHAR_BASE_UPPER.sample }
    self.lower_base = lambda { ALL_CHAR_BASE_LOWER.sample }
  else
    up = CHAR_BASE.fetch(options[:style], DEFAULT_BASE).fetch(:upper)
    lw = CHAR_BASE.fetch(options[:style], DEFAULT_BASE).fetch(:lower)

    self.upper_base = lambda { up }
    self.lower_base = lambda { lw }
  end
end

Instance Attribute Details

#lower_baseObject

Returns the value of attribute lower_base.



3
4
5
# File 'lib/fontlike/transmuter.rb', line 3

def lower_base
  @lower_base
end

#upper_baseObject

Returns the value of attribute upper_base.



3
4
5
# File 'lib/fontlike/transmuter.rb', line 3

def upper_base
  @upper_base
end

Instance Method Details

#transmute(str) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fontlike/transmuter.rb', line 32

def transmute(str)
  str.split("").map do |char|
    if char.match(/[A-Z]/)
      *prefix, suffix = upper_base.call
      last = suffix + char.ord - 'A'.ord
      [*prefix, last].pack('U*')
    elsif char.match(/[a-z]/)
      *prefix, suffix = lower_base.call
      last = suffix + char.ord - 'a'.ord
      [*prefix, last].pack('U*')
    else
      char
    end
  end.join("")
end