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