Class: Esvg::Symbol

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/esvg/symbol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#attributes, #compress, #dasherize, #sort, #sub_path

Constructor Details

#initialize(path, parent) ⇒ Symbol

Returns a new instance of Symbol.



9
10
11
12
13
14
15
# File 'lib/esvg/symbol.rb', line 9

def initialize(path, parent)
  @parent  = parent
  @path    = path
  @last_checked = 0
  load_data
  read
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def content
  @content
end

#defsObject (readonly)

Returns the value of attribute defs.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def defs
  @defs
end

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def group
  @group
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def id
  @id
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def mtime
  @mtime
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def name
  @name
end

#optimizedObject (readonly)

Returns the value of attribute optimized.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def optimized
  @optimized
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/esvg/symbol.rb', line 5

def size
  @size
end

Instance Method Details

#attrObject



110
111
112
# File 'lib/esvg/symbol.rb', line 110

def attr
  { id: @id, 'data-name' => @name }.merge @size
end

#changed?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/esvg/symbol.rb', line 209

def changed?
  last_modified != mtime
end

#configObject



17
18
19
# File 'lib/esvg/symbol.rb', line 17

def config
  @parent.config
end

#dataObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/esvg/symbol.rb', line 95

def data
  {
    path: @path,
    name: @name,
    group: @group,
    mtime: @mtime,
    size: @size,
    content: @content,
    defs: @defs,
    optimized: @optimized,
    optimized_at: @optimized_at,
    svgo_optimized: optimize? && @svgo_optimized
  }
end

#dirObject



25
26
27
# File 'lib/esvg/symbol.rb', line 25

def dir
  @group
end

#exist?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/esvg/symbol.rb', line 21

def exist?
  File.exist?(@path)
end

#heightObject



56
57
58
# File 'lib/esvg/symbol.rb', line 56

def height
  @size[:height]
end

#optimizeObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/esvg/symbol.rb', line 179

def optimize
  read if changed?

  # Only optimize again if the file has changed
  if @optimized && @optimized_at && @optimized_at > @mtime
    return @optimized
  end

  # Only optimize if SVGO is installed
  if optimize?
    puts "Optimizing #{name}.svg" if config[:print]

    response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@content}' -o -})
    if !response[0].empty? && response[2].success?
      @optimized = response[0]
      @svgo_optimized = true
    end

    post_optimize
    @optimized_at = Time.now.to_i

    @optimized
  end

end

#optimize?Boolean

Only optimize if

  • Configuration asks for it

  • SVGO is present

  • If Rails is present

Returns:

  • (Boolean)


175
176
177
# File 'lib/esvg/symbol.rb', line 175

def optimize?
  config[:optimize] && !!Esvg.node_module('svgo') && config[:env] == 'production'
end

#readObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/esvg/symbol.rb', line 29

def read
  return if !exist?

  # Ensure that cache optimization matches current optimization settings
  # If config has changed name, reset optimized build (name gets baked in)
  if changed? || @svgo_optimized != optimize? || name != file_name
    @optimized = nil
    @optimized_at = nil
  end

  @group = dir_key
  @name  = file_name
  @id    = file_id file_key

  if changed?
    @content = prep_defs pre_optimize File.read(@path)
    @mtime   = last_modified
    @size    = dimensions
  end

  self
end

#scale(a) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/esvg/symbol.rb', line 79

def scale( a )
  # Width was set, determine scaled height
  if a[:width]
    a[:height] ||= scale_height( a[:width] )
  # Height was set, determine scaled width
  elsif a[:height]
    a[:width] ||= scale_width( a[:height] )
  # Nothing was set, default to dimensions
  else
    a[:width]  = width
    a[:height] = height
  end

  a
end

#scale_height(w) ⇒ Object

Scale height based on propotion to width



67
68
69
70
# File 'lib/esvg/symbol.rb', line 67

def scale_height( w )
  s = split_unit( w )
  "#{( s[:size] / width * height ).round(2)}#{s[:unit]}"
end

#scale_width(h) ⇒ Object

Scale width based on propotion to height



61
62
63
64
# File 'lib/esvg/symbol.rb', line 61

def scale_width( h )
  s = split_unit( h )
  "#{( s[:size] / height * width ).round(2)}#{s[:unit]}"
end

#split_unit(size) ⇒ Object

Separate size and unit for easier math. Returns: { size: 10, unit: ‘px’ }



74
75
76
77
# File 'lib/esvg/symbol.rb', line 74

def split_unit( size )
  m = size.to_s.match(/(\d+)\s*(\D*)/)
  { size: m[1].to_f, unit: m[2] }
end

#symbolObject



205
206
207
# File 'lib/esvg/symbol.rb', line 205

def symbol
  symbolize( optimize || @content )
end

#use(options = {}) ⇒ Object



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
146
147
148
149
150
151
152
153
# File 'lib/esvg/symbol.rb', line 114

def use(options={})
  
  # If preset key is set, merge presets from configuration
  if options[:preset] && preset = config[:presets][ options.delete(:preset).to_sym ]
    options = options.merge( preset )
  end

  # If size key is set, merge size class from configuration
  if options[:size] && size_class = config[:sizes][ options.delete(:size).to_sym ]
    options = options.merge( size_class )
  end

  options.delete(:fallback)
  content = options.delete(:content) || ''

  if desc   = options.delete(:desc)
    content = "<desc>#{desc}</desc>#{content}"
  end
  if title  = options.delete(:title)
    content = "<title>#{title}</title>#{content}"
  end

  use_attr = options.delete(:use) || {}

  svg_attr = {
    class: [config[:class], config[:prefix]+"-"+@name, options.delete(:class)].compact.join(' '),
    viewBox: @size[:viewBox],
    role: 'img'
  }.merge(options)

  if svg_attr[:scale]
    # User doesn't want dimensions to be set
    svg_attr.delete(:scale)
  else
    # Scale dimensions based on attributes
    svg_attr = scale( svg_attr )
  end

  %Q{<svg #{attributes(svg_attr)}>#{use_tag(use_attr)}#{content}</svg>}
end

#use_tag(options = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/esvg/symbol.rb', line 155

def use_tag(options={})
  options["xlink:href"] = "##{@id}"

  if options[:scale] && config[:scale]
    # User doesn't want dimensions to be set
    options.delete(:scale)
  else
    # Scale dimensions based on attributes
    options = scale( options )
  end

  options.delete(:scale)

  %Q{<use #{attributes(options)}></use>}
end

#widthObject



52
53
54
# File 'lib/esvg/symbol.rb', line 52

def width
  @size[:width]
end