CSSFontFaceRule.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/CSS/CSSFontFaceRule.h>
  8. #include <LibWeb/CSS/Serialize.h>
  9. #include <LibWeb/HTML/Window.h>
  10. namespace Web::CSS {
  11. CSSFontFaceRule* CSSFontFaceRule::create(HTML::Window& window_object, FontFace&& font_face)
  12. {
  13. return window_object.heap().allocate<CSSFontFaceRule>(window_object.realm(), window_object, move(font_face));
  14. }
  15. CSSFontFaceRule::CSSFontFaceRule(HTML::Window& window_object, FontFace&& font_face)
  16. : CSSRule(window_object)
  17. , m_font_face(move(font_face))
  18. {
  19. set_prototype(&window_object.cached_web_prototype("CSSFontFaceRule"));
  20. }
  21. CSSStyleDeclaration* CSSFontFaceRule::style()
  22. {
  23. // FIXME: Return a CSSStyleDeclaration subclass that directs changes to the FontFace.
  24. return nullptr;
  25. }
  26. // https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
  27. String CSSFontFaceRule::serialized() const
  28. {
  29. StringBuilder builder;
  30. // The result of concatenating the following:
  31. // 1. The string "@font-face {", followed by a single SPACE (U+0020).
  32. builder.append("@font-face { "sv);
  33. // 2. The string "font-family:", followed by a single SPACE (U+0020).
  34. builder.append("font-family: "sv);
  35. // 3. The result of performing serialize a string on the rule’s font family name.
  36. serialize_a_string(builder, m_font_face.font_family());
  37. // 4. The string ";", i.e., SEMICOLON (U+003B).
  38. builder.append(';');
  39. // 5. If the rule’s associated source list is not empty, follow these substeps:
  40. if (!m_font_face.sources().is_empty()) {
  41. // 1. A single SPACE (U+0020), followed by the string "src:", followed by a single SPACE (U+0020).
  42. builder.append(" src: "sv);
  43. // 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.
  44. serialize_a_comma_separated_list(builder, m_font_face.sources(), [&](FontFace::Source source) {
  45. if (source.url.cannot_be_a_base_url()) {
  46. serialize_a_url(builder, source.url.to_string());
  47. } else {
  48. serialize_a_local(builder, source.url.to_string());
  49. }
  50. // NOTE: No spec currently exists for format()
  51. if (source.format.has_value()) {
  52. builder.append("format(\""sv);
  53. serialize_a_string(builder, source.format.value());
  54. builder.append("\")"sv);
  55. }
  56. });
  57. // 3. The string ";", i.e., SEMICOLON (U+003B).
  58. builder.append(';');
  59. }
  60. // 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).
  61. builder.append(" unicode-range: "sv);
  62. serialize_unicode_ranges(builder, m_font_face.unicode_ranges());
  63. builder.append(';');
  64. // FIXME: 7. If rule’s associated font-variant descriptor is present, a single SPACE (U+0020),
  65. // followed by the string "font-variant:", followed by a single SPACE (U+0020),
  66. // followed by the result of performing serialize a <'font-variant'>,
  67. // followed by the string ";", i.e., SEMICOLON (U+003B).
  68. // FIXME: 8. If rule’s associated font-feature-settings descriptor is present, a single SPACE (U+0020),
  69. // followed by the string "font-feature-settings:", followed by a single SPACE (U+0020),
  70. // followed by the result of performing serialize a <'font-feature-settings'>,
  71. // followed by the string ";", i.e., SEMICOLON (U+003B).
  72. // FIXME: 9. If rule’s associated font-stretch descriptor is present, a single SPACE (U+0020),
  73. // followed by the string "font-stretch:", followed by a single SPACE (U+0020),
  74. // followed by the result of performing serialize a <'font-stretch'>,
  75. // followed by the string ";", i.e., SEMICOLON (U+003B).
  76. // FIXME: 10. If rule’s associated font-weight descriptor is present, a single SPACE (U+0020),
  77. // followed by the string "font-weight:", followed by a single SPACE (U+0020),
  78. // followed by the result of performing serialize a <'font-weight'>,
  79. // followed by the string ";", i.e., SEMICOLON (U+003B).
  80. // FIXME: 11. If rule’s associated font-style descriptor is present, a single SPACE (U+0020),
  81. // followed by the string "font-style:", followed by a single SPACE (U+0020),
  82. // followed by the result of performing serialize a <'font-style'>,
  83. // followed by the string ";", i.e., SEMICOLON (U+003B).
  84. // 12. A single SPACE (U+0020), followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D).
  85. builder.append(" }"sv);
  86. return builder.to_string();
  87. }
  88. }