aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-font.c
blob: cfee9f0923c6ac489074a91971413f72bae178b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#define _E_FONT_C_

/*
 * e-font
 *
 * Temporary wrappers around GdkFonts to get unicode displaying
 *
 * Author: Lauris Kaplinski <lauris@helixcode.com>
 *
 * Copyright (C) 2000 Helix Code, Inc.
 *
 */

#include <string.h>
#include <gdk/gdkx.h>
#include <unicode.h>
#include "e-font.h"

struct _EFont {
    GdkFont font;
};

EFont *
e_font_from_gdk_name (const gchar *name)
{
    GdkFont *font;

    font = gdk_fontset_load (name);

    return (EFont *) font;
}

EFont *
e_font_from_gdk_font (GdkFont *font)
{
    gdk_font_ref (font);

    return (EFont *) font;
}

void
e_font_ref (EFont *font)
{
    gdk_font_ref (&font->font);
}

void
e_font_unref (EFont *font)
{
    gdk_font_unref (&font->font);
}

gint
e_font_ascent (EFont * font)
{
    return font->font.ascent;
}

gint
e_font_descent (EFont * font)
{
    return font->font.descent;
}

void
e_font_draw_utf8_text (GdkDrawable *drawable, EFont *font, EFontStyle style, GdkGC *gc, gint x, gint y, gchar *text, gint numbytes)
{
    guchar *iso;
    gchar *p;
    gint uni, len;

    g_return_if_fail (drawable != NULL);
    g_return_if_fail (font != NULL);
    g_return_if_fail (gc != NULL);
    g_return_if_fail (text != NULL);

    if (numbytes < 1) return;

    iso = alloca (numbytes);

    for (len = 0, p = text; p != NULL && p < (text + numbytes); len++, p = unicode_next_utf8 (p)) {
        unicode_get_utf8 (p, &uni);
        if ((uni < ' ') || (uni > 255)) uni = ' ';
        iso[len] = uni;
    }

    gdk_draw_text (drawable, &font->font, gc, x, y, iso, len);

    if (style & E_FONT_BOLD)
        gdk_draw_text (drawable, &font->font, gc, x + 1, y, iso, len);
}

gint
e_font_utf8_text_width (EFont *font, EFontStyle style, char *text, int numbytes)
{
    guchar *iso;
    gchar *p;
    gint uni, len;

    g_return_val_if_fail (font != NULL, 0);
    g_return_val_if_fail (text != NULL, 0);

    iso = alloca (numbytes);

    for (len = 0, p = text; p != NULL && p < (text + numbytes); len++, p = unicode_next_utf8 (p)) {
        unicode_get_utf8 (p, &uni);
        if ((uni < ' ') || (uni > 255)) uni = ' ';
        iso[len] = uni;
    }

    return gdk_text_width (&font->font, iso, len);
}

gint
e_font_utf8_char_width (EFont *font, EFontStyle style, char *text)
{
    unicode_char_t uni;
    guchar iso;

    g_return_val_if_fail (font != NULL, 0);
    g_return_val_if_fail (text != NULL, 0);

    if (!unicode_get_utf8 (text, &uni)) return 0;

    if ((uni < ' ') || (uni > 255)) uni = ' ';

    iso = uni;

    return gdk_text_width (&font->font, &iso, 1);
}

static const gchar *
translate_encoding (const gchar *encoding)
{
    static GHashTable *eh = NULL;
    gchar e[64];

    if (!eh) {
        eh = g_hash_table_new (g_str_hash, g_str_equal);

        g_hash_table_insert (eh, "iso8859-1", "iso-8859-1");
        g_hash_table_insert (eh, "iso8859-2", "iso-8859-2");
        g_hash_table_insert (eh, "iso8859-3", "iso-8859-3");
        g_hash_table_insert (eh, "iso8859-4", "iso-8859-4");
        g_hash_table_insert (eh, "iso8859-5", "iso-8859-5");
        g_hash_table_insert (eh, "iso8859-6", "iso-8859-6");
        g_hash_table_insert (eh, "iso8859-7", "iso-8859-7");
        g_hash_table_insert (eh, "iso8859-8", "iso-8859-8");
        g_hash_table_insert (eh, "iso8859-9", "iso-8859-9");
        g_hash_table_insert (eh, "iso8859-10", "iso-8859-10");
        g_hash_table_insert (eh, "iso8859-13", "iso-8859-13");
        g_hash_table_insert (eh, "iso8859-14", "iso-8859-14");
        g_hash_table_insert (eh, "iso8859-15", "iso-8859-15");
        g_hash_table_insert (eh, "iso10646-1", "UCS2");
    }

    strncpy (e, encoding, 64);
    g_strdown (e);

    return g_hash_table_lookup (eh, e);
}

