Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / css_font_face_rule.lucb

#==============================================================================================
#
#   css_font_face_rule - CSS::CSSFontFaceRule: an @font-face rule
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/CSSFontFaceRule.cpp (lines 1-229) and the inline members of
#       CSSFontFaceRule.h (lines 19-51): validity, the parsed font face, serialization and the
#       FontFace object that is CSS-connected to the rule (CSS Font Loading).
#
#       Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: CSSFontFaceRule.h =======================================================================

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.h:30 Web::CSS::CSSFontFaceRule::style.
pub func css_font_face_rule_style(this: CssFontFaceRule*) -> CssStyleDeclaration*:
    return this.m_style.css_style_declaration()

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.h:31 Web::CSS::CSSFontFaceRule::descriptors.
pub func css_font_face_rule_descriptors(this: CssFontFaceRule*) -> CssCssFontFaceDescriptors*:
    return this.m_style

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.h:31 Web::CSS::CSSFontFaceRule::descriptors (const).
pub func css_font_face_rule_descriptors_const(this: CssFontFaceRule*) -> CssCssFontFaceDescriptors*:
    return this.m_style

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.h:34 Web::CSS::CSSFontFaceRule::css_connected_font_face.
pub func css_font_face_rule_css_connected_font_face(this: CssFontFaceRule*) -> CssFontFace*?:
    return this.m_css_connected_font_face

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.h:34 Web::CSS::CSSFontFaceRule::set_css_connected_font_face.
pub func css_font_face_rule_set_css_connected_font_face(this: CssFontFaceRule*, font_face: CssFontFace*?):
    this.m_css_connected_font_face = font_face

# mark: CSSFontFaceRule.cpp =====================================================================

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:25 Web::CSS::CSSFontFaceRule::create.
pub func css_font_face_rule_create(realm: Realm*, style: CssCssFontFaceDescriptors*) -> CssFontFaceRule*:
    return realm_create_css_font_face_rule(realm, realm, style)

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:27 Web::CSS::CSSFontFaceRule::CSSFontFaceRule.
pub func css_font_face_rule_construct(this: CssFontFaceRule*, realm: Realm*, style: CssCssFontFaceDescriptors*):
    css_rule_construct(this.css_rule(), realm, CssRuleType.font_face)
    this.m_style = style
    css_style_declaration_set_parent_rule(this.m_style.css_style_declaration(), this.css_rule())

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:37 Web::CSS::CSSFontFaceRule::initialize (override).
pub func css_font_face_rule_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.css_font_face_rule)
    js_cell_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:43 Web::CSS::CSSFontFaceRule::is_valid.
pub func css_font_face_rule_is_valid(this: CssFontFaceRule*) -> bool:
    # @font-face rules require a font-family and src descriptor; if either of these are missing, the @font-face rule
    # must not be considered when performing the font matching algorithm.
    # https://drafts.csswg.org/css-fonts-4/#font-face-rule
    let descriptors = this.m_style.css_descriptors()
    return (css_descriptors_descriptor(descriptors, css_descriptor_name_and_id_from_id(css_data.DescriptorId.font_family)) != none and css_descriptors_descriptor(descriptors, css_descriptor_name_and_id_from_id(css_data.DescriptorId.src)) != none)

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:52 Web::CSS::CSSFontFaceRule::font_face.
pub func css_font_face_rule_font_face(this: CssFontFaceRule*) -> CssParsedFontFace:
    return css_parsed_font_face_from_descriptors(this.m_style)

