sfntest6.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * sfntest6.c
  3. *
  4. * Copyright (C) 2021 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 for valgrind testing, rendering Scalable Screen Font off-screen
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define SSFN_IMPLEMENTATION
  32. #include "../ssfn.h"
  33. /**
  34. * Load a font
  35. */
  36. ssfn_font_t *load_file(char *filename, int *size)
  37. {
  38. char *fontdata = NULL;
  39. FILE *f;
  40. f = fopen(filename, "rb");
  41. if(!f) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  42. *size = 0;
  43. fseek(f, 0, SEEK_END);
  44. *size = (int)ftell(f);
  45. fseek(f, 0, SEEK_SET);
  46. if(!*size) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  47. fontdata = (char*)malloc(*size);
  48. if(!fontdata) { fprintf(stderr,"memory allocation error\n"); exit(2); }
  49. fread(fontdata, *size, 1, f);
  50. fclose(f);
  51. return (ssfn_font_t*)fontdata;
  52. }
  53. /**
  54. * testing the SSFN library (normal renderer)
  55. */
  56. void do_test(char *fontfn, int argc)
  57. {
  58. /* start test string with "f", because in FreeSerifI.sfn it has a tail that overlaps with the prev glyph */
  59. /* end with "F" because when italic is calculated, it has a top right pixel */
  60. char *s, *str0 = "f Checking the Bounding Box F";
  61. int ret, size, l, t, w, h;
  62. ssfn_t ctx;
  63. ssfn_font_t *font;
  64. ssfn_buf_t buf;
  65. /* initialize the normal renderer */
  66. memset(&ctx, 0, sizeof(ssfn_t));
  67. buf.p = 4096;
  68. buf.w = 1024;
  69. buf.h = 768;
  70. buf.fg = 0xFF202020;
  71. buf.bg = 0;
  72. buf.ptr = (uint8_t*)malloc(buf.p * buf.h);
  73. if(!buf.ptr) { fprintf(stderr, "unable to allocate off-screen buffer\r\n"); exit(1); }
  74. memset(buf.ptr, 0xF8, buf.p * buf.h);
  75. /* load and select a font */
  76. font = load_file(fontfn ? fontfn : "../fonts/FreeSerif.sfn", &size);
  77. ret = ssfn_load(&ctx, font);
  78. if(ret != SSFN_OK) { fprintf(stderr, "ssfn load error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  79. /* size 8 */
  80. buf.y = 10;
  81. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 8);
  82. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  83. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  84. printf("w %d h %d left %d top %d\n", w, h, l, t);
  85. s = str0;
  86. buf.x = 30;
  87. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  88. s += ret;
  89. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  90. buf.y = 70;
  91. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_BOLD | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 8);
  92. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  93. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  94. printf("w %d h %d left %d top %d\n", w, h, l, t);
  95. s = str0;
  96. buf.x = 30;
  97. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  98. s += ret;
  99. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  100. buf.y = 130;
  101. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_ITALIC | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 8);
  102. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  103. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  104. printf("w %d h %d left %d top %d\n", w, h, l, t);
  105. s = str0;
  106. buf.x = 30;
  107. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  108. s += ret;
  109. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  110. /* size 16 */
  111. buf.y = 210;
  112. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 16);
  113. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  114. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  115. printf("w %d h %d left %d top %d\n", w, h, l, t);
  116. s = str0;
  117. buf.x = 30;
  118. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  119. s += ret;
  120. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  121. buf.y = 270;
  122. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_BOLD | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 16);
  123. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  124. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  125. printf("w %d h %d left %d top %d\n", w, h, l, t);
  126. s = str0;
  127. buf.x = 30;
  128. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  129. s += ret;
  130. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  131. buf.y = 330;
  132. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_ITALIC | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 16);
  133. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  134. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  135. printf("w %d h %d left %d top %d\n", w, h, l, t);
  136. s = str0;
  137. buf.x = 30;
  138. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  139. s += ret;
  140. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  141. /* size 24 */
  142. buf.y = 410;
  143. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 24);
  144. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  145. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  146. printf("w %d h %d left %d top %d\n", w, h, l, t);
  147. s = str0;
  148. buf.x = 30;
  149. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  150. s += ret;
  151. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  152. buf.y = 470;
  153. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_BOLD | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 24);
  154. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  155. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  156. printf("w %d h %d left %d top %d\n", w, h, l, t);
  157. s = str0;
  158. buf.x = 30;
  159. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  160. s += ret;
  161. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  162. buf.y = 530;
  163. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_ITALIC | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 24);
  164. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  165. ssfn_bbox(&ctx, str0, &w, &h, &l, &t);
  166. printf("w %d h %d left %d top %d\n", w, h, l, t);
  167. s = str0;
  168. buf.x = 30;
  169. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  170. s += ret;
  171. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); free(buf.ptr); exit(2); }
  172. printf("Memory allocated: %d\n", ssfn_mem(&ctx));
  173. ssfn_free(&ctx);
  174. free(font);
  175. free(buf.ptr);
  176. }
  177. /**
  178. * Main procedure
  179. */
  180. int main(int argc __attribute__((unused)), char **argv)
  181. {
  182. do_test(argv[1], argc);
  183. return 0;
  184. }