psf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-25 GuEe-GUI the first version
  9. */
  10. #include "psf.h"
  11. static void psf_read_header(struct psf_font *psf)
  12. {
  13. if (psf->header1->magic[0] == PSF1_MAGIC0 &&
  14. psf->header1->magic[1] == PSF1_MAGIC1)
  15. {
  16. psf->font_map = (void *)&psf->header1[1];
  17. psf->type = PSF_TYPE_1;
  18. if (psf->header1->mode & PSF1_MODE512)
  19. {
  20. psf->count = 512;
  21. }
  22. else
  23. {
  24. psf->count = 256;
  25. }
  26. psf->size = psf->header1->charsize;
  27. psf->height = psf->header1->charsize;
  28. psf->width = 8;
  29. }
  30. else if (psf->header2->magic[0] == PSF2_MAGIC0 &&
  31. psf->header2->magic[1] == PSF2_MAGIC1 &&
  32. psf->header2->magic[2] == PSF2_MAGIC2 &&
  33. psf->header2->magic[3] == PSF2_MAGIC3)
  34. {
  35. psf->font_map = (void *)&psf->header2[1];
  36. psf->type = PSF_TYPE_2;
  37. psf->count = psf->header2->length;
  38. psf->size = psf->header2->charsize;
  39. psf->height = psf->header2->height;
  40. psf->width = psf->header2->width;
  41. }
  42. else
  43. {
  44. psf->type = PSF_TYPE_UNKNOW;
  45. }
  46. psf->glyph = psf->height * psf->width;
  47. }
  48. rt_err_t psf_initialize(const void *psf_data, struct psf_font *out_psf)
  49. {
  50. struct psf_font *psf = out_psf;
  51. psf->raw_data = psf_data;
  52. psf_read_header(psf);
  53. if (psf->type == PSF_TYPE_UNKNOW)
  54. {
  55. return -RT_ENOSYS;
  56. }
  57. return RT_EOK;
  58. }
  59. rt_err_t psf_parse(struct psf_font *psf, const rt_uint8_t *font_data,
  60. rt_uint8_t *tmpglyph, rt_uint32_t color_size,
  61. rt_uint32_t foreground, rt_uint32_t background)
  62. {
  63. rt_uint8_t *font = (void *)font_data, *map = (void *)psf->font_map;
  64. psf->font_data = font_data;
  65. for (int n = 0; n < psf->count; ++n, map += psf->size)
  66. {
  67. rt_memcpy(tmpglyph, map, psf->size);
  68. for (int i = 0; i < psf->size; ++i)
  69. {
  70. for (int j = 0; j < 8; ++j)
  71. {
  72. if (i % (psf->size / psf->height) * 8 + j < psf->width)
  73. {
  74. if (tmpglyph[i] & 0x80)
  75. {
  76. rt_memcpy(font, &foreground, color_size);
  77. }
  78. else
  79. {
  80. rt_memcpy(font, &background, color_size);
  81. }
  82. font += color_size;
  83. }
  84. tmpglyph[i] = tmpglyph[i] << 1;
  85. }
  86. }
  87. }
  88. return RT_EOK;
  89. }