font.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <SDL2/SDL_ttf.h>
  2. #include <switch.h>
  3. #include "gfx/font.h"
  4. font_t FONT_TEXT[FONT_MAX];
  5. font_t FONT_BUTTON[FONT_MAX];
  6. font_t create_font_file(const char *file, int font_size)
  7. {
  8. font_t font;
  9. font.fnt = TTF_OpenFontRW(SDL_RWFromFile(file, "rb"), 1, font_size);
  10. return font;
  11. }
  12. font_t create_font_mem(void *mem, int mem_size, int font_size)
  13. {
  14. font_t font;
  15. font.fnt = TTF_OpenFontRW(SDL_RWFromMem(mem, mem_size), 1, font_size);
  16. return font;
  17. }
  18. void free_font(font_t *font)
  19. {
  20. TTF_CloseFont(font->fnt);
  21. }
  22. bool init_font()
  23. {
  24. if (R_FAILED(plInitialize(PlServiceType_User)))
  25. {
  26. return false;
  27. }
  28. PlFontData font = {0};
  29. PlFontData button = {0};
  30. if (R_FAILED(plGetSharedFontByType(&font, PlSharedFontType_Standard)))
  31. {
  32. return false;
  33. }
  34. if (R_FAILED(plGetSharedFontByType(&button, PlSharedFontType_NintendoExt)))
  35. {
  36. return false;
  37. }
  38. int font_sizes[] = { 15, 18, 20, 23, 25, 28, 30, 33, 35, 45, 48, 60, 63, 72, 170 };
  39. for (uint8_t i = 0; i < FONT_MAX; i++)
  40. {
  41. FONT_BUTTON[i] = create_font_mem(button.address, button.size, font_sizes[i]);
  42. FONT_TEXT[i] = create_font_mem(font.address, font.size, font_sizes[i]);
  43. }
  44. return true;
  45. }
  46. void exit_font()
  47. {
  48. for (uint8_t i = 0; i < FONT_MAX; i++)
  49. {
  50. free_font(&FONT_TEXT[i]);
  51. free_font(&FONT_BUTTON[i]);
  52. }
  53. plExit();
  54. }