//
// Created by vfs on 1/29/26.
//

#ifndef GAME_FONT_H
#define GAME_FONT_H

#include <cstdint>
#include <unordered_map>
#include <vector>
#include <string>

#include <freetype/freetype.h>
#include <raylib.h>
#include <kb_text_shape.h>

extern FT_Library ft_lib;
extern Shader shader;

extern const int32_t ATLAS_SIZE;

struct Glyph {
    uint32_t index;
    int16_t x, y;
    int32_t width, height;
    float dpi_width, dpi_height;
    int32_t bearing_x, bearing_y;
    float dpi_bearing_x, dpi_bearing_y;
    int32_t descent, ascent;
    float dpi_descent, dpi_ascent;
    Vector2 st0, st1;
};

struct MyFont {
    FT_Face face;
    std::unordered_map<uint32_t, Glyph> glyphs;
    Texture2D texture{};
    std::vector<uint8_t> kb_file_data;
    kbts_font kbts{};

    MyFont(const std::string& filename, int size);
    void deinit();

    void render_text(std::string_view text, Vector2 pos, Color color);

    struct size_ret {
        Vector2 size;
        float max_descent;
        float ascent;
    };

    size_ret size_row(std::string_view str, int32_t n, float max_width);
};

int32_t my_kbts_ShapeRun(kbts_shape_context *Context, kbts_run *Run);

#endif //GAME_FONT_H