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.



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 16

def initialize(title, width, height, css_file_path = nil)
  @width = width
  @height = height
  @css_file = nil
  @css_file = CSSFile.new(css_file_path) unless css_file_path.nil?
  @header = <<~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
  HEADER
  @events = <<~EVENTS

    [Events]
    Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
  EVENTS
  @ass_styles = []
  @ass_lines = []
end

Instance Attribute Details

#ass_linesObject

Returns the value of attribute ass_lines.



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

def ass_lines
  @ass_lines
end

#ass_stylesObject

Returns the value of attribute ass_styles.



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

def ass_styles
  @ass_styles
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#convert_vtt_to_ass(vtt_file, font_family, font_size, offset = { line: 0, caption: 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
# File 'lib/vtt2ass/ass_file.rb', line 48

def convert_vtt_to_ass(vtt_file, font_family, font_size, offset = { line: 0, caption: 0 }) # rubocop:disable Metrics/MethodLength
  fs = font_size
  vtt_file.lines.each do |line| # rubocop:disable Metrics/BlockLength
    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
        style_exists = true
        break
      end
    end
    next if style_exists

    unless @css_file.nil?
      css_rule = @css_file.find_rule(line.style)
      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 = "0#{property[:value]}".gsub('em', '').to_f if property[:value][0].eql? '.'
          font_size = (fs * em_size).to_i
        when 'color'
          font_color = ASSStyle.convert_color(property[:value])
        when 'font-weight'
          is_bold = true if property[:value].eql? 'bold'
        when 'font-style'
          is_italic = true if property[:value].eql? 'italic'
        end
      end
    end
    @ass_styles.push(
      ASSStyle.new(
        line.style, line.params,
        font_family, font_size, font_color, is_bold, is_italic,
        offset, @width, @height
      )
    )
  end
end

#to_sObject

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



104
105
106
# File 'lib/vtt2ass/ass_file.rb', line 104

def to_s
  "#{@header}#{@ass_styles.join("\n")}#{@events}#{@ass_lines.join("\n")}"
end

#write_to_file(file_path) ⇒ Object

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



95
96
97
98
99
100
# File 'lib/vtt2ass/ass_file.rb', line 95

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