phantom_svg 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bef19b043049de58cbe8f6f6b1760f25415760d7
4
- data.tar.gz: 42612111601ec34a3383ff81909a95c2e403397b
3
+ metadata.gz: e4b4dfe8fa254071a19ef5dedefca7f3e2d92f71
4
+ data.tar.gz: 9f420b9d38ac37a1c5bba9dbf973a723e9027211
5
5
  SHA512:
6
- metadata.gz: 42c97170d698d23a0adba93a12f9fe6e8f0021b5a0d10b4f10e5bbda198354a072747f2f5fb7ed8e17cec461564eeb006bdf91ccb2dd83b1229f9c382e506229
7
- data.tar.gz: 61dc006678e0d5524381c1d198fcd1b1638032cac52342671bf31ddf0a31b698b88d4aa67041a948f061ca53b4e4ea93d0a8b504e5e6bbd73eb8dfc8c762acd4
6
+ metadata.gz: 32ab275348cecb39938958c02fdebe8cd6b2165bca705813f0d41d2511b52be5defbcd17584c7335868b66fd6fbff84aebde6dccf0bb55f2054a6a6bd42684b5
7
+ data.tar.gz: 15a9aac286b4fa0f53024dd290994612f7cab34da7a07cec6fda16cac53be42382a48331095b23fbe7d8d19d39b6ad8dca148e1af100c0aba232d6ed0c533dc3
@@ -13,7 +13,7 @@ module Phantom
13
13
  end
14
14
 
15
15
  # Read image file from path.
16
- def read(path, options = {})
16
+ def read(_path, _options = {})
17
17
  fail 'Called abstract method.'
18
18
  end
19
19
 
@@ -11,7 +11,7 @@ module Phantom
11
11
 
12
12
  # Write image file from object to path.
13
13
  # Return write size.
14
- def write(path, object)
14
+ def write(_path, _object)
15
15
  fail 'Called abstract method.'
16
16
  end
17
17
  end # class AbstractImageWriter
@@ -13,7 +13,7 @@ module Phantom
13
13
  class GIFReader < AbstractImageReader
14
14
  include Magick
15
15
  # Read gif file from path.
16
- def read(path, options = {})
16
+ def read(path, _options = {})
17
17
  reset
18
18
 
19
19
  return if path.nil? || path.empty?
@@ -27,24 +27,27 @@ module Phantom
27
27
  private
28
28
 
29
29
  # Create frames for each frame in the gif.
30
- def create_frames(path, duration = nil)
30
+ def create_frames(path, _duration = nil)
31
31
  frames = []
32
32
  lst = ImageList.new path
33
33
 
34
34
  lst.each do |img|
35
- frame = Phantom::SVG::Frame.new
36
- frame.width = "#{img.columns}px"
37
- frame.height = "#{img.rows}px"
38
- frame.viewbox.set_from_text("0 0 #{img.columns} #{img.rows}")
39
- frame.surfaces = create_surfaces(img)
40
- frame.duration = img.delay * 0.1 unless img.delay.nil?
41
- frame.namespaces = {
42
- 'xmlns' => 'http://www.w3.org/2000/svg',
43
- 'xlink' => 'http://www.w3.org/1999/xlink'
44
- }
35
+ frame = set_param(img)
45
36
  frames << frame
46
37
  end
47
- frames
38
+ frames
39
+ end
40
+
41
+ def set_param(img)
42
+ frame = Phantom::SVG::Frame.new
43
+ frame.width = "#{img.columns}px"
44
+ frame.height = "#{img.rows}px"
45
+ frame.viewbox.set_from_text("0 0 #{img.columns} #{img.rows}")
46
+ frame.surfaces = create_surfaces(img)
47
+ frame.duration = img.delay * 0.1 unless img.delay.nil?
48
+ frame.namespaces = { 'xmlns' => 'http://www.w3.org/2000/svg',
49
+ 'xlink' => 'http://www.w3.org/1999/xlink' }
50
+ frame
48
51
  end
49
52
 
50
53
  # Create surfaces.
@@ -10,7 +10,7 @@ module Phantom
10
10
  # JPEG reader.
11
11
  class JPEGReader < AbstractImageReader
12
12
  # Read jpeg file from path.
13
- def read(path, options = {})
13
+ def read(path, _options = {})
14
14
  reset
15
15
 
16
16
  return if path.nil? || path.empty?
@@ -33,11 +33,8 @@ module Phantom
33
33
  frame.viewbox.set_from_text("0 0 #{pixbuf.width} #{pixbuf.height}")
