From 04f8006be70461c353cefa0b95c2ba2456200ce6 Mon Sep 17 00:00:00 2001 From: Duncan Robertson Date: Thu, 25 Mar 2021 16:18:06 +0000 Subject: [PATCH] Use lookup for path commands and prefer << when amending string --- lib/rqrcode/export/svg.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/rqrcode/export/svg.rb b/lib/rqrcode/export/svg.rb index 4744050..6024d7c 100644 --- a/lib/rqrcode/export/svg.rb +++ b/lib/rqrcode/export/svg.rb @@ -23,11 +23,13 @@ def end_y end end - DIRECTIONS = { + SVG_PATH_COMMANDS = { + move: "M", up: "v-", down: "v", left: "h-", - right: "h" + right: "h", + close: "z" } # @@ -139,30 +141,33 @@ def use_path(str, module_size, offset, color) end edge_count = edge_matrix.flatten.compact.count - path = [] + while edge_count > 0 edge_loop = [] matrix_cell = edge_matrix.find { |row| row.any? }.find { |cell| !cell.nil? && !cell.empty? } edge = 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 matrix_cell = edge_matrix[edge.end_y][edge.end_x] edge = matrix_cell.nil? ? nil : matrix_cell.first end first_edge = edge_loop.first - edge_loop_string = "M#{first_edge.start_x} #{first_edge.start_y}" + 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 += "#{DIRECTIONS[direction]}#{edges.length}" + edge_loop_string << "#{SVG_PATH_COMMANDS[direction]}#{edges.length}" end - edge_loop_string += "z" + edge_loop_string << SVG_PATH_COMMANDS[:close] path << edge_loop_string end