modify logging to simplify i18n
[voxelands-alt.git] / src / graphics / font.c
blobd7edbef397061f688c96569753c0ae7560aa691b
1 /************************************************************************
2 * font.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "path.h"
22 #define _WM_EXPOSE_ALL
23 #include "wm.h"
24 #include "graphics.h"
25 #include "array.h"
27 #include <string.h>
29 array_t *fonts = NULL;
31 /* load a font file */
32 int font_load(char* file, char* token)
34 char* t;
35 font_t *f;
36 int r = 1;
37 if (!fonts)
38 fonts = array_create(ARRAY_TYPE_PTR);
40 if (!file || !token)
41 return 0;
43 t = strrchr(file,'.');
44 if (!t)
45 return 0;
47 f = malloc(sizeof(font_t));
48 f->token = strdup(token);
50 t++;
52 if (!strcmp(t,"ttf")) {
53 r = font_load_ttf(f,file);
54 }else{
55 vlprintf(CN_ERROR, "Unsupported Font: %s",file);
58 if (r) {
59 free(f->token);
60 free(f);
61 return 0;
64 array_push_ptr(fonts,f);
66 return fonts->length;
69 /* get a font by id */
70 font_t *font_get(int id)
72 font_t *f;
73 if (!fonts)
74 font_load("font.ttf","default");
75 if (!fonts)
76 return NULL;
78 if (id == UI_DEFAULT)
79 id = 1;
81 f = array_get_ptr(fonts,id-1);
82 if (!f)
83 f = array_get_ptr(fonts,0);
85 return f;
88 /* get a font by token */
89 font_t *font_find(char* token)
91 int i;
92 font_t **f;
93 if (!fonts)
94 font_load("font.ttf","default");
95 if (!fonts)
96 return NULL;
98 f = fonts->data;
99 for (i=0; i<fonts->length; i++) {
100 if (!strcmp(f[i]->token,token))
101 return f[i];
104 return array_get_ptr(fonts,0);
107 /* get the id of a font */
108 int font_get_id(char* token)
110 int i;
111 font_t **f;
112 if (!fonts)
113 font_load("font.ttf","default");
114 if (!fonts)
115 return 0;
117 f = fonts->data;
118 for (i=0; i<fonts->length; i++) {
119 if (!strcmp(f[i]->token,token))
120 return i+1;
123 if (!strcmp(token,"default"))
124 return 1;
126 return 0;