Sha256: 1bee8d15c95297121f647c2754dadcda1a8aacc0824ac51acac03ecc2ae87ac0

Contents?: true

Size: 1.61 KB

Versions: 10

Compression:

Stored size: 1.61 KB

Contents

require 'rsvg2'
require 'rexml/document'

require_relative '../frame'
require_relative 'abstract_image_reader'

module Phantom
  module SVG
    module Parser
      # JPEG reader.
      class JPEGReader < AbstractImageReader
        # Read jpeg file from path.
        def read(path, _options = {})
          reset

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

          frame = create_frame(path)
          @frames << frame
          @width = "#{frame.width}px"
          @height = "#{frame.height}px"
        end

        private

        # Create frame.
        def create_frame(path, duration = nil)
          pixbuf = Gdk::Pixbuf.new(path)

          frame = Phantom::SVG::Frame.new
          frame.width = "#{pixbuf.width}px"
          frame.height = "#{pixbuf.height}px"
          frame.viewbox.set_from_text("0 0 #{pixbuf.width} #{pixbuf.height}")
          frame.surfaces = create_surfaces(path, pixbuf.width, pixbuf.height)
          frame.duration = duration unless duration.nil?
          frame.namespaces = { 'xmlns' => 'http://www.w3.org/2000/svg',
                               'xlink' => 'http://www.w3.org/1999/xlink' }
          frame
        end

        # Create surfaces.
        def create_surfaces(path, width, height)
          bin = File.binread(path)
          base64 = [bin].pack('m')

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

          [image]
        end
      end # class JPEGReader
    end # module Parser
  end # module SVG
end # module Phantom

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
phantom_svg-1.2.1 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.2.0 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.9 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.8 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.6 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.5 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.4 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.3 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.2 lib/phantom/parser/jpeg_reader.rb
phantom_svg-1.1.1 lib/phantom/parser/jpeg_reader.rb