Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-fonts

Source of release 0.7.2

0.7.2 / src / opentype / tests_units.lucb

#==============================================================================================
#
#   tests_units - the opentype tests that need no reference build: Ladybird's TestWOFF and
#                 TestWOFF2, FreeType's fixed-point arithmetic, bounded reads and List
#
#   DESCRIPTION:
#       Ported from Tests/LibGfx/TestWOFF.cpp and TestWOFF2.cpp (their inputs are in
#       tests/woff), with focused tests of the reader's helpers.
#
#==============================================================================================

# mark: Tests/LibGfx/TestWOFF.cpp and TestWOFF2.cpp ===============================================

## Whether `bytes` unwrap as a WOFF file whose sfnt opens.
func test_woff_loads(bytes: const u8[]) -> bool:
    let sfnt: u8[]? = woff_to_sfnt(bytes) catch:
        recover none
    let data = sfnt else return false
    let face: SfntFace*? = sfnt_face_create(data, 0) catch:
        recover none
    return face != none

## Whether `bytes` convert as a WOFF2 file.
func test_woff2_converts(bytes: const u8[]) -> bool:
    let sfnt: u8[]? = woff2_to_sfnt(bytes) catch:
        recover none
    return sfnt != none

test "malformed_woff":
    let test_inputs: str[1] = ["tests/woff/invalid_sfnt_size.woff"]
    for test_input in test_inputs:
        assert(not test_woff_loads(test_read_file(test_input)))

test "tolerate_incorrect_sfnt_size":
    # The header's totalSfntSize is wrong; WOFF2 decoders do not believe it.
    let sfnt = woff2_to_sfnt(test_read_file("tests/woff/incorrect_sfnt_size.woff2")) else trap("incorrect_sfnt_size.woff2 does not convert")
    let face = sfnt_face_create(sfnt, 0) else trap("incorrect_sfnt_size.woff2's sfnt does not open")
    assert(face.family_name == "Test")
    assert(face.num_glyphs == 4)
    sfnt_face_destroy(face)
    free(sfnt)

test "malformed_woff2":
    let test_inputs: str[2] = ["tests/woff/incorrect_compressed_size.woff2", "tests/woff/invalid_numtables.woff2"]
    for test_input in test_inputs:
        assert(not test_woff2_converts(test_read_file(test_input)))

test "woff_header_checks":
    ## WOFF's header checks, on hand-made headers.
    var header: u8[44]
    # A wrong signature.
    assert(not test_woff_loads(header))
    # 'wOFF' with no tables.
    header[0] = 'w'
    header[1] = 'O'
    header[2] = 'F'
    header[3] = 'F'
    header[11] = 44
    assert(not test_woff_loads(header))
    # Too short for a header.
    assert(not test_woff_loads(header[0..<20]))
    assert(woff_pow_2_less_than_or_equal(1) == 1)
    assert(woff_pow_2_less_than_or_equal(5) == 8)
    assert(woff_pow_2_less_than_or_equal(16) == 16)
    assert(woff_is_web_font(header))

test "woff_unwraps_hash_sans":
    let sfnt = woff_to_sfnt(test_read_file("tests/fonts/HashSans.woff")) else trap("HashSans.woff does not unwrap")
    let face = sfnt_face_create(sfnt, 0) else trap("HashSans.woff's sfnt does not open")
    assert(face.family_name == "HashSans")
    assert(face.outline_format != SfntOutlineFormat.none_)

# mark: The reader's helpers =====================================================================

test "fixed_point":
    # FreeType's arithmetic (ftcalc.c): FT_MulFix rounds halves away from zero.
    assert(ft_mul_fix(0x10000, 3) == 3)
    assert(ft_mul_fix(0x8000, 1) == 1)
    assert(ft_mul_fix(0x7FFF, 1) == 0)
    assert(ft_mul_fix(-0x7FFF, 1) == 0)
    assert(ft_mul_fix(-0x8000, 1) == -1)
    assert(ft_div_fix(1, 2) == 0x8000)
    assert(ft_div_fix(-1, 2) == -0x8000)
    assert(ft_div_fix(5, 0) == 0x7FFFFFFF)
    assert(ft_mul_div(7, 3, 2) == 11)
    assert(ft_mul_div(-7, 3, 2) == -11)
    assert(ft_pix_round(95) == 64)
    assert(ft_pix_round(96) == 128)
    assert(ft_pix_round(-33) == -64)
    assert(ft_pix_round(-32) == 0)

test "sfnt_reads":
    let data: u8[5] = [0x12, 0x34, 0x56, 0x78, 0x9A]
    assert(sfnt_u16(data, 0) == 0x1234)
    assert(sfnt_u24(data, 1) == 0x345678)
    assert(sfnt_u32(data, 1) == 0x3456789A)
    # Reads past the end answer 0 instead of trapping.
    assert(sfnt_u16(data, 4) == 0)
    assert(sfnt_u32(data, 2) == 0)
    assert(sfnt_u8(data, 9) == 0)
    assert(sfnt_i16(data, 3) == (i16)0x789A)
    assert(sfnt_slice(data, 3, 10).length == 2)
    assert(sfnt_slice(data, 7, 1).length == 0)
    assert(sfnt_tag('k', 'e', 'r', 'n') == 0x6B65726E)

test "a face that is not a font fails, and a face is destroyed with what it holds":
    let junk: u8[16] = [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    let failed: SfntFace*? = sfnt_face_create(junk, 0) catch failure:
        assert(failure.code == invalid_font)
        recover none
    assert(failed == none)
    with memory.heap:
        let data = test_read_file("tests/fonts/NotoSansTifinagh-Regular.otf")
        let face = sfnt_face_create(data, 0) else trap("the CFF test font does not open")
        assert(face.cff_font != none)
        sfnt_face_destroy(face)
        free(data)

test "a font's family name is read without opening the face":
    with memory.heap:
        for path in ["tests/fonts/NotoSansTifinagh-Regular.otf", "tests/fonts/Lato-Bold.ttf", "tests/fonts/SerenitySans-Regular.ttf"]:
            let data = test_read_file(path)
            let face = sfnt_face_create(data, 0) else trap("a test font does not open")
            let name = sfnt_family_name(data, 0) else trap("a test font's family name does not read")
            assert(name == face.family_name and name.length > 0)
            free(name.bytes)
            sfnt_face_destroy(face)
            free(data)
    let junk: u8[16] = [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    let failed: str? = sfnt_family_name(junk, 0) catch:
        recover none
    assert(failed == none)

test "lists grow, insert, remove and resize":
    var list: List[u32]
    for value in 0..<(u32)20:
        list.push(value)
    assert(list.length() == 20 and *list.at(19) == 19)
    list.insert(0, 100)
    list.remove(1)
    assert(*list.at(0) == 100 and *list.at(1) == 1 and *list.last() == 19)
    list.resize(25)
    assert(list.length() == 25 and *list.at(24) == 0)
    var copy = list.clone()
    copy.push(7)
    assert(copy.length() == 26 and list.length() == 25)
    copy.release()
    list.clear()
    assert(list.length() == 0)
    list.release()