CSSFontFaceRule.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_DEFINE_ALLOCATOR(CSSFontFaceRule);
  15. JS::NonnullGCPtr<CSSFontFaceRule> CSSFontFaceRule::create(JS::Realm& realm, ParsedFontFace&& font_face)
  16. {
  17. return realm.heap().allocate<CSSFontFaceRule>(realm, realm, move(font_face));
  18. }
  19. CSSFontFaceRule::CSSFontFaceRule(JS::Realm& realm, ParsedFontFace&& font_face)
  20. : CSSRule(realm)
  21. , m_font_face(move(font_face))
  22. {
  23. }
  24. void CSSFontFaceRule::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSFontFaceRule);
  28. }
  29. CSSStyleDeclaration* CSSFontFaceRule::style()
  30. {
  31. // FIXME: Return a CSSStyleDeclaration subclass that directs changes to the ParsedFontFace.
  32. return nullptr;
  33. }
  34. // https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
  35. String 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());
  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, ParsedFontFace::Source source) -> void {
  53. if (source.local_or_url.has<URL::URL>()) {
  54. serialize_a_url(builder, MUST(source.local_or_url.get<URL::URL>().to_string()));
  55. } else {
  56. builder.appendff("local({})", source.local_or_url.get<String>());
  57. }
  58. // NOTE: No spec currently exists for format()
  59. if (source.format.has_value()) {
  60. builder.append(" format("sv);
  61. serialize_a_string(builder, source.format.value());
  62. builder.append(")"sv);
  63. }
  64. });
  65. // 3. The string ";", i.e., SEMICOLON (U+003B).
  66. builder.append(';');
  67. }
  68. // 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).
  69. builder.append(" unicode-range: "sv);
  70. serialize_unicode_ranges(builder, m_font_face.unicode_ranges());
  71. builder.append(';');
  72. // FIXME: 7. If rule’s associated font-variant descriptor is present, a single SPACE (U+0020),
  73. // followed by the string "font-variant:", followed by a single SPACE (U+0020),
  74. // followed by the result of performing serialize a <'font-variant'>,
  75. // followed by the string ";", i.e., SEMICOLON (U+003B).
  76. // FIXME: 8. If rule’s associated font-feature-settings descriptor is present, a single SPACE (U+0020),
  77. // followed by the string "font-feature-settings:", followed by a single SPACE (U+0020),
  78. // followed by the result of performing serialize a <'font-feature-settings'>,
  79. // followed by the string ";", i.e., SEMICOLON (U+003B).
  80. // FIXME: 9. If rule’s associated font-stretch descriptor is present, a single SPACE (U+0020),
  81. // followed by the string "font-stretch:", followed by a single SPACE (U+0020),
  82. // followed by the result of performing serialize a <'font-stretch'>,
  83. // followed by the string ";", i.e., SEMICOLON (U+003B).
  84. // 10. If rule’s associated font-weight descriptor is present, a single SPACE (U+0020),
  85. // followed by the string "font-weight:", followed by a single SPACE (U+0020),
  86. // followed by the result of performing serialize a <'font-weight'>,
  87. // followed by the string ";", i.e., SEMICOLON (U+003B).
  88. if (m_font_face.weight().has_value()) {
  89. auto weight = m_font_face.weight().value();
  90. builder.append(" font-weight: "sv);
  91. if (weight == 400)
  92. builder.append("normal"sv);
  93. else if (weight == 700)
  94. builder.append("bold"sv);
  95. else
  96. builder.appendff("{}", weight);
  97. builder.append(";"sv);
  98. }
  99. // 11. If rule’s associated font-style descriptor is present, a single SPACE (U+0020),
  100. // followed by the string "font-style:", followed by a single SPACE (U+0020),
  101. // followed by the result of performing serialize a <'font-style'>,
  102. // followed by the string ";", i.e., SEMICOLON (U+003B).
  103. if (m_font_face.slope().has_value()) {
  104. auto slope = m_font_face.slope().value();
  105. builder.append(" font-style: "sv);
  106. if (slope == Gfx::name_to_slope("Normal"sv))
  107. builder.append("normal"sv);
  108. else if (slope == Gfx::name_to_slope("Italic"sv))
  109. builder.append("italic"sv);
  110. else {
  111. dbgln("FIXME: CSSFontFaceRule::serialized() does not support slope {}", slope);
  112. builder.append("italic"sv);
  113. }
  114. builder.append(";"sv);
  115. }
  116. // 12. A single SPACE (U+0020), followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D).
  117. builder.append(" }"sv);
  118. return MUST(builder.to_string());
  119. }
  120. }