class RadioVISGenerator::Slide
Public Class Methods
Composite two images to another image. Center gravity.
# File lib/radiovis-generator/slide.rb, line 128 def self.composite(top_in_path, bottom_in_path, out_path) `composite -gravity center #{top_in_path} #{bottom_in_path} #{out_path}` end
Set up the slide. Just sets the last time this slide was rendered up.
# File lib/radiovis-generator/slide.rb, line 59 def initialize @last_render_time = Time.now end
Renders an SVG to a PNG
# File lib/radiovis-generator/slide.rb, line 118 def self.render_svg(in_path, out_path) `inkscape -f #{in_path} -e #{out_path}` end
Resize and (if needed) crop to the size of a slide (640x480 by default), using center gravity.
# File lib/radiovis-generator/slide.rb, line 123 def self.resize_to_fit(in_path, out_path, size='640x480') `convert #{in_path} -resize #{size}^ -gravity center -extent #{size} #{out_path}` end
Rewrites an SVG at a given input path with the information in the given hash to the given output path. Hash is of the format “$$TARGET$$” => “Content”, where $$TARGET$$ is the target string to replace in the SVG and Content is the substitute.
# File lib/radiovis-generator/slide.rb, line 109 def self.rewrite_svg(in_path, out_path, sub_hash) svg_input = File.open(in_path).read() sub_hash.each_pair do |k,v| svg_input = svg_input.gsub(k,v) end File.open(out_path, 'w'){|f|f<<svg_input} end
Public Instance Methods
What image makes the background of this slide, and should be composited over it? Return nil to skip the composition stage and just display the SVG output. Defaults to nil.
# File lib/radiovis-generator/slide.rb, line 44 def background_image return nil end
Has this slide changed since last rendered? Reset anything you need to in the Slide#generate
method. For instance, if I’m showing the current show or now playing, I’d check if the current track on air
was the same as the one I'd stored last time I'd rendered. If it were different I'd return true. Then in Slide#generate, I'd store the current track on air.
This means we can get very prompt updates on realtime events - maximum delay defined by your largest
display_time of all the slides, ie 5 seconds with nothing overridden.
# File lib/radiovis-generator/slide.rb, line 22 def changed? return false end
How long should we display this slide for at a minimum? Values under 5 are rarely good.
# File lib/radiovis-generator/slide.rb, line 32 def display_time return 5 end
Generate the substitution hash to process on this run. This is where we add our dynamic content! Returns a hash where “$$TARGET$$” => “Content”, where $$TARGET$$ is the target string to replace in the SVG and Content is the substitute. This is passed to RadioVISGenerator::Slide.rewrite_svg
# File lib/radiovis-generator/slide.rb, line 6 def generate return {} end
Returns the name of this slide as a friendlyish string to be used in output image names.
# File lib/radiovis-generator/slide.rb, line 103 def name self.class.to_s.downcase.gsub("radiovisgenerator::","").gsub("slide","") end
What is the priority of this slide? Lower numbers takes precedence. Default 50.
# File lib/radiovis-generator/slide.rb, line 27 def priority return 50 end
Have we gotten bored enough to just show this again, assuming it’s not been too little time (as defined in Slide#redisplay_delay
)
# File lib/radiovis-generator/slide.rb, line 65 def redisplay? if @last_render_time < Time.now - 15 return true end return false end
How long should we wait between redisplays if we’ve got nothing better to do (ie no content changed)?
# File lib/radiovis-generator/slide.rb, line 37 def redisplay_delay return 23 end
Render this slide, returns a hash with image_big, image_small, output_path, name and text. Paths returned are relative to the provided output_path. Will create the output path if needed. Shouldn’t be overriden unless you know what you’re doing. Calls Slide#generate
and Slide#text
to get dynamic content and then spits out some images.
# File lib/radiovis-generator/slide.rb, line 77 def render(output_path) # Reset time-based cycling @last_render_time = Time.now # Make sure our output path exists FileUtils.mkdir_p(output_path) rescue nil Dir.mkdir('/tmp/radiovis-generator') rescue nil # Make our temporary storage RadioVISGenerator::Slide.rewrite_svg(svg_filename, "/tmp/#{self.name}.svg", self.generate) if background_image RadioVISGenerator::Slide.render_svg("/tmp/#{self.name}.svg", "/tmp/#{self.name}-precomp.png") RadioVISGenerator::Slide.composite("/tmp/#{self.name}-precomp.png", background_image, File.join(output_path, "#{self.name}-640x480.png")) else RadioVISGenerator::Slide.render_svg("/tmp/#{self.name}.svg", File.join(output_path, "#{self.name}-640x480.png")) end FileUtils.rm_rf('/tmp/radiovis-generator') rescue nil # Clean up after ourselves # Make our smaller-res version RadioVISGenerator::Slide.resize_to_fit(File.join(output_path, "#{self.name}-640x480.png"), File.join(output_path, "#{self.name}-320x240.png"), '320x240') return { image_big: "#{self.name}-640x480.png", image_small: "#{self.name}-320x240.png", text: self.text, output_path: output_path, name: self.name } end
What SVG file are we going to render? Defaults to an empty slide.
# File lib/radiovis-generator/slide.rb, line 50 def svg_filename return "#{File.expand_path(File.dirname(File.dirname(__FILE__)))}/templates/empty-slide.svg" end
Generate the text to display alongside this slide.
# File lib/radiovis-generator/slide.rb, line 11 def text return "Default slide text - RadioVIS Generator" end