Class: QrForge::Renderer
- Inherits:
-
Object
- Object
- QrForge::Renderer
- Defined in:
- lib/qr_forge/renderer.rb
Overview
Renderer class is responsible for generating the SVG representation of a QR code.
It takes QR data, design options, and renders the QR code with finder patterns, alignment patterns, and modules. It also handles the image if provided.
Constant Summary collapse
- DEFAULT_COLORS =
Rendering of how row and column indices relate to the finder pattern:
col_index (x) 0 1 2 3 4 5 6row_index (y)
0 # # # # # # # 1 # . . . . . # 2 # . # # # . # 3 # . # X # . # ← (3,3) is inside the finder 4 # . # # # . # 5 # . . . . . # 6 # # # # # # #Legend:
"#" = dark module (true) "." = light module (false) "X" = example point at (row_index=3, col_index=3) { module: "black", outer_eye: "black", inner_eye: "black" }.freeze
- DEFAULT_COMPONENTS =
{ outer_eye: ::QrForge::Components::EyeOuter::Circle, inner_eye: ::QrForge::Components::EyeInner::Circle, module: ::QrForge::Components::Module::Circle }.freeze
Instance Method Summary collapse
-
#canvas_size ⇒ Integer
Calculates the size of the canvas for the QR code.
-
#initialize(qr_data:, config:) ⇒ Renderer
constructor
A new instance of Renderer.
-
#to_svg ⇒ Object
Generates the SVG representation of the QR code.
Constructor Details
#initialize(qr_data:, config:) ⇒ Renderer
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/qr_forge/renderer.rb', line 43 def initialize(qr_data:, config:) @qr_data = qr_data @components = DEFAULT_COMPONENTS.merge(config.fetch(:components, {})) @quiet_zone = 4 @module_count = qr_data.module_count @image = config.dig(:design, :image) @size = config.dig(:output, :size) @colors = DEFAULT_COLORS.merge(config.dig(:design, :colors) || {}) @layout = QrForge::Layout.new(qr_data:, has_image: image_present?) end |
Instance Method Details
#canvas_size ⇒ Integer
Calculates the size of the canvas for the QR code. The canvas size is the module count plus the quiet zone on both sides.
The quiet zone is a margin around the QR code to ensure readability.
81 82 83 |
# File 'lib/qr_forge/renderer.rb', line 81 def canvas_size @canvas_size ||= @module_count + (@quiet_zone * 2) end |
#to_svg ⇒ Object
Generates the SVG representation of the QR code.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/qr_forge/renderer.rb', line 56 def to_svg Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml| xml.svg( width: @size, height: @size, xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 #{canvas_size} #{canvas_size}", shape_rendering: "crispEdges" ) do draw_background(xml) draw_image(xml) draw_finder_patterns(xml) draw_alignment_patterns(xml) draw_modules(xml) end end.to_xml end |