phantom_svg 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +42 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +11 -0
  5. data/.travis.yml +9 -0
  6. data/Gemfile +16 -0
  7. data/Guardfile +12 -0
  8. data/LICENSE +165 -0
  9. data/README.md +6 -0
  10. data/lib/phantom/frame.rb +71 -0
  11. data/lib/phantom/parser/abstract_animation_reader.rb +117 -0
  12. data/lib/phantom/parser/json_animation_reader.rb +42 -0
  13. data/lib/phantom/parser/raster.rb +115 -0
  14. data/lib/phantom/parser/svg_reader.rb +131 -0
  15. data/lib/phantom/parser/svg_writer.rb +163 -0
  16. data/lib/phantom/parser/xml_animation_reader.rb +32 -0
  17. data/lib/phantom/svg.rb +139 -0
  18. data/lib/phantom_svg.rb +1 -0
  19. data/phantom_svg.gemspec +25 -0
  20. data/spec/images/apngasm.png +0 -0
  21. data/spec/images/ninja.svg +63 -0
  22. data/spec/images/stuck_out_tongue/0.svg +103 -0
  23. data/spec/images/stuck_out_tongue/1.svg +103 -0
  24. data/spec/images/stuck_out_tongue/10.svg +103 -0
  25. data/spec/images/stuck_out_tongue/11.svg +103 -0
  26. data/spec/images/stuck_out_tongue/2.svg +103 -0
  27. data/spec/images/stuck_out_tongue/3.svg +103 -0
  28. data/spec/images/stuck_out_tongue/4.svg +103 -0
  29. data/spec/images/stuck_out_tongue/5.svg +103 -0
  30. data/spec/images/stuck_out_tongue/6.svg +103 -0
  31. data/spec/images/stuck_out_tongue/7.svg +103 -0
  32. data/spec/images/stuck_out_tongue/8.svg +103 -0
  33. data/spec/images/stuck_out_tongue/9.svg +103 -0
  34. data/spec/images/stuck_out_tongue/loops_test.json +9 -0
  35. data/spec/images/stuck_out_tongue/loops_test.xml +4 -0
  36. data/spec/images/stuck_out_tongue/skip_first_test.json +10 -0
  37. data/spec/images/stuck_out_tongue/skip_first_test.xml +5 -0
  38. data/spec/images/stuck_out_tongue/test1.json +20 -0
  39. data/spec/images/stuck_out_tongue/test1.xml +15 -0
  40. data/spec/images/stuck_out_tongue/test2.json +13 -0
  41. data/spec/images/stuck_out_tongue/test2.xml +4 -0
  42. data/spec/phantom/svg_spec.rb +421 -0
  43. data/spec/spec_helper.rb +81 -0
  44. metadata +170 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e574c48c746f370d9570bed8ad5e073003d4d50
