sfntest5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * sfntest5.c
  3. *
  4. * Copyright (C) 2019 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 testing Scalable Screen Font bounding box
  27. *
  28. */
  29. #include <stdio.h>
  30. #define SSFN_IMPLEMENTATION
  31. #include "../ssfn.h"
  32. #include <SDL.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(SDL_Surface *screen, char *fontfn, int argc)
  57. {
  58. SDL_Rect box;
  59. /* start test string with "f", because in FreeSerifI.sfn it has a tail that overlaps with the prev glyph */
  60. /* end with "F" because when italic is calculated, it has a top right pixel */
  61. char *s, *str0 = "f Checking the Bounding Box F";
  62. int ret, size, l, t, boxcol = 0xFFD0D0D0, leftcol = 0xFFA0A0A0;
  63. ssfn_t ctx;
  64. ssfn_font_t *font;
  65. ssfn_buf_t buf;
  66. /* initialize the normal renderer */
  67. memset(&ctx, 0, sizeof(ssfn_t));
  68. buf.ptr = (uint8_t*)screen->pixels;
  69. buf.p = screen->pitch;
  70. buf.w = screen->w;
  71. buf.h = screen->h;
  72. buf.fg = 0xFF202020;
  73. buf.bg = 0;
  74. /* load and select a font */
  75. font = load_file(fontfn ? fontfn : "../fonts/FreeSerif.sfn", &size);
  76. ret = ssfn_load(&ctx, font);
  77. if(ret != SSFN_OK) { fprintf(stderr, "ssfn load error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  78. /* size 8 */
  79. buf.y = 10;
  80. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 8);
  81. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  82. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  83. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  84. box.x = 30 - l; box.y = buf.y - t;
  85. SDL_FillRect(screen, &box, leftcol);
  86. box.x = 30; box.y = buf.y - t;
  87. SDL_FillRect(screen, &box, boxcol);
  88. s = str0;
  89. buf.x = 30;
  90. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  91. s += ret;
  92. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  93. buf.y = 70;
  94. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_BOLD | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 8);
  95. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  96. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  97. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  98. box.x = 30 - l; box.y = buf.y - t;
  99. SDL_FillRect(screen, &box, leftcol);
  100. box.x = 30; box.y = buf.y - t;
  101. SDL_FillRect(screen, &box, boxcol);
  102. s = str0;
  103. buf.x = 30;
  104. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  105. s += ret;
  106. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  107. buf.y = 130;
  108. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_ITALIC | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 8);
  109. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  110. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  111. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  112. box.x = 30 - l; box.y = buf.y - t;
  113. SDL_FillRect(screen, &box, leftcol);
  114. box.x = 30; box.y = buf.y - t;
  115. SDL_FillRect(screen, &box, boxcol);
  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)); exit(2); }
  121. /* size 16 */
  122. buf.y = 210;
  123. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 16);
  124. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  125. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  126. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  127. box.x = 30 - l; box.y = buf.y - t;
  128. SDL_FillRect(screen, &box, leftcol);
  129. box.x = 30; box.y = buf.y - t;
  130. SDL_FillRect(screen, &box, boxcol);
  131. s = str0;
  132. buf.x = 30;
  133. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  134. s += ret;
  135. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  136. buf.y = 270;
  137. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_BOLD | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 16);
  138. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  139. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  140. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  141. box.x = 30 - l; box.y = buf.y - t;
  142. SDL_FillRect(screen, &box, leftcol);
  143. box.x = 30; box.y = buf.y - t;
  144. SDL_FillRect(screen, &box, boxcol);
  145. s = str0;
  146. buf.x = 30;
  147. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  148. s += ret;
  149. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  150. buf.y = 330;
  151. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_ITALIC | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 16);
  152. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  153. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  154. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  155. box.x = 30 - l; box.y = buf.y - t;
  156. SDL_FillRect(screen, &box, leftcol);
  157. box.x = 30; box.y = buf.y - t;
  158. SDL_FillRect(screen, &box, boxcol);
  159. s = str0;
  160. buf.x = 30;
  161. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  162. s += ret;
  163. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  164. /* size 24 */
  165. buf.y = 410;
  166. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 24);
  167. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  168. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  169. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  170. box.x = 30 - l; box.y = buf.y - t;
  171. SDL_FillRect(screen, &box, leftcol);
  172. box.x = 30; box.y = buf.y - t;
  173. SDL_FillRect(screen, &box, boxcol);
  174. s = str0;
  175. buf.x = 30;
  176. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  177. s += ret;
  178. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  179. buf.y = 470;
  180. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_BOLD | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 24);
  181. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  182. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  183. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  184. box.x = 30 - l; box.y = buf.y - t;
  185. SDL_FillRect(screen, &box, leftcol);
  186. box.x = 30; box.y = buf.y - t;
  187. SDL_FillRect(screen, &box, boxcol);
  188. s = str0;
  189. buf.x = 30;
  190. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  191. s += ret;
  192. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  193. buf.y = 530;
  194. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_ITALIC | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), 24);
  195. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  196. ssfn_bbox(&ctx, str0, &box.w, &box.h, &l, &t);
  197. printf("w %d h %d left %d top %d\n", box.w, box.h, l, t);
  198. box.x = 30 - l; box.y = buf.y - t;
  199. SDL_FillRect(screen, &box, leftcol);
  200. box.x = 30; box.y = buf.y - t;
  201. SDL_FillRect(screen, &box, boxcol);
  202. s = str0;
  203. buf.x = 30;
  204. while((ret = ssfn_render(&ctx, &buf, s)) > 0)
  205. s += ret;
  206. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  207. printf("Memory allocated: %d\n", ssfn_mem(&ctx));
  208. ssfn_free(&ctx);
  209. free(font);
  210. }
  211. /**
  212. * Main procedure
  213. */
  214. int main(int argc __attribute__((unused)), char **argv)
  215. {
  216. SDL_Window *window;
  217. SDL_Surface *screen;
  218. SDL_Event event;
  219. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS)) {
  220. fprintf(stderr,"SDL error %s\n", SDL_GetError());
  221. return 2;
  222. }
  223. window = SDL_CreateWindow("SSFN scaling test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
  224. screen = SDL_GetWindowSurface(window);
  225. memset(screen->pixels, 0xF8, screen->pitch*screen->h);
  226. do_test(screen, argv[1], argc);
  227. do{ SDL_UpdateWindowSurface(window); SDL_Delay(10); } while(SDL_WaitEvent(&event) && event.type != SDL_QUIT &&
  228. event.type != SDL_MOUSEBUTTONDOWN && event.type != SDL_KEYDOWN);
  229. SDL_DestroyWindow(window);
  230. SDL_Quit();
  231. return 0;
  232. }