File size: 10,139 Bytes
336a9be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "gfx/font_engine.hpp"

#include <algorithm>
#include <cmath>
#include <cstring>
#include <dlfcn.h>

namespace astra {

namespace {

// Verified x86_64 FreeType 2 ABI offsets on libfreetype.so.6
constexpr size_t kFtFaceGlyphOffset = 152;
constexpr size_t kFtSlotAdvanceXOffset = 128;
constexpr size_t kFtSlotFormatOffset = 144;
constexpr size_t kFtSlotBitmapRowsOffset = 152;
constexpr size_t kFtSlotBitmapWidthOffset = 156;
constexpr size_t kFtSlotBitmapPitchOffset = 160;
constexpr size_t kFtSlotBitmapBufferOffset = 168;
constexpr size_t kFtSlotPixelModeOffset = 178;
constexpr size_t kFtSlotBitmapLeftOffset = 192;
constexpr size_t kFtSlotBitmapTopOffset = 196;

constexpr uint32_t kFtGlyphFormatBitmap = 0x62697473u; // 'bits'
constexpr int kFtLoadRender = 4;
constexpr uint8_t kFtPixelModeGray = 2;

template <typename T>
T read_at(const void* base, size_t byte_offset) {
    T v{};
    std::memcpy(&v, static_cast<const uint8_t*>(base) + byte_offset, sizeof(T));
    return v;
}

void* try_load_face(void* lib,
                    int (*ft_new_face)(void*, const char*, long, void**),
                    const std::vector<const char*>& candidates) {
    for (const char* path : candidates) {
        void* face = nullptr;
        if (ft_new_face(lib, path, 0, &face) == 0 && face != nullptr) {
            return face;
        }
    }
    return nullptr;
}

int fallback_glyph_scale(float font_size) {
    if (font_size >= 26.0f) return 3;
    if (font_size >= 18.0f) return 2;
    return 1;
}

} // namespace

FontEngine& FontEngine::instance() {
    static FontEngine eng;
    return eng;
}

FontEngine::FontEngine() {
    dl_handle_ = dlopen("libfreetype.so.6", RTLD_LAZY);
    if (!dl_handle_) dl_handle_ = dlopen("libfreetype.so", RTLD_LAZY);
    if (!dl_handle_) return;

    ft_init_ = reinterpret_cast<FT_Init_FreeType_fn>(dlsym(dl_handle_, "FT_Init_FreeType"));
    ft_done_ = reinterpret_cast<FT_Done_FreeType_fn>(dlsym(dl_handle_, "FT_Done_FreeType"));
    ft_new_face_ = reinterpret_cast<FT_New_Face_fn>(dlsym(dl_handle_, "FT_New_Face"));
    ft_done_face_ = reinterpret_cast<FT_Done_Face_fn>(dlsym(dl_handle_, "FT_Done_Face"));
    ft_set_pixel_sizes_ = reinterpret_cast<FT_Set_Pixel_Sizes_fn>(dlsym(dl_handle_, "FT_Set_Pixel_Sizes"));
    ft_load_char_ = reinterpret_cast<FT_Load_Char_fn>(dlsym(dl_handle_, "FT_Load_Char"));

    if (!ft_init_ || !ft_done_ || !ft_new_face_ || !ft_done_face_ ||
        !ft_set_pixel_sizes_ || !ft_load_char_) {
        return;
    }

    if (ft_init_(&ft_lib_) != 0 || !ft_lib_) return;

    face_sans_reg_ = try_load_face(ft_lib_, ft_new_face_, {
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
        "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
        "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf"
    });
    face_sans_bold_ = try_load_face(ft_lib_, ft_new_face_, {
        "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
        "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
        "/usr/share/fonts/truetype/noto/NotoSans-Bold.ttf"
    });
    face_mono_reg_ = try_load_face(ft_lib_, ft_new_face_, {
        "/usr/share/fonts/truetype/jetbrains-mono/JetBrainsMono-Regular.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
    });
    face_mono_bold_ = try_load_face(ft_lib_, ft_new_face_, {
        "/usr/share/fonts/truetype/jetbrains-mono/JetBrainsMono-Bold.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf"
    });

    if (!face_sans_reg_) return;

    // Runtime ABI sanity check on 'A' at 16px to guarantee safe glyph slot layout
    if (ft_set_pixel_sizes_(face_sans_reg_, 0, 16) == 0 &&
        ft_load_char_(face_sans_reg_, 'A', kFtLoadRender) == 0) {
        const void* slot = read_at<const void*>(face_sans_reg_, kFtFaceGlyphOffset);
        if (slot) {
            uint32_t fmt = read_at<uint32_t>(slot, kFtSlotFormatOffset);
            uint8_t pmode = read_at<uint8_t>(slot, kFtSlotPixelModeOffset);
            uint32_t rows = read_at<uint32_t>(slot, kFtSlotBitmapRowsOffset);
            uint32_t width = read_at<uint32_t>(slot, kFtSlotBitmapWidthOffset);
            if (fmt == kFtGlyphFormatBitmap && pmode == kFtPixelModeGray &&
                rows >= 6 && rows <= 32 && width >= 4 && width <= 32) {
                active_ = true;
            }
        }
    }
}

FontEngine::~FontEngine() {
    if (face_sans_reg_ && ft_done_face_) ft_done_face_(face_sans_reg_);
    if (face_sans_bold_ && ft_done_face_) ft_done_face_(face_sans_bold_);
    if (face_mono_reg_ && ft_done_face_) ft_done_face_(face_mono_reg_);
    if (face_mono_bold_ && ft_done_face_) ft_done_face_(face_mono_bold_);
    if (ft_lib_ && ft_done_) ft_done_(ft_lib_);
    if (dl_handle_) dlclose(dl_handle_);
}

uint32_t FontEngine::next_utf8_codepoint(std::string_view s, size_t& index) {
    if (index >= s.size()) return 0;
    uint8_t c0 = static_cast<uint8_t>(s[index++]);
    if (c0 < 0x80) return c0;
    if ((c0 & 0xE0) == 0xC0 && index < s.size()) {
        uint8_t c1 = static_cast<uint8_t>(s[index++]);
        return ((c0 & 0x1Fu) << 6) | (c1 & 0x3Fu);
    }
    if ((c0 & 0xF0) == 0xE0 && index + 1 < s.size()) {
        uint8_t c1 = static_cast<uint8_t>(s[index++]);
        uint8_t c2 = static_cast<uint8_t>(s[index++]);
        return ((c0 & 0x0Fu) << 12) | ((c1 & 0x3Fu) << 6) | (c2 & 0x3Fu);
    }
    if ((c0 & 0xF8) == 0xF0 && index + 2 < s.size()) {
        uint8_t c1 = static_cast<uint8_t>(s[index++]);
        uint8_t c2 = static_cast<uint8_t>(s[index++]);
        uint8_t c3 = static_cast<uint8_t>(s[index++]);
        return ((c0 & 0x07u) << 18) | ((c1 & 0x3Fu) << 12) | ((c2 & 0x3Fu) << 6) | (c3 & 0x3Fu);
    }
    return '?';
}

void FontEngine::evict_if_needed_locked() {
    while (cache_bytes_ > kMaxCacheBytes && !cache_.empty()) {
        auto oldest = cache_.begin();
        for (auto it = cache_.begin(); it != cache_.end(); ++it) {
            if (it->second.last_used_tick < oldest->second.last_used_tick) {
                oldest = it;
            }
        }
        cache_bytes_ -= oldest->second.alpha.size() + sizeof(CachedGlyph);
        cache_.erase(oldest);
    }
}

const CachedGlyph* FontEngine::get_glyph(uint32_t codepoint, int pixel_size, bool bold, bool mono) {
    if (!active_) return nullptr;
    int px = std::clamp(pixel_size, 8, 72);
    uint8_t sbits = static_cast<uint8_t>((bold ? 1 : 0) | (mono ? 2 : 0));
    GlyphKey key{codepoint, static_cast<uint16_t>(px), sbits};

    std::lock_guard<std::mutex> lk(mu_);
    auto it = cache_.find(key);
    if (it != cache_.end()) {
        it->second.last_used_tick = ++tick_;
        return &it->second;
    }

    void* face = nullptr;
    if (mono) {
        face = bold ? (face_mono_bold_ ? face_mono_bold_ : face_mono_reg_)
                    : (face_mono_reg_ ? face_mono_reg_ : face_sans_reg_);
    } else {
        face = bold ? (face_sans_bold_ ? face_sans_bold_ : face_sans_reg_)
                    : face_sans_reg_;
    }
    if (!face) face = face_sans_reg_;

    if (ft_set_pixel_sizes_(face, 0, static_cast<unsigned int>(px)) != 0) return nullptr;
    if (ft_load_char_(face, codepoint, kFtLoadRender) != 0) return nullptr;

    const void* slot = read_at<const void*>(face, kFtFaceGlyphOffset);
    if (!slot) return nullptr;

    uint32_t fmt = read_at<uint32_t>(slot, kFtSlotFormatOffset);
    if (fmt != kFtGlyphFormatBitmap) return nullptr;

    uint32_t rows = read_at<uint32_t>(slot, kFtSlotBitmapRowsOffset);
    uint32_t width = read_at<uint32_t>(slot, kFtSlotBitmapWidthOffset);
    int32_t pitch = read_at<int32_t>(slot, kFtSlotBitmapPitchOffset);
    const uint8_t* buf = read_at<const uint8_t*>(slot, kFtSlotBitmapBufferOffset);
    int32_t bleft = read_at<int32_t>(slot, kFtSlotBitmapLeftOffset);
    int32_t btop = read_at<int32_t>(slot, kFtSlotBitmapTopOffset);
    int64_t adv_x_26_6 = read_at<int64_t>(slot, kFtSlotAdvanceXOffset);

    CachedGlyph g;
    g.width = static_cast<int>(width);
    g.height = static_cast<int>(rows);
    g.bearing_x = bleft;
    g.bearing_y = btop;
    g.advance_x = std::max(1, static_cast<int>((adv_x_26_6 + 32) >> 6));
    g.last_used_tick = ++tick_;

    if (buf && width > 0 && rows > 0 && width <= 256 && rows <= 256) {
        g.alpha.resize(static_cast<size_t>(width) * rows, 0);
        int abs_pitch = std::abs(pitch);
        for (uint32_t r = 0; r < rows; ++r) {
            const uint8_t* src_row = (pitch >= 0)
                                         ? (buf + static_cast<size_t>(r) * abs_pitch)
                                         : (buf + static_cast<size_t>(rows - 1 - r) * abs_pitch);
            std::memcpy(g.alpha.data() + static_cast<size_t>(r) * width, src_row, width);
        }
    }

    cache_bytes_ += g.alpha.size() + sizeof(CachedGlyph);
    auto [ins_it, ok] = cache_.emplace(key, std::move(g));
    evict_if_needed_locked();
    return &ins_it->second;
}

float FontEngine::measure_text(std::string_view utf8, float font_size, bool bold, bool mono) {
    if (!active_) {
        int scale = fallback_glyph_scale(font_size);
        float adv = static_cast<float>(scale * 8 + (bold ? 1 : 0));
        return static_cast<float>(utf8.size()) * adv;
    }
    int px = static_cast<int>(std::round(std::clamp(font_size, 8.0f, 72.0f)));
    float total = 0.0f;
    size_t idx = 0;
    while (idx < utf8.size()) {
        uint32_t cp = next_utf8_codepoint(utf8, idx);
        const CachedGlyph* g = get_glyph(cp, px, bold, mono);
        if (g) total += static_cast<float>(g->advance_x);
        else total += static_cast<float>(px) * 0.5f;
    }
    return total;
}

float FontEngine::line_height(float font_size) const {
    if (!active_) {
        int scale = fallback_glyph_scale(font_size);
        return static_cast<float>(scale * 8) * 1.65f;
    }
    return std::ceil(std::clamp(font_size, 8.0f, 72.0f) * 1.48f);
}

size_t FontEngine::cache_bytes() const {
    std::lock_guard<std::mutex> lk(mu_);
    return cache_bytes_;
}

size_t FontEngine::cached_glyph_count() const {
    std::lock_guard<std::mutex> lk(mu_);
    return cache_.size();
}

} // namespace astra