]> git.openstreetmap.org Git - rails.git/blob - app/helpers/svg_helper.rb
Merge remote-tracking branch 'upstream/pull/4764'
[rails.git] / app / helpers / svg_helper.rb
1 module SvgHelper
2   def notice_svg_tag
3     path_data = "M 2 0 C 0.892 0 0 0.892 0 2 L 0 14 C 0 15.108 0.892 16 2 16 L 14 16 C 15.108 16 16 15.108 16 14 L 16 2 C 16 0.892 15.108 0 14 0 L 2 0 z M 7 3 L 9 3 L 9 8 L 7 8 L 7 3 z M 7 10 L 9 10 L 9 12 L 7 12 L 7 10 z"
4     path_tag = tag.path :d => path_data, :fill => "currentColor"
5     tag.svg path_tag, :width => 16, :height => 16
6   end
7
8   def previous_page_svg_tag(**options)
9     adjacent_page_svg_tag(dir == "rtl" ? 1 : -1, **options)
10   end
11
12   def next_page_svg_tag(**options)
13     adjacent_page_svg_tag(dir == "rtl" ? -1 : 1, **options)
14   end
15
16   def key_svg_tag(**options)
17     border_width = options["border"] ? (options["border-width"] || 1) : 0
18     rect_attrs = {
19       :width => "100%",
20       :height => "100%",
21       :fill => options["fill"] || "none"
22     }
23     if border_width.positive?
24       rect_attrs[:x] = rect_attrs[:y] = format("%g", 0.5 * border_width)
25       rect_attrs[:width] = options["width"] - border_width
26       rect_attrs[:height] = options["height"] - border_width
27     end
28     svg_attrs = options.slice("width", "height", "opacity", :class)
29
30     tag.svg(**svg_attrs) do
31       horizontal = "H#{options['width']}"
32       concat tag.rect(**rect_attrs, **stroke_attrs(options, "border")) if options["fill"] || options["border"]
33       if options["line"]
34         y_middle = format("%g", 0.5 * options["height"])
35         concat tag.path(:d => "M0,#{y_middle} #{horizontal}", **stroke_attrs(options, "line"))
36       end
37       if options["casing"]
38         casing_width = options["casing-width"] || 1
39         y_top = format("%g", 0.5 * casing_width)
40         y_bottom = format("%g", options["height"] - (0.5 * casing_width))
41         concat tag.path(:d => "M0,#{y_top} #{horizontal} M0,#{y_bottom} #{horizontal}", **stroke_attrs(options, "casing"))
42       end
43     end
44   end
45
46   private
47
48   # returns "<" shape if side == -1; ">" if side == 1
49   def adjacent_page_svg_tag(side, **options)
50     count = options[:count] || 1
51     height = options[:height] || 15
52     pad = 2
53     segment = (0.5 * height) - pad
54     width = (segment + (2 * count * pad)).ceil
55     angled_line_data = "l#{side * segment},#{segment} l#{-side * segment},#{segment}"
56     path_data = Array.new(count) { |i| "M#{side * ((2 * i) + 1) * pad},#{pad} #{angled_line_data}" }.join(" ")
57     path_tag = tag.path :d => path_data, :fill => "none", :stroke => "currentColor", :"stroke-width" => 1.5
58     view_box = "#{-width} 0 #{width} #{height}" if side.negative?
59     tag.svg path_tag, :width => width, :height => height, :viewBox => view_box, :class => options[:class]
60   end
61
62   def stroke_attrs(attrs, prefix)
63     attrs.select { |key| key.start_with?(prefix) }.transform_keys { |key| key.delete_prefix(prefix).prepend("stroke") }
64   end
65 end