Class: ASSFile

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

Overview

This class defines an ASS subtitle file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, width, height, css_file_path = nil) ⇒ ASSFile

Creates a new ASSFile instance and assigns the default values of instance variables.



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
# File 'lib/vtt2ass/ass_file.rb', line 15

def initialize(title, width, height, css_file_path = nil)
    @width = width
    @height = height
    if not css_file_path.nil? then
        @css_file = CSSFile.new(css_file_path)
    end
    @header = [
        '[Script Info]',
        "Title: #{title}",
        'ScriptType: v4.00+',
        'Collisions: Normal',
        'PlayDepth: 0',
        "PlayResX: #{@width}",
        "PlayResY: #{@height}",
        'WrapStyle: 0',
        'ScaledBorderAndShadow: yes',
        '',
        '[V4+ Styles]',
        'Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding'
    ]
    @events = [
        '',
        '[Events]',
        'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text'
    ]
    @ass_styles = []
    @ass_lines = []
end

Instance Attribute Details

#ass_linesObject

Returns the value of attribute ass_lines.



11
12
13
# File 'lib/vtt2ass/ass_file.rb', line 11

def ass_lines
  @ass_lines
end

#ass_stylesObject

Returns the value of attribute ass_styles.



11
12
13
# File 'lib/vtt2ass/ass_file.rb', line 11

def ass_styles
  @ass_styles
end

#heightObject (readonly)

Returns the value of attribute height.



10
11
12
# File 'lib/vtt2ass/ass_file.rb', line 10

def height
  @height
end

#titleObject (readonly)

Returns the value of attribute title.



10
11
12
# File 'lib/vtt2ass/ass_file.rb', line 10

def title
  @title
end

#widthObject (readonly)

Returns the value of attribute width.



10
11
12
# File 'lib/vtt2ass/ass_file.rb', line 10

def width
  @width
end

Instance Method Details

#convert_vtt_to_ass(vtt_file, font_family, font_size, line_offset = 0) ⇒ Object

This method receives a VTTFile object and font arguments creates new ASSLine with the params of each VTTLine. All those ASSLine are stored in an array. It also creates an array of ASSStyle that will be used in the ASS style list.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vtt2ass/ass_file.rb', line 48

def convert_vtt_to_ass(vtt_file, font_family, font_size, line_offset = 0)
    fs = font_size
    vtt_file.lines.each do |line|
        font_color = '&H00FFFFFF'
        is_italic = false
        is_bold = false
        @ass_lines.push(ASSLine.new(line.style, line.time_start, line.time_end, line.text))
        style_exists = false
        @ass_styles.each do |style|
            if (style.style_name.eql? line.style) then
                style_exists = true
                break
            end
        end
        if not style_exists then
            if defined?(@css_file) then
                css_rule = @css_file.find_rule(line.style)
                if not css_rule.nil? then
                    css_rule.properties.each do |property|
                        case property[:key]
                        when 'font-family'
                            font_family = property[:value].gsub('"', '').split(' ,').last
                        when 'font-size'
                            em_size = 1
                            #em_size = property[:value][0].eql? '.' ? "0#{property[:value]}" : property[:value]
                            if property[:value][0].eql? '.' then
                                em_size = "0#{property[:value]}".gsub('em', '').to_f
                            end
                            font_size = (fs * em_size).to_i
                        when 'color'
                            font_color = ASSStyle.convert_color(property[:value])
                        when 'font-weight'
                            if property[:value].eql? 'bold' then
                                is_bold = true
                            end
                        when 'font-style'
                            if property[:value].eql? 'italic' then
                                is_italic = true
                            end
                        end
                    end
                end
            end
            @ass_styles.push(ASSStyle.new(line.style, line.params, font_family, font_size, font_color, is_bold, is_italic, line_offset, @width, @height))
        end
    end
end

#to_sObject

This method concatenates the object data in the right order for a string output.



107
108
109
# File 'lib/vtt2ass/ass_file.rb', line 107

def to_s
    return @header + @ass_styles + @events + @ass_lines
end

#write_to_file(file_path) ⇒ Object

This method writes the content of the ASSFile object into a file path that is provided.



98
99
100
101
102
103
# File 'lib/vtt2ass/ass_file.rb', line 98

def write_to_file(file_path)
    File.open(file_path, 'w') do |line|
        line.print "\ufeff"
        line.puts self.to_s
    end
end