svg_profiler 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,9 +2,18 @@
2
2
 
3
3
  Profiles a Scalable Vector Graphics xml string.
4
4
 
5
- ## Usage
5
+ ## Setup
6
+
7
+ SVG Profiler depends on an open-source application called Inkscape.
6
8
 
7
9
  ```
10
+ brew install inkscape
11
+ gem install svg_profiler
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```ruby
8
17
  require "svg_profiler"
9
18
 
10
19
  profile = SVGProfiler.new(File.read("file.svg"))
data/lib/svg_profiler.rb CHANGED
@@ -1,3 +1,4 @@
1
- require "nokogiri"
1
+ require "tempfile"
2
2
 
3
3
  require "svg_profiler/base"
4
+ require "svg_profiler/inkscape"
@@ -1,40 +1,23 @@
1
1
  class SVGProfiler
2
2
 
3
- attr_reader :xml
4
-
5
3
  def initialize(xml_string)
6
- @xml = Nokogiri::XML(xml_string)
7
- end
8
-
9
- def dimensions
10
- svg = xml.css("svg")
11
-
12
- width, height = %w(width height).map { |s| attribute(svg, s) }
13
- view_box = attribute(svg, "viewBox")
14
-
15
- if view_box
16
- _, _, v_width, v_height = view_box.split(" ")
17
- end
18
-
19
- [width || v_width, height || v_height].map(&:to_i)
4
+ @inkscape = Inkscape.new(xml_string)
20
5
  end
21
6
 
22
7
  def width
23
- dimensions.first
8
+ @inkscape.width
24
9
  end
25
10
 
26
11
  def height
27
- dimensions.last
12
+ @inkscape.height
28
13
  end
29
14
 
30
- def aspect
31
- width.to_f / height
15
+ def dimensions
16
+ [width, height]
32
17
  end
33
18
 
34
- private
35
- def attribute(node, attribute)
36
- a = node.attr(attribute)
37
- a.value if a
19
+ def aspect
20
+ width.to_f / height
38
21
  end
39
22
 
40
23
  end
@@ -0,0 +1,35 @@
1
+ class SVGProfiler::Inkscape
2
+
3
+ def initialize(xml)
4
+ check_for_inkscape
5
+
6
+ input = Tempfile.new(["input", ".svg"])
7
+ input.write(xml.to_s)
8
+ input.close
9
+ @path = input.path
10
+ end
11
+
12
+ def width
13
+ exec("-W").to_i
14
+ end
15
+
16
+ def height
17
+ exec("-H").to_i
18
+ end
19
+
20
+ private
21
+ def check_for_inkscape
22
+ unless system("which inkscape &>/dev/null")
23
+ raise DependencyError.new(
24
+ "Could not locate Inkscape, try `brew install inkscape'"
25
+ )
26
+ end
27
+ end
28
+
29
+ def exec(command)
30
+ `inkscape -z #{command} #@path`
31
+ end
32
+
33
+ class DependencyError < StandardError; end
34
+
35
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg_profiler
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Patuzzo
@@ -18,24 +18,10 @@ cert_chain: []
18
18
  date: 2013-03-09 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: nokogiri
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
33
- type: :runtime
34
- version_requirements: *id001
35
21
  - !ruby/object:Gem::Dependency
36
22
  name: rspec
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ requirement: &id001 !ruby/object:Gem::Requirement
39
25
  none: false
40
26
  requirements:
41
27
  - - ">="
@@ -45,7 +31,7 @@ dependencies:
45
31
  - 0
46
32
  version: "0"
47
33
  type: :development
48
- version_requirements: *id002
34
+ version_requirements: *id001
49
35
  description: Profiles a Scalable Vector Graphics xml string.
50
36
  email: chris@patuzzo.co.uk
51
37
  executables: []
@@ -57,6 +43,7 @@ extra_rdoc_files: []
57
43
  files:
58
44
  - README.md
59
45
  - lib/svg_profiler/base.rb
46
+ - lib/svg_profiler/inkscape.rb
60
47
  - lib/svg_profiler.rb
61
48
  has_rdoc: true
62
49
  homepage: https://github.com/cpatuzzo/svg_profiler