Class: RadioVISGenerator::Slide

Inherits:
Object
  • Object
show all
Defined in:
lib/radiovis-generator/slide.rb

Direct Known Subclasses

BrandingSlide, NowPlayingSlide

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSlide

Set up the slide. Just sets the last time this slide was rendered up.



59
60
61
# File 'lib/radiovis-generator/slide.rb', line 59

def initialize
  @last_render_time = Time.now
end

Class Method Details

.composite(top_in_path, bottom_in_path, out_path) ⇒ Object

Composite two images to another image. Center gravity.



128
129
130
# 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

.render_svg(in_path, out_path) ⇒ Object

Renders an SVG to a PNG



118
119
120
# 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_to_fit(in_path, out_path, size = '640x480') ⇒ Object

Resize and (if needed) crop to the size of a slide (640x480 by default), using center gravity.



123
124
125
# 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

.rewrite_svg(in_path, out_path, sub_hash) ⇒ Object

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.



109
110
111
112
113
114
115
# 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

Instance Method Details

#background_imageObject

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.



44
45
46
# File 'lib/radiovis-generator/slide.rb', line 44

def background_image
  return nil
end

#changed?Boolean

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.

Returns:

  • (Boolean)


22
23
24
# File 'lib/radiovis-generator/slide.rb', line 22

def changed?
  return false
end

#display_timeObject

How long should we display this slide for at a minimum? Values under 5 are rarely good.



32
33
34
# File 'lib/radiovis-generator/slide.rb', line 32

def display_time
  return 5
end

#generateObject

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



6
7
8
# File 'lib/radiovis-generator/slide.rb', line 6

def generate
  return {}
end

#nameObject

Returns the name of this slide as a friendlyish string to be used in output image names.



103
104
105
# File 'lib/radiovis-generator/slide.rb', line 103

def name
  self.class.to_s.downcase.gsub("radiovisgenerator::","").gsub("slide","")
end

#priorityObject

What is the priority of this slide? Lower numbers takes precedence. Default 50.



27
28
29
# File 'lib/radiovis-generator/slide.rb', line 27

def priority
  return 50
end

#redisplay?Boolean

Have we gotten bored enough to just show this again, assuming it’s not been too little time (as defined in Slide#redisplay_delay)

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/radiovis-generator/slide.rb', line 65

def redisplay?
  if @last_render_time < Time.now - 15
    return true
  end
  return false
end

#redisplay_delayObject

How long should we wait between redisplays if we’ve got nothing better to do (ie no content changed)?



37
38
39
# File 'lib/radiovis-generator/slide.rb', line 37

def redisplay_delay
  return 23
end

#render(output_path) ⇒ Object

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

#svg_filenameObject

What SVG file are we going to render? Defaults to an empty slide.



50
51
52
# 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

#textObject

Generate the text to display alongside this slide.



11
12
13
# File 'lib/radiovis-generator/slide.rb', line 11

def text
  return "Default slide text - RadioVIS Generator"
end