forticons 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ module Forticons
2
+ class Forticon
3
+ DEFAULT_HEIGHT = 16
4
+
5
+ attr_reader :path, :options, :width, :height, :symbol, :keywords
6
+
7
+ def initialize(symbol, options = {})
8
+ @symbol = symbol.to_s
9
+ if forticon = get_forticon(@symbol, options)
10
+ @path = forticon["path"]
11
+ @width = forticon["width"]
12
+ @height = forticon["height"]
13
+ @keywords = forticon["keywords"]
14
+ @options = options.dup
15
+ @options.merge!({
16
+ class: classes,
17
+ viewBox: viewbox,
18
+ version: "1.1"
19
+ })
20
+ @options.merge!(size)
21
+ @options.merge!(a11y)
22
+ else
23
+ raise "Couldn't find forticon symbol for #{@symbol.inspect}"
24
+ end
25
+ end
26
+
27
+ # Returns an string representing a <svg> tag
28
+ def to_svg
29
+ "<svg #{html_attributes}>#{@path}</svg>"
30
+ end
31
+
32
+ private
33
+
34
+ def html_attributes
35
+ attrs = ""
36
+ @options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
37
+ attrs.strip
38
+ end
39
+
40
+ # add some accessibility features to svg
41
+ def a11y
42
+ accessible = {}
43
+
44
+ if @options[:"aria-label"].nil? && @options["aria-label"].nil?
45
+ accessible[:"aria-hidden"] = "true"
46
+ else
47
+ accessible[:role] = "img"
48
+ end
49
+
50
+ accessible
51
+ end
52
+
53
+ # prepare the forticon class
54
+ def classes
55
+ "forticon forticon-#{@symbol} #{@options[:class]} ".strip
56
+ end
57
+
58
+ def viewbox
59
+ "0 0 #{@width} #{@height}"
60
+ end
61
+
62
+ # determine the height and width of the forticon based on :size option
63
+ def size
64
+ size = {
65
+ width: @width,
66
+ height: @height
67
+ }
68
+
69
+ # Specific size
70
+ unless @options[:width].nil? && @options[:height].nil?
71
+ size[:width] = @options[:width].nil? ? calculate_width(@options[:height]) : @options[:width]
72
+ size[:height] = @options[:height].nil? ? calculate_height(@options[:width]) : @options[:height]
73
+ end
74
+
75
+ size
76
+ end
77
+
78
+ def calculate_width(height)
79
+ (height.to_i * @width) / @height
80
+ end
81
+
82
+ def calculate_height(width)
83
+ (width.to_i * @height) / @width
84
+ end
85
+
86
+ def get_forticon(symbol, options = {})
87
+ if forticon = Forticons::FORTICON_SYMBOLS[symbol]
88
+ # We're using width as an approximation for height if the height option is not passed in
89
+ height = options[:height] || options[:width] || DEFAULT_HEIGHT
90
+ natural_height = closest_natural_height(forticon["heights"].keys, height)
91
+ return {
92
+ "name" => forticon["name"],
93
+ "keywords" => forticon["keywords"],
94
+ "width" => forticon["heights"][natural_height.to_s]["width"].to_i,
95
+ "height" => natural_height,
96
+ "path" => forticon["heights"][natural_height.to_s]["path"]
97
+ }
98
+ end
99
+ end
100
+
101
+ def closest_natural_height(natural_heights, height)
102
+ return natural_heights.reduce(natural_heights[0].to_i) do |acc, natural_height|
103
+ natural_height.to_i <= height.to_i ? natural_height.to_i : acc
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module Forticons
2
+ VERSION = "0.0.2".freeze
3
+ end
data/lib/forticons.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "forticons/version"
2
+ require "forticons/forticon"
3
+ require "json"
4
+
5
+ module Forticons
6
+ file_data = File.read(File.join(File.dirname(__FILE__), "./build/data.json"))
7
+ FORTICON_SYMBOLS = JSON.parse(file_data).freeze
8
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forticons
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - anosim114
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A package that distributes FortAwesome icons in a gem
14
+ email:
15
+ - arnold_siemens@pm.me
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/build/data.json
23
+ - lib/forticons.rb
24
+ - lib/forticons/forticon.rb
25
+ - lib/forticons/version.rb
26
+ homepage: https://github.com/anosim114/forticons
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.4.10
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Octicons like FortAwesome icon library
49
+ test_files: []