const gchar *
e_gdk_font_encoding (GdkFont *font)
{
    Atom font_atom, atom;
    Bool status;
    char *name, *p;
    const gchar *encoding;
    gint i;

    if (!font) return NULL;

    font_atom = gdk_atom_intern ("FONT", FALSE);

    if (font->type == GDK_FONT_FONTSET) {
        XFontStruct **font_structs;
        gint num_fonts;
        gchar **font_names;

        num_fonts = XFontsOfFontSet (GDK_FONT_XFONT (font),
                         &font_structs,
                         &font_names);
        status = XGetFontProperty (font_structs[0],
                       font_atom,
                       &atom);
    } else {
        status = XGetFontProperty (GDK_FONT_XFONT (font),
                       font_atom,
                       &atom);
    }

    if (!status) return NULL;

    name = p = gdk_atom_name (atom);

    for (i = 0; i < 13; i++) {
        /* Skip hyphen */
        while (*p && (*p != '-')) p++;
        if (*p) p++;
    }

#if 0
    p = strchr (name, '-'); /* Foundry */
    p = strchr (p + 1, '-'); /* Family */
    p = strchr (p + 1, '-'); /* Weight */
    p = strchr (p + 1, '-'); /* Slant */
    p = strchr (p + 1, '-'); /* Set Width */
    p = strchr (p + 1, '-'); /* Add Style */
    p = strchr (p + 1, '-'); /* Pixel Size */
    p = strchr (p + 1, '-'); /* Point Size */
    p = strchr (p + 1, '-'); /* Resolution X */
    p = strchr (p + 1, '-'); /* Resolution Y */
    p = strchr (p + 1, '-'); /* Spacing */
    p = strchr (p + 1, '-'); /* Average Width */
    p = strchr (p + 1, '-'); /* Charset */

    encoding = translate_encoding (p + 1);
#else
    if (!*p) return NULL;

    encoding = translate_encoding (p);
#endif

    g_free (name);

    return encoding;
}

unicode_iconv_t
e_uiconv_from_gdk_font (GdkFont *font)
{
    static GHashTable *uh = NULL;
    const gchar *enc;
    unicode_iconv_t uiconv;

    if (!font) return (unicode_iconv_t) -1;

    enc = e_gdk_font_encoding (font);

    if (!enc) return (unicode_iconv_t) -1;

    if (!uh) uh = g_hash_table_new (g_str_hash, g_str_equal);

    uiconv = g_hash_table_lookup (uh, enc);

    if (!uiconv) {
        uiconv = unicode_iconv_open ("UTF-8", enc);
        if (uiconv == (unicode_iconv_t) -1) return uiconv;
        g_hash_table_insert (uh, (gpointer) enc, uiconv);
    }

    return uiconv;
}

unicode_iconv_t
e_uiconv_to_gdk_font (GdkFont *font)
{
    static GHashTable *uh = NULL;
    const gchar *enc;
    unicode_iconv_t uiconv;

    if (!font) return (unicode_iconv_t) -1;

    enc = e_gdk_font_encoding (font);

    if (!enc) return (unicode_iconv_t) -1;

    if (!uh) uh = g_hash_table_new (g_str_hash, g_str_equal);

    uiconv = g_hash_table_lookup (uh, enc);

    if (!uiconv) {
        uiconv = unicode_iconv_open (enc, "UTF-8");
        if (uiconv == (unicode_iconv_t) -1) return uiconv;
        g_hash_table_insert (uh, (gpointer) enc, uiconv);
    }

    return uiconv;
}