CSSFontFaceRule.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. JS::NonnullGCPtr<CSSFontFaceRule> CSSFontFaceRule::create(JS::Realm& realm, FontFace&& font_face)
  15. {
  16. return 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. void CSSFontFaceRule::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSFontFaceRulePrototype>(realm, "CSSFontFaceRule"));
  27. }
  28. CSSStyleDeclaration* CSSFontFaceRule::style()
  29. {
  30. // FIXME: Return a CSSStyleDeclaration subclass that directs changes to the FontFace.
  31. return nullptr;
  32. }
  33. // https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
  34. DeprecatedString CSSFontFaceRule::serialized() const
  35. {
  36. StringBuilder builder;
  37. // The result of concatenating the following:
  38. // 1. The string "@font-face {", followed by a single SPACE (U+0020).
  39. builder.append("@font-face { "sv);
  40. // 2. The string "font-family:", followed by a single SPACE (U+0020).
  41. builder.append("font-family: "sv);
  42. // 3. The result of performing serialize a string on the rule’s font family name.
  43. serialize_a_string(builder, m_font_face.font_family());
  44. // 4. The string ";", i.e., SEMICOLON (U+003B).
  45. builder.append(';');
  46. // 5. If the rule’s associated source list is not empty, follow these substeps:
  47. if (!m_font_face.sources().is_empty()) {
  48. // 1. A single SPACE (U+0020), followed by the string "src:", followed by a single SPACE (U+0020).
  49. builder.append(" src: "sv);
  50. // 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.
  51. serialize_a_comma_separated_list(builder, m_font_face.sources(), [&](StringBuilder& builder, FontFace::Source source) -> void {
  52. if (source.local_or_url.has<AK::URL>()) {
  53. serialize_a_url(builder, source.local_or_url.get<AK::URL>().to_deprecated_string());
  54. } else {
  55. builder.appendff("local({})", source.local_or_url.get<String>());
  56. }
  57. // NOTE: No spec currently exists for format()
  58. if (source.format.has_value()) {
  59. builder.append(" format("sv);
  60. serialize_a_string(builder, source.format.value());
  61. builder.append(")"sv);
  62. }
  63. });
  64. // 3. The string ";", i.e., SEMICOLON (U+003B).
  65. builder.append(';');
  66. }
  67. // 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).
  68. builder.append(" unicode-range: "sv);
  69. serialize_unicode_ranges(builder, m_font_face.unicode_ranges());
  70. builder.append(';');
  71. // FIXME: 7. If rule’s associated font-variant descriptor is present, a single SPACE (U+0020),
  72. // followed by the string "font-variant:", followed by a single SPACE (U+0020),
  73. // followed by the result of performing serialize a <'font-variant'>,
  74. // followed by the string ";", i.e., SEMICOLON (U+003B).
  75. // FIXME: 8. If rule’s associated font-feature-settings descriptor is present, a single SPACE (U+0020),
  76. // followed by the string "font-feature-settings:", followed by a single SPACE (U+0020),
  77. // followed by the result of performing serialize a <'font-feature-settings'>,
  78. // followed by the string ";", i.e., SEMICOLON (U+003B).
  79. // FIXME: 9. If rule’s associated font-stretch descriptor is present, a single SPACE (U+0020),
  80. // followed by the string "font-stretch:", followed by a single SPACE (U+0020),
  81. // followed by the result of performing serialize a <'font-stretch'>,
  82. // followed by the string ";", i.e., SEMICOLON (U+003B).
  83. // 10. If rule’s associated font-weight descriptor is present, a single SPACE (U+0020),
  84. // followed by the string "font-weight:", followed by a single SPACE (U+0020),
  85. // followed by the result of performing serialize a <'font-weight'>,
  86. // followed by the string ";", i.e., SEMICOLON (U+003B).
  87. if (m_font_face.weight().has_value()) {
  88. auto weight = m_font_face.weight().value();
  89. builder.append(" font-weight: "sv);
  90. if (weight == 400)
  91. builder.append("normal"sv);
  92. else if (weight == 700)
  93. builder.append("bold"sv);
  94. else
  95. builder.appendff("{}", weight);
  96. builder.append(";"sv);
  97. }
  98. // 11. If rule’s associated font-style descriptor is present, a single SPACE (U+0020),
  99. // followed by the string "font-style:", followed by a single SPACE (U+0020),
  100. // followed by the result of performing serialize a <'font-style'>,
  101. // followed by the string ";", i.e., SEMICOLON (U+003B).
  102. if (m_font_face.slope().has_value()) {
  103. auto slope = m_font_face.slope().value();
  104. builder.append(" font-style: "sv);
  105. if (slope == Gfx::name_to_slope("Normal"sv))
  106. builder.append("normal"sv);
  107. else if (slope == Gfx::name_to_slope("Italic"sv))
  108. builder.append("italic"sv);
  109. else {
  110. dbgln("FIXME: CSSFontFaceRule::serialized() does not support slope {}", slope);
  111. builder.append("italic"sv);
  112. }
  113. builder.append(";"sv);
  114. }
  115. // 12. A single SPACE (U+0020), followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D).
  116. builder.append(" }"sv);
  117. return builder.to_deprecated_string();
  118. }
  119. }