Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.1

0.6.1 / src / web_fonts / tests_support.lucb

#==============================================================================================
#
#   tests_support - helpers of the web_fonts tests: loading the test fonts and checking the
#                   module's answers against the reference build's
#
#   DESCRIPTION:
#       The test fonts live in tests/web_fonts/fonts (see NOTICE there); `luce-base test`
#       runs from the repository root (test.sh). tests_oracle.lucb holds the expectations,
#       printed by a local program over the reference build's LibGfx (Skia's FreeType
#       backend with the FontConfig font manager, as Ladybird's test mode forces, and
#       HarfBuzz; DESIGN.md ยง6.7).
#
#==============================================================================================

## The directory of the test fonts, relative to the repository root.
let test_fonts_directory: str = "tests/web_fonts/fonts/"

## A typeface loaded once per test run.
struct TestLoadedTypeface:
    var name: str
    var typeface: Typeface*

var test_loaded_typefaces: ak.Vector[TestLoadedTypeface]

## Reads a test file into a ByteBuffer.
func test_read_file(relative_path: str) -> ak.ByteBuffer:
    return path_font_provider_read_file(path_font_provider_c_string(relative_path)) else trap("cannot read a test file")

## The path of the test font `name`, in managed memory.
func test_font_path(name: str) -> str:
    let bytes = new u8[test_fonts_directory.length + name.length] else trap("out of memory")
    memory.copy(bytes, test_fonts_directory.bytes, test_fonts_directory.length)
    memory.copy(bytes[test_fonts_directory.length..], name.bytes, name.length)
    return (str)bytes

## The typeface of the test font `name` (a file of tests/web_fonts/fonts).
func test_typeface(name: str) -> Typeface*:
    for loaded in ak.vector_span_const(&test_loaded_typefaces):
        if loaded.name == name:
            return loaded.typeface
    let path = test_font_path(name)
    let font_data = font_data_create_from_byte_buffer(test_read_file(path))
    let typeface = typeface_try_load_from_font_data(font_data, 0) else trap("cannot load a test font")
    ak.vector_append_t(&test_loaded_typefaces, TestLoadedTypeface(name = name, typeface = typeface))
    return typeface

## A Utf16String's view.
func test_utf16(text: str) -> ak.Utf16String:
    return ak.utf16_string_from_str(text)

## The text of a FlyString.
func test_fly_text(string: ak.FlyString) -> str:
    return ak.string_view_text(ak.fly_string_bytes_as_string_view(&string))

## Asserts that two floats are the same float (bit for bit), naming what differs.
func test_expect_bits(what: str, actual: f32, expected_bits: u32):
    if actual.bits() != expected_bits:
        print(f"{what}: got {actual} ({actual.bits():x}), expected {f32.bits(expected_bits)} ({expected_bits:x})")
        assert(false, "a float differs from the reference build")

# mark: Test bodies =================================================================================

## GlyphRun::ensure_text_blob and cached_blob_bounds (Skia's conservative text-blob bounds).
func test_body_blob_bounds(file: str, point_size: f32, text: str, scale: f32, bounds: u32[4]):
    # The test fonts and what is made from them outlive the test (the typeface cache),
    # so they come from the C heap, not the per-test buffer.
    with memory.heap:
        let font = typeface_font(test_typeface(file), point_size)
        let string = test_utf16(text)
        let run = shape_text_f32(gfx.FloatPoint(m_x = 3.5, m_y = 20.25), 0.0, ak.utf16_string_base_utf16_view(&string.utf16_string_base), font, GlyphRunTextType.common)
        glyph_run_ensure_text_blob(run, scale)
        let actual = glyph_run_cached_blob_bounds(run)
        test_expect_bits("blob x", actual.m_location.m_x, bounds[0])
        test_expect_bits("blob y", actual.m_location.m_y, bounds[1])
        test_expect_bits("blob width", actual.m_size.m_width, bounds[2])
        test_expect_bits("blob height", actual.m_size.m_height, bounds[3])
        assert(glyph_run_cached_skia_text_blob(run) != none)

## GlyphRun::get_glyph_intercepts for the band 14..16 (times the scale): the port solves
## curves by subdivision, so the intervals match Skia's to a small tolerance.
func test_body_intercepts(file: str, point_size: f32, text: str, scale: f32, expected: const f32[]):
    # The test fonts and what is made from them outlive the test (the typeface cache),
    # so they come from the C heap, not the per-test buffer.
    with memory.heap:
        let font = typeface_font(test_typeface(file), point_size)
        let string = test_utf16(text)
        let run = shape_text_f32(gfx.FloatPoint(m_x = 3.5, m_y = 20.25), 0.0, ak.utf16_string_base_utf16_view(&string.utf16_string_base), font, GlyphRunTextType.common)
        let intercepts = glyph_run_get_glyph_intercepts(run, scale, 14.0 * scale, 16.0 * scale)
        let actual = ak.vector_span_const(&intercepts)
        if actual.length != expected.length:
            print(f"{text}: {actual.length} intercepts, expected {expected.length}")
        assert(actual.length == expected.length)
        for (index, value) in actual.indexed():
            let difference = value - expected[index]
            if difference > 0.05 or difference < -0.05:
                print(f"{text} intercept {index}: {value}, expected {expected[index]}")
            assert(difference <= 0.05 and difference >= -0.05)