chiark / gitweb /
New option: movres.ignore_transience.
[e16] / dox / ttfont.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
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 "dox.h"
24
25 struct _efont {
26    Imlib_Font          face;
27 };
28
29 static void
30 ImlibSetFgColorFromGC(Display * dpy, GC gc, Colormap cm)
31 {
32    XGCValues           xgcv;
33    XColor              xclr;
34    int                 r, g, b;
35
36    XGetGCValues(dpy, gc, GCForeground, &xgcv);
37    xclr.pixel = xgcv.foreground;
38    XQueryColor(dpy, cm, &xclr);
39    EGetColor(&xclr, &r, &g, &b);
40    imlib_context_set_color(r, g, b, 255);
41 }
42
43 void
44 EFont_draw_string(Display * dpy, Drawable win, GC gc, int x, int y,
45                   const char *text, Efont * f, Visual * vis __UNUSED__,
46                   Colormap cm)
47 {
48    Imlib_Image         im;
49    int                 w, h, ascent, descent, max_asc;
50
51    Efont_extents(f, text, &ascent, &descent, &w, &max_asc, NULL, NULL, NULL);
52    h = ascent + descent;
53
54    imlib_context_set_drawable(win);
55    im = imlib_create_image_from_drawable(0, x, y - ascent, w, h, 0);
56    imlib_context_set_image(im);
57
58    imlib_context_set_font(f->face);
59    ImlibSetFgColorFromGC(dpy, gc, cm);
60    imlib_text_draw(0, ascent - max_asc, text);
61    imlib_render_image_on_drawable(x, y - ascent);
62
63    imlib_free_image();
64 }
65
66 void
67 Efont_free(Efont * f)
68 {
69    if (!f)
70       return;
71
72    imlib_context_set_font(f->face);
73    imlib_free_font();
74
75    Efree(f);
76 }
77
78 Efont              *
79 Efont_load(const char *file, int size)
80 {
81    static char         fp_set = 0;
82    char                s[4096];
83    Efont              *f;
84    Imlib_Font          ff;
85
86    if (!fp_set)
87      {
88         imlib_add_path_to_font_path(docdir);
89         sprintf(s, "%s/../ttfonts", docdir);
90         imlib_add_path_to_font_path(s);
91         sprintf(s, "%s/fonts", ENLIGHTENMENT_ROOT);
92         imlib_add_path_to_font_path(s);
93         fp_set = 1;
94      }
95
96    Esnprintf(s, sizeof(s), "%s/%d", file, size);
97    ff = imlib_load_font(s);
98    if (ff == NULL)
99       return NULL;
100
101    f = EMALLOC(Efont, 1);
102    f->face = ff;
103
104    return f;
105 }
106
107 void
108 Efont_extents(Efont * f, const char *text, int *font_ascent_return,
109               int *font_descent_return, int *width_return,
110               int *max_ascent_return, int *max_descent_return,
111               int *lbearing_return __UNUSED__, int *rbearing_return __UNUSED__)
112 {
113    int                 w, h;
114
115    if (!f)
116       return;
117
118    imlib_context_set_font(f->face);
119    imlib_get_text_advance(text, &w, &h);
120    if (width_return)
121       *width_return = w;
122    if (font_ascent_return)
123       *font_ascent_return = imlib_get_font_ascent();
124    if (font_descent_return)
125       *font_descent_return = imlib_get_font_descent();
126    if (max_ascent_return)
127       *max_ascent_return = imlib_get_maximum_font_ascent();
128    if (max_descent_return)
129       *max_descent_return = imlib_get_maximum_font_descent();
130 }
131
132 #if TEST_TTFONT
133
134 #undef XSync
135
136 Display            *disp;
137
138 int
139 main(int argc, char **argv)
140 {
141    Efont              *f;
142    GC                  gc;
143    XGCValues           gcv;
144    Window              win;
145    int                 i, j;
146
147    disp = XOpenDisplay(NULL);
148
149    imlib_context_set_display(disp);
150    imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
151    imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
152
153    srand(time(NULL));
154    win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 640, 480, 0,
155                              0, 0);
156    XMapWindow(disp, win);
157    XSync(disp, False);
158
159    gcv.subwindow_mode = IncludeInferiors;
160    gc = XCreateGC(disp, win, GCSubwindowMode, &gcv);
161    for (;; j++)
162      {
163         for (i = 3; i < argc; i++)
164           {
165              XSetForeground(disp, gc, rand() << 16 | rand());
166              f = Efont_load(argv[i], atoi(argv[1]));
167              if (f)
168                 EFont_draw_string(disp, win, gc, 20,
169                                   atoi(argv[1]) * (i - 2), argv[2], f,
170                                   DefaultVisual(disp, DefaultScreen(disp)),
171                                   DefaultColormap(disp, DefaultScreen(disp)));
172              Efont_free(f);
173              f = NULL;
174           }
175      }
176    return 0;
177 }
178 #endif