esvg 4.4.3 → 4.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7284570aded091cd4def0968ecd79cd23e416a1413f5125a4aba7054ec7942fd
4
- data.tar.gz: 2feba6f145d0a09480c315420a6ffb2d327ab57893ab166bd857e51a19d4caec
3
+ metadata.gz: '09c5c765988ce0a64353406948e42d7d97d4c5b7326ae4d8531987d98650f510'
4
+ data.tar.gz: 63248c8391f17ec3d197f2ef525e88d9e84f67415a9e85e0b2b7d3533764f3dc
5
5
  SHA512:
6
- metadata.gz: 952db8d5555a887a6f1b94f2703a2999d8eb74931e84dc97cf916f90350eec8f830a3a5d222a6d22a1c13a56023d35b49e8c841201f637a5ec34eeaf9c343ad1
7
- data.tar.gz: 7cf058895072daa328a45ff0e1ac81dd770ede834b2cfad97256da2da36fe08fa048aa66ee202f9e141f0b2651772cdf47d09b871569a9f65421dbf7568d74f3
6
+ metadata.gz: 7a480482a7aa333789dee900e968fae4930998f06da35af24f18238253302542caf542748707898140d0fac81c658f01330e5625751583c62f51d946594202c5
7
+ data.tar.gz: 53016c37c452c2adb00234baa5d98a9c6e059373136dba962b26864bac3da8512e0c9cf77f973857a002b2ae3c7bda6235866727d001610e3bd511d038632ced
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ### 4.5.0 (2018-12-11)
4
+ - New: width and height will be scaled proportionately. So passing only `width: 50` will set height proportionately based on original svg dimensions.
5
+ - To disable automatic calculation pass `scale: true`.
6
+ - Units are supported, so `height: 1em` will find the proper `em` based proportion for width.
7
+
3
8
  ### 4.4.3 (2018-12-09)
4
9
  - IE 10 compatibility fix: Replaced self-closing `<use>` tag with close tag version.
5
10
  - IE 10 compatibility fix: Using `forEach` instead of `for var of` because IE 10 is garbage. ¯\_(ツ)_/¯
@@ -45,6 +45,41 @@ module Esvg
45
45
  @size[:height]
46
46
  end
47
47
 
48
+ # Scale width based on propotion to height
49
+ def scale_width( h )
50
+ s = split_unit( h )
51
+ "#{( s[:size] / height * width ).round(2)}#{s[:unit]}"
52
+ end
53
+
54
+ # Scale height based on propotion to width
55
+ def scale_height( w )
56
+ s = split_unit( w )
57
+ "#{( s[:size] / width * height ).round(2)}#{s[:unit]}"
58
+ end
59
+
60
+ # Separate size and unit for easier math.
61
+ # Returns: { size: 10, unit: 'px' }
62
+ def split_unit( size )
63
+ m = size.to_s.match(/(\d+)\s*(\D*)/)
64
+ { size: m[1].to_f, unit: m[2] }
65
+ end
66
+
67
+ def scale( a )
68
+ # Width was set, determine scaled height
69
+ if a[:width]
70
+ a[:height] ||= scale_height( a[:width] )
71
+ # Height was set, determine scaled width
72
+ elsif a[:height]
73
+ a[:width] ||= scale_width( a[:height] )
74
+ # Nothing was set, default to dimensions
75
+ else
76
+ a[:width] = width
77
+ a[:height] = height
78
+ end
79
+
80
+ a
81
+ end
82
+
48
83
  def data
49
84
  {
50
85
  path: @path,
@@ -84,24 +119,26 @@ module Esvg
84
119
  role: 'img'
85
120
  }.merge(options)
86
121
 
87
- # If user doesn't pass a size or set scale: true
88
- if svg_attr[:width].nil? && svg_attr[:height].nil? && !svg_attr[:scale]
89
- svg_attr[:width] = width
90
- svg_attr[:height] = height
122
+ if svg_attr[:scale]
123
+ # User doesn't want dimensions to be set
124
+ svg_attr.delete(:scale)
125
+ else
126
+ # Scale dimensions based on attributes
127
+ svg_attr = scale( svg_attr )
91
128
  end
92
129
 
93
- svg_attr.delete(:scale)
94
-
95
130
  %Q{<svg #{attributes(svg_attr)}>#{use_tag(use_attr)}#{content}</svg>}
96
131
  end
97
132
 
98
133
  def use_tag(options={})
99
134
  options["xlink:href"] = "##{@id}"
100
135
 
101
- # If user doesn't pass a size or set scale: true
102
- if options[:width].nil? && options[:height].nil? && !options[:scale] && !@config[:scale]
103
- options[:width] ||= width
104
- options[:height] ||= height
136
+ if options[:scale] && @config[:scale]
137
+ # User doesn't want dimensions to be set
138
+ options.delete(:scale)
139
+ else
140
+ # Scale dimensions based on attributes
141
+ options = scale( options )
105
142
  end
106
143
 
107
144
  options.delete(:scale)
@@ -121,7 +158,7 @@ module Esvg
121
158
 
122
159
  @optimized = @content
123
160
 
124
- if svgo?
161
+ if svgo?
125
162
  response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@optimized}' -o -})
126
163
  if !response[0].empty? && response[2].success?
127
164
  @optimized = response[0]
@@ -255,7 +292,7 @@ module Esvg
255
292
 
256
293
  # <defs> should be moved to the beginning of the SVG file for braod browser support. Ahem, Firefox ಠ_ಠ
257
294
  # When symbols are reassembled, @defs will be added back
258
-
295
+
259
296
  if @defs = svg.scan(/<defs>(.+)<\/defs>/m).flatten[0]
260
297
  svg.sub!("<defs>#{@defs}</defs>", '')
261
298
  @defs.gsub!(/(\n|\s{2,})/,'')
@@ -1,3 +1,3 @@
1
1
  module Esvg
2
- VERSION = "4.4.3"
2
+ VERSION = "4.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esvg
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.3
4
+ version: 4.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-09 00:00:00.000000000 Z
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler