a31e5636388cef857309215ff45b099b75f0618b
[monky] / src / fonts.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31 #include "conky.h"
32 #include "fonts.h"
33 #include "logging.h"
34
35 int selected_font = 0;
36 int font_count = -1;
37 struct font_list *fonts = NULL;
38 char fontloaded = 0;
39
40 void set_font(void)
41 {
42 #ifdef XFT
43         if (use_xft) return;
44 #endif /* XFT */
45         if (font_count > -1 && fonts[selected_font].font) {
46                 XSetFont(display, window.gc, fonts[selected_font].font->fid);
47         }
48 }
49
50 void setup_fonts(void)
51 {
52         if ((output_methods & TO_X) == 0) {
53                 return;
54         }
55 #ifdef XFT
56         if (use_xft) {
57                 if (window.xftdraw) {
58                         XftDrawDestroy(window.xftdraw);
59                         window.xftdraw = 0;
60                 }
61                 window.xftdraw = XftDrawCreate(display, window.drawable,
62                                 DefaultVisual(display, screen), DefaultColormap(display, screen));
63         }
64 #endif /* XFT */
65         set_font();
66 }
67
68 int add_font(const char *data_in)
69 {
70         if ((output_methods & TO_X) == 0) {
71                 return 0;
72         }
73         if (font_count > MAX_FONTS) {
74                 CRIT_ERR(NULL, NULL, "you don't need that many fonts, sorry.");
75         }
76         font_count++;
77         if (font_count == 0) {
78                 if (fonts != NULL) {
79                         free(fonts);
80                 }
81                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
82                                 == NULL) {
83                         CRIT_ERR(NULL, NULL, "malloc");
84                 }
85                 memset(fonts, 0, sizeof(struct font_list));
86         }
87         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
88         memset(&fonts[font_count], 0, sizeof(struct font_list));
89         if (fonts == NULL) {
90                 CRIT_ERR(NULL, NULL, "realloc in add_font");
91         }
92         // must account for null terminator
93         if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
94                 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
95 #ifdef XFT
96                 fonts[font_count].font_alpha = 0xffff;
97 #endif
98         } else {
99                 CRIT_ERR(NULL, NULL, "Oops...looks like something overflowed in add_font().");
100         }
101         return font_count;
102 }
103
104 void set_first_font(const char *data_in)
105 {
106         if ((output_methods & TO_X) == 0) {
107                 return;
108         }
109         if (font_count < 0) {
110                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
111                                 == NULL) {
112                         CRIT_ERR(NULL, NULL, "malloc");
113                 }
114                 memset(fonts, 0, sizeof(struct font_list));
115                 font_count++;
116         }
117         if (strlen(data_in) > 1) {
118                 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
119 #ifdef XFT
120                 fonts[0].font_alpha = 0xffff;
121 #endif
122         }
123 }
124
125 void free_fonts(void)
126 {
127         int i;
128
129         if ((output_methods & TO_X) == 0) {
130                 return;
131         }
132         if(fontloaded == 0) {
133                 free(fonts);
134                 return;
135         }
136         for (i = 0; i <= font_count; i++) {
137 #ifdef XFT
138                 if (use_xft) {
139                         XftFontClose(display, fonts[i].xftfont);
140                         fonts[i].xftfont = 0;
141                 } else
142 #endif /* XFT */
143                 {
144                         XFreeFont(display, fonts[i].font);
145                         fonts[i].font = 0;
146                 }
147         }
148         free(fonts);
149         fonts = 0;
150         font_count = -1;
151         selected_font = 0;
152 #ifdef XFT
153         if (window.xftdraw) {
154                 XftDrawDestroy(window.xftdraw);
155                 window.xftdraw = 0;
156         }
157 #endif /* XFT */
158 }
159
160 void load_fonts(void)
161 {
162         int i;
163
164         if ((output_methods & TO_X) == 0)
165                 return;
166         for (i = 0; i <= font_count; i++) {
167 #ifdef XFT
168                 /* load Xft font */
169                 if (use_xft && fonts[i].xftfont) {
170                         continue;
171                 } else if (use_xft) {
172                         fonts[i].xftfont = XftFontOpenName(display, screen,
173                                         fonts[i].name);
174                         if (fonts[i].xftfont) {
175                                 continue;
176                         }
177
178                         NORM_ERR("can't load Xft font '%s'", fonts[i].name);
179                         if ((fonts[i].xftfont = XftFontOpenName(display, screen,
180                                         "courier-12")) != NULL) {
181                                 continue;
182                         }
183
184                         NORM_ERR("can't load Xft font '%s'", "courier-12");
185
186                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
187                                 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
188                         }
189                         use_xft = 0;
190
191                         continue;
192                 }
193 #endif
194                 /* load normal font */
195                 if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
196                         NORM_ERR("can't load font '%s'", fonts[i].name);
197                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
198                                 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
199                         }
200                 }
201         }
202         fontloaded = 1;
203 }