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.



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

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.



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

def frames
  @frames
end

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#loopsObject

Returns the value of attribute loops.



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

def loops
  @loops
end

#skip_firstObject

Returns the value of attribute skip_first.



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

def skip_first
  @skip_first
end

#widthObject

Returns the value of attribute width.



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

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



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

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



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

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

#combine(path) ⇒ Object

Combine image.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/phantom/svg.rb', line 132

def combine(path)
  src = Base.new(path, {unique_ids: true})

  if @width != src.width || @height != src.height
    fail "Can't combine source images of different sizes."
    return
  end

  if @frames.length == 1
    rest_duration = (src.total_duration * 1000).to_i
    @frames[0].duration = rest_duration * 0.001
  elsif src.frames.length == 1
    rest_duration = (total_duration * 1000).to_i
    src.frames[0].duration = rest_duration * 0.001
  else
    rest_duration = (total_duration * 1000).to_i.lcm((src.total_duration * 1000).to_i)
  end

  base_i = src_i = -1
  base_duration = src_duration = 0;
  base_frame = src_frame = nil
  new_frames = []

  begin
    if base_duration == 0
      base_i = (base_i + 1) % @frames.length
      base_frame = @frames[base_i]
      base_duration = (base_frame.duration * 1000).to_i
    end

    if src_duration == 0
      src_i = (src_i + 1) % src.frames.length
      src_frame = src.frames[src_i]
      src_duration = (src_frame.duration * 1000).to_i
    end

    elapsed = [base_duration, src_duration].min

    new_frame = base_frame.clone
    new_frame.duration = elapsed * 0.001
    new_frame.surfaces += src_frame.surfaces
    new_frame.namespaces = src_frame.namespaces.merge(new_frame.namespaces)
    new_frames << new_frame

    base_duration -= elapsed
    src_duration -= elapsed
    rest_duration -= elapsed
  end while rest_duration > 0

  @frames = new_frames
end

#load_file(file, options) ⇒ Object



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

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



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

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

#save_apng(path) ⇒ Object



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

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

#save_svg(path) ⇒ Object



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

def save_svg(path)
  set_size

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

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



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

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



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

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



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

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



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

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



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

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

#set_width(width) ⇒ Object



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

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.



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

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