Class: Artii::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.



6
7
8
# File 'lib/figlet/smusher.rb', line 6

def initialize(font)
  @font = font
end

Instance Method Details

#[](result) ⇒ Object



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

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



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

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)


37
38
39
# File 'lib/figlet/smusher.rb', line 37

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

#patternObject



25
26
27
# File 'lib/figlet/smusher.rb', line 25

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

#symbolsObject



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

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