CSSFontFaceRule.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Font/FontStyleMapping.h>
  8. #include <LibWeb/Bindings/CSSFontFaceRulePrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/CSS/CSSFontFaceRule.h>
  11. #include <LibWeb/CSS/Serialize.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::CSS {
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSFontFaceRule>> CSSFontFaceRule::create(JS::Realm& realm, FontFace&& font_face)
  15. {
  16. return MUST_OR_THROW_OOM(realm.heap().allocate<CSSFontFaceRule>(realm, realm, move(font_face)));
  17. }
  18. CSSFontFaceRule::CSSFontFaceRule(JS::Realm& realm, FontFace&& font_face)
  19. : CSSRule(realm)
  20. , m_font_face(move(font_face))
  21. {
  22. }
  23. JS::ThrowCompletionOr<void> CSSFontFaceRule::initialize(JS::Realm& realm)
  24. {
  25. MUST_OR_THROW_OOM(Base::initialize(realm));
  26. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSFontFaceRulePrototype>(realm, "CSSFontFaceRule"));
  27. return {};
  28. }
  29. CSSStyleDeclaration* CSSFontFaceRule::style()
  30. {
  31. // FIXME: Return a CSSStyleDeclaration subclass that directs changes to the FontFace.
  32. return nullptr;
  33. }
  34. // https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
  35. DeprecatedString CSSFontFaceRule::serialized() const
  36. {
  37. StringBuilder builder;
  38. // The result of concatenating the following:
  39. // 1. The string "@font-face {", followed by a single SPACE (U+0020).
  40. builder.append("@font-face { "sv);
  41. // 2. The string "font-family:", followed by a single SPACE (U+0020).
  42. builder.append("font-family: "sv);
  43. // 3. The result of performing serialize a string on the rule’s font family name.
  44. serialize_a_string(builder, m_font_face.font_family()).release_value_but_fixme_should_propagate_errors();
  45. // 4. The string ";", i.e., SEMICOLON (U+003B).
  46. builder.append(';');
  47. // 5. If the rule’s associated source list is not empty, follow these substeps:
  48. if (!m_font_face.sources().is_empty()) {
  49. // 1. A single SPACE (U+0020), followed by the string "src:", followed by a single SPACE (U+0020).
  50. builder.append(" src: "sv);
  51. // 2. The result of invoking serialize a comma-separated list on performing serialize a URL or serialize a LOCAL for each source on the source list.
  52. serialize_a_comma_separated_list(builder, m_font_face.sources(), [&](StringBuilder& builder, FontFace::Source source) -> ErrorOr<void> {
  53. if (source.url.cannot_be_a_base_url()) {
  54. TRY(serialize_a_url(builder, source.url.to_deprecated_string()));
  55. } else {
  56. TRY(serialize_a_local(builder, source.url.to_deprecated_string()));
  57. }
  58. // NOTE: No spec currently exists for format()
  59. if (source.format.has_value()) {
  60. TRY(builder.try_append("format(\""sv));
  61. TRY(serialize_a_string(builder, source.format.value()));
  62. TRY(builder.try_append("\")"sv));
  63. }
  64. return {};
  65. }).release_value_but_fixme_should_propagate_errors();
  66. // 3. The string ";", i.e., SEMICOLON (U+003B).
  67. builder.append(';');
  68. }
  69. // 6. If rule’s associated unicode-range descriptor is present, a single SPACE (U+0020), followed by the string "unicode-range:", followed by a single SPACE (U+0020), followed by the result of performing serialize a <'unicode-range'>, followed by the string ";", i.e., SEMICOLON (U+003B).
  70. builder.append(" unicode-range: "sv);
  71. serialize_unicode_ranges(builder, m_font_face.unicode_ranges()).release_value_but_fixme_should_propagate_errors();
  72. builder.append(';');
  73. // FIXME: 7. If rule’s associated font-variant descriptor is present, a single SPACE (U+0020),
  74. // followed by the string "font-variant:", followed by a single SPACE (U+0020),
  75. // followed by the result of performing serialize a <'font-variant'>,
  76. // followed by the string ";", i.e., SEMICOLON (U+003B).
  77. // FIXME: 8. If rule’s associated font-feature-settings descriptor is present, a single SPACE (U+0020),
  78. // followed by the string "font-feature-settings:", followed by a single SPACE (U+0020),
  79. // followed by the result of performing serialize a <'font-feature-settings'>,
  80. // followed by the string ";", i.e., SEMICOLON (U+003B).
  81. // FIXME: 9. If rule’s associated font-stretch descriptor is present, a single SPACE (U+0020),
  82. // followed by the string "font-stretch:", followed by a single SPACE (U+0020),
  83. // followed by the result of performing serialize a <'font-stretch'>,
  84. // followed by the string ";", i.e., SEMICOLON (U+003B).
  85. // 10. If rule’s associated font-weight descriptor is present, a single SPACE (U+0020),
  86. // followed by the string "font-weight:", followed by a single SPACE (U+0020),
  87. // followed by the result of performing serialize a <'font-weight'>,
  88. // followed by the string ";", i.e., SEMICOLON (U+003B).
  89. if (m_font_face.weight().has_value()) {
  90. auto weight = m_font_face.weight().value();
  91. builder.append(" font-weight: "sv);
  92. if (weight == 400)
  93. builder.append("normal"sv);
  94. else if (weight == 700)
  95. builder.append("bold"sv);
  96. else
  97. builder.appendff("{}", weight);
  98. builder.append(";"sv);
  99. }
  100. // 11. If rule’s associated font-style descriptor is present, a single SPACE (U+0020),
  101. // followed by the string "font-style:", followed by a single SPACE (U+0020),
  102. // followed by the result of performing serialize a <'font-style'>,
  103. // followed by the string ";", i.e., SEMICOLON (U+003B).
  104. if (m_font_face.slope().has_value()) {
  105. auto slope = m_font_face.slope().value();
  106. builder.append(" font-style: "sv);
  107. if (slope == Gfx::name_to_slope("Normal"sv))
  108. builder.append("normal"sv);
  109. else if (slope == Gfx::name_to_slope("Italic"sv))
  110. builder.append("italic"sv);
  111. else {
  112. dbgln("FIXME: CSSFontFaceRule::serialized() does not support slope {}", slope);
  113. builder.append("italic"sv);
  114. }
  115. builder.append(";"sv);
  116. }
  117. // 12. A single SPACE (U+0020), followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D).
  118. builder.append(" }"sv);
  119. return builder.to_deprecated_string();
  120. }
  121. }