2 showfont: An example of using the SDL_ttf library with 2D graphics.
3 Copyright (C) 2001-2016 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
22 /* A simple program to test the text rendering feature of the TTF library */
24 /* quiet windows compiler warnings */
\r
25 #define _CRT_SECURE_NO_WARNINGS
\r
34 #define DEFAULT_PTSIZE 18
35 #define DEFAULT_TEXT "The quick brown fox jumped over the lazy dog"
36 #define NUM_COLORS 256
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";
50 static void draw_scene(SDL_Renderer *renderer, Scene *scene)
52 /* Clear the background to background color */
53 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
54 SDL_RenderClear(renderer);
56 SDL_RenderCopy(renderer, scene->caption, NULL, &scene->captionRect);
57 SDL_RenderCopy(renderer, scene->message, NULL, &scene->messageRect);
58 SDL_RenderPresent(renderer);
61 static void cleanup(int exitcode)
68 int main(int argc, char *argv[])
70 char *argv0 = argv[0];
72 SDL_Renderer *renderer;
78 SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
79 SDL_Color black = { 0x00, 0x00, 0x00, 0 };
94 char *message, string[128];
96 /* Look for special execution mode */
98 /* Look for special rendering types */
100 renderstyle = TTF_STYLE_NORMAL;
101 rendertype = RENDER_LATIN1;
103 hinting = TTF_HINTING_NORMAL;
105 /* Default is black and white */
108 for ( i=1; argv[i] && argv[i][0] == '-'; ++i ) {
109 if ( strcmp(argv[i], "-solid") == 0 ) {
112 if ( strcmp(argv[i], "-utf8") == 0 ) {
113 rendertype = RENDER_UTF8;
115 if ( strcmp(argv[i], "-unicode") == 0 ) {
116 rendertype = RENDER_UNICODE;
118 if ( strcmp(argv[i], "-b") == 0 ) {
119 renderstyle |= TTF_STYLE_BOLD;
121 if ( strcmp(argv[i], "-i") == 0 ) {
122 renderstyle |= TTF_STYLE_ITALIC;
124 if ( strcmp(argv[i], "-u") == 0 ) {
125 renderstyle |= TTF_STYLE_UNDERLINE;
127 if ( strcmp(argv[i], "-s") == 0 ) {
128 renderstyle |= TTF_STYLE_STRIKETHROUGH;
130 if ( strcmp(argv[i], "-outline") == 0 ) {
131 if ( sscanf (argv[++i], "%d", &outline) != 1 ) {
132 fprintf(stderr, Usage, argv0);
136 if ( strcmp(argv[i], "-hintlight") == 0 ) {
137 hinting = TTF_HINTING_LIGHT;
139 if ( strcmp(argv[i], "-hintmono") == 0 ) {
140 hinting = TTF_HINTING_MONO;
142 if ( strcmp(argv[i], "-hintnone") == 0 ) {
143 hinting = TTF_HINTING_NONE;
145 if ( strcmp(argv[i], "-nokerning") == 0 ) {
148 if ( strcmp(argv[i], "-dump") == 0 ) {
151 if ( strcmp(argv[i], "-fgcol") == 0 ) {
153 if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
154 fprintf(stderr, Usage, argv0);
157 forecol->r = (Uint8)r;
158 forecol->g = (Uint8)g;
159 forecol->b = (Uint8)b;
161 if ( strcmp(argv[i], "-bgcol") == 0 ) {
163 if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
164 fprintf(stderr, Usage, argv0);
167 backcol->r = (Uint8)r;
168 backcol->g = (Uint8)g;
169 backcol->b = (Uint8)b;
171 fprintf(stderr, Usage, argv0);
180 fprintf(stderr, Usage, argv0);
184 /* Initialize the TTF library */
185 if ( TTF_Init() < 0 ) {
186 fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
191 /* Open the font file with the requested point size */
194 ptsize = atoi(argv[1]);
198 ptsize = DEFAULT_PTSIZE;
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());
208 TTF_SetFontStyle(font, renderstyle);
209 TTF_SetFontOutline(font, outline);
210 TTF_SetFontKerning(font, kerning);
211 TTF_SetFontHinting(font, hinting);
214 for( i = 48; i < 123; i++ ) {
215 SDL_Surface* glyph = NULL;
217 glyph = TTF_RenderGlyph_Shaded( font, i, *forecol, *backcol );
221 sprintf( outname, "glyph-%d.bmp", i );
222 SDL_SaveBMP( glyph, outname );
229 /* Create a window */
230 if (SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer) < 0) {
231 fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
235 /* Show which font file we're looking at */
236 sprintf(string, "Font file: %s", argv[0]); /* possible overflow */
238 text = TTF_RenderText_Solid(font, string, *forecol);
240 text = TTF_RenderText_Shaded(font, string, *forecol, *backcol);
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);
251 /* Render and center the message */
255 message = DEFAULT_TEXT;
257 switch (rendertype) {
260 text = TTF_RenderText_Solid(font,message,*forecol);
262 text = TTF_RenderText_Shaded(font,message,*forecol,*backcol);
268 text = TTF_RenderUTF8_Solid(font,message,*forecol);
270 text = TTF_RenderUTF8_Shaded(font,message,*forecol,*backcol);
276 Uint16 *unicode_text = SDL_iconv_utf8_ucs2(message);
278 text = TTF_RenderUNICODE_Solid(font,
279 unicode_text, *forecol);
281 text = TTF_RenderUNICODE_Shaded(font,
282 unicode_text, *forecol, *backcol);
284 SDL_free(unicode_text);
288 text = NULL; /* This shouldn't happen */
291 if ( text == NULL ) {
292 fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError());
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);
304 draw_scene(renderer, &scene);
306 /* Wait for a keystroke, and blit text on mouse press */
309 if ( SDL_WaitEvent(&event) < 0 ) {
310 fprintf(stderr, "SDL_PullEvent() error: %s\n",
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);
332 SDL_FreeSurface(text);
334 SDL_DestroyTexture(scene.caption);
335 SDL_DestroyTexture(scene.message);
338 /* Not reached, but fixes compiler warnings */
342 /* vi: set ts=4 sw=4 expandtab: */