Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/CSS/StyleComputer.cpp (C++)

Code before:

RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, int slope, double font_weight, Percentage const& font_width, HashMap<FlyString, NumberOrCalculated> font_variation_settings, Length::ResolutionContext const& length_resolution_context) const
{
    // FIXME: We round to int here as that is what is expected by our font infrastructure below
    auto width = round_to<int>(font_width.value());

    // FIXME: We round to int here as that is what is expected by our font infrastructure below
    auto weight = round_to<int>(font_weight);

    // FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm

    float const font_size_in_pt = font_size * 0.75f;

    auto find_font = [&](FlyString const& family) -> RefPtr<Gfx::FontCascadeList const> {
        FontFaceKey key {
            .family_name = family,
            .weight = weight,
            .slope = slope,
        };
        auto result = Gfx::FontCascadeList::create();
        if (auto it = m_loaded_fonts.find(key); it != m_loaded_fonts.end()) {
            auto const& loaders = it->value;

            Gfx::FontVariationSettings variation;
            variation.set_weight(font_weight);
            variation.set_width(font_width.value());

            CalculationResolutionContext context {
                .length_resolution_context = length_resolution_context,
            };

            for (auto const& [tag_string, value] : font_variation_settings) {
                auto string_view = tag_string.bytes_as_string_view();
                if (string_view.length() != 4)
                    continue;
                auto tag = Gfx::FourCC(string_view.characters_without_null_termination());

                auto resolved_value = value.resolved(context);
                if (!resolved_value.has_value())
                    continue;

                variation.axes.set(tag, resolved_value.release_value());
            }

            for (auto const& loader : loaders) {
                if (auto found_font = loader->font_with_point_size(font_size_in_pt, variation))
                    result->add(*found_font, loader->unicode_ranges());
            }

            return result;
        }

Review comment (suggestion):
there’s `int` version of this, `width`, above (the `FIXME` comment above it might be somewhat misleading – ignore if not relevant).

Code after:
            generic_font = Platform::GenericFont::Cursive;
            break;
        case Keyword::UiSerif:
            generic_font = Platform::GenericFont::UiSerif;
            break;
        case Keyword::UiSansSerif:
            generic_font = Platform::GenericFont::UiSansSerif;
            break;
        case Keyword::UiRounded:
            generic_font = Platform::GenericFont::UiRounded;
            break;
        default:
            return {};
        }
        return find_font(Platform::FontPlugin::the().generic_font_name(generic_font));
    };

    auto font_list = Gfx::FontCascadeList::create();
    if (font_family.is_value_list()) {
        auto const& family_list = static_cast<StyleValueList const&>(font_family).values();
        for (auto const& family : family_list) {
            RefPtr<Gfx::FontCascadeList const> other_font_list;
            if (family->is_keyword()) {
                other_font_list = find_generic_font(family->to_keyword());
            } else if (family->is_string()) {
                other_font_list = find_font(family->as_string().string_value());
            } else if (family->is_custom_ident()) {
                other_font_list = find_font(family->as_custom_ident().custom_ident());
            }
            if (other_font_list)
                font_list->extend(*other_font_list);
        }
    } else if (font_family.is_keyword()) {
        if (auto other_font_list = find_generic_font(font_family.to_keyword()))
            font_list->extend(*other_font_list);
    } else if (font_family.is_string()) {
        if (auto other_font_list = find_font(font_family.as_string().string_value()))
            font_list->extend(*other_font_list);
    } else if (font_family.is_custom_ident()) {
        if (auto other_font_list = find_font(font_family.as_custom_ident().custom_ident()))
            font_list->extend(*other_font_list);
    }

    auto default_font = Platform::FontPlugin::the().default_font(font_size_in_pt);
    if (font_list->is_empty()) {
        // This is needed to make sure we check default font before reaching to emojis.
        font_list->add(*default_font);
    }

    if (auto emoji_font = Platform::FontPlugin::the().default_emoji_font(font_size_in_pt); emoji_font) {
        font_list->add(*emoji_font);