Advertisement
robn

Untitled

May 15th, 2011
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/mman.h>
  7. #include <SDL.h>
  8.  
  9. #define STB_TRUETYPE_IMPLEMENTATION
  10. #include "stb_truetype.h"
  11.  
  12. int main(int argc, char **argv) {
  13.     if (argc != 2) {
  14.         printf("usage: fonttest <text>\n");
  15.         exit(-1);
  16.     }
  17.  
  18.     int fontfd = open("/usr/share/fonts/truetype/msttcorefonts/arialbd.ttf", O_RDONLY);
  19.     if (fontfd < 0) {
  20.         perror("couldn't open font file");
  21.         exit(1);
  22.     }
  23.  
  24.     struct stat st;
  25.     if (fstat(fontfd, &st) < 0) {
  26.         perror("couldn't stat font file");
  27.         close(fontfd);
  28.         exit(1);
  29.     }
  30.  
  31.     void *fontdata = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fontfd, 0);
  32.     if (!fontdata) {
  33.         perror("couldn't map font file");
  34.         close(fontfd);
  35.         exit(1);
  36.     }
  37.  
  38.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  39.         fprintf(stderr, "sdl init failed: %s\n", SDL_GetError());
  40.         munmap(fontdata, st.st_size);
  41.         close(fontfd);
  42.         exit(1);
  43.     }
  44.  
  45.     SDL_Surface *glyphdata = SDL_CreateRGBSurface(SDL_SWSURFACE, 512, 512, 8, 0, 0, 0, 0);
  46.     if (!glyphdata) {
  47.         fprintf(stderr, "couldn't create sdl buffer: %s\n", SDL_GetError());
  48.         munmap(fontdata, st.st_size);
  49.         close(fontfd);
  50.         SDL_Quit();
  51.         exit(1);
  52.     }
  53.  
  54.     SDL_Color colors[256];
  55.     for(int i = 0; i < 256; i++){
  56.         colors[i].r=i;
  57.         colors[i].g=i;
  58.         colors[i].b=i;
  59.     }
  60.     SDL_SetPalette(glyphdata, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256);
  61.  
  62.     stbtt_bakedchar cdata[96];
  63.     stbtt_BakeFontBitmap(fontdata, stbtt_GetFontOffsetForIndex(fontdata, 0), 32.0, glyphdata->pixels, 512, 512, 32, 96, cdata);
  64.  
  65.     munmap(fontdata, st.st_size);
  66.     close(fontfd);
  67.  
  68.     SDL_Surface *s = SDL_SetVideoMode(640, 480, 32, 0);
  69.     if (!s) {
  70.         fprintf(stderr, "sdl video mode init failed: %s\n", SDL_GetError());
  71.         SDL_FreeSurface(glyphdata);
  72.         SDL_Quit();
  73.         exit(1);
  74.     }
  75.  
  76.     float x = 0, y = 0;
  77.     for (char *c = argv[1]; *c; c++) {
  78.         stbtt_aligned_quad q;
  79.         stbtt_GetBakedQuad(cdata, 512, 512, *c-32, &x, &y, &q, 1);
  80.  
  81.         int w = q.x1-q.x0;
  82.         int h = q.y1-q.y0;
  83.  
  84.         SDL_Rect src  = { .x = q.s0*512, .y = q.t0*512, .w = w, .h = h };
  85.         SDL_Rect dest = { .x = q.x0, .y = 32+q.y0, .w = w, .h = h };
  86.  
  87.         SDL_BlitSurface(glyphdata, &src, s, &dest);
  88.     }
  89.  
  90.     SDL_FreeSurface(glyphdata);
  91.  
  92.     SDL_Event e;
  93.     while(SDL_WaitEvent(&e) && e.type != SDL_KEYDOWN && e.key.keysym.sym != SDLK_ESCAPE );
  94.  
  95.     SDL_FreeSurface(s);
  96.     SDL_Quit();
  97.  
  98.     exit(0);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement