Class: ASSStyleParams

Inherits:
Object
  • Object
show all
Defined in:
lib/vtt2ass/ass_style_params.rb

Overview

This class defines the ASS style parameters from VTT cue settings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, width, height) ⇒ ASSStyleParams

Creates an instance of ASSStyleParams It takes VTT style arguments and assign them to their respectful instance variable. It calls methods to create ASS values from the VTT cue settings.



12
13
14
15
16
17
18
# File 'lib/vtt2ass/ass_style_params.rb', line 12

def initialize(params, width, height)
  @align = nil
  split_params(params)
  create_alignment
  create_horizontal_margin(width)
  create_vertical_margin(height)
end

Instance Attribute Details

#alignObject

Returns the value of attribute align.



6
7
8
# File 'lib/vtt2ass/ass_style_params.rb', line 6

def align
  @align
end

#alignmentObject

Returns the value of attribute alignment.



6
7
8
# File 'lib/vtt2ass/ass_style_params.rb', line 6

def alignment
  @alignment
end

#horizontal_marginObject

Returns the value of attribute horizontal_margin.



6
7
8
# File 'lib/vtt2ass/ass_style_params.rb', line 6

def horizontal_margin
  @horizontal_margin
end

#vertical_marginObject

Returns the value of attribute vertical_margin.



6
7
8
# File 'lib/vtt2ass/ass_style_params.rb', line 6

def vertical_margin
  @vertical_margin
end

Instance Method Details

#create_alignmentObject

This method decides the alignement value in a 9 position grid based of the values in cue settings “align” and “line”.



37
38
39
40
41
42
43
44
45
46
# File 'lib/vtt2ass/ass_style_params.rb', line 37

def create_alignment
  @alignment =
    if defined?(@line) && !defined?(@position)
      find_alignment(@align)
    elsif defined?(@line) && defined?(@position)
      1 # bottom left
    else
      find_default_alignment(@align)
    end
end

#create_horizontal_margin(width) ⇒ Object

This method calculates the horizontal margin in px between the alignement position and and the content displayed by using the “position” cue setting.



83
84
85
86
87
88
89
90
91
# File 'lib/vtt2ass/ass_style_params.rb', line 83

def create_horizontal_margin(width)
  steps = (width / 100).to_i
  @horizontal_margin =
    if defined?(@position)
      @position * steps
    else
      0
    end
end

#create_vertical_margin(height) ⇒ Object

This method calculates the vertical margin in px between the alignement position and and the content displayed by using the “line” cue setting.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vtt2ass/ass_style_params.rb', line 96

def create_vertical_margin(height)
  steps = (height / 100).to_i
  @vertical_margin =
    if defined?(@line)
      if @alignment == 1
        (100 - @line) * steps
      else
        @line >= 50 ? (100 - @line) * steps : @line * steps
      end
    else
      50
    end
end

#find_alignment(align) ⇒ Object

This method returns alignment when “line” value is specified but not “position”



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vtt2ass/ass_style_params.rb', line 50

def find_alignment(align)
  if align.nil?
    # If position is higher than 50% align to bottom center, else align to top center
    @line >= 50 ? 2 : 8
  else
    case align
    when 'left', 'start'
      @line >= 50 ? 1 : 7
    when 'right', 'end'
      @line >= 50 ? 3 : 9
    when 'center', 'middle'
      @line >= 50 ? 2 : 8
    end
  end
end

#find_default_alignment(align) ⇒ Object

This method returns alignment when “line” and “position” values are not specified



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vtt2ass/ass_style_params.rb', line 68

def find_default_alignment(align)
  case align
  when 'left', 'start'
    1
  when 'right', 'end'
    3
  # when 'center', 'middle'
  else
    2
  end
end

#split_params(params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vtt2ass/ass_style_params.rb', line 20

def split_params(params)
  (params.split.map { |p| p.split(':') }).each do |p|
    case p[0]
    when 'position'
      @position = p[1].gsub(/%/, '').to_i
    when 'line'
      @line = p[1].gsub(/%/, '').to_i
      @line = @line == -1 ? 100 : @line
    when 'align'
      @align = p[1].chomp
    end
  end
end