sfntest8.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * sfntest8.c
  3. *
  4. * Copyright (C) 2022 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief rendering Scalable Screen Font off-screen into a buffer and save it to an image file
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #define SSFN_IMPLEMENTATION
  33. #include "ssfn.h"
  34. /**
  35. * Load a font
  36. */
  37. ssfn_font_t *load_file(char *filename, int *size)
  38. {
  39. char *fontdata = NULL;
  40. FILE *f;
  41. f = fopen(filename, "rb");
  42. if(!f) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  43. *size = 0;
  44. fseek(f, 0, SEEK_END);
  45. *size = (int)ftell(f);
  46. fseek(f, 0, SEEK_SET);
  47. if(!*size) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  48. fontdata = (char*)malloc(*size);
  49. if(!fontdata) { fprintf(stderr,"memory allocation error\n"); exit(2); }
  50. fread(fontdata, *size, 1, f);
  51. fclose(f);
  52. return (ssfn_font_t*)fontdata;
  53. }
  54. /**
  55. * Save a buffer into an image file
  56. */
  57. void dump_buffer(ssfn_buf_t *buffer)
  58. {
  59. unsigned char tmp[18] = { 0 };
  60. FILE *f;
  61. f = fopen("buffer.tga", "wb");
  62. /* write the header */
  63. tmp[2] = 2;
  64. *((uint16_t*)(tmp + 10)) = *((uint16_t*)(tmp + 12)) = buffer->w;
  65. *((uint16_t*)(tmp + 14)) = buffer->h;
  66. tmp[16] = 4 * 8;
  67. tmp[17] = 40;
  68. fwrite(tmp, 18, 1, f);
  69. /* write the data */
  70. fwrite(buffer->ptr, buffer->w * 4 * buffer->h, 1, f);
  71. fclose(f);
  72. }
  73. /**
  74. * testing the SSFN library (normal renderer)
  75. */
  76. void do_test(char *fontfn)
  77. {
  78. ssfn_t ctx;
  79. ssfn_font_t *font;
  80. ssfn_buf_t *buf;
  81. int ret, size;
  82. /* initialize the normal renderer */
  83. memset(&ctx, 0, sizeof(ssfn_t));
  84. /* load and select a font */
  85. font = load_file(fontfn ? fontfn : "../fonts/FreeSerif.sfn", &size);
  86. ret = ssfn_load(&ctx, font);
  87. if(ret != SSFN_OK) { fprintf(stderr, "ssfn load error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  88. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR, 16);
  89. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  90. /* generate a buffer and save it to file */
  91. buf = ssfn_text(&ctx, "Lorem ipsum dolor sic amet", 0xffffffff);
  92. if(!buf || !buf->ptr) { fprintf(stderr, "ssfn text: no buffer returned\n"); exit(2); }
  93. printf("Buffer allocated: width %d height %d left %d top %d\n", buf->w, buf->h, buf->x, buf->y);
  94. dump_buffer(buf);
  95. free(buf->ptr);
  96. free(buf);
  97. ssfn_free(&ctx);
  98. free(font);
  99. }
  100. /**
  101. * Main procedure
  102. */
  103. int main(int argc __attribute__((unused)), char **argv)
  104. {
  105. do_test(argv[1]);
  106. return 0;
  107. }