]>
Dogcows Code - chaz/openbox/blob - render/font.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 font.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5 Copyright (c) 2003 Derek Foreman
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
26 #include <X11/Xft/Xft.h>
32 #define ELIPSES_LENGTH(font) \
33 (font->elipses_length + (font->shadow ? font->offset : 0))
35 #define OB_SHADOW "shadow"
36 #define OB_SHADOW_OFFSET "shadowoffset"
37 #define OB_SHADOW_ALPHA "shadowtint"
39 FcObjectType objs
[] = {
40 { OB_SHADOW
, FcTypeBool
},
41 { OB_SHADOW_OFFSET
, FcTypeInteger
},
42 { OB_SHADOW_ALPHA
, FcTypeInteger
}
45 static gboolean started
= FALSE
;
47 static void font_startup(void)
50 g_warning(_("Couldn't initialize Xft."));
53 FcNameRegisterObjectTypes(objs
, (sizeof(objs
) / sizeof(objs
[0])));
56 static void measure_font(RrFont
*f
)
60 /* measure an elipses */
61 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
62 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
63 f
->elipses_length
= (signed) info
.xOff
;
66 static RrFont
*openfont(const RrInstance
*inst
, gchar
*fontstring
)
69 FcPattern
*pat
, *match
;
74 if (!(pat
= XftNameParse(fontstring
)))
77 match
= XftFontMatch(RrDisplay(inst
), RrScreen(inst
), pat
, &res
);
78 FcPatternDestroy(pat
);
82 out
= g_new(RrFont
, 1);
85 if (FcPatternGetBool(match
, OB_SHADOW
, 0, &out
->shadow
) != FcResultMatch
)
88 if (FcPatternGetInteger(match
, OB_SHADOW_OFFSET
, 0, &out
->offset
) !=
92 if (FcPatternGetInteger(match
, OB_SHADOW_ALPHA
, 0, &tint
) != FcResultMatch
)
94 if (tint
> 100) tint
= 100;
95 else if (tint
< -100) tint
= -100;
98 font
= XftFontOpenPattern(RrDisplay(inst
), match
);
100 FcPatternDestroy(match
);
111 RrFont
*RrFontOpen(const RrInstance
*inst
, gchar
*fontstring
)
120 if ((out
= openfont(inst
, fontstring
)))
122 g_warning(_("Unable to load font: %s\n"), fontstring
);
123 g_warning(_("Trying fallback font: %s\n"), "sans");
125 if ((out
= openfont(inst
, "sans")))
127 g_warning(_("Unable to load font: %s\n"), "sans");
132 void RrFontClose(RrFont
*f
)
135 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
140 static void font_measure_full(const RrFont
*f
, const gchar
*str
,
145 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
146 (const FcChar8
*)str
, strlen(str
), &info
);
148 *x
= (signed) info
.xOff
+ (f
->shadow
? ABS(f
->offset
) : 0);
149 *y
= info
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
152 gint
RrFontMeasureString(const RrFont
*f
, const gchar
*str
)
155 font_measure_full (f
, str
, &x
, &y
);
159 gint
RrFontHeight(const RrFont
*f
)
161 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+
162 (f
->shadow
? f
->offset
: 0);
165 gint
RrFontMaxCharWidth(const RrFont
*f
)
167 return (signed) f
->xftfont
->max_advance_width
;
170 void RrFontDraw(XftDraw
*d
, RrTextureText
*t
, RrRect
*area
)
177 gboolean shortened
= FALSE
;
179 /* center vertically */
181 (area
->height
- RrFontHeight(t
->font
)) / 2;
182 /* the +2 and -4 leave a small blank edge on the sides */
187 text
= g_string_new(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
) {
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
)
197 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
198 mw
+= ELIPSES_LENGTH(t
->font
);
201 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
206 switch (t
->justify
) {
207 case RR_JUSTIFY_LEFT
:
209 case RR_JUSTIFY_RIGHT
:
212 case RR_JUSTIFY_CENTER
:
217 l
= strlen(text
->str
); /* number of bytes */
219 if (t
->font
->shadow
) {
220 if (t
->font
->tint
>= 0) {
224 c
.color
.alpha
= 0xffff * t
->font
->tint
/ 100;
225 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
226 RrScreen(t
->font
->inst
));
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
));
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
);
239 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
240 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
241 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
242 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
243 c
.pixel
= t
->color
->pixel
;
245 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
246 t
->font
->xftfont
->ascent
+ y
,
247 (FcChar8
*)text
->str
, l
);
249 g_string_free(text
, TRUE
);
This page took 0.045889 seconds and 4 git commands to generate.