font.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifndef __EACSMB_font_h__
  2. #define __EACSMB_font_h__
  3. #include <stdatomic.h>
  4. #include "c3dlas.h"
  5. #include "ds.h"
  6. #include "hash.h"
  7. #include <ft2build.h>
  8. #include FT_FREETYPE_H
  9. struct charInfo {
  10. uint32_t code;
  11. // final output texture coordinates
  12. int texIndex;
  13. Vector2i texelOffset; // from the top left
  14. Vector2i texelSize; // size of the character data in texels
  15. Vector2 texNormOffset; // normalized texture coordinates
  16. Vector2 texNormSize;
  17. // typographic info
  18. float advance; // horizonatal distance to advance after this char
  19. Vector2 topLeftOffset; // offset from the baseline to the top left vertex of the quad
  20. Vector2 size;
  21. };
  22. typedef struct GUIFont {
  23. char* name;
  24. char hasRegular;
  25. char hasItalic;
  26. char hasBold;
  27. char hasBoldItalic;
  28. int charsLen;
  29. struct charInfo* regular;
  30. struct charInfo* italic;
  31. struct charInfo* bold;
  32. struct charInfo* boldItalic;
  33. int size;
  34. // TODO: kerning info
  35. } GUIFont;
  36. // generation info for a single character
  37. typedef struct FontGen {
  38. GUIFont* font;
  39. uint32_t code;
  40. char italic;
  41. char bold;
  42. // the raw glyph is oversampled by FontManager.oversample times
  43. int oversample;
  44. int magnitude;
  45. // metrics for the raw glyph, in pixels
  46. uint8_t* rawGlyph;
  47. Vector2i rawGlyphSize; // size of the raw bitmap
  48. Vector rawBearing; // distance from the origin to the top left corner of the glyph
  49. float rawAdvance; // horizontal advance, in pixels
  50. // the sdf is smaller than the raw glyph
  51. // metrics for the sdf glyph, in pixels
  52. uint8_t* sdfGlyph;
  53. Vector2i sdfGlyphSize; // size of the sdf bitmap
  54. AABB2 sdfBounds; // bounding box of the non-empty data in the sdf bitmap, in pixels
  55. Vector2i sdfDataSize; // size of the non-empty sdf data in the bitmap
  56. Vector sdfBearing; // distance from the origin to the top left corner of the clipped sdf data
  57. float sdfAdvance; // horizontal advance, in pixels
  58. // final texture data
  59. struct charInfo charinfo;
  60. } FontGen;
  61. typedef struct FontManager {
  62. VEC(GUIFont*) fonts;
  63. FT_Face fallback;
  64. // SDF generation
  65. VEC(FontGen*) gen;
  66. atomic_int genCounter;
  67. // SDF config
  68. int oversample;
  69. int magnitude;
  70. int maxAtlasSize;
  71. VEC(uint8_t*) atlas;
  72. uint32_t atlasSize;
  73. char* pngFileFormat;
  74. int maxThreads;
  75. int verbose;
  76. } FontManager;
  77. void FontManager_createAtlas(FontManager* fm);
  78. void FontManager_saveAtlas(FontManager* fm, char* path);
  79. int FontManager_loadAtlas(FontManager* fm, char* path);
  80. void FontManager_addFont2(FontManager* fm, char* name, uint32_t* charset, int size, char bold, char italic);
  81. void FontManager_finalize(FontManager* fm);
  82. void FontManager_saveJSON(FontManager* fm, char* path);
  83. GUIFont* FontManager_findFont(FontManager* fm, char* name);
  84. FontManager* FontManager_alloc();
  85. void FontManager_init(FontManager* fm);
  86. static size_t u32strlen(const uint32_t* const s) {
  87. size_t l = 0;
  88. while(s[l] != 0) l++;
  89. return l;
  90. }
  91. #endif // __EACSMB_font_h__