chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / fonts.c
1 /*
2  * Copyright (C) 2004-2009 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 "e16-ecore_list.h"
25
26 typedef struct {
27    char               *name;
28    char               *font;
29 } FontAlias;
30
31 static Ecore_List  *font_list = NULL;
32
33 static void
34 _FontAliasDestroy(void *data)
35 {
36    FontAlias          *fa = (FontAlias *) data;
37
38    if (!fa)
39       return;
40    Efree(fa->name);
41    Efree(fa->font);
42
43    Efree(fa);
44 }
45
46 static FontAlias   *
47 FontAliasCreate(const char *name, const char *font)
48 {
49    FontAlias          *fa;
50
51    fa = EMALLOC(FontAlias, 1);
52    if (!fa)
53       return NULL;
54
55    fa->name = Estrdup(name);
56    fa->font = Estrdup(font);
57
58    if (!font_list)
59      {
60         font_list = ecore_list_new();
61         ecore_list_free_cb_set(font_list, _FontAliasDestroy);
62      }
63    ecore_list_prepend(font_list, fa);
64
65    return fa;
66 }
67
68 static int
69 _FontMatchName(const void *data, const void *match)
70 {
71    return strcmp(((const FontAlias *)data)->name, (const char *)match);
72 }
73
74 const char         *
75 FontLookup(const char *name)
76 {
77    FontAlias          *fa;
78
79    fa = (FontAlias *) ecore_list_find(font_list, _FontMatchName, name);
80
81    return (fa) ? fa->font : NULL;
82 }
83
84 /*
85  * Configuration load
86  */
87
88 static int
89 _FontConfigLoad(FILE * fs)
90 {
91    int                 err = 0;
92    FontAlias          *fa;
93    char                s[FILEPATH_LEN_MAX];
94    char                s1[128], *p2;
95    int                 i2;
96
97    while (GetLine(s, sizeof(s), fs))
98      {
99         s1[0] = '\0';
100         i2 = 0;
101         sscanf(s, "%120s %n", s1, &i2);
102         if (i2 <= 0)
103            continue;
104         p2 = s + i2;
105         if (!p2[0])
106            continue;
107         if (strncmp(s, "font-", 5))
108            continue;
109         fa = FontAliasCreate(s1, p2);
110      }
111
112    return err;
113 }
114
115 static int
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
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    _FontConfigLoad1("fonts.cfg", 0);
144
145 #if USE_PANGO
146    if (!_FontConfigLoad1("fonts.pango.cfg", 1))
147       return;
148 #endif
149 #if USE_XFT
150    if (!_FontConfigLoad1("fonts.xft.cfg", 1))
151       return;
152 #endif
153    _FontConfigLoad1("fonts.cfg", 1);
154 }
155
156 void
157 FontConfigUnload(void)
158 {
159    ecore_list_destroy(font_list);
160    font_list = NULL;
161 }