FontFace.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/FontFacePrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/CSS/FontFace.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/WebIDL/Promise.h>
  13. namespace Web::CSS {
  14. JS_DEFINE_ALLOCATOR(FontFace);
  15. template<CSS::PropertyID PropertyID>
  16. RefPtr<CSS::StyleValue const> parse_property_string(JS::Realm& realm, StringView value)
  17. {
  18. auto maybe_parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), value);
  19. if (maybe_parser.is_error())
  20. return {};
  21. return maybe_parser.release_value().parse_as_css_value(PropertyID);
  22. }
  23. JS::NonnullGCPtr<FontFace> FontFace::construct_impl(JS::Realm& realm, String family, FontFaceSource source, FontFaceDescriptors const& descriptors)
  24. {
  25. return realm.heap().allocate<FontFace>(realm, realm, move(family), move(source), descriptors);
  26. }
  27. FontFace::FontFace(JS::Realm& realm, String font_family, FontFaceSource, FontFaceDescriptors const& descriptors)
  28. : Bindings::PlatformObject(realm)
  29. {
  30. // FIXME: Validate these values the same way as the setters
  31. m_family = move(font_family);
  32. m_style = descriptors.style;
  33. m_weight = descriptors.weight;
  34. m_stretch = descriptors.stretch;
  35. m_unicode_range = descriptors.unicode_range;
  36. m_feature_settings = descriptors.feature_settings;
  37. m_variation_settings = descriptors.variation_settings;
  38. m_display = descriptors.display;
  39. m_ascent_override = descriptors.ascent_override;
  40. m_descent_override = descriptors.descent_override;
  41. m_line_gap_override = descriptors.line_gap_override;
  42. }
  43. void FontFace::initialize(JS::Realm& realm)
  44. {
  45. Base::initialize(realm);
  46. WEB_SET_PROTOTYPE_FOR_INTERFACE(FontFace);
  47. }
  48. // https://drafts.csswg.org/css-font-loading/#dom-fontface-family
  49. WebIDL::ExceptionOr<void> FontFace::set_family(String const& string)
  50. {
  51. auto property = parse_property_string<CSS::PropertyID::FontFamily>(realm(), string);
  52. if (!property)
  53. return WebIDL::SyntaxError::create(realm(), "FontFace.family setter: Invalid font descriptor"_fly_string);
  54. if (m_is_css_connected) {
  55. // FIXME: Propagate to the CSSFontFaceRule and update the font-family property
  56. }
  57. m_family = property->to_string();
  58. return {};
  59. }
  60. // https://drafts.csswg.org/css-font-loading/#dom-fontface-style
  61. WebIDL::ExceptionOr<void> FontFace::set_style(String const& string)
  62. {
  63. auto property = parse_property_string<CSS::PropertyID::FontStyle>(realm(), string);
  64. if (!property)
  65. return WebIDL::SyntaxError::create(realm(), "FontFace.style setter: Invalid font descriptor"_fly_string);
  66. if (m_is_css_connected) {
  67. // FIXME: Propagate to the CSSFontFaceRule and update the font-style property
  68. }
  69. m_style = property->to_string();
  70. return {};
  71. }
  72. // https://drafts.csswg.org/css-font-loading/#dom-fontface-weight
  73. WebIDL::ExceptionOr<void> FontFace::set_weight(String const& string)
  74. {
  75. auto property = parse_property_string<CSS::PropertyID::FontWeight>(realm(), string);
  76. if (!property)
  77. return WebIDL::SyntaxError::create(realm(), "FontFace.weight setter: Invalid font descriptor"_fly_string);
  78. if (m_is_css_connected) {
  79. // FIXME: Propagate to the CSSFontFaceRule and update the font-weight property
  80. }
  81. m_weight = property->to_string();
  82. return {};
  83. }
  84. // https://drafts.csswg.org/css-font-loading/#dom-fontface-stretch
  85. WebIDL::ExceptionOr<void> FontFace::set_stretch(String const& string)
  86. {
  87. auto property = parse_property_string<CSS::PropertyID::FontStretch>(realm(), string);
  88. if (!property)
  89. return WebIDL::SyntaxError::create(realm(), "FontFace.stretch setter: Invalid font descriptor"_fly_string);
  90. if (m_is_css_connected) {
  91. // FIXME: Propagate to the CSSFontFaceRule and update the font-stretch property
  92. }
  93. m_stretch = property->to_string();
  94. return {};
  95. }
  96. // https://drafts.csswg.org/css-font-loading/#dom-fontface-unicoderange
  97. WebIDL::ExceptionOr<void> FontFace::set_unicode_range(String const&)
  98. {
  99. // FIXME: This *should* work, but the <urange> production is hard to parse
  100. // from just a value string in our implementation
  101. return WebIDL::NotSupportedError::create(realm(), "unicode range is not yet implemented"_fly_string);
  102. }
  103. // https://drafts.csswg.org/css-font-loading/#dom-fontface-featuresettings
  104. WebIDL::ExceptionOr<void> FontFace::set_feature_settings(String const&)
  105. {
  106. return WebIDL::NotSupportedError::create(realm(), "feature settings is not yet implemented"_fly_string);
  107. }
  108. // https://drafts.csswg.org/css-font-loading/#dom-fontface-variationsettings
  109. WebIDL::ExceptionOr<void> FontFace::set_variation_settings(String const&)
  110. {
  111. return WebIDL::NotSupportedError::create(realm(), "variation settings is not yet implemented"_fly_string);
  112. }
  113. // https://drafts.csswg.org/css-font-loading/#dom-fontface-display
  114. WebIDL::ExceptionOr<void> FontFace::set_display(String const&)
  115. {
  116. return WebIDL::NotSupportedError::create(realm(), "display is not yet implemented"_fly_string);
  117. }
  118. // https://drafts.csswg.org/css-font-loading/#dom-fontface-ascentoverride
  119. WebIDL::ExceptionOr<void> FontFace::set_ascent_override(String const&)
  120. {
  121. return WebIDL::NotSupportedError::create(realm(), "ascent override is not yet implemented"_fly_string);
  122. }
  123. // https://drafts.csswg.org/css-font-loading/#dom-fontface-descentoverride
  124. WebIDL::ExceptionOr<void> FontFace::set_descent_override(String const&)
  125. {
  126. return WebIDL::NotSupportedError::create(realm(), "descent override is not yet implemented"_fly_string);
  127. }
  128. // https://drafts.csswg.org/css-font-loading/#dom-fontface-linegapoverride
  129. WebIDL::ExceptionOr<void> FontFace::set_line_gap_override(String const&)
  130. {
  131. return WebIDL::NotSupportedError::create(realm(), "line gap override is not yet implemented"_fly_string);
  132. }
  133. // https://drafts.csswg.org/css-font-loading/#dom-fontface-load
  134. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> FontFace::load()
  135. {
  136. // FIXME: Do the steps
  137. auto promise = WebIDL::create_rejected_promise(realm(), WebIDL::NotSupportedError::create(realm(), "FontFace::load is not yet implemented"_fly_string));
  138. return verify_cast<JS::Promise>(*promise->promise());
  139. }
  140. }