Class: QRCode::Output::SVG
- Inherits:
-
Object
- Object
- QRCode::Output::SVG
- Defined in:
- lib/qrcode/output/svg.rb
Overview
SVG renderer for scalable vector graphics
Instance Attribute Summary collapse
-
#border ⇒ Object
readonly
Returns the value of attribute border.
-
#cell_size ⇒ Object
readonly
Returns the value of attribute cell_size.
-
#dark_color ⇒ Object
readonly
Returns the value of attribute dark_color.
-
#light_color ⇒ Object
readonly
Returns the value of attribute light_color.
-
#qrcode ⇒ Object
readonly
Returns the value of attribute qrcode.
Instance Method Summary collapse
-
#initialize(qrcode, cell_size: 10, border: 2, dark_color: "#000000", light_color: "#ffffff") ⇒ SVG
constructor
A new instance of SVG.
- #render ⇒ Object
- #transparent? ⇒ Boolean
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
#border ⇒ Object (readonly)
Returns the value of attribute border.
10 11 12 |
# File 'lib/qrcode/output/svg.rb', line 10 def border @border end |
#cell_size ⇒ Object (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_color ⇒ Object (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_color ⇒ Object (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 |
#qrcode ⇒ Object (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
#render ⇒ Object
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
21 22 23 |
# File 'lib/qrcode/output/svg.rb', line 21 def transparent? @light_color.nil? || @light_color == "transparent" end |