Class: Ass::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/mpv_ass/text.rb

Overview

Represents an ASS text containing styled fragments with layout tags.

Constant Summary collapse

ALIGN =
{
  top_left:    7, top:    8, top_right:    9,
  left:        4, center: 5, right:        6,
  bottom_left: 1, bottom: 2, bottom_right: 3,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(*spans) ⇒ Text

Creates an ASS::Text.

Parameters:

  • spans (Array<Ass::Span, String>)

    initial text fragments



16
17
18
19
20
# File 'lib/mpv_ass/text.rb', line 16

def initialize(*spans)
  @tags = {}
  @spans = []
  add(*spans)
end

Instance Method Details

#add(*spans) ⇒ self

Adds new fragments to the current text.

Parameters:

  • spans (Array<Ass::Span, String>)

    text fragments to append

Returns:

  • (self)


25
26
27
28
# File 'lib/mpv_ass/text.rb', line 25

def add(*spans)
  spans.each{ |span| @spans << (span.is_a?(Span) ? span : Span.new(span)) }
  self
end

#align(value) ⇒ self

Sets the alignment anchor of the text.

Parameters:

  • value (Integer, Symbol)

    1..9 number or symbolic alignment name

Returns:

  • (self)


56
57
58
59
# File 'lib/mpv_ass/text.rb', line 56

def align(value)
  @tags["an"] = value.is_a?(Symbol) ? ALIGN.fetch(value) : value.to_i
  self
end

#bareself

Removes all layout tags.

Returns:

  • (self)


63
64
65
66
# File 'lib/mpv_ass/text.rb', line 63

def bare
  @tags.clear
  self
end

#getArray<Ass::Span>

Gets the current text fragments.

Returns:



40
41
42
# File 'lib/mpv_ass/text.rb', line 40

def get()
  @spans.dup.freeze
end

#position(x, y) ⇒ self

Sets the absolute position of the text.

Parameters:

  • x (Numeric)

    horizontal coordinate

  • y (Numeric)

    vertical coordinate

Returns:

  • (self)


48
49
50
51
# File 'lib/mpv_ass/text.rb', line 48

def position(x, y)
  @tags["pos"] = x.round(2), y.round(2)
  self
end

#set(*spans) ⇒ self

Replaces the current text fragments.

Parameters:

  • spans (Array<Ass::Span, String>)

    new text fragments

Returns:

  • (self)


33
34
35
36
# File 'lib/mpv_ass/text.rb', line 33

def set(*spans)
  @spans.clear
  add(*spans)
end

#to_aArray<String>

Returns the plain text content of each fragment.

Returns:

  • (Array<String>)

    plain text fragments



81
82
83
# File 'lib/mpv_ass/text.rb', line 81

def to_a
  @spans.map(&:to_s)
end

#to_sString

Returns the full plain text content.

Returns:

  • (String)

    concatenated plain text



87
88
89
# File 'lib/mpv_ass/text.rb', line 87

def to_s
  to_a.join
end

#to_scriptString

Converts the text to ASS syntax.

Returns:

  • (String)

    ASS text representation



70
71
72
73
74
75
76
77
# File 'lib/mpv_ass/text.rb', line 70

def to_script
  tags = @tags.map do |(key, value)|
    value = "(#{value.join(",")})" if value.is_a?(Array)
    "\\#{key}#{value}"
  end
  head = "{#{tags.join}}" unless tags.empty?
  "#{head}#{@spans.map(&:to_script).join}"
end