1 | /* |
---|
2 | * makefont.c: create a font bitmap from a .ttf file. |
---|
3 | * $Id: makefont.c 2315 2008-04-26 07:25:03Z sam $ |
---|
4 | * |
---|
5 | * Copyright: (c) 2004 Sam Hocevar <sam@zoy.org> |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | * |
---|
12 | * Build example: |
---|
13 | * gcc -Wall makefont.c -o makefont `sdl-config --cflags --libs` -lSDL_ttf |
---|
14 | * |
---|
15 | * Usage: |
---|
16 | * makefont <font.ttf> <size> <text> <output.bmp> |
---|
17 | */ |
---|
18 | |
---|
19 | #include <stdlib.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <string.h> |
---|
22 | |
---|
23 | #include "SDL.h" |
---|
24 | #include "SDL_ttf.h" |
---|
25 | |
---|
26 | int main(int argc, char *argv[]) |
---|
27 | { |
---|
28 | unsigned char *text; |
---|
29 | SDL_Color bg = { 0xff, 0xff, 0xff, 0 }; |
---|
30 | SDL_Color fg = { 0x00, 0x00, 0x00, 0 }; |
---|
31 | SDL_Surface *surface; |
---|
32 | TTF_Font *font; |
---|
33 | int i; |
---|
34 | |
---|
35 | if(argc != 5) |
---|
36 | { |
---|
37 | fprintf(stderr, "usage: %s <font.ttf> <size> <text> <output.bmp>\n", |
---|
38 | argv[0]); |
---|
39 | return 1; |
---|
40 | } |
---|
41 | |
---|
42 | /* Load font */ |
---|
43 | TTF_Init(); |
---|
44 | font = TTF_OpenFont(argv[1], atoi(argv[2])); |
---|
45 | if(!font) |
---|
46 | { |
---|
47 | fprintf(stderr, "could not load font %s: %s\n", |
---|
48 | argv[1], SDL_GetError()); |
---|
49 | TTF_Quit(); |
---|
50 | return 1; |
---|
51 | } |
---|
52 | |
---|
53 | /* Add spaces to string */ |
---|
54 | text = malloc(2 * strlen(argv[3]) * sizeof(char)); |
---|
55 | for(i = 0; argv[3][i]; i++) |
---|
56 | { |
---|
57 | text[i * 2] = argv[3][i]; |
---|
58 | text[i * 2 + 1] = ' '; |
---|
59 | } |
---|
60 | text[i * 2 - 1] = '\0'; |
---|
61 | |
---|
62 | /* Render text to surface */ |
---|
63 | TTF_SetFontStyle(font, TTF_STYLE_NORMAL); |
---|
64 | surface = TTF_RenderUTF8_Shaded(font, text, fg, bg); |
---|
65 | if(!surface) |
---|
66 | { |
---|
67 | fprintf(stderr, "surface rendering failed: %s\n", SDL_GetError()); |
---|
68 | TTF_CloseFont(font); |
---|
69 | TTF_Quit(); |
---|
70 | return 1; |
---|
71 | } |
---|
72 | |
---|
73 | /* Clean up surface */ |
---|
74 | |
---|
75 | /* Save surface and free everything */ |
---|
76 | SDL_SaveBMP(surface, argv[4]); |
---|
77 | SDL_FreeSurface(surface); |
---|
78 | TTF_CloseFont(font); |
---|
79 | TTF_Quit(); |
---|
80 | |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|