Class: RQRCode::Export::SVG::Path

Inherits:
BaseOutputSVG show all
Defined in:
lib/rqrcode/export/svg.rb

Instance Attribute Summary

Attributes inherited from BaseOutputSVG

#result

Instance Method Summary collapse

Methods inherited from BaseOutputSVG

#initialize

Constructor Details

This class inherits a constructor from RQRCode::Export::SVG::BaseOutputSVG

Instance Method Details

#build(module_size, offset, color) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rqrcode/export/svg.rb', line 18

def build(module_size, offset, color)
  modules_array = @qrcode.modules
  matrix_width = matrix_height = modules_array.length + 1
  empty_row = [Array.new(matrix_width - 1, false)]
  edge_matrix = Array.new(matrix_height) { Array.new(matrix_width) }

  (empty_row + modules_array + empty_row).each_cons(2).with_index do |row_pair, row_index|
    first_row, second_row = row_pair

    # horizontal edges
    first_row.zip(second_row).each_with_index do |cell_pair, column_index|
      edge = case cell_pair
      when [true, false] then Edge.new column_index + 1, row_index, :left
      when [false, true] then Edge.new column_index, row_index, :right
      end

      (edge_matrix[edge.start_y][edge.start_x] ||= []) << edge if edge
    end

    # vertical edges
    ([false] + second_row + [false]).each_cons(2).each_with_index do |cell_pair, column_index|
      edge = case cell_pair
      when [true, false] then Edge.new column_index, row_index, :down
      when [false, true] then Edge.new column_index, row_index + 1, :up
      end

      (edge_matrix[edge.start_y][edge.start_x] ||= []) << edge if edge
    end
  end

  edge_count = edge_matrix.flatten.compact.count
  path = []

  while edge_count > 0
    edge_loop = []
    next_matrix_cell = edge_matrix.find(&:any?).find { |cell| cell&.any? }
    edge = next_matrix_cell.first

    while edge
      edge_loop << edge
      matrix_cell = edge_matrix[edge.start_y][edge.start_x]
      matrix_cell.delete edge
      edge_matrix[edge.start_y][edge.start_x] = nil if matrix_cell.empty?
      edge_count -= 1

      # try to find an edge continuing the current edge
      edge = edge_matrix[edge.end_y][edge.end_x]&.first
    end

    first_edge = edge_loop.first
    edge_loop_string = SVG_PATH_COMMANDS[:move]
    edge_loop_string += "#{first_edge.start_x} #{first_edge.start_y}"

    edge_loop.chunk(&:direction).to_a[0...-1].each do |direction, edges|
      edge_loop_string << "#{SVG_PATH_COMMANDS[direction]}#{edges.length}"
    end
    edge_loop_string << SVG_PATH_COMMANDS[:close]

    path << edge_loop_string
  end

  # Prefix hexadecimal colors unless using a named color (symbol)
  color = "##{color}" unless color.is_a?(Symbol)

  @result << %{<path d="#{path.join}" fill="#{color}" transform="translate(#{offset},#{offset}) scale(#{module_size})"/>}
end