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

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

Constant Summary collapse

DIR_UP =

Direction constants for edge representation Edges stored as [start_x, start_y, direction] arrays instead of Struct

0
DIR_DOWN =
1
DIR_LEFT =
2
DIR_RIGHT =
3
DIR_DELTAS =

Pre-computed end coordinate deltas: [dx, dy] for each direction

[
  [0, -1], # UP
  [0, 1],  # DOWN
  [-1, 0], # LEFT
  [1, 0]   # RIGHT
].freeze
DIR_PATH_COMMANDS =

SVG path commands indexed by direction constant

["v-", "v", "h-", "h"].freeze

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, options = {}) ⇒ Object



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rqrcode/export/svg.rb', line 36

def build(module_size, options = {})
  color = options[:color]
  offset_x = options[:offset_x].to_i
  offset_y = options[:offset_y].to_i

  modules_array = @qrcode.modules
  module_count = modules_array.length
  matrix_size = module_count + 1

  # Edge matrix stores arrays of [x, y, direction] tuples
  edge_matrix = Array.new(matrix_size) { Array.new(matrix_size) }
  edge_count = 0

  # Process horizontal edges (between vertically adjacent cells)
  (module_count + 1).times do |row_index|
    module_count.times do |col_index|
      above = row_index > 0 && modules_array[row_index - 1][col_index]
      below = row_index < module_count && modules_array[row_index][col_index]

      if above && !below
        # Edge going left at (col+1, row)
        x = col_index + 1
        y = row_index
        (edge_matrix[y][x] ||= []) << [x, y, DIR_LEFT]
        edge_count += 1
      elsif !above && below
        # Edge going right at (col, row)
        x = col_index
        y = row_index
        (edge_matrix[y][x] ||= []) << [x, y, DIR_RIGHT]
        edge_count += 1
      end
    end
  end

  # Process vertical edges (between horizontally adjacent cells)
  module_count.times do |row_index|
    (module_count + 1).times do |col_index|
      left = col_index > 0 && modules_array[row_index][col_index - 1]
      right = col_index < module_count && modules_array[row_index][col_index]

      if left && !right
        # Edge going down at (col, row)
        x = col_index
        y = row_index
        (edge_matrix[y][x] ||= []) << [x, y, DIR_DOWN]
        edge_count += 1
      elsif !left && right
        # Edge going up at (col, row+1)
        x = col_index
        y = row_index + 1
        (edge_matrix[y][x] ||= []) << [x, y, DIR_UP]
        edge_count += 1
      end
    end
  end

  path_parts = []

  # Track search position to avoid re-scanning from beginning
  search_y = 0
  search_x = 0

  while edge_count > 0
    # Find next non-empty cell, starting from last position
    start_edge = nil
    found_y = search_y
    found_x = search_x

    # Continue from where we left off
    (search_y...matrix_size).each do |y|
      start_col = (y == search_y) ? search_x : 0
      (start_col...matrix_size).each do |x|
        cell = edge_matrix[y][x]
        next unless cell && !cell.empty?

        start_edge = cell.first
        found_y = y
        found_x = x
        break
      end
      break if start_edge
    end

    # Update search position for next iteration
    search_y = found_y
    search_x = found_x

    # Build path string directly without intermediate edge_loop array
    path_str = String.new(capacity: 64)
    path_str << "M" << start_edge[0].to_s << " " << start_edge[1].to_s

    current_edge = start_edge
    current_dir = nil
    current_count = 0

    while current_edge
      ex, ey, edir = current_edge

      # Remove edge from matrix
      cell = edge_matrix[ey][ex]
      cell.delete(current_edge)
      edge_matrix[ey][ex] = nil if cell.empty?
      edge_count -= 1

      # Accumulate consecutive edges in same direction
      if edir == current_dir
        current_count += 1
      else
        # Flush previous direction
        path_str << DIR_PATH_COMMANDS[current_dir] << current_count.to_s if current_dir
        current_dir = edir
        current_count = 1
      end

      # Find next edge at end coordinates
      delta = DIR_DELTAS[edir]
      next_x = ex + delta[0]
      next_y = ey + delta[1]
      next_cell = edge_matrix[next_y]&.[](next_x)
      current_edge = next_cell&.first
    end

    # Don't output the last direction segment - close path instead
    path_str << "z"
    path_parts << path_str
  end

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