]>
Dogcows Code - chaz/openbox/blob - font.c
b9a17b48786c744d62e5a7bcec372288e1660bd5
5 #include "kernel/geom.h"
6 #include "kernel/gettext.h"
7 #define _(str) gettext(str)
9 #include <X11/Xft/Xft.h>
14 #define ELIPSES_LENGTH(font, shadow, offset) \
15 (font->elipses_length + (shadow ? offset : 0))
17 static gboolean started
= FALSE
;
19 static void font_startup(void)
25 g_warning(_("Couldn't initialize Xft.\n"));
29 version
= XftGetVersion();
30 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
31 version
/ 10000 % 100, version
/ 100 % 100, version
% 100,
32 XFT_MAJOR
, XFT_MINOR
, XFT_REVISION
);
36 static void measure_font(RrFont
*f
)
40 /* measure an elipses */
41 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
42 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
43 f
->elipses_length
= (signed) info
.xOff
;
46 RrFont
*RrFontOpen(const RrInstance
*inst
, char *fontstring
)
56 if ((xf
= XftFontOpenName(RrDisplay(inst
), RrScreen(inst
), fontstring
))) {
57 out
= g_new(RrFont
, 1);
63 g_warning(_("Unable to load font: %s\n"), fontstring
);
64 g_warning(_("Trying fallback font: %s\n"), "sans");
66 if ((xf
= XftFontOpenName(RrDisplay(inst
), RrScreen(inst
), "sans"))) {
67 out
= g_new(RrFont
, 1);
73 g_warning(_("Unable to load font: %s\n"), "sans");
78 void RrFontClose(RrFont
*f
)
81 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
86 static void font_measure_full(const RrFont
*f
, const gchar
*str
,
87 gint shadow
, gint offset
, gint
*x
, gint
*y
)
91 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
92 (const FcChar8
*)str
, strlen(str
), &info
);
94 *x
= (signed) info
.xOff
+ (shadow
? ABS(offset
) : 0);
95 *y
= info
.height
+ (shadow
? ABS(offset
) : 0);
98 int RrFontMeasureString(const RrFont
*f
, const gchar
*str
,
99 gint shadow
, gint offset
)
102 font_measure_full (f
, str
, shadow
, offset
, &x
, &y
);
106 int RrFontHeight(const RrFont
*f
, gint shadow
, gint offset
)
108 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+ (shadow
? offset
: 0);
111 int RrFontMaxCharWidth(const RrFont
*f
)
113 return (signed) f
->xftfont
->max_advance_width
;
116 void RrFontDraw(XftDraw
*d
, RrTextureText
*t
, Rect
*area
)
123 gboolean shortened
= FALSE
;
125 /* center vertically */
127 (area
->height
- RrFontHeight(t
->font
, t
->shadow
, t
->offset
)) / 2;
131 text
= g_string_new(t
->string
);
132 l
= g_utf8_strlen(text
->str
, -1);
133 font_measure_full(t
->font
, text
->str
, t
->shadow
, t
->offset
, &mw
, &mh
);
134 while (l
&& mw
> area
->width
) {
136 /* remove a character from the middle */
137 text
= g_string_erase(text
, l
-- / 2, 1);
138 em
= ELIPSES_LENGTH(t
->font
, t
->shadow
, t
->offset
);
139 /* if the elipses are too large, don't show them at all */
140 if (em
> area
->width
)
142 font_measure_full(t
->font
, text
->str
, t
->shadow
, t
->offset
, &mw
, &mh
);
146 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
151 switch (t
->justify
) {
152 case RR_JUSTIFY_LEFT
:
155 case RR_JUSTIFY_RIGHT
:
156 x
= area
->x
+ (w
- mw
);
158 case RR_JUSTIFY_CENTER
:
159 x
= area
->x
+ (w
- mw
) / 2;
163 l
= strlen(text
->str
); /* number of bytes */
170 c
.color
.alpha
= 0xffff * t
->tint
/ 100; /* transparent shadow */
171 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
172 RrScreen(t
->font
->inst
));
174 c
.color
.red
= 0xffff * -t
->tint
/ 100;
175 c
.color
.green
= 0xffff * -t
->tint
/ 100;
176 c
.color
.blue
= 0xffff * -t
->tint
/ 100;
177 c
.color
.alpha
= 0xffff * -t
->tint
/ 100; /* transparent shadow */
178 c
.pixel
= WhitePixel(RrDisplay(t
->font
->inst
),
179 RrScreen(t
->font
->inst
));
181 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
+ t
->offset
,
182 t
->font
->xftfont
->ascent
+ y
+ t
->offset
,
183 (FcChar8
*)text
->str
, l
);
185 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
186 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
187 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
188 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
189 c
.pixel
= t
->color
->pixel
;
191 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
192 t
->font
->xftfont
->ascent
+ y
,
193 (FcChar8
*)text
->str
, l
);
This page took 0.041462 seconds and 3 git commands to generate.