aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-font.c
blob: a13f0b962c49de8af834b7db20a15ad775eb2553 (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
#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 <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;

    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;

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

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

    iso = uni;

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