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