CSSFontFaceRule.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Font/Font.h>
  8. #include <LibGfx/Font/FontStyleMapping.h>
  9. #include <LibWeb/Bindings/CSSFontFaceRulePrototype.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/CSS/CSSFontFaceRule.h>
  12. #include <LibWeb/CSS/Serialize.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::CSS {
  15. JS_DEFINE_ALLOCATOR(CSSFontFaceRule);
  16. JS::NonnullGCPtr<CSSFontFaceRule> CSSFontFaceRule::create(JS::Realm& realm, ParsedFontFace&& font_face)
  17. {
  18. return realm.heap().allocate<CSSFontFaceRule>(realm, realm, move(font_face));
  19. }
  20. CSSFontFaceRule::CSSFontFaceRule(JS::Realm& realm, ParsedFontFace&& font_face)
  21. : CSSRule(realm)
  22. , m_font_face(move(font_face))
  23. {
  24. }
  25. void CSSFontFaceRule::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSFontFaceRule);
  29. }
  30. CSSStyleDeclaration* CSSFontFaceRule::style()
  31. {
  32. // FIXME: Return a CSSStyleDeclaration subclass that directs changes to the ParsedFontFace.
  33. return nullptr;
  34. }
  35. // https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
  36. String CSSFontFaceRule::serialized() const
  37. {
  38. StringBuilder builder;
  39. // The result of concatenating the following:
  40. // 1. The string "@font-face {", followed by a single SPACE (U+0020).
  41. builder.append("@font-face { "sv);
  42. // 2. The string "font-family:", followed by a single SPACE (U+0020).
  43. builder.append("font-family: "sv);
  44. // 3. The result of performing serialize a string on the rule’s font family name.
  45. serialize_a_string(builder, m_font_face.font_family());
  46. // 4. The string ";", i.e., SEMICOLON (U+003B).
  47. builder.append(';');
  48. // 5. If the rule’s associated source list is not empty, follow these substeps:
  49. if (!m_font_face.sources().is_empty()) {
  50. // 1. A single SPACE (U+0020), followed by the string "src:", followed by a single SPACE (U+0020).
  51. builder.append(" src: "sv);
  52. // 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.
  53. serialize_a_comma_separated_list(builder, m_font_face.sources(), [&](StringBuilder& builder, ParsedFontFace::Source source) -> void {
  54. if (source.local_or_url.has<URL::URL>()) {
  55. serialize_a_url(builder, MUST(source.local_or_url.get<URL::URL>().to_string()));
  56. } else {
  57. builder.appendff("local({})", source.local_or_url.get<String>());
  58. }
  59. // NOTE: No spec currently exists for format()
  60. if (source.format.has_value()) {
  61. builder.append(" format("sv);
  62. serialize_a_string(builder, source.format.value());
  63. builder.append(")"sv);
  64. }
  65. });
  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());
  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. // 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. if (m_font_face.font_feature_settings().has_value()) {
  82. auto const& feature_settings = m_font_face.font_feature_settings().value();
  83. builder.append(" font-feature-settings: "sv);
  84. // NOTE: We sort the tags during parsing, so they're already in the correct order.
  85. bool first = true;
  86. for (auto const& [key, value] : feature_settings) {
  87. if (first) {
  88. first = false;
  89. } else {
  90. builder.append(", "sv);
  91. }
  92. serialize_a_string(builder, key);
  93. // NOTE: 1 is the default value, so don't serialize it.
  94. if (value != 1)
  95. builder.appendff(" {}", value);
  96. }
  97. builder.append(";"sv);
  98. }
  99. // 9. If rule’s associated font-stretch descriptor is present, a single SPACE (U+0020),
  100. // followed by the string "font-stretch:", followed by a single SPACE (U+0020),
  101. // followed by the result of performing serialize a <'font-stretch'>,
  102. // followed by the string ";", i.e., SEMICOLON (U+003B).
  103. // NOTE: font-stretch is now an alias for font-width, so we use that instead.
  104. if (m_font_face.width().has_value()) {
  105. builder.append(" font-width: "sv);
  106. // NOTE: font-width is supposed to always be serialized as a percentage.
  107. // Right now, it's stored as a Gfx::FontWidth value, so we have to lossily convert it back.
  108. float percentage = 100.0f;
  109. switch (m_font_face.width().value()) {
  110. case Gfx::FontWidth::UltraCondensed:
  111. percentage = 50.0f;
  112. break;
  113. case Gfx::FontWidth::ExtraCondensed:
  114. percentage = 62.5f;
  115. break;
  116. case Gfx::FontWidth::Condensed:
  117. percentage = 75.0f;
  118. break;
  119. case Gfx::FontWidth::SemiCondensed:
  120. percentage = 87.5f;
  121. break;
  122. case Gfx::FontWidth::Normal:
  123. percentage = 100.0f;
  124. break;
  125. case Gfx::FontWidth::SemiExpanded:
  126. percentage = 112.5f;
  127. break;
  128. case Gfx::FontWidth::Expanded:
  129. percentage = 125.0f;
  130. break;
  131. case Gfx::FontWidth::ExtraExpanded:
  132. percentage = 150.0f;
  133. break;
  134. case Gfx::FontWidth::UltraExpanded:
  135. percentage = 200.0f;
  136. break;
  137. default:
  138. break;
  139. }
  140. builder.appendff("{}%", percentage);
  141. builder.append(";"sv);
  142. }
  143. // 10. If rule’s associated font-weight descriptor is present, a single SPACE (U+0020),
  144. // followed by the string "font-weight:", followed by a single SPACE (U+0020),
  145. // followed by the result of performing serialize a <'font-weight'>,
  146. // followed by the string ";", i.e., SEMICOLON (U+003B).
  147. if (m_font_face.weight().has_value()) {
  148. auto weight = m_font_face.weight().value();
  149. builder.append(" font-weight: "sv);
  150. if (weight == 400)
  151. builder.append("normal"sv);
  152. else if (weight == 700)
  153. builder.append("bold"sv);
  154. else
  155. builder.appendff("{}", weight);
  156. builder.append(";"sv);
  157. }
  158. // 11. If rule’s associated font-style descriptor is present, a single SPACE (U+0020),
  159. // followed by the string "font-style:", followed by a single SPACE (U+0020),
  160. // followed by the result of performing serialize a <'font-style'>,
  161. // followed by the string ";", i.e., SEMICOLON (U+003B).
  162. if (m_font_face.slope().has_value()) {
  163. auto slope = m_font_face.slope().value();
  164. builder.append(" font-style: "sv);
  165. if (slope == Gfx::name_to_slope("Normal"sv))
  166. builder.append("normal"sv);
  167. else if (slope == Gfx::name_to_slope("Italic"sv))
  168. builder.append("italic"sv);
  169. else {
  170. dbgln("FIXME: CSSFontFaceRule::serialized() does not support slope {}", slope);
  171. builder.append("italic"sv);
  172. }
  173. builder.append(";"sv);
  174. }
  175. // 12. A single SPACE (U+0020), followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D).
  176. builder.append(" }"sv);
  177. return MUST(builder.to_string());
  178. }
  179. }