Class: Ass::Span

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

Overview

Represents a styled ASS text fragment.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil) ⇒ Span

Creates an ASS::Span text fragment.

Parameters:

  • content (String) (defaults to: nil)

    plain text content



15
16
17
18
# File 'lib/mpv_ass/span.rb', line 15

def initialize(content=nil)
  @content = content
  @tags = {}
end

Instance Attribute Details

#contentString Also known as: to_s

Gets or changes the plain text fragment content.

Returns:

  • (String)

    plain text content



10
11
12
# File 'lib/mpv_ass/span.rb', line 10

def content
  @content
end

Instance Method Details

#bareself

Removes all style tags.

Returns:

  • (self)


104
105
106
107
# File 'lib/mpv_ass/span.rb', line 104

def bare
  @tags.clear
  self
end

#blur(strength) ⇒ self

Sets the blur strength.

Parameters:

  • strength (Numeric)

    blur strength value

Returns:

  • (self)


97
98
99
100
# File 'lib/mpv_ass/span.rb', line 97

def blur(strength)
  @tags["blur"] = strength.to_f
  self
end

#bold(value = true) ⇒ self

Sets bold text rendering.

Parameters:

  • value (Boolean) (defaults to: true)

    whether bold styling is enabled

Returns:

  • (self)


31
32
33
34
# File 'lib/mpv_ass/span.rb', line 31

def bold(value=true)
  @tags["b"] = value ? 1 : 0
  self
end

#color(value) ⇒ self

Sets the font color.

Parameters:

  • value (Ass::Color, Array<Integer>, Symbol)

    font color

Returns:

  • (self)


71
72
73
74
# File 'lib/mpv_ass/span.rb', line 71

def color(value)
  @tags["1c"] = value.is_a?(Color) ? value : Color.new(value)
  self
end

#font(name) ⇒ self

Sets the font name.

Parameters:

  • name (String)

    font family name

Returns:

  • (self)


23
24
25
26
# File 'lib/mpv_ass/span.rb', line 23

def font(name)
  @tags["fn"] = name.to_s
  self
end

#italic(value = true) ⇒ self

Sets italic text rendering.

Parameters:

  • value (Boolean) (defaults to: true)

    whether italic styling is enabled

Returns:

  • (self)


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

def italic(value=true)
  @tags["i"] = value ? 1 : 0
  self
end

#outline(size, color) ⇒ self

Sets the outline thickness and color.

Parameters:

  • size (Integer)

    outline thickness value

  • color (Ass::Color, Array<Integer>, Symbol)

    outline color value

Returns:

  • (self)


80
81
82
83
84
# File 'lib/mpv_ass/span.rb', line 80

def outline(size, color)
  @tags["3c"] = color.is_a?(Color) ? color : Color.new(color)
  @tags["bord"] = size.to_i
  self
end

#shadow(size) ⇒ self

Sets the shadow size.

Parameters:

  • size (Integer)

    shadow size value

Returns:

  • (self)


89
90
91
92
# File 'lib/mpv_ass/span.rb', line 89

def shadow(size)
  @tags["shad"] = size.to_i
  self
end

#size(value) ⇒ self

Sets the font size.

Parameters:

  • value (Integer)

    font size

Returns:

  • (self)


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

def size(value)
  @tags["fs"] = value.to_i
  self
end

#strike(value = true) ⇒ self

Sets strikethrough text rendering.

Parameters:

  • value (Boolean) (defaults to: true)

    whether strikethrough styling is enabled

Returns:

  • (self)


55
56
57
58
# File 'lib/mpv_ass/span.rb', line 55

def strike(value=true)
  @tags["s"] = value ? 1 : 0
  self
end

#to_scriptString

Converts the text fragment to ASS syntax.

Returns:

  • (String)

    ASS text fragment representation



111
112
113
114
115
116
117
118
119
# File 'lib/mpv_ass/span.rb', line 111

def to_script
  tags = @tags.map do |(key, value)|
    value = value.to_script if value.is_a?(Color)
    "\\#{key}#{value}"
  end
  head, tail = "{#{tags.join}}", "{\\r}" unless tags.empty?
  content = @content.to_s.gsub("\\", "\\\\").gsub("{", "\\{").gsub("}", "\\}").gsub("\n", "\\N")
  "#{head}#{content}#{tail}"
end

#underline(value = true) ⇒ self

Sets underline text rendering.

Parameters:

  • value (Boolean) (defaults to: true)

    whether underline styling is enabled

Returns:

  • (self)


47
48
49
50
# File 'lib/mpv_ass/span.rb', line 47

def underline(value=true)
  @tags["u"] = value ? 1 : 0
  self
end