ladybird/Libraries/LibWeb/WebVTT/VTTCue.cpp
2025-08-13 23:05:50 +02:00

206 lines
7.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2024-2025, Jamie Mansfield <jmansfield@cadixdev.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebVTT/VTTCue.h>
namespace Web::WebVTT {
GC_DEFINE_ALLOCATOR(VTTCue);
// https://w3c.github.io/webvtt/#dom-vttcue-vttcue
WebIDL::ExceptionOr<GC::Ref<VTTCue>> VTTCue::construct_impl(JS::Realm& realm, double start_time, double end_time, String const& text)
{
// 1. Create a new WebVTT cue. Let cue be that WebVTT cue.
auto cue = realm.create<VTTCue>(realm, nullptr);
// 2. Let cues text track cue start time be the value of the startTime argument.
cue->m_start_time = start_time;
// 3. If the value of the endTime argument is negative Infinity or a Not-a-Number (NaN) value, then throw a TypeError exception.
// Otherwise, let cues text track cue end time be the value of the endTime argument.
if (end_time == -AK::Infinity<double> || isnan(end_time))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "End time is negative infinity or NaN"_string };
cue->m_end_time = end_time;
// 4. Let cues cue text be the value of the text argument, and let the rules for extracting the chapter title be the WebVTT rules
// for extracting the chapter title.
cue->m_text = text;
// FIXME: let the rules for extracting the chapter title be the WebVTT rules for extracting the chapter title.
// 5. Let cues text track cue identifier be the empty string.
cue->m_identifier = ""_string;
// 6. Let cues text track cue pause-on-exit flag be false.
cue->m_pause_on_exit = false;
// 7. Let cues WebVTT cue region be null.
cue->m_region = nullptr;
// 8. Let cues WebVTT cue writing direction be horizontal.
cue->m_writing_direction = WritingDirection::Horizontal;
// 9. Let cues WebVTT cue snap-to-lines flag be true.
cue->m_snap_to_lines = true;
// 10. Let cues WebVTT cue line be auto.
cue->m_line = Bindings::AutoKeyword::Auto;
// 11. Let cues WebVTT cue line alignment be start alignment.
cue->m_line_alignment = Bindings::LineAlignSetting::Start;
// 12. Let cues WebVTT cue position be auto.
cue->m_position = Bindings::AutoKeyword::Auto;
// 13. Let cues WebVTT cue position alignment be auto.
cue->m_position_alignment = Bindings::PositionAlignSetting::Auto;
// 14. Let cues WebVTT cue size be 100.
cue->m_size = 100;
// 15. Let cues WebVTT cue text alignment be center alignment.
cue->m_text_alignment = Bindings::AlignSetting::Center;
// 16. Return the VTTCue object representing cue.
return cue;
}
VTTCue::VTTCue(JS::Realm& realm, GC::Ptr<HTML::TextTrack> track)
: HTML::TextTrackCue(realm, track)
{
}
void VTTCue::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(VTTCue);
Base::initialize(realm);
}
void VTTCue::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_region);
}
// https://w3c.github.io/webvtt/#dom-vttcue-vertical
Bindings::DirectionSetting VTTCue::vertical() const
{
switch (m_writing_direction) {
case WritingDirection::Horizontal:
return Bindings::DirectionSetting::Empty;
case WritingDirection::VerticalGrowingLeft:
return Bindings::DirectionSetting::Rl;
case WritingDirection::VerticalGrowingRight:
return Bindings::DirectionSetting::Lr;
}
VERIFY_NOT_REACHED();
}
// https://w3c.github.io/webvtt/#dom-vttcue-vertical
void VTTCue::set_vertical(Bindings::DirectionSetting vertical)
{
switch (vertical) {
case Bindings::DirectionSetting::Empty:
m_writing_direction = WritingDirection::Horizontal;
break;
case Bindings::DirectionSetting::Rl:
m_writing_direction = WritingDirection::VerticalGrowingLeft;
break;
case Bindings::DirectionSetting::Lr:
m_writing_direction = WritingDirection::VerticalGrowingRight;
break;
}
}
// https://w3c.github.io/webvtt/#cue-computed-line
double VTTCue::computed_line()
{
// 1. If the line is numeric, the WebVTT cue snap-to-lines flag of the WebVTT cue is false, and the line is negative
// or greater than 100, then return 100 and abort these steps.
if (m_line.has<double>() && !m_snap_to_lines && (m_line.get<double>() < 0 || m_line.get<double>() > 100))
return 100;
// 2. If the line is numeric, return the value of the WebVTT cue line and abort these steps. (Either the WebVTT cue
// snap-to-lines flag is true, so any value, not just those in the range 0..100, is valid, or the value is in the
// range 0..100 and is thus valid regardless of the value of that flag.)
if (m_line.has<double>())
return m_line.get<double>();
// 3. If the WebVTT cue snap-to-lines flag of the WebVTT cue is false, return the value 100 and abort these steps.
// (The line is the special value auto.)
if (!m_snap_to_lines)
return 100;
// FIXME: 4. Let cue be the WebVTT cue.
// FIXME: 5. If cue is not in a list of cues of a text track, or if that text track is not in the list of text tracks of
// a media element, return 1 and abort these steps.
// FIXME: 6. Let track be the text track whose list of cues the cue is in.
// FIXME: 7. Let n be the number of text tracks whose text track mode is showing and that are in the media elements list
// of text tracks before track.
auto n = 0;
// 8. Increment n by one.
n++;
// 9. Negate n.
n = -n;
// 10. Return n.
dbgln("FIXME: Stubbed VTTCue.computed_line()");
return n;
}
// https://w3c.github.io/webvtt/#cue-computed-position
double VTTCue::computed_position()
{
// 1. If the position is numeric between 0 and 100, then return the value of the position and abort these steps.
// (Otherwise, the position is the special value auto.)
if (m_position.has<double>() && m_position.get<double>() >= 0 && m_position.get<double>() <= 100)
return m_position.get<double>();
// 2. If the cue text alignment is left, return 0 and abort these steps.
if (m_text_alignment == Bindings::AlignSetting::Left)
return 0;
// 3. If the cue text alignment is right, return 100 and abort these steps.
if (m_text_alignment == Bindings::AlignSetting::Right)
return 100;
// 4. Otherwise, return 50 and abort these steps.
return 50;
}
// https://w3c.github.io/webvtt/#cue-computed-position-alignment
Bindings::PositionAlignSetting VTTCue::computed_position_alignment()
{
// 1. If the WebVTT cue position alignment is not auto, then return the value of the WebVTT cue position alignment and abort these
// steps.
if (m_position_alignment != Bindings::PositionAlignSetting::Auto)
return m_position_alignment;
// 2. If the WebVTT cue text alignment is left, return line-left and abort these steps.
if (m_text_alignment == Bindings::AlignSetting::Left)
return Bindings::PositionAlignSetting::LineLeft;
// 3. If the WebVTT cue text alignment is right, return line-right and abort these steps.
if (m_text_alignment == Bindings::AlignSetting::Right)
return Bindings::PositionAlignSetting::LineRight;
// FIXME: 4. If the WebVTT cue text alignment is start, return line-left if the base direction of the cue text is left-to-right, line-right
// otherwise.
// FIXME: 5. If the WebVTT cue text alignment is end, return line-right if the base direction of the cue text is left-to-right, line-left
// otherwise.
// 6. Otherwise, return center.
return Bindings::PositionAlignSetting::Center;
}
}