Advertisement
Guest User

create ascii tileset from ttf font

a guest
Jun 18th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. // compile with gcc -o font2tileset font2tileset.c -lSDL2 -lSDL2_image -lSDL2_ttf
  2. // usage: ./font2tileset <font> <font-size> <style> <tile-width> <tile-height> <output.png>
  3. // example ./font2tileset arial.ttf 16 normal 16 16 arial_16x16.png
  4.  
  5. #include <SDL2/SDL.h>
  6. #include <SDL2/SDL_ttf.h>
  7. #include <SDL2/SDL_image.h>
  8.  
  9. static char* utf8_ascii_table[] = {
  10.     " ", "☺", "☻", "♥", "♦", "♣", "♠", "•", "◘", "○", "◙", "♂", "♀", "♪", "♫", "☼",
  11.     "►", "◄", "↕", "‼", "¶", "§", "▬", "↨", "↑", "↓", "→", "←", "∟", "↔", "▲", "▼",
  12.     " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
  13.     "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
  14.     "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
  15.     "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_",
  16.     "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
  17.     "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "⌂",
  18.     "Ç", "ü", "é", "â", "ä", "à", "å", "ç", "ê", "ë", "è", "ï", "î", "ì", "Ä", "Å",
  19.     "É", "æ", "Æ", "ô", "ö", "ò", "û", "ù", "ÿ", "Ö", "Ü", "¢", "£", "¥", "₧", "ƒ",
  20.     "á", "í", "ó", "ú", "ñ", "Ñ", "ª", "º", "¿", "⌐", "¬", "½", "¼", "¡", "«", "»",
  21.     "░", "▒", "▓", "│", "┤", "╡", "╢", "╖", "╕", "╣", "║", "╗", "╝", "╜", "╛", "┐",
  22.     "└", "┴", "┬", "├", "─", "┼", "╞", "╟", "╚", "╔", "╩", "╦", "╠", "═", "╬", "╧",
  23.     "╨", "╤", "╥", "╙", "╘", "╒", "╓", "╫", "╪", "┘", "┌", "█", "▄", "▌", "▐", "▀",
  24.     "α", "ß", "Γ", "π", "Σ", "σ", "µ", "τ", "Φ", "Θ", "Ω", "δ", "∞", "φ", "ε", "∩",
  25.     "≡", "±", "≥", "≤", "⌠", "⌡", "÷", "≈", "°", "∙", "·", "√", "ⁿ", "²", "■", "□",
  26. };
  27.  
  28. int main(int argc, char* argv[]) {
  29.     if(argc != 7) {
  30.         fprintf(stderr, "usage: %s <font> <font-size> <style> <tile-width> <tile-height> <output.png>\n", argv[0]);
  31.         return 1;
  32.     }
  33.     if(SDL_Init(SDL_INIT_EVERYTHING) < 0) return 1;
  34.     if(IMG_Init(IMG_INIT_PNG) < 0) return 1;
  35.     if(TTF_Init() < 0) return 1;
  36.  
  37.     int size = strtol(argv[2], NULL, 10);
  38.     int width = strtol(argv[4], NULL, 10);
  39.     int height = strtol(argv[5], NULL, 10);
  40.     char* style = argv[3];
  41.     TTF_Font* font = TTF_OpenFont(argv[1], size);
  42.     if(font == NULL) {
  43.         fprintf(stderr, "could not load font '%s'\n", argv[1]);
  44.         return 1;
  45.     }
  46.     if(!strcmp(style, "bold")) {
  47.         TTF_SetFontStyle(font, TTF_STYLE_BOLD);
  48.     } else if(!strcmp(style, "italic")) {
  49.         TTF_SetFontStyle(font, TTF_STYLE_ITALIC);
  50.     } else if(strcmp(style, "normal")) {
  51.         fprintf(stderr, "invalid style '%s', try normal, bold or italic.\n", style);
  52.         return 1;
  53.     }
  54.  
  55.     //TTF_SetFontHinting(font, TTF_HINTING_LIGHT);
  56.  
  57.     SDL_Surface* tileset = SDL_CreateRGBSurfaceWithFormat(0, 16 * width, 16 * height, 32, SDL_PIXELFORMAT_RGBA32);
  58.     if(tileset == NULL) {
  59.         fprintf(stderr, "could not create tileset surface of size %dx%d\n", 16 * width, 16 * height);
  60.         return 1;
  61.     }
  62.  
  63.     SDL_Color fg = {255, 255, 255, 255};
  64.     for(int i = 0; i < 256; i++) {
  65.         SDL_Surface* rendered = TTF_RenderUTF8_Blended(font, utf8_ascii_table[i], fg);
  66.         int x = i % 16;
  67.         int y = i / 16;
  68.         SDL_Rect dst_rect = {x * width + (width - rendered->w) / 2, y * height + height - rendered->h, rendered->w, rendered->h};
  69.         SDL_BlitSurface(rendered, NULL, tileset, &dst_rect);
  70.         SDL_FreeSurface(rendered);
  71.     }
  72.  
  73.     if(IMG_SavePNG(tileset, argv[6]) < 0) {
  74.         fprintf(stderr, "could not save result to '%s'\n", argv[5]);
  75.         return 1;
  76.     }
  77.  
  78.     SDL_FreeSurface(tileset);
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement