lib/powerpoint/slide/pictorial.rb in powerpoint-1.5 vs lib/powerpoint/slide/pictorial.rb in powerpoint-1.6

- old
+ new

@@ -1,108 +1,56 @@ require 'zip/filesystem' require 'fileutils' +require 'fastimage' +require 'erb' module Powerpoint module Slide - class Powerpoint::Slide::Pictorial - def initialize extract_path, title, image_path, slide_number, coords={} - - image_name = File.basename(image_path) - FileUtils.copy_file(image_path, "#{extract_path}/ppt/media/#{image_name}") + class Pictorial + include Powerpoint::Util - coords_tag = (coords.empty?) ? '' : '<p:spPr> - <a:xfrm> - <a:off x="' + coords[:x].to_s + '" y="' + coords[:y].to_s + '" /> - <a:ext cx="' + coords[:cx].to_s + '" cy="' + coords[:cy].to_s + '" /> - </a:xfrm> - </p:spPr>' + attr_reader :image_name, :title, :coords, :image_path - rel_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> - <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> - <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/' + image_name + '" /> - <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout2.xml" /> - </Relationships>' + def initialize(options={}) + require_arguments [:presentation, :title, :image_path], options + options.each {|k, v| instance_variable_set("@#{k}", v)} + @coords = default_coords unless @coords.any? + @image_name = File.basename(@image_path) + end - rel_path = "#{extract_path}/ppt/slides/_rels/slide#{slide_number}.xml.rels" - File.open(rel_path, 'w'){ |f| f << rel_xml } + def save(index) + copy_media_to_extract_path + save_rel_xml(index) + save_slide_xml(index) + end - slide_xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> - <p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"> - <p:cSld> - <p:spTree> - <p:nvGrpSpPr> - <p:cNvPr id="1" name="" /> - <p:cNvGrpSpPr /> - <p:nvPr /> - </p:nvGrpSpPr> - <p:grpSpPr> - <a:xfrm> - <a:off x="0" y="0" /> - <a:ext cx="0" cy="0" /> - <a:chOff x="0" y="0" /> - <a:chExt cx="0" cy="0" /> - </a:xfrm> - </p:grpSpPr> - <p:sp> - <p:nvSpPr> - <p:cNvPr id="2" name="Title 1" /> - <p:cNvSpPr> - <a:spLocks noGrp="1" /> - </p:cNvSpPr> - <p:nvPr> - <p:ph type="title" /> - </p:nvPr> - </p:nvSpPr> - <p:spPr /> - <p:txBody> - <a:bodyPr /> - <a:lstStyle /> - <a:p> - <a:r> - <a:rPr lang="en-US" dirty="0" smtClean="0" /> - <a:t>' + title + '</a:t> - </a:r> - <a:endParaRPr lang="en-US" dirty="0" /> - </a:p> - </p:txBody> - </p:sp> - <p:pic> - <p:nvPicPr> - <p:cNvPr id="4" name="Content Placeholder 3" /> - <p:cNvPicPr> - <a:picLocks noGrp="1" noChangeAspect="1" /> - </p:cNvPicPr> - <p:nvPr> - <p:ph idx="1" /> - </p:nvPr> - </p:nvPicPr> - <p:blipFill> - <a:blip r:embed="rId2"> - <a:extLst> - <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"> - <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" /> - </a:ext> - </a:extLst> - </a:blip> - <a:stretch> - <a:fillRect /> - </a:stretch> - </p:blipFill>' + coords_tag + - '</p:pic> - </p:spTree> - <p:extLst> - <p:ext uri="{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}"> - <p14:creationId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="1474098577" /> - </p:ext> - </p:extLst> - </p:cSld> - <p:clrMapOvr> - <a:masterClrMapping /> - </p:clrMapOvr> - </p:sld>' + def file_type + File.extname(image_name).gsub('.', '') + end - slide_path = "#{extract_path}/ppt/slides/slide#{slide_number}.xml" - File.open(slide_path, 'w'){ |f| f << slide_xml } + private + + def default_coords + slide_width = pixle_to_pt(720); slide_height = pixle_to_pt(540); default_width = pixle_to_pt(550) + return {} unless dimensions = FastImage.size(image_path) + image_width, image_height = dimensions.map {|d| pixle_to_pt(d)} + new_width = default_width < image_width ? default_width : image_width + ratio = new_width / image_width.to_f + new_height = (image_height.to_f * ratio).round + {x: (slide_width / 2) - (new_width/2), y: pixle_to_pt(120), cx: new_width, cy: new_height} end + + def copy_media_to_extract_path + FileUtils.copy_file(@image_path, "#{extract_path}/ppt/media/#{@image_name}") + end + + def save_rel_xml index + File.open("#{extract_path}/ppt/slides/_rels/slide#{index}.xml.rels", 'w'){ |f| f << render_view('pictorial_rel.xml.erb') } + end + + def save_slide_xml index + File.open("#{extract_path}/ppt/slides/slide#{index}.xml", 'w'){ |f| f << render_view('pictorial_slide.xml.erb') } + end + end end end \ No newline at end of file