Class: Svgr::Arrange::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/svgr/arrange/grid.rb

Class Method Summary collapse

Class Method Details

.create_combined_svg(elements, rows, columns, scaling_factor, margin: {}) ⇒ Object



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
# File 'lib/svgr/arrange/grid.rb', line 100

def create_combined_svg(
  elements,
  rows,
  columns,
  scaling_factor,
  margin: {}
)
  margin_top = margin.fetch(:top, 0)
  margin_left = margin.fetch(:left, 0)

  width =
    columns * 100 * scaling_factor +
    (columns - 1) * margin_left * scaling_factor
  height =
    rows * 100 * scaling_factor + (rows - 1) * margin_top * scaling_factor

  combined_svg =
    Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
      xml.svg(
        xmlns: "http://www.w3.org/2000/svg",
        width: width,
        height: height,
      ) do
        elements.each_with_index do |element, index|
          row = index / columns
          col = index % columns

          # Adjust the 'transform' attribute to position and scale the element in the grid
          x =
            col * (100 * scaling_factor + margin_left * scaling_factor) +
            (width - 100 * scaling_factor) / 2
          y = row * (100 * scaling_factor + margin_top * scaling_factor)

          # Offset by half the size of the element vertically and horizontally
          x += 50 * scaling_factor
          y += 50 * scaling_factor

          transform = "translate(#{x}, #{y}) scale(#{scaling_factor})"
          element["transform"] = transform
          xml.parent << element
        end
      end
    end

  combined_svg.to_xml
end

.extract_svg_elements(svg_content) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/svgr/arrange/grid.rb', line 81

def extract_svg_elements(svg_content)
  doc = Nokogiri.XML(svg_content)
  doc.remove_namespaces!
  top_level_groups = doc.xpath("//svg/g")

  if top_level_groups.empty?
    # Wrap all child elements of the SVG document in a group
    svg_element = doc.at_xpath("//svg")
    new_group = Nokogiri::XML::Node.new("g", doc)

    svg_element.children.each { |child| new_group.add_child(child) }

    svg_element.add_child(new_group)
    top_level_groups = [new_group]
  end

  top_level_groups
end

.read_svg_file(file) ⇒ Object



77
78
79
# File 'lib/svgr/arrange/grid.rb', line 77

def read_svg_file(file)
  File.read(file)
end

.start(argv) ⇒ Object



10
11
12
13
14
15
16
17
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
# File 'lib/svgr/arrange/grid.rb', line 10

def start(argv)
  options = {
    scaling_factor: 1,
    margin_top: 0,
    margin_left: 0,
  }

  opt_parser =
    OptionParser.new do |opts|
      opts.banner =
        "Usage: svgr arrange:grid [options] <file_paths> <rows> <columns>"

      opts.on(
        "-s",
        "--scaling-factor FACTOR",
        Float,
        "Scaling factor for the SVG elements",
      ) { |s| options[:scaling_factor] = s }

      opts.on(
        "-t",
        "--margin-top MARGIN",
        Integer,
        "Top margin between the SVG elements",
      ) { |t| options[:margin_top] = t }

      opts.on(
        "-l",
        "--margin-left MARGIN",
        Integer,
        "Left margin between the SVG elements",
      ) { |l| options[:margin_left] = l }

      opts.on("-h", "--help", "Prints this help") do
        puts opts
        exit
      end
    end
  opt_parser.parse!(argv)

  if argv.length < 3
    puts opt_parser.help
    exit(1)
  end

  file_paths, rows, columns = argv.shift(3)
  rows = rows.to_i
  columns = columns.to_i
  svg_files = file_paths.split(",")[0...rows * columns]

  combined_elements =
    svg_files.flat_map do |file|
      content = read_svg_file(file)
      extract_svg_elements(content)
    end

  combined_svg = create_combined_svg(
    combined_elements,
    rows,
    columns,
    options[:scaling_factor],
    margin: { top: options[:margin_top], left: options[:margin_left] },
  )

  write_svg(combined_svg)
end

.write_svg(svg) ⇒ Object



147
148
149
# File 'lib/svgr/arrange/grid.rb', line 147

def write_svg(svg)
  puts svg
end