34
34
  frame.surfaces = create_surfaces(path, pixbuf.width, pixbuf.height)
35
35
  frame.duration = duration unless duration.nil?
36
- frame.namespaces = {
37
- 'xmlns' => 'http://www.w3.org/2000/svg',
38
- 'xlink' => 'http://www.w3.org/1999/xlink'
39
- }
40
-
36
+ frame.namespaces = { 'xmlns' => 'http://www.w3.org/2000/svg',
37
+ 'xlink' => 'http://www.w3.org/1999/xlink' }
41
38
  frame
42
39
  end
43
40
 
@@ -0,0 +1,18 @@
1
+ require 'rexml/xmldecl'
2
+
3
+ module Phantom
4
+ module SVG
5
+ module Parser
6
+ # convert single quotes into double quotes.
7
+ class PhantomXMLDecl < REXML::XMLDecl
8
+ private
9
+ def content(enc)
10
+ rv = "version=\"#@version\""
11
+ rv << " encoding=\"#{enc}\"" if @writeencoding || enc !~ /\Autf-8\z/i
12
+ rv << " standalone=\"#@standalone\"" if @standalone
13
+ rv
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -13,7 +13,7 @@ module Phantom
13
13
  # PNG reader.
14
14
  class PNGReader < AbstractImageReader
15
15
  # Read png file from path.
16
- def read(path, options = {})
16
+ def read(path, _options = {})
17
17
  reset
18
18
 
19
19
  return if path.nil? || path.empty?
@@ -40,16 +40,7 @@ module Phantom
40
40
  def read_apng(apngasm)
41
41
  @width = @height = 0
42
42
  Dir.mktmpdir(nil, File.dirname(__FILE__)) do |dir|
43
- # Create temporary file.
44
- apngasm.save_pngs(dir)
45
-
46
- # Create frames.
47
- apngasm.get_frames.each_with_index do |png_frame, index|
48
- @width = png_frame.width if @width < png_frame.width
49
- @height = png_frame.height if @height < png_frame.height
50
- duration = png_frame.delay_numerator.to_f / png_frame.delay_denominator.to_f
51
- @frames << create_frame("#{dir}/#{index}.png", duration)
52
- end
43
+ set_frame(apngasm, dir)
53
44
  end
54
45
 
55
46
  @width = "#{@width}px"
@@ -59,21 +50,36 @@ module Phantom
59
50
  @has_animation = true
60
51
  end
61
52
 
53
+ def set_frame(apngasm, dir)
54
+ # Create temporary file.
55
+ apngasm.save_pngs(dir)
56
+
57
+ # Create frames.
58
+ apngasm.get_frames.each_with_index do |png_frame, index|
59
+ @width = png_frame.width if @width < png_frame.width
60
+ @height = png_frame.height if @height < png_frame.height
61
+ duration = png_frame.delay_numerator.to_f / png_frame.delay_denominator.to_f
62
+ @frames << create_frame("#{dir}/#{index}.png", duration)
63
+ end
64
+ end
65
+
62
66
  # Create frame.
63
67
  def create_frame(path, duration = nil)
64
68
  pixbuf = Gdk::Pixbuf.new(path)
69
+ frame = set_param(path, pixbuf, duration)
65
70
 
71
+ frame
72
+ end
73
+
74
+ def set_param(path, pixbuf, duration)
66
75
  frame = Phantom::SVG::Frame.new
67
76
  frame.width = "#{pixbuf.width}px"
68
77
  frame.height = "#{pixbuf.height}px"
69
78
  frame.viewbox.set_from_text("0 0 #{pixbuf.width} #{pixbuf.height}")
70
79
  frame.surfaces = create_surfaces(path, pixbuf.width, pixbuf.height)
71
80
  frame.duration = duration unless duration.nil?
72
- frame.namespaces = {
73
- 'xmlns' => 'http://www.w3.org/2000/svg',
74
- 'xlink' => 'http://www.w3.org/1999/xlink'
75
- }
76
-
81
+ frame.namespaces = { 'xmlns' => 'http://www.w3.org/2000/svg',
82
+ 'xlink' => 'http://www.w3.org/1999/xlink' }
77
83
  frame
78
84
  end
79
85
 
@@ -91,7 +97,6 @@ module Phantom
91
97
 
92
98
  [image]
93
99
  end
94
-
95
100
  end # class PNGReader
96
101
  end # Parser
97
102
  end # SVG
