Class: Storyboard::Renderer

Inherits:
Object show all
Defined in:
lib/storyboard/generators/sub.rb

Direct Known Subclasses

GifRenderer, PDFRenderer

Constant Summary collapse

@@size_canvas =
Prawn::Document.new

Instance Method Summary collapse

Instance Method Details

#add_subtitle(image, subtitle, dimensions) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/storyboard/generators/sub.rb', line 14

def add_subtitle(image, subtitle, dimensions)
  offset = 0
  subtitle.lines.reverse.each_with_index {|caption,i|
    escaped = caption.gsub("'", "\\\\'")
    font_size = 30
    text_width = dimensions[0] + 1
    while(text_width > (dimensions[0] * 0.9))
      font_size -= 1
      text_width = @@size_canvas.width_of(caption.encode!("utf-8"), :size => font_size)
    end

    font = Storyboard.needs_KFhimaji ? "#{Storyboard.path}/fonts/KFhimaji.otf" : "#{Storyboard.path}/fonts/DejaVuSans.ttf"

    write_mvg(offset,escaped, 0)
    image.combine_options do |c|
      c.font font
      c.fill "#333333"
      c.strokewidth '1'
      c.stroke '#000000'
      c.pointsize font_size.to_s
      c.gravity "south"
      c.draw "@#{File.join(@storyboard.options[:save_directory],'tmp.mvg')}"
    end


    write_mvg(offset,escaped, -2)
    #and the shadow
    image.combine_options do |c|
      c.font font
      c.fill "#ffffff"
      c.strokewidth '1'
      c.stroke 'transparent'
      c.pointsize font_size.to_s
      c.gravity "south"
      c.draw "@#{File.join(@storyboard.options[:save_directory],'tmp.mvg')}"
    end

    offset += (@@size_canvas.height_of(caption.encode!("utf-8"), :size => font_size)).ceil
  }
end

#write_mvg(offset, line, nudge = 0) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/storyboard/generators/sub.rb', line 6

def write_mvg(offset, line, nudge=0)
  out = File.open(File.join(@storyboard.options[:save_directory], 'tmp.mvg'), 'wt', encoding: 'UTF-8')
  out.print("text #{(0+nudge).to_s}, #{(offset+nudge).to_s} '")
  out.print line
  out.print "'"
  out.close
end