]> icculus.org git repositories - mikachu/openbox.git/blob - render/font.c
paint gets more parameters
[mikachu/openbox.git] / render / font.c
1 #include <stdlib.h>
2 #include <X11/Xft/Xft.h>
3 #include "../kernel/openbox.h"
4 #include "font.h"
5
6 #include "../src/gettext.h"
7 #define _(str) gettext(str)
8
9 void font_startup(void)
10 {
11 #ifdef DEBUG
12     int version;
13 #endif /* DEBUG */
14     if (!XftInit(0)) {
15         g_warning(_("Couldn't initialize Xft.\n\n"));
16         exit(3);
17     }
18 #ifdef DEBUG
19     version = XftGetVersion();
20     g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).\n",
21               version / 10000 % 100, version / 100 % 100, version % 100,
22               XFT_MAJOR, XFT_MINOR, XFT_REVISION);
23 #endif
24 }
25
26 ObFont *font_open(char *fontstring)
27 {
28     ObFont *out;
29     XftFont *xf;
30     
31     if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
32         out = malloc(sizeof(ObFont));
33         out->xftfont = xf;
34         return out;
35     }
36     g_warning(_("Unable to load font: %s\n"), fontstring);
37     g_warning(_("Trying fallback font: %s\n"), "fixed");
38
39     if ((xf = XftFontOpenName(ob_display, ob_screen, "fixed"))) {
40         out = malloc(sizeof(ObFont));
41         out->xftfont = xf;
42         return out;
43     }
44     g_warning(_("Unable to load font: %s\n"), "fixed");
45     g_warning(_("Aborting!.\n"));
46
47     exit(3); // can't continue without a font
48 }
49
50 void font_close(ObFont *f)
51 {
52     XftFontClose(ob_display, f->xftfont);
53 }
54
55 int font_measure_string(ObFont *f, const char *str, int shadow, int offset)
56 {
57     XGlyphInfo info;
58
59     XftTextExtentsUtf8(ob_display, f->xftfont,
60                        (FcChar8*)str, strlen(str), &info);
61
62     return (signed) info.xOff + (shadow ? offset : 0);
63 }
64
65 int font_height(ObFont *f, int shadow, int offset)
66 {
67     return (signed) f->xftfont->height + (shadow ? offset : 0);
68 }
69
70 int font_max_char_width(ObFont *f)
71 {
72     return (signed) f->xftfont->max_advance_width;
73 }
74
75 void font_draw(XftDraw *d, TextureText *t)
76 {
77     int x = 0, y = 0;
78     XftColor c;
79     if (t->shadow) {
80         c.color.red = 0;
81         c.color.green = 0;
82         c.color.blue = 0;
83         c.color.alpha = t->tint | t->tint << 8; // transparent shadow
84         c.pixel = BlackPixel(ob_display, ob_screen);
85   
86         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
87                           t->font->xftfont->ascent + y + t->offset,
88                           (FcChar8*)t->string, strlen(t->string));
89     }  
90     c.color.red = t->color->r | t->color->r << 8;
91     c.color.green = t->color->g | t->color->g << 8;
92     c.color.blue = t->color->b | t->color->b << 8;
93     c.pixel = t->color->pixel;
94     c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
95                      
96     XftDrawStringUtf8(d, &c, t->font->xftfont, x, t->font->xftfont->ascent + y,
97                      (FcChar8*)t->string, strlen(t->string));
98     return;
99 }