@@ -46,7 +46,8 @@ module Phantom
46
46
  handle = RSVG::Handle.new_from_file("#{path}.svg")
47
47
  surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, width, height)
48
48
  context = Cairo::Context.new(surface)
49
- context.scale(width.to_f / handle.dimensions.width, height.to_f / handle.dimensions.height)
49
+ context.scale(width.to_f / handle.dimensions.width,
50
+ height.to_f / handle.dimensions.height)
50
51
  context.render_rsvg_handle(handle)
51
52
  surface.write_to_png("#{path}.png")
52
53
  surface.finish
@@ -47,18 +47,34 @@ module Phantom
47
47
 
48
48
  # Read size from node to dest.
49
49
  def read_size(node, dest, options = {})
50
- dest.viewbox.set_from_text(choice_value(node.attributes['viewBox'], options[:viewbox]).to_s) unless node.attributes['viewBox'].nil?
50
+ set_view_box(node, dest, options)
51
+ set_width(node, dest, options)
52
+ set_height(node, dest, options)
53
+ end
54
+
55
+ def set_view_box(node, dest, options = {})
56
+ return if node.attributes['viewBox'].nil?
57
+ dest.viewbox.set_from_text(choice_value(node.attributes['viewBox'],
58
+ options[:viewbox]).to_s)
59
+ end
51
60
 
61
+ def set_width(node, dest, options = {})
52
62
  if node.attributes['width'].nil?
53
- dest.instance_variable_set(:@width, choice_value("#{dest.viewbox.width}px", options[:width]))
63
+ dest.instance_variable_set(:@width,
64
+ choice_value("#{dest.viewbox.width}px", options[:width]))
54
65
  else
55
- dest.instance_variable_set(:@width, choice_value(node.attributes['width'], options[:width]))
66
+ dest.instance_variable_set(:@width,
67
+ choice_value(node.attributes['width'], options[:width]))
56
68
  end
69
+ end
57
70
 
71
+ def set_height(node, dest, options = {})
58
72
  if node.attributes['height'].nil?
59
- dest.instance_variable_set(:@height, choice_value("#{dest.viewbox.height}px", options[:height]))
73
+ dest.instance_variable_set(:@height,
74
+ choice_value("#{dest.viewbox.height}px", options[:height]))
60
75
  else
61
- dest.instance_variable_set(:@height, choice_value(node.attributes['height'], options[:height]))
76
+ dest.instance_variable_set(:@height,
77
+ choice_value(node.attributes['height'], options[:height]))
62
78
  end
63
79
  end
64
80
 
@@ -2,6 +2,7 @@
2
2
  require 'rexml/document'
3
3
 
4
4
  require_relative 'abstract_image_writer.rb'
5
+ require_relative 'phantom_xmldecl.rb'
5
6
 
6
7
  module Phantom
7
8
  module SVG
@@ -33,7 +34,8 @@ module Phantom
33
34
  # Reset SVGWriter object.
34
35
  def reset
35
36
  @root = REXML::Document.new
36
- @root << REXML::XMLDecl.new('1.0', 'UTF-8')
37
+ @root.context[:attribute_quote] = :quote
38
+ @root << Phantom::SVG::Parser::PhantomXMLDecl.new('1.0', 'UTF-8')
37
39
  @root << REXML::Comment.new(' Generated by phantom_svg. ')
38
40
  end
39
41
 
data/lib/phantom/svg.rb CHANGED
@@ -30,15 +30,19 @@ module Phantom
30
30
 
31
31
  def add_frame_from_file(path, options = {})
32
32
  create_file_list(path).each do |file|
33
- case File.extname(file)
34
- when '.svg' then load_from_svg(file, options)
35
- when '.png' then load_from_png(file, options)
36
- when '.jpg' then load_from_jpeg(file, options)
37
- when '.jpeg' then load_from_jpeg(file, options)
38
- when '.gif' then load_from_gif(file, options)
39
- when '.json' then load_from_json(file, options)
40
- when '.xml' then load_from_xml(file, options)
41
- end
33
+ load_file(file, options)
34
+ end
35
+ end
36
+
37
+ def load_file(file, options)
38
+ case File.extname(file)
39
+ when '.svg' then load_from_svg(file, options)
40
+ when '.png' then load_from_png(file, options)
41
+ when '.jpg' then load_from_jpeg(file, options)
42
+ when '.jpeg' then load_from_jpeg(file, options)
43
+ when '.gif' then load_from_gif(file, options)
44
+ when '.json' then load_from_json(file, options)
45
+ when '.xml' then load_from_xml(file, options)
42
46
  end
