]> icculus.org git repositories - mikachu/openbox.git/blob - render/font.c
clean up comments regarding alpha
[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"));
16         exit(3);
17     }
18 #ifdef DEBUG
19     version = XftGetVersion();
20     g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
21               version / 10000 % 100, version / 100 % 100, version % 100,
22               XFT_MAJOR, XFT_MINOR, XFT_REVISION);
23 #endif
24 }
25
26 static void measure_height(ObFont *f)
27 {
28     XGlyphInfo info;
29     char *str;
30
31     /* XXX add some extended UTF8 characters in here? */
32     str = "12345678900-qwertyuiopasdfghjklzxcvbnm"
33         "!@#$%^&*()_+QWERTYUIOPASDFGHJKLZXCVBNM"
34         "`~[]\\;',./{}|:\"<>?";
35
36     XftTextExtentsUtf8(ob_display, f->xftfont,
37                        (FcChar8*)str, strlen(str), &info);
38     f->height = (signed) info.height;
39 }
40
41 ObFont *font_open(char *fontstring)
42 {
43     ObFont *out;
44     XftFont *xf;
45     
46     if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
47         out = malloc(sizeof(ObFont));
48         out->xftfont = xf;
49         measure_height(out);
50         return out;
51     }
52     g_warning(_("Unable to load font: %s\n"), fontstring);
53     g_warning(_("Trying fallback font: %s\n"), "fixed");
54
55     if ((xf = XftFontOpenName(ob_display, ob_screen, "fixed"))) {
56         out = malloc(sizeof(ObFont));
57         out->xftfont = xf;
58         measure_height(out);
59         return out;
60     }
61     g_warning(_("Unable to load font: %s\n"), "fixed");
62     g_warning(_("Aborting!.\n"));
63
64     exit(3); // can't continue without a font
65 }
66
67 void font_close(ObFont *f)
68 {
69     XftFontClose(ob_display, f->xftfont);
70 }
71
72 int font_measure_string(ObFont *f, const char *str, int shadow, int offset)
73 {
74     XGlyphInfo info;
75
76     XftTextExtentsUtf8(ob_display, f->xftfont,
77                        (FcChar8*)str, strlen(str), &info);
78
79     return (signed) info.xOff + (shadow ? offset : 0);
80 }
81
82 int font_height(ObFont *f, int shadow, int offset)
83 {
84     return f->height + (shadow ? offset : 0);
85 }
86
87 int font_max_char_width(ObFont *f)
88 {
89     return (signed) f->xftfont->max_advance_width;
90 }
91
92 void font_draw(XftDraw *d, TextureText *t, int x, int y, int w, int h)
93 {
94     XftColor c;
95
96     /* accomidate for areas bigger/smaller than Xft thinks the font is tall */
97     y += (h - t->font->xftfont->height) / 2;
98
99     x += 3; /* XXX figure out X with justification */
100
101     if (t->shadow) {
102         c.color.red = 0;
103         c.color.green = 0;
104         c.color.blue = 0;
105         c.color.alpha = t->tint | t->tint << 8; /* transparent shadow */
106         c.pixel = BlackPixel(ob_display, ob_screen);
107   
108         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
109                           t->font->xftfont->ascent + y + t->offset,
110                           (FcChar8*)t->string, strlen(t->string));
111     }  
112     c.color.red = t->color->r | t->color->r << 8;
113     c.color.green = t->color->g | t->color->g << 8;
114     c.color.blue = t->color->b | t->color->b << 8;
115     c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
116     c.pixel = t->color->pixel;
117                      
118     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
119                       t->font->xftfont->ascent + y -
120                       (t->font->xftfont->height - t->font->height) / 2,
121                      (FcChar8*)t->string, strlen(t->string));
122     return;
123 }