Class: Phantom::SVG::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/phantom/svg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, options = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
# File 'lib/phantom/svg.rb', line 17

def initialize(path = nil, options = {})
  reset

  add_frame_from_file(path, options) if path
end

Instance Attribute Details

#framesObject

Returns the value of attribute frames.



15
16
17
# File 'lib/phantom/svg.rb', line 15

def frames
  @frames
end

#heightObject

Returns the value of attribute height.



15
16
17
# File 'lib/phantom/svg.rb', line 15

def height
  @height
end

#loopsObject

Returns the value of attribute loops.



15
16
17
# File 'lib/phantom/svg.rb', line 15

def loops
  @loops
end

#skip_firstObject

Returns the value of attribute skip_first.



15
16
17
# File 'lib/phantom/svg.rb', line 15

def skip_first
  @skip_first
end

#widthObject

Returns the value of attribute width.



15
16
17
# File 'lib/phantom/svg.rb', line 15

def width
  @width
end

Instance Method Details

#add_frame(frame = nil, options = {}) ⇒ Object

Creates a blank frame when no arguments are passed Takes another Phantom::SVG object or file path



51
52
53
54
55
56
57
# File 'lib/phantom/svg.rb', line 51

def add_frame(frame = nil, options = {})
  if    frame.nil?                              then @frames << Phantom::SVG::Frame.new
  elsif frame.instance_of?(Phantom::SVG::Frame) then @frames << frame
  elsif frame.instance_of?(Phantom::SVG::Base)  then @frames += frame.frames
  elsif frame.instance_of?(String)              then add_frame_from_file(frame, options)
  end
end

#add_frame_from_file(path, options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/phantom/svg.rb', line 31

def add_frame_from_file(path, options = {})
  create_file_list(path).each do |file|
    load_file(file, options)
  end
end

#load_file(file, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/phantom/svg.rb', line 37

def load_file(file, options)
  case File.extname(file).downcase
  when '.svg'   then  load_from_svg(file, options)
  when '.png'   then  load_from_png(file, options)
  when '.jpg'   then  load_from_jpeg(file, options)
  when '.jpeg'  then  load_from_jpeg(file, options)
  when '.gif'   then  load_from_gif(file, options)
  when '.json'  then  load_from_json(file, options)
  when '.xml'   then  load_from_xml(file, options)
  end
end

#resetObject



23
24
25
26
27
28
29
# File 'lib/phantom/svg.rb', line 23

def reset
  @frames = []
  @width = 0
  @height = 0
  @loops = 0
  @skip_first = false
end

#save_apng(path) ⇒ Object



118
119
120
# File 'lib/phantom/svg.rb', line 118

def save_apng(path)
  Parser::PNGWriter.new.write(path, self)
end

#save_svg(path) ⇒ Object



98
99
100
101
102
# File 'lib/phantom/svg.rb', line 98

def save_svg(path)
  set_size

  Parser::SVGWriter.new.write(path, self)
end

#save_svg_frame(path, frame, width = nil, height = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/phantom/svg.rb', line 104

def save_svg_frame(path, frame, width = nil, height = nil)
  old_width = frame.width
  old_height = frame.height
  frame.width = width unless width.nil?
  frame.height = height unless height.nil?

  write_size = Parser::SVGWriter.new.write(path, frame)

  frame.width = old_width
  frame.height = old_height

  write_size
end

#scale_h(height) ⇒ Object



93
94
95
96
# File 'lib/phantom/svg.rb', line 93

def scale_h(height)
  @width = (@width.to_i * height.to_i / @height.to_i).to_i
  @height = height.to_i
end

#scale_w(width) ⇒ Object



88
89
90
91
# File 'lib/phantom/svg.rb', line 88

def scale_w(width)
  @height = (@height.to_i * width.to_i / @width.to_i).to_i
  @width = width.to_i
end

#set_height(height) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/phantom/svg.rb', line 76

def set_height(height)
  if height.nil?
    if @height.nil? || @height == 0
      frames.each do |frame|
        @height = frame.height.to_i if frame.height.to_i > @height
      end
    end
  else
    @height = height
  end
end

#set_size(width = nil, height = nil) ⇒ Object



59
60
61
62
# File 'lib/phantom/svg.rb', line 59

def set_size(width = nil, height = nil)
  set_width(width)
  set_height(height)
end

#set_width(width) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/phantom/svg.rb', line 64

def set_width(width)
  if width.nil?
    if @width.nil? || @width == 0
      frames.each do |frame|
        @width = frame.width.to_i if frame.width.to_i > @width
      end
    end
  else
    @width = width
  end
end

#total_durationObject

Calculate and return total duration.



123
124
125
126
127
128
129
130
# File 'lib/phantom/svg.rb', line 123

def total_duration
  result = 0.0
  @frames.each_with_index do |frame, i|
    next if i == 0 && @skip_first
    result += frame.duration
  end
  result
end