]> Dogcows Code - chaz/openbox/blob - render/font.c
cc6fe2875bb62ca6bffb74c928660f90d1ce2fb4
[chaz/openbox] / render / font.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 font.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5 Copyright (c) 2003 Derek Foreman
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "font.h"
21 #include "color.h"
22 #include "mask.h"
23 #include "theme.h"
24 #include "gettext.h"
25
26 #include <X11/Xft/Xft.h>
27 #include <glib.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #define ELIPSES "..."
32 #define ELIPSES_LENGTH(font) \
33 (font->elipses_length + (font->shadow ? font->offset : 0))
34
35 #define OB_SHADOW "shadow"
36 #define OB_SHADOW_OFFSET "shadowoffset"
37 #define OB_SHADOW_ALPHA "shadowtint"
38
39 FcObjectType objs[] = {
40 { OB_SHADOW, FcTypeBool },
41 { OB_SHADOW_OFFSET, FcTypeInteger },
42 { OB_SHADOW_ALPHA, FcTypeInteger }
43 };
44
45 static gboolean started = FALSE;
46
47 static void font_startup(void)
48 {
49 if (!XftInit(0)) {
50 g_warning(_("Couldn't initialize Xft."));
51 exit(EXIT_FAILURE);
52 }
53 FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
54 }
55
56 static void measure_font(RrFont *f)
57 {
58 XGlyphInfo info;
59
60 /* measure an elipses */
61 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
62 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
63 f->elipses_length = (signed) info.xOff;
64 }
65
66 static RrFont *openfont(const RrInstance *inst, gchar *fontstring)
67 {
68 RrFont *out;
69 FcPattern *pat, *match;
70 XftFont *font;
71 FcResult res;
72 gint tint;
73
74 if (!(pat = XftNameParse(fontstring)))
75 return NULL;
76
77 match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
78 FcPatternDestroy(pat);
79 if (!match)
80 return NULL;
81
82 out = g_new(RrFont, 1);
83 out->inst = inst;
84
85 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
86 out->shadow = FALSE;
87
88 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
89 FcResultMatch)
90 out->offset = 1;
91
92 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
93 tint = 25;
94 if (tint > 100) tint = 100;
95 else if (tint < -100) tint = -100;
96 out->tint = tint;
97
98 font = XftFontOpenPattern(RrDisplay(inst), match);
99 if (!font) {
100 FcPatternDestroy(match);
101 g_free(out);
102 return NULL;
103 } else
104 out->xftfont = font;
105
106 measure_font(out);
107
108 return out;
109 }
110
111 RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring)
112 {
113 RrFont *out;
114
115 if (!started) {
116 font_startup();
117 started = TRUE;
118 }
119
120 if ((out = openfont(inst, fontstring)))
121 return out;
122 g_warning(_("Unable to load font: %s\n"), fontstring);
123 g_warning(_("Trying fallback font: %s\n"), "sans");
124
125 if ((out = openfont(inst, "sans")))
126 return out;
127 g_warning(_("Unable to load font: %s\n"), "sans");
128
129 return NULL;
130 }
131
132 void RrFontClose(RrFont *f)
133 {
134 if (f) {
135 XftFontClose(RrDisplay(f->inst), f->xftfont);
136 g_free(f);
137 }
138 }
139
140 static void font_measure_full(const RrFont *f, const gchar *str,
141 gint *x, gint *y)
142 {
143 XGlyphInfo info;
144
145 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
146 (const FcChar8*)str, strlen(str), &info);
147
148 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
149 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
150 }
151
152 gint RrFontMeasureString(const RrFont *f, const gchar *str)
153 {
154 gint x, y;
155 font_measure_full (f, str, &x, &y);
156 return x + 4;
157 }
158
159 gint RrFontHeight(const RrFont *f)
160 {
161 return f->xftfont->ascent + f->xftfont->descent +
162 (f->shadow ? f->offset : 0);
163 }
164
165 gint RrFontMaxCharWidth(const RrFont *f)
166 {
167 return (signed) f->xftfont->max_advance_width;
168 }
169
170 void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *area)
171 {
172 gint x,y,w,h;
173 XftColor c;
174 GString *text;
175 gint mw, mh;
176 size_t l;
177 gboolean shortened = FALSE;
178
179 /* center vertically */
180 y = area->y +
181 (area->height - RrFontHeight(t->font)) / 2;
182 /* the +2 and -4 leave a small blank edge on the sides */
183 x = area->x + 2;
184 w = area->width - 4;
185 h = area->height;
186
187 text = g_string_new(strfry(t->string));
188 l = g_utf8_strlen(text->str, -1);
189 font_measure_full(t->font, text->str, &mw, &mh);
190 while (l && mw > area->width) {
191 shortened = TRUE;
192 /* remove a character from the middle */
193 text = g_string_erase(text, l-- / 2, 1);
194 /* if the elipses are too large, don't show them at all */
195 if (ELIPSES_LENGTH(t->font) > area->width)
196 shortened = FALSE;
197 font_measure_full(t->font, text->str, &mw, &mh);
198 mw += ELIPSES_LENGTH(t->font);
199 }
200 if (shortened) {
201 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
202 l += 3;
203 }
204 if (!l) return;
205
206 switch (t->justify) {
207 case RR_JUSTIFY_LEFT:
208 break;
209 case RR_JUSTIFY_RIGHT:
210 x += (w - mw);
211 break;
212 case RR_JUSTIFY_CENTER:
213 x += (w - mw) / 2;
214 break;
215 }
216
217 l = strlen(text->str); /* number of bytes */
218
219 if (t->font->shadow) {
220 if (t->font->tint >= 0) {
221 c.color.red = 0;
222 c.color.green = 0;
223 c.color.blue = 0;
224 c.color.alpha = 0xffff * t->font->tint / 100;
225 c.pixel = BlackPixel(RrDisplay(t->font->inst),
226 RrScreen(t->font->inst));
227 } else {
228 c.color.red = 0xffff;
229 c.color.green = 0xffff;
230 c.color.blue = 0xffff;
231 c.color.alpha = 0xffff * -t->font->tint / 100;
232 c.pixel = WhitePixel(RrDisplay(t->font->inst),
233 RrScreen(t->font->inst));
234 }
235 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
236 t->font->xftfont->ascent + y + t->font->offset,
237 (FcChar8*)text->str, l);
238 }
239 c.color.red = t->color->r | g_random_int_range(0,255) << 8; //t->color->r << 8;
240 c.color.green = t->color->g | g_random_int_range(0,255) << 8; //t->color->g << 8;
241 c.color.blue = t->color->b | g_random_int_range(0,255) << 8; //t->color->b << 8;
242 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
243 c.pixel = t->color->pixel;
244
245 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
246 t->font->xftfont->ascent + y,
247 (FcChar8*)text->str, l);
248
249 g_string_free(text, TRUE);
250 return;
251 }
This page took 0.048798 seconds and 3 git commands to generate.