showfont.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. showfont: An example of using the SDL_ttf library with 2D graphics.
  3. Copyright (C) 2001-2018 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* A simple program to test the text rendering feature of the TTF library */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "SDL.h"
  23. #include "SDL_ttf.h"
  24. #define DEFAULT_PTSIZE 18
  25. #define DEFAULT_TEXT "The quick brown fox jumped over the lazy dog"
  26. #define NUM_COLORS 256
  27. #define WIDTH 640
  28. #define HEIGHT 480
  29. static char *Usage =
  30. "Usage: %s [-solid] [-shaded] [-blended] [-utf8|-unicode] [-b] [-i] [-u] [-s] [-outline size] [-hintlight|-hintmono|-hintnone] [-nokerning] [-fgcol r,g,b,a] [-bgcol r,g,b,a] <font>.ttf [ptsize] [text]\n";
  31. typedef enum
  32. {
  33. TextRenderSolid,
  34. TextRenderShaded,
  35. TextRenderBlended
  36. } TextRenderMethod;
  37. typedef struct {
  38. SDL_Texture *caption;
  39. SDL_Rect captionRect;
  40. SDL_Texture *message;
  41. SDL_Rect messageRect;
  42. } Scene;
  43. static void draw_scene(SDL_Renderer *renderer, Scene *scene)
  44. {
  45. /* Clear the background to background color */
  46. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  47. SDL_RenderClear(renderer);
  48. SDL_RenderCopy(renderer, scene->caption, NULL, &scene->captionRect);
  49. SDL_RenderCopy(renderer, scene->message, NULL, &scene->messageRect);
  50. SDL_RenderPresent(renderer);
  51. }
  52. static void cleanup(int exitcode)
  53. {
  54. TTF_Quit();
  55. SDL_Quit();
  56. exit(exitcode);
  57. }
  58. int main(int argc, char *argv[])
  59. {
  60. char *argv0 = argv[0];
  61. SDL_Window *window;
  62. SDL_Renderer *renderer;
  63. TTF_Font *font;
  64. SDL_Surface *text;
  65. Scene scene;
  66. int ptsize;
  67. int i, done;
  68. SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
  69. SDL_Color black = { 0x00, 0x00, 0x00, 0 };
  70. SDL_Color *forecol;
  71. SDL_Color *backcol;
  72. SDL_Event event;
  73. TextRenderMethod rendermethod;
  74. int renderstyle;
  75. int outline;
  76. int hinting;
  77. int kerning;
  78. int dump;
  79. enum {
  80. RENDER_LATIN1,
  81. RENDER_UTF8,
  82. RENDER_UNICODE
  83. } rendertype;
  84. char *message, string[128];
  85. /* Look for special execution mode */
  86. dump = 0;
  87. /* Look for special rendering types */
  88. rendermethod = TextRenderShaded;
  89. renderstyle = TTF_STYLE_NORMAL;
  90. rendertype = RENDER_LATIN1;
  91. outline = 0;
  92. hinting = TTF_HINTING_NORMAL;
  93. kerning = 1;
  94. /* Default is black and white */
  95. forecol = &black;
  96. backcol = &white;
  97. for (i=1; argv[i] && argv[i][0] == '-'; ++i) {
  98. if (strcmp(argv[i], "-solid") == 0) {
  99. rendermethod = TextRenderSolid;
  100. } else
  101. if (strcmp(argv[i], "-shaded") == 0) {
  102. rendermethod = TextRenderShaded;
  103. } else
  104. if (strcmp(argv[i], "-blended") == 0) {
  105. rendermethod = TextRenderBlended;
  106. } else
  107. if (strcmp(argv[i], "-utf8") == 0) {
  108. rendertype = RENDER_UTF8;
  109. } else
  110. if (strcmp(argv[i], "-unicode") == 0) {
  111. rendertype = RENDER_UNICODE;
  112. } else
  113. if (strcmp(argv[i], "-b") == 0) {
  114. renderstyle |= TTF_STYLE_BOLD;
  115. } else
  116. if (strcmp(argv[i], "-i") == 0) {
  117. renderstyle |= TTF_STYLE_ITALIC;
  118. } else
  119. if (strcmp(argv[i], "-u") == 0) {
  120. renderstyle |= TTF_STYLE_UNDERLINE;
  121. } else
  122. if (strcmp(argv[i], "-s") == 0) {
  123. renderstyle |= TTF_STYLE_STRIKETHROUGH;
  124. } else
  125. if (strcmp(argv[i], "-outline") == 0) {
  126. if (sscanf (argv[++i], "%d", &outline) != 1) {
  127. fprintf(stderr, Usage, argv0);
  128. return(1);
  129. }
  130. } else
  131. if (strcmp(argv[i], "-hintlight") == 0) {
  132. hinting = TTF_HINTING_LIGHT;
  133. } else
  134. if (strcmp(argv[i], "-hintmono") == 0) {
  135. hinting = TTF_HINTING_MONO;
  136. } else
  137. if (strcmp(argv[i], "-hintnone") == 0) {
  138. hinting = TTF_HINTING_NONE;
  139. } else
  140. if (strcmp(argv[i], "-nokerning") == 0) {
  141. kerning = 0;
  142. } else
  143. if (strcmp(argv[i], "-dump") == 0) {
  144. dump = 1;
  145. } else
  146. if (strcmp(argv[i], "-fgcol") == 0) {
  147. int r, g, b, a = 0xFF;
  148. if (sscanf (argv[++i], "%d,%d,%d,%d", &r, &g, &b, &a) < 3) {
  149. fprintf(stderr, Usage, argv0);
  150. return(1);
  151. }
  152. forecol->r = (Uint8)r;
  153. forecol->g = (Uint8)g;
  154. forecol->b = (Uint8)b;
  155. forecol->a = (Uint8)a;
  156. } else
  157. if (strcmp(argv[i], "-bgcol") == 0) {
  158. int r, g, b, a = 0xFF;
  159. if (sscanf (argv[++i], "%d,%d,%d,%d", &r, &g, &b, &a) < 3) {
  160. fprintf(stderr, Usage, argv0);
  161. return(1);
  162. }
  163. backcol->r = (Uint8)r;
  164. backcol->g = (Uint8)g;
  165. backcol->b = (Uint8)b;
  166. backcol->a = (Uint8)a;
  167. } else {
  168. fprintf(stderr, Usage, argv0);
  169. return(1);
  170. }
  171. }
  172. argv += i;
  173. argc -= i;
  174. /* Check usage */
  175. if (!argv[0]) {
  176. fprintf(stderr, Usage, argv0);
  177. return(1);
  178. }
  179. /* Initialize the TTF library */
  180. if (TTF_Init() < 0) {
  181. fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
  182. SDL_Quit();
  183. return(2);
  184. }
  185. /* Open the font file with the requested point size */
  186. ptsize = 0;
  187. if (argc > 1) {
  188. ptsize = atoi(argv[1]);
  189. }
  190. if (ptsize == 0) {
  191. i = 2;
  192. ptsize = DEFAULT_PTSIZE;
  193. } else {
  194. i = 3;
  195. }
  196. font = TTF_OpenFont(argv[0], ptsize);
  197. if (font == NULL) {
  198. fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",
  199. ptsize, argv[0], SDL_GetError());
  200. cleanup(2);
  201. }
  202. TTF_SetFontStyle(font, renderstyle);
  203. TTF_SetFontOutline(font, outline);
  204. TTF_SetFontKerning(font, kerning);
  205. TTF_SetFontHinting(font, hinting);
  206. if(dump) {
  207. for(i = 48; i < 123; i++) {
  208. SDL_Surface* glyph = NULL;
  209. glyph = TTF_RenderGlyph_Shaded(font, i, *forecol, *backcol);
  210. if(glyph) {
  211. char outname[64];
  212. SDL_snprintf(outname, sizeof(outname), "glyph-%d.bmp", i);
  213. SDL_SaveBMP(glyph, outname);
  214. }
  215. }
  216. cleanup(0);
  217. }
  218. /* Create a window */
  219. if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer) < 0) {
  220. fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
  221. cleanup(2);
  222. }
  223. /* Show which font file we're looking at */
  224. SDL_snprintf(string, sizeof(string), "Font file: %s", argv[0]); /* possible overflow */
  225. switch (rendermethod) {
  226. case TextRenderSolid:
  227. text = TTF_RenderText_Solid(font, string, *forecol);
  228. break;
  229. case TextRenderShaded:
  230. text = TTF_RenderText_Shaded(font, string, *forecol, *backcol);
  231. break;
  232. case TextRenderBlended:
  233. text = TTF_RenderText_Blended(font, string, *forecol);
  234. break;
  235. }
  236. if (text != NULL) {
  237. scene.captionRect.x = 4;
  238. scene.captionRect.y = 4;
  239. scene.captionRect.w = text->w;
  240. scene.captionRect.h = text->h;
  241. scene.caption = SDL_CreateTextureFromSurface(renderer, text);
  242. SDL_FreeSurface(text);
  243. }
  244. /* Render and center the message */
  245. if (argc > 2) {
  246. message = argv[2];
  247. } else {
  248. message = DEFAULT_TEXT;
  249. }
  250. switch (rendertype) {
  251. case RENDER_LATIN1:
  252. switch (rendermethod) {
  253. case TextRenderSolid:
  254. text = TTF_RenderText_Solid(font, message, *forecol);
  255. break;
  256. case TextRenderShaded:
  257. text = TTF_RenderText_Shaded(font, message, *forecol, *backcol);
  258. break;
  259. case TextRenderBlended:
  260. text = TTF_RenderText_Blended(font, message, *forecol);
  261. break;
  262. }
  263. break;
  264. case RENDER_UTF8:
  265. switch (rendermethod) {
  266. case TextRenderSolid:
  267. text = TTF_RenderUTF8_Solid(font, message, *forecol);
  268. break;
  269. case TextRenderShaded:
  270. text = TTF_RenderUTF8_Shaded(font, message, *forecol, *backcol);
  271. break;
  272. case TextRenderBlended:
  273. text = TTF_RenderUTF8_Blended(font, message, *forecol);
  274. break;
  275. }
  276. break;
  277. case RENDER_UNICODE:
  278. {
  279. Uint16 *unicode_text = SDL_iconv_utf8_ucs2(message);
  280. switch (rendermethod) {
  281. case TextRenderSolid:
  282. text = TTF_RenderUNICODE_Solid(font, unicode_text, *forecol);
  283. break;
  284. case TextRenderShaded:
  285. text = TTF_RenderUNICODE_Shaded(font, unicode_text, *forecol, *backcol);
  286. break;
  287. case TextRenderBlended:
  288. text = TTF_RenderUNICODE_Blended(font, unicode_text, *forecol);
  289. break;
  290. }
  291. SDL_free(unicode_text);
  292. }
  293. break;
  294. default:
  295. text = NULL; /* This shouldn't happen */
  296. break;
  297. }
  298. if (text == NULL) {
  299. fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError());
  300. TTF_CloseFont(font);
  301. cleanup(2);
  302. }
  303. scene.messageRect.x = (WIDTH - text->w)/2;
  304. scene.messageRect.y = (HEIGHT - text->h)/2;
  305. scene.messageRect.w = text->w;
  306. scene.messageRect.h = text->h;
  307. scene.message = SDL_CreateTextureFromSurface(renderer, text);
  308. printf("Font is generally %d big, and string is %d big\n",
  309. TTF_FontHeight(font), text->h);
  310. draw_scene(renderer, &scene);
  311. /* Wait for a keystroke, and blit text on mouse press */
  312. done = 0;
  313. while (!done) {
  314. if (SDL_WaitEvent(&event) < 0) {
  315. fprintf(stderr, "SDL_PullEvent() error: %s\n",
  316. SDL_GetError());
  317. done = 1;
  318. continue;
  319. }
  320. switch (event.type) {
  321. case SDL_MOUSEBUTTONDOWN:
  322. scene.messageRect.x = event.button.x - text->w/2;
  323. scene.messageRect.y = event.button.y - text->h/2;
  324. scene.messageRect.w = text->w;
  325. scene.messageRect.h = text->h;
  326. draw_scene(renderer, &scene);
  327. break;
  328. case SDL_KEYDOWN:
  329. case SDL_QUIT:
  330. done = 1;
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. SDL_FreeSurface(text);
  337. TTF_CloseFont(font);
  338. SDL_DestroyTexture(scene.caption);
  339. SDL_DestroyTexture(scene.message);
  340. cleanup(0);
  341. /* Not reached, but fixes compiler warnings */
  342. return 0;
  343. }
  344. /* vi: set ts=4 sw=4 expandtab: */