]>
Dogcows Code - chaz/openbox/blob - render/font.c
3 #include "kernel/geom.h"
4 #include "kernel/gettext.h"
5 #define _(str) gettext(str)
7 #include <X11/Xft/Xft.h>
12 #define ELIPSES_LENGTH(font, shadow, offset) \
13 (font->elipses_length + (shadow ? offset : 0))
15 void font_startup(void)
21 g_warning(_("Couldn't initialize Xft.\n"));
25 version
= XftGetVersion();
26 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
27 version
/ 10000 % 100, version
/ 100 % 100, version
% 100,
28 XFT_MAJOR
, XFT_MINOR
, XFT_REVISION
);
32 static void measure_height(RrFont
*f
)
36 /* measure an elipses */
37 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
38 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
39 f
->elipses_length
= (signed) info
.xOff
;
42 RrFont
*font_open(const RrInstance
*inst
, char *fontstring
)
47 if ((xf
= XftFontOpenName(RrDisplay(inst
), RrScreen(inst
), fontstring
))) {
48 out
= g_new(RrFont
, 1);
54 g_warning(_("Unable to load font: %s\n"), fontstring
);
55 g_warning(_("Trying fallback font: %s\n"), "sans");
57 if ((xf
= XftFontOpenName(RrDisplay(inst
), RrScreen(inst
), "sans"))) {
58 out
= g_new(RrFont
, 1);
64 g_warning(_("Unable to load font: %s\n"), "sans");
65 g_warning(_("Aborting!.\n"));
67 exit(3); /* can't continue without a font */
70 void font_close(RrFont
*f
)
73 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
78 void font_measure_full(RrFont
*f
, char *str
, int shadow
, int offset
,
83 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
84 (FcChar8
*)str
, strlen(str
), &info
);
86 *x
= (signed) info
.xOff
+ (shadow
? ABS(offset
) : 0);
87 *y
= info
.height
+ (shadow
? ABS(offset
) : 0);
90 int font_measure_string(RrFont
*f
, char *str
, int shadow
, int offset
)
93 font_measure_full (f
, str
, shadow
, offset
, &x
, &y
);
97 int font_height(RrFont
*f
, int shadow
, int offset
)
99 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+ (shadow
? offset
: 0);
102 int font_max_char_width(RrFont
*f
)
104 return (signed) f
->xftfont
->max_advance_width
;
107 void font_draw(XftDraw
*d
, RrTextureText
*t
, Rect
*area
)
114 gboolean shortened
= FALSE
;
116 /* center vertically */
118 (area
->height
- font_height(t
->font
, t
->shadow
, t
->offset
)) / 2;
122 text
= g_string_new(t
->string
);
123 l
= g_utf8_strlen(text
->str
, -1);
124 font_measure_full(t
->font
, text
->str
, t
->shadow
, t
->offset
, &mw
, &mh
);
125 while (l
&& mw
> area
->width
) {
127 /* remove a character from the middle */
128 text
= g_string_erase(text
, l
-- / 2, 1);
129 em
= ELIPSES_LENGTH(t
->font
, t
->shadow
, t
->offset
);
130 /* if the elipses are too large, don't show them at all */
131 if (em
> area
->width
)
133 font_measure_full(t
->font
, text
->str
, t
->shadow
, t
->offset
, &mw
, &mh
);
137 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
142 switch (t
->justify
) {
143 case RR_JUSTIFY_LEFT
:
146 case RR_JUSTIFY_RIGHT
:
147 x
= area
->x
+ (w
- mw
);
149 case RR_JUSTIFY_CENTER
:
150 x
= area
->x
+ (w
- mw
) / 2;
154 l
= strlen(text
->str
); /* number of bytes */
161 c
.color
.alpha
= 0xffff * t
->tint
/ 100; /* transparent shadow */
162 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
163 RrScreen(t
->font
->inst
));
165 c
.color
.red
= 0xffff * -t
->tint
/ 100;
166 c
.color
.green
= 0xffff * -t
->tint
/ 100;
167 c
.color
.blue
= 0xffff * -t
->tint
/ 100;
168 c
.color
.alpha
= 0xffff * -t
->tint
/ 100; /* transparent shadow */
169 c
.pixel
= WhitePixel(RrDisplay(t
->font
->inst
),
170 RrScreen(t
->font
->inst
));
172 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
+ t
->offset
,
173 t
->font
->xftfont
->ascent
+ y
+ t
->offset
,
174 (FcChar8
*)text
->str
, l
);
176 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
177 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
178 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
179 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
180 c
.pixel
= t
->color
->pixel
;
182 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
183 t
->font
->xftfont
->ascent
+ y
,
184 (FcChar8
*)text
->str
, l
);
This page took 0.042872 seconds and 4 git commands to generate.