Class: QRCode::Output::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/qrcode/output/text.rb

Overview

Text renderer using Unicode half-height block characters

Constant Summary collapse

BLOCKS =

Unicode block characters for different combinations

{
  [false, false] => " ",   # Both light - single space
  [false, true]  => "▄",   # Top light, bottom dark
  [true, false]  => "▀",   # Top dark, bottom light  
  [true, true]   => "█"    # Both dark - full block
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qrcode, border: 2) ⇒ Text

Returns a new instance of Text.



20
21
22
23
# File 'lib/qrcode/output/text.rb', line 20

def initialize(qrcode, border: 2)
  @qrcode = qrcode
  @border = border
end

Instance Attribute Details

#borderObject (readonly)

Returns the value of attribute border.



18
19
20
# File 'lib/qrcode/output/text.rb', line 18

def border
  @border
end

#qrcodeObject (readonly)

Returns the value of attribute qrcode.



18
19
20
# File 'lib/qrcode/output/text.rb', line 18

def qrcode
  @qrcode
end

Instance Method Details

#renderObject



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/qrcode/output/text.rb', line 25

def render
  lines = []
  
  # Add top border
  border.times do
    lines << " " * total_width
  end
  
  # Process QR code in pairs of rows to use half-height blocks
  (0...qrcode.size).step(2) do |row|
    line = " " * border  # Left border
    
    qrcode.size.times do |col|
      top = qrcode.checked?(row, col)
      bottom = (row + 1 < qrcode.size) ? qrcode.checked?(row + 1, col) : false
      line += BLOCKS[[top, bottom]]
    end
    
    line += " " * border  # Right border
    lines << line
  end
  
  # Add bottom border
  border.times do
    lines << " " * total_width
  end
  
  lines.join("\n")
end