]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/gnome_helpers.C
Forgot to add file.
[lyx.git] / src / frontends / gnome / gnome_helpers.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include <gdk--/font.h>
14 #include <gdk/gdktypes.h>
15 #include <gdk/gdkx.h>
16 #include <glib.h>
17
18 #include "gnome_helpers.h"
19
20 string get_font_name(Gdk_Font const & font)
21 {
22         gchar * name = get_font_name(font.gdkobj());
23         string name_str(name);
24         g_free(name);
25         return name_str;
26 }
27
28
29 gchar *
30 get_font_name (const GdkFont * font)
31 {
32         Atom font_atom, atom;
33         Bool status;
34
35 #ifdef E_FONT_VERBOSE
36         gint i;
37         g_print ("Extracting X font info\n");
38 #endif
39
40         font_atom = gdk_atom_intern ("FONT", FALSE);
41
42         if (font->type == GDK_FONT_FONTSET) {
43                 XFontStruct **font_structs;
44                 gint num_fonts;
45                 gchar **font_names;
46
47                 num_fonts = XFontsOfFontSet (GDK_FONT_XFONT (font), &font_structs, &font_names);
48 #ifdef E_FONT_VERBOSE
49                 g_print ("Fonts of fontset:\n");
50                 for (i = 0; i < num_fonts; i++) g_print ("  %s\n", font_names[i]);
51 #endif
52                 status = XGetFontProperty (font_structs[0], font_atom, &atom);
53         } else {
54                 status = XGetFontProperty (GDK_FONT_XFONT (font), font_atom, &atom);
55         }
56
57         if (status) {
58                 return gdk_atom_name (atom);
59         }
60
61         return NULL;
62 }
63
64 #if USE_UNUSED_STUFF_FROM_E_FONT
65 /*
66  * Splits full X font name into pieces, overwriting hyphens
67  */
68
69 static void
70 split_name (gchar * c[], gchar * name)
71 {
72         gchar *p;
73         gint i;
74
75         p = name;
76         if (*p == '-') p++;
77
78         for (i = 0; i < 12; i++) {
79                 c[i] = p;
80                 /* Skip text */
81                 while (*p && (*p != '-')) p++;
82                 /* Replace hyphen with '\0' */
83                 if (*p) *p++ = '\0';
84         }
85
86         c[i] = p;
87 }
88
89 /*
90  * Find light and bold variants of a font, ideally using the provided
91  * weight for the light variant, and a weight 2 shades darker than it
92  * for the bold variant. If there isn't something 2 shades darker, use
93  * something 3 or more shades darker if it exists, or 1 shade darker
94  * if that's all there is. If there is nothing darker than the provided
95  * weight, but there are lighter fonts, then use the darker one for
96  * bold and a lighter one for light.
97  */
98
99 static gboolean
100 find_variants (gchar **namelist, gint length, gchar *weight,
101                gchar **lightname, gchar **boldname)
102 {
103         static GHashTable *wh = NULL;
104         /* Standard, Found, Bold, Light */
105         gint sw, fw, bw, lw;
106         gchar s[32];
107         gchar *f, *b, *l;
108         gchar *p;
109         gint i;
110
111         if (!wh) {
112                 wh = g_hash_table_new (g_str_hash, g_str_equal);
113                 g_hash_table_insert (wh, "light", GINT_TO_POINTER (1));
114                 g_hash_table_insert (wh, "book", GINT_TO_POINTER (2));
115                 g_hash_table_insert (wh, "regular", GINT_TO_POINTER (2));
116                 g_hash_table_insert (wh, "medium", GINT_TO_POINTER (3));
117                 g_hash_table_insert (wh, "demibold", GINT_TO_POINTER (5));
118                 g_hash_table_insert (wh, "bold", GINT_TO_POINTER (6));
119                 g_hash_table_insert (wh, "black", GINT_TO_POINTER (8));
120         }
121
122         g_snprintf (s, 32, weight);
123         g_strdown (s);
124         sw = GPOINTER_TO_INT (g_hash_table_lookup (wh, s));
125         if (sw == 0) return FALSE;
126
127         fw = 0; lw = 0; bw = 32;
128         f = NULL; l = NULL; b = NULL;
129         *lightname = NULL; *boldname = NULL;
130
131         for (i = 0; i < length; i++) {
132                 p = namelist[i];
133                 if (*p) p++;
134                 while (*p && (*p != '-')) p++;
135                 if (*p) p++;
136                 while (*p && (*p != '-')) p++;
137                 if (*p) p++;
138                 f = p;
139                 while (*p && (*p != '-')) p++;
140                 if (*p) *p = '\0';
141                 g_strdown (f);
142                 fw = GPOINTER_TO_INT (g_hash_table_lookup (wh, f));
143                 if (fw) {
144                         if (fw > sw) {
145                                 if ((fw - 2 == sw) ||
146                                     ((fw > bw) && (bw == sw + 1)) ||
147                                     ((fw < bw) && (fw - 2 > sw))) {
148                                         bw = fw;
149                                         b = f;
150                                 }
151                         } else if (fw < sw) {
152                                 if ((fw + 2 == sw) ||
153                                     ((fw < lw) && (lw == sw - 1)) ||
154                                     ((fw > lw) && (fw + 2 < sw))) {
155                                         lw = fw;
156                                         l = f;
157                                 }
158                         }
159                 }
160         }
161
162         if (b) {
163                 *lightname = weight;
164                 *boldname = b;
165                 return TRUE;
166         } else if (l) {
167                 *lightname = l;
168                 *boldname = weight;
169                 return TRUE;
170         }
171         return FALSE;
172 }
173 #endif
174
175 #ifdef E_FONT_VERBOSE
176 /*
177  * Return newly allocated full name
178  */
179
180 static void
181 e_font_print_gdk_font_name (const GdkFont * font)
182 {
183         Atom font_atom, atom;
184         Bool status;
185
186         font_atom = gdk_atom_intern ("FONT", FALSE);
187
188         g_print ("-------- start of font description --------\n");
189
190         if (font == NULL) {
191                 g_print ("GdkFont is NULL\n");
192         } else if (font->type == GDK_FONT_FONTSET) {
193                 XFontStruct **font_structs;
194                 gint num_fonts;
195                 gchar **font_names;
196                 gint i;
197
198                 num_fonts = XFontsOfFontSet (GDK_FONT_XFONT (font), &font_structs, &font_names);
199
200                 g_print ("Gdk Fontset, locale: %s\n", XLocaleOfFontSet (GDK_FONT_XFONT (font)));
201                 for (i = 0; i < num_fonts; i++) {
202                         g_print ("    %s\n", font_names[i]);
203                 }
204         } else {
205                 gchar * name;
206                 status = XGetFontProperty (GDK_FONT_XFONT (font), font_atom, &atom);
207                 name = gdk_atom_name (atom);
208                 g_print ("GdkFont: %s\n", name);
209                 if (name) g_free (name);
210         }
211
212         g_print ("-------- end of font description --------\n");
213 }
214 #endif