Skip to content

Commit 6e0561e

Browse files
committed
ass: add support classes for text styling
1 parent 038ba40 commit 6e0561e

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

lib/ass/color.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module Ass
4+
# Represents a SubStationAlpha Color
5+
class Color
6+
{
7+
black: [0, 0, 0],
8+
white: [255, 255, 255],
9+
red: [255, 0, 0],
10+
green: [0, 255, 0],
11+
yellow: [0, 255, 255],
12+
}.each do |k, v|
13+
define_singleton_method(k) { new(*v) }
14+
end
15+
16+
# @param red [Integer] 0 to 255
17+
# @param green [Integer] 0 to 255
18+
# @param blue [Integer] 0 to 255
19+
def initialize(red, green, blue)
20+
@rgb = [red, green, blue]
21+
end
22+
23+
# @return [String] ASS script representation
24+
def to_script
25+
s = @rgb.map(&method(:to_hex)).join("")
26+
"&H#{s}&"
27+
end
28+
29+
private
30+
31+
def to_hex(component)
32+
component.to_s(16).upcase.rjust(2, "0")
33+
end
34+
end
35+
end

lib/ass/text.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
module Ass
4+
# Represents a SubStationAlpha styled String
5+
class Text
6+
def initialize(text)
7+
@text = text
8+
@style = {}
9+
end
10+
11+
# @param size [Integer]
12+
# @return [Text]
13+
def font_size(size)
14+
@style.merge!(fs: size)
15+
self
16+
end
17+
18+
# @param size [Integer]
19+
# @param color [Color]
20+
# @return [Text]
21+
def border(size, color)
22+
@style.merge!(bord: size, "3c": color.to_script)
23+
self
24+
end
25+
26+
# @param color [Color]
27+
# @return [Text]
28+
def color(color)
29+
@style.merge!("1c": color.to_script)
30+
self
31+
end
32+
33+
# @return [String] ASS script representation
34+
def to_script
35+
[@style.map { |k, v| "{\\#{k}#{v}}" }.join, @text].join
36+
end
37+
end
38+
end

lib/mpv.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
module MPV
66
end
77

8+
require_relative "ass/color"
9+
require_relative "ass/text"
10+
811
require_relative "mpv/exceptions"
912
require_relative "mpv/utils"
1013
require_relative "mpv/client"

spec/ass_color_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
describe Ass::Color do
6+
it "converts white" do
7+
expect(Ass::Color.white.to_script).to eql("&HFFFFFF&")
8+
end
9+
end

spec/ass_text_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
describe Ass::Text do
6+
it "deals with styled text" do
7+
colors = Ass::Color
8+
s = Ass::Text.new("適当")
9+
s = s.border(1, colors.black).color(colors.white).font_size(40)
10+
expect(s.to_script).to eql("{\\bord1}{\\3c&H000000&}{\\1c&HFFFFFF&}{\\fs40}適当")
11+
end
12+
end

0 commit comments

Comments
 (0)