]> git.openstreetmap.org Git - rails.git/blob - app/helpers/svg_helper.rb
21b7c91adacfdd8bb46829806f76770759952e89
[rails.git] / app / helpers / svg_helper.rb
1 module SvgHelper
2   def key_svg_tag(**options)
3     border_width = options["border"] ? (options["border-width"] || 1) : 0
4     rect_attrs = {
5       :width => "100%",
6       :height => "100%",
7       :fill => options["fill"] || "none"
8     }
9     if border_width.positive?
10       rect_attrs[:x] = rect_attrs[:y] = format("%g", 0.5 * border_width)
11       rect_attrs[:width] = options["width"] - border_width
12       rect_attrs[:height] = options["height"] - border_width
13     end
14     svg_attrs = options.slice("width", "height", "opacity", :class)
15
16     tag.svg(**svg_attrs) do
17       concat tag.rect(**rect_attrs, **stroke_attrs(options, "border")) if options["fill"] || options["border"]
18       concat tag.line(:x2 => "100%", :y1 => "50%", :y2 => "50%", **stroke_attrs(options, "line")) if options["line"]
19       if options["casing"]
20         casing_width = options["casing-width"] || 1
21         y_top = 0.5 * casing_width
22         y_bottom = options["height"] - (0.5 * casing_width)
23         concat tag.g(tag.line(:x2 => "100%", :y1 => y_top, :y2 => y_top) +
24                      tag.line(:x2 => "100%", :y1 => y_bottom, :y2 => y_bottom),
25                      **stroke_attrs(options, "casing"))
26       end
27     end
28   end
29
30   private
31
32   def stroke_attrs(attrs, prefix)
33     attrs.select { |key| key.start_with?(prefix) }.transform_keys { |key| key.delete_prefix(prefix).prepend("stroke") }
34   end
35 end