Class: Figlet::Smusher

Inherits:
Object
  • Object
show all
Defined in:
lib/figlet/smusher.rb

Instance Method Summary collapse

Constructor Details

#initialize(font) ⇒ Smusher

Returns a new instance of Smusher.



3
4
5
# File 'lib/figlet/smusher.rb', line 3

def initialize(font)
  @font = font
end

Instance Method Details

#[](result) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/figlet/smusher.rb', line 7

def [](result)
  todo = false

  @font.height.times do |j|
    result[j] = result[j].sub(pattern) { todo, x = callback(todo, $1, $2); x }
  end

  @font.height.times do |j|
    result[j] = if todo
      result[j].sub(/\s\x00(?!$)|\x00\s/, '').sub(/\x00(?!$)/, '')
    else
      result[j].sub(/\x00(?!$)/, '')
    end
  end
end

#callback(s, a, b) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/figlet/smusher.rb', line 39

def callback(s, a, b)
  combined = a + b

  if old_layout?(1) && a == b
    return true, a
  elsif old_layout?(2) && ('_' == a && symbols[24].include?(b) || '_' == b && symbols[24].include?(a))
    return true, a
  elsif old_layout?(4) && ((left = symbols[24].index(a)) && (right = symbols[24].index(b)))
    return true, (right > left ? b : a)
  elsif old_layout?(8) && (symbols[8].has_key?(b) && symbols[8][b] == a)
    return true, '|'
  elsif old_layout?(16) && symbols[16].has_key?(combined)
    return true, symbols[16][combined]
  elsif old_layout?(32) && (a == b && @font.hard_blank == a)
    return true, @font.hard_blank
  else
    return s, "#{a}\00#{b}"
  end
end

#old_layout?(n) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/figlet/smusher.rb', line 35

def old_layout?(n)
  @font.old_layout & n > 0
end

#patternObject



23
24
25
# File 'lib/figlet/smusher.rb', line 23

def pattern
  @pattern ||= /([^#{@font.hard_blank}\x00\s])\x00([^#{@font.hard_blank}\x00\s])/
end

#symbolsObject



27
28
29
30
31
32
33
# File 'lib/figlet/smusher.rb', line 27

def symbols
  @@symbols ||= {
    24 => '|/\\[]{}()<>',
    8 => {'[' => ']', ']' => '[', '{' => '}', '}' => '{', '(' => ')', ')' => '('},
    16 => {"/\\" => '|', "\\/" => 'Y', '><' => 'X'}
  }
end