VTTCue.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/HTML/Window.h>
  8. #include <LibWeb/WebVTT/VTTCue.h>
  9. namespace Web::WebVTT {
  10. GC_DEFINE_ALLOCATOR(VTTCue);
  11. // https://w3c.github.io/webvtt/#dom-vttcue-vttcue
  12. WebIDL::ExceptionOr<GC::Ref<VTTCue>> VTTCue::construct_impl(JS::Realm& realm, double start_time, double end_time, String const& text)
  13. {
  14. // 1. Create a new WebVTT cue. Let cue be that WebVTT cue.
  15. auto cue = realm.create<VTTCue>(realm, nullptr);
  16. // 2. Let cue’s text track cue start time be the value of the startTime argument.
  17. cue->m_start_time = start_time;
  18. // 3. If the value of the endTime argument is negative Infinity or a Not-a-Number (NaN) value, then throw a TypeError exception.
  19. // Otherwise, let cue’s text track cue end time be the value of the endTime argument.
  20. if (end_time == -AK::Infinity<double> || isnan(end_time))
  21. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "End time is negative infinity or NaN"_string };
  22. cue->m_end_time = end_time;
  23. // 4. Let cue’s cue text be the value of the text argument, and let the rules for extracting the chapter title be the WebVTT rules
  24. // for extracting the chapter title.
  25. cue->m_text = text;
  26. // FIXME: let the rules for extracting the chapter title be the WebVTT rules for extracting the chapter title.
  27. // 5. Let cue’s text track cue identifier be the empty string.
  28. cue->m_identifier = ""_string;
  29. // 6. Let cue’s text track cue pause-on-exit flag be false.
  30. cue->m_pause_on_exit = false;
  31. // 7. Let cue’s WebVTT cue region be null.
  32. cue->m_region = nullptr;
  33. // 8. Let cue’s WebVTT cue writing direction be horizontal.
  34. cue->m_writing_direction = WritingDirection::Horizontal;
  35. // 9. Let cue’s WebVTT cue snap-to-lines flag be true.
  36. cue->m_snap_to_lines = true;
  37. // FIXME: 10. Let cue’s WebVTT cue line be auto.
  38. // 11. Let cue’s WebVTT cue line alignment be start alignment.
  39. cue->m_line_alignment = Bindings::LineAlignSetting::Start;
  40. // FIXME: 12. Let cue’s WebVTT cue position be auto.
  41. // 13. Let cue’s WebVTT cue position alignment be auto.
  42. cue->m_position_alignment = Bindings::PositionAlignSetting::Auto;
  43. // 14. Let cue’s WebVTT cue size be 100.
  44. cue->m_size = 100;
  45. // 15. Let cue’s WebVTT cue text alignment be center alignment.
  46. cue->m_text_alignment = Bindings::AlignSetting::Center;
  47. // 16. Return the VTTCue object representing cue.
  48. return cue;
  49. }
  50. VTTCue::VTTCue(JS::Realm& realm, GC::Ptr<HTML::TextTrack> track)
  51. : HTML::TextTrackCue(realm, track)
  52. {
  53. }
  54. void VTTCue::initialize(JS::Realm& realm)
  55. {
  56. Base::initialize(realm);
  57. WEB_SET_PROTOTYPE_FOR_INTERFACE(VTTCue);
  58. }
  59. void VTTCue::visit_edges(Visitor& visitor)
  60. {
  61. Base::visit_edges(visitor);
  62. visitor.visit(m_region);
  63. }
  64. // https://w3c.github.io/webvtt/#dom-vttcue-vertical
  65. Bindings::DirectionSetting VTTCue::vertical() const
  66. {
  67. switch (m_writing_direction) {
  68. case WritingDirection::Horizontal:
  69. return Bindings::DirectionSetting::Empty;
  70. case WritingDirection::VerticalGrowingLeft:
  71. return Bindings::DirectionSetting::Rl;
  72. case WritingDirection::VerticalGrowingRight:
  73. return Bindings::DirectionSetting::Lr;
  74. }
  75. VERIFY_NOT_REACHED();
  76. }
  77. // https://w3c.github.io/webvtt/#dom-vttcue-vertical
  78. void VTTCue::set_vertical(Bindings::DirectionSetting vertical)
  79. {
  80. switch (vertical) {
  81. case Bindings::DirectionSetting::Empty:
  82. m_writing_direction = WritingDirection::Horizontal;
  83. break;
  84. case Bindings::DirectionSetting::Rl:
  85. m_writing_direction = WritingDirection::VerticalGrowingLeft;
  86. break;
  87. case Bindings::DirectionSetting::Lr:
  88. m_writing_direction = WritingDirection::VerticalGrowingRight;
  89. break;
  90. }
  91. }
  92. // https://w3c.github.io/webvtt/#cue-computed-position-alignment
  93. Bindings::PositionAlignSetting VTTCue::computed_position_alignment()
  94. {
  95. // 1. If the WebVTT cue position alignment is not auto, then return the value of the WebVTT cue position alignment and abort these
  96. // steps.
  97. if (m_position_alignment != Bindings::PositionAlignSetting::Auto)
  98. return m_position_alignment;
  99. // 2. If the WebVTT cue text alignment is left, return line-left and abort these steps.
  100. if (m_text_alignment == Bindings::AlignSetting::Left)
  101. return Bindings::PositionAlignSetting::LineLeft;
  102. // 3. If the WebVTT cue text alignment is right, return line-right and abort these steps.
  103. if (m_text_alignment == Bindings::AlignSetting::Right)
  104. return Bindings::PositionAlignSetting::LineRight;
  105. // 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
  106. // otherwise.
  107. // 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
  108. // otherwise.
  109. // 6. Otherwise, return center.
  110. return Bindings::PositionAlignSetting::Center;
  111. }
  112. }