]>
Dogcows Code - chaz/openbox/blob - render/font.c
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) \
15 (font->elipses_length + (font->shadow ? font->offset : 0))
17 #define OB_SHADOW "shadow"
18 #define OB_SHADOW_OFFSET "shadowoffset"
19 #define OB_SHADOW_ALPHA "shadowtint"
21 FcObjectType objs
[] = {
22 { OB_SHADOW
, FcTypeBool
},
23 { OB_SHADOW_OFFSET
, FcTypeInteger
},
24 { OB_SHADOW_ALPHA
, FcTypeInteger
}
27 static gboolean started
= FALSE
;
29 static void font_startup(void)
32 g_warning(_("Couldn't initialize Xft.\n"));
35 FcNameRegisterObjectTypes(objs
, (sizeof(objs
) / sizeof(objs
[0])));
38 static void measure_font(RrFont
*f
)
42 /* measure an elipses */
43 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
44 (FcChar8
*)ELIPSES
, strlen(ELIPSES
), &info
);
45 f
->elipses_length
= (signed) info
.xOff
;
48 static RrFont
*openfont(const RrInstance
*inst
, char *fontstring
)
51 FcPattern
*pat
, *match
;
56 if (!(pat
= XftNameParse(fontstring
)))
59 match
= XftFontMatch(RrDisplay(inst
), RrScreen(inst
), pat
, &res
);
60 FcPatternDestroy(pat
);
64 out
= g_new(RrFont
, 1);
67 if (FcPatternGetBool(match
, OB_SHADOW
, 0, &out
->shadow
) != FcResultMatch
)
69 g_message("shadow %d", out
->shadow
);
71 if (FcPatternGetInteger(match
, OB_SHADOW_OFFSET
, 0, &out
->offset
) !=
75 if (FcPatternGetInteger(match
, OB_SHADOW_ALPHA
, 0, &tint
) != FcResultMatch
)
77 if (tint
> 100) tint
= 100;
78 else if (tint
< -100) tint
= -100;
81 font
= XftFontOpenPattern(RrDisplay(inst
), match
);
93 RrFont
*RrFontOpen(const RrInstance
*inst
, char *fontstring
)
102 if ((out
= openfont(inst
, fontstring
)))
104 g_warning(_("Unable to load font: %s\n"), fontstring
);
105 g_warning(_("Trying fallback font: %s\n"), "sans");
107 if ((out
= openfont(inst
, "sans")))
109 g_warning(_("Unable to load font: %s\n"), "sans");
114 void RrFontClose(RrFont
*f
)
117 XftFontClose(RrDisplay(f
->inst
), f
->xftfont
);
122 static void font_measure_full(const RrFont
*f
, const gchar
*str
,
127 XftTextExtentsUtf8(RrDisplay(f
->inst
), f
->xftfont
,
128 (const FcChar8
*)str
, strlen(str
), &info
);
130 *x
= (signed) info
.xOff
+ (f
->shadow
? ABS(f
->offset
) : 0);
131 *y
= info
.height
+ (f
->shadow
? ABS(f
->offset
) : 0);
134 int RrFontMeasureString(const RrFont
*f
, const gchar
*str
)
137 font_measure_full (f
, str
, &x
, &y
);
141 int RrFontHeight(const RrFont
*f
)
143 return f
->xftfont
->ascent
+ f
->xftfont
->descent
+
144 (f
->shadow
? f
->offset
: 0);
147 int RrFontMaxCharWidth(const RrFont
*f
)
149 return (signed) f
->xftfont
->max_advance_width
;
152 void RrFontDraw(XftDraw
*d
, RrTextureText
*t
, Rect
*area
)
159 gboolean shortened
= FALSE
;
161 /* center vertically */
163 (area
->height
- RrFontHeight(t
->font
)) / 2;
164 /* the +2 and -4 leave a small blank edge on the sides */
169 text
= g_string_new(t
->string
);
170 l
= g_utf8_strlen(text
->str
, -1);
171 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
172 while (l
&& mw
> area
->width
) {
174 /* remove a character from the middle */
175 text
= g_string_erase(text
, l
-- / 2, 1);
176 em
= ELIPSES_LENGTH(t
->font
);
177 /* if the elipses are too large, don't show them at all */
178 if (em
> area
->width
)
180 font_measure_full(t
->font
, text
->str
, &mw
, &mh
);
184 text
= g_string_insert(text
, (l
+ 1) / 2, ELIPSES
);
189 switch (t
->justify
) {
190 case RR_JUSTIFY_LEFT
:
192 case RR_JUSTIFY_RIGHT
:
195 case RR_JUSTIFY_CENTER
:
200 l
= strlen(text
->str
); /* number of bytes */
202 if (t
->font
->shadow
) {
203 if (t
->font
->tint
>= 0) {
207 c
.color
.alpha
= 0xffff * t
->font
->tint
/ 100;
208 c
.pixel
= BlackPixel(RrDisplay(t
->font
->inst
),
209 RrScreen(t
->font
->inst
));
211 c
.color
.red
= 0xffff;
212 c
.color
.green
= 0xffff;
213 c
.color
.blue
= 0xffff;
214 c
.color
.alpha
= 0xffff * -t
->font
->tint
/ 100;
215 c
.pixel
= WhitePixel(RrDisplay(t
->font
->inst
),
216 RrScreen(t
->font
->inst
));
218 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
+ t
->font
->offset
,
219 t
->font
->xftfont
->ascent
+ y
+ t
->font
->offset
,
220 (FcChar8
*)text
->str
, l
);
222 c
.color
.red
= t
->color
->r
| t
->color
->r
<< 8;
223 c
.color
.green
= t
->color
->g
| t
->color
->g
<< 8;
224 c
.color
.blue
= t
->color
->b
| t
->color
->b
<< 8;
225 c
.color
.alpha
= 0xff | 0xff << 8; /* fully opaque text */
226 c
.pixel
= t
->color
->pixel
;
228 XftDrawStringUtf8(d
, &c
, t
->font
->xftfont
, x
,
229 t
->font
->xftfont
->ascent
+ y
,
230 (FcChar8
*)text
->str
, l
);
This page took 0.043389 seconds and 4 git commands to generate.