class Phantom::SVG::Parser::GIFReader

GIF reader.

Public Instance Methods

read(path, _options = {}) click to toggle source

Read gif file from path.

# File lib/phantom/parser/gif_reader.rb, line 16
def read(path, _options = {})
  reset

  return if path.nil? || path.empty?

  frames = create_frames(path)
  @frames += frames
  @width = "#{frames.first.width}"
  @height = "#{frames.first.height}"
  @loops = 0
  @skip_first = frames[0].duration == 0.0
  @has_animation = true
end

Private Instance Methods

create_frames(path, _duration = nil) click to toggle source

Create frames for each frame in the gif.

# File lib/phantom/parser/gif_reader.rb, line 33
def create_frames(path, _duration = nil)
  frames = []
  lst = ImageList.new path
  lst = lst.coalesce

  lst.each do |img|
    frame = set_param(img)
    frames << frame
  end

  frames
end
create_surfaces(img) click to toggle source

Create surfaces.

# File lib/phantom/parser/gif_reader.rb, line 59
def create_surfaces(img)
  img.format = 'PNG'
  base64 = Base64.encode64(img.to_blob)

  image = REXML::Element.new('image')
  image.add_attributes(
    'width' => img.columns,
    'height' => img.rows,
    'xlink:href' => "data:image/png;base64,#{base64}"
  )

  [image]
end
set_param(img) click to toggle source
# File lib/phantom/parser/gif_reader.rb, line 46
def set_param(img)
  frame = Phantom::SVG::Frame.new
  frame.width = "#{img.columns}px"
  frame.height = "#{img.rows}px"
  frame.viewbox.set_from_text("0 0 #{img.columns} #{img.rows}")
  frame.surfaces = create_surfaces(img)
  frame.duration = img.delay * 0.01 unless img.delay.nil?
  frame.namespaces = { 'xmlns' => 'http://www.w3.org/2000/svg',
                       'xlink' => 'http://www.w3.org/1999/xlink' }
  frame
end