src/atlas.c
 1#include "internal.h"
 2
 3typedef struct
 4{
 5    uint32_t character;
 6    FT_Long glyph_index;
 7} Glyph;
 8
 9uint64_t hash_glyph(const void* item, const uint64_t seed0, const uint64_t seed1)
10{
11    return hashmap_murmur(&((Glyph*)item)->character, sizeof(FT_Long), seed0, seed1);
12}
13
14int compare_glyph(const void* va, const void* vb, void* udata)
15{
16    const Glyph* a = va;
17    const Glyph* b = vb;
18    return a->character - b->character;
19}
20
21Atlas atlas_new(const uint32_t capacity)
22{
23    Atlas atlas = {0};
24    atlas.glyphs = hashmap_new(sizeof(Glyph), capacity, 0, 0, hash_glyph, compare_glyph, NULL, NULL);
25    if (capacity != 0)
26    {
27        atlas_reserve(&atlas, capacity);
28    }
29    return atlas;
30}
31
32void atlas_reserve(Atlas* atlas, const uint32_t capacity)
33{
34    dynarray_reserve(&atlas->glyph_sizes, capacity * 4);
35}
36
37void atlas_append(Atlas* atlas, const FT_Face face, const uint32_t character)
38{
39    const FT_Long glyph_index = FT_Load_Char(face, character, FT_LOAD_DEFAULT);
40
41    const Glyph glyph = {.glyph_index = glyph_index};
42    const uint32_t hash = hash_glyph(&glyph, 0, 0); // Execute hash only once
43    if (hashmap_get_with_hash(atlas->glyphs, &glyph, hash) == NULL)
44    {
45        hashmap_set_with_hash(atlas->glyphs, &glyph, hash);
46        atlas->dirty = 1;
47    }
48}
49
50void atlas_generate_image(const Atlas* atlas)
51{
52    if (atlas->dirty == 0) return;
53
54    size_t iter = 0;
55    Glyph* glyph;
56    while (hashmap_iter(atlas->glyphs, &iter, (void**)&glyph))
57    {
58    }
59}
60
61void atlas_release(const Atlas* atlas)
62{
63    hashmap_free(atlas->glyphs);
64    dynarray_release(&atlas->glyph_sizes);
65}