Repository: LadybirdBrowser/ladybird
File: Libraries/LibGfx/TextLayout.cpp (C++)

Code before:
        auto code_point = *it;
        auto const* font = &font_cascade_list.font_for_code_point(code_point);
        if (font != last_font) {
            auto substring = string.substring_view(substring_begin_offset, string.iterator_offset(it) - substring_begin_offset);
            add_run(substring, *last_font);
            last_font = font;
            substring_begin_offset = string.iterator_offset(it);
        }
        ++it;
    }

    auto end_offset = string.iterator_offset(it);
    if (substring_begin_offset < end_offset) {
        auto substring = string.substring_view(substring_begin_offset, end_offset - substring_begin_offset);
        add_run(substring, *last_font);
    }

    return runs;
}

static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, ShapeFeatures const& features)
{
    static hb_buffer_t* buffer = hb_buffer_create();
    hb_buffer_reset(buffer);

    if (string.has_ascii_storage())
        hb_buffer_add_utf8(buffer, string.ascii_span().data(), string.length_in_code_units(), 0, -1);
    else
        hb_buffer_add_utf16(buffer, reinterpret_cast<u16 const*>(string.utf16_span().data()), string.length_in_code_units(), 0, -1);

    hb_buffer_guess_segment_properties(buffer);

    auto* hb_font = font.harfbuzz_font();
    hb_feature_t const* hb_features_data = nullptr;
    Vector<hb_feature_t> hb_features;
    if (!features.is_empty()) {
        hb_features.ensure_capacity(features.size());
        for (auto const& feature : features) {
            hb_features.unchecked_append({
                .tag = HB_TAG(feature.tag[0], feature.tag[1], feature.tag[2], feature.tag[3]),
                .value = feature.value,
                .start = 0,
                .end = HB_FEATURE_GLOBAL_END,
            });
        }
        hb_features_data = hb_features.data();
    }

    hb_shape(hb_font, buffer, hb_features_data, features.size());

    return buffer;

Review comment (suggestion):
You might want to use [`hb_buffer_add_latin1()`](https://harfbuzz.github.io/harfbuzz-hb-buffer.html#hb-buffer-add-latin1) here.

Code after:
        auto code_point = *it;
        auto const* font = &font_cascade_list.font_for_code_point(code_point);
        if (font != last_font) {
            auto substring = string.substring_view(substring_begin_offset, string.iterator_offset(it) - substring_begin_offset);
            add_run(substring, *last_font);
            last_font = font;
            substring_begin_offset = string.iterator_offset(it);
        }
        ++it;
    }

    auto end_offset = string.iterator_offset(it);
    if (substring_begin_offset < end_offset) {
        auto substring = string.substring_view(substring_begin_offset, end_offset - substring_begin_offset);
        add_run(substring, *last_font);
    }

    return runs;
}

static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, ShapeFeatures const& features)
{
    hb_buffer_t* buffer = hb_buffer_create();

    if (string.has_ascii_storage())
        hb_buffer_add_utf8(buffer, string.ascii_span().data(), string.length_in_code_units(), 0, -1);
    else
        hb_buffer_add_utf16(buffer, reinterpret_cast<u16 const*>(string.utf16_span().data()), string.length_in_code_units(), 0, -1);

    hb_buffer_guess_segment_properties(buffer);

    auto* hb_font = font.harfbuzz_font();
    hb_feature_t const* hb_features_data = nullptr;
    Vector<hb_feature_t, 4> hb_features;
    if (!features.is_empty()) {
        hb_features.ensure_capacity(features.size());
        for (auto const& feature : features) {
            hb_features.unchecked_append({
                .tag = HB_TAG(feature.tag[0], feature.tag[1], feature.tag[2], feature.tag[3]),
                .value = feature.value,
                .start = 0,
                .end = HB_FEATURE_GLOBAL_END,
            });
        }
        hb_features_data = hb_features.data();
    }

    hb_shape(hb_font, buffer, hb_features_data, features.size());

    return buffer;
}