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