Class: QRCode::Output::SVG

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

Overview

SVG renderer for scalable vector graphics

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qrcode, cell_size: 10, border: 2, dark_color: "#000000", light_color: "#ffffff") ⇒ SVG

Returns a new instance of SVG.



12
13
14
15
16
17
18
# File 'lib/qrcode/output/svg.rb', line 12

def initialize(qrcode, cell_size: 10, border: 2, dark_color: "#000000", light_color: "#ffffff")
  @qrcode = qrcode
  @cell_size = cell_size
  @border = border
  @dark_color = dark_color
  @light_color = light_color
end

Instance Attribute Details

#borderObject (readonly)

Returns the value of attribute border.



10
11
12
# File 'lib/qrcode/output/svg.rb', line 10

def border
  @border
end

#cell_sizeObject (readonly)

Returns the value of attribute cell_size.



10
11
12
# File 'lib/qrcode/output/svg.rb', line 10

def cell_size
  @cell_size
end

#dark_colorObject (readonly)

Returns the value of attribute dark_color.



10
11
12
# File 'lib/qrcode/output/svg.rb', line 10

def dark_color
  @dark_color
end

#light_colorObject (readonly)

Returns the value of attribute light_color.



10
11
12
# File 'lib/qrcode/output/svg.rb', line 10

def light_color
  @light_color
end

#qrcodeObject (readonly)

Returns the value of attribute qrcode.



10
11
12
# File 'lib/qrcode/output/svg.rb', line 10

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
# File 'lib/qrcode/output/svg.rb', line 25

def render
  total_size = (qrcode.size + (border * 2)) * cell_size
  
  svg = []
  svg << %[<?xml version="1.0" encoding="UTF-8"?>]
  svg << %[<svg xmlns="http://www.w3.org/2000/svg" width="#{total_size}" height="#{total_size}" viewBox="0 0 #{total_size} #{total_size}">]
  
  # Background rectangle (only if not transparent)
  unless transparent?
    svg << %[  <rect x="0" y="0" width="#{total_size}" height="#{total_size}" fill="#{light_color}"/>]
  end
  
  # Generate dark modules as rectangles
  qrcode.size.times do |row|
    qrcode.size.times do |col|
      if qrcode.checked?(row, col)
        x = (col + border) * cell_size
        y = (row + border) * cell_size
        svg << %[  <rect x="#{x}" y="#{y}" width="#{cell_size}" height="#{cell_size}" fill="#{dark_color}"/>]
      end
    end
  end
  
  svg << %[</svg>]
  svg.join("\n")
end

#transparent?Boolean

Returns:

  • (Boolean)


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

def transparent?
  @light_color.nil? || @light_color == "transparent"
end