Class: ASSStyleParams

Inherits:
Object
  • Object
show all
Defined in:
lib/vtt2ass/ASSStyleParams.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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vtt2ass/ASSStyleParams.rb', line 10

def initialize(params, width, height)
    (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
    createAlignment()
    createHorizontalMargin(width)
    createVerticalMargin(height)
end

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



4
5
6
# File 'lib/vtt2ass/ASSStyleParams.rb', line 4

def align
  @align
end

#alignmentObject (readonly)

Returns the value of attribute alignment.



4
5
6
# File 'lib/vtt2ass/ASSStyleParams.rb', line 4

def alignment
  @alignment
end

#horizontal_marginObject (readonly)

Returns the value of attribute horizontal_margin.



4
5
6
# File 'lib/vtt2ass/ASSStyleParams.rb', line 4

def horizontal_margin
  @horizontal_margin
end

#vertical_marginObject (readonly)

Returns the value of attribute vertical_margin.



4
5
6
# File 'lib/vtt2ass/ASSStyleParams.rb', line 4

def vertical_margin
  @vertical_margin
end

Instance Method Details

#createAlignmentObject

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vtt2ass/ASSStyleParams.rb', line 30

def createAlignment()
    if (defined?(@line) and not defined?(@position)) then
        if (defined?(@align)) then
            case @align
            when 'left', 'start'
                @alignment = @line >= 50 ? 1 : 7
            when 'right', 'end'
                @alignment = @line >= 50 ? 3 : 9
            when 'center', 'middle'
                @alignment = @line >= 50 ? 2 : 8
            end
        else
            @alignment = @line >= 50 ? 2 : 8 # If position is higher than 50% align to bottom center, else align to top center
        end
    elsif (defined?(@line) and defined?(@position)) then
        @alignment = 1
    else
        case @align
        when 'left', 'start'
            @alignment = 1
        when 'right', 'end'
            @alignment = 3
        when 'center', 'middle'
            @alignment = 2
        else
            @alignment = 2
        end
    end
end

#createHorizontalMargin(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.



63
64
65
66
67
68
69
70
# File 'lib/vtt2ass/ASSStyleParams.rb', line 63

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

#createVerticalMargin(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.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vtt2ass/ASSStyleParams.rb', line 75

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