summaryrefslogtreecommitdiff
path: root/Tests/LibGfx/TestFontHandling.cpp
blob: e2917f038c23b644989cd080df5fad0f27d6d856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2020, the SerenityOS developers.
 * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGfx/Font/BitmapFont.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibTest/TestCase.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

TEST_CASE(test_fontdatabase_get_by_name)
{
    auto name = "Liza 10 400 0"sv;
    auto& font_database = Gfx::FontDatabase::the();
    EXPECT(!font_database.get_by_name(name)->name().is_null());
}

TEST_CASE(test_fontdatabase_get)
{
    auto& font_database = Gfx::FontDatabase::the();
    EXPECT(!font_database.get("Liza", 10, 400, 0)->name().is_null());
}

TEST_CASE(test_fontdatabase_for_each_font)
{
    auto& font_database = Gfx::FontDatabase::the();
    font_database.for_each_font([&](Gfx::Font const& font) {
        EXPECT(!font.name().is_null());
        EXPECT(!font.qualified_name().is_null());
        EXPECT(!font.family().is_null());
        EXPECT(font.glyph_count() > 0);
    });
}

TEST_CASE(test_clone)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    auto new_font = font->clone();
    EXPECT(!new_font->name().is_null());
    EXPECT(!new_font->qualified_name().is_null());
    EXPECT(!new_font->family().is_null());
    EXPECT(new_font->glyph_count() > 0);
}

TEST_CASE(test_set_name)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    auto name = "my newly created font"sv;
    font->set_name(name);

    EXPECT(!font->name().is_null());
    EXPECT(font->name().contains(name));
}

TEST_CASE(test_set_family)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    auto family = "my newly created font family"sv;
    font->set_family(family);

    EXPECT(!font->family().is_null());
    EXPECT(font->family().contains(family));
}

TEST_CASE(test_set_glyph_width)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    size_t ch = 123;
    font->set_glyph_width(ch, glyph_width);

    EXPECT(font->glyph_width(ch) == glyph_width);
}

TEST_CASE(test_set_glyph_spacing)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    u8 glyph_spacing = 8;
    font->set_glyph_spacing(glyph_spacing);

    EXPECT(font->glyph_spacing() == glyph_spacing);
}

TEST_CASE(test_width)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    EXPECT(font->width("A"sv) == glyph_width);
}

TEST_CASE(test_glyph_or_emoji_width)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    EXPECT(font->glyph_or_emoji_width(0));
}

TEST_CASE(test_load_from_file)
{
    auto font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonBold14.font");
    EXPECT(!font->name().is_null());
}

TEST_CASE(test_write_to_file)
{
    u8 glyph_height = 1;
    u8 glyph_width = 1;
    auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);

    char path[] = "/tmp/new.font.XXXXXX";
    EXPECT(mkstemp(path) != -1);
    EXPECT(!font->write_to_file(path).is_error());
    unlink(path);
}

TEST_CASE(test_character_set_masking)
{
    auto font = Gfx::BitmapFont::try_load_from_file("/usr/Tests/LibGfx/TestFont.font");
    EXPECT(!font.is_error());

    auto unmasked_font = font.value()->unmasked_character_set();
    EXPECT(!unmasked_font.is_error());
    EXPECT(unmasked_font.value()->glyph_index(0x0041).value() == 0x0041);
    EXPECT(unmasked_font.value()->glyph_index(0x0100).value() == 0x0100);
    EXPECT(unmasked_font.value()->glyph_index(0xFFFD).value() == 0xFFFD);

    auto masked_font = unmasked_font.value()->masked_character_set();
    EXPECT(!masked_font.is_error());
    EXPECT(masked_font.value()->glyph_index(0x0041).value() == 0x0041);
    EXPECT(!masked_font.value()->glyph_index(0x0100).has_value());
    EXPECT(masked_font.value()->glyph_index(0xFFFD).value() == 0x1FD);
}