1 /*
2  * Copyright (C) 2014 Neverball authors
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14 
15 #include <string.h>
16 #include <stdlib.h>
17 
18 #include <SDL_ttf.h>
19 #include <SDL_rwops.h>
20 
21 #include "font.h"
22 #include "common.h"
23 #include "fs.h"
24 
25 /*---------------------------------------------------------------------------*/
26 
font_load(struct font * ft,const char * path,int sizes[3])27 int font_load(struct font *ft, const char *path, int sizes[3])
28 {
29     if (ft && path && *path)
30     {
31         memset(ft, 0, sizeof (*ft));
32 
33         if ((ft->data = fs_load(path, &ft->datalen)))
34         {
35             int i;
36 
37             SAFECPY(ft->path, path);
38 
39             ft->rwops = SDL_RWFromConstMem(ft->data, ft->datalen);
40 
41             for (i = 0; i < ARRAYSIZE(ft->ttf); i++)
42             {
43                 SDL_RWseek(ft->rwops, 0, SEEK_SET);
44                 ft->ttf[i] = TTF_OpenFontRW(ft->rwops, 0, sizes[i]);
45             }
46             return 1;
47         }
48     }
49     return 0;
50 }
51 
font_free(struct font * ft)52 void font_free(struct font *ft)
53 {
54     if (ft)
55     {
56         int i;
57 
58         for (i = 0; i < ARRAYSIZE(ft->ttf); i++)
59             if (ft->ttf[i])
60                 TTF_CloseFont(ft->ttf[i]);
61 
62         if (ft->rwops)
63             SDL_RWclose(ft->rwops);
64 
65         if (ft->data)
66             free(ft->data);
67 
68         memset(ft, 0, sizeof (*ft));
69     }
70 }
71 
font_init(void)72 int font_init(void)
73 {
74     return (TTF_Init() == 0);
75 }
76 
font_quit(void)77 void font_quit(void)
78 {
79     TTF_Quit();
80 }
81 
82 /*---------------------------------------------------------------------------*/
83