388921d675ab70d8bdbeb64b407365e01343559d
[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  * vim: ts=4 sw=4 noet ai cindent syntax=c
27  *
28  */
29 #include "conky.h"
30 #include "fonts.h"
31 #include "logging.h"
32
33 int selected_font = 0;
34 int font_count = -1;
35 struct font_list *fonts = NULL;
36 char fontloaded = 0;
37
38 void set_font(void)
39 {
40 #ifdef XFT
41         if (use_xft) return;
42 #endif /* XFT */
43         if (font_count > -1 && fonts[selected_font].font) {
44                 XSetFont(display, window.gc, fonts[selected_font].font->fid);
45         }
46 }
47
48 void setup_fonts(void)
49 {
50         if ((output_methods & TO_X) == 0) {
51                 return;
52         }
53 #ifdef XFT
54         if (use_xft) {
55                 if (window.xftdraw) {
56                         XftDrawDestroy(window.xftdraw);
57                         window.xftdraw = 0;
58                 }
59                 window.xftdraw = XftDrawCreate(display, window.drawable,
60                                 DefaultVisual(display, screen), DefaultColormap(display, screen));
61         }
62 #endif /* XFT */
63         set_font();
64 }
65
66 int add_font(const char *data_in)
67 {
68         if ((output_methods & TO_X) == 0) {
69                 return 0;
70         }
71         if (font_count > MAX_FONTS) {
72                 CRIT_ERR(NULL, NULL, "you don't need that many fonts, sorry.");
73         }
74         font_count++;
75         if (font_count == 0) {
76                 if (fonts != NULL) {
77                         free(fonts);
78                 }
79                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
80                                 == NULL) {
81                         CRIT_ERR(NULL, NULL, "malloc");
82                 }
83                 memset(fonts, 0, sizeof(struct font_list));
84         }
85         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count + 1)));
86         memset(&fonts[font_count], 0, sizeof(struct font_list));
87         if (fonts == NULL) {
88                 CRIT_ERR(NULL, NULL, "realloc in add_font");
89         }
90         // must account for null terminator
91         if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) {
92                 strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
93 #ifdef XFT
94                 fonts[font_count].font_alpha = 0xffff;
95 #endif
96         } else {
97                 CRIT_ERR(NULL, NULL, "Oops...looks like something overflowed in add_font().");
98         }
99         return font_count;
100 }
101
102 void set_first_font(const char *data_in)
103 {
104         if ((output_methods & TO_X) == 0) {
105                 return;
106         }
107         if (font_count < 0) {
108                 if ((fonts = (struct font_list *) malloc(sizeof(struct font_list)))
109                                 == NULL) {
110                         CRIT_ERR(NULL, NULL, "malloc");
111                 }
112                 memset(fonts, 0, sizeof(struct font_list));
113                 font_count++;
114         }
115         if (strlen(data_in) > 1) {
116                 strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE);
117 #ifdef XFT
118                 fonts[0].font_alpha = 0xffff;
119 #endif
120         }
121 }
122
123 void free_fonts(void)
124 {
125         int i;
126
127         if ((output_methods & TO_X) == 0) {
128                 return;
129         }
130         if(fontloaded == 0) {
131                 free(fonts);
132                 return;
133         }
134         for (i = 0; i <= font_count; i++) {
135 #ifdef XFT
136                 if (use_xft) {
137                         XftFontClose(display, fonts[i].xftfont);
138                         fonts[i].xftfont = 0;
139                 } else
140 #endif /* XFT */
141                 {
142                         XFreeFont(display, fonts[i].font);
143                         fonts[i].font = 0;
144                 }
145         }
146         free(fonts);
147         fonts = 0;
148         font_count = -1;
149         selected_font = 0;
150 #ifdef XFT
151         if (window.xftdraw) {
152                 XftDrawDestroy(window.xftdraw);
153                 window.xftdraw = 0;
154         }
155 #endif /* XFT */
156 }
157
158 void load_fonts(void)
159 {
160         int i;
161
162         if ((output_methods & TO_X) == 0)
163                 return;
164         for (i = 0; i <= font_count; i++) {
165 #ifdef XFT
166                 /* load Xft font */
167                 if (use_xft && fonts[i].xftfont) {
168                         continue;
169                 } else if (use_xft) {
170                         fonts[i].xftfont = XftFontOpenName(display, screen,
171                                         fonts[i].name);
172                         if (fonts[i].xftfont) {
173                                 continue;
174                         }
175
176                         ERR("can't load Xft font '%s'", fonts[i].name);
177                         if ((fonts[i].xftfont = XftFontOpenName(display, screen,
178                                         "courier-12")) != NULL) {
179                                 continue;
180                         }
181
182                         ERR("can't load Xft font '%s'", "courier-12");
183
184                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
185                                 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
186                         }
187                         use_xft = 0;
188
189                         continue;
190                 }
191 #endif
192                 /* load normal font */
193                 if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
194                         ERR("can't load font '%s'", fonts[i].name);
195                         if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
196                                 CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed");
197                         }
198                 }
199         }
200         fontloaded = 1;
201 }