Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebVTT writer: Add support for ruby #434

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/main/python/ttconv/vtt/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,38 @@ def __init__(self, config: VTTWriterConfiguration):
)

def process_inline_element(self, element: model.ContentElement, begin: Fraction, end: Optional[Fraction]):
"""Converts inline element (span and br) to VTT content"""
"""Converts inline element (ruby, rb, rp, rt, rbc, rtc, span and br) to VTT content"""

if isinstance(element, model.Ruby):
self._paragraphs[-1].append_text('<ruby>')
rtc = False
for elem in list(element):
if isinstance(elem, model.Rtc):
if rtc: continue
rtc = True
self.process_inline_element(elem, begin, end)
self._paragraphs[-1].append_text('</ruby>')

if isinstance(element, model.Rbc):
for elem in list(element):
self.process_inline_element(elem, begin, end)

if isinstance(element, model.Rtc):
for elem in list(element):
self.process_inline_element(elem, begin, end)

if isinstance(element, model.Rb):
for elem in list(element):
self.process_inline_element(elem, begin, end)

if isinstance(element, model.Rp):
pass

if isinstance(element, model.Rt):
self._paragraphs[-1].append_text('<rt>')
for elem in list(element):
self.process_inline_element(elem, begin, end)
self._paragraphs[-1].append_text('</rt>')

if isinstance(element, model.Span):
is_bold = style.is_element_bold(element)
Expand Down
47 changes: 47 additions & 0 deletions src/test/python/test_vtt_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,53 @@ def test_position(self):
vtt_from_model = vtt_writer.from_model(model, config)
self.assertEqual(expected_vtt, vtt_from_model)

def test_ruby(self):
ttml_doc_str = """<?xml version="1.0" encoding="UTF-8"?>
<tt xml:lang="en-US" xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" ttp:frameRate="24" ttp:frameRateMultiplier="1000 1001" ttp:profile="http://www.w3.org/ns/ttml/profile/imsc1/text" ttp:timeBase="media">
<head>
<layout>
<region tts:displayAlign="center" tts:textAlign="center" tts:backgroundColor="black" xml:id="r1" tts:position="center center" tts:extent="40% 40%"/>
</layout>
</head>
<body>
<div region="r1">
<p begin="0s" end="1s"><span tts:ruby="container">
<span tts:ruby="base">base</span><span tts:ruby="text">text</span></span>
</p>
<p begin="1s" end="2s">
<span tts:ruby="container"><span tts:ruby="base">base</span><span tts:ruby="delimiter">(</span><span tts:ruby="text">text</span><span tts:ruby="delimiter">)</span></span>
</p>
<p begin="2s" end="3s">
<span tts:ruby="container">
<span tts:ruby="baseContainer"><span tts:ruby="base">base</span></span>
<span tts:ruby="textContainer" tts:rubyPosition="before"><span tts:ruby="text">text1</span></span>
<span tts:ruby="textContainer" tts:rubyPosition="after"><span tts:ruby="text">text2</span></span>
</span>
</p>
</div>
</body>
</tt>"""

expected_vtt="""WEBVTT

1
00:00:00.000 --> 00:00:01.000
<ruby>base<rt>text</rt></ruby>

2
00:00:01.000 --> 00:00:02.000
<ruby>base<rt>text</rt></ruby>

3
00:00:02.000 --> 00:00:03.000
<ruby>base<rt>text1</rt></ruby>
"""

model = imsc_reader.to_model(et.ElementTree(et.fromstring(ttml_doc_str)))
config = VTTWriterConfiguration()
vtt_from_model = vtt_writer.from_model(model, config)
self.assertEqual(expected_vtt, vtt_from_model)

def test_align(self):
ttml_doc_str = """<?xml version="1.0" encoding="UTF-8"?>
<tt xml:lang="en-US" xmlns="http://www.w3.org/ns/ttml" xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" ttp:frameRate="24" ttp:frameRateMultiplier="1000 1001" ttp:profile="http://www.w3.org/ns/ttml/profile/imsc1/text" ttp:timeBase="media">
Expand Down
Loading