43
47
  end
44
48
 
@@ -53,9 +57,13 @@ module Phantom
53
57
  end
54
58
 
55
59
  def set_size(width = nil, height = nil)
56
- # width
57
- if width.nil? then
58
- if @width.nil? || @width == 0 then
60
+ set_width(width)
61
+ set_height(height)
62
+ end
63
+
64
+ def set_width(width)
65
+ if width.nil?
66
+ if @width.nil? || @width == 0
59
67
  frames.each do |frame|
60
68
  @width = frame.width.to_i if frame.width.to_i > @width
61
69
  end
@@ -63,10 +71,11 @@ module Phantom
63
71
  else
64
72
  @width = width
65
73
  end
74
+ end
66
75
 
67
- # height
68
- if height.nil? then
69
- if @height.nil? || @height == 0 then
76
+ def set_height(height)
77
+ if height.nil?
78
+ if @height.nil? || @height == 0
70
79
  frames.each do |frame|
71
80
  @height = frame.height.to_i if frame.height.to_i > @height
72
81
  end
@@ -120,6 +129,14 @@ module Phantom
120
129
  result
121
130
  end
122
131
 
132
+ def run_gc
133
+ @frames = nil
134
+
135
+ disabled = GC.enable
136
+ GC.start
137
+ GC.disable if disabled
138
+ end
139
+
123
140
  private
124
141
 
125
142
  def load_from_svg(path, options)
@@ -178,7 +195,7 @@ module Phantom
178
195
  load_from_reader(Parser::XMLAnimationReader.new(path), options)
179
196
  end
180
197
 
181
- def load_from_reader(reader, options)
198
+ def load_from_reader(reader, _options)
182
199
  if @frames.empty?
183
200
  @loops = reader.loops
184
201
  @skip_first = reader.skip_first
data/phantom_svg.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.platform = Gem::Platform::RUBY
3
3
  s.name = 'phantom_svg'
4
- s.version = '1.1.0'
4
+ s.version = '1.1.1'
5
5
  s.license = 'LGPL-3'
6
6
  s.summary = 'Hight end SVG manipulation tools for Ruby'
7
7
  s.description = 'Hight end SVG manipulation tools for Ruby.\n' \
@@ -463,7 +463,6 @@ describe Phantom::SVG::Base do
463
463
  expect(@loader.width).to eq('64px')
464
464
  expect(@loader.height).to eq('64px')
465
465
  expect(@loader.loops).to eq(0)
466
- expect(@loader.skip_first).to eq(true)
467
466
  end
468
467
 
469
468
  it 'loads an animation spec from a JSON.' do
@@ -514,7 +513,7 @@ describe Phantom::SVG::Base do
514
513
  @loader = Phantom::SVG::Base.new
515
514
  end
516
515
 
517
- it "loads frames." do
516
+ it 'loads frames.' do
518
517
  test_name = 'jpeg_test'
519
518
  @loader.add_frame_from_file("#{@source_dir}/*.jpg")
520
519
  expect(@loader.frames.size).to eq(34)
@@ -545,7 +544,7 @@ describe Phantom::SVG::Base do
545
544
  @loader = Phantom::SVG::Base.new
546
545
  end
547
546
 
548
- it "loads frames / saves to SVG and PNG" do
547
+ it 'loads frames / saves to SVG and PNG' do
549
548
  test_name = 'gif_test'
550
549
  @loader.add_frame_from_file(@source)
551
550
  expect(@loader.frames.size).to eq(34)
@@ -562,6 +561,8 @@ describe Phantom::SVG::Base do
562
561
  expect(write_size).not_to eq(0)
563
562
  write_size = @loader.save_apng("#{@destination_dir}/#{test_name}.png")
564
563
  expect(write_size).not_to eq(0)
564
+
565
+ @loader.run_gc
565
566
  end
566
567
  end
567
568
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phantom_svg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rika Yoshida
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-10-09 00:00:00.000000000 Z
13
+ date: 2014-10-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: cairo
@@ -90,6 +90,7 @@ files:
90
90
  - lib/phantom/parser/gif_reader.rb
91
91
  - lib/phantom/parser/jpeg_reader.rb
92
92
  - lib/phantom/parser/json_animation_reader.rb
93
+ - lib/phantom/parser/phantom_xmldecl.rb
93
94
  - lib/phantom/parser/png_reader.rb
94
95
  - lib/phantom/parser/png_writer.rb
95
96
  - lib/phantom/parser/svg_reader.rb