1 /*
2  * Copyright (C) 2004-2013 Kim Woelders
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "E.h"
24 #include "list.h"
25 #include "parse.h"
26 
27 typedef struct {
28    dlist_t             list;
29    char               *name;
30    char               *font;
31 } FontAlias;
32 
33 static              LIST_HEAD(font_list);
34 
35 static void
_FontAliasDestroy(void * data)36 _FontAliasDestroy(void *data)
37 {
38    FontAlias          *fa = (FontAlias *) data;
39 
40    if (!fa)
41       return;
42    Efree(fa->name);
43    Efree(fa->font);
44 
45    Efree(fa);
46 }
47 
48 static FontAlias   *
_FontAliasCreate(const char * name,const char * font)49 _FontAliasCreate(const char *name, const char *font)
50 {
51    FontAlias          *fa;
52 
53    fa = EMALLOC(FontAlias, 1);
54    if (!fa)
55       return NULL;
56 
57    fa->name = Estrdup(name);
58    fa->font = Estrdup(font);
59 
60    LIST_PREPEND(FontAlias, &font_list, fa);
61 
62    return fa;
63 }
64 
65 static int
_FontMatchName(const void * data,const void * match)66 _FontMatchName(const void *data, const void *match)
67 {
68    return strcmp(((const FontAlias *)data)->name, (const char *)match);
69 }
70 
71 const char         *
FontLookup(const char * name)72 FontLookup(const char *name)
73 {
74    FontAlias          *fa;
75 
76    fa = LIST_FIND(FontAlias, &font_list, _FontMatchName, name);
77 
78    return (fa) ? fa->font : NULL;
79 }
80 
81 /*
82  * Configuration load
83  */
84 
85 static int
_FontConfigLoad(FILE * fs)86 _FontConfigLoad(FILE * fs)
87 {
88    int                 err = 0;
89    char                s[FILEPATH_LEN_MAX], *ss, *name, *font;
90    int                 len;
91 
92    for (;;)
93      {
94 	ss = fgets(s, sizeof(s), fs);
95 	if (!ss)
96 	   break;
97 
98 	len = strcspn(s, "#\r\n");
99 	if (len <= 0)
100 	   continue;
101 
102 	name = font = NULL;
103 	parse(s, "%S%S", &name, &font);
104 	if (!name || !font)
105 	   continue;
106 
107 	if (strncmp(name, "font-", 5))
108 	   continue;
109 	_FontAliasCreate(name, font);
110      }
111 
112    return err;
113 }
114 
115 static int
_FontConfigLoad1(const char * cfg,int look_in_theme_too)116 _FontConfigLoad1(const char *cfg, int look_in_theme_too)
117 {
118    const char         *path;
119 
120    path = (look_in_theme_too) ? Mode.theme.path : NULL;
121 
122    return ConfigFileLoad(cfg, path, _FontConfigLoad, 0);
123 }
124 
125 void
FontConfigLoad(void)126 FontConfigLoad(void)
127 {
128    /* First check explicitly specified configuration (not in theme dir) */
129    if (Conf.theme.use_alt_font_cfg && Conf.theme.font_cfg)
130      {
131 	if (!_FontConfigLoad1(Conf.theme.font_cfg, 0))
132 	   return;
133      }
134 
135    /* If using theme font is specified look for that */
136    if (Conf.theme.use_theme_font_cfg)
137      {
138 	if (!_FontConfigLoad1("fonts.theme.cfg", 1))
139 	   return;
140      }
141 
142    /* Look in user config dir (not in theme dir) */
143    if (!_FontConfigLoad1("fonts.cfg", 0))
144       return;
145 
146 #if USE_PANGO
147    if (!_FontConfigLoad1("fonts.pango.cfg", 1))
148       return;
149 #endif
150 #if USE_XFT
151    if (!_FontConfigLoad1("fonts.xft.cfg", 1))
152       return;
153 #endif
154    _FontConfigLoad1("fonts.cfg", 1);
155 }
156 
157 void
FontConfigUnload(void)158 FontConfigUnload(void)
159 {
160    FontAlias          *fa, *tmp;
161 
162    LIST_FOR_EACH_SAFE(FontAlias, &font_list, fa, tmp)
163    {
164       LIST_REMOVE(FontAlias, &font_list, fa);
165       _FontAliasDestroy(fa);
166    }
167 }
168