Sha256: 4b96b215e298ffc847aac72fdac30f0506c37c9faf7c57dfdd85ec3ae0c0560a

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require 'zip/filesystem'
require 'fileutils'
require 'fastimage'
require 'erb'

module RubySlides
  module Slide
    class DescriptionPic
      include RubySlides::Util

      attr_reader :title, :content, :image_name, :image_path, :coords

      def initialize(options={})
        require_arguments [:presentation, :title, :image_path, :content], options
        options.each {|k, v| instance_variable_set("@#{k}", v)}
        @coords = default_coords
        @image_name = File.basename(@image_path)
      end

      def save(extract_path, index)
        copy_media(extract_path, @image_path)
        save_rel_xml(extract_path, index)
        save_slide_xml(extract_path, index)
      end

      def file_type
        File.extname(image_name).gsub('.', '')
      end

      def default_coords
        slide_width = pixel_to_pt(720)
        default_width = pixel_to_pt(550)
        default_height = pixel_to_pt(300)

        return {} unless dimensions = FastImage.size(image_path)
        image_width, image_height = dimensions.map {|d| pixel_to_pt(d)}

        capped_width = [default_width, image_width].min
        w_ratio = capped_width / image_width.to_f

        capped_height = [default_height, image_height].min
        h_ratio = capped_height / image_height.to_f

        ratio = [w_ratio, h_ratio].min

        new_width = (image_width.to_f * ratio).round
        new_height = (image_height.to_f * ratio).round
        {x: (slide_width / 2) - (new_width/2), y: pixel_to_pt(60), cx: new_width, cy: new_height}
      end
      private :default_coords

      def save_rel_xml(extract_path, index)
        render_view('picture_description_rels.xml.erb', "#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels", index: 2)
      end
      private :save_rel_xml

      def save_slide_xml(extract_path, index)
        render_view('picture_description_slide.xml.erb', "#{extract_path}/ppt/slides/slide#{index}.xml")
      end
      private :save_slide_xml
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_slides-1.2.1 lib/ruby_slides/slide/picture_description.rb
ruby_slides-1.2.0 lib/ruby_slides/slide/picture_description.rb