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