2015-03-30 10:27:59 +00:00
|
|
|
class AsciicastImageGenerator
|
2015-03-29 14:15:05 +00:00
|
|
|
PIXEL_DENSITY = 2
|
2015-03-27 11:35:34 +00:00
|
|
|
|
2015-03-30 10:23:58 +00:00
|
|
|
attr_reader :template_renderer, :rasterizer, :image_inspector
|
2015-03-27 11:35:34 +00:00
|
|
|
|
2015-03-30 10:23:58 +00:00
|
|
|
def initialize(template_renderer, rasterizer = Rasterizer.new, image_inspector = ImageInspector.new)
|
|
|
|
@template_renderer = template_renderer
|
2015-03-29 14:15:05 +00:00
|
|
|
@rasterizer = rasterizer
|
|
|
|
@image_inspector = image_inspector
|
2015-03-27 11:35:34 +00:00
|
|
|
end
|
|
|
|
|
2015-03-30 10:27:59 +00:00
|
|
|
def generate(asciicast)
|
2015-03-27 11:35:34 +00:00
|
|
|
Dir.mktmpdir do |dir|
|
2017-01-12 10:12:33 +00:00
|
|
|
asciicast_url = asciicast.data.absolute_url
|
2015-03-29 14:15:05 +00:00
|
|
|
image_path = "#{dir}/#{asciicast.image_filename}"
|
2017-01-11 14:25:48 +00:00
|
|
|
time = asciicast.snapshot_at || asciicast.duration / 2
|
2017-01-11 18:06:38 +00:00
|
|
|
theme = AsciicastDecorator.new(asciicast).theme_name
|
2015-03-27 11:35:34 +00:00
|
|
|
|
2017-01-11 18:06:38 +00:00
|
|
|
rasterizer.generate_image(asciicast_url, image_path, time, PIXEL_DENSITY, theme)
|
2015-03-30 16:41:45 +00:00
|
|
|
image_width, image_height = get_size(image_path)
|
2015-03-27 11:35:34 +00:00
|
|
|
|
2015-03-30 10:23:58 +00:00
|
|
|
update_asciicast(asciicast, image_path, image_width, image_height)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-03-30 16:41:45 +00:00
|
|
|
def get_size(image_path)
|
|
|
|
width, height = image_inspector.get_size(image_path)
|
|
|
|
|
|
|
|
# "display" size is 1/PIXEL_DENSITY of the actual one
|
|
|
|
[width / PIXEL_DENSITY, height / PIXEL_DENSITY]
|
|
|
|
end
|
|
|
|
|
2015-03-30 10:23:58 +00:00
|
|
|
def update_asciicast(asciicast, image_path, image_width, image_height)
|
|
|
|
File.open(image_path) do |f|
|
|
|
|
asciicast.image = f
|
2015-03-27 11:35:34 +00:00
|
|
|
end
|
2015-03-30 10:23:58 +00:00
|
|
|
|
2015-03-30 16:41:45 +00:00
|
|
|
asciicast.image_width = image_width
|
|
|
|
asciicast.image_height = image_height
|
2015-03-30 10:23:58 +00:00
|
|
|
|
|
|
|
asciicast.save!
|
2015-03-27 11:35:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|