/*
 * The part of the FTGL API that Chromium B.S.U. uses (FTBufferFont: load a TrueType font, set its size,
 * render a string at the origin and measure it), implemented with stb_truetype for the fredygames.com
 * build, which has no FreeType or FTGL. Text is drawn as textured quads through the game's own OpenGL
 * calls, one baked glyph atlas per font. See ftgl-stb.cpp.
 *
 * Written in 2026 by FredyGames for the fredygames.com build of Chromium B.S.U.; distributed with the
 * game under the Clarified Artistic License (see COPYING).
 */
#ifndef FTGL_STB_H_
#define FTGL_STB_H_

// The real FTGL headers bring these in, and TextFTGL.cpp counts on them (strdup, free, fprintf).
#include <cstdio>
#include <cstdlib>
#include <cstring>

class FTFont {
public:
  virtual ~FTFont();
  int Error() const { return error; }
  bool FaceSize(unsigned int size);
  void Render(const char *str, int len = -1);
  float Advance(const char *str, int len = -1);
  float LineHeight() const;

protected:
  explicit FTFont(const char *path);

private:
  int error;
  unsigned int size;
  unsigned char *fontData;
  void *glyphs;  // stbtt_bakedchar[96]
  unsigned char *atlas;  // glyph coverage, until it becomes the texture
  unsigned int texture;
  float lineHeight;  // at the baking size
  void upload();
};

class FTBufferFont : public FTFont {
public:
  explicit FTBufferFont(const char *path) : FTFont(path) {}
};

#endif
