Class: Ass::Text
- Inherits:
-
Object
- Object
- Ass::Text
- 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
-
#add(*spans) ⇒ self
Adds new fragments to the current text.
-
#align(value) ⇒ self
Sets the alignment anchor of the text.
-
#bare ⇒ self
Removes all layout tags.
-
#get ⇒ Array<Ass::Span>
Gets the current text fragments.
-
#initialize(*spans) ⇒ Text
constructor
Creates an ASS::Text.
-
#position(x, y) ⇒ self
Sets the absolute position of the text.
-
#set(*spans) ⇒ self
Replaces the current text fragments.
-
#to_a ⇒ Array<String>
Returns the plain text content of each fragment.
-
#to_s ⇒ String
Returns the full plain text content.
-
#to_script ⇒ String
Converts the text to ASS syntax.
Constructor Details
#initialize(*spans) ⇒ Text
Creates an ASS::Text.
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.
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.
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 |
#bare ⇒ self
Removes all layout tags.
63 64 65 66 |
# File 'lib/mpv_ass/text.rb', line 63 def @tags.clear self end |
#get ⇒ Array<Ass::Span>
Gets the current text fragments.
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.
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.
33 34 35 36 |
# File 'lib/mpv_ass/text.rb', line 33 def set(*spans) @spans.clear add(*spans) end |
#to_a ⇒ Array<String>
Returns the plain text content of each fragment.
81 82 83 |
# File 'lib/mpv_ass/text.rb', line 81 def to_a @spans.map(&:to_s) end |
#to_s ⇒ String
Returns the full plain text content.
87 88 89 |
# File 'lib/mpv_ass/text.rb', line 87 def to_s to_a.join end |
#to_script ⇒ String
Converts the text to ASS syntax.
70 71 72 73 74 75 76 77 |
# File 'lib/mpv_ass/text.rb', line 70 def to_script = @tags.map do |(key, value)| value = "(#{value.join(",")})" if value.is_a?(Array) "\\#{key}#{value}" end head = "{#{.join}}" unless .empty? "#{head}#{@spans.map(&:to_script).join}" end |