4
+ data.tar.gz: fdeb8c6f09df7db61bfa701c5349cb66ca4423d5
5
+ SHA512:
6
+ metadata.gz: 1ec5761ab6442dc05a8fe603b9ed4cd8655d407574533560c91c2dad6af4a612f2c1bd1455f68120d91b2b895f87f3b94739227119ef008be547dff3675acc36
7
+ data.tar.gz: a4091515ef04be0db7ba0dd7b9e9007ceaa66f2d598bb87dad7f1ac764c312d1d7de541008d26b96fd8104b8688242dd24dc8ffee951c7e16e42cff8fcf52e2f
data/.gitignore ADDED
@@ -0,0 +1,42 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ Gemfile.lock
13
+
14
+ ## Specific to RubyMotion:
15
+ .dat*
16
+ .repl_history
17
+ build/
18
+
19
+ ## Documentation cache and generated files:
20
+ /.yardoc/
21
+ /_yardoc/
22
+ /doc/
23
+ /rdoc/
24
+
25
+ ## Environment normalisation:
26
+ /.bundle/
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
37
+
38
+ # Mac
39
+ .DS_Store
40
+
41
+ *.swp
42
+ *.swo
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format documentation
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AsciiIdentifiers:
2
+ Enabled: false
3
+
4
+ AsciiComments:
5
+ Enabled: false
6
+
7
+ AccessorMethodName:
8
+ Enabled: false
9
+
10
+ LineLength:
11
+ Max: 99
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+
5
+ before_install:
6
+ - sudo add-apt-repository -y ppa:zero-tsuki/ppa
7
+ - sudo apt-get update -qq
8
+
9
+ install: sudo apt-get install -qq libapngasm
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rspec'
7
+ end
8
+
9
+ group :development do
10
+ gem 'rb-inotify', require: false
11
+ gem 'rb-fsevent', require: false
12
+ gem 'guard'
13
+ gem 'guard-rspec'
14
+ gem 'rubocop'
15
+ gem 'guard-rubocop'
16
+ end
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ guard :rubocop do
2
+ watch(%r{.+\.rb$})
3
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
4
+ end
5
+
6
+ guard :rspec do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+
11
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ Phantom SVG
2
+ ===========
3
+ [![Build Status](https://travis-ci.org/Genshin/phantom_svg.svg)](https://travis-ci.org/Genshin/phantom_svg)
4
+ [![Code Climate](https://codeclimate.com/github/Genshin/phantom_svg.png)](https://codeclimate.com/github/Genshin/phantom_svg)
5
+
6
+ SVG manipulation and conversion tools written in Ruby by Phantom Creation Inc.
@@ -0,0 +1,71 @@
1
+
2
+ module Phantom
3
+ module SVG
4
+ # Frame class for "Key Frames" implementation in SVG
5
+ class Frame
6
+ attr_accessor :duration, :surfaces, :width, :height, :viewbox, :namespaces
7
+
8
+ def initialize(options = {})
9
+ set_duration(options[:duration])
10
+ set_surfaces(options[:surfaces])
11
+ set_width(options[:width])
12
+ set_height(options[:height])
13
+ set_viewbox(options[:viewbox])
14
+ set_namespaces(options[:namespaces])
15
+ end
16
+
17
+ # ViewBox helper.
18
+ class ViewBox
19
+ attr_accessor :x, :y, :width, :height
20
+
21
+ def initialize(x = 0, y = 0, width = 64, height = 64)
22
+ @x = x
23
+ @y = y
24
+ @width = width
25
+ @height = height
26
+ end
27
+
28
+ def set_from_text(text)
29
+ values = text.split(' ', 4)
30
+ initialize(values[0], values[1], values[2], values[3])
31
+ self
32
+ end
33
+
34
+ def to_s
35
+ "#{@x.to_i} #{@y.to_i} #{@width.to_i} #{@height.to_i}"
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def set_duration(val)
42
+ @duration = val.nil? ? 0.1 : val
43
+ end
44
+
45
+ def set_surfaces(val)
46
+ @surfaces = val.nil? ? nil : val
47
+ end
48
+
49
+ def set_width(val)
50
+ @width = val.nil? ? 64 : val
51
+ end
52
+
53
+ def set_height(val)
54
+ @height = val.nil? ? 64 : val
55
+ end
56
+
57
+ def set_viewbox(val)
58
+ @viewbox =
59
+ if val.nil? then ViewBox.new
60
+ elsif val.is_a?(ViewBox) then val
61
+ elsif val.is_a?(String) then ViewBox.new.set_from_text(val)
62
+ else ViewBox.new
63
+ end
64
+ end
65
+
66
+ def set_namespaces(val)
67
+ @namespaces = val.nil? ? {} : val
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,117 @@
1
+
2
+ require_relative 'svg_reader.rb'
3
+
4
+ module Phantom
5
+ module SVG
6
+ module Parser
7
+ # AnimationReader base.
8
+ class AbstractAnimationReader
9
+ attr_accessor :frames, :loops, :skip_first
10
+
11
+ # Construct AbstractAnimationReader object.
12
+ def initialize(path = nil)
13
+ reset
14
+ read(path) unless path.nil?
15
+ end
16
+
17
+ # Read and create frames from animation information file.
18
+ # Return array of Phantom::SVG::Frame.
19
+ def read(path, do_reset = true)
20
+ reset if do_reset
21
+
22
+ # Read parameter from spec file.
23
+ read_parameter(path)
24
+
25
+ # Change current directory to animation information file's directory.
26
+ old_dir = Dir.pwd
27
+ Dir.chdir(File.dirname(path))
28
+
29
+ # Create frames from animation information file parameter.
30
+ create_frames
31
+
32
+ # Change current directory to default.
33
+ Dir.chdir(old_dir)
34
+
35
+ # Return frames.
36
+ @frames
37
+ end
38
+
39
+ private
40
+
41
+ # Reset fields.
42
+ def reset
43
+ @name = ''
44
+ @loops = 0
45
+ @skip_first = false
46
+ @default_delay = 100.0 / 1000.0
47
+ @frame_infos = []
48
+ @delays = []
49
+ @frames = []
50
+ end
51
+
52
+ # Create frames from parameter.
53
+ def create_frames
54
+ i = 0
55
+ @frame_infos.each do |frame_info|
56
+ create_file_list(frame_info[:name]).each do |file|
57
+ reader = Parser::SVGReader.new(file, create_options(i, frame_info[:delay]))
58
+ @frames += reader.frames
59
+ i += 1
60
+ end
61
+ end
62
+ end
63
+
64
+ # Create frame options.
65
+ def create_options(index, delay_override)
66
+ result = {}
67
+ result[:duration] =
68
+ if delay_override.nil?
69
+ if @delays.empty? then @default_delay
70
+ else @delays[index % @delays.length]
71
+ end
72
+ else
73
+ delay_override
74
+ end
75
+ result
76
+ end
77
+
78
+ # Create file list.
79
+ def create_file_list(path)
80
+ result = Dir.glob(path).sort_by { |k| k[/\d+/].to_i }
81
+ if result.empty?
82
+ result <<
83
+ if File.exist?(path) then path
84
+ elsif File.exist?("#{path}.svg") then "#{path}.svg"
85
+ elsif File.exist?("#{path}.png") then "#{path}.png"
86
+ else path # Illegal path.
87
+ end
88
+ end
89
+ result
90
+ end
91
+
92
+ # Convert string to delay.
93
+ def str2delay(str)
94
+ tmp = str.to_s.split('/', 2)
95
+ tmp[0].to_f / (tmp.length > 1 ? tmp[1].to_f : 1000.0)
96
+ end
97
+
98
+ def set_parameter(key, val)
99
+ case key
100
+ when 'name' then @name = val.to_s
101
+ when 'loops' then @loops = val.to_i
102
+ when 'skip_first' then @skip_first = (val.to_s == 'true' ? true : false)
103
+ when 'default_delay' then @default_delay = str2delay(val)
104
+ end
105
+ end
106
+
107
+ def add_frame_info(name, delay = nil)
108
+ @frame_infos << { name: name.to_s, delay: (delay.nil? ? delay : str2delay(delay)) }
109
+ end
110
+
111
+ def add_delay(delay)
112
+ @delays << str2delay(delay)
113
+ end
114
+ end # class AbstractAnimationReader
115
+ end # module Parser
116
+ end # module SVG
117
+ end # module Phantom