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