## Serialize the descriptor `id` of `descriptors` into `builder` after `prefix`, if it is present (steps 6 and 8-11
## of serialized()).
func css_font_face_rule_serialize_descriptor(builder: ak.StringBuilder*, descriptors: CssDescriptors*, id: css_data.DescriptorId, prefix: str):
    if let value = css_descriptors_descriptor(descriptors, css_descriptor_name_and_id_from_id(id)):
        ak.string_builder_append(builder, prefix)
        css_style_value_serialize(value, builder, css_syntax.SerializationMode.normal)
        ak.string_builder_append(builder, ";")

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:58 Web::CSS::CSSFontFaceRule::serialized (override).
# https://www.w3.org/TR/cssom/#ref-for-cssfontfacerule
pub func css_font_face_rule_serialized_impl(base: CssRule*) -> ak.String:
    let this = (CssFontFaceRule*)base
    let descriptors = this.m_style.css_descriptors()

    var builder = ak.string_builder_new()
    # The result of concatenating the following:

    # 1. The string "@font-face {", followed by a single SPACE (U+0020).
    # AD-HOC: We add the in the below AD-HOC block to avoid an extra space if there is no font-family descriptor.
    ak.string_builder_append(&builder, "@font-face {")

    # AD-HOC: We don't necessary always have a font-family descriptor as the spec assumes,
    #         see https://github.com/w3c/csswg-drafts/issues/13323

    if let font_family = css_descriptors_descriptor(descriptors, css_descriptor_name_and_id_from_id(css_data.DescriptorId.font_family)):
        ak.string_builder_append_u8(&builder, ' ')

        # 2. The string "font-family:", followed by a single SPACE (U+0020).
        ak.string_builder_append(&builder, "font-family: ")

        # 3. The result of performing serialize a string on the rule’s font family name.
        css_style_value_serialize(font_family, &builder, css_syntax.SerializationMode.normal)

        # 4. The string ";", i.e., SEMICOLON (U+003B).
        ak.string_builder_append_u8(&builder, ';')

    # 5. If the rule’s associated source list is not empty, follow these substeps:
    if let sources = css_descriptors_descriptor(descriptors, css_descriptor_name_and_id_from_id(css_data.DescriptorId.src)):
        # 1. A single SPACE (U+0020), followed by the string "src:", followed by a single SPACE (U+0020).
        ak.string_builder_append(&builder, " src: ")

        # 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.
        css_style_value_serialize(sources, &builder, css_syntax.SerializationMode.normal)

        # 3. The string ";", i.e., SEMICOLON (U+003B).
        ak.string_builder_append_u8(&builder, ';')

    # 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).
    css_font_face_rule_serialize_descriptor(&builder, descriptors, css_data.DescriptorId.unicode_range, " unicode-range: ")

    # FIXME: 7. If rule’s associated font-variant descriptor is present, a single SPACE (U+0020),
    # followed by the string "font-variant:", followed by a single SPACE (U+0020),
    # followed by the result of performing serialize a <'font-variant'>,
    # followed by the string ";", i.e., SEMICOLON (U+003B).

    # 8. If rule’s associated font-feature-settings descriptor is present, a single SPACE (U+0020),
    #    followed by the string "font-feature-settings:", followed by a single SPACE (U+0020),
    #    followed by the result of performing serialize a <'font-feature-settings'>,
    #    followed by the string ";", i.e., SEMICOLON (U+003B).
    css_font_face_rule_serialize_descriptor(&builder, descriptors, css_data.DescriptorId.font_feature_settings, " font-feature-settings: ")

    # 9. If rule’s associated font-stretch descriptor is present, a single SPACE (U+0020),
    #    followed by the string "font-stretch:", followed by a single SPACE (U+0020),
    #    followed by the result of performing serialize a <'font-stretch'>,
    #    followed by the string ";", i.e., SEMICOLON (U+003B).
    # NOTE: font-stretch is now an alias for font-width, so we use that instead.
    css_font_face_rule_serialize_descriptor(&builder, descriptors, css_data.DescriptorId.font_width, " font-stretch: ")

    # 10. If rule’s associated font-weight descriptor is present, a single SPACE (U+0020),
    #     followed by the string "font-weight:", followed by a single SPACE (U+0020),
    #     followed by the result of performing serialize a <'font-weight'>,
    #     followed by the string ";", i.e., SEMICOLON (U+003B).
    css_font_face_rule_serialize_descriptor(&builder, descriptors, css_data.DescriptorId.font_weight, " font-weight: ")

    # 11. If rule’s associated font-style descriptor is present, a single SPACE (U+0020),
    #     followed by the string "font-style:", followed by a single SPACE (U+0020),
    #     followed by the result of performing serialize a <'font-style'>,
    #     followed by the string ";", i.e., SEMICOLON (U+003B).
    css_font_face_rule_serialize_descriptor(&builder, descriptors, css_data.DescriptorId.font_style, " font-style: ")

    # 12. A single SPACE (U+0020), followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D).
    ak.string_builder_append(&builder, " }")

    return ak.string_builder_to_string(&builder) else trap("MUST: StringBuilder::to_string")

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:157 Web::CSS::CSSFontFaceRule::visit_edges (override).
pub func css_font_face_rule_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (CssFontFaceRule*)base
    css_rule_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_style)
    gc.cell_visitor_visit_t(visitor, this.m_css_connected_font_face)

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:163 Web::CSS::CSSFontFaceRule::handle_descriptor_change.
pub func css_font_face_rule_handle_descriptor_change(this: CssFontFaceRule*, property: ak.FlyString):
    if this.m_css_connected_font_face == none:
        return

    if not css_font_face_rule_is_valid(this):
        css_font_face_rule_disconnect_font_face(this)
        return

    if ak.fly_string_equals_ignoring_ascii_case_string_view(&property, ak.sv("src")):
        css_font_face_rule_handle_src_descriptor_change(this)

    # https://drafts.csswg.org/css-font-loading/#font-face-css-connection
    # any change made to a @font-face descriptor is immediately reflected in the corresponding FontFace attribute
    css_font_face_reparse_connected_css_font_face_rule_descriptors(this.m_css_connected_font_face else trap("null m_css_connected_font_face"))

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:182 Web::CSS::CSSFontFaceRule::handle_src_descriptor_change.
# https://drafts.csswg.org/css-font-loading/#font-face-css-connection
func css_font_face_rule_handle_src_descriptor_change(this: CssFontFaceRule*):
    # If a @font-face rule has its src descriptor changed to a new value, the original connected FontFace object must
    # stop being CSS-connected. A new FontFace reflecting its new src must be created and CSS-connected to the
    # @font-face.

    if this.m_css_connected_font_face == none:
        return

    css_font_face_rule_disconnect_font_face(this)

    let style_sheet = css_rule_parent_style_sheet(this.css_rule()) else return

    let document = css_css_style_sheet_owning_document(style_sheet) else return

    let new_font_face = css_font_face_create_css_connected(bind_platform_object_realm(this.bind_platform_object()), this)
    css_font_face_set_add_css_connected_font(dom_document_fonts(document), new_font_face)

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:204 Web::CSS::CSSFontFaceRule::disconnect_font_face.
pub func css_font_face_rule_disconnect_font_face(this: CssFontFaceRule*):
    let font_face = this.m_css_connected_font_face else return

    css_font_face_disconnect_from_css_rule(font_face)

    if let style_sheet = css_rule_parent_style_sheet(this.css_rule()):
        if let document = css_css_style_sheet_owning_document(style_sheet):
            _ = css_font_face_set_delete(dom_document_fonts(document), font_face)

    this.m_css_connected_font_face = none

## Ported from Libraries/LibWeb/CSS/CSSFontFaceRule.cpp:219 Web::CSS::CSSFontFaceRule::dump (override).
pub func css_font_face_rule_dump_impl(base: CssRule*, builder: ak.StringBuilder*, indent_levels: i32):
    let this = (CssFontFaceRule*)base
    css_rule_dump_impl(base, builder, indent_levels)

    dump_indent(builder, indent_levels + 1)
    dump_appendff(builder, "Valid: {}\n", [ak.arg_bool(css_font_face_rule_is_valid(this))])
    dump_descriptors(builder, css_font_face_rule_descriptors_const(this).css_descriptors(), indent_levels + 1)