Include and link physfs properly.
[tuxanci.git] / src / client / font.c
blobd052cbbc765776e685864d892ee8830fe880919f
1 #include "main.h"
2 #include "interface.h"
3 #include "font.h"
4 #include "image.h"
5 #include "fontConfig.h"
7 static TTF_Font *g_font;
8 static int fontSize;
10 static bool_t isFontInit = FALSE;
12 bool_t font_is_inicialized()
14 return isFontInit;
17 void font_init()
19 char *arg[] = {":lang=he:outline=true:style=Book", "family", "style", "weight", "file", NULL};
20 char *font_file;
21 font_config_t *font_list;
22 int res;
24 assert(interface_is_inicialized() == TRUE);
26 res = fontconfig_init();
28 if (res != 0) {
29 return;
32 font_list = fontconfig_find(arg);
34 int i;
35 for(i = 0 ; font_list[i].path != NULL ; i++ )
37 printf("path[%d]=%s\nflag[%d]=%s\n\n", i, font_list[i].path, i, font_list[i].flag);
40 if (font_list == NULL) {
41 error("Unable to locate a font");
42 return;
45 font_file = strdup(font_list[0].path);
46 fontSize = FONT_SIZE;
48 fontconfig_del_list(font_list);
49 fontconfig_quit();
51 if (TTF_Init() == -1) {
52 free(font_file);
53 error("SDL: %s", SDL_GetError());
54 return;
57 accessExistFile(font_file);
59 g_font = TTF_OpenFont(font_file, fontSize);
60 TTF_SetFontStyle(g_font, TTF_STYLE_NORMAL);
62 debug("Loading font [%s]", font_file);
64 free(font_file);
65 isFontInit = TRUE;
69 * Shows text *string on coordinates [x,y] with RGB color
71 void font_draw(char *string, int x, int y, int r, int g, int b)
73 SDL_Surface *text;
74 image_t *image;
75 SDL_Color font_color = {r, g, b, SDL_ALPHA_OPAQUE};
77 assert( string != NULL );
79 text = TTF_RenderUTF8_Blended(g_font, string, font_color);
81 /* because if string=="" TTF_RenderUTF8_Blended returns NULLĀ */
82 if (text != NULL) {
83 image = image_new(text);
84 image_draw(image, x, y, 0, 0, image->w, image->h);
85 image_destroy(image);
89 void font_drawMaxSize(char *s, int x, int y, int w, int h, int r, int g, int b)
91 SDL_Rect src_rect, dst_rect;
92 SDL_Surface *text;
93 image_t *i;
94 SDL_Color font_color = {r, g, b, SDL_ALPHA_OPAQUE};
95 int my_w, my_h;
97 assert(s != NULL);
99 text = TTF_RenderUTF8_Blended(g_font, s, font_color);
101 my_w = text->w;
103 if (my_w > w) {
104 my_w = w;
107 my_h = text->h;
109 if (my_w > w) {
110 my_h = h;
113 src_rect.x = 0;
114 src_rect.y = 0;
115 src_rect.w = my_w;
116 src_rect.h = my_h;
118 dst_rect.x = x;
119 dst_rect.y = y;
121 i = image_new(text);
123 /* possibly broken */
124 image_draw(i, x, y, 0, 0, my_w, my_h);
125 image_destroy(i);
129 * Returns size of the font
131 int font_get_size()
133 return fontSize;
136 void font_text_size(char *s, int *w, int *h)
138 assert(s != NULL);
139 assert(w != NULL);
140 assert(h != NULL);
142 TTF_SizeUTF8(g_font, s, w, h);
146 * Frees font from memory
148 void font_quit()
150 TTF_CloseFont(g_font);
151 TTF_Quit();
153 debug("Unloading font");
155 isFontInit = FALSE;