Class: Figlet::Typesetter

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

Instance Method Summary collapse

Constructor Details

#initialize(font, options = {}) ⇒ Typesetter

Returns a new instance of Typesetter.



3
4
5
6
7
# File 'lib/figlet/typesetter.rb', line 3

def initialize(font, options = {})
  @font = font

  @smush = options.fetch(:smush) { true }
end

Instance Method Details

#[](str) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/figlet/typesetter.rb', line 9

def [](str)
  result = []

  str.bytes.each_with_index do |byte, index|
    if @font.has_char?(byte)
      char = @font[byte]
    elsif @font.has_char?(0)
      char = @font[0]
    else
      next
    end

    @font.height.times do |j|
      result[j] = @font.right_to_left? ? "#{char[j]}#{result[j]}" : "#{result[j]}#{char[j]}"
    end

    if @font.old_layout > -1 && index > 0
      diff = -1

      @font.height.times do |j|
        if match = /\S(\s*\x00\s*)\S/.match(result[j])
          len = match[1].length

          diff = (diff == -1 ? len : min(diff, len))
        end
      end

      diff -= 1

      if diff > 0
        @font.height.times do |j|
          if match = /\x00(\s{0,#{diff}})/.match(result[j])
            b = diff - match[1].length

            result[j] = result[j].sub(/\s{0,#{b}}\x00\s{#{match[1].length}}/, "\0")
          end
        end
      end

      smush[result] if @smush
    end
  end

  result.join("\n").gsub(/\0/, '').gsub(@font.hard_blank, ' ')
end