Coverage Report

Created: 2025-11-16 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibGfx/Font/Font.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
3
 * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#pragma once
9
10
#include <AK/Bitmap.h>
11
#include <AK/ByteReader.h>
12
#include <AK/RefCounted.h>
13
#include <AK/RefPtr.h>
14
#include <AK/String.h>
15
#include <AK/Types.h>
16
#include <LibCore/MappedFile.h>
17
#include <LibGfx/Bitmap.h>
18
#include <LibGfx/Size.h>
19
20
namespace Gfx {
21
22
// FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
23
class GlyphBitmap {
24
public:
25
0
    GlyphBitmap() = default;
26
    GlyphBitmap(Bytes rows, IntSize size)
27
0
        : m_rows(rows)
28
0
        , m_size(size)
29
0
    {
30
0
    }
31
32
0
    unsigned row(unsigned index) const { return ByteReader::load32(bitmap(index).data()); }
33
34
0
    bool bit_at(int x, int y) const { return bitmap(y).get(x); }
35
0
    void set_bit_at(int x, int y, bool b) { bitmap(y).set(x, b); }
36
37
0
    IntSize size() const { return m_size; }
38
0
    int width() const { return m_size.width(); }
39
0
    int height() const { return m_size.height(); }
40
41
0
    static constexpr size_t bytes_per_row() { return sizeof(u32); }
42
0
    static constexpr int max_width() { return bytes_per_row() * 8; }
43
0
    static constexpr int max_height() { return max_width() + bytes_per_row(); }
44
45
private:
46
    AK::Bitmap bitmap(size_t y) const
47
0
    {
48
0
        return { const_cast<u8*>(m_rows.offset_pointer(bytes_per_row() * y)), bytes_per_row() * 8 };
49
0
    }
50
51
    Bytes m_rows;
52
    IntSize m_size { 0, 0 };
53
};
54
55
class Glyph {
56
public:
57
    Glyph(GlyphBitmap const& glyph_bitmap, float left_bearing, float advance, float ascent)
58
0
        : m_glyph_bitmap(glyph_bitmap)
59
0
        , m_left_bearing(left_bearing)
60
0
        , m_advance(advance)
61
0
        , m_ascent(ascent)
62
0
    {
63
0
    }
64
65
    Glyph(RefPtr<Bitmap> bitmap, float left_bearing, float advance, float ascent, bool is_color_bitmap)
66
0
        : m_bitmap(bitmap)
67
0
        , m_left_bearing(left_bearing)
68
0
        , m_advance(advance)
69
0
        , m_ascent(ascent)
70
0
        , m_color_bitmap(is_color_bitmap)
71
0
    {
72
0
    }
73
74
0
    bool is_color_bitmap() const { return m_color_bitmap; }
75
76
0
    bool is_glyph_bitmap() const { return !m_bitmap; }
77
0
    GlyphBitmap glyph_bitmap() const { return m_glyph_bitmap; }
78
0
    RefPtr<Bitmap> bitmap() const { return m_bitmap; }
79
0
    float left_bearing() const { return m_left_bearing; }
80
0
    float advance() const { return m_advance; }
81
0
    float ascent() const { return m_ascent; }
82
83
private:
84
    GlyphBitmap m_glyph_bitmap;
85
    RefPtr<Bitmap> m_bitmap;
86
    float m_left_bearing;
87
    float m_advance;
88
    float m_ascent;
89
    bool m_color_bitmap { false };
90
};
91
92
struct GlyphSubpixelOffset {
93
    u8 x;
94
    u8 y;
95
96
    // TODO: Allow setting this at runtime via some config?
97
0
    static constexpr int subpixel_divisions() { return 3; }
98
0
    FloatPoint to_float_point() const { return FloatPoint(x / float(subpixel_divisions()), y / float(subpixel_divisions())); }
99
100
0
    bool operator==(GlyphSubpixelOffset const&) const = default;
101
};
102
103
struct GlyphRasterPosition {
104
    // Where the glyph bitmap should be drawn/blitted.
105
    IntPoint blit_position;
106
107
    // A subpixel offset to be used when rendering the glyph.
108
    // This improves kerning and alignment at the expense of caching a few extra bitmaps.
109
    // This is (currently) snapped to thirds of a subpixel (i.e. 0, 0.33, 0.66).
110
    GlyphSubpixelOffset subpixel_offset;
111
112
    static GlyphRasterPosition get_nearest_fit_for(FloatPoint position);
113
};
114
115
struct FontPixelMetrics {
116
    float size { 0 };
117
    float x_height { 0 };
118
    float advance_of_ascii_zero { 0 };
119
    float glyph_spacing { 0 };
120
121
    // Number of pixels the font extends above the baseline.
122
    float ascent { 0 };
123
124
    // Number of pixels the font descends below the baseline.
125
    float descent { 0 };
126
127
    // Line gap specified by font.
128
    float line_gap { 0 };
129
130
0
    float line_spacing() const { return ascent + descent + line_gap; }
131
};
132
133
// https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
134
enum FontWidth {
135
    UltraCondensed = 1,
136
    ExtraCondensed = 2,
137
    Condensed = 3,
138
    SemiCondensed = 4,
139
    Normal = 5,
140
    SemiExpanded = 6,
141
    Expanded = 7,
142
    ExtraExpanded = 8,
143
    UltraExpanded = 9
144
};
145
146
class Font : public RefCounted<Font> {
147
public:
148
    enum class AllowInexactSizeMatch {
149
        No,
150
        Yes,
151
        Larger,
152
        Smaller,
153
    };
154
155
    virtual NonnullRefPtr<Font> clone() const = 0;
156
    virtual ErrorOr<NonnullRefPtr<Font>> try_clone() const = 0;
157
0
    virtual ~Font() = default;
158
159
    virtual FontPixelMetrics pixel_metrics() const = 0;
160
161
    virtual u8 presentation_size() const = 0;
162
    virtual u8 slope() const = 0;
163
164
    // Font point size (distance between ascender and descender).
165
    virtual float point_size() const = 0;
166
167
    // Font pixel size (distance between ascender and descender).
168
    virtual float pixel_size() const = 0;
169
170
    // Font pixel size, rounded up to the nearest integer.
171
    virtual int pixel_size_rounded_up() const = 0;
172
173
    virtual u16 width() const = 0;
174
175
    virtual u16 weight() const = 0;
176
    virtual Optional<Glyph> glyph(u32 code_point) const = 0;
177
    virtual Optional<Glyph> glyph(u32 code_point, GlyphSubpixelOffset) const = 0;
178
    virtual Optional<Glyph> glyph_for_postscript_name(StringView, GlyphSubpixelOffset) const = 0;
179
    virtual bool contains_glyph(u32 code_point) const = 0;
180
    virtual bool contains_glyph_for_postscript_name(StringView name) const = 0;
181
182
    virtual float glyph_left_bearing(u32 code_point) const = 0;
183
    virtual Optional<float> glyph_left_bearing_for_postscript_name(StringView) const = 0;
184
    virtual float glyph_width(u32 code_point) const = 0;
185
    virtual Optional<float> glyph_width_for_postscript_name(StringView) const = 0;
186
    virtual float glyph_or_emoji_width(Utf8CodePointIterator&) const = 0;
187
    virtual float glyph_or_emoji_width(Utf32CodePointIterator&) const = 0;
188
    virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const = 0;
189
    virtual int x_height() const = 0;
190
    virtual float preferred_line_height() const = 0;
191
192
    virtual u8 min_glyph_width() const = 0;
193
    virtual u8 max_glyph_width() const = 0;
194
    virtual u8 glyph_fixed_width() const = 0;
195
196
    virtual u8 baseline() const = 0;
197
    virtual u8 mean_line() const = 0;
198
199
    virtual float width(StringView) const = 0;
200
    virtual float width(Utf8View const&) const = 0;
201
    virtual float width(Utf32View const&) const = 0;
202
203
    virtual int width_rounded_up(StringView) const = 0;
204
205
    virtual String name() const = 0;
206
207
    virtual bool is_fixed_width() const = 0;
208
209
    virtual u8 glyph_spacing() const = 0;
210
211
    virtual size_t glyph_count() const = 0;
212
213
    virtual String family() const = 0;
214
    virtual String variant() const = 0;
215
216
    virtual String qualified_name() const = 0;
217
    virtual String human_readable_name() const = 0;
218
219
    virtual NonnullRefPtr<Font> with_size(float point_size) const = 0;
220
221
    Font const& bold_variant() const;
222
223
    virtual bool has_color_bitmaps() const = 0;
224
225
private:
226
    mutable RefPtr<Gfx::Font const> m_bold_variant;
227
};
228
229
}