VTTRegion.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/VTTRegion.h>
  9. namespace Web::WebVTT {
  10. GC_DEFINE_ALLOCATOR(VTTRegion);
  11. // https://w3c.github.io/webvtt/#dom-vttregion-vttregion
  12. WebIDL::ExceptionOr<GC::Ref<VTTRegion>> VTTRegion::construct_impl(JS::Realm& realm)
  13. {
  14. // 1. Create a new WebVTT region. Let region be that WebVTT region.
  15. auto region = realm.create<VTTRegion>(realm);
  16. // 2. Let region’s WebVTT region identifier be the empty string.
  17. region->m_identifier = ""_string;
  18. // 3. Let region’s WebVTT region width be 100.
  19. region->m_width = 100;
  20. // 4. Let region’s WebVTT region lines be 3.
  21. region->m_lines = 3;
  22. // 5. Let region’s text track region regionAnchorX be 0.
  23. region->m_anchor_x = 0;
  24. // 6. Let region’s text track region regionAnchorY be 100.
  25. region->m_anchor_y = 100;
  26. // 7. Let region’s text track region viewportAnchorX be 0.
  27. region->m_viewport_anchor_x = 0;
  28. // 8. Let region’s text track region viewportAnchorY be 100.
  29. region->m_viewport_anchor_y = 100;
  30. // 9. Let region’s WebVTT region scroll be the empty string.
  31. region->m_scroll_setting = Bindings::ScrollSetting::Empty;
  32. // 10. Return the VTTRegion object representing region.
  33. return region;
  34. }
  35. VTTRegion::VTTRegion(JS::Realm& realm)
  36. : PlatformObject(realm)
  37. {
  38. }
  39. void VTTRegion::initialize(JS::Realm& realm)
  40. {
  41. Base::initialize(realm);
  42. WEB_SET_PROTOTYPE_FOR_INTERFACE(VTTRegion);
  43. }
  44. // https://w3c.github.io/webvtt/#dom-vttregion-width
  45. WebIDL::ExceptionOr<void> VTTRegion::set_width(double width)
  46. {
  47. // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown.
  48. if (width < 0 || width > 100)
  49. return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_string);
  50. // Otherwise, the WebVTT region width must be set to the new value.
  51. m_width = width;
  52. return {};
  53. }
  54. // https://w3c.github.io/webvtt/#dom-vttregion-regionanchorx
  55. WebIDL::ExceptionOr<void> VTTRegion::set_region_anchor_x(double region_anchor_x)
  56. {
  57. // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown.
  58. if (region_anchor_x < 0 || region_anchor_x > 100)
  59. return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_string);
  60. // Otherwise, the WebVTT region anchor X distance must be set to the new value.
  61. m_anchor_x = region_anchor_x;
  62. return {};
  63. }
  64. // https://w3c.github.io/webvtt/#dom-vttregion-regionanchory
  65. WebIDL::ExceptionOr<void> VTTRegion::set_region_anchor_y(double region_anchor_y)
  66. {
  67. // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown.
  68. if (region_anchor_y < 0 || region_anchor_y > 100)
  69. return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_string);
  70. // Otherwise, the WebVTT region anchor Y distance must be set to the new value.
  71. m_anchor_y = region_anchor_y;
  72. return {};
  73. }
  74. // https://w3c.github.io/webvtt/#dom-vttregion-viewportanchorx
  75. WebIDL::ExceptionOr<void> VTTRegion::set_viewport_anchor_x(double viewport_anchor_x)
  76. {
  77. // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown.
  78. if (viewport_anchor_x < 0 || viewport_anchor_x > 100)
  79. return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_string);
  80. // Otherwise, the WebVTT region viewport anchor X distance must be set to the new value.
  81. m_viewport_anchor_x = viewport_anchor_x;
  82. return {};
  83. }
  84. // https://w3c.github.io/webvtt/#dom-vttregion-viewportanchory
  85. WebIDL::ExceptionOr<void> VTTRegion::set_viewport_anchor_y(double viewport_anchor_y)
  86. {
  87. // On setting, if the new value is negative or greater than 100, then an IndexSizeError exception must be thrown.
  88. if (viewport_anchor_y < 0 || viewport_anchor_y > 100)
  89. return WebIDL::IndexSizeError::create(realm(), "Value is negative or greater than 100"_string);
  90. // Otherwise, the WebVTT region viewport anchor Y distance must be set to the new value.
  91. m_viewport_anchor_y = viewport_anchor_y;
  92. return {};
  93. }
  94. }